Skip to content

Commit 9ddf497

Browse files
committed
[CSGen]: getTypeForPattern should perform member lookup into the external pattern metatype if enum element has externalPatternType
1 parent 1f3e159 commit 9ddf497

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/Sema/CSGen.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,18 @@ namespace {
27212721
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
27222722

27232723
baseType = parentType;
2724+
// Perform member lookup into the external pattern metatype. e.g.
2725+
// `case let .test(tuple) as Test`.
2726+
} else if (externalPatternType) {
2727+
Type externalMetaType = MetatypeType::get(externalPatternType);
2728+
2729+
CS.addValueMemberConstraint(
2730+
externalMetaType, enumPattern->getName(), memberType, CurDC,
2731+
functionRefKind, {},
2732+
CS.getConstraintLocator(locator,
2733+
LocatorPathElt::PatternMatch(pattern)));
2734+
2735+
baseType = externalPatternType;
27242736
} else {
27252737
// Use the pattern type for member lookup.
27262738
CS.addUnresolvedValueMemberConstraint(

test/Constraints/issue-60806.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck %s
2+
3+
// https://github.com/apple/swift/issues/60806
4+
5+
struct Foo<T> {
6+
init(_: (T) -> Void) {}
7+
}
8+
9+
protocol Bar {}
10+
11+
enum Baz: Bar {
12+
case someCase(Int)
13+
}
14+
15+
enum NonBarBaz {
16+
case someCase(Int)
17+
}
18+
19+
let _: Foo<Bar> = Foo<Bar> { (a: Bar) -> Void in
20+
switch a {
21+
// CHECK: (pattern_is type='Bar' value_cast Baz
22+
// CHECK-NEXT: (pattern_enum_element type='Baz' Baz.someCase
23+
case let .someCase(value) as Baz:
24+
print(value)
25+
// expected-warning@-1 {{cast from 'any Bar' to unrelated type 'NonBarBaz' always fails}}
26+
case let .someCase(value) as NonBarBaz:
27+
print(value)
28+
default:
29+
break
30+
}
31+
}

0 commit comments

Comments
 (0)