From 872c69f684bae484b2f8d7eeb02df7458b8ab986 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Tue, 9 May 2023 19:58:18 +0200 Subject: [PATCH 1/2] Warn when calling methods of topClasses on Predef or on package objects Co-Authored-By: Dale Wijnand Co-Authored-By: itielsa <38278165+itielsa@users.noreply.github.com> --- .../tools/dotc/reporting/ErrorMessageID.scala | 2 + .../dotty/tools/dotc/reporting/messages.scala | 19 +++++ .../dotty/tools/dotc/typer/RefChecks.scala | 20 +++++ compiler/test/dotty/tools/utils.scala | 4 +- tests/neg/i17266.check | 84 +++++++++++++++++++ tests/neg/i17266.scala | 49 +++++++++++ 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i17266.check create mode 100644 tests/neg/i17266.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index cc2741bcb614..1802139de502 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -194,6 +194,8 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case MissingArgumentListID // errorNumber: 178 case MatchTypeScrutineeCannotBeHigherKindedID // errorNumber: 179 case AmbiguousExtensionMethodID // errorNumber 180 + case CallToAnyRefMethodOnPredefID // errorNumber: 181 + case CallToAnyRefMethodOnPackageObjectID // errorNumber: 182 def errorNumber = ordinal - 1 diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 423c1cdef264..42b5951384a3 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2284,6 +2284,25 @@ class PureExpressionInStatementPosition(stat: untpd.Tree, val exprOwner: Symbol) |It can be removed without changing the semantics of the program. This may indicate an error.""" } +class CallToAnyRefMethodOnPredef(stat: untpd.Tree, method: Symbol)(using Context) + extends Message(CallToAnyRefMethodOnPredefID) { + def kind = MessageKind.PotentialIssue + def msg(using Context) = i"Suspicious call to ${hl("Predef." + method.name)}" + def explain(using Context) = + i"""Top-level unqualified calls to ${hl("AnyRef")} or ${hl("Any")} methods such as ${hl(method.name.toString)} are + |resolved to calls on ${hl("Predef")}. This might not be what you intended.""" +} + +class CallToAnyRefMethodOnPackageObject(stat: untpd.Tree, method: Symbol)(using Context) + extends Message(CallToAnyRefMethodOnPackageObjectID) { + def kind = MessageKind.PotentialIssue + def msg(using Context) = i"Suspicious top-level call to ${hl("this." + method.name)}" + def explain(using Context) = + i"""Top-level calls to ${hl("AnyRef")} or ${hl("Any")} methods are resolved to calls on the + |synthetic package object generated for the current file. This might not be + |what you intended.""" +} + class TraitCompanionWithMutableStatic()(using Context) extends SyntaxMsg(TraitCompanionWithMutableStaticID) { def msg(using Context) = i"Companion of traits cannot define mutable @static fields" diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 73331046b41f..108a141e28c6 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -1090,6 +1090,17 @@ object RefChecks { end checkImplicitNotFoundAnnotation + def checkSynchronizedCall(tree: Tree, symbol: Symbol)(using Context) = + if symbol.exists && defn.topClasses.contains(symbol.owner) then + tree.tpe match + case tp: NamedType => + val prefixSymbol = tp.prefix.typeSymbol + if prefixSymbol == defn.ScalaPredefModuleClass then + report.warning(CallToAnyRefMethodOnPredef(tree, symbol), tree) + if prefixSymbol.isPackageObject && !tp.symbol.isConstructor then + report.warning(CallToAnyRefMethodOnPackageObject(tree, symbol), tree) + case _ => () + } import RefChecks._ @@ -1168,6 +1179,15 @@ class RefChecks extends MiniPhase { thisPhase => report.error(ex, tree.srcPos) tree } + + override def transformSelect(tree: Select)(using Context): Tree = + checkSynchronizedCall(tree, tree.denot.symbol) + tree + + override def transformIdent(tree: Ident)(using Context): Tree = + checkSynchronizedCall(tree, tree.symbol) + tree + } /* todo: rewrite and re-enable diff --git a/compiler/test/dotty/tools/utils.scala b/compiler/test/dotty/tools/utils.scala index bfedc338f25a..75918674146c 100644 --- a/compiler/test/dotty/tools/utils.scala +++ b/compiler/test/dotty/tools/utils.scala @@ -17,8 +17,10 @@ import scala.util.control.{ControlThrowable, NonFatal} import dotc.config.CommandLineParser +object Dummy + def scripts(path: String): Array[File] = { - val dir = new File(this.getClass.getResource(path).getPath) + val dir = new File(Dummy.getClass.getResource(path).getPath) assert(dir.exists && dir.isDirectory, "Couldn't load scripts dir") dir.listFiles.filter { f => val path = if f.isDirectory then f.getPath + "/" else f.getPath diff --git a/tests/neg/i17266.check b/tests/neg/i17266.check new file mode 100644 index 000000000000..11742a5f2378 --- /dev/null +++ b/tests/neg/i17266.check @@ -0,0 +1,84 @@ +-- [E181] Potential Issue Error: tests/neg/i17266.scala:4:2 ------------------------------------------------------------ +4 | synchronized { // error + | ^^^^^^^^^^^^ + | Suspicious call to Predef.synchronized + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as synchronized are + | resolved to calls on Predef. This might not be what you intended. + --------------------------------------------------------------------------------------------------------------------- +-- [E182] Potential Issue Error: tests/neg/i17266.scala:9:7 ------------------------------------------------------------ +9 | this.synchronized { // error + | ^^^^^^^^^^^^^^^^^ + | Suspicious top-level call to this.synchronized + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level calls to AnyRef or Any methods are resolved to calls on the + | synthetic package object generated for the current file. This might not be + | what you intended. + --------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:22:2 ----------------------------------------------------------- +22 | wait() // error + | ^^^^ + | Suspicious call to Predef.wait + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef. This might not be what you intended. + -------------------------------------------------------------------------------------------------------------------- +-- [E182] Potential Issue Error: tests/neg/i17266.scala:25:7 ----------------------------------------------------------- +25 | this.wait() // error + | ^^^^^^^^^ + | Suspicious top-level call to this.wait + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level calls to AnyRef or Any methods are resolved to calls on the + | synthetic package object generated for the current file. This might not be + | what you intended. + -------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:32:2 ----------------------------------------------------------- +32 | wait(10) // error + | ^^^^ + | Suspicious call to Predef.wait + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef. This might not be what you intended. + -------------------------------------------------------------------------------------------------------------------- +-- [E182] Potential Issue Error: tests/neg/i17266.scala:35:7 ----------------------------------------------------------- +35 | this.wait(10) // error + | ^^^^^^^^^ + | Suspicious top-level call to this.wait + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level calls to AnyRef or Any methods are resolved to calls on the + | synthetic package object generated for the current file. This might not be + | what you intended. + -------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:42:2 ----------------------------------------------------------- +42 | hashCode() // error + | ^^^^^^^^ + | Suspicious call to Predef.hashCode + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as hashCode are + | resolved to calls on Predef. This might not be what you intended. + -------------------------------------------------------------------------------------------------------------------- +-- [E182] Potential Issue Error: tests/neg/i17266.scala:45:7 ----------------------------------------------------------- +45 | this.hashCode() // error + | ^^^^^^^^^^^^^ + | Suspicious top-level call to this.hashCode + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level calls to AnyRef or Any methods are resolved to calls on the + | synthetic package object generated for the current file. This might not be + | what you intended. + -------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i17266.scala b/tests/neg/i17266.scala new file mode 100644 index 000000000000..1edcebf4781c --- /dev/null +++ b/tests/neg/i17266.scala @@ -0,0 +1,49 @@ +// scalac: -Werror -explain + +def test1 = + synchronized { // error + println("hello") + } + +def test2 = + this.synchronized { // error + println("hello") + } + +object MyLib + +def test3 = + import MyLib.* + synchronized { // not an error (should be?) + println("hello") + } + +def test4 = + wait() // error + +def test5 = + this.wait() // error + +def test6 = + import MyLib.* + wait() // not an error (should be?) + +def test7 = + wait(10) // error + +def test8 = + this.wait(10) // error + +def test9 = + import MyLib.* + wait(10) // not an error (should be?) + +def test10 = + hashCode() // error + +def test11 = + this.hashCode() // error + +def test12 = + import MyLib.* + hashCode() // not an error (should be?) From 5cadc06ed555da1ecf85b56c8206f8fb58cbab30 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Thu, 11 May 2023 15:06:59 +0200 Subject: [PATCH 2/2] Warn on unqualified top-level calls to Any or AnyRef methods --- .../tools/dotc/reporting/ErrorMessageID.scala | 3 +- .../dotty/tools/dotc/reporting/messages.scala | 19 +-- .../dotty/tools/dotc/typer/RefChecks.scala | 21 +-- tests/neg/i17266.check | 148 +++++++++--------- tests/neg/i17266.scala | 127 +++++++++++++-- 5 files changed, 199 insertions(+), 119 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index 1802139de502..fc679210db17 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -194,8 +194,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case MissingArgumentListID // errorNumber: 178 case MatchTypeScrutineeCannotBeHigherKindedID // errorNumber: 179 case AmbiguousExtensionMethodID // errorNumber 180 - case CallToAnyRefMethodOnPredefID // errorNumber: 181 - case CallToAnyRefMethodOnPackageObjectID // errorNumber: 182 + case UnqualifiedCallToAnyRefMethodID // errorNumber: 181 def errorNumber = ordinal - 1 diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 42b5951384a3..3a0854f36be1 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2284,23 +2284,14 @@ class PureExpressionInStatementPosition(stat: untpd.Tree, val exprOwner: Symbol) |It can be removed without changing the semantics of the program. This may indicate an error.""" } -class CallToAnyRefMethodOnPredef(stat: untpd.Tree, method: Symbol)(using Context) - extends Message(CallToAnyRefMethodOnPredefID) { +class UnqualifiedCallToAnyRefMethod(stat: untpd.Tree, method: Symbol)(using Context) + extends Message(UnqualifiedCallToAnyRefMethodID) { def kind = MessageKind.PotentialIssue - def msg(using Context) = i"Suspicious call to ${hl("Predef." + method.name)}" + def msg(using Context) = i"Suspicious top-level unqualified call to ${hl(method.name.toString)}" def explain(using Context) = i"""Top-level unqualified calls to ${hl("AnyRef")} or ${hl("Any")} methods such as ${hl(method.name.toString)} are - |resolved to calls on ${hl("Predef")}. This might not be what you intended.""" -} - -class CallToAnyRefMethodOnPackageObject(stat: untpd.Tree, method: Symbol)(using Context) - extends Message(CallToAnyRefMethodOnPackageObjectID) { - def kind = MessageKind.PotentialIssue - def msg(using Context) = i"Suspicious top-level call to ${hl("this." + method.name)}" - def explain(using Context) = - i"""Top-level calls to ${hl("AnyRef")} or ${hl("Any")} methods are resolved to calls on the - |synthetic package object generated for the current file. This might not be - |what you intended.""" + |resolved to calls on ${hl("Predef")} or on imported methods. This might not be what + |you intended.""" } class TraitCompanionWithMutableStatic()(using Context) diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index 108a141e28c6..fe28a8b18833 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -1090,16 +1090,11 @@ object RefChecks { end checkImplicitNotFoundAnnotation - def checkSynchronizedCall(tree: Tree, symbol: Symbol)(using Context) = - if symbol.exists && defn.topClasses.contains(symbol.owner) then - tree.tpe match - case tp: NamedType => - val prefixSymbol = tp.prefix.typeSymbol - if prefixSymbol == defn.ScalaPredefModuleClass then - report.warning(CallToAnyRefMethodOnPredef(tree, symbol), tree) - if prefixSymbol.isPackageObject && !tp.symbol.isConstructor then - report.warning(CallToAnyRefMethodOnPackageObject(tree, symbol), tree) - case _ => () + def checkAnyRefMethodCall(tree: Tree)(using Context) = + if tree.symbol.exists + && defn.topClasses.contains(tree.symbol.owner) + && (!ctx.owner.enclosingClass.exists || ctx.owner.enclosingClass.isPackageObject) then + report.warning(UnqualifiedCallToAnyRefMethod(tree, tree.symbol), tree) } import RefChecks._ @@ -1180,12 +1175,8 @@ class RefChecks extends MiniPhase { thisPhase => tree } - override def transformSelect(tree: Select)(using Context): Tree = - checkSynchronizedCall(tree, tree.denot.symbol) - tree - override def transformIdent(tree: Ident)(using Context): Tree = - checkSynchronizedCall(tree, tree.symbol) + checkAnyRefMethodCall(tree) tree } diff --git a/tests/neg/i17266.check b/tests/neg/i17266.check index 11742a5f2378..7e07e3d43de4 100644 --- a/tests/neg/i17266.check +++ b/tests/neg/i17266.check @@ -1,84 +1,88 @@ -- [E181] Potential Issue Error: tests/neg/i17266.scala:4:2 ------------------------------------------------------------ 4 | synchronized { // error | ^^^^^^^^^^^^ - | Suspicious call to Predef.synchronized + | Suspicious top-level unqualified call to synchronized |--------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level unqualified calls to AnyRef or Any methods such as synchronized are - | resolved to calls on Predef. This might not be what you intended. + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. --------------------------------------------------------------------------------------------------------------------- --- [E182] Potential Issue Error: tests/neg/i17266.scala:9:7 ------------------------------------------------------------ -9 | this.synchronized { // error - | ^^^^^^^^^^^^^^^^^ - | Suspicious top-level call to this.synchronized - |--------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level calls to AnyRef or Any methods are resolved to calls on the - | synthetic package object generated for the current file. This might not be - | what you intended. - --------------------------------------------------------------------------------------------------------------------- --- [E181] Potential Issue Error: tests/neg/i17266.scala:22:2 ----------------------------------------------------------- -22 | wait() // error - | ^^^^ - | Suspicious call to Predef.wait - |-------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level unqualified calls to AnyRef or Any methods such as wait are - | resolved to calls on Predef. This might not be what you intended. - -------------------------------------------------------------------------------------------------------------------- --- [E182] Potential Issue Error: tests/neg/i17266.scala:25:7 ----------------------------------------------------------- -25 | this.wait() // error - | ^^^^^^^^^ - | Suspicious top-level call to this.wait - |-------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level calls to AnyRef or Any methods are resolved to calls on the - | synthetic package object generated for the current file. This might not be - | what you intended. - -------------------------------------------------------------------------------------------------------------------- --- [E181] Potential Issue Error: tests/neg/i17266.scala:32:2 ----------------------------------------------------------- -32 | wait(10) // error - | ^^^^ - | Suspicious call to Predef.wait - |-------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level unqualified calls to AnyRef or Any methods such as wait are - | resolved to calls on Predef. This might not be what you intended. - -------------------------------------------------------------------------------------------------------------------- --- [E182] Potential Issue Error: tests/neg/i17266.scala:35:7 ----------------------------------------------------------- -35 | this.wait(10) // error - | ^^^^^^^^^ - | Suspicious top-level call to this.wait - |-------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level calls to AnyRef or Any methods are resolved to calls on the - | synthetic package object generated for the current file. This might not be - | what you intended. - -------------------------------------------------------------------------------------------------------------------- --- [E181] Potential Issue Error: tests/neg/i17266.scala:42:2 ----------------------------------------------------------- -42 | hashCode() // error - | ^^^^^^^^ - | Suspicious call to Predef.hashCode - |-------------------------------------------------------------------------------------------------------------------- - | Explanation (enabled by `-explain`) - |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level unqualified calls to AnyRef or Any methods such as hashCode are - | resolved to calls on Predef. This might not be what you intended. - -------------------------------------------------------------------------------------------------------------------- --- [E182] Potential Issue Error: tests/neg/i17266.scala:45:7 ----------------------------------------------------------- -45 | this.hashCode() // error - | ^^^^^^^^^^^^^ - | Suspicious top-level call to this.hashCode +-- [E181] Potential Issue Error: tests/neg/i17266.scala:17:2 ----------------------------------------------------------- +17 | synchronized { // error + | ^^^^^^^^^^^^ + | Suspicious top-level unqualified call to synchronized |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Top-level calls to AnyRef or Any methods are resolved to calls on the - | synthetic package object generated for the current file. This might not be - | what you intended. + | Top-level unqualified calls to AnyRef or Any methods such as synchronized are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. -------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:108:2 ---------------------------------------------------------- +108 | wait() // error + | ^^^^ + | Suspicious top-level unqualified call to wait + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:115:2 ---------------------------------------------------------- +115 | wait() // error + | ^^^^ + | Suspicious top-level unqualified call to wait + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:121:2 ---------------------------------------------------------- +121 | wait(10) // error + | ^^^^ + | Suspicious top-level unqualified call to wait + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:128:2 ---------------------------------------------------------- +128 | wait(10) // error + | ^^^^ + | Suspicious top-level unqualified call to wait + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as wait are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:134:2 ---------------------------------------------------------- +134 | hashCode() // error + | ^^^^^^^^ + | Suspicious top-level unqualified call to hashCode + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as hashCode are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- +-- [E181] Potential Issue Error: tests/neg/i17266.scala:141:2 ---------------------------------------------------------- +141 | hashCode() // error + | ^^^^^^^^ + | Suspicious top-level unqualified call to hashCode + |------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Top-level unqualified calls to AnyRef or Any methods such as hashCode are + | resolved to calls on Predef or on imported methods. This might not be what + | you intended. + ------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i17266.scala b/tests/neg/i17266.scala index 1edcebf4781c..5b74ea76810b 100644 --- a/tests/neg/i17266.scala +++ b/tests/neg/i17266.scala @@ -6,7 +6,7 @@ def test1 = } def test2 = - this.synchronized { // error + this.synchronized { // not an error (should be?) println("hello") } @@ -14,36 +14,131 @@ object MyLib def test3 = import MyLib.* - synchronized { // not an error (should be?) + synchronized { // error println("hello") } def test4 = + 1.synchronized { // not an error (should be?) + println("hello") + } + +object Test4: + synchronized { // not an error + println("hello") + } + +object Test5: + def test5 = + synchronized { // not an error + println("hello") + } + +object Test6: + import MyLib.* + synchronized { // not an error + println("hello") + } + +object Test7: + import MyLib.* + def test7 = + synchronized { // not an error + println("hello") + } + +/* +object Test7b: + def test8 = + import MyLib.* + synchronized { // already an error: Reference to synchronized is ambiguous. + println("hello") + } +*/ + +class Test8: + synchronized { // not an error + println("hello") + } + +class Test9: + def test5 = + synchronized { // not an error + println("hello") + } + +class Test10: + import MyLib.* + synchronized { // not an error + println("hello") + } + +class Test11: + import MyLib.* + def test7 = + synchronized { // not an error + println("hello") + } + +trait Test12: + synchronized { // not an error + println("hello") + } + +trait Test13: + def test5 = + synchronized { // not an error + println("hello") + } + +trait Test14: + import MyLib.* + synchronized { // not an error + println("hello") + } + +trait Test15: + import MyLib.* + def test7 = + synchronized { // not an error + println("hello") + } + +def test16 = wait() // error -def test5 = - this.wait() // error +def test17 = + this.wait() // not an error (should be?) -def test6 = +def test18 = import MyLib.* - wait() // not an error (should be?) + wait() // error -def test7 = +def test19 = + 1.wait() // not an error (should be?) + +def test20 = wait(10) // error -def test8 = - this.wait(10) // error +def test21 = + this.wait(10) // not an error (should be?) -def test9 = +def test22 = import MyLib.* - wait(10) // not an error (should be?) + wait(10) // error + +def test23 = + 1.wait(10) // not an error (should be?) -def test10 = +def test24 = hashCode() // error -def test11 = - this.hashCode() // error +def test25 = + this.hashCode() // not an error (should be?) -def test12 = +def test26 = import MyLib.* - hashCode() // not an error (should be?) + hashCode() // error + +def test27 = + 1.hashCode()// not an error (should be? probably not)