Skip to content

Commit 655aedb

Browse files
committed
unescape unicode in names, fix overload bug
1 parent 06ddb6c commit 655aedb

File tree

11 files changed

+191
-8
lines changed

11 files changed

+191
-8
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,22 @@ class ExtractSemanticDB extends Phase {
5959
/** The symbol occurrences generated so far, as a set */
6060
private val generated = new mutable.HashSet[SymbolOccurrence]
6161

62+
private val unicodeEscape = raw"\$$u(\p{XDigit}{4})".r
63+
6264
/** Add semanticdb name of the given symbol to string builder */
6365
private def addSymName(b: StringBuilder, sym: Symbol, inOwner: Boolean = false)(given ctx: Context): Unit =
6466

6567
def isJavaIdent(str: String) =
6668
isJavaIdentifierStart(str.head) && str.tail.forall(isJavaIdentifierPart)
6769

70+
def (name: Name) unescapeUnicode = {
71+
unicodeEscape.replaceAllIn(name.toString, m =>
72+
String.valueOf(Integer.parseInt(m.group(1), 16).toChar)
73+
)
74+
}
75+
6876
def addName(name: Name) =
69-
val str = name.toString
77+
val str = name.unescapeUnicode
7078
if isJavaIdent(str) then b.append(str)
7179
else b.append('`').append(str).append('`')
7280

@@ -79,8 +87,8 @@ class ExtractSemanticDB extends Phase {
7987
if !owner.isRoot then addSymName(b, owner, inOwner = true)
8088

8189
def addOverloadIdx(sym: Symbol): Unit =
82-
val alts = sym.owner.info.decls.lookupAll(sym.name).toList.reverse
83-
if alts.tail.nonEmpty then
90+
val alts = sym.owner.info.decls.lookupAll(sym.name).filter(_.is(Method)).toList.reverse
91+
if alts.nonEmpty && alts.tail.nonEmpty then
8492
val idx = alts.indexOf(sym)
8593
assert(idx >= 0)
8694
if idx > 0 then
@@ -161,6 +169,9 @@ class ExtractSemanticDB extends Phase {
161169
private def excludeDefStrict(sym: Symbol)(given Context): Boolean =
162170
sym.name.is(NameKinds.DefaultGetterName)
163171
|| sym == defn.Predef_classOf
172+
173+
private def blacklistPrefix(sym: Symbol)(given Context): Boolean =
174+
sym.is(Package)
164175
|| sym == defn.ScalaPredefModule
165176
|| sym == defn.StringContextModule
166177

@@ -191,7 +202,7 @@ class ExtractSemanticDB extends Phase {
191202
def registerPath(expr: Tree): Unit = expr match
192203
case t @ Select(expr, _) =>
193204
registerUse(t.symbol, t.span)
194-
if !expr.symbol.is(Package) then
205+
if !blacklistPrefix(expr.symbol) then
195206
registerPath(expr)
196207

197208
case _ =>
@@ -239,7 +250,7 @@ class ExtractSemanticDB extends Phase {
239250
if source.content()(end - 1) == '`' then end - len - 1 else end - len
240251
else limit
241252
registerUse(tree.symbol, Span(start max limit, end))
242-
if !tree.qualifier.symbol.is(Package) then
253+
if !blacklistPrefix(tree.qualifier.symbol) then
243254
traverseChildren(tree)
244255
case tree: Import =>
245256
if tree.span.exists && tree.span.start != tree.span.end then
@@ -250,7 +261,7 @@ class ExtractSemanticDB extends Phase {
250261
registerUse(alt.symbol, sel.imported.span)
251262
if (alt.symbol.companionClass.exists)
252263
registerUse(alt.symbol.companionClass, sel.imported.span)
253-
if !tree.expr.symbol.is(Package) then
264+
if !blacklistPrefix(tree.expr.symbol) then
254265
registerPath(tree.expr)
255266
case tree: Inlined =>
256267
traverse(tree.call)

tests/semanticdb/InstrumentTyper.expect.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ class InstrumentTyper/*<<=example.InstrumentTyper#*/ { self/*<<=local0*/: AnyRef
2121
List/*=>>scala.package.List.*//*=>>scala.collection.IterableFactory#apply().*/()
2222
)
2323
type AnnotatedType/*<<=example.InstrumentTyper#AnnotatedType#*/ = Int/*=>>scala.Int#*/ @param
24-
def singletonType/*<<=example.InstrumentTyper#singletonType().*/(x/*<<=example.InstrumentTyper#singletonType().(x)*/: Predef.type) = ???/*=>>scala.Predef.`???`().*/
24+
def singletonType/*<<=example.InstrumentTyper#singletonType().*/(x/*<<=example.InstrumentTyper#singletonType().(x)*/: Predef/*=>>scala.Predef.*/.type) = ???/*=>>scala.Predef.`???`().*/
2525
final val clazzOf/*<<=example.InstrumentTyper#clazzOf.*/ = classOf[Option/*=>>scala.Option#*/[Int/*=>>scala.Int#*/]]
2626
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package example
2+
3+
class Local/*<<=example.Local#*/ {
4+
def a/*<<=example.Local#a().*/() = {
5+
def id/*<<=local0*/[A/*<<=local1*/](a/*<<=local2*/: A/*=>>local1*/): A/*=>>local1*/ = a/*=>>local2*/
6+
id/*=>>local0*/(1)
7+
}
8+
}

tests/semanticdb/Local.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package example
2+
3+
class Local {
4+
def a() = {
5+
def id[A](a: A): A = a
6+
id(1)
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package locals
2+
3+
object Test/*<<=locals.Test.*/ {
4+
val xs/*<<=locals.Test.xs.*/ = {
5+
val x/*<<=local0*/ = 42
6+
List/*=>>scala.package.List.*//*=>>scala.collection.IterableFactory#apply().*/(x/*=>>local0*/)
7+
}
8+
}

tests/semanticdb/Locals.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package locals
2+
3+
object Test {
4+
val xs = {
5+
val x = 42
6+
List(x)
7+
}
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package example
2+
3+
class MethodUsages/*<<=example.MethodUsages#*/ {
4+
val m/*<<=example.MethodUsages#m.*/ = new Methods/*=>>example.Methods#*/[Int/*=>>scala.Int#*/]/*=>>example.Methods#`<init>`().*/
5+
m/*=>>example.MethodUsages#m.*/.m1/*=>>example.Methods#m1().*/
6+
m/*=>>example.MethodUsages#m.*/.m2/*=>>example.Methods#m2().*/()
7+
m/*=>>example.MethodUsages#m.*/.m3/*=>>example.Methods#m3().*/(0)
8+
m/*=>>example.MethodUsages#m.*/.m4/*=>>example.Methods#m4().*/(0)(0)
9+
m/*=>>example.MethodUsages#m.*/.m5/*=>>example.Methods#m5().*/("")
10+
m/*=>>example.MethodUsages#m.*/.m5/*=>>example.Methods#m5(+1).*/(0)
11+
m/*=>>example.MethodUsages#m.*/.m6/*=>>example.Methods#m6().*/(0)
12+
m/*=>>example.MethodUsages#m.*/.m6/*=>>example.Methods#m6(+1).*/(new m/*=>>example.MethodUsages#m.*/.List/*=>>example.Methods#List#*/[Int/*=>>scala.Int#*/]/*=>>example.Methods#List#`<init>`().*/)
13+
m/*=>>example.MethodUsages#m.*/.m6/*=>>example.Methods#m6(+2).*/(Nil/*=>>scala.package.Nil.*/)
14+
m/*=>>example.MethodUsages#m.*/.m7/*=>>example.Methods#m7().*/(m/*=>>example.MethodUsages#m.*/, new m/*=>>example.MethodUsages#m.*/.List/*=>>example.Methods#List#*/[Int/*=>>scala.Int#*/]/*=>>example.Methods#List#`<init>`().*/)/*=>>scala.math.Ordering.Int.*/
15+
m/*=>>example.MethodUsages#m.*/.`m8().`/*=>>example.Methods#`m8().`().*/()
16+
m/*=>>example.MethodUsages#m.*/.m9/*=>>example.Methods#m9().*/(null)
17+
m/*=>>example.MethodUsages#m.*/.m10/*=>>example.Methods#m10().*/(null)
18+
m/*=>>example.MethodUsages#m.*/.m11/*=>>example.Methods#m11().*/(Predef/*=>>scala.Predef.*/)
19+
m/*=>>example.MethodUsages#m.*/.m11/*=>>example.Methods#m11(+1).*/(Example/*=>>example.Example.*/)
20+
m/*=>>example.MethodUsages#m.*/.m12a/*=>>example.Methods#m12a().*/(null)
21+
m/*=>>example.MethodUsages#m.*/.m12b/*=>>example.Methods#m12b().*/(null)
22+
m/*=>>example.MethodUsages#m.*/.m13/*=>>example.Methods#m13().*/(0)
23+
m/*=>>example.MethodUsages#m.*/.m15/*=>>example.Methods#m15().*/(0)
24+
m/*=>>example.MethodUsages#m.*/.m16/*=>>example.Methods#m16().*/(0)
25+
m/*=>>example.MethodUsages#m.*/.m16/*=>>example.Methods#m16().*/(0)
26+
m/*=>>example.MethodUsages#m.*/.m17/*=>>example.Methods#m17.*/.m/*=>>example.Methods#m17.m().*/()
27+
m/*=>>example.MethodUsages#m.*/.m17/*=>>example.Methods#m17().*/(1)
28+
m/*=>>example.MethodUsages#m.*/.m17/*=>>example.Methods#m17(+1).*/("")
29+
m/*=>>example.MethodUsages#m.*/.m18/*=>>example.Methods#m18.*/.m/*=>>example.Methods#m17.m().*/()
30+
m/*=>>example.MethodUsages#m.*/.m18/*=>>example.Methods#m18().*/(1)
31+
m/*=>>example.MethodUsages#m.*/.m18/*=>>example.Methods#m18(+1).*/("")
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package example
2+
3+
class MethodUsages {
4+
val m = new Methods[Int]
5+
m.m1
6+
m.m2()
7+
m.m3(0)
8+
m.m4(0)(0)
9+
m.m5("")
10+
m.m5(0)
11+
m.m6(0)
12+
m.m6(new m.List[Int])
13+
m.m6(Nil)
14+
m.m7(m, new m.List[Int])
15+
m.`m8().`()
16+
m.m9(null)
17+
m.m10(null)
18+
m.m11(Predef)
19+
m.m11(Example)
20+
m.m12a(null)
21+
m.m12b(null)
22+
m.m13(0)
23+
m.m15(0)
24+
m.m16(0)
25+
m.m16(0)
26+
m.m17.m()
27+
m.m17(1)
28+
m.m17("")
29+
m.m18.m()
30+
m.m18(1)
31+
m.m18("")
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package example
2+
3+
import scala.math.Ordering/*=>>scala.math.Ordering.*//*=>>scala.math.Ordering#*/
4+
import scala.language/*=>>scalaShadowing.language.*/.existentials/*=>>scalaShadowing.language.existentials.*/
5+
6+
class Methods/*<<=example.Methods#*/[T/*<<=example.Methods#[T]*/] {
7+
class List/*<<=example.Methods#List#*/[T/*<<=example.Methods#List#[T]*/]
8+
type AList/*<<=example.Methods#AList#*/[T/*<<=example.Methods#AList#[T]*/] = List/*=>>example.Methods#List#*/[T/*=>>example.Methods#AList#[T]*/]
9+
def m1/*<<=example.Methods#m1().*/ = ???/*=>>scala.Predef.`???`().*/
10+
def m2/*<<=example.Methods#m2().*/() = ???/*=>>scala.Predef.`???`().*/
11+
def m3/*<<=example.Methods#m3().*/(x/*<<=example.Methods#m3().(x)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
12+
def m4/*<<=example.Methods#m4().*/(x/*<<=example.Methods#m4().(x)*/: Int/*=>>scala.Int#*/)(y/*<<=example.Methods#m4().(y)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
13+
def m5/*<<=example.Methods#m5().*/(x/*<<=example.Methods#m5().(x)*/: String/*=>>scala.Predef.String#*/) = ???/*=>>scala.Predef.`???`().*/
14+
def m5/*<<=example.Methods#m5(+1).*/(x/*<<=example.Methods#m5(+1).(x)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
15+
def m6/*<<=example.Methods#m6().*/(x/*<<=example.Methods#m6().(x)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
16+
def m6/*<<=example.Methods#m6(+1).*/(x/*<<=example.Methods#m6(+1).(x)*/: List/*=>>example.Methods#List#*/[T/*=>>example.Methods#[T]*/]) = ???/*=>>scala.Predef.`???`().*/
17+
def m6/*<<=example.Methods#m6(+2).*/(x/*<<=example.Methods#m6(+2).(x)*/: scala.List/*=>>scala.package.List#*/[T/*=>>example.Methods#[T]*/]) = ???/*=>>scala.Predef.`???`().*/
18+
def m7/*<<=example.Methods#m7().*/[U/*<<=example.Methods#m7().[U]*//*<<=example.Methods#m7().(evidence$1)*/: Ordering/*=>>scala.math.Ordering#*//*=>>example.Methods#m7().[U]*/](c/*<<=example.Methods#m7().(c)*/: Methods/*=>>example.Methods#*/[T/*=>>example.Methods#[T]*/], l/*<<=example.Methods#m7().(l)*/: List/*=>>example.Methods#List#*/[U/*=>>example.Methods#m7().[U]*/]) = ???/*=>>scala.Predef.`???`().*/
19+
def `m8().`() =/*<<=example.Methods#`m8().`().*/ ???/*=>>scala.Predef.`???`().*/
20+
class `m9().`
21+
d/*<<=example.Methods#`m9().`#*/ef m9/*<<=example.Methods#m9().*/(x/*<<=example.Methods#m9().(x)*/: `m9().`/*=>>example.Methods#`m9().`#*/) = ???/*=>>scala.Predef.`???`().*/
22+
def m10/*<<=example.Methods#m10().*/(x/*<<=example.Methods#m10().(x)*/: AList/*=>>example.Methods#AList#*/[T/*=>>example.Methods#[T]*/]) = ???/*=>>scala.Predef.`???`().*/
23+
def m11/*<<=example.Methods#m11().*/(x/*<<=example.Methods#m11().(x)*/: Predef/*=>>scala.Predef.*/.type) = ???/*=>>scala.Predef.`???`().*/
24+
def m11/*<<=example.Methods#m11(+1).*/(x/*<<=example.Methods#m11(+1).(x)*/: Example/*=>>example.Example.*/.type) = ???/*=>>scala.Predef.`???`().*/
25+
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.`???`().*/
27+
def m13/*<<=example.Methods#m13().*/(x/*<<=example.Methods#m13().(x)*/: Int/*=>>scala.Int#*/ @unchecked) = ???/*=>>scala.Predef.`???`().*/
28+
def m15/*<<=example.Methods#m15().*/(x/*<<=example.Methods#m15().(x)*/: => Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
29+
def m16/*<<=example.Methods#m16().*/(x/*<<=example.Methods#m16().(x)*/: Int/*=>>scala.Int#*/*) = ???/*=>>scala.Predef.`???`().*/
30+
object m17/*<<=example.Methods#m17.*/ { def m/*<<=example.Methods#m17.m().*/() = ???/*=>>scala.Predef.`???`().*/ }
31+
def m17/*<<=example.Methods#m17().*/(a/*<<=example.Methods#m17().(a)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
32+
def m17/*<<=example.Methods#m17(+1).*/(b/*<<=example.Methods#m17(+1).(b)*/: String/*=>>scala.Predef.String#*/) = ???/*=>>scala.Predef.`???`().*/
33+
val m18/*<<=example.Methods#m18.*/ = m17/*=>>example.Methods#m17.*/
34+
def m18/*<<=example.Methods#m18().*/(a/*<<=example.Methods#m18().(a)*/: Int/*=>>scala.Int#*/) = ???/*=>>scala.Predef.`???`().*/
35+
def m18/*<<=example.Methods#m18(+1).*/(b/*<<=example.Methods#m18(+1).(b)*/: String/*=>>scala.Predef.String#*/) = ???/*=>>scala.Predef.`???`().*/
36+
def m19/*<<=example.Methods#m19().*/(x/*<<=example.Methods#m19().(x)*//*<<=example.Methods#m19$default$3().(x)*/: Int/*=>>scala.Int#*/, y/*<<=example.Methods#m19().(y)*//*<<=example.Methods#m19$default$3().(y)*/: Int/*=>>scala.Int#*/ = 2)(z/*<<=example.Methods#m19().(z)*/: Int/*=>>scala.Int#*/ = 3) = ???/*=>>scala.Predef.`???`().*/
37+
}

tests/semanticdb/Methods.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package example
2+
3+
import scala.math.Ordering
4+
import scala.language.existentials
5+
6+
class Methods[T] {
7+
class List[T]
8+
type AList[T] = List[T]
9+
def m1 = ???
10+
def m2() = ???
11+
def m3(x: Int) = ???
12+
def m4(x: Int)(y: Int) = ???
13+
def m5(x: String) = ???
14+
def m5(x: Int) = ???
15+
def m6(x: Int) = ???
16+
def m6(x: List[T]) = ???
17+
def m6(x: scala.List[T]) = ???
18+
def m7[U: Ordering](c: Methods[T], l: List[U]) = ???
19+
def `m8().`() = ???
20+
class `m9().`
21+
def m9(x: `m9().`) = ???
22+
def m10(x: AList[T]) = ???
23+
def m11(x: Predef.type) = ???
24+
def m11(x: Example.type) = ???
25+
def m12a(x: {}) = ???
26+
def m12b(x: { val x: Int }) = ???
27+
def m13(x: Int @unchecked) = ???
28+
def m15(x: => Int) = ???
29+
def m16(x: Int*) = ???
30+
object m17 { def m() = ??? }
31+
def m17(a: Int) = ???
32+
def m17(b: String) = ???
33+
val m18 = m17
34+
def m18(a: Int) = ???
35+
def m18(b: String) = ???
36+
def m19(x: Int, y: Int = 2)(z: Int = 3) = ???
37+
}

0 commit comments

Comments
 (0)