Skip to content

Commit d21fe59

Browse files
committed
Reset local symbol index for each TextDocument, updateExpect
1 parent 2847a1c commit d21fe59

File tree

10 files changed

+120
-95
lines changed

10 files changed

+120
-95
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import scala.collection.mutable
2222
import scala.annotation.{ threadUnsafe => tu, tailrec }
2323
import scala.PartialFunction.condOpt
2424

25-
import SemanticSymbolBuilder._
2625
import SymbolInformationOps._
26+
import dotty.tools.dotc.semanticdb.SemanticSymbolBuilder
2727

2828
/** Extract symbol references and uses to semanticdb files.
2929
* See https://scalameta.org/docs/semanticdb/specification.html#symbol-1
@@ -50,6 +50,7 @@ class ExtractSemanticDB extends Phase:
5050

5151
/** Extractor of symbol occurrences from trees */
5252
class Extractor extends TreeTraverser:
53+
given builder: SemanticSymbolBuilder = SemanticSymbolBuilder()
5354

5455
/** The bodies of synthetic locals */
5556
private val localBodies = mutable.HashMap[Symbol, Tree]()
@@ -188,7 +189,7 @@ class ExtractSemanticDB extends Phase:
188189
else
189190
tree.body.foreach(traverse)
190191
case tree: Apply =>
191-
@tu lazy val genParamSymbol: Name => String = funParamSymbol(tree.fun.symbol)
192+
@tu lazy val genParamSymbol: Name => String = tree.fun.symbol.funParamSymbol
192193
traverse(tree.fun)
193194
for arg <- tree.args do
194195
arg match
@@ -283,7 +284,7 @@ class ExtractSemanticDB extends Phase:
283284

284285

285286
private def registerSymbol(sym: Symbol, symkinds: Set[SymbolKind])(using Context): Unit =
286-
val sname = symbolName(sym)
287+
val sname = sym.symbolName
287288
val isLocal = sname.isLocal
288289
if !isLocal || !localNames.contains(sname) then
289290
if isLocal then
@@ -304,13 +305,13 @@ class ExtractSemanticDB extends Phase:
304305
registerUse(sym, span, treeSource)
305306

306307
private def registerUse(sym: Symbol, span: Span, treeSource: SourceFile)(using Context): Unit =
307-
registerUse(symbolName(sym), span, treeSource)
308+
registerUse(sym.symbolName, span, treeSource)
308309

309310
private def registerUse(symbol: String, span: Span, treeSource: SourceFile)(using Context): Unit =
310311
registerOccurrence(symbol, span, SymbolOccurrence.Role.REFERENCE, treeSource)
311312

312313
private def registerDefinition(sym: Symbol, span: Span, symkinds: Set[SymbolKind], treeSource: SourceFile)(using Context) =
313-
val sname = symbolName(sym)
314+
val sname = sym.symbolName
314315
val finalSpan = if !span.hasLength || !sym.is(Given) || namePresentInSource(sym, span, treeSource) then
315316
span
316317
else

compiler/src/dotty/tools/dotc/semanticdb/SemanticSymbolBuilder.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Names.Name
1111
import scala.annotation.tailrec
1212
import scala.collection.mutable
1313

14-
object SemanticSymbolBuilder:
14+
class SemanticSymbolBuilder:
1515
import Scala3.{_, given}
1616

1717
private var nextLocalIdx: Int = 0
@@ -23,19 +23,20 @@ object SemanticSymbolBuilder:
2323
private val symsAtOffset = new mutable.HashMap[Int, Set[Symbol]]():
2424
override def default(key: Int) = Set[Symbol]()
2525

