diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index d9b385b00469..d50a258fe1fc 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -510,7 +510,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] => */ def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = { val tree1 = ConstFold(tree) - tree1.tpe.widenTermRefExpr match { + tree1.tpe.widenTermRefExpr.dealias match { case ConstantType(value) => if (isIdempotentExpr(tree1)) Literal(value).withSpan(tree.span) else tree1 match { diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index cf18be49fce8..11e16c6aabd9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -887,7 +887,7 @@ trait Checking { if sym.is(Inline, butNot = DeferredOrTermParamOrAccessor) && !ctx.erasedTypes && !ctx.inInlineMethod then // final vals can be marked inline even if they're not pure, see Typer#patchFinalVals val purityLevel = if (sym.is(Final)) Idempotent else Pure - tpt.tpe.widenTermRefExpr match + tpt.tpe.widenTermRefExpr.dealias match case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok case _ => ctx.error(em"type of inline must be a known value", tree.sourcePos) diff --git a/tests/pos/inline-val-constValue-1.scala b/tests/pos/inline-val-constValue-1.scala new file mode 100644 index 000000000000..efd482cd5c09 --- /dev/null +++ b/tests/pos/inline-val-constValue-1.scala @@ -0,0 +1,15 @@ +import compiletime._ + +class C: + type X <: Tuple + +def test: Unit = + val a: C { type X = Tuple1[Any] } = ??? + f(a) + +inline def f(c: C): Unit = { + inline val size = constValue[Tuple.Size[c.X]] + val n = size + val m: Int = n + ??? +} diff --git a/tests/pos/inline-val-constValue-2.scala b/tests/pos/inline-val-constValue-2.scala new file mode 100644 index 000000000000..33ae23069fa2 --- /dev/null +++ b/tests/pos/inline-val-constValue-2.scala @@ -0,0 +1,15 @@ +import compiletime._ + +class C: + type N <: Int + +def test: Unit = + val a: C { type N = 3 } = ??? + f(a) + +inline def f(c: C): Unit = { + inline val size = constValue[c.N] + val n = size + val m: Int = n + ??? +} diff --git a/tests/pos/inline-val-constValue-3.scala b/tests/pos/inline-val-constValue-3.scala new file mode 100644 index 000000000000..3918173012fb --- /dev/null +++ b/tests/pos/inline-val-constValue-3.scala @@ -0,0 +1,10 @@ + +inline def f[N <: Int]: Unit = { + inline val size = compiletime.constValue[N] + inline val n = size + val m: Int = n + ??? +} + +type N = 4 +def test: Unit = f[N]