Skip to content

Fix #9306: Don't allow inline functions with errors in bodies #9319

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
merged 1 commit into from
Jul 16, 2020
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
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,8 @@ class Namer { typer: Typer =>
rhsCtx.setFreshGADTBounds
rhsCtx.gadt.addToConstraint(typeParams)
}
def rhsType = typedAheadExpr(mdef.rhs, (inherited orElse rhsProto).widenExpr)(using rhsCtx).tpe
def rhsType = PrepareInlineable.dropInlineIfError(sym,
typedAheadExpr(mdef.rhs, (inherited orElse rhsProto).widenExpr)(using rhsCtx)).tpe

// Approximate a type `tp` with a type that does not contain skolem types.
val deskolemize = new ApproximatingTypeMap {
Expand Down
27 changes: 18 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ object PrepareInlineable {
if original.mods.hasMod(classOf[untpd.Mod.Transparent]) then rhs
else Typed(rhs, tpt)

/** Return result of evaluating `op`, but drop `Inline` flag and `Body` annotation
* of `sym` in case that leads to errors.
*/
def dropInlineIfError(sym: Symbol, op: => Tree)(using Context): Tree =
val initialErrorCount = ctx.reporter.errorCount
try op
finally
if ctx.reporter.errorCount != initialErrorCount then
sym.resetFlag(Inline)
sym.removeAnnotation(defn.BodyAnnot)

/** Register inline info for given inlineable method `sym`.
*
* @param sym The symbol denotation of the inlineable method for which info is registered
Expand All @@ -227,21 +238,18 @@ object PrepareInlineable {
val inlineCtx = ctx
inlined.updateAnnotation(LazyBodyAnnotation {
given ctx as Context = inlineCtx
val initialErrorCount = ctx.reporter.errorCount
var inlinedBody = treeExpr
if (ctx.reporter.errorCount == initialErrorCount) {
inlinedBody = ctx.compilationUnit.inlineAccessors.makeInlineable(inlinedBody)
checkInlineMethod(inlined, inlinedBody)
if (ctx.reporter.errorCount != initialErrorCount)
inlinedBody = EmptyTree
}
var inlinedBody = dropInlineIfError(inlined, treeExpr)
if inlined.isInlineMethod then
inlinedBody = dropInlineIfError(inlined,
checkInlineMethod(inlined,
ctx.compilationUnit.inlineAccessors.makeInlineable(inlinedBody)))
inlining.println(i"Body to inline for $inlined: $inlinedBody")
inlinedBody
})
}
}

def checkInlineMethod(inlined: Symbol, body: Tree)(using Context): Unit = {
def checkInlineMethod(inlined: Symbol, body: Tree)(using Context): body.type = {
if (inlined.owner.isClass && inlined.owner.seesOpaques)
ctx.error(em"Implementation restriction: No inline methods allowed where opaque type aliases are in scope", inlined.sourcePos)
if Inliner.inInlineMethod(using ctx.outer) then
Expand Down Expand Up @@ -278,5 +286,6 @@ object PrepareInlineable {
}
checkMacro(body)
}
body
}
}
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1936,11 +1936,11 @@ class Typer extends Namer
}

if (sym.isInlineMethod) rhsCtx.addMode(Mode.InlineableBody)
val rhs1 =
val rhs1 = PrepareInlineable.dropInlineIfError(sym,
if sym.isScala2Macro then typedScala2MacroBody(ddef.rhs)(using rhsCtx)
else typedExpr(ddef.rhs, tpt1.tpe.widenExpr)(using rhsCtx)
else typedExpr(ddef.rhs, tpt1.tpe.widenExpr)(using rhsCtx))

if (sym.isInlineMethod)
if sym.isInlineMethod then
val rhsToInline = PrepareInlineable.wrapRHS(ddef, tpt1, rhs1)
PrepareInlineable.registerInlineInfo(sym, rhsToInline)

Expand Down
4 changes: 2 additions & 2 deletions tests/neg-macros/i6530.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object Macros {
inline def q : Int = ${ '[ Int ] } // error
val x : Int = 1 + q // error
}
val x : Int = 1 + q
}
2 changes: 1 addition & 1 deletion tests/neg/i7294-a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ inline given f[T <: Foo] as T = ??? match {
case x: T => x.g(10) // error
}

@main def Test = f // error // error
@main def Test = f
2 changes: 1 addition & 1 deletion tests/neg/i7294-b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ inline given f[T <: Foo] as T = ??? match {
case x: T => x.g(10) // error
}

@main def Test = f // error // error
@main def Test = f
17 changes: 17 additions & 0 deletions tests/neg/i9308.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

object Baz {
class Foo {
private var v = 0
inline def run1 = {
v += { v = 1 } // error
v
}
inline def run2: Int = {
v += { v = 1 } // error
v
}
}
val foo = new Foo
foo.run1
foo.run2
}