Skip to content

Commit a770371

Browse files
committed
ModuleInterface: Print patterns with opaque result types using some keyword.
Previously, the opaque types in patterns were printed using their full stable reference which cannot be resolved when parsing a swiftinterface. Resolves rdar://127771885.
1 parent 2788c1d commit a770371

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,8 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
13351335
printPattern(TP->getSubPattern());
13361336
Printer << ": ";
13371337

1338+
PrintWithOpaqueResultTypeKeywordRAII x(Options);
1339+
13381340
// Make sure to check if the underlying var decl is an implicitly unwrapped
13391341
// optional.
13401342
bool isIUO = false;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2771,7 +2771,18 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27712771
// Properties with an opaque return type need an initializer to
27722772
// determine their underlying type.
27732773
if (var->getOpaqueResultTypeDecl()) {
2774-
var->diagnose(diag::opaque_type_var_no_init);
2774+
// ...but don't enforce this for SIL or module interface files.
2775+
switch (SF->Kind) {
2776+
case SourceFileKind::Interface:
2777+
case SourceFileKind::SIL:
2778+
break;
2779+
case SourceFileKind::DefaultArgument:
2780+
case SourceFileKind::Main:
2781+
case SourceFileKind::Library:
2782+
case SourceFileKind::MacroExpansion:
2783+
var->diagnose(diag::opaque_type_var_no_init);
2784+
break;
2785+
}
27752786
}
27762787

27772788
// Non-member observing properties need an initializer.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@ func someTypeIsTheSame() {
6060
f = barString.foo(MyFoo()) // expected-error{{cannot assign}}
6161
f = barInt.foo(YourFoo()) // expected-error{{cannot convert value of type 'YourFoo' to expected argument type 'MyFoo'}}
6262
f = barString.foo(MyFoo()) // expected-error{{cannot assign}}
63+
64+
var g = globalComputedVar
65+
g = globalVar // expected-error{{cannot assign}}
66+
g = globalComputedVar
67+
g = 123 // expected-error{{cannot assign}}
68+
69+
var h = globalVar
70+
h = globalComputedVar // expected-error{{cannot assign}}
71+
h = globalVar
72+
h = 123 // expected-error{{cannot assign}}
73+
74+
var i = globalVarTuple
75+
i = (123, foo(123)) // expected-error{{cannot assign}}
76+
i = globalVarTuple
77+
i = (globalVarTuple.0, globalVarTuple.1)
6378
}

test/ModuleInterface/opaque-result-types.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public func foo<T: Foo>(_ x: T) -> some Foo {
2525
return x
2626
}
2727

28+
// CHECK-LABEL: public var globalComputedVar: some OpaqueResultTypes.Foo {
29+
// CHECK-NEXT: get
30+
// CHECK-NEXT: }
31+
@available(SwiftStdlib 5.1, *)
32+
public var globalComputedVar: some Foo { 123 }
33+
34+
// CHECK-LABEL: public var globalVar: some OpaqueResultTypes.Foo{{$}}
35+
@available(SwiftStdlib 5.1, *)
36+
public var globalVar: some Foo = 123
37+
38+
// CHECK-LABEL: public var globalVarTuple: (some OpaqueResultTypes.Foo, some OpaqueResultTypes.Foo){{$}}
39+
@available(SwiftStdlib 5.1, *)
40+
public var globalVarTuple: (some Foo, some Foo) = (123, foo(123))
41+
2842
public protocol AssocTypeInference {
2943
associatedtype Assoc: Foo
3044
associatedtype AssocProperty: Foo

0 commit comments

Comments
 (0)