Skip to content

Commit af9cf43

Browse files
committed
Improve inliner constant detection
1 parent 010acc6 commit af9cf43

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,10 +1473,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
14731473
}.apply(Nil, tree)
14741474

14751475
object ConstantValue {
1476-
def unapply(tree: Tree)(using Context): Option[Any] = tree.tpe.widenTermRefExpr.normalized match {
1477-
case ConstantType(Constant(x)) => Some(x)
1478-
case _ => None
1479-
}
1476+
def unapply(tree: Tree)(using Context): Option[Any] =
1477+
tree match
1478+
case Typed(expr, _) => unapply(expr)
1479+
case Inlined(_, Nil, expr) => unapply(expr)
1480+
case Block(Nil, expr) => unapply(expr)
1481+
case _ =>
1482+
tree.tpe.widenTermRefExpr.normalized match
1483+
case ConstantType(Constant(x)) => Some(x)
1484+
case _ => None
14801485
}
14811486

14821487
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
3+
inline def isTrue: Boolean = ${ isTrueImpl }
4+
def isTrueImpl(using qctx: QuoteContext) = {
5+
Expr(true)
6+
}
7+
8+
inline def oneOf(): String = {
9+
inline if isTrue then
10+
"foo"
11+
else
12+
"bar"
13+
}

tests/pos-macros/i10107b/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test1 = oneOf()

tests/pos/i10107.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
inline def f() =
2+
inline if true: Boolean then () else ()
3+
4+
def test = f()

0 commit comments

Comments
 (0)