Skip to content

Commit 96d486a

Browse files
committed
Fix name mangling for structural opaque result types.
We weren't using the ordinal value at all, but instead had a hard-coded zero.
1 parent b2aa851 commit 96d486a

File tree

3 files changed

+60
-31
lines changed

3 files changed

+60
-31
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ class ASTMangler : public Mangler {
339339
}
340340

341341
void appendBoundGenericArgs(Type type, GenericSignature sig,
342-
bool &isFirstArgList);
342+
bool &isFirstArgList,
343+
const ValueDecl *forDecl = nullptr);
343344

344345
/// Append the bound generics arguments for the given declaration context
345346
/// based on a complete substitution map.
@@ -349,18 +350,21 @@ class ASTMangler : public Mangler {
349350
unsigned appendBoundGenericArgs(DeclContext *dc,
350351
GenericSignature sig,
351352
SubstitutionMap subs,
352-
bool &isFirstArgList);
353+
bool &isFirstArgList,
354+
const ValueDecl *forDecl = nullptr);
353355

354356
/// Append the bound generic arguments as a flat list, disregarding depth.
355357
void appendFlatGenericArgs(SubstitutionMap subs,
356-
GenericSignature sig);
358+
GenericSignature sig,
359+
const ValueDecl *forDecl = nullptr);
357360

358361
/// Append any retroactive conformances.
359362
void appendRetroactiveConformances(Type type, GenericSignature sig);
360363
void appendRetroactiveConformances(SubstitutionMap subMap,
361364
GenericSignature sig,
362365
ModuleDecl *fromModule);
363-
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig);
366+
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig,
367+
const ValueDecl *forDecl = nullptr);
364368

365369
void appendContextOf(const ValueDecl *decl);
366370

lib/AST/ASTMangler.cpp

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
11611161

11621162
appendAnyGenericType(decl);
11631163
bool isFirstArgList = true;
1164-
appendBoundGenericArgs(type, sig, isFirstArgList);
1164+
appendBoundGenericArgs(type, sig, isFirstArgList, forDecl);
11651165
appendRetroactiveConformances(type, sig);
11661166
appendOperator("G");
11671167
addTypeSubstitution(type, sig);
@@ -1290,7 +1290,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
12901290
} else {
12911291
appendAnyGenericType(Decl);
12921292
bool isFirstArgList = true;
1293-
appendBoundGenericArgs(type, sig, isFirstArgList);
1293+
appendBoundGenericArgs(type, sig, isFirstArgList, forDecl);
12941294
appendRetroactiveConformances(type, sig);
12951295
appendOperator("G");
12961296
}
@@ -1302,7 +1302,8 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13021302
}
13031303

13041304
case TypeKind::SILFunction:
1305-
return appendImplFunctionType(cast<SILFunctionType>(tybase), sig);
1305+
return appendImplFunctionType(cast<SILFunctionType>(tybase), sig,
1306+
forDecl);
13061307

13071308
// type ::= archetype
13081309
case TypeKind::PrimaryArchetype:
@@ -1329,13 +1330,11 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13291330
bool isFirstArgList = true;
13301331
appendBoundGenericArgs(opaqueDecl, sig,
13311332
opaqueType->getSubstitutions(),
1332-
isFirstArgList);
1333+
isFirstArgList, forDecl);
13331334
appendRetroactiveConformances(opaqueType->getSubstitutions(), sig,
13341335
opaqueDecl->getParentModule());
13351336

1336-
// TODO: If we support multiple opaque types in a return, put the
1337-
// ordinal for this archetype here.
1338-
appendOperator("Qo", Index(0));
1337+
appendOperator("Qo", Index(opaqueType->getOrdinal()));
13391338

13401339
addTypeSubstitution(type, sig);
13411340
return;
@@ -1351,7 +1350,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13511350
if (tryMangleTypeSubstitution(nestedType, sig))
13521351
return;
13531352

1354-
appendType(opaque, sig);
1353+
appendType(opaque, sig, forDecl);
13551354
bool isAssocTypeAtDepth = false;
13561355
appendAssocType(nestedType->getInterfaceType(),
13571356
sig, isAssocTypeAtDepth);
@@ -1360,7 +1359,7 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13601359
return;
13611360
}
13621361

1363-
appendType(nestedType->getParent(), sig);
1362+
appendType(nestedType->getParent(), sig, forDecl);
13641363
appendIdentifier(nestedType->getName().str());
13651364
appendOperator("Qa");
13661365
return;
@@ -1516,20 +1515,22 @@ void ASTMangler::appendOpWithGenericParamIndex(StringRef Op,
15161515
}
15171516

