Skip to content

Use context.outers instead of owner chain to find class for suspicious top-level resolution #20717

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

Closed
wants to merge 2 commits 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
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ object Contexts {

/** The next outer context whose tree is a template or package definition
* Note: Currently unused
def enclTemplate: Context = {
def enclTemplate: Context =
var c = this
while (c != NoContext && !c.tree.isInstanceOf[Template[?]] && !c.tree.isInstanceOf[PackageDef[?]])
c = c.outer
while c != NoContext && !c.tree.isInstanceOf[Template[?]] && !c.tree.isInstanceOf[PackageDef[?]]
do c = c.outer
c
}*/
*/

/** The context for a supercall. This context is used for elaborating
* the parents of a class and their arguments.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ class PureUnitExpression(stat: untpd.Tree, tpe: Type)(using Context)
class UnqualifiedCallToAnyRefMethod(stat: untpd.Tree, method: Symbol)(using Context)
extends Message(UnqualifiedCallToAnyRefMethodID) {
def kind = MessageKind.PotentialIssue
def msg(using Context) = i"Suspicious top-level unqualified call to ${hl(method.name.toString)}"
def msg(using Context) = i"Universal method ${hl(method.name.toString)} does not resolve to the enclosing class"
def explain(using Context) =
val getClassExtraHint =
if method.name == nme.getClass_ && ctx.settings.classpath.value.contains("scala3-staging") then
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1252,11 +1252,13 @@ object RefChecks {
end checkImplicitNotFoundAnnotation

def checkAnyRefMethodCall(tree: Tree)(using Context): Unit =
extension (c: Context) def enclosingClass: Symbol =
c.outersIterator.find(_.isClassDefContext).map(_.owner).getOrElse(NoSymbol)
if tree.symbol.exists && defn.topClasses.contains(tree.symbol.owner) then
tree.tpe match
case tp: NamedType if tp.prefix.typeSymbol != ctx.owner.enclosingClass =>
case tp: NamedType if tp.prefix.typeSymbol != ctx.enclosingClass =>
report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree)
case _ => ()
case _ =>
}
import RefChecks.*

Expand Down
18 changes: 9 additions & 9 deletions tests/warn/i17266.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:4:2 ---------------------------------------------------------
4 | synchronized { // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
| Universal method synchronized does not resolve to the enclosing class
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -12,7 +12,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:17:2 --------------------------------------------------------
17 | synchronized { // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
| Universal method synchronized does not resolve to the enclosing class
|--------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -33,7 +33,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:108:2 -------------------------------------------------------
108 | wait() // warn
| ^^^^
| Suspicious top-level unqualified call to wait
| Universal method wait does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -44,7 +44,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:115:2 -------------------------------------------------------
115 | wait() // warn
| ^^^^
| Suspicious top-level unqualified call to wait
| Universal method wait does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -55,7 +55,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:121:2 -------------------------------------------------------
121 | wait(10) // warn
| ^^^^
| Suspicious top-level unqualified call to wait
| Universal method wait does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -66,7 +66,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:128:2 -------------------------------------------------------
128 | wait(10) // warn
| ^^^^
| Suspicious top-level unqualified call to wait
| Universal method wait does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -77,7 +77,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:134:2 -------------------------------------------------------
134 | hashCode() // warn
| ^^^^^^^^
| Suspicious top-level unqualified call to hashCode
| Universal method hashCode does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -88,7 +88,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:141:2 -------------------------------------------------------
141 | hashCode() // warn
| ^^^^^^^^
| Suspicious top-level unqualified call to hashCode
| Universal method hashCode does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -99,7 +99,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17266.scala:148:2 -------------------------------------------------------
148 | synchronized { // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
| Universal method synchronized does not resolve to the enclosing class
|-------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
2 changes: 1 addition & 1 deletion tests/warn/i17493.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- [E181] Potential Issue Warning: tests/warn/i17493.scala:4:10 --------------------------------------------------------
4 | def g = synchronized { println("hello, world") } // warn
| ^^^^^^^^^^^^
| Suspicious top-level unqualified call to synchronized
| Universal method synchronized does not resolve to the enclosing class
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
20 changes: 20 additions & 0 deletions tests/warn/i20651.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package example

class B(val x: String)

class C:
def wasBad = new B(getClass.getName){}.x // nowarn ever C.this.getClass
def neverBad = new B(getClass.getName).x // nowarn ever

def alwaysBad = new B(getClass.getName).x // warn Predef.getClass not `package`.getClass

object A:
def main(args: Array[String]): Unit =
println(new B(getClass.getName){}.x) // nowarn was warn bc A is not $anon
println(new B(getClass.getName).x) // nowarn A.getClass

trait T(val x: String)

object U:
def main(args: Array[String]): Unit =
println(new T(getClass.getName){}.x) // nowarn
Loading