diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 458e60ddfa38..dd8b69020d2f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -561,30 +561,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer return typedQuotedTypeVar(tree, pt) end if - // Shortcut for the root package, this is not just a performance - // optimization, it also avoids forcing imports thus potentially avoiding - // cyclic references. - if (name == nme.ROOTPKG) - return tree.withType(defn.RootPackage.termRef) - - val rawType = - val saved1 = unimported - val saved2 = foundUnderScala2 - unimported = Set.empty - foundUnderScala2 = NoType - try - val found = findRef(name, pt, EmptyFlags, EmptyFlags, tree.srcPos) - if foundUnderScala2.exists && !(foundUnderScala2 =:= found) then - report.migrationWarning( - em"""Name resolution will change. - | currently selected : $foundUnderScala2 - | in the future, without -source 3.0-migration: $found""", tree.srcPos) - foundUnderScala2 - else found - finally - unimported = saved1 - foundUnderScala2 = saved2 - /** Normally, returns `ownType` except if `ownType` is a constructor proxy, * and there is another shadowed type accessible with the same name that is not: * - if the prototype is an application: @@ -623,6 +599,31 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer checkLegalValue(tree2, pt) tree2 + // Shortcut for the root package, this is not just a performance + // optimization, it also avoids forcing imports thus potentially avoiding + // cyclic references. + if (name == nme.ROOTPKG) + return setType(defn.RootPackage.termRef) + + val rawType = + val saved1 = unimported + val saved2 = foundUnderScala2 + unimported = Set.empty + foundUnderScala2 = NoType + try + val found = findRef(name, pt, EmptyFlags, EmptyFlags, tree.srcPos) + if foundUnderScala2.exists && !(foundUnderScala2 =:= found) then + report.migrationWarning( + em"""Name resolution will change. + | currently selected : $foundUnderScala2 + | in the future, without -source 3.0-migration: $found""", tree.srcPos) + foundUnderScala2 + else found + finally + unimported = saved1 + foundUnderScala2 = saved2 + + def isLocalExtensionMethodRef: Boolean = rawType match case rawType: TermRef => rawType.denot.hasAltWith(_.symbol.is(ExtensionMethod)) diff --git a/tests/neg/i17757.check b/tests/neg/i17757.check new file mode 100644 index 000000000000..017ef24c8e5f --- /dev/null +++ b/tests/neg/i17757.check @@ -0,0 +1,24 @@ +-- [E008] Not Found Error: tests/neg/i17757.scala:3:17 ----------------------------------------------------------------- +3 | println(_root_.length) // error + | ^^^^^^^^^^^^^ + | value length is not a member of +-- [E119] Type Error: tests/neg/i17757.scala:4:10 ---------------------------------------------------------------------- +4 | println(_root_) // error + | ^^^^^^ + | package _root_ is not a value +-- [E119] Type Error: tests/neg/i17757.scala:7:2 ----------------------------------------------------------------------- +7 | _root_ // error + | ^^^^^^ + | package _root_ is not a value +-- [E119] Type Error: tests/neg/i17757.scala:8:9 ----------------------------------------------------------------------- +8 | _root_.scala // error + | ^^^^^^^^^^^^ + | package scala is not a value +-- [E119] Type Error: tests/neg/i17757.scala:9:10 ---------------------------------------------------------------------- +9 | println(_root_) // error + | ^^^^^^ + | package _root_ is not a value +-- [E119] Type Error: tests/neg/i17757.scala:10:17 --------------------------------------------------------------------- +10 | println(_root_.scala) // error + | ^^^^^^^^^^^^ + | package scala is not a value diff --git a/tests/neg/i17757.scala b/tests/neg/i17757.scala new file mode 100644 index 000000000000..c165cbbda5f9 --- /dev/null +++ b/tests/neg/i17757.scala @@ -0,0 +1,10 @@ +def foo: Unit = + val _root_ = "abc" + println(_root_.length) // error + println(_root_) // error + +def bar: Unit = + _root_ // error + _root_.scala // error + println(_root_) // error + println(_root_.scala) // error