26-
/** The semanticdb name of the given symbol */
27-
def symbolName(sym: Symbol)(using Context): String =
28-
val b = StringBuilder(20)
29-
addSymName(b, sym)
30-
b.toString
31-
32-
def funParamSymbol(funSym: Symbol)(using Context): Name => String =
33-
if funSym.isGlobal then
34-
val funSymbol = symbolName(funSym)
35-
name => s"$funSymbol($name)"
36-
else
37-
name => locals.keys.find(local => local.isTerm && local.owner == funSym && local.name == name)
38-
.fold("<?>")(Symbols.LocalPrefix + _)
26+
extension (sym: Symbol)
27+
/** The semanticdb name of the given symbol */
28+
def symbolName(using Context): String =
29+
val b = StringBuilder(20)
30+
addSymName(b, sym)
31+
b.toString
32+
33+
def funParamSymbol(using Context): Name => String =
34+
if sym.isGlobal then
35+
val funSymbol = sym.symbolName
36+
name => s"$funSymbol($name)"
37+
else
38+
name => locals.keys.find(local => local.isTerm && local.owner == sym && local.name == name)
39+
.fold("<?>")(Symbols.LocalPrefix + _)
3940

4041
/** Add semanticdb name of the given symbol to string builder */
4142
private def addSymName(b: StringBuilder, sym: Symbol)(using Context): Unit =

compiler/src/dotty/tools/dotc/semanticdb/SymbolInformationOps.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package semanticdb
44

55
import dotty.tools.dotc.{semanticdb => s}
66
import dotty.tools.dotc.semanticdb.Scala3.SymbolKind
7-
import SemanticSymbolBuilder._
87
import Scala3.Symbols
98

109
import core.NameKinds
@@ -18,13 +17,13 @@ import scala.collection.mutable
1817

1918
object SymbolInformationOps:
2019
extension (sym: Symbol)
21-
def toSymbolInformation(using LinkMode, Context): s.SymbolInformation =
20+
def toSymbolInformation(using LinkMode, Context, SemanticSymbolBuilder): s.SymbolInformation =
2221
// val symkinds = sym.defTree.symbolKinds
2322
sym.symbolInfo(Set.empty)
2423

