Skip to content

Commit e2c4c09

Browse files
fix: Fix defs+refs for globals. (#118)
1 parent 387495b commit e2c4c09

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

scip_indexer/SCIPIndexer.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@ class AliasMap final {
553553
if (sym == core::Symbols::Magic_undeclaredFieldStub()) {
554554
ENFORCE(!bind.loc.empty());
555555
ENFORCE(klass.isClassOrModule());
556+
auto fieldName = instr->name.shortName(gs);
557+
if (!fieldName.empty() && fieldName[0] == '$') {
558+
auto klass = core::Symbols::rootSingleton();
559+
this->map.insert( // no trim(...) because globals can't have a :: prefix
560+
{bind.bind.variable,
561+
{GenericSymbolRef::undeclaredField(klass, instr->name, bind.bind.type), bind.loc, false}});
562+
continue;
563+
}
556564
auto result = findUnresolvedFieldTransitive(ctx, ctx.locAt(bind.loc), klass.asClassOrModuleRef(),
557565
instr->name);
558566
if (absl::holds_alternative<core::ClassOrModuleRef>(result)) {

test/scip/testdata/globals.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# typed: true
2+
3+
$aa = 0
4+
5+
def f
6+
$aa = 10
7+
$bb = $aa
8+
$aa = $bb
9+
return
10+
end
11+
12+
class C
13+
def g
14+
$c = $bb
15+
end
16+
end
17+
18+
puts $c
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# typed: true
2+
3+
$aa = 0
4+
#^^^ definition [..] `<Class:<root>>`#$aa.
5+
6+
def f
7+
# ^ definition [..] Object#f().
8+
$aa = 10
9+
# ^^^ definition [..] `<Class:<root>>`#$aa.
10+
$bb = $aa
11+
# ^^^ definition [..] `<Class:<root>>`#$bb.
12+
# ^^^ reference [..] `<Class:<root>>`#$aa.
13+
$aa = $bb
14+
# ^^^ reference (write) [..] `<Class:<root>>`#$aa.
15+
# ^^^ reference [..] `<Class:<root>>`#$bb.
16+
return
17+
end
18+
19+
class C
20+
# ^ definition [..] C#
21+
def g
22+
# ^ definition [..] C#g().
23+
$c = $bb
24+
# ^^ definition [..] `<Class:<root>>`#$c.
25+
# ^^^^^^^^ reference [..] `<Class:<root>>`#$c.
26+
# ^^^ reference [..] `<Class:<root>>`#$bb.
27+
end
28+
end
29+
30+
puts $c
31+
#^^^^ reference [..] Kernel#puts().
32+
# ^^ reference [..] `<Class:<root>>`#$c.

0 commit comments

Comments
 (0)