Skip to content

Commit e55392d

Browse files
committed
[CSFix] Diagnose extraneous force unwraps in ambiguous context
1 parent 5a3220e commit e55392d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,10 @@ class RemoveUnwrap final : public ConstraintFix {
12051205

12061206
bool diagnose(const Solution &solution, bool asNote = false) const override;
12071207

1208+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
1209+
return diagnose(*commonFixes.front().first);
1210+
}
1211+
12081212
static RemoveUnwrap *create(ConstraintSystem &cs, Type baseType,
12091213
ConstraintLocator *locator);
12101214

test/Constraints/optional.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,14 @@ do {
588588
takesP(value)
589589
// expected-error@-1 {{argument type '(any BinaryInteger)?' does not conform to expected type 'P'}}
590590
}
591-
}
591+
}
592+
593+
// Diagnose extraneous force unwrap in ambiguous context
594+
do {
595+
func test(_: Int) {} // expected-note {{found this candidate}}
596+
func test(_: String) {} // expected-note {{found this candidate}}
597+
598+
var x: Double = 42
599+
test(x!) // expected-error {{no exact matches in call to local function 'test'}}
600+
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
601+
}

0 commit comments

Comments
 (0)