Skip to content

Commit d14a205

Browse files
authored
Merge pull request #63838 from xedin/extra-force-unwrap-in-ambiguous-context
[CSFix] Diagnose extraneous force unwraps in ambiguous context
2 parents 08e62a3 + bb1da5e commit d14a205

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-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+
}

test/StringProcessing/Parse/forward-slash-regex.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ do {
121121
} // expected-error {{expected expression after operator}}
122122

123123
_ = /x/??/x/ // expected-error {{'/' is not a postfix unary operator}}
124+
// expected-error@-1 2 {{cannot use optional chaining on non-optional value of type 'Regex<Substring>'}}
124125

125126
_ = /x/ ... /y/ // expected-error {{referencing operator function '...' on 'Comparable' requires that 'Regex<Substring>' conform to 'Comparable'}}
126127

0 commit comments

Comments
 (0)