From 88814e4fbfa91ac93325c188601bd527180dacac Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 24 Apr 2025 14:36:12 -0400 Subject: [PATCH] SIL: Use SubstitutionMap::mapIntoTypeExpansionContext() in SILTypeSubstituter I made a mistake in 47156e006bf0029c59edfd072392ae17045db73d. There was a call to call to forAbstract() in SILTypeSubstituter that passed in the wrong subject ype. This call was inside SILTypeSubstituter's own implementation of replacing opaque types with underlying types in a substitution map. This duplicates an existing utility method in SubstitutionMap anyway, so let's just use that instead. Fixes rdar://149353285. --- lib/SIL/IR/SILTypeSubstitution.cpp | 15 +------------ test/SILGen/subst_function_type_opaque.swift | 23 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 test/SILGen/subst_function_type_opaque.swift diff --git a/lib/SIL/IR/SILTypeSubstitution.cpp b/lib/SIL/IR/SILTypeSubstitution.cpp index 6c2cee5788cb4..f9b2fbc35bf77 100644 --- a/lib/SIL/IR/SILTypeSubstitution.cpp +++ b/lib/SIL/IR/SILTypeSubstitution.cpp @@ -68,20 +68,7 @@ class SILTypeSubstituter : if (!typeExpansionContext.shouldLookThroughOpaqueTypeArchetypes()) return subs; - return subs.subst([&](SubstitutableType *s) -> Type { - return substOpaqueTypesWithUnderlyingTypes(s->getCanonicalType(), - typeExpansionContext); - }, [&](CanType dependentType, - Type conformingReplacementType, - ProtocolDecl *conformedProtocol) -> ProtocolConformanceRef { - return substOpaqueTypesWithUnderlyingTypes( - ProtocolConformanceRef::forAbstract(conformingReplacementType, - conformedProtocol), - conformingReplacementType->getCanonicalType(), - typeExpansionContext); - }, - SubstFlags::SubstituteOpaqueArchetypes | - SubstFlags::PreservePackExpansionLevel); + return subs.mapIntoTypeExpansionContext(typeExpansionContext); } // Substitute a function type. diff --git a/test/SILGen/subst_function_type_opaque.swift b/test/SILGen/subst_function_type_opaque.swift new file mode 100644 index 0000000000000..eaa7483cf4207 --- /dev/null +++ b/test/SILGen/subst_function_type_opaque.swift @@ -0,0 +1,23 @@ +// RUN: %target-swift-emit-silgen %s + +public protocol P {} + +extension P { +} + +public struct S: P { + public func callAsFunction(_: () -> V) { } +} + +public func f() -> some P { + S() +} + +public struct G: P { + public init(_: () -> T) {} +} + +S() { + return G { return f() } +} +