Skip to content

Allow inline vals with alias to constant type #8840

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
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: maybe we should call .normalized, as dealias is just one of many possible normalization operations on types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have that in #8843. But when I tried to normalize it here it did not work. Maybe I need to deeply dealias and then normalize.

case ConstantType(value) =>
if (isIdempotentExpr(tree1)) Literal(value).withSpan(tree.span)
else tree1 match {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/inline-val-constValue-1.scala
Original file line number Diff line number Diff line change
@@ -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
???
}
15 changes: 15 additions & 0 deletions tests/pos/inline-val-constValue-2.scala
Original file line number Diff line number Diff line change
@@ -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
???
}
10 changes: 10 additions & 0 deletions tests/pos/inline-val-constValue-3.scala
Original file line number Diff line number Diff line change
@@ -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]