Skip to content

Fix #10107: Improve inline if constant condition detraction and reporting #10114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
else Block(cond1 :: Nil, selected)
case cond1 =>
if (tree.isInline)
errorTree(tree, em"""cannot reduce inline if
| its condition ${tree.cond}
| is not a constant value""")
errorTree(tree,
em"Cannot reduce `inline if` because its condition is not a constant value: $cond1")
else
cond1.computeNullableDeeply()
val if1 = untpd.cpy.If(tree)(cond = untpd.TypedSplice(cond1))
Expand Down Expand Up @@ -1474,10 +1473,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
}.apply(Nil, tree)

object ConstantValue {
def unapply(tree: Tree)(using Context): Option[Any] = tree.tpe.widenTermRefExpr.normalized match {
case ConstantType(Constant(x)) => Some(x)
case _ => None
}
def unapply(tree: Tree)(using Context): Option[Any] =
tree match
case Typed(expr, _) => unapply(expr)
case Inlined(_, Nil, expr) => unapply(expr)
case Block(Nil, expr) => unapply(expr)
case _ =>
tree.tpe.widenTermRefExpr.normalized match
case ConstantType(Constant(x)) => Some(x)
case _ => None
}

}
13 changes: 13 additions & 0 deletions tests/pos-macros/i10107b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._

inline def isTrue: Boolean = ${ isTrueImpl }
def isTrueImpl(using qctx: QuoteContext) = {
Expr(true)
}

inline def oneOf(): String = {
inline if isTrue then
"foo"
else
"bar"
}
1 change: 1 addition & 0 deletions tests/pos-macros/i10107b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test1 = oneOf()
4 changes: 4 additions & 0 deletions tests/pos/i10107.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inline def f() =
inline if true: Boolean then () else ()

def test = f()