Skip to content

Commit a1ae75b

Browse files
authored
Merge pull request #20455 from slavapestov/fix-misc-regressions
Fix a handful of regressions
2 parents aa3e16d + 6786ceb commit a1ae75b

15 files changed

+137
-88
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ class ASTMangler : public Mangler {
3333
protected:
3434
CanGenericSignature CurGenericSignature;
3535
ModuleDecl *Mod = nullptr;
36-
const DeclContext *DeclCtx = nullptr;
37-
GenericEnvironment *GenericEnv = nullptr;
3836

3937
/// Optimize out protocol names if a type only conforms to one protocol.
4038
bool OptimizeProtocolNames = true;
4139

42-
/// If enabled, Arche- and Alias types are mangled with context.
40+
/// If enabled, non-canonical types are allowed and type alias types get a
41+
/// special mangling.
4342
bool DWARFMangling;
4443

4544
/// If enabled, entities that ought to have names but don't get a placeholder.
@@ -148,8 +147,7 @@ class ASTMangler : public Mangler {
148147
std::string mangleKeyPathHashHelper(ArrayRef<CanType> indices,
149148
GenericSignature *signature);
150149

151-
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC,
152-
GenericEnvironment *GE);
150+
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC);
153151

154152
std::string mangleDeclType(const ValueDecl *decl);
155153

@@ -269,8 +267,7 @@ class ASTMangler : public Mangler {
269267
void appendClosureEntity(const AbstractClosureExpr *closure);
270268

271269
void appendClosureComponents(Type Ty, unsigned discriminator, bool isImplicit,
272-
const DeclContext *parentContext,
273-
const DeclContext *localContext);
270+
const DeclContext *parentContext);
274271

275272
void appendDefaultArgumentEntity(const DeclContext *ctx, unsigned index);
276273

lib/AST/ASTMangler.cpp

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "swift/AST/ASTContext.h"
1919
#include "swift/AST/ASTVisitor.h"
2020
#include "swift/AST/ExistentialLayout.h"
21-
#include "swift/AST/GenericEnvironment.h"
21+
#include "swift/AST/GenericSignature.h"
2222
#include "swift/AST/Initializer.h"
2323
#include "swift/AST/Module.h"
2424
#include "swift/AST/Ownership.h"
@@ -378,18 +378,15 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
378378
return finalize();
379379
}
380380

