Skip to content

Commit

Permalink
Merge pull request #534 from adpi2/fix-427
Browse files Browse the repository at this point in the history
Fix evaluation on extractor
  • Loading branch information
adpi2 committed Jul 28, 2023
2 parents 44a5f64 + 6d2ceea commit f6fe0c3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,21 @@ class InsertExpression(using exprCtx: ExpressionContext) extends Phase:
expressionInserted = false
cpy.PackageDef(transformed)(
transformed.pid,
transformed.stats ++ expressionClass.map(
_.withSpan(tree.span)
)
transformed.stats ++ expressionClass.map(_.withSpan(tree.span))
)
else transformed
case tree @ DefDef(name, paramss, tpt, rhs) if rhs != EmptyTree && isOnBreakpoint(tree) =>
cpy.DefDef(tree)(
name,
paramss,
tpt,
mkExprBlock(expression, tree.rhs)
)
cpy.DefDef(tree)(name, paramss, tpt, mkExprBlock(expression, tree.rhs))
case tree @ Match(selector, caseDefs) if isOnBreakpoint(tree) || caseDefs.exists(isOnBreakpoint) =>
// the expression is on the match or a case of the match
// if it is on the case of the match the program could pause on the pattern, the guard or the body
// we assume it pauses on the pattern because that is the first instruction
// in that case we cannot compile the expression val in the pattern, but we can compile it in the selector
cpy.Match(tree)(mkExprBlock(expression, selector), caseDefs)
case tree @ ValDef(name, tpt, rhs) if isOnBreakpoint(tree) =>
case tree @ ValDef(name, tpt, _) if isOnBreakpoint(tree) =>
cpy.ValDef(tree)(name, tpt, mkExprBlock(expression, tree.rhs))
case tree @ PatDef(mods, pat, tpt, rhs) if isOnBreakpoint(tree) =>
PatDef(mods, pat, tpt, mkExprBlock(expression, rhs))
case tree: (Ident | Select | GenericApply | Literal | This | New | InterpolatedString | OpTree | Tuple |
Assign | Block) if isOnBreakpoint(tree) =>
mkExprBlock(expression, tree)
Expand Down Expand Up @@ -207,9 +202,7 @@ class InsertExpression(using exprCtx: ExpressionContext) extends Phase:
if tree.span.exists then tree.sourcePos.startLine + 1 else -1
startLine == exprCtx.breakpointLine

private def mkExprBlock(expr: Tree, tree: Tree)(using
Context
): Tree =
private def mkExprBlock(expr: Tree, tree: Tree)(using Context): Tree =
if expressionInserted then
warnOrError("expression already inserted", tree.srcPos)
tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@ class ExpressionCompilerDebug extends munit.FunSuite:

override def munitTimeout: Duration = 1.hour

test("debug expression compiler".ignore) {
val javaSource =
"""|package example;
|
|class A {
| protected static String x = "x";
| protected static String m() {
| return "m";
| }
|}
|""".stripMargin
val javaModule = TestingDebuggee.fromJavaSource(javaSource, "example.A", scalaVersion)
val scalaSource =
test("tuple extractor") {
val source =
"""|package example
|
|object Main extends A {
|object Main {
| def main(args: Array[String]): Unit = {
| println("Hello, World!")
| val tuple = (1, 2)
| val (x, y) = tuple
| println("ok")
| }
|}
|""".stripMargin
implicit val debuggee: TestingDebuggee =
TestingDebuggee.mainClass(scalaSource, "example.Main", scalaVersion, Seq.empty, Seq(javaModule.mainModule))
evaluate(5, "A.x")
evaluate(5, "A.m")
implicit val debuggee: TestingDebuggee = TestingDebuggee.mainClass(source, "example.Main", scalaVersion)
evaluate(5, "tuple._1", localVariables = Set("tuple"))
}

def evaluate(line: Int, expression: String, localVariables: Set[String] = Set.empty)(using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,30 @@ abstract class ScalaEvaluationTests(scalaVersion: ScalaVersion) extends DebugTes
Evaluation.successOrIgnore("A.m()", "m", isScala2)
)
}

test("tuple extractor") {
val source =
"""|package example
|object Main {
| def main(args: Array[String]): Unit = {
| val tuple = (1, 2)
| val (x, y) = tuple
| println("ok")
| }
|}
|""".stripMargin
implicit val debuggee: TestingDebuggee = TestingDebuggee.mainClass(source, "example.Main", scalaVersion)
check(
Breakpoint(5),
Evaluation.success("tuple._1", 1),
Evaluation.success(
"""|val (x, y) = tuple
|y
|""".stripMargin,
2
)
)
}
}

abstract class Scala2EvaluationTests(val scalaVersion: ScalaVersion) extends ScalaEvaluationTests(scalaVersion) {
Expand Down

0 comments on commit f6fe0c3

Please sign in to comment.