mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Emit references to shadowed variables in if-let shorthands as variables when indexing locals is enabled.
Track the original-decl/captured decl as part of the symbol passed to the IndexConsumer. This allows the Rename consumer to check if the symbol is a shadowed reference to a decl being renamed, without the index skipping the other relevant output when visiting shadowing variables. https://github.com/swiftlang/swift/issues/76805
This commit is contained in:
@@ -65,6 +65,7 @@ struct IndexSymbol : IndexRelation {
|
||||
SmallVector<IndexRelation, 3> Relations;
|
||||
unsigned line = 0;
|
||||
unsigned column = 0;
|
||||
const Decl *originalDecl = nullptr;
|
||||
|
||||
IndexSymbol() = default;
|
||||
|
||||
|
||||
@@ -850,13 +850,13 @@ private:
|
||||
if (Loc.isInvalid() || isSuppressed(Loc))
|
||||
return true;
|
||||
|
||||
IndexSymbol Info;
|
||||
|
||||
// Dig back to the original captured variable
|
||||
if (auto *VD = dyn_cast<VarDecl>(D)) {
|
||||
D = firstDecl(D);
|
||||
Info.originalDecl = firstDecl(D);
|
||||
}
|
||||
|
||||
IndexSymbol Info;
|
||||
|
||||
if (Data.isImplicit)
|
||||
Info.roles |= (unsigned)SymbolRole::Implicit;
|
||||
|
||||
@@ -1580,13 +1580,17 @@ bool IndexSwiftASTWalker::report(ValueDecl *D) {
|
||||
if (!reportRef(shadowedDecl, loc, info, AccessKind::Read))
|
||||
return false;
|
||||
|
||||
// Suppress the reference if there is any (it is implicit and hence
|
||||
// already skipped in the shorthand if let case, but explicit in the
|
||||
// captured case).
|
||||
suppressRefAtLoc(loc);
|
||||
// Keep the refs and definition for the real decl when indexing locals,
|
||||
// so the references to the shadowing variable are distinct.
|
||||
if (!IdxConsumer.indexLocals()) {
|
||||
// Suppress the reference if there is any (it is implicit and hence
|
||||
// already skipped in the shorthand if let case, but explicit in the
|
||||
// captured case).
|
||||
suppressRefAtLoc(loc);
|
||||
|
||||
// Skip the definition of a shadowed decl
|
||||
return true;
|
||||
// Skip the definition of a shadowed decl
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (startEntityDecl(D)) {
|
||||
|
||||
@@ -221,7 +221,7 @@ private:
|
||||
bool finishDependency(bool isClangModule) override { return true; }
|
||||
|
||||
Action startSourceEntity(const IndexSymbol &symbol) override {
|
||||
if (symbol.decl != declToRename) {
|
||||
if (symbol.decl != declToRename && symbol.originalDecl != declToRename) {
|
||||
return IndexDataConsumer::Continue;
|
||||
}
|
||||
auto loc = indexSymbolToRenameLoc(symbol);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s --include-locals | %FileCheck %s
|
||||
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s
|
||||
|
||||
// RUN: %empty-directory(%t)
|
||||
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s -include-locals | %FileCheck %s -check-prefix=CHECK_LOCALS
|
||||
|
||||
// The index will output references to the shadowed-declaration rather than
|
||||
// the one defined by the shorthand if-let or capture. It also skips
|
||||
@@ -9,61 +12,76 @@ struct ShadowedTest {
|
||||
let shadowedVar: Int?? = 1
|
||||
|
||||
func localShadowTest() {
|
||||
// CHECK: [[@LINE+1]]:9 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Def
|
||||
// CHECK_LOCALS: [[@LINE+1]]:9 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Def
|
||||
let localShadowedVar: Int? = 2
|
||||
|
||||
// CHECK-NOT: [[@LINE+2]]:12 {{.*}} localShadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+1]]:12 | variable(local)/Swift{{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL0_Sivp {{.*}}Def
|
||||
if let localShadowedVar {
|
||||
// CHECK: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS-NOT: [[@LINE+2]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}
|
||||
// CHECK_LOCALS: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL0_Sivp {{.*}}Ref
|
||||
_ = localShadowedVar
|
||||
}
|
||||
|
||||
// CHECK-NOT: [[@LINE+2]]:12 {{.*}} localShadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+1]]:12 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL1_SiSgvp {{.*}}Def
|
||||
_ = { [localShadowedVar] in
|
||||
// CHECK: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS-NOT: [[@LINE+2]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL_SiSgvp {{.*}}
|
||||
// CHECK_LOCALS: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV011localShadowE0yyF0fD3VarL1_SiSgvp {{.*}}Ref
|
||||
_ = localShadowedVar
|
||||
}
|
||||
}
|
||||
|
||||
func shadowTest() {
|
||||
// CHECK_LOCALS: [[@LINE+4]]:12 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL_SiSgvp {{.*}}Def
|
||||
// CHECK_LOCALS: [[@LINE+3]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+2]]:12 {{.*}} shadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
if let shadowedVar {
|
||||
// CHECK: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS-NOT: [[@LINE+3]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL_SiSgvp {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+1]]:11 {{.*}} shadowedVar {{.*}}Ref
|
||||
_ = shadowedVar
|
||||
|
||||
// CHECK_LOCALS: [[@LINE+4]]:14 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+3]]:14 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL0_Sivp {{.*}}Def
|
||||
// CHECK-NOT: [[@LINE+2]]:14 {{.*}} shadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:14 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
if let shadowedVar {
|
||||
// CHECK: [[@LINE+1]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL0_Sivp {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+1]]:13 {{.*}} shadowedVar {{.*}}Ref
|
||||
_ = shadowedVar
|
||||
}
|
||||
}
|
||||
|
||||
// CHECK_LOCALS: [[@LINE+4]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+3]]:12 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL1_SiSgSgvp {{.*}}Def
|
||||
// CHECK-NOT: [[@LINE+2]]:12 {{.*}} shadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:12 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
_ = { [shadowedVar] in
|
||||
// CHECK: [[@LINE+1]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:11 {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyF11shadowedVarL1_SiSgSgvp {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+1]]:11 {{.*}} shadowedVar {{.*}}Ref
|
||||
_ = shadowedVar
|
||||
|
||||
// CHECK_LOCALS: [[@LINE+4]]:14 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+3]]:14 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyFyycfU_11shadowedVarL_SiSgSgvp {{.*}}Def
|
||||
// CHECK-NOT: [[@LINE+2]]:14 {{.*}} shadowedVar {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:14 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
_ = { [shadowedVar] in
|
||||
// CHECK: [[@LINE+1]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV11shadowedVarSiSgSgvp {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+2]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV06shadowE0yyFyycfU_11shadowedVarL_SiSgSgvp {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+1]]:13 {{.*}} shadowedVar {{.*}}Ref
|
||||
_ = shadowedVar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nestedFuncTest() {
|
||||
// CHECK: [[@LINE+1]]:10 {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyF {{.*}}Def
|
||||
// CHECK_LOCALS: [[@LINE+1]]:10 {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyF {{.*}}Def
|
||||
func shadowedFunc() {
|
||||
// CHECK-NOT: [[@LINE+2]]:14 {{.*}} shadowedFunc {{.*}}Def
|
||||
// CHECK: [[@LINE+1]]:14 {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyF {{.*}}Ref
|
||||
// CHECK_LOCALS: [[@LINE+1]]:14 | variable(local)/Swift {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyFAEL_yycvp {{.*}}Def
|
||||
_ = { [shadowedFunc] in
|
||||
// CHECK: [[@LINE+1]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyF {{.*}}Ref
|
||||
// CHECK-NOT: [[@LINE+2]]:14 {{.*}} shadowedFunc {{.*}}Def
|
||||
// CHECK_LOCALS: [[@LINE+1]]:13 {{.*}} s:14swift_ide_test12ShadowedTestV010nestedFuncE0yyF08shadowedG0L_yyFAEL_yycvp {{.*}}Ref
|
||||
_ = shadowedFunc
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user