Skip to content

IDE: Fix USR mangling for unnamed subscript parameters #8335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,26 +385,43 @@ static bool isInPrivateOrLocalContext(const ValueDecl *D) {
return isInPrivateOrLocalContext(nominal);
}

static unsigned getUnnamedParamIndex(const Decl *D) {
unsigned UnnamedIndex = 0;
static bool getUnnamedParamIndex(const ParameterList *ParamList,
const ParamDecl *D,
unsigned &UnnamedIndex) {
for (auto Param : *ParamList) {
if (!Param->hasName()) {
if (Param == D)
return true;
++UnnamedIndex;
}
}
return false;
}

static unsigned getUnnamedParamIndex(const ParamDecl *D) {
if (auto SD = dyn_cast<SubscriptDecl>(D->getDeclContext())) {
unsigned UnnamedIndex = 0;
auto *ParamList = SD->getIndices();
if (getUnnamedParamIndex(ParamList, D, UnnamedIndex))
return UnnamedIndex;
llvm_unreachable("param not found");
}

ArrayRef<ParameterList *> ParamLists;

if (auto AFD = dyn_cast<AbstractFunctionDecl>(D->getDeclContext())) {
ParamLists = AFD->getParameterLists();
} else if (auto ACE = dyn_cast<AbstractClosureExpr>(D->getDeclContext())) {
ParamLists = ACE->getParameterLists();
} else {
llvm_unreachable("unhandled param context");
auto ACE = cast<AbstractClosureExpr>(D->getDeclContext());
ParamLists = ACE->getParameterLists();
}

unsigned UnnamedIndex = 0;
for (auto ParamList : ParamLists) {
for (auto Param : *ParamList) {
if (!Param->hasName()) {
if (Param == D)
return UnnamedIndex;
++UnnamedIndex;
}
}
if (getUnnamedParamIndex(ParamList, D, UnnamedIndex))
return UnnamedIndex;
}

llvm_unreachable("param not found");
}

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

if (decl->getDeclContext()->isLocalContext()) {
if (isa<ParamDecl>(decl) && !decl->hasName()) {
// Mangle unnamed params with their ordering.
return appendOperator("L", Index(getUnnamedParamIndex(decl)));
if (auto *paramDecl = dyn_cast<ParamDecl>(decl)) {
if (!decl->hasName()) {
// Mangle unnamed params with their ordering.
return appendOperator("L", Index(getUnnamedParamIndex(paramDecl)));
}
}
// Mangle local declarations with a numeric discriminator.
return appendOperator("L", Index(decl->getLocalDiscriminator()));
Expand Down
7 changes: 7 additions & 0 deletions test/IDE/print_usrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class MyCls {
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSicfs{{$}}
set {}
}
// CHECK: [[@LINE+1]]:3 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitci{{$}}
subscript(_: Int, _: Int) -> Float {
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitcfg{{$}}
get { return 0.0 }
// CHECK: [[@LINE+1]]:5 s:14swift_ide_test5MyClsC9subscriptSfSi_Sitcfs{{$}}
set {}
}
}

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