Skip to content

Commit

Permalink
exclude top level wrapper objects
Browse files Browse the repository at this point in the history
fixes scala#11693, remove top level definition
wrapper objects appearing as a definition
occurrence in semanticdb, still record
them as a symbol
  • Loading branch information
bishabosha committed Jun 1, 2021
1 parent f0aa7a9 commit aab4701
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ExtractSemanticDB extends Phase:
|| sym.isLocalDummy
|| sym.is(Synthetic)
|| sym.isSetter
|| sym.isTopLevelWrapperObject
|| excludeDefOrUse(sym)

private def excludeDefOrUse(sym: Symbol)(using Context): Boolean =
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/semanticdb/Scala3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import core.Names.Name
import core.Types.Type
import core.Flags._
import core.NameKinds
import core.StdNames.nme
import core.StdNames.{nme, str}

import java.lang.Character.{isJavaIdentifierPart, isJavaIdentifierStart}

Expand Down Expand Up @@ -81,6 +81,13 @@ object Scala3:
case NameKinds.ModuleClassName(original) => original.isScala2PackageObjectName
case _ => false

def isTopLevelWrapperObjectName: Boolean = name match
case name: Names.TermName => name.endsWith(str.TOPLEVEL_SUFFIX)
case name: Names.TypeName =>
name.toTermName match
case NameKinds.ModuleClassName(original) => original.isTopLevelWrapperObjectName
case _ => false

