Skip to content

Commit d94b08a

Browse files
committed
sourcepath: fix handling of top-level definitions
5c2a2d5 was a bit too clever: instead of running the desugaring of top-level definitions, it manually checked if such a desugaring was needed and then entered the package object if needed and traversed the non-desugared package statements, but in a situation like this: package foo type Foo object Foo The desugaring will move `object Foo` inside the package object, so by traversing the non-desugared tree, we were creating a symbol for Foo at the wrong level. This commit fixes this by simply calling the appropriate desugaring method before doing any traversal. Unfortunately I wasn't able to manufacture a testcase where this makes a difference, but this fixes some issues I saw when compiling dotty-library itself.
1 parent 0a33ad3 commit d94b08a

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,12 +1229,6 @@ object desugar {
12291229
case _ =>
12301230
false
12311231

1232-
/** Does this package contains at least one top-level definition
1233-
* that will require a wrapping object ?
1234-
*/
1235-
def hasTopLevelDef(pdef: PackageDef)(given Context): Boolean =
1236-
pdef.stats.exists(isTopLevelDef)
1237-
12381232
/** Assuming `src` contains top-level definition, returns the name that should
12391233
* be using for the package object that will wrap them.
12401234
*/

compiler/src/dotty/tools/dotc/core/SymbolLoaders.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import ast.Trees._
1818
import parsing.JavaParsers.OutlineJavaParser
1919
import parsing.Parsers.OutlineParser
2020
import reporting.trace
21-
import ast.desugar.{ packageObjectName, hasTopLevelDef }
21+
import ast.desugar
2222

2323
object SymbolLoaders {
2424
import ast.untpd._
@@ -135,11 +135,16 @@ object SymbolLoaders {
135135
ok
136136
}
137137

138-
def traverse(tree: Tree, path: List[TermName]): Unit = tree match {
138+
/** Run the subset of desugaring necessary to record the correct symbols */
139+
def simpleDesugar(tree: Tree): Tree = tree match
140+
case tree: PackageDef =>
141+
desugar.packageDef(tree)
142+
case _ =>
143+
tree
144+
145+
def traverse(tree: Tree, path: List[TermName]): Unit = simpleDesugar(tree) match {
139146
case tree @ PackageDef(pid, body) =>
140147
val path1 = addPrefix(pid, path)
141-
if hasTopLevelDef(tree) && checkPathMatches(path1, "package", pid)
142-
enterModule(owner, packageObjectName(unit.source), completer, scope = scope)
143148
for (stat <- body) traverse(stat, path1)
144149
case tree: TypeDef if tree.isClassDef =>
145150
if (checkPathMatches(path, "class", tree))

0 commit comments

Comments
 (0)