15181517
void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs,
1519-
GenericSignature sig) {
1518+
GenericSignature sig,
1519+
const ValueDecl *forDecl) {
15201520
appendOperator("y");
15211521

15221522
for (auto replacement : subs.getReplacementTypes()) {
15231523
if (replacement->hasArchetype())
15241524
replacement = replacement->mapTypeOutOfContext();
1525-
appendType(replacement, sig);
1525+
appendType(replacement, sig, forDecl);
15261526
}
15271527
}
15281528

15291529
unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
15301530
GenericSignature sig,
15311531
SubstitutionMap subs,
1532-
bool &isFirstArgList) {
1532+
bool &isFirstArgList,
1533+
const ValueDecl *forDecl) {
15331534
auto decl = dc->getInnermostDeclarationDeclContext();
15341535
if (!decl) return 0;
15351536

@@ -1543,7 +1544,8 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
15431544

15441545
// Handle the generic arguments of the parent.
15451546
unsigned currentGenericParamIdx =
1546-
appendBoundGenericArgs(decl->getDeclContext(), sig, subs, isFirstArgList);
1547+
appendBoundGenericArgs(decl->getDeclContext(), sig, subs, isFirstArgList,
1548+
forDecl);
15471549

15481550
// If this is potentially a generic context, emit a generic argument list.
15491551
if (auto genericContext = decl->getAsGenericContext()) {
@@ -1568,7 +1570,7 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
15681570
if (replacementType->hasArchetype())
15691571
replacementType = replacementType->mapTypeOutOfContext();
15701572

1571-
appendType(replacementType, sig);
1573+
appendType(replacementType, sig, forDecl);
15721574
}
15731575
}
15741576
}
@@ -1577,29 +1579,33 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
15771579
}
15781580

15791581
void ASTMangler::appendBoundGenericArgs(Type type, GenericSignature sig,
1580-
bool &isFirstArgList) {
1582+
bool &isFirstArgList,
1583+
const ValueDecl *forDecl) {
15811584
TypeBase *typePtr = type.getPointer();
15821585
ArrayRef<Type> genericArgs;
15831586
if (auto *typeAlias = dyn_cast<TypeAliasType>(typePtr)) {
15841587
appendBoundGenericArgs(typeAlias->getDecl(), sig,
15851588
typeAlias->getSubstitutionMap(),
1586-
isFirstArgList);
1589+
isFirstArgList, forDecl);
15871590
return;
15881591
}
15891592

15901593
if (auto *unboundType = dyn_cast<UnboundGenericType>(typePtr)) {
15911594
if (Type parent = unboundType->getParent())
1592-
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList);
1595+
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList,
1596+
forDecl);
15931597
} else if (auto *nominalType = dyn_cast<NominalType>(typePtr)) {
15941598
if (Type parent = nominalType->getParent())
1595-
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList);
1599+
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList,
1600+
forDecl);
15961601
} else {
15971602
auto boundType = cast<BoundGenericType>(typePtr);
15981603
genericArgs = boundType->getGenericArgs();
15991604
if (Type parent = boundType->getParent()) {
16001605
GenericTypeDecl *decl = boundType->getAnyGeneric();
16011606
if (!getSpecialManglingContext(decl, UseObjCRuntimeNames))
1602-
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList);
1607+
appendBoundGenericArgs(parent->getDesugaredType(), sig, isFirstArgList,
1608+
forDecl);
16031609
}
16041610
}
16051611
if (isFirstArgList) {
@@ -1609,7 +1615,7 @@ void ASTMangler::appendBoundGenericArgs(Type type, GenericSignature sig,
16091615
appendOperator("_");
16101616
}
16111617
for (Type arg : genericArgs) {
1612-
appendType(arg, sig);
1618+
appendType(arg, sig, forDecl);
16131619
}
16141620
}
16151621

@@ -1777,7 +1783,8 @@ getResultDifferentiability(SILResultDifferentiability diffKind) {
17771783
}
17781784

17791785
void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
1780-
GenericSignature outerGenericSig) {
1786+
GenericSignature outerGenericSig,
1787+
const ValueDecl *forDecl) {
17811788

17821789
llvm::SmallVector<char, 32> OpArgs;
17831790

@@ -1880,7 +1887,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
18801887
OpArgs.push_back(getParamConvention(param.getConvention()));
18811888
if (auto diffKind = getParamDifferentiability(param.getDifferentiability()))
18821889
OpArgs.push_back(*diffKind);
1883-
appendType(param.getInterfaceType(), sig);
1890+
appendType(param.getInterfaceType(), sig, forDecl);
18841891
}
18851892

18861893
// Mangle the results.
@@ -1889,30 +1896,30 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
18891896
if (auto diffKind =
18901897
getResultDifferentiability(result.getDifferentiability()))
18911898
OpArgs.push_back(*diffKind);
1892-
appendType(result.getInterfaceType(), sig);
1899+
appendType(result.getInterfaceType(), sig, forDecl);
18931900
}
18941901

