Skip to content

Commit 644ed76

Browse files
committed
[Constraint system] Prefer enum cases to static members when pattern matching.
1 parent 6cfa0b0 commit 644ed76

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5693,6 +5693,22 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
56935693
}
56945694
}
56955695

5696+
// If we are pattern-matching an enum element and we found any enum elements,
5697+
// ignore anything that isn't an enum element.
5698+
bool onlyAcceptEnumElements = false;
5699+
if (memberLocator &&
5700+
memberLocator->isLastElement<LocatorPathElt::PatternMatch>() &&
5701+
isa<EnumElementPattern>(
5702+
memberLocator->getLastElementAs<LocatorPathElt::PatternMatch>()
5703+
->getPattern())) {
5704+
for (const auto &result: lookup) {
5705+
if (isa<EnumElementDecl>(result.getValueDecl())) {
5706+
onlyAcceptEnumElements = true;
5707+
break;
5708+
}
5709+
}
5710+
}
5711+
56965712
// If the instance type is String bridged to NSString, compute
56975713
// the type we'll look in for bridging.
56985714
Type bridgedType;
@@ -5717,6 +5733,10 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
57175733
return;
57185734
}
57195735

5736+
// If we only accept enum elements but this isn't one, ignore it.
5737+
if (onlyAcceptEnumElements && !isa<EnumElementDecl>(decl))
5738+
return;
5739+
57205740
// Dig out the instance type and figure out what members of the instance type
57215741
// we are going to see.
57225742
auto baseTy = candidate.getBaseType();

test/stmt/if_while_var.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,16 @@ func matchImplicitTupling(pr: SomeParseResult<Int>) {
206206
let y: Int = x // expected-error{{cannot convert value of type '(value: String, repetitions: Int)' to specified type 'Int'}}
207207
}
208208
}
209+
210+
// Cope with an ambiguity between a case name and a static member. Prefer the
211+
// case.
212+
enum CaseStaticAmbiguity {
213+
case C(Bool)
214+
215+
var isC: Bool {
216+
if case .C = self { return true }
217+
return false
218+
}
219+
220+
static func C(_: Int) -> CaseStaticAmbiguity { return .C(true) }
221+
}

0 commit comments

Comments
 (0)