Skip to content

Commit 4bb349c

Browse files
committed
Warn if implicit default shadows given
1 parent ce9fdfc commit 4bb349c

File tree

8 files changed

+37
-0
lines changed

8 files changed

+37
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
232232
case UnnecessaryNN // errorNumber: 216
233233
case ErasedNotPureID // errorNumber: 217
234234
case IllegalErasedDefID // errorNumber: 218
235+
case DefaultShadowsGivenID // errorNumber: 219
235236

236237
def errorNumber = ordinal - 1
237238

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3652,3 +3652,9 @@ final class IllegalErasedDef(sym: Symbol)(using Context) extends TypeMsg(Illegal
36523652
override protected def explain(using Context): String =
36533653
"Only non-lazy immutable values can be `erased`"
36543654
end IllegalErasedDef
3655+
3656+
final class DefaultShadowsGiven(name: Name)(using Context) extends TypeMsg(DefaultShadowsGivenID):
3657+
override protected def msg(using Context): String =
3658+
i"Argument for parameter $name was supplied using a default argument."
3659+
override protected def explain(using Context): String =
3660+
"Usually it's intended to prefer the given in scope, but you must specify it explicitly."

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 sym == ctx.owner
778+
|| methodType.isImplicitMethod && ctx.mode.is(Mode.ImplicitsEnabled)
779+
&& inferImplicitArg(formal, appPos.span).tpe.isError == false
780+
then
781+
report.warning(DefaultShadowsGiven(methodType.paramNames(n)), appPos)
777782
defaultArg.tpe.widen match
778783
case _: MethodOrPoly if testOnly => matchArgs(args1, formals1, n + 1)
779784
case _ => matchArgs(args1, addTyped(treeToArg(defaultArg)), n + 1)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/warn/i23541.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
def fun(x: Int)(using p: Int, q: Int = 0): Int =
3+
if x <= 0 then p * q
4+
else fun(x - 1)(using p = p + x) // warn recurse uses default (instead of given passed down the stack)
5+
6+
def gun(x: Int)(p: Int, q: Int = 0): Int =
7+
if x <= 0 then p * q
8+
else gun(x - 1)(p = p + x) // warn recurse uses default (value not passed down the stack)
9+
10+
def f(using s: String, i: Int = 1): String = s * i
11+
def g(using s: String)(using i: Int = 1): String = s * i
12+
13+
@main def Test =
14+
println(fun(3)(using p = 0, q = 1))
15+
locally:
16+
given String = "ab"
17+
println(f) // prints "ab"
18+
println(g) // prints "ab"
19+
locally:
20+
println(f(using s = "ab")) // prints "ab"
21+
println(g(using s = "ab")) // prints "ab"
22+
locally:
23+
given Int = 2
24+
println(f(using s = "ab")) // warn uses default instead of given // prints "ab"
25+
println(g(using s = "ab")) // prints "abab"

0 commit comments

Comments
 (0)