18951902
// Mangle the yields.
18961903
for (auto yield : fn->getYields()) {
18971904
OpArgs.push_back('Y');
18981905
OpArgs.push_back(getParamConvention(yield.getConvention()));
1899-
appendType(yield.getInterfaceType(), sig);
1906+
appendType(yield.getInterfaceType(), sig, forDecl);
19001907
}
19011908

19021909
// Mangle the error result if present.
19031910
if (fn->hasErrorResult()) {
19041911
auto error = fn->getErrorResult();
19051912
OpArgs.push_back('z');
19061913
OpArgs.push_back(getResultConvention(error.getConvention()));
1907-
appendType(error.getInterfaceType(), sig);
1914+
appendType(error.getInterfaceType(), sig, forDecl);
19081915
}
19091916

19101917
if (auto invocationSig = fn->getInvocationGenericSignature()) {
19111918
appendGenericSignature(invocationSig);
19121919
sig = outerGenericSig;
19131920
}
19141921
if (auto subs = fn->getInvocationSubstitutions()) {
1915-
appendFlatGenericArgs(subs, sig);
1922+
appendFlatGenericArgs(subs, sig, forDecl);
19161923
appendRetroactiveConformances(subs, sig, Mod);
19171924
}
19181925
if (auto subs = fn->getPatternSubstitutions()) {
@@ -1921,7 +1928,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
19211928
fn->getInvocationGenericSignature()
19221929
? fn->getInvocationGenericSignature()
19231930
: outerGenericSig;
1924-
appendFlatGenericArgs(subs, sig);
1931+
appendFlatGenericArgs(subs, sig, forDecl);
19251932
appendRetroactiveConformances(subs, sig, Mod);
19261933
}
19271934

test/IRGen/opaque_result_type_debug.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public struct Foo {
2222
}
2323
}
2424

25+
public class C: P { }
26+
27+
public func bar() -> [(some P, (some AnyObject & P)?)] {
28+
return [(1, C())]
29+
}
30+
2531
#else
2632

2733
import opaque_result_type_debug_other
@@ -43,6 +49,12 @@ public func bar<T: P>(genericValue: T) {
4349

4450
let opaqueSubValue = Foo()[]
4551
use(opaqueSubValue)
52+
53+
let opaqueArray = bar()
54+
let opaqueArrayFirst = opaqueArray[0].0
55+
use(opaqueArrayFirst)
56+
let opaqueArraySecond = opaqueArray[0].1!
57+
use(opaqueArraySecond)
4658
}
4759

4860
#endif
@@ -56,3 +68,9 @@ public func bar<T: P>(genericValue: T) {
5668
// CHECK-DAG: {{![0-9]+}} = !DILocalVariable(name: "opaqueValue",{{.*}} type: ![[LET_OPAQUE_TYPE]])
5769
// CHECK-DAG: {{![0-9]+}} = !DILocalVariable(name: "opaquePropValue",{{.*}} type: ![[LET_OPAQUE_PROP_TYPE]])
5870
// CHECK-DAG: {{![0-9]+}} = !DILocalVariable(name: "opaqueSubValue",{{.*}} type: ![[LET_OPAQUE_SUB_TYPE]])
71+
// CHECK-DAG: ![[OPAQUE_ARRAY_FIRST_TYPE:[0-9]+]] = !DICompositeType({{.*}} name: "$s30opaque_result_type_debug_other3barSayQr_QrSgtGyFQOyQo_D"
72+
// CHECK-DAG: ![[LET_OPAQUE_ARRAY_FIRST_TYPE:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[OPAQUE_ARRAY_FIRST_TYPE]])
73+
// CHECK-DAG: {{![0-9]+}} = !DILocalVariable(name: "opaqueArrayFirst",{{.*}} type: ![[LET_OPAQUE_ARRAY_FIRST_TYPE]])
74+
// CHECK-DAG: ![[OPAQUE_ARRAY_SECOND_TYPE:[0-9]+]] = !DICompositeType({{.*}} name: "$s30opaque_result_type_debug_other3barSayQr_QrSgtGyFQOyQo0_D"
75+
// CHECK-DAG: ![[LET_OPAQUE_ARRAY_SECOND_TYPE:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type, baseType: ![[OPAQUE_ARRAY_SECOND_TYPE]])
76+
// CHECK-DAG: {{![0-9]+}} = !DILocalVariable(name: "opaqueArraySecond",{{.*}} type: ![[LET_OPAQUE_ARRAY_SECOND_TYPE]])

0 commit comments

Comments
 (0)