Skip to content

Commit 134afc6

Browse files
committed
Transform unused arguments to dummy values in typer
1 parent 3b030cf commit 134afc6

File tree

7 files changed

+30
-74
lines changed

7 files changed

+30
-74
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Compiler {
5151
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
5252
new ElimJavaPackages), // Eliminate syntactic references to Java packages
5353
List(new CheckStatic, // Check restrictions that apply to @static members
54-
new UnusedRefs, // Removes all calls and references to unused values
5554
new ElimRepeated, // Rewrite vararg parameters and arguments
5655
new NormalizeFlags, // Rewrite some definition flags
5756
new ExtensionMethods, // Expand methods of value classes with extension methods

compiler/src/dotty/tools/dotc/core/Mode.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,4 @@ object Mode {
100100
/** We are in the IDE */
101101
val Interactive = newMode(20, "Interactive")
102102

103-
/** We are currently in code that will not be used at runtime.
104-
* It can be in an argument to an unused parameter or a type selection.
105-
*/
106-
val Unused = newMode(21, "Unused")
107103
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
173173
override def transform(tree: Tree)(implicit ctx: Context): Tree =
174174
try tree match {
175175
case tree: Ident if !tree.isType =>
176-
chekedUnused(tree)
176+
checkNotUnused(tree)
177177
tree.tpe match {
178178
case tpe: ThisType => This(tpe.cls).withPos(tree.pos)
179179
case _ => paramFwd.adaptRef(fixSignature(tree))
180180
}
181181
case tree @ Select(qual, name) =>
182-
chekedUnused(tree)
182+
checkNotUnused(tree)
183183
if (name.isTypeName) {
184184
Checking.checkRealizable(qual.tpe, qual.pos.focus)
185-
super.transform(tree)(ctx.addMode(Mode.Unused))
185+
super.transform(tree)(ctx.addMode(Mode.Type))
186186
}
187187
else
188188
transformSelect(paramFwd.adaptRef(fixSignature(tree)), Nil)
@@ -191,17 +191,14 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
191191
ctx.error(SuperCallsNotAllowedInline(ctx.owner), tree.pos)
192192
super.transform(tree)
193193
case tree: Apply =>
194-
def transformApply() = {
195-
val ctx1 = if (tree.fun.tpe.widen.isUnusedMethod) ctx.addMode(Mode.Unused) else ctx
196-
cpy.Apply(tree)(transform(tree.fun), transform(tree.args)(ctx1))
197-
}
198194
methPart(tree) match {
199195
case Select(nu: New, nme.CONSTRUCTOR) if isCheckable(nu) =>
200196
// need to check instantiability here, because the type of the New itself
201197
// might be a type constructor.
202198
Checking.checkInstantiable(tree.tpe, nu.pos)
203-
withNoCheckNews(nu :: Nil)(transformApply())
204-
case _ => transformApply()
199+
withNoCheckNews(nu :: Nil)(super.transform(tree))
200+
case _ =>
201+
super.transform(tree)
205202
}
206203
case tree: TypeApply =>
207204
val tree1 @ TypeApply(fn, args) = normalizeTypeArgs(tree)
@@ -311,8 +308,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
311308
throw ex
312309
}
313310

314-
private def chekedUnused(tree: RefTree)(implicit ctx: Context): Unit = {
315-
if (tree.symbol.is(Unused) && !ctx.mode.is(Mode.Unused))
311+
private def checkNotUnused(tree: RefTree)(implicit ctx: Context): Unit = {
312+
if (tree.symbol.is(Unused) && !ctx.mode.is(Mode.Type))
316313
ctx.error(i"`unused` value $tree can only be used as unused arguments", tree.pos)
317314
}
318315
}

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,16 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
771771
checkCanEqual(left.tpe.widen, right.tpe.widen, app.pos)
772772
case _ =>
773773
}
774-
app
774+
app match {
775+
case Apply(fun, args) if fun.tpe.widen.isUnusedMethod =>
776+
val erasedArgs = args.map { arg =>
777+
if (!isPureExpr(arg))
778+
ctx.warning("This argument is given to an unused parameter. This expression will not be evaluated.", arg.pos)
779+
defaultValue(arg.tpe)
780+
}
781+
tpd.cpy.Apply(app)(fun = fun, args = erasedArgs)
782+
case _ => app
783+
}
775784
}
776785
}
777786

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ object EtaExpansion {
2727
else {
2828
val name = UniqueName.fresh(prefix)
2929
val liftedType = fullyDefinedType(expr.tpe.widen, "lifted expression", expr.pos)
30-
val flags = expr.symbol.flags & Unused | Synthetic
31-
val sym = ctx.newSymbol(ctx.owner, name, flags, liftedType, coord = positionCoord(expr.pos))
30+
val sym = ctx.newSymbol(ctx.owner, name, Synthetic, liftedType, coord = positionCoord(expr.pos))
3231
defs += ValDef(sym, expr)
3332
ref(sym.termRef).withPos(expr.pos)
3433
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,17 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
20322032
arg :: implicitArgs(formals1)
20332033
}
20342034
}
2035-
val args = implicitArgs(wtp.paramInfos)
2035+
def eraseUnusedArgs(args: List[Tree]): List[Tree] = {
2036+
if (!wtp.isUnusedMethod) args
2037+
else args.map { arg =>
2038+
arg.tpe match {
2039+
case _: AmbiguousImplicits | _: SearchFailureType => arg
2040+
case tpe => defaultValue(tpe)
2041+
}
2042+
}
2043+
}
2044+
val args = eraseUnusedArgs(implicitArgs(wtp.paramInfos))
2045+
20362046

20372047
def propagatedFailure(args: List[Tree]): Type = args match {
20382048
case arg :: args1 =>

0 commit comments

Comments
 (0)