Skip to content

Commit 8039b16

Browse files
authored
Merge pull request #8335 from slavapestov/fix-usr-subscript-mangling
IDE: Fix USR mangling for unnamed subscript parameters
2 parents 8a0006e + 344504e commit 8039b16

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -385,26 +385,43 @@ static bool isInPrivateOrLocalContext(const ValueDecl *D) {
385385
return isInPrivateOrLocalContext(nominal);
386386
}
387387

388-
static unsigned getUnnamedParamIndex(const Decl *D) {
389-
unsigned UnnamedIndex = 0;
388+
static bool getUnnamedParamIndex(const ParameterList *ParamList,
389+
const ParamDecl *D,
390+
unsigned &UnnamedIndex) {
391+
for (auto Param : *ParamList) {
392+
if (!Param->hasName()) {
393+
if (Param == D)
394+
return true;
395+
++UnnamedIndex;
396+
}
397+
}
398+
return false;
399+
}
400+
401+
static unsigned getUnnamedParamIndex(const ParamDecl *D) {
402+
if (auto SD = dyn_cast<SubscriptDecl>(D->getDeclContext())) {
403+
unsigned UnnamedIndex = 0;
404+
auto *ParamList = SD->getIndices();
405+
if (getUnnamedParamIndex(ParamList, D, UnnamedIndex))
406+
return UnnamedIndex;
407+
llvm_unreachable("param not found");
408+
}
409+
390410
ArrayRef<ParameterList *> ParamLists;
391411

392412
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D->getDeclContext())) {
393413
ParamLists = AFD->getParameterLists();
394-
} else if (auto ACE = dyn_cast<AbstractClosureExpr>(D->getDeclContext())) {
395-
ParamLists = ACE->getParameterLists();
396414
} else {
397-
llvm_unreachable("unhandled param context");
415+
auto ACE = cast<AbstractClosureExpr>(D->getDeclContext());
416+
ParamLists = ACE->getParameterLists();
398417
}
418+
419+
unsigned UnnamedIndex = 0;
399420
for (auto ParamList : ParamLists) {
400-
for (auto Param : *ParamList) {
401-
if (!Param->hasName()) {
402-
if (Param == D)
403-
return UnnamedIndex;
404-
++UnnamedIndex;
405-
}
406-
}
421+
if (getUnnamedParamIndex(ParamList, D, UnnamedIndex))
422+
return UnnamedIndex;
407423
}
424+
408425
llvm_unreachable("param not found");
409426
}
410427

@@ -427,9 +444,11 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
427444
}
428445

429446
if (decl->getDeclContext()->isLocalContext()) {
430-
if (isa<ParamDecl>(decl) && !decl->hasName()) {
431-
// Mangle unnamed params with their ordering.
432-
return appendOperator("L", Index(getUnnamedParamIndex(decl)));
447+
if (auto *paramDecl = dyn_cast<ParamDecl>(decl)) {
448+
if (!decl->hasName()) {
449+
// Mangle unnamed params with their ordering.
450+
return appendOperator("L", Index(getUnnamedParamIndex(paramDecl)));
451+
}
433452
}
434453
// Mangle local declarations with a numeric discriminator.
435454
return appendOperator("L", Index(decl->getLocalDiscriminator()));

test/IDE/print_usrs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class MyCls {
2727
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSicfs{{$}}
2828
set {}
2929
}
30+
// CHECK: [[@LINE+1]]:3 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitci{{$}}
31+
subscript(_: Int, _: Int) -> Float {
32+
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitcfg{{$}}
33+
get { return 0.0 }
34+
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitcfs{{$}}
35+
set {}
36+
}
3037
}
3138

3239
// CHECK: [[@LINE+1]]:7 s:14swift_ide_test12GenericClassC{{$}}

0 commit comments

Comments
 (0)