Skip to content

[CSFix] Diagnose extraneous force unwraps in ambiguous context #63838

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 1 commit into from
Feb 28, 2023
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
4 changes: 4 additions & 0 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,10 @@ class RemoveUnwrap final : public ConstraintFix {

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

bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

static RemoveUnwrap *create(ConstraintSystem &cs, Type baseType,
ConstraintLocator *locator);

Expand Down
12 changes: 11 additions & 1 deletion test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,14 @@ do {
takesP(value)
// expected-error@-1 {{argument type '(any BinaryInteger)?' does not conform to expected type 'P'}}
}
}
}

// Diagnose extraneous force unwrap in ambiguous context
do {
func test(_: Int) {} // expected-note {{found this candidate}}
func test(_: String) {} // expected-note {{found this candidate}}

var x: Double = 42
test(x!) // expected-error {{no exact matches in call to local function 'test'}}
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
}
1 change: 1 addition & 0 deletions test/StringProcessing/Parse/forward-slash-regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ do {
} // expected-error {{expected expression after operator}}

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

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

Expand Down