Skip to content

Commit 9a22bac

Browse files
authored
Merge pull request #62315 from hank121314/fix/issue-60806
[CSGen]: `getTypeForPattern` should perform member lookup into the external pattern metatype
2 parents 45cd83d + 9ddf497 commit 9a22bac

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
@@ -2752,6 +2752,18 @@ namespace {
27522752
locator.withPathElement(LocatorPathElt::PatternMatch(pattern)));
27532753

27542754
baseType = parentType;
2755+
// Perform member lookup into the external pattern metatype. e.g.
2756+
// `case let .test(tuple) as Test`.
2757+
} else if (externalPatternType) {
2758+
Type externalMetaType = MetatypeType::get(externalPatternType);
2759+
2760+
CS.addValueMemberConstraint(
2761+
externalMetaType, enumPattern->getName(), memberType, CurDC,
2762+
functionRefKind, {},
2763+
CS.getConstraintLocator(locator,
2764+
LocatorPathElt::PatternMatch(pattern)));
2765+
2766+
baseType = externalPatternType;
27552767
} else {
27562768
// Use the pattern type for member lookup.
27572769
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)