Skip to content

Commit b2aa851

Browse files
committed
Use TypeRepr::hasOpaque() consistently.
Replace shallow checks for `OpaqueReturnTypeRepr` with calls to `hasOpaque`, which ensures that we consider opaque result types in structural positions.
1 parent 474155a commit b2aa851

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6777,8 +6777,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
67776777

67786778
bool hasOpaqueReturnTy = false;
67796779
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
6780-
hasOpaqueReturnTy =
6781-
isa<OpaqueReturnTypeRepr>(typedPattern->getTypeRepr());
6780+
hasOpaqueReturnTy = typedPattern->getTypeRepr()->hasOpaque();
67826781
}
67836782
auto sf = CurDeclContext->getParentSourceFile();
67846783

@@ -7108,7 +7107,7 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
71087107
CurDeclContext);
71097108

71107109
// Let the source file track the opaque return type mapping, if any.
7111-
if (isa_and_nonnull<OpaqueReturnTypeRepr>(FuncRetTy) &&
7110+
if (FuncRetTy && FuncRetTy->hasOpaque() &&
71127111
!InInactiveClauseEnvironment) {
71137112
if (auto sf = CurDeclContext->getParentSourceFile()) {
71147113
sf->addUnvalidatedDeclWithOpaqueResultType(FD);
@@ -7984,7 +7983,7 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
79847983
Subscript->getAttrs() = Attributes;
79857984

79867985
// Let the source file track the opaque return type mapping, if any.
7987-
if (isa_and_nonnull<OpaqueReturnTypeRepr>(ElementTy.get()) &&
7986+
if (ElementTy.get() && ElementTy.get()->hasOpaque() &&
79887987
!InInactiveClauseEnvironment) {
79897988
if (auto sf = CurDeclContext->getParentSourceFile()) {
79907989
sf->addUnvalidatedDeclWithOpaqueResultType(Subscript);

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
769769
return nullptr;
770770
}
771771
}();
772-
if (resultTypeRepr && !isa<OpaqueReturnTypeRepr>(resultTypeRepr)) {
772+
if (resultTypeRepr && !resultTypeRepr->hasOpaque()) {
773773
const auto resultType =
774774
resolution.withOptions(TypeResolverContext::FunctionResult)
775775
.resolveType(resultTypeRepr);

test/IRGen/opaque_result_type.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public var globalProp: some O {
6262
return 0
6363
}
6464

65-
public class C: P, Q {
65+
public class C: P, Q, Marker {
6666
// CHECK-LABEL: @"$s18opaque_result_type1CC3pooQryFQOMQ" = {{.*}} constant <{ {{.*}} }> <{
6767
// -- header: opaque type context (0x4), generic (0x80), unique (0x40), two entries (0x2_0000)
6868
// CHECK-SAME: <i32 0x2_00c4>
@@ -154,6 +154,10 @@ func fizz<T: P & Q & Marker>(z: T) -> some P & Q & Marker {
154154
return z
155155
}
156156

157+
func bauble<T: P & Q & Marker, U: Q>(z: T, u: U) -> [(some P & Q & Marker, (some Q)?)] {
158+
return [(z, u)]
159+
}
160+
157161
// Ensure the local type's opaque descriptor gets emitted.
158162
// CHECK-LABEL: @"$s18opaque_result_type11localOpaqueQryF0D0L_QryFQOMQ" =
159163
func localOpaque() -> some P {
@@ -180,8 +184,12 @@ public func useFoo(x: String, y: C) {
180184
let pqb = pq.qoo()
181185
pqb.bar()
182186
pqb.baz()
187+
188+
let _ = bauble(z: y, u: y)
183189
}
184190

191+
// CHECK-LABEL: define {{.*}} @"$s18opaque_result_type6bauble1z1uSayQr_QrSgtGx_q_tAA6MarkerRzAA1PRzAA1QRzAaIR_r0_lF"
192+
185193
// CHECK-LABEL: define {{.*}} @"$s18opaque_result_type6useFoo1x1yySS_AA1CCtF"
186194
// CHECK: [[OPAQUE:%.*]] = call {{.*}} @"$s18opaque_result_type3baz1zQrx_tAA1PRzAA1QRzlFQOMg"
187195
// CHECK: [[CONFORMANCE:%.*]] = call swiftcc i8** @swift_getOpaqueTypeConformance(i8* {{.*}}, %swift.type_descriptor* [[OPAQUE]], [[WORD:i32|i64]] 1)

0 commit comments

Comments
 (0)