25-
def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, Context): SymbolInformation =
24+
def symbolInfo(symkinds: Set[SymbolKind])(using LinkMode, Context, SemanticSymbolBuilder): SymbolInformation =
2625
import s.SymbolOps._
27-
val sname = symbolName(sym)
26+
val sname = sym.symbolName
2827
val signature = sym.sig
2928
SymbolInformation(
3029
symbol = sname,

compiler/src/dotty/tools/dotc/semanticdb/SymbolOps.scala

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import ast.tpd._
1111

1212
import dotty.tools.dotc.{semanticdb => s}
1313

14-
import SemanticSymbolBuilder._
15-
1614
object SymbolOps:
1715
extension (sym: Symbol)
18-
def sig(using LinkMode, Context): s.Signature =
16+
def sig(using LinkMode, Context, SemanticSymbolBuilder): s.Signature =
1917
import TypeOps._
2018
// println("")
2119
val sig = sym.info.toSemanticSig(sym)
@@ -29,7 +27,7 @@ object SymbolOps:
2927
object TypeOps:
3028
import SymbolScopeOps._
3129
extension (tpe: Type)
32-
def toSemanticSig(using LinkMode, Context)(sym: Symbol): s.Signature =
30+
def toSemanticSig(using LinkMode, Context, SemanticSymbolBuilder)(sym: Symbol): s.Signature =
3331
def loop(tpe: Type): s.Signature = tpe match {
3432
case mt: MethodType =>
3533
val stparams = Some(s.Scope())
@@ -94,7 +92,7 @@ object TypeOps:
9492
}
9593
loop(tpe)
9694

97-
private def toSemanticType(using LinkMode)(using ctx: Context)(sym: Symbol): s.Type =
95+
private def toSemanticType(using LinkMode, SemanticSymbolBuilder, Context)(sym: Symbol): s.Type =
9896
import ConstantOps._
9997
def loop(tpe: Type): s.Type = tpe match {
10098
case ExprType(tpe) =>
@@ -103,12 +101,12 @@ object TypeOps:
103101

104102
case TypeRef(pre, desig) if desig.isInstanceOf[Symbol] =>
105103
val spre = if(tpe.hasTrivialPrefix) s.Type.Empty else loop(pre)
106-
val ssym = symbolName(desig.asInstanceOf[Symbol])
104+
val ssym = desig.asInstanceOf[Symbol].symbolName
107105
s.TypeRef(spre, ssym, Seq.empty)
108106

109107
case TermRef(pre, desig) if desig.isInstanceOf[Symbol] =>
110108
val spre = if(tpe.hasTrivialPrefix) s.Type.Empty else loop(pre)
111-
val ssym = symbolName(desig.asInstanceOf[Symbol])
109+
val ssym = desig.asInstanceOf[Symbol].symbolName
112110
s.SingleType(spre, ssym)
113111

114112
case tref: ParamRef =>
@@ -118,7 +116,7 @@ object TypeOps:
118116
}.find(p => p.name == tref.paramName)
119117
paramref match {
120118
case Some(ref) =>
121-
val ssym = symbolName(ref)
119+
val ssym = ref.symbolName
122120
tref match {
123121
case _: TypeParamRef => s.TypeRef(s.Type.Empty, ssym, Seq.empty)
124122
case _: TermParamRef => s.SingleType(s.Type.Empty, ssym)
@@ -128,12 +126,12 @@ object TypeOps:
128126
}
129127

130128
case ThisType(TypeRef(_, desig)) if desig.isInstanceOf[Symbol] =>
131-
val ssym = symbolName(desig.asInstanceOf[Symbol])
129+
val ssym = desig.asInstanceOf[Symbol].symbolName
132130
s.ThisType(ssym)
133131

134132
case SuperType(thistpe, supertpe) =>
135133
val spre = loop(thistpe.typeSymbol.info)
136-
val ssym = symbolName(supertpe.typeSymbol)
134+
val ssym = supertpe.typeSymbol.symbolName
137135
s.SuperType(spre, ssym)
138136

139137
// val clazzOf = classOf[...]
@@ -248,10 +246,10 @@ object TypeOps:
248246
object SymbolScopeOps:
249247
import SymbolInformationOps._
250248
extension (syms: List[Symbol])
251-
def sscope(using linkMode: LinkMode)(using Context): s.Scope =
249+
def sscope(using linkMode: LinkMode)(using SemanticSymbolBuilder, Context): s.Scope =
252250
linkMode match {
253251
case LinkMode.SymlinkChildren =>
254-
s.Scope(symlinks = syms.map(symbolName))
252+
s.Scope(symlinks = syms.map(_.symbolName))
255253
case LinkMode.HardlinkChildren =>
256254
s.Scope(hardlinks = syms.map(_.toSymbolInformation))
257255
}

tests/semanticdb/expect/Advanced.expect.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ class C/*<-advanced::C#*/[T/*<-advanced::C#[T]*/] {
1010
}
1111

1212
class Structural/*<-advanced::Structural#*/ {
13-
def s1/*<-advanced::Structural#s1().*/: { val x/*<-local0*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
14-
def s2/*<-advanced::Structural#s2().*/: { val x/*<-local1*/: Int/*->scala::Int#*/ } = new { val x/*<-local3*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
15-
def s3/*<-advanced::Structural#s3().*/: { def m/*<-local4*/(x/*<-local5*/: Int/*->scala::Int#*/): Int/*->scala::Int#*/ } = new { def m/*<-local7*/(x/*<-local8*/: Int/*->scala::Int#*/): Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
13+
def s1/*<-advanced::Structural#s1().*/: { val x/*<-local1*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
14+
def s2/*<-advanced::Structural#s2().*/: { val x/*<-local3*/: Int/*->scala::Int#*/ } = new { val x/*<-local6*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
15+
def s3/*<-advanced::Structural#s3().*/: { def m/*<-local9*/(x/*<-local10*/: Int/*->scala::Int#*/): Int/*->scala::Int#*/ } = new { def m/*<-local13*/(x/*<-local14*/: Int/*->scala::Int#*/): Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
1616
}
1717

1818
class Wildcards/*<-advanced::Wildcards#*/ {
@@ -34,8 +34,8 @@ object Test/*<-advanced::Test.*/ {
3434

3535
{
3636
(???/*->scala::Predef.`???`().*/ : Any/*->scala::Any#*/) match {
37-
case e3/*<-local9*/: List/*->scala::package.List#*/[_] =>
38-
val e3x/*<-local10*/ = e3/*->local9*/.head/*->scala::collection::IterableOps#head().*/
37+
case e3/*<-local19*/: List/*->scala::package.List#*/[_] =>
38+
val e3x/*<-local21*/ = e3/*->local19*/.head/*->scala::collection::IterableOps#head().*/
3939
()
4040
}
4141
}

tests/semanticdb/expect/Classes.expect.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ class C12/*<-classes::C12#*/ {
4747

4848
object N/*<-classes::N.*/ {
4949
val anonClass/*<-classes::N.anonClass.*/ = new C7/*->classes::C7#*/(42) {
50-
val local/*<-local1*/ = ???/*->scala::Predef.`???`().*/
50+
val local/*<-local2*/ = ???/*->scala::Predef.`???`().*/
5151
}
52-
val anonFun/*<-classes::N.anonFun.*/ = List/*->scala::package.List.*//*->scala::collection::IterableFactory#apply().*/(1).map/*->scala::collection::immutable::List#map().*/ { i/*<-local2*/ =>
53-
val local/*<-local3*/ = 2
54-
local/*->local3*/ +/*->scala::Int#`+`(+4).*/ 2
52+
val anonFun/*<-classes::N.anonFun.*/ = List/*->scala::package.List.*//*->scala::collection::IterableFactory#apply().*/(1).map/*->scala::collection::immutable::List#map().*/ { i/*<-local3*/ =>
53+
val local/*<-local4*/ = 2
54+
local/*->local4*/ +/*->scala::Int#`+`(+4).*/ 2
5555
}
5656
}

tests/semanticdb/expect/Methods.expect.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Methods/*<-example::Methods#*/[T/*<-example::Methods#[T]*/] {
2323
def m11/*<-example::Methods#m11().*/(x/*<-example::Methods#m11().(x)*/: Predef/*->scala::Predef.*/.type) = ???/*->scala::Predef.`???`().*/
2424
def m11/*<-example::Methods#m11(+1).*/(x/*<-example::Methods#m11(+1).(x)*/: Example/*->example::Example.*/.type) = ???/*->scala::Predef.`???`().*/
2525
def m12a/*<-example::Methods#m12a().*/(x/*<-example::Methods#m12a().(x)*/: {}) = ???/*->scala::Predef.`???`().*/
26-
def m12b/*<-example::Methods#m12b().*/(x/*<-example::Methods#m12b().(x)*/: { val x/*<-local0*/: Int/*->scala::Int#*/ }) = ???/*->scala::Predef.`???`().*/
26+
def m12b/*<-example::Methods#m12b().*/(x/*<-example::Methods#m12b().(x)*/: { val x/*<-local1*/: Int/*->scala::Int#*/ }) = ???/*->scala::Predef.`???`().*/
2727
def m13/*<-example::Methods#m13().*/(x/*<-example::Methods#m13().(x)*/: Int/*->scala::Int#*/ @unchecked/*->scala::unchecked#*/) = ???/*->scala::Predef.`???`().*/
2828
def m15/*<-example::Methods#m15().*/(x/*<-example::Methods#m15().(x)*/: => Int/*->scala::Int#*/) = ???/*->scala::Predef.`???`().*/
2929
def m16/*<-example::Methods#m16().*/(x/*<-example::Methods#m16().(x)*/: Int/*->scala::Int#*/*) = ???/*->scala::Predef.`???`().*/

tests/semanticdb/expect/Traits.expect.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ object U/*<-traits::U.*/ {
1010
}
1111

1212
class C/*<-traits::C#*/
13-
trait V/*<-traits::V#*/ { self/*<-local1*/: C/*->traits::C#*/ =>
13+
trait V/*<-traits::V#*/ { self/*<-local2*/: C/*->traits::C#*/ =>
1414
}

tests/semanticdb/expect/semanticdb-Types.expect.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ object Test/*<-types::Test.*/ {
5959
val superType2/*<-types::Test.C#superType2.*/ = super[M].m/*->types::Test.M#m().*/
6060
val superType3/*<-types::Test.C#superType3.*/ = C.super[M].m/*->types::Test.M#m().*/
6161

62-
val compoundType1/*<-types::Test.C#compoundType1.*/: { def k/*<-local0*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
62+
val compoundType1/*<-types::Test.C#compoundType1.*/: { def k/*<-local1*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
6363
val compoundType2/*<-types::Test.C#compoundType2.*/: M/*->types::Test.M#*/ with N/*->types::Test.N#*/ = ???/*->scala::Predef.`???`().*/
64-
val compoundType3/*<-types::Test.C#compoundType3.*/: M/*->types::Test.M#*/ with N/*->types::Test.N#*/ { def k/*<-local1*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
65-
val compoundType4/*<-types::Test.C#compoundType4.*/ = new { def k/*<-local3*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
64+
val compoundType3/*<-types::Test.C#compoundType3.*/: M/*->types::Test.M#*/ with N/*->types::Test.N#*/ { def k/*<-local3*/: Int/*->scala::Int#*/ } = ???/*->scala::Predef.`???`().*/
65+
val compoundType4/*<-types::Test.C#compoundType4.*/ = new { def k/*<-local6*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
6666
val compoundType5/*<-types::Test.C#compoundType5.*/ = new M/*->types::Test.M#*/ with N/*->types::Test.N#*/
67-
val compoundType6/*<-types::Test.C#compoundType6.*/ = new M/*->types::Test.M#*/ with N/*->types::Test.N#*/ { def k/*<-local6*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
67+
val compoundType6/*<-types::Test.C#compoundType6.*/ = new M/*->types::Test.M#*/ with N/*->types::Test.N#*/ { def k/*<-local11*/: Int/*->scala::Int#*/ = ???/*->scala::Predef.`???`().*/ }
6868

6969
val annType1/*<-types::Test.C#annType1.*/: T/*->types::T#*/ @ann(42) = ???/*->scala::Predef.`???`().*/
7070
val annType2/*<-types::Test.C#annType2.*/: T/*->types::T#*/ @ann1/*->types::ann1#*/ @ann2/*->types::ann2#*/ = ???/*->scala::Predef.`???`().*/
@@ -74,7 +74,7 @@ object Test/*<-types::Test.*/ {
7474
val existentialType4/*<-types::Test.C#existentialType4.*/ = Class/*->java::lang::Class#*/.forName/*->java::lang::Class#forName().*/("foo.Bar")
7575

7676
def typeLambda1/*<-types::Test.C#typeLambda1().*/[M/*<-types::Test.C#typeLambda1().[M]*/[_]] = ???/*->scala::Predef.`???`().*/
77-
typeLambda1/*->types::Test.C#typeLambda1().*/[({ type L/*<-local7*/[T/*<-local8*/] = List/*->scala::package.List#*/[T/*->local8*/] })#L]
77+
typeLambda1/*->types::Test.C#typeLambda1().*/[({ type L/*<-local12*/[T/*<-local14*/] = List/*->scala::package.List#*/[T/*->local14*/] })#L]
7878

7979
object ClassInfoType1/*<-types::Test.C#ClassInfoType1.*/
8080
class ClassInfoType2/*<-types::Test.C#ClassInfoType2#*/ extends B/*->types::B#*/ { def x/*<-types::Test.C#ClassInfoType2#x().*/ = 42 }

0 commit comments

Comments
 (0)