Skip to content

Commit dbd3a48

Browse files
committed
Track var/subscript opaque return decls for type reconstruction.
1 parent a99716b commit dbd3a48

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,12 +5226,22 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
52265226
pattern = patternRes.get();
52275227
}
52285228

5229+
bool hasOpaqueReturnTy = false;
5230+
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
5231+
hasOpaqueReturnTy =
5232+
isa<OpaqueReturnTypeRepr>(typedPattern->getTypeRepr());
5233+
}
5234+
auto sf = CurDeclContext->getParentSourceFile();
5235+
52295236
// Configure all vars with attributes, 'static' and parent pattern.
52305237
pattern->forEachVariable([&](VarDecl *VD) {
52315238
VD->setStatic(StaticLoc.isValid());
52325239
VD->getAttrs() = Attributes;
52335240
setLocalDiscriminator(VD);
52345241
Decls.push_back(VD);
5242+
if (hasOpaqueReturnTy && sf) {
5243+
sf->addUnvalidatedDeclWithOpaqueResultType(VD);
5244+
}
52355245
});
52365246

52375247
// Remember this pattern/init pair for our ultimate PatternBindingDecl. The
@@ -6394,6 +6404,13 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
63946404
CurDeclContext,
63956405
nullptr);
63966406
Subscript->getAttrs() = Attributes;
6407+
6408+
// Let the source file track the opaque return type mapping, if any.
6409+
if (ElementTy.get() && isa<OpaqueReturnTypeRepr>(ElementTy.get())) {
6410+
if (auto sf = CurDeclContext->getParentSourceFile()) {
6411+
sf->addUnvalidatedDeclWithOpaqueResultType(Subscript);
6412+
}
6413+
}
63976414

63986415
DefaultArgs.setFunctionContext(Subscript, Subscript->getIndices());
63996416

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,12 @@ static bool shouldUseOpaqueReadAccessor(TypeChecker &TC,
19811981

19821982
static void validateAbstractStorageDecl(TypeChecker &TC,
19831983
AbstractStorageDecl *storage) {
1984+
if (storage->getOpaqueResultTypeDecl()) {
1985+
if (auto sf = storage->getInnermostDeclContext()->getParentSourceFile()) {
1986+
sf->markDeclWithOpaqueResultTypeAsValidated(storage);
1987+
}
1988+
}
1989+
19841990
if (shouldUseOpaqueReadAccessor(TC, storage))
19851991
storage->setOpaqueReadOwnership(OpaqueReadOwnership::Borrowed);
19861992

test/ParseableInterface/Inputs/opaque-result-types-client.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import OpaqueResultTypes
33
func getAssocType<T: AssocTypeInference>(_ x: T) -> T.Assoc {
44
return x.foo(0)
55
}
6+
func getAssocPropType<T: AssocTypeInference>(_ x: T) -> T.AssocProperty {
7+
return x.prop
8+
}
9+
func getAssocSubscriptType<T: AssocTypeInference>(_ x: T) -> T.AssocSubscript {
10+
return x[]
11+
}
612

713
struct MyFoo: Foo {}
814
struct YourFoo: Foo {}
@@ -29,6 +35,18 @@ func someTypeIsTheSame() {
2935
d = barString.foo(0) // expected-error{{cannot assign}}
3036
d = getAssocType(barInt)
3137
d = getAssocType(barString) // expected-error{{cannot assign}}
38+
39+
var d2 = barInt.prop
40+
d2 = barInt.prop
41+
d2 = barString.prop // expected-error{{cannot assign}}
42+
d2 = getAssocPropType(barInt)
43+
d2 = getAssocPropType(barString) // expected-error{{cannot assign}}
44+
45+
var d3 = barInt[]
46+
d3 = barInt[]
47+
d3 = barString[] // expected-error{{cannot assign}}
48+
d3 = getAssocSubscriptType(barInt)
49+
d3 = getAssocSubscriptType(barString) // expected-error{{cannot assign}}
3250

3351
var e = barString.foo(0)
3452
e = barInt.foo(0) // expected-error{{cannot assign}}

test/ParseableInterface/opaque-result-types.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ public func foo<T: Foo>(_ x: T) -> some Foo {
2323

2424
public protocol AssocTypeInference {
2525
associatedtype Assoc: Foo
26+
associatedtype AssocProperty: Foo
27+
associatedtype AssocSubscript: Foo
2628

2729
func foo(_: Int) -> Assoc
30+
31+
var prop: AssocProperty { get }
32+
subscript() -> AssocSubscript { get }
2833
}
2934

3035
public struct Bar<T>: AssocTypeInference {
@@ -61,7 +66,16 @@ public struct Bar<T>: AssocTypeInference {
6166
return x
6267
}
6368

69+
public var prop: some Foo {
70+
return 123
71+
}
72+
public subscript() -> some Foo {
73+
return 123
74+
}
75+
6476
// CHECK-LABEL: public typealias Assoc = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
77+
// CHECK-LABEL: public typealias AssocProperty = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
78+
// CHECK-LABEL: public typealias AssocSubscript = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
6579
}
6680

6781
public struct Bass<U: Foo>: AssocTypeInference {
@@ -85,11 +99,28 @@ public struct Bar<T>: AssocTypeInference {
8599
public func foo<V: Foo>(_ x: V) -> some Foo {
86100
return x
87101
}
102+
public var prop: some Foo {
103+
return 123
104+
}
105+
public subscript() -> some Foo {
106+
return 123
107+
}
88108

89109
// CHECK-LABEL: public typealias Assoc = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T, U>
110+
// CHECK-LABEL: public typealias AssocProperty = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T, U>
111+
// CHECK-LABEL: public typealias AssocSubscript = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T, U>
112+
}
113+
114+
public var prop: some Foo {
115+
return 123
116+
}
117+
public subscript() -> some Foo {
118+
return 123
90119
}
91120

92121
// CHECK-LABEL: public typealias Assoc = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
122+
// CHECK-LABEL: public typealias AssocProperty = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
123+
// CHECK-LABEL: public typealias AssocSubscript = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<T>
93124
}
94125

95126
public struct Zim: AssocTypeInference {
@@ -123,6 +154,13 @@ public struct Zim: AssocTypeInference {
123154
public func foo<U: Foo>(_ x: U) -> some Foo {
124155
return x
125156
}
157+
158+
public var prop: some Foo {
159+
return 123
160+
}
161+
public subscript() -> some Foo {
162+
return 123
163+
}
126164
}
127165

128166
public struct Zung<U: Foo>: AssocTypeInference {
@@ -146,6 +184,22 @@ public struct Zim: AssocTypeInference {
146184
return x
147185
}
148186

187+
public var prop: some Foo {
188+
return 123
189+
}
190+
public subscript() -> some Foo {
191+
return 123
192+
}
193+
149194
// CHECK-LABEL: public typealias Assoc = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<U>
195+
// CHECK-LABEL: public typealias AssocProperty = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<U>
196+
// CHECK-LABEL: public typealias AssocSubscript = @_opaqueReturnTypeOf("{{.*}}", 0) {{.*}}<U>
197+
}
198+
199+
public var prop: some Foo {
200+
return 123
201+
}
202+
public subscript() -> some Foo {
203+
return 123
150204
}
151205
}

0 commit comments

Comments
 (0)