Skip to content

Commit

Permalink
fix: outer class lookup and primitive type match error
Browse files Browse the repository at this point in the history
  • Loading branch information
iusildra committed May 22, 2023
1 parent 0087b7c commit 81774d1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,12 @@ class RuntimeValidator(frame: JdiFrame, logger: Logger) {
* @param expression
* @return a [[ValidationTree]] of the expression
*/
private def validateWithClass(expression: Stat): Validation[RuntimeTree] = {
val res = expression match {
private def validateWithClass(expression: Stat): Validation[RuntimeTree] =
expression match {
case value: Term.Name => validateName(value, thisTree).orElse(validateClass(value.value, thisTree.toOption))
case select: Term.Select => validateInnerSelect(select)
case _ => validate(expression)
}
println(s"validation of ${expression} WITH CLASS: ${res}")
res
}

/**
* Returns a ValidationTree of the [[Term.Select]] nested in another [[Term.Select]]. Provides access to [[ClassTree]], so it mustn't be used directly and must be wrapped in an [[EvaluationTree]]
Expand Down Expand Up @@ -310,6 +307,12 @@ class RuntimeValidator(frame: JdiFrame, logger: Logger) {
else Unrecoverable(s"Cannot access non-static class ${cls.name()}")
}
}
.orElse {
of match {
case None => Recoverable(s"Cannot find class $name")
case Some(value) => findOuter(value, frame).flatMap(outer => validateClass(name, Some(outer)))
}
}

private def validateName(
value: Term.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,49 @@ object RuntimeBinaryOp {

/* --------------------------- Numeric operations --------------------------- */
sealed trait NumericOp extends RuntimeBinaryOp {
override def typeCheck(lhs: Type, rhs: Type): PrimitiveType =
private def primitiveTypeCheck(lhs: Type, rhs: Type): Option[PrimitiveType] =
(lhs, rhs) match {
case (d: DoubleType, _) => d
case (_, d: DoubleType) => d
case (f: FloatType, _) => f
case (_, f: FloatType) => f
case (l: LongType, _) => l
case (_, l: LongType) => l
case (i: IntegerType, _) => i
case (_, i: IntegerType) => i
case (s: ShortType, _) => s
case (_, s: ShortType) => s
case (b: ByteType, _) => b
case (_, b: ByteType) => b
case (c: CharType, _) => c
case (_, c: CharType) => c
case (d: DoubleType, _) => Some(d)
case (_, d: DoubleType) => Some(d)
case (f: FloatType, _) => Some(f)
case (_, f: FloatType) => Some(f)
case (l: LongType, _) => Some(l)
case (_, l: LongType) => Some(l)
case (i: IntegerType, _) => Some(i)
case (_, i: IntegerType) => Some(i)
case (s: ShortType, _) => Some(s)
case (_, s: ShortType) => Some(s)
case (b: ByteType, _) => Some(b)
case (_, b: ByteType) => Some(b)
case (c: CharType, _) => Some(c)
case (_, c: CharType) => Some(c)
case _ => None
}

private def referenceTypeCheck(lhs: Type, rhs: Type): Option[Type] = {
(lhs.name(), rhs.name()) match {
case ("java.lang.Double", _) => Some(lhs)
case (_, "java.lang.Double") => Some(rhs)
case ("java.lang.Float", _) => Some(lhs)
case (_, "java.lang.Float") => Some(rhs)
case ("java.lang.Long", _) => Some(lhs)
case (_, "java.lang.Long") => Some(rhs)
case ("java.lang.Integer", _) => Some(lhs)
case (_, "java.lang.Integer") => Some(rhs)
case ("java.lang.Short", _) => Some(lhs)
case (_, "java.lang.Short") => Some(rhs)
case ("java.lang.Byte", _) => Some(lhs)
case (_, "java.lang.Byte") => Some(rhs)
case ("java.lang.Character", _) => Some(lhs)
case (_, "java.lang.Character") => Some(rhs)
case _ => None
}
}

override def typeCheck(lhs: Type, rhs: Type): Type =
primitiveTypeCheck(lhs, rhs).orElse(referenceTypeCheck(lhs, rhs)) match {
case Some(t) => t
case None => throw new IllegalArgumentException(s"Unexpected types $lhs and $rhs")
}

private def computeFractional[T <: AnyVal](x: T, y: T, clsLoader: JdiClassLoader)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object RuntimeEvaluatorPrimitiveEnvironment {
|
|object Main {
| def main(args: Array[String]): Unit = {
| val name = "world"
| val name = "world"; new java.lang.Long(42L)
| println(name)
| val foo1 = Foo(1)
| val foo2 = Foo(2)
Expand All @@ -40,14 +40,14 @@ object RuntimeEvaluatorPrimitiveEnvironment {
class ScalaRuntimeEvaluatorPrimitiveOperationTests extends RuntimeEvaluatorPrimitiveOperationTests(ScalaVersion.`3.1+`)

abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVersion) extends DebugTestSuite {
lazy val localVar =
lazy val primitiveOps =
TestingDebuggee.mainClass(RuntimeEvaluatorPrimitiveEnvironment.primitiveSource, "example.Main", scalaVersion)

protected override def defaultConfig: DebugConfig =
super.defaultConfig.copy(evaluationMode = DebugConfig.RuntimeEvaluationOnly)

test("Should compute primitive method calls on doubles") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -71,7 +71,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on floats") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -95,7 +95,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on longs") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -118,7 +118,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on ints") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -141,7 +141,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on shorts") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -164,7 +164,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on bytes") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -177,7 +177,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on chars") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -199,7 +199,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute primitive method calls on booleans") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -222,7 +222,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should take accounts of order of priorities") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -244,7 +244,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute mixed operands type primitive numeric operations") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand Down Expand Up @@ -273,7 +273,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should compute a mix of literal primitive values and computed primitive values") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -290,7 +290,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should unbox types supporting primitive operations") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -313,7 +313,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should decide whether 2 instances are equals or not") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(11),
DebugStepAssert.inParallel(
Expand All @@ -338,7 +338,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should refuse bad types primitive operations") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand All @@ -354,7 +354,7 @@ abstract class RuntimeEvaluatorPrimitiveOperationTests(val scalaVersion: ScalaVe
}

test("Should accept unary operators on primitive types") {
implicit val debuggee = localVar
implicit val debuggee = primitiveOps
check(
Breakpoint(6),
DebugStepAssert.inParallel(
Expand Down

0 comments on commit 81774d1

Please sign in to comment.