Skip to content
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

Use Attachment to mark the expression #147

Merged
merged 1 commit into from
Oct 25, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,25 @@ abstract class ExpressionEvaluatorSuite(scalaVersion: ScalaVersion)
)
}

"should evaluate expression with breakpoint on a field's assignment" - {
val source =
"""class Foo {
| val a = 1
| val b = 2
| def bar() = a + b
|}
|
|object EvaluateTest {
| def main(args: Array[String]): Unit = {
| new Foo()
| }
|}
|""".stripMargin
assertEvaluationInMainClass(source, "EvaluateTest", 3, "a + 2")(
_.exists(_.toInt == 3)
)
}

"should evaluate expression with breakpoint on method definition" - {
val source =
"""class Foo {
Expand Down Expand Up @@ -705,14 +724,14 @@ abstract class ExpressionEvaluatorSuite(scalaVersion: ScalaVersion)
ExpressionEvaluation(
5,
"1 + 1",
_.exists(_ == "\"values are not the same\""),
_.exists(_.toInt == 2),
stoppageNo = 0
),
// evaluating twice because the program stops twice at the same breakpoint...
ExpressionEvaluation(
5,
"1 + 1",
_.exists(_ == "\"values are not the same\""),
_.exists(_.toInt == 2),
stoppageNo = 1
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private[nsc] class EvalGlobal(
)
}

case object ExpressionAttachment extends PlainAttachment

class InsertExpression extends Transform with TypingTransformers {
override val global: EvalGlobal.this.type = EvalGlobal.this
override val phaseName: String = "insertexpression"
Expand Down Expand Up @@ -176,8 +178,7 @@ private[nsc] class EvalGlobal(

override def transform(tree: Tree): Tree = tree match {
case tree: DefDef if tree.pos.line == line =>
expressionInserted = true
atPos(tree.pos)(
insertAt(tree.pos)(
filterOutTailRec(
treeCopy.DefDef(
tree,
Expand All @@ -193,8 +194,7 @@ private[nsc] class EvalGlobal(
case tree: DefDef =>
super.transform(filterOutTailRec(tree))
case vd: ValDef if vd.pos.line == line =>
expressionInserted = true
atPos(vd.pos)(
insertAt(vd.pos)(
treeCopy.ValDef(
vd,
vd.mods,
Expand All @@ -203,10 +203,8 @@ private[nsc] class EvalGlobal(
mkExprBlock(vd.rhs)
)
)

case tree if tree.pos.line == line =>
expressionInserted = true
atPos(tree.pos)(mkExprBlock(tree))
insertAt(tree.pos)(mkExprBlock(tree))
case tree: PackageDef =>
val transformed = super.transform(tree).asInstanceOf[PackageDef]
if (expressionInserted) {
Expand All @@ -225,11 +223,25 @@ private[nsc] class EvalGlobal(
super.transform(tree)
}

private def mkExprBlock(tree: Tree): Tree =
if (tree.isDef)
Block(List(parsedExpression, tree), Literal(Constant(())))
else
Block(List(parsedExpression), tree)
private def insertAt(pos: Position)(tree: Tree): Tree = {
expressionInserted = true
atPos(pos)(tree)
}

private def mkExprBlock(tree: Tree): Tree = {
val block =
if (tree.isDef)
Block(List(parsedExpression, tree), Literal(Constant(())))
else
Block(List(parsedExpression), tree)
addExpressionAttachment(block)
}

// `ExpressionAttachment` allows to find the inserted expression later on
private def addExpressionAttachment(tree: Tree): Tree = {
val attachments = tree.attachments.update(ExpressionAttachment)
tree.setAttachments(attachments)
}
}
}

Expand Down Expand Up @@ -282,21 +294,26 @@ private[nsc] class EvalGlobal(
// Don't extract expression from the Expression class
case tree: ClassDef if tree.name.decode == expressionClassName =>
// ignore
case tree: DefDef if !expressionExtracted && tree.pos.line == line =>
case tree: DefDef if shouldExtract(tree) =>
expressionOwners = ownerChain(tree)
extractedExpression = extractExpression(tree.rhs)
// default arguments will have an additional method generated, which we need to skip
case tree: ValDef if tree.rhs.isEmpty =>
case tree: ValDef if !expressionExtracted && tree.pos.line == line =>
case tree: ValDef if shouldExtract(tree) =>
expressionOwners = ownerChain(tree)
extractedExpression = extractExpression(tree.rhs)
case _ if !expressionExtracted && tree.pos.line == line =>
case _ if shouldExtract(tree) =>
expressionOwners = ownerChain(tree)
extractedExpression = extractExpression(tree)
case _ =>
super.traverse(tree)
}

private def shouldExtract(tree: Tree): Boolean =
!expressionExtracted && tree.attachments
.get[ExpressionAttachment.type]
.isDefined

private def ownerChain(tree: Tree): List[Symbol] =
if (tree.symbol == null)
currentOwner.ownerChain
Expand Down