Skip to content

[5.5][ConstraintSystem] Bind missing member in pattern match to a hole early #38236

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
Jul 6, 2021
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
25 changes: 21 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4878,14 +4878,24 @@ bool ConstraintSystem::repairFailures(
}

case ConstraintLocator::PatternMatch: {
auto *pattern = elt.castTo<LocatorPathElt::PatternMatch>().getPattern();
bool isMemberMatch =
lhs->is<FunctionType>() && isa<EnumElementPattern>(pattern);

// If member reference couldn't be resolved, let's allow pattern
// to have holes.
if (rhs->isPlaceholder() && isMemberMatch) {
markAnyTypeVarsAsPotentialHoles(lhs);
return true;
}

// If either type is a placeholder, consider this fixed.
if (lhs->isPlaceholder() || rhs->isPlaceholder())
return true;

// If the left-hand side is a function type and the pattern is an enum
// element pattern, call it a contextual mismatch.
auto pattern = elt.castTo<LocatorPathElt::PatternMatch>().getPattern();
if (lhs->is<FunctionType>() && isa<EnumElementPattern>(pattern)) {
// If member reference didn't match expected pattern,
// let's consider that a contextual mismatch.
if (isMemberMatch) {
markAnyTypeVarsAsPotentialHoles(lhs);
markAnyTypeVarsAsPotentialHoles(rhs);

Expand Down Expand Up @@ -8189,6 +8199,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
// `key path` constraint can't be retired until all components
// are simplified.
addTypeVariableConstraintsToWorkList(memberTypeVar);
} else if (locator->isLastElement<LocatorPathElt::PatternMatch>()) {
// Let's handle member patterns specifically because they use
// equality instead of argument application constraint, so allowing
// them to bind member could mean missing valid hole positions in
// the pattern.
assignFixedType(memberTypeVar,
PlaceholderType::get(getASTContext(), memberTypeVar));
} else {
recordPotentialHole(memberTypeVar);
}
Expand Down
29 changes: 29 additions & 0 deletions test/Constraints/result_builder_diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,32 @@ struct TuplifiedStructWithInvalidClosure {
}
}
}

// rdar://65667992 - invalid case in enum causes fallback diagnostic
func test_rdar65667992() {
@resultBuilder
struct Builder {
static func buildBlock<T>(_ t: T) -> T { t }
static func buildEither<T>(first: T) -> T { first }
static func buildEither<T>(second: T) -> T { second }
}

struct S {}

enum E {
case set(v: Int, choices: [Int])
case notSet(choices: [Int])
}

struct MyView {
var entry: E

@Builder var body: S {
switch entry { // expected-error {{type 'E' has no member 'unset'}}
case .set(_, _): S()
case .unset(_): S()
default: S()
}
}
}
}