Skip to content

Make sure that inline val can be inlined #8724

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
19 changes: 9 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -862,15 +862,14 @@ trait Checking {
}

/** Check that `tree` can be right hand-side or argument to `inline` value or parameter. */
def checkInlineConformant(tree: Tree, isFinal: Boolean, what: => String)(using Context): Unit = {
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
val purityLevel = if (isFinal) Idempotent else Pure
tree.tpe.widenTermRefExpr match {
case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok
case _ =>
if (!ctx.erasedTypes && !ctx.inInlineMethod)
ctx.error(em"$what must be a known value", tree.sourcePos)
}
def checkInlineConformant(tpt: Tree, tree: Tree, sym: Symbol)(using Context): Unit = {
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
case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok
case _ =>
ctx.error(em"type of inline must be a known value", tree.sourcePos)
}

/** A hook to exclude selected symbols from double declaration check */
Expand Down Expand Up @@ -1199,7 +1198,7 @@ trait NoChecking extends ReChecking {
override def checkImplicitConversionDefOK(sym: Symbol)(using Context): Unit = ()
override def checkImplicitConversionUseOK(sym: Symbol, posd: Positioned)(using Context): Unit = ()
override def checkFeasibleParent(tp: Type, pos: SourcePosition, where: => String = "")(using Context): Type = tp
override def checkInlineConformant(tree: Tree, isFinal: Boolean, what: => String)(using Context): Unit = ()
override def checkInlineConformant(tpt: Tree, tree: Tree, sym: Symbol)(using Context): Unit = ()
override def checkNoAlphaConflict(stats: List[Tree])(using Context): Unit = ()
override def checkParentCall(call: Tree, caller: ClassSymbol)(using Context): Unit = ()
override def checkSimpleKinded(tpt: Tree)(using Context): Tree = tpt
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1803,8 +1803,7 @@ class Typer extends Namer
}
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
checkSignatureRepeatedParam(sym)
if (sym.is(Inline, butNot = DeferredOrTermParamOrAccessor))
checkInlineConformant(rhs1, isFinal = sym.is(Final), em"right-hand side of inline $sym")
checkInlineConformant(tpt1, rhs1, sym)
patchFinalVals(vdef1)
vdef1.setDefTree
}
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/i2421.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ inline trait Qux // error: modifier(s) `inline' incompatible with type definitio

object Quux {
inline type T // error: modifier(s) `inline' incompatible with type definition
inline var x: Int = 42 // error: modifier(s) `inline' incompatible with var definition
inline lazy val y: Int = 43 // error: modifier(s) `inline' incompatible with lazy val definition
inline var x: 42 = 42 // error: modifier(s) `inline' incompatible with var definition
inline lazy val y: 43 = 43 // error: modifier(s) `inline' incompatible with lazy val definition
}
4 changes: 4 additions & 0 deletions tests/neg/inline-val.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

inline val a = 1 : Int // error
inline val b: Int = 1 // error
inline val c = b // error