Skip to content

Commit 1f9fbed

Browse files
Merge pull request #39039 from LucianoPAlmeida/SR-14408-ambiguity-enum
[Sema][SR-14408] Improvements on enum equality diagnostics
2 parents 0a0926b + 41e31e4 commit 1f9fbed

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8251,6 +8251,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
82518251
if (instanceTy->isAny() || instanceTy->isAnyObject())
82528252
impact += 5;
82538253

8254+
// Increasing the impact for missing member in any argument position so it
8255+
// doesn't affect situations where there are another fixes involved.
8256+
auto *anchorExpr = getAsExpr(locator->getAnchor());
8257+
if (anchorExpr && isArgumentExpr(anchorExpr)) {
8258+
impact += 5;
8259+
}
8260+
82548261
if (recordFix(fix, impact))
82558262
return SolutionKind::Error;
82568263

@@ -8301,7 +8308,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
83018308
functionRefKind, locator,
83028309
/*includeInaccessibleMembers*/ true);
83038310

8304-
// If uwrapped type still couldn't find anything for a given name,
8311+
// If unwrapped type still couldn't find anything for a given name,
83058312
// let's fallback to a "not such member" fix.
83068313
if (result.ViableCandidates.empty() && result.UnviableCandidates.empty())
83078314
return fixMissingMember(origBaseTy, memberTy, locator);

lib/Sema/ConstraintSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4934,6 +4934,15 @@ ConstraintSystem::isArgumentExpr(Expr *expr) {
49344934
return None;
49354935
}
49364936

4937+
// Specifically for unresolved member expr getParentExpr returns a chain
4938+
// result expr as its immediate parent, so let's look one level up on AST.
4939+
if (auto *URMCR = getAsExpr<UnresolvedMemberChainResultExpr>(argList)) {
4940+
argList = getParentExpr(URMCR);
4941+
if (!argList) {
4942+
return None;
4943+
}
4944+
}
4945+
49374946
if (isa<ParenExpr>(argList)) {
49384947
for (;;) {
49394948
auto *parent = getParentExpr(argList);

test/Constraints/enum_cases.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,28 @@ struct EnumElementPatternFromContextualType<T> {
171171
}
172172
}
173173
}
174+
175+
// SR-14408
176+
enum CompassPoint {
177+
case North(Int)
178+
case South
179+
case East
180+
case West
181+
}
182+
183+
func isNorth(c : CompassPoint) -> Bool {
184+
// expected-error@+1{{member 'North' expects argument of type 'Int'}}
185+
return c == .North // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
186+
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
187+
}
188+
189+
func isNorth2(c : CompassPoint) -> Bool {
190+
// expected-error@+1{{member 'North' expects argument of type 'Int'}}
191+
return .North == c // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
192+
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
193+
}
194+
195+
func isSouth(c : CompassPoint) -> Bool {
196+
return c == .South // expected-error {{binary operator '==' cannot be applied to two 'CompassPoint' operands}}
197+
// expected-note@-1 {{binary operator '==' cannot be synthesized for enums with associated values}}
198+
}

0 commit comments

Comments
 (0)