Skip to content

Commit

Permalink
Original of literal
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 7, 2025
1 parent 869ef7a commit 4c58507
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import dotty.tools.dotc.report
import dotty.tools.dotc.reporting.{CodeAction, UnusedSymbol}
import dotty.tools.dotc.rewrites.Rewrites
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
import dotty.tools.dotc.typer.ImportInfo
import dotty.tools.dotc.typer.{ImportInfo, Typer}
import dotty.tools.dotc.util.{Property, Spans, SrcPos}, Spans.Span
import dotty.tools.dotc.util.Chars.{isLineBreakChar, isWhitespace}
import dotty.tools.dotc.util.chaining.*
Expand All @@ -40,9 +40,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
override def isRunnable(using Context): Boolean = super.isRunnable && ctx.settings.WunusedHas.any && !ctx.isJava

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

override def transformLiteral(tree: Literal)(using Context): tree.type =
tree.getAttachment(Typer.AdaptedTree).foreach(transformAllDeep)
tree

override def prepareForCaseDef(tree: CaseDef)(using Context): Context =
nowarner.traverse(tree.pat)
ctx
Expand Down
21 changes: 11 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ object Typer {
/** Indicates that an expression is explicitly ascribed to [[Unit]] type. */
val AscribedToUnit = new Property.StickyKey[Unit]

/** Tree adaptation lost fidelity; this attachment preserves the original tree. */
val AdaptedTree = new Property.StickyKey[tpd.Tree]

/** An attachment on a Select node with an `apply` field indicating that the `apply`
* was inserted by the Typer.
*/
Expand Down Expand Up @@ -4565,16 +4568,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
}

/** Adapt an expression of constant type to a different constant type `tpe`. */
def adaptConstant(tree: Tree, tpe: ConstantType): Tree = {
def lit = Literal(tpe.value).withSpan(tree.span)
tree match {
case Literal(c) => lit
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
case tree =>
if (isIdempotentExpr(tree)) lit // See discussion in phase Literalize why we demand isIdempotentExpr
else Block(tree :: Nil, lit)
}
}
def adaptConstant(tree: Tree, tpe: ConstantType): Tree =
def lit = Literal(tpe.value).withSpan(tree.span).withAttachment(AdaptedTree, tree)
tree match
case Literal(_) => lit
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
case tree =>
if isIdempotentExpr(tree) then lit // See discussion in phase FirstTransform why we demand isIdempotentExpr
else Block(tree :: Nil, lit)

def toSAM(tree: Tree, samParent: Type): Tree = tree match {
case tree: Block => tpd.cpy.Block(tree)(tree.stats, toSAM(tree.expr, samParent))
Expand Down
32 changes: 32 additions & 0 deletions tests/warn/i17318.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

//> using options -Wunused:all

object events {
final val PollOut = 0x002
transparent inline def POLLIN = 0x001
}

def withShort(v: Short): Unit = ???
def withInt(v: Int): Unit = ???

def usage() =
import events.POLLIN // reports unused
def v: Short = POLLIN
println(v)

def usage2() =
import events.POLLIN // reports unused
withShort(POLLIN)

def usage3() =
import events.POLLIN // does not report unused
withInt(POLLIN)

def usage4() =
import events.POLLIN // reports unused
withShort(POLLIN)

def value = 42
def withDouble(v: Double): Unit = ???
def usage5() = withDouble(value)
def usage6() = withShort(events.POLLIN)

0 comments on commit 4c58507

Please sign in to comment.