Skip to content

[Sema][SR-14408] Improvements on enum equality diagnostics #39039

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
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
9 changes: 8 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8251,6 +8251,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
if (instanceTy->isAny() || instanceTy->isAnyObject())
impact += 5;

// Increasing the impact for missing member in any argument position so it
// doesn't affect situations where there are another fixes involved.
auto *anchorExpr = getAsExpr(locator->getAnchor());
if (anchorExpr && isArgumentExpr(anchorExpr)) {
impact += 5;
}

if (recordFix(fix, impact))
return SolutionKind::Error;

Expand Down Expand Up @@ -8301,7 +8308,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
functionRefKind, locator,
/*includeInaccessibleMembers*/ true);

// If uwrapped type still couldn't find anything for a given name,
// If unwrapped type still couldn't find anything for a given name,
// let's fallback to a "not such member" fix.
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty())
return fixMissingMember(origBaseTy, memberTy, locator);
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4934,6 +4934,15 @@ ConstraintSystem::isArgumentExpr(Expr *expr) {
return None;
}

// Specifically for unresolved member expr getParentExpr returns a chain
// result expr as its immediate parent, so let's look one level up on AST.
if (auto *URMCR = getAsExpr<UnresolvedMemberChainResultExpr>(argList)) {
argList = getParentExpr(URMCR);
if (!argList) {
return None;
}
}

if (isa<ParenExpr>(argList)) {
for (;;) {
auto *parent = getParentExpr(argList);
Expand Down
25 changes: 25 additions & 0 deletions test/Constraints/enum_cases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,28 @@ struct EnumElementPatternFromContextualType<T> {
}
}
}

// SR-14408
enum CompassPoint {
case North(Int)
case South
case East
case West
}

func isNorth(c : CompassPoint) -> Bool {
// expected-error@+1{{member 'North' expects argument of type 'Int'}}
return c == .North // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
}

func isNorth2(c : CompassPoint) -> Bool {
// expected-error@+1{{member 'North' expects argument of type 'Int'}}
return .North == c // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
}

func isSouth(c : CompassPoint) -> Bool {
return c == .South // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
}