381-
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC,
382-
GenericEnvironment *GE) {
381+
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
383382
PrettyStackTraceType prettyStackTrace(Ty->getASTContext(),
384383
"mangling type for debugger", Ty);
385384

386-
GenericEnv = GE;
387385
DWARFMangling = true;
388386
beginMangling();
389387

390388
if (DC)
391389
bindGenericParameters(DC);
392-
DeclCtx = DC;
393390

394391
if (auto *fnType = Ty->getAs<AnyFunctionType>()) {
395392
appendFunction(fnType, false);
@@ -682,20 +679,10 @@ static bool shouldMangleAsGeneric(Type type) {
682679
if (!type)
683680
return false;
684681

685-
TypeBase *typePtr = type.getPointer();
686-
if (auto typeAlias = dyn_cast<NameAliasType>(typePtr))
682+
if (auto typeAlias = dyn_cast<NameAliasType>(type.getPointer()))
687683
return !typeAlias->getSubstitutionMap().empty();
688684

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;
685+
return type->isSpecialized();
699686
}
700687

701688
/// Mangle a type into the buffer.
@@ -1113,15 +1100,15 @@ void ASTMangler::appendBoundGenericArgs(Type type, bool &isFirstArgList) {
11131100

11141101
if (auto *unboundType = dyn_cast<UnboundGenericType>(typePtr)) {
11151102
if (Type parent = unboundType->getParent())
1116-
appendBoundGenericArgs(parent, isFirstArgList);
1103+
appendBoundGenericArgs(parent->getDesugaredType(), isFirstArgList);
11171104
} else if (auto *nominalType = dyn_cast<NominalType>(typePtr)) {
11181105
if (Type parent = nominalType->getParent())
1119-
appendBoundGenericArgs(parent, isFirstArgList);
1106+
appendBoundGenericArgs(parent->getDesugaredType(), isFirstArgList);
11201107
} else {
11211108
auto boundType = cast<BoundGenericType>(typePtr);
11221109
genericArgs = boundType->getGenericArgs();
11231110
if (Type parent = boundType->getParent())
1124-
appendBoundGenericArgs(parent, isFirstArgList);
1111+
appendBoundGenericArgs(parent->getDesugaredType(), isFirstArgList);
11251112
}
11261113
if (isFirstArgList) {
11271114
appendOperator("y");
@@ -2060,29 +2047,24 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt) {
20602047
void ASTMangler::appendClosureEntity(
20612048
const SerializedAbstractClosureExpr *closure) {
20622049
appendClosureComponents(closure->getType(), closure->getDiscriminator(),
2063-
closure->isImplicit(), closure->getParent(),
2064-
closure->getLocalContext());
2050+
closure->isImplicit(), closure->getParent());
20652051
}
20662052

20672053
void ASTMangler::appendClosureEntity(const AbstractClosureExpr *closure) {
20682054
appendClosureComponents(closure->getType(), closure->getDiscriminator(),
2069-
isa<AutoClosureExpr>(closure), closure->getParent(),
2070-
closure->getLocalContext());
2055+
isa<AutoClosureExpr>(closure), closure->getParent());
20712056
}
20722057

20732058
void ASTMangler::appendClosureComponents(Type Ty, unsigned discriminator,
2074-
bool isImplicit,
2075-
const DeclContext *parentContext,
2076-
const DeclContext *localContext) {
2077-
if (!DeclCtx) DeclCtx = localContext;
2078-
2059+
bool isImplicit,
2060+
const DeclContext *parentContext) {
20792061
assert(discriminator != AbstractClosureExpr::InvalidDiscriminator
20802062
&& "closure must be marked correctly with discriminator");
20812063

20822064
appendContext(parentContext);
20832065

20842066
if (!Ty)
2085-
Ty = ErrorType::get(localContext->getASTContext());
2067+
Ty = ErrorType::get(parentContext->getASTContext());
20862068

20872069
Ty = Ty->mapTypeOutOfContext();
20882070
appendType(Ty->getCanonicalType());
@@ -2226,7 +2208,6 @@ void ASTMangler::appendAccessorEntity(StringRef accessorKindCode,
22262208

22272209
void ASTMangler::appendEntity(const ValueDecl *decl, StringRef EntityOp,
22282210
bool isStatic) {
2229-
if (!DeclCtx) DeclCtx = decl->getInnermostDeclContext();
22302211
appendContextOf(decl);
22312212
appendDeclName(decl);
22322213
appendDeclType(decl);
@@ -2236,7 +2217,6 @@ void ASTMangler::appendEntity(const ValueDecl *decl, StringRef EntityOp,
22362217
}
22372218

22382219
void ASTMangler::appendEntity(const ValueDecl *decl) {
2239-
if (!DeclCtx) DeclCtx = decl->getInnermostDeclContext();
22402220
assert(!isa<ConstructorDecl>(decl));
22412221
assert(!isa<DestructorDecl>(decl));
22422222

lib/AST/Decl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,18 +1781,18 @@ bool AbstractStorageDecl::isFormallyResilient() const {
17811781
if (getAttrs().hasAttribute<FixedLayoutAttr>())
17821782
return false;
17831783

1784-
// Private and (unversioned) internal variables always have a
1785-
// fixed layout.
1786-
if (!getFormalAccessScope(/*useDC=*/nullptr,
1787-
/*treatUsableFromInlineAsPublic=*/true).isPublic())
1788-
return false;
1789-
17901784
// If we're an instance property of a nominal type, query the type.
17911785
auto *dc = getDeclContext();
17921786
if (!isStatic())
17931787
if (auto *nominalDecl = dc->getSelfNominalTypeDecl())
17941788
return nominalDecl->isResilient();
17951789

1790+
// Non-public global and static variables always have a
1791+
// fixed layout.
1792+
if (!getFormalAccessScope(/*useDC=*/nullptr,
1793+
/*treatUsableFromInlineAsPublic=*/true).isPublic())
1794+
return false;
1795+
17961796
return true;
17971797
}
17981798

lib/AST/USRGeneration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static inline StringRef getUSRSpacePrefix() {
3535
bool ide::printTypeUSR(Type Ty, raw_ostream &OS) {
3636
assert(!Ty->hasArchetype() && "cannot have contextless archetypes mangled.");
3737
Mangle::ASTMangler Mangler;
38-
OS << Mangler.mangleTypeForDebugger(Ty->getRValueType(), nullptr, nullptr);
38+
OS << Mangler.mangleTypeForDebugger(Ty->getRValueType(), nullptr);
3939
return false;
4040
}
4141

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,9 +2428,11 @@ IRGenModule::getAddrOfLLVMVariableOrGOTEquivalent(LinkEntity entity,
24282428
// Handle SILFunctions specially, because unlike other entities they aren't
24292429
// variables and aren't kept in the GlobalVars table.
24302430
if (entity.isSILFunction()) {
2431-
auto fn = getAddrOfSILFunction(entity.getSILFunction(), NotForDefinition);
2432-
if (entity.getSILFunction()->isDefinition()
2433-
&& !isAvailableExternally(entity.getSILFunction()->getLinkage())) {
2431+
auto *silFn = entity.getSILFunction();
2432+
auto fn = getAddrOfSILFunction(silFn, NotForDefinition);
2433+
if (silFn->isDefinition() &&
2434+
!isAvailableExternally(silFn->getLinkage()) &&
2435+
this == IRGen.getGenModule(silFn)) {
24342436
return {fn, ConstantReference::Direct};
24352437
}
24362438

lib/IRGen/GenKeyPath.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ getAccessorForComputedComponent(IRGenModule &IGM,
120120
// If it's only externally available, we need a local thunk to relative-
121121
// reference.
122122
if (requirements.empty() &&
123-
!LinkEntity::forSILFunction(accessor, false).isAvailableExternally(IGM)) {
124-
123+
!isAvailableExternally(accessor->getLinkage()) &&
124+
&IGM == IGM.IRGen.getGenModule(accessor)) {
125125
return IGM.getAddrOfSILFunction(accessor, NotForDefinition);
126126
}
127127
auto accessorFn = IGM.getAddrOfSILFunction(accessor, NotForDefinition);
@@ -1030,33 +1030,23 @@ emitKeyPathComponent(IRGenModule &IGM,
10301030
fields.add(llvm::ConstantExpr::getTruncOrBitCast(idValue, IGM.Int32Ty));
10311031
break;
10321032
}
1033-
1034-
if (isInstantiableOnce) {
1035-
// No generic arguments or indexes, so we can invoke the
1036-
// getter/setter as is.
1033+
1034+
// Push the accessors, possibly thunked to marshal generic environment.
1035+
fields.addRelativeAddress(
1036+
getAccessorForComputedComponent(IGM, component, Getter,
1037+
genericEnv, requirements,
1038+
hasSubscriptIndices));
1039+
if (settable)
10371040
fields.addRelativeAddress(
1038-
IGM.getAddrOfSILFunction(component.getComputedPropertyGetter(),
1039-
NotForDefinition));
1040-
if (settable)
1041-
fields.addRelativeAddress(
1042-
IGM.getAddrOfSILFunction(component.getComputedPropertySetter(),
1043-
NotForDefinition));
1044-
} else {
1041+
getAccessorForComputedComponent(IGM, component, Setter,
1042+
genericEnv, requirements,
1043+
hasSubscriptIndices));
1044+
1045+
if (!isInstantiableOnce) {
10451046
// If there's generic context or subscript indexes, embed as
10461047
// arguments in the component. Thunk the SIL-level accessors to give the
10471048
// runtime implementation a polymorphically-callable interface.
1048-
1049-
// Push the accessors, possibly thunked to marshal generic environment.
1050-
fields.addRelativeAddress(
1051-
getAccessorForComputedComponent(IGM, component, Getter,
1052-
genericEnv, requirements,
1053-
hasSubscriptIndices));
1054-
if (settable)
1055-
fields.addRelativeAddress(
1056-
getAccessorForComputedComponent(IGM, component, Setter,
1057-
genericEnv, requirements,
1058-
hasSubscriptIndices));
1059-
1049+
10601050
fields.addRelativeAddress(
10611051
getLayoutFunctionForComputedComponent(IGM, component,
10621052
genericEnv, requirements));

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
651651

652652
Mangle::ASTMangler Mangler;
653653
std::string Name = Mangler.mangleTypeForDebugger(
654-
Ty, DbgTy.getDeclContext(), DbgTy.getGenericEnvironment());
654+
Ty, DbgTy.getDeclContext());
655655
return BumpAllocatedString(Name);
656656
}
657657

test/DebugInfo/typealias.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ struct Generic<T> {
1919
}
2020

2121
typealias Specific = Generic<Int>
22+
typealias NotSpecific<T> = Generic<T>.Inner
23+
24+
struct Outer : P {
25+
enum Inner {
26+
case x
27+
}
28+
}
29+
30+
protocol P {
31+
typealias T = Outer
32+
}
33+
34+
// CHECK-DAG: [[INNER_TYPE:![0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type{{.*}} identifier: "$s9typealias5OuterV5InnerOD"
2235

2336
func main () {
2437
// CHECK-DAG: !DILocalVariable(name: "a",{{.*}} type: ![[DIEOFFSET]]
@@ -33,9 +46,23 @@ func main () {
3346
markUsed(c);
3447

3548
// CHECK-DAG: !DILocalVariable(name: "d", {{.*}} type: [[NONGENERIC_TYPE:![0-9]+]])
36-
// CHECK: [[NONGENERIC_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type{{.*}} identifier: "$s9typealias7GenericV5InnerOD"
49+
// CHECK: [[NONGENERIC_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type{{.*}} identifier: "$s9typealias7GenericV5InnerOySi_GD"
3750
let d: Specific.Inner = .value
3851
markUsed(d)
52+
53+
// CHECK-DAG: !DILocalVariable(name: "e", {{.*}} type: [[INNER_TYPE]])
54+
let e: Outer.T.Inner = .x
55+
markUsed(e)
56+
57+
// CHECK-DAG: !DILocalVariable(name: "f", {{.*}} type: [[OUTER_T_TYPE:![0-9]+]])
58+
// CHECK: [[OUTER_T_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$s9typealias1PP1TayAA5OuterV_GD"
59+
let f: Outer.T = Outer()
60+
markUsed(f)
61+
62+
// CHECK-DAG: !DILocalVariable(name: "g", {{.*}} type: [[GENERIC_TYPE:![0-9]+]])
63+
// CHECK: [[GENERIC_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$s9typealias11NotSpecificaySiGD"
64+
let g: NotSpecific<Int> = .value
65+
markUsed(g)
3966
}
4067

41-
main();
68+
main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct A {
2+
// note: not public
3+
var foo: Int { get { return 0 } set { } }
4+
}

test/IRGen/keypaths.sil

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,30 @@ entry:
249249
}
250250

251251
sil @k_id : $@convention(thin) () -> ()
252-
sil @k_get : $@convention(thin) (@in_guaranteed S) -> @out Int
252+
sil @k_get : $@convention(thin) (@in_guaranteed S) -> @out Int {
253+
bb0(%0 : @trivial $*Int, %1 : @trivial $*S):
254+
unreachable
255+
}
256+
257+
sil @l_get : $@convention(thin) (@in_guaranteed C) -> @out Int {
258+
bb0(%0 : @trivial $*Int, %1 : @trivial $*C):
259+
unreachable
260+
}
253261

254-
sil @l_get : $@convention(thin) (@in_guaranteed C) -> @out Int
255-
sil @l_set : $@convention(thin) (@in_guaranteed Int, @in_guaranteed C) -> ()
262+
sil @l_set : $@convention(thin) (@in_guaranteed Int, @in_guaranteed C) -> () {
263+
bb0(%0 : @trivial $*Int, %1 : @trivial $*C):
264+
unreachable
265+
}
256266

257-
sil @m_get : $@convention(thin) (@in_guaranteed S) -> @out @callee_guaranteed () -> @out ()
258-
sil @m_set : $@convention(thin) (@in_guaranteed @callee_guaranteed () -> @out (), @inout S) -> ()
267+
sil @m_get : $@convention(thin) (@in_guaranteed S) -> @out @callee_guaranteed () -> @out () {
268+
bb0(%0 : @trivial $*@callee_guaranteed () -> @out (), %1 : @trivial $*S):
269+
unreachable
270+
}
271+
272+
sil @m_set : $@convention(thin) (@in_guaranteed @callee_guaranteed () -> @out (), @inout S) -> () {
273+
bb0(%0 : @trivial $*@callee_guaranteed () -> @out (), %1 : @trivial $*S):
274+
unreachable
275+
}
259276

260277
sil @m2_get : $@convention(thin) (@in_guaranteed C2) -> @out @callee_guaranteed () -> @out ()
261278
sil @m2_set : $@convention(thin) (@in_guaranteed @callee_guaranteed () -> @out (), @inout C2) -> ()

test/IRGen/multithread_keypaths.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -c %S/Inputs/multithread_keypaths_other.swift %s -num-threads 2 -o %t/1.o -o %t/2.o -module-name multithread_keypaths
3+
// RUN: %target-swift-frontend -c %S/Inputs/multithread_keypaths_other.swift %s -num-threads 2 -o %t/1.o -o %t/2.o -module-name multithread_keypaths -enable-testing
4+
// RUN: %target-swift-frontend -c %S/Inputs/multithread_keypaths_other.swift %s -num-threads 2 -o %t/1.o -o %t/2.o -module-name multithread_keypaths -enable-resilience
5+
// RUN: %target-swift-frontend -c %S/Inputs/multithread_keypaths_other.swift %s -num-threads 2 -o %t/1.o -o %t/2.o -module-name multithread_keypaths -enable-testing -enable-resilience
6+
7+
func f(_ k: WritableKeyPath<A, Int>) {}
8+
9+
func g() {
10+
f(\A.foo)
11+
}

test/Inputs/resilient_struct.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,7 @@ public struct ResilientWeakRef {
9191
public struct ResilientRef {
9292
public var r: Referent
9393
}
94+
95+
public struct ResilientWithInternalField {
96+
var x: Int
97+
}

test/SILGen/struct_resilience.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
// RUN: %empty-directory(%t)
3-
// RUN: %target-swift-frontend -module-name struct_resilience -emit-module -enable-resilience -emit-module-path=%t/resilient_struct.swiftmodule -enable-sil-ownership -module-name=resilient_struct %S/../Inputs/resilient_struct.swift
4-
// RUN: %target-swift-emit-silgen -module-name struct_resilience -I %t -enable-sil-ownership -enable-resilience %s | %FileCheck %s
3+
// RUN: %target-swift-frontend -emit-module -enable-resilience -emit-module-path=%t/resilient_struct.swiftmodule -enable-sil-ownership %S/../Inputs/resilient_struct.swift
4+
// RUN: %target-swift-emit-silgen -I %t -enable-sil-ownership -enable-resilience %s | %FileCheck %s
55

66
import resilient_struct
77

0 commit comments

Comments
 (0)