Skip to content

Commit

Permalink
Merge pull request scala#8840 from dotty-staging/allow-inline-vals-wi…
Browse files Browse the repository at this point in the history
…th-alias-to-constant-type

Allow inline vals with alias to constant type
  • Loading branch information
nicolasstucki authored Apr 30, 2020
2 parents d7c0574 + 8b23e2b commit 6f51dbb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
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 {
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]

0 comments on commit 6f51dbb

Please sign in to comment.