Skip to content

Commit 5f9e569

Browse files
committed
Don't inline when errors are detected
Inlining is only well-defined if the body to inline does not have any errors. We therefore check for errors before we perform any transformation of trees related to inlining. The error check is global, i.e. we stop on any error not just on errors in the code to be inlined. This is a safe approximation, of course.
1 parent f467be6 commit 5f9e569

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,6 @@ trait Reporting { this: Context =>
171171
throw ex
172172
}
173173
}
174-
175-
/** Implements a fold that applies the function `f` to the result of `op` if
176-
* there are no new errors in the reporter
177-
*
178-
* @param op operation checked for errors
179-
* @param f function applied to result of op
180-
* @return either the result of `op` if it had errors or the result of `f`
181-
* applied to it
182-
*/
183-
def withNoError[A, B >: A](op: => A)(f: A => B): B = {
184-
val before = reporter.errorCount
185-
val op0 = op
186-
187-
if (reporter.errorCount > before) op0
188-
else f(op0)
189-
}
190174
}
191175

192176
/**

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import transform.TypeUtils._
2727
object Inliner {
2828
import tpd._
2929

30-
/** Adds accessors accessors for all non-public term members accessed
30+
/** Adds accessors for all non-public term members accessed
3131
* from `tree`. Non-public type members are currently left as they are.
3232
* This means that references to a private type will lead to typing failures
3333
* on the code when it is inlined. Less than ideal, but hard to do better (see below).
@@ -190,7 +190,8 @@ object Inliner {
190190
val inlineCtx = ctx
191191
sym.updateAnnotation(LazyBodyAnnotation { _ =>
192192
implicit val ctx = inlineCtx
193-
ctx.withNoError(treeExpr(ctx))(makeInlineable)
193+
val body = treeExpr(ctx)
194+
if (ctx.reporter.hasErrors) body else makeInlineable(body)
194195
})
195196
}
196197
}
@@ -233,8 +234,10 @@ object Inliner {
233234
* and body that replace it.
234235
*/
235236
def inlineCall(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
236-
if (enclosingInlineds.length < ctx.settings.xmaxInlines.value)
237-
new Inliner(tree, bodyToInline(tree.symbol)).inlined(pt)
237+
if (enclosingInlineds.length < ctx.settings.xmaxInlines.value) {
238+
val body = bodyToInline(tree.symbol) // can typecheck the tree and thereby produce errors
239+
if (ctx.reporter.hasErrors) tree else new Inliner(tree, body).inlined(pt)
240+
}
238241
else errorTree(
239242
tree,
240243
i"""|Maximal number of successive inlines (${ctx.settings.xmaxInlines.value}) exceeded,

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
19641964
if (Inliner.hasBodyToInline(tree.symbol) &&
19651965
!ctx.owner.ownersIterator.exists(_.isInlineMethod) &&
19661966
!ctx.settings.YnoInline.value &&
1967-
!ctx.isAfterTyper)
1967+
!ctx.isAfterTyper &&
1968+
!ctx.reporter.hasErrors)
19681969
adapt(Inliner.inlineCall(tree, pt), pt)
19691970
else if (ctx.typeComparer.GADTused && pt.isValueType)
19701971
// Insert an explicit cast, so that -Ycheck in later phases succeeds.

tests/neg/i2006.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
3+
inline def foo(f: ImplicitFunction1[Int, Int]): AnyRef = f // error
4+
inline def bar(f: ImplicitFunction1[Int, Int]) = f // error
5+
6+
def main(args: Array[String]) = {
7+
foo(implicit thisTransaction => 43)
8+
bar(implicit thisTransaction => 44)
9+
}
10+
}

0 commit comments

Comments
 (0)