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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2869,6 +2869,10 @@ static bool addTypeCoerceFixit(InFlightDiagnostic &diag, ConstraintSystem &CS,
Type fromType, Type toType, Expr *expr) {
// Look through optional types; casts can add them, but can't remove extra
// ones.
bool bothOptional =
fromType->getOptionalObjectType() && toType->getOptionalObjectType();
if (bothOptional)
fromType = fromType->getOptionalObjectType();
toType = toType->lookThroughAllOptionalTypes();

CheckedCastKind Kind = CS.getTypeChecker().typeCheckCheckedCast(
Expand All @@ -2877,9 +2881,11 @@ static bool addTypeCoerceFixit(InFlightDiagnostic &diag, ConstraintSystem &CS,
if (Kind != CheckedCastKind::Unresolved) {
SmallString<32> buffer;
llvm::raw_svector_ostream OS(buffer);
toType->print(OS);
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.

diag.fixItInsert(
Lexer::getLocForEndOfToken(CS.DC->getASTContext().SourceMgr,
expr->getEndLoc()),
Expand Down
26 changes: 15 additions & 11 deletions test/Constraints/bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,30 +276,34 @@ func rdar19831698() {
// <rdar://problem/19836341> Incorrect fixit for NSString? to String? conversions
func rdar19836341(_ ns: NSString?, vns: NSString?) {
var vns = vns
let _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}
var _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}
// FIXME: there should be a fixit appending "as String?" to the line; for now
// it's sufficient that it doesn't suggest appending "as String"
let _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{22-22= as String?}}
var _: String? = ns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{22-22= as String?}}

// Important part about below diagnostic is that from-type is described as
// 'NSString?' and not '@lvalue NSString?':
let _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}
var _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}
let _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{23-23= as String?}}
var _: String? = vns // expected-error{{cannot convert value of type 'NSString?' to specified type 'String?'}}{{23-23= as String?}}

vns = ns
}

// <rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!"
func rdar20029786(_ ns: NSString?) {
var s: String = ns ?? "str" as String as String // expected-error{{cannot convert value of type 'NSString?' to expected argument type 'String?'}}
var s2 = ns ?? "str" as String as String // expected-error {{cannot convert value of type 'String' to expected argument type 'NSString'}}
var s: String = ns ?? "str" as String as String // expected-error{{cannot convert value of type 'NSString?' to expected argument type 'String?'}}{{21-21= as String?}}
var s2 = ns ?? "str" as String as String // expected-error {{cannot convert value of type 'String' to expected argument type 'NSString'}}{{43-43= as NSString}}

let s3: NSString? = "str" as String? // expected-error {{cannot convert value of type 'String?' to specified type 'NSString?'}}
let s3: NSString? = "str" as String? // expected-error {{cannot convert value of type 'String?' to specified type 'NSString?'}}{{39-39= as NSString?}}

var s4: String = ns ?? "str" // expected-error{{cannot convert value of type 'NSString' to specified type 'String'}}
var s4: String = ns ?? "str" // expected-error{{cannot convert value of type 'NSString' to specified type 'String'}}{{31-31= as String}}
var s5: String = (ns ?? "str") as String // fixed version
}

// Make sure more complicated cast has correct parenthesization
func castMoreComplicated(anInt: Int?) {
let _: (NSObject & NSCopying)? = anInt // expected-error{{cannot convert value of type 'Int?' to specified type '(NSObject & NSCopying)?'}}{{41-41= as (NSObject & NSCopying)?}}
}


// <rdar://problem/19813772> QoI: Using as! instead of as in this case produces really bad diagnostic
func rdar19813772(_ nsma: NSMutableArray) {
var a1 = nsma as! Array // expected-error{{generic parameter 'Element' could not be inferred in cast to 'Array'}} expected-note {{explicitly specify the generic arguments to fix this issue}} {{26-26=<Any>}}
Expand Down
2 changes: 1 addition & 1 deletion test/FixCode/fixits-apply-objc.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func foo1(_ an : Any) {
}

func foo2(_ messageData: Any?) -> AnyObject? {
return messageData as AnyObject
return messageData as AnyObject?
}