Skip to content

Commit 4cf3bf0

Browse files
authored
Merge pull request #10114 from dotty-staging/fix-#10107
Fix #10107: Improve `inline if` constant condition detraction and reporting
2 parents 5dd9ecf + af9cf43 commit 4cf3bf0

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
12351235
else Block(cond1 :: Nil, selected)
12361236
case cond1 =>
12371237
if (tree.isInline)
1238-
errorTree(tree, em"""cannot reduce inline if
1239-
| its condition ${tree.cond}
1240-
| is not a constant value""")
1238+
errorTree(tree,
1239+
em"Cannot reduce `inline if` because its condition is not a constant value: $cond1")
12411240
else
12421241
cond1.computeNullableDeeply()
12431242
val if1 = untpd.cpy.If(tree)(cond = untpd.TypedSplice(cond1))
@@ -1474,10 +1473,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
14741473
}.apply(Nil, tree)
14751474

14761475
object ConstantValue {
1477-
def unapply(tree: Tree)(using Context): Option[Any] = tree.tpe.widenTermRefExpr.normalized match {
1478-
case ConstantType(Constant(x)) => Some(x)
1479-
case _ => None
1480-
}
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
14811485
}
14821486

14831487
}
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)