Skip to content

Commit 5fcc702

Browse files
committed
[FixCode] Add diagnosis/fixit to help users deal with BooleanType's removal (#4392)
* [FixCode] Add diagnosis/fixit to help users deal with BooleanType's removal. Due to migration reasons, types used to conform to BooleanType, which must contain a member var 'boolValue', now does not convert to Bool. This patch adds a specific diagnosis/fixit to explicitly invoke 'boolValue' to please the context types. * Address Jordan's code review comments.
1 parent 81b1afc commit 5fcc702

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
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: 21 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,

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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)