Skip to content

Commit 4c58507

Browse files
committed
Original of literal
1 parent 869ef7a commit 4c58507

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import dotty.tools.dotc.report
1616
import dotty.tools.dotc.reporting.{CodeAction, UnusedSymbol}
1717
import dotty.tools.dotc.rewrites.Rewrites
1818
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
19-
import dotty.tools.dotc.typer.ImportInfo
19+
import dotty.tools.dotc.typer.{ImportInfo, Typer}
2020
import dotty.tools.dotc.util.{Property, Spans, SrcPos}, Spans.Span
2121
import dotty.tools.dotc.util.Chars.{isLineBreakChar, isWhitespace}
2222
import dotty.tools.dotc.util.chaining.*
@@ -40,9 +40,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
4040
override def isRunnable(using Context): Boolean = super.isRunnable && ctx.settings.WunusedHas.any && !ctx.isJava
4141

4242
override def prepareForUnit(tree: Tree)(using Context): Context =
43-
val infos = tree.getAttachment(refInfosKey).getOrElse {
43+
val infos = tree.getAttachment(refInfosKey).getOrElse:
4444
RefInfos().tap(tree.withAttachment(refInfosKey, _))
45-
}
4645
ctx.fresh.setProperty(refInfosKey, infos)
4746
override def transformUnit(tree: Tree)(using Context): tree.type =
4847
if phaseMode == PhaseMode.Report then
@@ -68,6 +67,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6867
refUsage(tree.symbol)
6968
tree
7069

70+
override def transformLiteral(tree: Literal)(using Context): tree.type =
71+
tree.getAttachment(Typer.AdaptedTree).foreach(transformAllDeep)
72+
tree
73+
7174
override def prepareForCaseDef(tree: CaseDef)(using Context): Context =
7275
nowarner.traverse(tree.pat)
7376
ctx

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ object Typer {
8484
/** Indicates that an expression is explicitly ascribed to [[Unit]] type. */
8585
val AscribedToUnit = new Property.StickyKey[Unit]
8686

87+
/** Tree adaptation lost fidelity; this attachment preserves the original tree. */
88+
val AdaptedTree = new Property.StickyKey[tpd.Tree]
89+
8790
/** An attachment on a Select node with an `apply` field indicating that the `apply`
8891
* was inserted by the Typer.
8992
*/
@@ -4565,16 +4568,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
45654568
}
45664569

45674570
/** Adapt an expression of constant type to a different constant type `tpe`. */
4568-
def adaptConstant(tree: Tree, tpe: ConstantType): Tree = {
4569-
def lit = Literal(tpe.value).withSpan(tree.span)
4570-
tree match {
4571-
case Literal(c) => lit
4572-
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
4573-
case tree =>
4574-
if (isIdempotentExpr(tree)) lit // See discussion in phase Literalize why we demand isIdempotentExpr
4575-
else Block(tree :: Nil, lit)
4576-
}
4577-
}
4571+
def adaptConstant(tree: Tree, tpe: ConstantType): Tree =
4572+
def lit = Literal(tpe.value).withSpan(tree.span).withAttachment(AdaptedTree, tree)
4573+
tree match
4574+
case Literal(_) => lit
4575+
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
4576+
case tree =>
4577+
if isIdempotentExpr(tree) then lit // See discussion in phase FirstTransform why we demand isIdempotentExpr
4578+
else Block(tree :: Nil, lit)
45784579

45794580
def toSAM(tree: Tree, samParent: Type): Tree = tree match {
45804581
case tree: Block => tpd.cpy.Block(tree)(tree.stats, toSAM(tree.expr, samParent))

tests/warn/i17318.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
//> using options -Wunused:all
3+
4+
object events {
5+
final val PollOut = 0x002
6+
transparent inline def POLLIN = 0x001
7+
}
8+
9+
def withShort(v: Short): Unit = ???
10+
def withInt(v: Int): Unit = ???
11+
12+
def usage() =
13+
import events.POLLIN // reports unused
14+
def v: Short = POLLIN
15+
println(v)
16+
17+
def usage2() =
18+
import events.POLLIN // reports unused
19+
withShort(POLLIN)
20+
21+
def usage3() =
22+
import events.POLLIN // does not report unused
23+
withInt(POLLIN)
24+
25+
def usage4() =
26+
import events.POLLIN // reports unused
27+
withShort(POLLIN)
28+
29+
def value = 42
30+
def withDouble(v: Double): Unit = ???
31+
def usage5() = withDouble(value)
32+
def usage6() = withShort(events.POLLIN)

0 commit comments

Comments
 (0)