Skip to content

[Sema] Improve type coercion fixits for optional-to-optional conversions. #18757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 16, 2018

Conversation

gregomni
Copy link
Contributor

This is a followup to @xedin's comments in #18324 about the less than ideal fixits for coercions between optionals. If from and to are both optional, and we could coerce the non-optional types to each other, suggest as T? instead of as! T.

@gregomni gregomni requested a review from xedin August 16, 2018 16:47
@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test.

@xedin
Copy link
Contributor

xedin commented Aug 16, 2018

LGTM!

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test and merge.

@@ -2883,7 +2887,8 @@ static bool addTypeCoerceFixit(InFlightDiagnostic &diag, ConstraintSystem &CS,
diag.fixItInsert(
Lexer::getLocForEndOfToken(CS.DC->getASTContext().SourceMgr,
expr->getEndLoc()),
(llvm::Twine(canUseAs ? " as " : " as! ") + OS.str()).str());
(llvm::Twine(canUseAs ? " as " : " as! ") + OS.str() +
llvm::Twine(canUseAs && bothOptional ? "?" : "")).str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little iffy; not all types can just have ? stuck on the end. (Consider () -> Void.) It'd be better to add one layer back in the both-optional case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait but both types already have to be optional to produce this fix, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then it was stripped the optional out (both for the coerce check and then for the type producing the fixit).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry what I mean is - if both types were optional before they could be again right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't () -> Void just need parens around it to be optional e.g. (() -> Void)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Before Greg was adding the ? manually, but I pointed out that sometimes that's not good enough. So now he's rebuilding the optional type and having it print itself.

Copy link
Contributor

@xedin xedin Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry for the noise, I totally misunderstood...

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test.

bool canUseAs = Kind == CheckedCastKind::Coercion ||
Kind == CheckedCastKind::BridgingCoercion;
if (bothOptional && canUseAs)
toType = OptionalType::get(toType);
toType->print(OS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…and then it's worth adding a test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Do you have any suggestions for a more complex test case? Function types can't actually be coerced this way (they end up as CheckedCastKind::ValueCast)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocol types might be good enough? someOptionalInt as (NSObject & NSCopying)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. That works.

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test.

@gregomni gregomni merged commit ec9d114 into swiftlang:master Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants