From 230622a78b1b5a0cfdfb822257570861ffbcf4f0 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 10 Jan 2023 18:42:28 +0000 Subject: [PATCH] Dealias in ConstantValue, for inline if cond With the change in AvoidMap's derivedSelect, making it widen less aggressively, now inlining can return a constant value typed with an alias to a constant type, e.g. `false: BaseLogger_this.False`. Similarly to inline val, we can dealias these. --- .../src/dotty/tools/dotc/ast/TreeInfo.scala | 2 +- tests/pos/i16641.scala | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i16641.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index b429d7542387..ccc6e99737d4 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -1079,7 +1079,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] => case Inlined(_, Nil, expr) => unapply(expr) case Block(Nil, expr) => unapply(expr) case _ => - tree.tpe.widenTermRefExpr.normalized match + tree.tpe.widenTermRefExpr.dealias.normalized match case ConstantType(Constant(x)) => Some(x) case _ => None } diff --git a/tests/pos/i16641.scala b/tests/pos/i16641.scala new file mode 100644 index 000000000000..e4e8af0e5cba --- /dev/null +++ b/tests/pos/i16641.scala @@ -0,0 +1,25 @@ +trait Logger { + inline def debug: debug = valueOf[debug] + final type debug = false + + // fails + inline final def log(inline s: String): Unit = + inline if (debug) println(s) +} + +trait BaseLogger extends Logger { + // fails + def bar() = log("case1") +} + +object Logger { + inline def log(s: String): Unit = + inline if (valueOf[Logger#debug]) println(s) +} + +class Test: + def fails(x: BaseLogger) = + x.log("case2") + + def works = + Logger.log("case3")