Skip to content

Fix #8892: Fix position adjustments in Inliner #8912

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
May 9, 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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/ast/Positioned.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
def cloneIn(src: SourceFile): this.type = {
val newpd: this.type = clone.asInstanceOf[this.type]
newpd.uniqueId = src.nextId
// assert(newpd.uniqueId != 2208, s"source = $this, ${this.uniqueId}, ${this.span}")
newpd
}

Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Names._, StdNames._, NameOps._, Symbols._
import typer.ConstFold
import reporting.trace
import dotty.tools.dotc.transform.SymUtils._
import Decorators._

import scala.annotation.tailrec

Expand Down Expand Up @@ -836,6 +837,12 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
}
}

def assertAllPositioned(tree: Tree)(using Context): Unit =
tree.foreachSubTree {
case t: WithoutTypeOrPos[_] =>
case t => assert(t.span.exists, i"$t")
}

/** Extractors for quotes */
object Quoted {
/** Extracts the content of a quoted tree.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ object Trees {
def Apply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): Apply = tree match {
case tree: Apply if (fun eq tree.fun) && (args eq tree.args) => tree
case _ => finalize(tree, untpd.Apply(fun, args)(sourceFile(tree)))
//.ensuring(res => res.uniqueId != 2213, s"source = $tree, ${tree.uniqueId}, ${tree.span}")
}
def TypeApply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = tree match {
case tree: TypeApply if (fun eq tree.fun) && (args eq tree.args) => tree
Expand All @@ -1060,7 +1061,6 @@ object Trees {
def Typed(tree: Tree)(expr: Tree, tpt: Tree)(implicit ctx: Context): Typed = tree match {
case tree: Typed if (expr eq tree.expr) && (tpt eq tree.tpt) => tree
case tree => finalize(tree, untpd.Typed(expr, tpt)(sourceFile(tree)))
//.ensuring(res => res.uniqueId != 1471, s"source = $tree, ${tree.uniqueId}")
}
def NamedArg(tree: Tree)(name: Name, arg: Tree)(implicit ctx: Context): NamedArg = tree match {
case tree: NamedArg if (name == tree.name) && (arg eq tree.arg) => tree
Expand Down
17 changes: 10 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ object Inliner {
* inlined call `call` to the position of `call`. This transform is necessary
* when lifting bindings from the expansion to the outside of the call.
*/
def liftFromInlined(call: Tree) = new TreeMap {
def liftFromInlined(call: Tree) = new TreeMap:
override def transform(t: Tree)(using Context) =
t match {
case Inlined(t, Nil, expr) if t.isEmpty => expr
case _ if t.isEmpty => t
case _ => super.transform(t.withSpan(call.span))
}
}
if call.span.exists then
t match
case Inlined(t, Nil, expr) if t.isEmpty => expr
case _ if t.isEmpty => t
case _ => super.transform(t.withSpan(call.span))
else t
end liftFromInlined

val bindings = new mutable.ListBuffer[Tree]

Expand All @@ -108,6 +109,7 @@ object Inliner {
tree
}

// assertAllPositioned(tree) // debug
val tree1 = liftBindings(tree, identity)
if (bindings.nonEmpty)
cpy.Block(tree)(bindings.toList, inlineCall(tree1))
Expand Down Expand Up @@ -590,6 +592,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
* itself, it has to be marked as an inlined argument.
*/
def integrate(tree: Tree, originalOwner: Symbol)(using Context): Tree =
// assertAllPositioned(tree) // debug
tree.changeOwner(originalOwner, ctx.owner)

def tryConstValue: Tree =
Expand Down
28 changes: 28 additions & 0 deletions tests/pos/i8892.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
trait Reporter:
def report(m: String): Unit

class Dummy extends Reporter:
def report(m: String) = ()

object ABug {
sealed trait Nat {
inline def ++ : Succ[this.type] = Succ(this)

transparent inline def +(inline that: Nat): Nat =
inline this match {
case Zero => that
case Succ(p) => p + that.++
}
}

case object Zero extends Nat
case class Succ[N <: Nat](p: N) extends Nat

transparent inline def toIntg(inline n: Nat): Int =
inline n match {
case Zero => 0
case Succ(p) => toIntg(p) + 1
}

val j31 = toIntg(Zero.++.++.++ + Zero.++)
}