Skip to content

Commit cda25b7

Browse files
committed
IRGen: Mangle DWARF types with the right generic signature
The symptom here was a crash in getConformanceAccessPath(), but the root cause was that the ASTMangler used an incorrect generic signature when looking up the conformance path for a type parameter. The mangler requires a generic signature to be set to properly mangle types containing retroactive conformances. However, DebugInfo just used the IRGenModule's current signature, which wasn't always the same as the signature describing the archetypes found inside the SILFunction. Fixes rdar://problem/81683650 / https://bugs.swift.org/browse/SR-15046.
1 parent 07f889f commit cda25b7

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "GenType.h"
2222
#include "swift/AST/ASTMangler.h"
2323
#include "swift/AST/Expr.h"
24+
#include "swift/AST/GenericEnvironment.h"
2425
#include "swift/AST/IRGenOptions.h"
2526
#include "swift/AST/Module.h"
2627
#include "swift/AST/ModuleLoader.h"
@@ -825,9 +826,29 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
825826
return MetadataTypeDeclCache.find(DbgTy.getDecl()->getName().str())
826827
->getKey();
827828

829+
// This is a bit of a hack. We need a generic signature to use for mangling.
830+
// If we started with an interface type, just use IGM.getCurGenericContext(),
831+
// since callers that use interface types typically push a signature that way.
832+
//
833+
// Otherwise, if we have a contextual type, find an archetype and ask it for
834+
// it's generic signature. The context generic signature from the IRGenModule
835+
// is unlikely to be useful here.
836+
GenericSignature Sig;
828837
Type Ty = DbgTy.getType();
829-
if (Ty->hasArchetype())
838+
if (Ty->hasArchetype()) {
839+
Ty.findIf([&](Type t) -> bool {
840+
if (auto *archetypeTy = t->getAs<PrimaryArchetypeType>()) {
841+
Sig = archetypeTy->getGenericEnvironment()->getGenericSignature();
842+
return true;
843+
}
844+
845+
return false;
846+
});
847+
830848
Ty = Ty->mapTypeOutOfContext();
849+
} else {
850+
Sig = IGM.getCurGenericContext();
851+
}
831852

832853
// Strip off top level of type sugar (except for type aliases).
833854
// We don't want Optional<T> and T? to get different debug types.
@@ -852,7 +873,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
852873
IGM.getSILModule());
853874

854875
Mangle::ASTMangler Mangler;
855-
GenericSignature Sig = IGM.getCurGenericContext();
856876
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);
857877

858878
if (!Opts.DisableRoundTripDebugTypes) {

test/IRGen/retroactive_conformance_path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -module-name=test %s -o %t/a.out
2+
// RUN: %target-build-swift -module-name=test %s -o %t/a.out -requirement-machine=verify
33
// RUN: %target-run %t/a.out | %FileCheck %s
44
// REQUIRES: executable_test
55
// REQUIRES: CPU=arm64 || CPU=x86_64
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -emit-ir -g -primary-file %s -requirement-machine=verify
2+
3+
public struct TestType<T: Error> { }
4+
5+
extension Array: Error where Element: Error { }
6+
7+
public struct GenericType<G> {
8+
public func test<T>(_ value: TestType<[T]>) { }
9+
}

0 commit comments

Comments
 (0)