Skip to content

Commit 022a2ff

Browse files
authored
Merge pull request #20353 from DougGregor/mangle-nongeneric-typealias
2 parents 14dbc9a + 5e439f7 commit 022a2ff

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,27 @@ static bool isStdlibType(const TypeDecl *decl) {
677677
return dc->isModuleScopeContext() && dc->getParentModule()->isStdlibModule();
678678
}
679679

680+
/// Whether to mangle the given type as generic.
681+
static bool shouldMangleAsGeneric(Type type) {
682+
if (!type)
683+
return false;
684+
685+
TypeBase *typePtr = type.getPointer();
686+
if (auto typeAlias = dyn_cast<NameAliasType>(typePtr))
687+
return !typeAlias->getSubstitutionMap().empty();
688+
689+
if (auto bound = dyn_cast<BoundGenericType>(typePtr))
690+
return true;
691+
692+
if (auto nominal = dyn_cast<NominalType>(typePtr))
693+
return shouldMangleAsGeneric(nominal->getParent());
694+
695+
if (auto unbound = dyn_cast<UnboundGenericType>(typePtr))
696+
return shouldMangleAsGeneric(unbound->getParent());
697+
698+
return false;
699+
}
700+
680701
/// Mangle a type into the buffer.
681702
///
682703
void ASTMangler::appendType(Type type) {
@@ -845,11 +866,12 @@ void ASTMangler::appendType(Type type) {
845866
case TypeKind::BoundGenericClass:
846867
case TypeKind::BoundGenericEnum:
847868
case TypeKind::BoundGenericStruct: {
848-
// We can't use getAnyNominal here because this can be TypeAliasDecl only
849-
// in case of UnboundGenericType. Such mangling happens in, for instance,
850-
// SourceKit 'cursorinfo' request.
851-
auto *Decl = type->getAnyGeneric();
852-
if (type->isSpecialized()) {
869+
GenericTypeDecl *Decl;
870+
if (auto typeAlias = dyn_cast<NameAliasType>(type.getPointer()))
871+
Decl = typeAlias->getDecl();
872+
else
873+
Decl = type->getAnyGeneric();
874+
if (shouldMangleAsGeneric(type)) {
853875
// Try to mangle the entire name as a substitution.
854876
if (tryMangleSubstitution(tybase))
855877
return;
@@ -869,7 +891,7 @@ void ASTMangler::appendType(Type type) {
869891
addSubstitution(type.getPointer());
870892
return;
871893
}
872-
appendAnyGenericType(Decl);
894+
appendAnyGenericType(type->getAnyGeneric());
873895
return;
874896
}
875897

@@ -1155,14 +1177,23 @@ static bool containsRetroactiveConformance(
11551177
}
11561178

11571179
void ASTMangler::appendRetroactiveConformances(Type type) {
1158-
auto nominal = type->getAnyNominal();
1159-
if (!nominal) return;
1180+
// Dig out the substitution map to use.
1181+
SubstitutionMap subMap;
1182+
ModuleDecl *module;
1183+
if (auto typeAlias = dyn_cast<NameAliasType>(type.getPointer())) {
1184+
module = Mod ? Mod : typeAlias->getDecl()->getModuleContext();
1185+
subMap = typeAlias->getSubstitutionMap();
1186+
} else {
1187+
if (type->hasUnboundGenericType())
1188+
return;
11601189

1161-
auto genericSig = nominal->getGenericSignatureOfContext();
1162-
if (!genericSig) return;
1190+
auto nominal = type->getAnyNominal();
1191+
if (!nominal) return;
1192+
1193+
module = Mod ? Mod : nominal->getModuleContext();
1194+
subMap = type->getContextSubstitutionMap(module, nominal);
1195+
}
11631196

1164-
auto module = Mod ? Mod : nominal->getModuleContext();
1165-
auto subMap = type->getContextSubstitutionMap(module, nominal);
11661197
if (subMap.empty()) return;
11671198

11681199
unsigned numProtocolRequirements = 0;

test/DebugInfo/typealias.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ class DWARF {
1212
fileprivate static func usePrivateType() -> PrivateType { return () }
1313
}
1414

15+
struct Generic<T> {
16+
enum Inner {
17+
case value
18+
}
19+
}
20+
21+
typealias Specific = Generic<Int>
22+
1523
func main () {
1624
// CHECK-DAG: !DILocalVariable(name: "a",{{.*}} type: ![[DIEOFFSET]]
1725
let a : DWARF.DIEOffset = 123
@@ -23,6 +31,11 @@ func main () {
2331
// CHECK-DAG: !DILocalVariable(name: "c",{{.*}} type: ![[PRIVATETYPE]]
2432
let c = DWARF.usePrivateType()
2533
markUsed(c);
34+
35+
// CHECK-DAG: !DILocalVariable(name: "d", {{.*}} type: [[NONGENERIC_TYPE:![0-9]+]])
36+
// CHECK: [[NONGENERIC_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type{{.*}} identifier: "$s9typealias7GenericV5InnerOD"
37+
let d: Specific.Inner = .value
38+
markUsed(d)
2639
}
2740

2841
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %target-swift-frontend -emit-ir -g -o - %s
2+
3+
// REQUIRES: objc_interop
4+
5+
import Foundation
6+
public final class Foo: NSObject, Collection {
7+
public var storage = [String: Any]()
8+
9+
public typealias DictionaryType = [String: Any]
10+
public typealias IndexDistance = Int
11+
public typealias Indices = DictionaryType.Indices
12+
public typealias Iterator = DictionaryType.Iterator
13+
public typealias SubSequence = DictionaryType.SubSequence
14+
public typealias Index = DictionaryType.Index
15+
16+
public var startIndex: Index { return storage.startIndex }
17+
public var endIndex: DictionaryType.Index { return storage.endIndex }
18+
public subscript(position: Index) -> Iterator.Element {
19+
return storage[position]
20+
}
21+
22+
public subscript(bounds: Range<Index>) -> SubSequence {
23+
return storage[bounds]
24+
}
25+
26+
public var indices: Indices { return storage.indices }
27+
28+
public subscript(key: String) -> Any? {
29+
get { return storage[key] }
30+
set { storage[key] = newValue }
31+
}
32+
33+
public func index(after i: Index) -> Index { return storage.index(after: i) }
34+
35+
public func makeIterator() -> DictionaryIterator<String, Any> {
36+
return storage.makeIterator()
37+
}
38+
39+
public override func value(forKey key: String) -> Any? {
40+
return storage[key]
41+
}
42+
43+
public override func value(forUndefinedKey key: String) -> Any? {
44+
return storage[key]
45+
}
46+
}

0 commit comments

Comments
 (0)