Skip to content

[5.7][CSSimplify] Don't attempt to synthesize ~= for optional base types #58666

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
May 11, 2022
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
22 changes: 13 additions & 9 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9205,17 +9205,21 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
dyn_cast<EnumElementPattern>(patternLoc->getPattern())) {
auto enumType = baseObjTy->getMetatypeInstanceType();

// If the synthesis of ~= resulted in errors (i.e. broken stdlib)
// that would be diagnosed inline, so let's just fall through and
// let this situation be diagnosed as a missing member.
auto hadErrors = inferEnumMemberThroughTildeEqualsOperator(
// Optional base type does not trigger `~=` synthesis, but it tries
// to find member on both `Optional` and its wrapped type.
if (!enumType->getOptionalObjectType()) {
// If the synthesis of ~= resulted in errors (i.e. broken stdlib)
// that would be diagnosed inline, so let's just fall through and
// let this situation be diagnosed as a missing member.
auto hadErrors = inferEnumMemberThroughTildeEqualsOperator(
*this, enumElement, enumType, memberTy, locator);

// Let's consider current member constraint solved because it's
// replaced by a new set of constraints that would resolve member
// type.
if (!hadErrors)
return SolutionKind::Solved;
// Let's consider current member constraint solved because it's
// replaced by a new set of constraints that would resolve member
// type.
if (!hadErrors)
return SolutionKind::Solved;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-typecheck-verify-swift

enum MyEnum {
case test(Int)
}

func test(result: MyEnum, optResult: MyEnum?) {
if let .co = result { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum' has no member 'co'}}
}

if let .co(42) = result { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type of expression is ambiguous without more context}}
}

if let .co = optResult { // expected-error {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum?' has no member 'co'}}
}

let _ = {
if let .co = result { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum' has no member 'co'}}
}
}

let _ = {
if let .co = optResult { // expected-error 2 {{pattern matching in a condition requires the 'case' keyword}}
// expected-error@-1 {{type 'MyEnum?' has no member 'co'}}
}
}
}