Skip to content

Commit fc8f08a

Browse files
committed
do not semanticdb symbol occurences for wildcards
1 parent 65c715a commit fc8f08a

File tree

8 files changed

+81
-29
lines changed

8 files changed

+81
-29
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,16 +2508,16 @@ object Parsers {
25082508
case t => simplePatternRest(t)
25092509
}
25102510
case USCORE =>
2511-
val wildIndent = wildcardIdent()
2511+
val wildIdent = wildcardIdent()
25122512

25132513
// compatibility for Scala2 `x @ _*` and `_*` syntax
25142514
// `x: _*' is parsed in `ascription'
25152515
if (isIdent(nme.raw.STAR)) {
25162516
in.nextToken()
2517-
if (in.token != RPAREN) syntaxError(SeqWildcardPatternPos(), wildIndent.span)
2518-
atSpan(wildIndent.span) { Ident(tpnme.WILDCARD_STAR) }
2517+
if (in.token != RPAREN) syntaxError(SeqWildcardPatternPos(), wildIdent.span)
2518+
atSpan(wildIdent.span) { Ident(tpnme.WILDCARD_STAR) }
25192519
}
2520-
else wildIndent
2520+
else wildIdent
25212521
case LPAREN =>
25222522
atSpan(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
25232523
case QUOTE =>

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ class ExtractSemanticDB extends Phase {
8989
def addDescriptor(sym: Symbol): Unit =
9090
if sym.is(ModuleClass) then
9191
addDescriptor(sym.sourceModule)
92-
else if sym.is(Param) || sym.is(ParamAccessor) then
93-
b.append('('); addName(sym.name); b.append(')')
9492
else if sym.is(TypeParam) then
9593
b.append('['); addName(sym.name); b.append(']')
94+
else if sym.is(Param) || sym.is(ParamAccessor) then
95+
b.append('('); addName(sym.name); b.append(')')
9696
else
9797
addName(sym.name)
9898
if sym.is(Package) then b.append('/')
@@ -141,6 +141,13 @@ class ExtractSemanticDB extends Phase {
141141
val (endLine, endCol) = lineCol(span.end)
142142
Some(Range(startLine, startCol, endLine, endCol))
143143

144+
private val WILDCARDTypeName = nme.WILDCARD.toTypeName
145+
146+
private def isWildcard(name: Name)(given ctx: Context) = name match
147+
case nme.WILDCARD | WILDCARDTypeName => true
148+
case _ if name.is(NameKinds.WildcardParamName) => true
149+
case _ => false
150+
144151
/** Definitions of this symbol should be excluded from semanticdb */
145152
private def excludeDef(sym: Symbol)(given Context): Boolean =
146153
!sym.exists || sym.isLocalDummy || sym.is(Synthetic)
@@ -156,9 +163,13 @@ class ExtractSemanticDB extends Phase {
156163
generated += occ
157164

158165
private def registerUse(sym: Symbol, span: Span)(given Context) =
159-
if !excludeUse(sym, span) then
166+
if !excludeUse(sym, span) && !isWildcard(sym.name) then
160167
registerOccurrence(sym, span, SymbolOccurrence.Role.REFERENCE)
161168

169+
private def registerDefinition(sym: Symbol, span: Span)(given Context) =
170+
if !isWildcard(sym.name) then
171+
registerOccurrence(sym, span, SymbolOccurrence.Role.DEFINITION)
172+
162173
override def traverse(tree: Tree)(given ctx: Context): Unit =
163174
def registerPath(expr: Tree): Unit = expr match
164175
case t @ Select(expr, _) =>
@@ -177,10 +188,11 @@ class ExtractSemanticDB extends Phase {
177188
case tree: ValDef if tree.symbol.is(Module) => // skip module val
178189
case tree: NamedDefTree
179190
if !excludeDef(tree.symbol) && tree.span.start != tree.span.end =>
180-
registerOccurrence(tree.symbol, tree.nameSpan, SymbolOccurrence.Role.DEFINITION)
191+
registerDefinition(tree.symbol, tree.nameSpan)
181192
traverseChildren(tree)
182193
case tree: Ident =>
183-
registerUse(tree.symbol, tree.span)
194+
if tree.name != nme.WILDCARD && !excludeUse(tree.symbol, tree.span) then
195+
registerUse(tree.symbol, tree.span)
184196
case tree: Select =>
185197
if !excludeUse(tree.symbol, tree.span) then
186198
val end = tree.span.end

tests/semanticdb/Advanced.expect.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.language/*=>>scalaShadowing.language.*/.reflectiveCalls/*=>>scalaSh
66
import scala.reflect/*=>>scala.reflect.*/.Selectable/*=>>scala.reflect.Selectable.*/.reflectiveSelectable/*=>>scala.reflect.Selectable.reflectiveSelectable().*/
77

88
class /*=>>java.lang.Object#`<init>`().*/C/*<<=advanced.C#*/[T] {
9-
/*<<=advanced.C#`<init>`().*//*<<=advanced.C#`<init>`().(T)*//*<<=advanced.C#(T)*/ def t/*<<=advanced.C#t().*/: T/*=>>advanced.C#(T)*/ = ???/*=>>scala.Predef.`???`().*/
9+
/*<<=advanced.C#`<init>`().*//*<<=advanced.C#`<init>`().[T]*//*<<=advanced.C#[T]*/ def t/*<<=advanced.C#t().*/: T/*=>>advanced.C#[T]*/ = ???/*=>>scala.Predef.`???`().*/
1010
}
1111

1212
class /*=>>java.lang.Object#`<init>`().*/Structural/*<<=advanced.Structural#*/ {
@@ -34,8 +34,8 @@ object /*=>>java.lang.Object#`<init>`().*/Test/*<<=advanced.Test.*/ {
3434

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

tests/semanticdb/Annotations.expect.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import scala.annotation.meta._
55
import scala.language/*=>>scalaShadowing.language.*/.experimental/*=>>scalaShadowing.language.experimental.*/.macros/*=>>scalaShadowing.language.experimental.macros.*/
66

77
@ClassAnnotation/*=>>com.javacp.annot.ClassAnnotation#*/
8-
class /*=>>java.lang.Object#`<init>`().*/Annotations/*<<=annot.Annotations#*/[@Type/*<<=annot.Annotations#`<init>`().*/ParameterAnnotation/*=>>com.javacp.annot.TypeParameterAnnotation#*/ T/*<<=annot.Annotations#`<init>`().(T)*//*<<=annot.Annotations#(T)*/](@ParameterAnnotation/*=>>com.javacp.annot.ParameterAnnotation#*/ x/*<<=annot.Annotations#`<init>`().(x)*//*<<=annot.Annotations#(x)*/: T/*=>>annot.Annotations#`<init>`().(T)*/) { self/*<<=local0*/: AnyRef/*=>>scala.AnyRef#*/ =>
8+
class /*=>>java.lang.Object#`<init>`().*/Annotations/*<<=annot.Annotations#*/[@Type/*<<=annot.Annotations#`<init>`().*/ParameterAnnotation/*=>>com.javacp.annot.TypeParameterAnnotation#*/ T/*<<=annot.Annotations#`<init>`().[T]*//*<<=annot.Annotations#[T]*/](@ParameterAnnotation/*=>>com.javacp.annot.ParameterAnnotation#*/ x/*<<=annot.Annotations#`<init>`().(x)*//*<<=annot.Annotations#(x)*/: T/*=>>annot.Annotations#`<init>`().[T]*/) { self/*<<=local0*/: AnyRef/*=>>scala.AnyRef#*/ =>
99
@FieldAnnotation/*=>>com.javacp.annot.FieldAnnotation#*/
1010
val field/*<<=annot.Annotations#field.*/ = 42
1111

@@ -27,7 +27,7 @@ class /*=>>java.lang.Object#`<init>`().*/B/*<<=annot.B#*/ @Cons/*<<=annot.B#`<in
2727
@ObjectAnnotation/*=>>com.javacp.annot.ObjectAnnotation#*/
2828
object /*=>>java.lang.Object#`<init>`().*/M/*<<=annot.M.*/ {
2929
/*=>>scala.package.Serializable#*//*=>>scala.*//*=>>_root_*//*=>>annot.M.*/@MacroAnnotation/*=>>com.javacp.annot.MacroAnnotation#*/
30-
def m/*<<=annot.M.m().*/[TT/*<<=annot.M.m().(TT)*/]: Int/*=>>scala.Int#*//*=>>scala.Predef.`???`().*//*=>>scala.Predef.*//*=>>scala.*//*=>>_root_*/ = macro ???
30+
def m/*<<=annot.M.m().*/[TT/*<<=annot.M.m().[TT]*/]: Int/*=>>scala.Int#*//*=>>scala.Predef.`???`().*//*=>>scala.Predef.*//*=>>scala.*//*=>>_root_*/ = macro ???
3131
}
3232

3333
@TraitAnnotation/*=>>com.javacp.annot.TraitAnnotation#*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package example
2+
import scala.language/*=>>scalaShadowing.language.*/.higherKinds/*=>>scalaShadowing.language.higherKinds.*/
3+
4+
class /*=>>java.lang.Object#`<init>`().*/Anonymous/*<<=example.Anonymous#*/ {
5+
this: Anonymous/*=>>example.Anonymous#*/ =>
6+
7+
def locally/*<<=example.Anonymous#locally().*/[A/*<<=example.Anonymous#locally().[A]*/](x/*<<=example.Anonymous#locally().(x)*/: A/*=>>example.Anonymous#locally().[A]*/): A/*=>>example.Anonymous#locally().[A]*/ = x/*=>>example.Anonymous#locally().(x)*/
8+
9+
def m1/*<<=example.Anonymous#m1().*/[T/*<<=example.Anonymous#m1().[T]*/[_]] = ???/*=>>scala.Predef.`???`().*/
10+
def m2/*<<=example.Anonymous#m2().*/: Map/*=>>scala.Predef.Map#*/[_, List/*=>>scala.package.List#*/[_]] = ???/*=>>scala.Predef.`???`().*/
11+
locally/*=>>example.Anonymous#locally().*/ {
12+
???/*=>>scala.Predef.`???`().*/ match { case _: List/*=>>scala.package.List#*/[_] => }
13+
}
14+
locally/*=>>example.Anonymous#locally().*/ {
15+
val x/*<<=local0*/: Int/*=>>scala.Int#*/ => Int/*=>>scala.Int#*/ = _ => ???/*=>>scala.Predef.`???`().*/
16+
}
17+
18+
trait Foo/*<<=example.Anonymous#Foo#*/
19+
/*=>>java.lang.Object#`<init>`().*/new F/*<<=local1*/oo/*=>>example.Anonymous#Foo#*/ {}/*=>>local2*//*=>>local1*/
20+
}

tests/semanticdb/Anonymous.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package example
2+
import scala.language.higherKinds
3+
4+
class Anonymous {
5+
this: Anonymous =>
6+
7+
def locally[A](x: A): A = x
8+
9+
def m1[T[_]] = ???
10+
def m2: Map[_, List[_]] = ???
11+
locally {
12+
??? match { case _: List[_] => }
13+
}
14+
locally {
15+
val x: Int => Int = _ => ???
16+
}
17+
18+
trait Foo
19+
new Foo {}
20+
}

0 commit comments

Comments
 (0)