From b401012c7c99ed9db0cb0c116c2f2ca6c3283e84 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Wed, 21 Sep 2022 12:54:32 +0800 Subject: [PATCH] fix: Fix defs+refs for globals. --- scip_indexer/SCIPIndexer.cc | 8 +++++++ test/scip/testdata/globals.rb | 18 +++++++++++++++ test/scip/testdata/globals.snapshot.rb | 32 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 test/scip/testdata/globals.rb create mode 100644 test/scip/testdata/globals.snapshot.rb diff --git a/scip_indexer/SCIPIndexer.cc b/scip_indexer/SCIPIndexer.cc index 8f3f9f75d..f6de9f48e 100644 --- a/scip_indexer/SCIPIndexer.cc +++ b/scip_indexer/SCIPIndexer.cc @@ -553,6 +553,14 @@ class AliasMap final { if (sym == core::Symbols::Magic_undeclaredFieldStub()) { ENFORCE(!bind.loc.empty()); ENFORCE(klass.isClassOrModule()); + auto fieldName = instr->name.shortName(gs); + if (!fieldName.empty() && fieldName[0] == '$') { + auto klass = core::Symbols::rootSingleton(); + this->map.insert( // no trim(...) because globals can't have a :: prefix + {bind.bind.variable, + {GenericSymbolRef::undeclaredField(klass, instr->name, bind.bind.type), bind.loc, false}}); + continue; + } auto result = findUnresolvedFieldTransitive(ctx, ctx.locAt(bind.loc), klass.asClassOrModuleRef(), instr->name); if (absl::holds_alternative(result)) { diff --git a/test/scip/testdata/globals.rb b/test/scip/testdata/globals.rb new file mode 100644 index 000000000..0c487a23a --- /dev/null +++ b/test/scip/testdata/globals.rb @@ -0,0 +1,18 @@ +# typed: true + +$aa = 0 + +def f + $aa = 10 + $bb = $aa + $aa = $bb + return +end + +class C + def g + $c = $bb + end +end + +puts $c diff --git a/test/scip/testdata/globals.snapshot.rb b/test/scip/testdata/globals.snapshot.rb new file mode 100644 index 000000000..227bddcd9 --- /dev/null +++ b/test/scip/testdata/globals.snapshot.rb @@ -0,0 +1,32 @@ + # typed: true + + $aa = 0 +#^^^ definition [..] `>`#$aa. + + def f +# ^ definition [..] Object#f(). + $aa = 10 +# ^^^ definition [..] `>`#$aa. + $bb = $aa +# ^^^ definition [..] `>`#$bb. +# ^^^ reference [..] `>`#$aa. + $aa = $bb +# ^^^ reference (write) [..] `>`#$aa. +# ^^^ reference [..] `>`#$bb. + return + end + + class C +# ^ definition [..] C# + def g +# ^ definition [..] C#g(). + $c = $bb +# ^^ definition [..] `>`#$c. +# ^^^^^^^^ reference [..] `>`#$c. +# ^^^ reference [..] `>`#$bb. + end + end + + puts $c +#^^^^ reference [..] Kernel#puts(). +# ^^ reference [..] `>`#$c.