Skip to content

Fixit enhancement for migration needs (3.0) #4411

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 2 commits into from
Aug 19, 2016
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ NOTE(in_cast_expr_types,none,
"in cast from type %0 to %1",
(Type, Type))

ERROR(types_not_convertible_use_bool_value,none,
"%0 is not convertible to %1; did you mean %0.boolValue", (Type, Type))

ERROR(tuple_types_not_convertible_nelts,none,
"%0 is not convertible to %1, "
"tuples have a different number of elements", (Type, Type))
Expand Down
23 changes: 23 additions & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,27 @@ bool FailureDiagnosis::diagnoseGeneralConversionFailure(Constraint *constraint){
}
return true;
}

// Due to migration reasons, types used to conform to BooleanType, which
// contain a member var 'boolValue', now does not convert to Bool. This block
// tries to add a specific diagnosis/fixit to explicitly invoke 'boolValue'.
if (toType->isBool()) {
auto LookupResult = CS->TC.lookupMember(CS->DC, fromType,
DeclName(CS->TC.Context.getIdentifier("boolValue")));
if (!LookupResult.empty()) {
if (isa<VarDecl>(LookupResult.begin()->Decl)) {
if (anchor->canAppendCallParentheses())
diagnose(anchor->getLoc(), diag::types_not_convertible_use_bool_value,
fromType, toType).fixItInsertAfter(anchor->getEndLoc(),
".boolValue");
else
diagnose(anchor->getLoc(), diag::types_not_convertible_use_bool_value,
fromType, toType).fixItInsert(anchor->getStartLoc(), "(").
fixItInsertAfter(anchor->getEndLoc(), ").boolValue");
return true;
}
}
}

diagnose(anchor->getLoc(), diag::types_not_convertible,
constraint->getKind() == ConstraintKind::Subtype,
Expand Down Expand Up @@ -4026,6 +4047,8 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
case CTP_DictionaryKey:
case CTP_DictionaryValue:
case CTP_AssignSource:
case CTP_Initialization:
case CTP_ReturnStmt:
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
KnownProtocolKind::ExpressibleByIntegerLiteral,
expr) ||
Expand Down
8 changes: 8 additions & 0 deletions test/FixCode/fixits-apply-objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ func foo(an : Any) {
let a3 : AnyObject!
a3 = an
}

func foo1(_ an : Any) {
let obj: AnyObject = an
}

func foo2(_ messageData: Any?) -> AnyObject? {
return messageData
}
8 changes: 8 additions & 0 deletions test/FixCode/fixits-apply-objc.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ func foo(an : Any) {
let a3 : AnyObject!
a3 = an as AnyObject!
}

func foo1(_ an : Any) {
let obj: AnyObject = an as AnyObject
}

func foo2(_ messageData: Any?) -> AnyObject? {
return messageData as AnyObject?
}
11 changes: 10 additions & 1 deletion test/FixCode/fixits-apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func baz(var x: Int) {
x += 10
}
func foo(let y: String, inout x: Int) {

}

struct Test1 : OptionSet {
Expand Down Expand Up @@ -261,3 +261,12 @@ func disable_unnamed_param_reorder(p: Int, _: String) {}
disable_unnamed_param_reorder(0, "") // no change.

prefix operator ***** {}

class BoolFoo : BooleanType {
var boolValue: Bool {return false}
}
func testBoolValue(a : BoolFoo) {
if a { }
guard a {}
if a as BoolFoo {}
}
13 changes: 11 additions & 2 deletions test/FixCode/fixits-apply.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct MyMask : OptionSet {
}

func supported() -> MyMask {
return MyMask.Bingo
return MyMask(rawValue: UInt(MyMask.Bingo))
}

struct MyEventMask2 : OptionSet {
Expand Down Expand Up @@ -121,7 +121,7 @@ func baz(x: Int) {
x += 10
}
func foo(y: String, x: inout Int) {

}

struct Test1 : OptionSet {
Expand Down Expand Up @@ -264,3 +264,12 @@ func disable_unnamed_param_reorder(p: Int, _: String) {}
disable_unnamed_param_reorder(0, "") // no change.

prefix operator *****

class BoolFoo : Bool {
var boolValue: Bool {return false}
}
func testBoolValue(a : BoolFoo) {
if a.boolValue { }
guard a.boolValue {}
if (a as BoolFoo).boolValue {}
}