def isEmptyNumbered: Boolean =
!name.is(NameKinds.WildcardParamName)
&& { name match
Expand All @@ -97,6 +104,9 @@ object Scala3:
def isScala2PackageObject(using Context): Boolean =
sym.name.isScala2PackageObjectName && sym.owner.is(Package) && sym.is(Module)

def isTopLevelWrapperObject(using Context): Boolean =
sym.name.isTopLevelWrapperObjectName && sym.owner.is(Package) && sym.is(Module)

def isAnonymous(using Context): Boolean =
sym.isAnonymousClass
|| sym.isAnonymousModuleVal
Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/AnonymousGiven.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package angiven

trait Foo/*<-angiven::Foo#*/

/*<-angiven::AnonymousGiven$package.*/def bar/*<-angiven::AnonymousGiven$package.bar().*/(using Foo/*->angiven::Foo#*/) = 42
def bar/*<-angiven::AnonymousGiven$package.bar().*/(using Foo/*->angiven::Foo#*/) = 42
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/EndMarkers.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package endmarkers:
end this/*->endmarkers::MultiCtor#`<init>`(+1).*/
end MultiCtor/*->endmarkers::MultiCtor#*/

/*<-endmarkers::EndMarkers$package.*/def topLevelMethod/*<-endmarkers::EndMarkers$package.topLevelMethod().*/: String/*->scala::Predef.String#*/ =
def topLevelMethod/*<-endmarkers::EndMarkers$package.topLevelMethod().*/: String/*->scala::Predef.String#*/ =
"hello"
end topLevelMethod/*->endmarkers::EndMarkers$package.topLevelMethod().*/

Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/Extension.expect.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ext

/*<-ext::Extension$package.*/extension (s/*<-ext::Extension$package.foo().(s)*//*<-ext::Extension$package.`#*#`().(s)*/: String/*->scala::Predef.String#*/)
extension (s/*<-ext::Extension$package.foo().(s)*//*<-ext::Extension$package.`#*#`().(s)*/: String/*->scala::Predef.String#*/)
def foo/*<-ext::Extension$package.foo().*/: Int/*->scala::Int#*/ = 42
def #*#/*<-ext::Extension$package.`#*#`().*/ (i/*<-ext::Extension$package.`#*#`().(i)*/: Int/*->scala::Int#*/): (String/*->scala::Predef.String#*/, Int/*->scala::Int#*/) = (/*->scala::Tuple2.apply().*/s/*->ext::Extension$package.`#*#`().(s)*/, i/*->ext::Extension$package.`#*#`().(i)*/)

Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/InventedNames.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Z/*<-givens::Z#*/[T/*<-givens::Z#[T]*/]:



/*<-givens::InventedNames$package.*/given intValue/*<-givens::InventedNames$package.intValue.*/: Int/*->scala::Int#*/ = 4
given intValue/*<-givens::InventedNames$package.intValue.*/: Int/*->scala::Int#*/ = 4
given /*<-givens::InventedNames$package.given_String.*/String/*->scala::Predef.String#*/ = "str"
given /*<-givens::InventedNames$package.given_Double().*/(using Int/*->scala::Int#*/): Double/*->scala::Double#*/ = 4.0
given /*<-givens::InventedNames$package.given_List_T().*/[T/*<-givens::InventedNames$package.given_List_T().[T]*/]: List/*->scala::package.List#*/[T/*->givens::InventedNames$package.given_List_T().[T]*/] = Nil/*->scala::package.Nil.*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ext

/*<-ext::RightAssociativeExtension$package.*/extension (s/*<-ext::RightAssociativeExtension$package.`:*:`().(s)*/: String/*->scala::Predef.String#*/)
extension (s/*<-ext::RightAssociativeExtension$package.`:*:`().(s)*/: String/*->scala::Predef.String#*/)
def :*:/*<-ext::RightAssociativeExtension$package.`:*:`().*/ (i/*<-ext::RightAssociativeExtension$package.`:*:`().(i)*/: Int/*->scala::Int#*/): (String/*->scala::Predef.String#*/, Int/*->scala::Int#*/) = (/*->scala::Tuple2.apply().*/s/*->ext::RightAssociativeExtension$package.`:*:`().(s)*/, i/*->ext::RightAssociativeExtension$package.`:*:`().(i)*/)

val b/*<-ext::RightAssociativeExtension$package.b.*/ = "foo" :*:/*->ext::RightAssociativeExtension$package.`:*:`().*/ 23
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/exports-package.expect.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package exports

/*<-exports::`exports-package$package`.*/export example.{Decoder/*<-exports::`exports-package$package`.Decoder#*/, Encoder/*<-exports::`exports-package$package`.Encoder#*/, Codec/*<-exports::`exports-package$package`.Codec#*/}
export example.{Decoder/*<-exports::`exports-package$package`.Decoder#*/, Encoder/*<-exports::`exports-package$package`.Encoder#*/, Codec/*<-exports::`exports-package$package`.Codec#*/}
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/i9727.expect.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package i9727

class Test/*<-i9727::Test#*/(a/*<-i9727::Test#a.*/: Int/*->scala::Int#*/)
/*<-i9727::i9727$package.*/val a/*<-i9727::i9727$package.a.*/ = new Test/*->i9727::Test#*/(1)
val a/*<-i9727::i9727$package.a.*/ = new Test/*->i9727::Test#*/(1)
val b/*<-i9727::i9727$package.b.*/ = new Test/*->i9727::Test#*/(2)
3 changes: 2 additions & 1 deletion tests/semanticdb/expect/toplevel.expect.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*<-_empty_::toplevel$package.*/inline val a/*<-_empty_::toplevel$package.a.*/ = ""
inline val a/*<-_empty_::toplevel$package.a.*/ = ""
extension (x/*<-_empty_::toplevel$package.combine().(x)*/: Int/*->scala::Int#*/) def combine/*<-_empty_::toplevel$package.combine().*/ (y/*<-_empty_::toplevel$package.combine().(y)*/: Int/*->scala::Int#*/) = x/*->_empty_::toplevel$package.combine().(x)*/ +/*->scala::Int#`+`(+4).*/ y/*->_empty_::toplevel$package.combine().(y)*/
def combine/*<-_empty_::toplevel$package.combine(+1).*/(x/*<-_empty_::toplevel$package.combine(+1).(x)*/: Int/*->scala::Int#*/, y/*<-_empty_::toplevel$package.combine(+1).(y)*/: Int/*->scala::Int#*/, z/*<-_empty_::toplevel$package.combine(+1).(z)*/: Int/*->scala::Int#*/) = x/*->_empty_::toplevel$package.combine(+1).(x)*/ +/*->scala::Int#`+`(+4).*/ y/*->_empty_::toplevel$package.combine(+1).(y)*/ +/*->scala::Int#`+`(+4).*/ z/*->_empty_::toplevel$package.combine(+1).(z)*/
def combine/*<-_empty_::toplevel$package.combine(+2).*/ = 0
def foo/*<-_empty_::toplevel$package.foo().*/ = "foo"
/*<-_empty_::MyProgram#*//*->_empty_::toplevel$package.MyProgram().*//*->scala::util::CommandLineParser.parseArgument().*//*->_empty_::MyProgram#main().(args)*//*->scala::util::CommandLineParser.FromString.given_FromString_Int.*//*->scala::util::CommandLineParser.showError().*//*->local0*/@main/*->scala::main#*/ def MyProgram/*<-_empty_::toplevel$package.MyProgram().*/(times/*<-_empty_::toplevel$package.MyProgram().(times)*/: Int/*->scala::Int#*/): Unit/*->scala::Unit#*/ = (/*->scala::LowPriorityImplicits#intWrapper().*/1 to/*->scala::runtime::RichInt#to().*/ times/*->_empty_::toplevel$package.MyProgram().(times)*/) foreach/*->scala::collection::immutable::Range#foreach().*/ (_ => println/*->scala::Predef.println(+1).*/("hello"))
def fooRef/*<-_empty_::toplevel$package.fooRef().*/ = toplevel$package/*->_empty_::toplevel$package.*/.foo/*->_empty_::toplevel$package.foo().*/
1 change: 1 addition & 0 deletions tests/semanticdb/expect/toplevel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ def combine(x: Int, y: Int, z: Int) = x + y + z
def combine = 0
def foo = "foo"
@main def MyProgram(times: Int): Unit = (1 to times) foreach (_ => println("hello"))
def fooRef = toplevel$package.foo
30 changes: 13 additions & 17 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ Uri => AnonymousGiven.scala
Text => empty
Language => Scala
Symbols => 5 entries
Occurrences => 6 entries
Occurrences => 5 entries

Symbols:
angiven/AnonymousGiven$package. => final package object angiven
Expand All @@ -351,7 +351,6 @@ Occurrences:
[0:8..0:15): angiven <- angiven/
[2:0..2:0): <- angiven/Foo#`<init>`().
[2:6..2:9): Foo <- angiven/Foo#
[4:0..4:0): <- angiven/AnonymousGiven$package.
[4:4..4:7): bar <- angiven/AnonymousGiven$package.bar().
[4:14..4:17): Foo -> angiven/Foo#

Expand Down Expand Up @@ -667,7 +666,7 @@ Uri => EndMarkers.scala
Text => empty
Language => Scala
Symbols => 24 entries
Occurrences => 43 entries
Occurrences => 42 entries

Symbols:
endmarkers/Container# => class Container
Expand Down Expand Up @@ -705,7 +704,6 @@ Occurrences:
[4:11..4:11): -> endmarkers/MultiCtor#`<init>`().
[5:8..5:12): this -> endmarkers/MultiCtor#`<init>`(+1).
[6:6..6:15): MultiCtor -> endmarkers/MultiCtor#
[8:2..8:2): <- endmarkers/EndMarkers$package.
[8:6..8:20): topLevelMethod <- endmarkers/EndMarkers$package.topLevelMethod().
[8:22..8:28): String -> scala/Predef.String#
[10:6..10:20): topLevelMethod -> endmarkers/EndMarkers$package.topLevelMethod().
Expand Down Expand Up @@ -1278,7 +1276,7 @@ Uri => Extension.scala
Text => empty
Language => Scala
Symbols => 8 entries
Occurrences => 19 entries
Occurrences => 18 entries

Symbols:
ext/Extension$package. => final package object ext
Expand All @@ -1292,7 +1290,6 @@ ext/Extension$package.foo().(s) => param s

Occurrences:
[0:8..0:11): ext <- ext/
[2:0..2:0): <- ext/Extension$package.
[2:11..2:12): s <- ext/Extension$package.foo().(s)
[2:11..2:12): s <- ext/Extension$package.`#*#`().(s)
[2:14..2:20): String -> scala/Predef.String#
Expand Down Expand Up @@ -1747,7 +1744,7 @@ Uri => InventedNames.scala
Text => empty
Language => Scala
Symbols => 45 entries
Occurrences => 73 entries
Occurrences => 72 entries

Symbols:
givens/InventedNames$package. => final package object givens
Expand Down Expand Up @@ -1812,7 +1809,6 @@ Occurrences:
[9:6..9:9): doZ <- givens/Z#doZ().
[9:11..9:15): List -> scala/package.List#
[9:16..9:17): T -> givens/Z#[T]
[13:0..13:0): <- givens/InventedNames$package.
[13:6..13:14): intValue <- givens/InventedNames$package.intValue.
[13:16..13:19): Int -> scala/Int#
[14:6..14:6): <- givens/InventedNames$package.given_String.
Expand Down Expand Up @@ -2731,7 +2727,7 @@ Uri => RightAssociativeExtension.scala
Text => empty
Language => Scala
Symbols => 5 entries
Occurrences => 14 entries
Occurrences => 13 entries

Symbols:
ext/RightAssociativeExtension$package. => final package object ext
Expand All @@ -2742,7 +2738,6 @@ ext/RightAssociativeExtension$package.b. => val method b

Occurrences:
[0:8..0:11): ext <- ext/
[2:0..2:0): <- ext/RightAssociativeExtension$package.
[2:11..2:12): s <- ext/RightAssociativeExtension$package.`:*:`().(s)
[2:14..2:20): String -> scala/Predef.String#
[3:6..3:9): :*: <- ext/RightAssociativeExtension$package.`:*:`().
Expand Down Expand Up @@ -3471,7 +3466,7 @@ Uri => exports-package.scala
Text => empty
Language => Scala
Symbols => 4 entries
Occurrences => 6 entries
Occurrences => 5 entries

Symbols:
exports/`exports-package$package`. => final package object exports
Expand All @@ -3481,7 +3476,6 @@ exports/`exports-package$package`.Encoder# => final type Encoder

Occurrences:
[0:8..0:15): exports <- exports/
[2:0..2:0): <- exports/`exports-package$package`.
[2:7..2:14): example -> exports/example/
[2:16..2:23): Decoder <- exports/`exports-package$package`.Decoder#
[2:25..2:32): Encoder <- exports/`exports-package$package`.Encoder#
Expand Down Expand Up @@ -3516,7 +3510,7 @@ Uri => i9727.scala
Text => empty
Language => Scala
Symbols => 7 entries
Occurrences => 12 entries
Occurrences => 11 entries

Symbols:
i9727/Test# => class Test
Expand All @@ -3533,7 +3527,6 @@ Occurrences:
[2:10..2:10): <- i9727/Test#`<init>`().
[2:11..2:12): a <- i9727/Test#a.
[2:14..2:17): Int -> scala/Int#
[3:0..3:0): <- i9727/i9727$package.
[3:4..3:5): a <- i9727/i9727$package.a.
[3:12..3:16): Test -> i9727/Test#
[3:16..3:16): -> i9727/Test#`<init>`().
Expand Down Expand Up @@ -4381,8 +4374,8 @@ Schema => SemanticDB v4
Uri => toplevel.scala
Text => empty
Language => Scala
Symbols => 18 entries
Occurrences => 42 entries
Symbols => 19 entries
Occurrences => 44 entries

Symbols:
_empty_/MyProgram# => final class MyProgram
Expand All @@ -4402,10 +4395,10 @@ _empty_/toplevel$package.combine(+1).(y) => param y
_empty_/toplevel$package.combine(+1).(z) => param z
_empty_/toplevel$package.combine(+2). => method combine
_empty_/toplevel$package.foo(). => method foo
_empty_/toplevel$package.fooRef(). => method fooRef
local0 => val local error

Occurrences:
[0:0..0:0): <- _empty_/toplevel$package.
[0:11..0:12): a <- _empty_/toplevel$package.a.
[1:11..1:12): x <- _empty_/toplevel$package.combine().(x)
[1:14..1:17): Int -> scala/Int#
Expand Down Expand Up @@ -4447,4 +4440,7 @@ Occurrences:
[5:46..5:51): times -> _empty_/toplevel$package.MyProgram().(times)
[5:53..5:60): foreach -> scala/collection/immutable/Range#foreach().
[5:67..5:74): println -> scala/Predef.println(+1).
[6:4..6:10): fooRef <- _empty_/toplevel$package.fooRef().
[6:13..6:29): toplevel$package -> _empty_/toplevel$package.
[6:30..6:33): foo -> _empty_/toplevel$package.foo().

0 comments on commit aab4701

Please sign in to comment.