Skip to content

Commit 13076ab

Browse files
authored
Merge pull request #31788 from jckarter/replace-opaque-underlying-type-with-underlying-type-5.3
[5.3] ReplaceOpaqueTypesWithUnderlyingTypes: Handle a type being "substituted" with itself.
2 parents 1a4f523 + 74c2106 commit 13076ab

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/AST/Type.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,12 +3043,21 @@ ProtocolConformanceRef ReplaceOpaqueTypesWithUnderlyingTypes::
30433043
operator()(CanType maybeOpaqueType, Type replacementType,
30443044
ProtocolDecl *protocol) const {
30453045
auto abstractRef = ProtocolConformanceRef(protocol);
3046-
3046+
30473047
auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
30483048
if (!archetypeAndRoot) {
3049-
assert(maybeOpaqueType->isTypeParameter() ||
3050-
maybeOpaqueType->is<ArchetypeType>());
3051-
return abstractRef;
3049+
if (maybeOpaqueType->isTypeParameter() ||
3050+
maybeOpaqueType->is<ArchetypeType>())
3051+
return abstractRef;
3052+
3053+
// SIL type lowering may have already substituted away the opaque type, in
3054+
// which case we'll end up "substituting" the same type.
3055+
if (maybeOpaqueType->isEqual(replacementType)) {
3056+
return inContext->getParentModule()
3057+
->lookupConformance(replacementType, protocol);
3058+
}
3059+
3060+
llvm_unreachable("origType should have been an opaque type or type parameter");
30523061
}
30533062

30543063
auto archetype = archetypeAndRoot->first;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-emit-silgen -disable-availability-checking -verify %s
2+
protocol P {}
3+
extension Int: P {}
4+
5+
func foo() -> some P { return 0 }
6+
func bar<T: P>(_ x: T) -> some P { return x }
7+
8+
struct Bas<T: P> { init(_: T) {} }
9+
10+
func abstraction_level<T>(x: T) -> (T) -> () {
11+
return { _ in () }
12+
}
13+
14+
func test() {
15+
abstraction_level(x: Bas(bar(foo())))(Bas(bar(foo())))
16+
}

0 commit comments

Comments
 (0)