Skip to content

[5.9] [CS] Avoid crashing if we have no contextual type for initialization #66769

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
Jun 20, 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
16 changes: 15 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,21 @@ ValueDecl *RequirementFailure::getDeclRef() const {
return cast<ValueDecl>(getDC()->getParent()->getAsDecl());
}

return getAffectedDeclFromType(contextualTy);
// We check for a contextual type here because we form a
// ContextualType(CTP_Initialization) LocatorPathElt for pattern bindings
// when there is no TypedPattern present. In such a case, there will be
// no recorded contextual type (though the pattern may produce a type that
// could be considered contextual).
if (contextualTy) {
// If the contextual type is e.g a tuple, we may not be able to resolve
// a decl. Fall through to getting the 'owner type' in that case.
if (auto *D = getAffectedDeclFromType(contextualTy))
return D;
} else {
assert((contextualPurpose == CTP_Initialization ||
contextualPurpose == CTP_Unused) &&
"Should have had a contextual type");
}
}

if (getLocator()->isFirstElement<LocatorPathElt::CoercionOperand>())
Expand Down
66 changes: 66 additions & 0 deletions test/Constraints/patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,69 @@ enum LotsOfOptional {
func testLotsOfNil(_ x: LotsOfOptional) {
if case .yup(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) = x {}
}

// https://github.com/apple/swift/issues/66752
// FIXME: We ought to improve the diagnostics here
func issue66752(_ x: Result<String, Error>) {
let _ = {
if case .failure() = x {}
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}
}
let _ = {
if case (.failure(), let y) = (x, 0) {}
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}
}
if case .failure() = x {}
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}

if case (.failure(), let y) = (x, 0) {}
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}
}

// https://github.com/apple/swift/issues/66750
// FIXME: We ought to improve the diagnostics here
func issue66750(_ x: Result<String, Error>) {
let _ = {
switch x {
case .success:
"a"
case .failure():
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}
"b"
}
}
let _ = {
switch (x, 0) {
case (.success, let y):
"a"
case (.failure(), let y):
// expected-error@-1 {{type '()' cannot conform to 'Error}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
// expected-note@-3 {{required by generic enum 'Result' where 'Failure' = '()'}}
"b"
}
}
switch x {
case .success:
break
case .failure(): // expected-error {{tuple pattern cannot match values of the non-tuple type 'any Error'}}
break
}
switch (x, 0) {
case (.success, let y):
break
case (.failure(), let y):
// expected-error@-1 {{tuple pattern cannot match values of the non-tuple type 'any Error'}}
break
}
}