From c6085a4b230cb914aa79541761e10cddbbe529ed Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 20 Feb 2023 23:39:00 -0800 Subject: [PATCH] Optionally warn if params should have parens --- .../scala/tools/nsc/ast/parser/Parsers.scala | 11 +++++++++-- .../scala/tools/nsc/typechecker/Contexts.scala | 12 ++++++------ test/files/neg/parens-for-params.check | 7 +++++++ test/files/neg/parens-for-params.scala | 8 ++++++++ test/files/pos/parens-for-params-silent.scala | 8 ++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 test/files/neg/parens-for-params.check create mode 100644 test/files/neg/parens-for-params.scala create mode 100644 test/files/pos/parens-for-params-silent.scala diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index cf8b278818da..127b75e9e665 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -777,8 +777,15 @@ self => /** Convert tree to formal parameter list. */ def convertToParams(tree: Tree): List[ValDef] = tree match { - case Parens(ts) => ts map convertToParam - case _ => List(convertToParam(tree)) + case Parens(ts) => ts.map(convertToParam) + case Typed(Ident(_), _) => + val msg = "parentheses are required around the parameter of a lambda" + val wrn = sm"""|$msg + |Use '-Wconf:msg=lambda-parens:s' to silence this warning.""" + if (currentRun.isScala3) + deprecationWarning(tree.pos.point, wrn, "2.13.11") + List(convertToParam(tree)) + case _ => List(convertToParam(tree)) } /** Convert tree to formal parameter. */ diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index 2b97393aac55..6ff69b02252b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -1374,17 +1374,17 @@ trait Contexts { self: Analyzer => val parent = currentClass.parentSymbols.find(_.isNonBottomSubClass(inherited1.owner)).getOrElse(NoSymbol) val inherit = if (parent.exists && parent != inherited1.owner) s", inherited through parent $parent" else "" val message = - s"""it is both defined in the enclosing ${outer1.owner} and inherited in the enclosing $classDesc as $inherited1 (defined in ${inherited1.ownsString}$inherit) - |In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope. - |Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.${outer1.name}`.""".stripMargin + sm"""|it is both defined in the enclosing ${outer1.owner} and inherited in the enclosing $classDesc as $inherited1 (defined in ${inherited1.ownsString}$inherit) + |In Scala 2, symbols inherited from a superclass shadow symbols defined in an outer scope. + |Such references are ambiguous in Scala 3. To continue using the inherited symbol, write `this.${outer1.name}`.""" if (currentRun.isScala3) Some(LookupAmbiguous(message)) else { // passing the message to `typedIdent` as attachment, we don't have the position here to report the warning inherited.updateAttachment(LookupAmbiguityWarning( - s"""reference to ${outer1.name} is ambiguous; - |$message - |Or use `-Wconf:msg=legacy-binding:s` to silence this warning.""".stripMargin)) + sm"""|reference to ${outer1.name} is ambiguous; + |$message + |Or use `-Wconf:msg=legacy-binding:s` to silence this warning.""")) None } } diff --git a/test/files/neg/parens-for-params.check b/test/files/neg/parens-for-params.check new file mode 100644 index 000000000000..36d277404e0b --- /dev/null +++ b/test/files/neg/parens-for-params.check @@ -0,0 +1,7 @@ +parens-for-params.scala:5: warning: parentheses are required around the parameter of a lambda +Use '-Wconf:msg=lambda-parens:s' to silence this warning. + x: Int => x * 2 + ^ +error: No warnings can be incurred under -Werror. +1 warning +1 error diff --git a/test/files/neg/parens-for-params.scala b/test/files/neg/parens-for-params.scala new file mode 100644 index 000000000000..92e77875bdd1 --- /dev/null +++ b/test/files/neg/parens-for-params.scala @@ -0,0 +1,8 @@ +// scalac: -Werror -Xlint -Xsource:3 + +class C { + def f = { + x: Int => x * 2 + } + def g = (x: Int) => x * 2 +} diff --git a/test/files/pos/parens-for-params-silent.scala b/test/files/pos/parens-for-params-silent.scala new file mode 100644 index 000000000000..6eb07a5c3c8a --- /dev/null +++ b/test/files/pos/parens-for-params-silent.scala @@ -0,0 +1,8 @@ +// scalac: -Werror -Wconf:msg=lambda-parens:s -Xsource:3 + +class C { + def f = { + x: Int => x * 2 + } + def g = (x: Int) => x * 2 +}