Skip to content

Commit abdd6c9

Browse files
committed
Name the parameter using a default
1 parent 58bc4da commit abdd6c9

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,8 +3671,8 @@ final class DefaultShadowsGiven(name: Name)(using Context) extends TypeMsg(Defau
36713671
override protected def explain(using Context): String =
36723672
"Usually the given in scope is intended, but you must specify it after explicit `using`."
36733673

3674-
final class RecurseWithDefault(using Context) extends TypeMsg(RecurseWithDefaultID):
3674+
final class RecurseWithDefault(name: Name)(using Context) extends TypeMsg(RecurseWithDefaultID):
36753675
override protected def msg(using Context): String =
3676-
i"Recursive call used a default argument."
3676+
i"Recursive call used a default argument for parameter $name."
36773677
override protected def explain(using Context): String =
36783678
"It's more explicit to pass current or modified arguments in a recursion."

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,11 @@ class TailRec extends MiniPhase {
327327

328328
if isRecursiveCall then
329329
if ctx.settings.Whas.recurseWithDefault then
330-
if tree.args.exists(_.symbol.name.is(DefaultGetterName)) then
331-
report.warning(RecurseWithDefault(), tree.srcPos)
330+
tree.args.find(_.symbol.name.is(DefaultGetterName)) match
331+
case Some(arg) =>
332+
val DefaultGetterName(_, index) = arg.symbol.name: @unchecked
333+
report.warning(RecurseWithDefault(calledMethod.info.firstParamNames(index)), tree.srcPos)
334+
case _ =>
332335

333336
if (inTailPosition) {
334337
tailrec.println("Rewriting tail recursive call: " + tree.span)

tests/warn/i23541.check

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- [E220] Type Warning: tests/warn/i23541.scala:29:13 ------------------------------------------------------------------
2+
29 | println(f(using s = "ab")) // warn uses default instead of given // prints "ab"
3+
| ^^^^^^^^^^^^^^^^^
4+
| Argument for implicit parameter i was supplied using a default argument.
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E221] Type Warning: tests/warn/i23541.scala:5:17 -------------------------------------------------------------------
8+
5 | else fun(x - 1)(using p = p + x) // warn recurse uses default (instead of given passed down the stack)
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
| Recursive call used a default argument for parameter q.
11+
|
12+
| longer explanation available when compiling with `-explain`
13+
-- [E221] Type Warning: tests/warn/i23541.scala:9:17 -------------------------------------------------------------------
14+
9 | else gun(x - 1)(p = p + x) // warn recurse uses default (value not passed down the stack)
15+
| ^^^^^^^^^^^^^^^^^^^^^
16+
| Recursive call used a default argument for parameter q.
17+
|
18+
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)