Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use setType on shortcut for ROOTPKG #18019

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down
24 changes: 24 additions & 0 deletions tests/neg/i17757.check
Original file line number Diff line number Diff line change
@@ -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 <root>
-- [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
10 changes: 10 additions & 0 deletions tests/neg/i17757.scala
Original file line number Diff line number Diff line change
@@ -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
Loading