Skip to content

Commit fe392de

Browse files
author
ematejska
authored
Merge pull request #4411 from nkcsgexi/fixit-enhancement-3.0
Fixit enhancement for migration needs (3.0)
2 parents c633ea6 + 5fcc702 commit fe392de

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ NOTE(in_cast_expr_types,none,
681681
"in cast from type %0 to %1",
682682
(Type, Type))
683683

684+
ERROR(types_not_convertible_use_bool_value,none,
685+
"%0 is not convertible to %1; did you mean %0.boolValue", (Type, Type))
686+
684687
ERROR(tuple_types_not_convertible_nelts,none,
685688
"%0 is not convertible to %1, "
686689
"tuples have a different number of elements", (Type, Type))

lib/Sema/CSDiag.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,27 @@ bool FailureDiagnosis::diagnoseGeneralConversionFailure(Constraint *constraint){
29142914
}
29152915
return true;
29162916
}
2917+
2918+
// Due to migration reasons, types used to conform to BooleanType, which
2919+
// contain a member var 'boolValue', now does not convert to Bool. This block
2920+
// tries to add a specific diagnosis/fixit to explicitly invoke 'boolValue'.
2921+
if (toType->isBool()) {
2922+
auto LookupResult = CS->TC.lookupMember(CS->DC, fromType,
2923+
DeclName(CS->TC.Context.getIdentifier("boolValue")));
2924+
if (!LookupResult.empty()) {
2925+
if (isa<VarDecl>(LookupResult.begin()->Decl)) {
2926+
if (anchor->canAppendCallParentheses())
2927+
diagnose(anchor->getLoc(), diag::types_not_convertible_use_bool_value,
2928+
fromType, toType).fixItInsertAfter(anchor->getEndLoc(),
2929+
".boolValue");
2930+
else
2931+
diagnose(anchor->getLoc(), diag::types_not_convertible_use_bool_value,
2932+
fromType, toType).fixItInsert(anchor->getStartLoc(), "(").
2933+
fixItInsertAfter(anchor->getEndLoc(), ").boolValue");
2934+
return true;
2935+
}
2936+
}
2937+
}
29172938

29182939
diagnose(anchor->getLoc(), diag::types_not_convertible,
29192940
constraint->getKind() == ConstraintKind::Subtype,
@@ -4026,6 +4047,8 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
40264047
case CTP_DictionaryKey:
40274048
case CTP_DictionaryValue:
40284049
case CTP_AssignSource:
4050+
case CTP_Initialization:
4051+
case CTP_ReturnStmt:
40294052
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
40304053
KnownProtocolKind::ExpressibleByIntegerLiteral,
40314054
expr) ||

test/FixCode/fixits-apply-objc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ func foo(an : Any) {
2121
let a3 : AnyObject!
2222
a3 = an
2323
}
24+
25+
func foo1(_ an : Any) {
26+
let obj: AnyObject = an
27+
}
28+
29+
func foo2(_ messageData: Any?) -> AnyObject? {
30+
return messageData
31+
}

test/FixCode/fixits-apply-objc.swift.result

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ func foo(an : Any) {
2121
let a3 : AnyObject!
2222
a3 = an as AnyObject!
2323
}
24+
25+
func foo1(_ an : Any) {
26+
let obj: AnyObject = an as AnyObject
27+
}
28+
29+
func foo2(_ messageData: Any?) -> AnyObject? {
30+
return messageData as AnyObject?
31+
}

test/FixCode/fixits-apply.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func baz(var x: Int) {
118118
x += 10
119119
}
120120
func foo(let y: String, inout x: Int) {
121-
121+
122122
}
123123

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

263263
prefix operator ***** {}
264+
265+
class BoolFoo : BooleanType {
266+
var boolValue: Bool {return false}
267+
}
268+
func testBoolValue(a : BoolFoo) {
269+
if a { }
270+
guard a {}
271+
if a as BoolFoo {}
272+
}

test/FixCode/fixits-apply.swift.result

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct MyMask : OptionSet {
2828
}
2929

3030
func supported() -> MyMask {
31-
return MyMask.Bingo
31+
return MyMask(rawValue: UInt(MyMask.Bingo))
3232
}
3333

3434
struct MyEventMask2 : OptionSet {
@@ -121,7 +121,7 @@ func baz(x: Int) {
121121
x += 10
122122
}
123123
func foo(y: String, x: inout Int) {
124-
124+
125125
}
126126

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

266266
prefix operator *****
267+
268+
class BoolFoo : Bool {
269+
var boolValue: Bool {return false}
270+
}
271+
func testBoolValue(a : BoolFoo) {
272+
if a.boolValue { }
273+
guard a.boolValue {}
274+
if (a as BoolFoo).boolValue {}
275+
}

0 commit comments

Comments
 (0)