Skip to content

Commit 83f2a61

Browse files
committed
Warn if implicit default shadows given
-Wrecurse-with-default warns on self-recursion with default arg.
1 parent 207604b commit 83f2a61

File tree

11 files changed

+66
-10
lines changed

11 files changed

+66
-10
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private sealed trait WarningSettings:
167167
private val WimplausiblePatterns = BooleanSetting(WarningSetting, "Wimplausible-patterns", "Warn if comparison with a pattern value looks like it might always fail.")
168168
private val WunstableInlineAccessors = BooleanSetting(WarningSetting, "WunstableInlineAccessors", "Warn an inline methods has references to non-stable binary APIs.")
169169
private val WtoStringInterpolated = BooleanSetting(WarningSetting, "Wtostring-interpolated", "Warn a standard interpolator used toString on a reference type.")
170+
private val WrecurseWithDefault = BooleanSetting(WarningSetting, "Wrecurse-with-default", "Warn when a method calls itself with a default argument.")
170171
private val Wunused: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
171172
WarningSetting,
172173
name = "Wunused",
@@ -309,6 +310,7 @@ private sealed trait WarningSettings:
309310
def implausiblePatterns(using Context): Boolean = allOr(WimplausiblePatterns)
310311
def unstableInlineAccessors(using Context): Boolean = allOr(WunstableInlineAccessors)
311312
def toStringInterpolated(using Context): Boolean = allOr(WtoStringInterpolated)
313+
def recurseWithDefault(using Context): Boolean = allOr(WrecurseWithDefault)
312314
def checkInit(using Context): Boolean = allOr(WcheckInit)
313315

314316
/** -X "Extended" or "Advanced" settings */

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
233233
case ErasedNotPureID // errorNumber: 217
234234
case IllegalErasedDefID // errorNumber: 218
235235
case CannotInstantiateQuotedTypeVarID // errorNumber: 219
236+
case DefaultShadowsGivenID // errorNumber: 220
237+
case RecurseWithDefaultID // errorNumber: 221
236238

237239
def errorNumber = ordinal - 1
238240

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,3 +3664,15 @@ final class IllegalErasedDef(sym: Symbol)(using Context) extends TypeMsg(Illegal
36643664
override protected def explain(using Context): String =
36653665
"Only non-lazy immutable values can be `erased`"
36663666
end IllegalErasedDef
3667+
3668+
final class DefaultShadowsGiven(name: Name)(using Context) extends TypeMsg(DefaultShadowsGivenID):
3669+
override protected def msg(using Context): String =
3670+
i"Argument for implicit parameter $name was supplied using a default argument."
3671+
override protected def explain(using Context): String =
3672+
"Usually the given in scope is intended, but you must specify it after explicit `using`."
3673+
3674+
final class RecurseWithDefault(using Context) extends TypeMsg(RecurseWithDefaultID):
3675+
override protected def msg(using Context): String =
3676+
i"Recursive call used a default argument."
3677+
override protected def explain(using Context): String =
3678+
"It's more explicit to pass current or modified arguments in a recursion."

compiler/src/dotty/tools/dotc/transform/TailRec.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package transform
44
import ast.{TreeTypeMap, tpd}
55
import config.Printers.tailrec
66
import core.*
7-
import Contexts.*, Flags.*, Symbols.*, Decorators.em
7+
import Contexts.*, Flags.*, Symbols.*, Decorators.*
88
import Constants.Constant
9-
import NameKinds.{TailLabelName, TailLocalName, TailTempName}
9+
import NameKinds.{DefaultGetterName, TailLabelName, TailLocalName, TailTempName}
1010
import StdNames.nme
1111
import reporting.*
1212
import transform.MegaPhase.MiniPhase
@@ -325,7 +325,11 @@ class TailRec extends MiniPhase {
325325
method.matches(calledMethod) &&
326326
enclosingClass.appliedRef.widen <:< prefix.tpe.widenDealias
327327

328-
if (isRecursiveCall)
328+
if isRecursiveCall then
329+
if ctx.settings.Whas.recurseWithDefault then
330+
if tree.args.exists(_.symbol.name.is(DefaultGetterName)) then
331+
report.warning(RecurseWithDefault(), tree.srcPos)
332+
329333
if (inTailPosition) {
330334
tailrec.println("Rewriting tail recursive call: " + tree.span)
331335
rewrote = true

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,11 @@ trait Applications extends Compatibility {
774774
methodType.isImplicitMethod && (applyKind == ApplyKind.Using || failEmptyArgs)
775775

776776
if !defaultArg.isEmpty then
777+
if methodType.isImplicitMethod && ctx.mode.is(Mode.ImplicitsEnabled)
778+
&& !inferImplicitArg(formal, appPos.span).tpe.isError
779+
then
780+
report.warning(DefaultShadowsGiven(methodType.paramNames(n)), appPos)
781+
777782
defaultArg.tpe.widen match
778783
case _: MethodOrPoly if testOnly => matchArgs(args1, formals1, n + 1)
779784
case _ => matchArgs(args1, addTyped(treeToArg(defaultArg)), n + 1)

scaladoc/src/dotty/tools/scaladoc/tasty/TypesSupport.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package dotty.tools.scaladoc
22
package tasty
33

4-
import scala.jdk.CollectionConverters._
5-
6-
import scala.quoted._
4+
import scala.annotation.*
5+
import scala.jdk.CollectionConverters.*
6+
import scala.quoted.*
77
import scala.util.control.NonFatal
88

9-
import NameNormalizer._
10-
import SyntheticsSupport._
9+
import NameNormalizer.*
10+
import SyntheticsSupport.*
1111

1212
trait TypesSupport:
1313
self: TastyParser =>
@@ -81,6 +81,7 @@ trait TypesSupport:
8181
case tpe => inner(tpe, skipThisTypePrefix)
8282

8383
// TODO #23 add support for all types signatures that make sense
84+
@nowarn("id=E219")
8485
private def inner(
8586
using Quotes,
8687
)(

tests/neg/19414-desugared.check renamed to tests/neg/i19414-desugared.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- [E172] Type Error: tests/neg/19414-desugared.scala:22:34 ------------------------------------------------------------
1+
-- [E172] Type Error: tests/neg/i19414-desugared.scala:22:34 -----------------------------------------------------------
22
22 | summon[BodySerializer[JsObject]] // error: Ambiguous given instances
33
| ^
44
|No best given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
File renamed without changes.

tests/neg/19414.check renamed to tests/neg/i19414.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- [E172] Type Error: tests/neg/19414.scala:15:34 ----------------------------------------------------------------------
1+
-- [E172] Type Error: tests/neg/i19414.scala:15:34 ---------------------------------------------------------------------
22
15 | summon[BodySerializer[JsObject]] // error: Ambiguous given instances
33
| ^
44
|No best given instance of type BodySerializer[JsObject] was found for parameter x of method summon in object Predef.
File renamed without changes.

0 commit comments

Comments
 (0)