Skip to content

[5.3] ReplaceOpaqueTypesWithUnderlyingTypes: Handle a type being "substituted" with itself. #31788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3043,12 +3043,21 @@ ProtocolConformanceRef ReplaceOpaqueTypesWithUnderlyingTypes::
operator()(CanType maybeOpaqueType, Type replacementType,
ProtocolDecl *protocol) const {
auto abstractRef = ProtocolConformanceRef(protocol);

auto archetypeAndRoot = getArchetypeAndRootOpaqueArchetype(maybeOpaqueType);
if (!archetypeAndRoot) {
assert(maybeOpaqueType->isTypeParameter() ||
maybeOpaqueType->is<ArchetypeType>());
return abstractRef;
if (maybeOpaqueType->isTypeParameter() ||
maybeOpaqueType->is<ArchetypeType>())
return abstractRef;

// SIL type lowering may have already substituted away the opaque type, in
// which case we'll end up "substituting" the same type.
if (maybeOpaqueType->isEqual(replacementType)) {
return inContext->getParentModule()
->lookupConformance(replacementType, protocol);
}

llvm_unreachable("origType should have been an opaque type or type parameter");
}

auto archetype = archetypeAndRoot->first;
Expand Down
16 changes: 16 additions & 0 deletions test/SILGen/opaque_type_lowering_in_substitution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-swift-emit-silgen -disable-availability-checking -verify %s
protocol P {}
extension Int: P {}

func foo() -> some P { return 0 }
func bar<T: P>(_ x: T) -> some P { return x }

struct Bas<T: P> { init(_: T) {} }

func abstraction_level<T>(x: T) -> (T) -> () {
return { _ in () }
}

func test() {
abstraction_level(x: Bas(bar(foo())))(Bas(bar(foo())))
}