Skip to content

Commit 2bf7f36

Browse files
committed
Don't build opaque archetypes referencing the outer environment.
When there are same-type constraints involving opaque archetype parameters and generic parameters of the enclosing environment, a nested type of an opaque type archetype might in fact refer to something in the outer environment of the opaque type. Recognize this case and perform substitutions on such types using the opaque type's substitutions.
1 parent 209167a commit 2bf7f36

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/AST/GenericEnvironment.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,24 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
362362
break;
363363
}
364364

365-
case Kind::Opaque:
365+
case Kind::Opaque: {
366+
// If the anchor type isn't rooted in a generic parameter that
367+
// represents an opaque declaration, then apply the outer substitutions.
368+
// It would be incorrect to build an opaque type archetype here.
369+
auto rootGP = requirements.anchor->getRootGenericParam();
370+
unsigned opaqueDepth =
371+
getOpaqueTypeDecl()->getOpaqueGenericParams().front()->getDepth();
372+
if (rootGP->getDepth() < opaqueDepth) {
373+
result = maybeApplyOpaqueTypeSubstitutions(requirements.anchor);
374+
break;
375+
}
376+
366377
result = OpaqueTypeArchetypeType::getNew(this, requirements.anchor,
367378
requirements.protos, superclass,
368379
requirements.layout);
369380
break;
370381
}
382+
}
371383
}
372384

373385
if (genericParam)

test/Interpreter/named_opaque_result_types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ struct X<T, U: Hashable>: P {
3434
func f() -> <
3535
R1: Collection, R2: Collection where R1.Element == T, R2.Element == U
3636
> (R1, R2) {
37-
// FIXME: Use unzipCollection here
38-
return (Array(data.map { $0.0 }), Set(data.map { $0.1 }))
37+
return unzipCollection(data)
3938
}
4039
}
4140

@@ -71,6 +70,7 @@ if #available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) {
7170
print("too old")
7271
print("too old")
7372
print("too old")
73+
7474
print("too old")
7575
print("too old")
7676
}

0 commit comments

Comments
 (0)