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

Failed evaluation in dotty #425

Closed
adpi2 opened this issue May 16, 2023 · 6 comments · Fixed by #511
Closed

Failed evaluation in dotty #425

adpi2 opened this issue May 16, 2023 · 6 comments · Fixed by #511
Labels
bug Something isn't working expression compiler
Milestone

Comments

@adpi2
Copy link
Member

adpi2 commented May 16, 2023

In dotty dotty.tools.dotc.rewrites.Rewrites.scala, evaluation of pbuf(2).span fails with:

IllegalArgumentException@5626 "java.lang.IllegalArgumentException: object is not an instance of declaring class"
@adpi2 adpi2 added bug Something isn't working runtime evaluation labels May 16, 2023
@adpi2 adpi2 added this to the 3.2.x milestone Jul 5, 2023
@iusildra
Copy link
Contributor

iusildra commented Jul 6, 2023

Minimized code:

object Rewrites {
  private class Patch(val span: Span)
  
  def main(args: Array[String]): Unit = {
    val patch = Patch(Span(0))
    println("ok")
  }
}

class Span(start: Int) extends AnyVal

The problem seems to be with private classes having value classes as attributes

@iusildra
Copy link
Contributor

iusildra commented Jul 7, 2023

Hum, I'm trying to print the trees generated by the compiler, but even when passing -Xprint:all in scalacOptions to DebugTools#loadExpressionCompiler, the trees are not printed...
So I compile the minimized code in a separate file, but could not find any difference between the private class Patch and class Patch versions, each with and without a val expr$19744 = patch.span after the val patch = ..., apart from a different ordering, but I don't thing it matters

@adpi2
Copy link
Member Author

adpi2 commented Jul 7, 2023

I think the IllegalArgumentException comes from one of the method in

s"""|class ${exprCtx.expressionClassName}(names: Array[String], values: Array[Any]):
| import java.lang.reflect.InvocationTargetException
| val classLoader = getClass.getClassLoader.nn
|
| def evaluate(): Any =
| ()
|
| def getLocalValue(name: String): Any =
| val idx = names.indexOf(name)
| if idx == -1 then throw new NoSuchElementException(name)
| else values(idx)
|
| def setLocalValue(name: String, value: Any): Any =
| val idx = names.indexOf(name)
| if idx == -1 then throw new NoSuchElementException(name)
| else values(idx) = value
|
| def callMethod(obj: Any, className: String, methodName: String, paramTypesNames: Array[String], returnTypeName: String, args: Array[Object]): Any =
| val clazz = classLoader.loadClass(className).nn
| val method = clazz.getDeclaredMethods.nn
| .map(_.nn)
| .find { m =>
| m.getName == methodName &&
| m.getReturnType.nn.getName == returnTypeName &&
| m.getParameterTypes.nn.map(_.nn.getName).toSeq == paramTypesNames.toSeq
| }
| .getOrElse(throw new NoSuchMethodException(methodName))
| method.setAccessible(true)
| unwrapException(method.invoke(obj, args*))
|
| def callConstructor(className: String, paramTypesNames: Array[String], args: Array[Object]): Any =
| val clazz = classLoader.loadClass(className).nn
| val constructor = clazz.getConstructors.nn
| .map(_.nn)
| .find { c => c.getParameterTypes.nn.map(_.nn.getName).toSeq == paramTypesNames.toSeq }
| .getOrElse(throw new NoSuchMethodException(s"new $$className"))
| constructor.setAccessible(true)
| unwrapException(constructor.newInstance(args*))
|
| def getField(obj: Any, className: String, fieldName: String): Any =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField(fieldName).nn
| field.setAccessible(true)
| field.get(obj)
|
| def setField(obj: Any, className: String, fieldName: String, value: Any): Unit =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField(fieldName).nn
| field.setAccessible(true)
| field.set(obj, value)
|
| def getOuter(obj: Any, outerTypeName: String): Any =
| val clazz = obj.getClass
| val field = getSuperclassIterator(clazz)
| .flatMap(_.getDeclaredFields.nn.toSeq)
| .map(_.nn)
| .find { field => field.getName == "$$outer" && field.getType.nn.getName == outerTypeName }
| .getOrElse(throw new NoSuchFieldException("$$outer"))
| field.setAccessible(true)
| field.get(obj)
|
| def getStaticObject(className: String): Any =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField("MODULE$$").nn
| field.setAccessible(true)
| field.get(null)
|
| def getSuperclassIterator(clazz: Class[?] | Null): Iterator[Class[?]] =
| Iterator.iterate(clazz)(_.nn.getSuperclass).takeWhile(_ != null).map(_.nn)
|
| // a fake method that is used between the extract-expression and the resolve-reflect-eval phases,
| // which transforms them to calls of one of the methods defined above.
| def reflectEval(qualifier: Object, term: String, args: Array[Object]): Any = ???
|
| private def unwrapException(f: => Any): Any =
| try f
| catch
| case e: InvocationTargetException => throw e.getCause.nn
|
| extension [T] (x: T | Null)
| private def nn: T =
| assert(x != null)
| x.asInstanceOf[T]
|""".stripMargin

You should be able to get the stack trace of this exception by inspecting it in the debug console or by wrapping the debug console in a try catch. Then you can add println in the methods in

s"""|class ${exprCtx.expressionClassName}(names: Array[String], values: Array[Any]):
| import java.lang.reflect.InvocationTargetException
| val classLoader = getClass.getClassLoader.nn
|
| def evaluate(): Any =
| ()
|
| def getLocalValue(name: String): Any =
| val idx = names.indexOf(name)
| if idx == -1 then throw new NoSuchElementException(name)
| else values(idx)
|
| def setLocalValue(name: String, value: Any): Any =
| val idx = names.indexOf(name)
| if idx == -1 then throw new NoSuchElementException(name)
| else values(idx) = value
|
| def callMethod(obj: Any, className: String, methodName: String, paramTypesNames: Array[String], returnTypeName: String, args: Array[Object]): Any =
| val clazz = classLoader.loadClass(className).nn
| val method = clazz.getDeclaredMethods.nn
| .map(_.nn)
| .find { m =>
| m.getName == methodName &&
| m.getReturnType.nn.getName == returnTypeName &&
| m.getParameterTypes.nn.map(_.nn.getName).toSeq == paramTypesNames.toSeq
| }
| .getOrElse(throw new NoSuchMethodException(methodName))
| method.setAccessible(true)
| unwrapException(method.invoke(obj, args*))
|
| def callConstructor(className: String, paramTypesNames: Array[String], args: Array[Object]): Any =
| val clazz = classLoader.loadClass(className).nn
| val constructor = clazz.getConstructors.nn
| .map(_.nn)
| .find { c => c.getParameterTypes.nn.map(_.nn.getName).toSeq == paramTypesNames.toSeq }
| .getOrElse(throw new NoSuchMethodException(s"new $$className"))
| constructor.setAccessible(true)
| unwrapException(constructor.newInstance(args*))
|
| def getField(obj: Any, className: String, fieldName: String): Any =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField(fieldName).nn
| field.setAccessible(true)
| field.get(obj)
|
| def setField(obj: Any, className: String, fieldName: String, value: Any): Unit =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField(fieldName).nn
| field.setAccessible(true)
| field.set(obj, value)
|
| def getOuter(obj: Any, outerTypeName: String): Any =
| val clazz = obj.getClass
| val field = getSuperclassIterator(clazz)
| .flatMap(_.getDeclaredFields.nn.toSeq)
| .map(_.nn)
| .find { field => field.getName == "$$outer" && field.getType.nn.getName == outerTypeName }
| .getOrElse(throw new NoSuchFieldException("$$outer"))
| field.setAccessible(true)
| field.get(obj)
|
| def getStaticObject(className: String): Any =
| val clazz = classLoader.loadClass(className).nn
| val field = clazz.getDeclaredField("MODULE$$").nn
| field.setAccessible(true)
| field.get(null)
|
| def getSuperclassIterator(clazz: Class[?] | Null): Iterator[Class[?]] =
| Iterator.iterate(clazz)(_.nn.getSuperclass).takeWhile(_ != null).map(_.nn)
|
| // a fake method that is used between the extract-expression and the resolve-reflect-eval phases,
| // which transforms them to calls of one of the methods defined above.
| def reflectEval(qualifier: Object, term: String, args: Array[Object]): Any = ???
|
| private def unwrapException(f: => Any): Any =
| try f
| catch
| case e: InvocationTargetException => throw e.getCause.nn
|
| extension [T] (x: T | Null)
| private def nn: T =
| assert(x != null)
| x.asInstanceOf[T]
|""".stripMargin
to get some info.

@iusildra
Copy link
Contributor

Here's what I get when try-catching in the debug console 🙃

$ try { patch.span } catch { case e => e.getStackTrace() }
> 0
$ patch.span
> IllegalArgumentException@1232 "java.lang.IllegalArgumentException: object is not an instance of declaring class"
$ try { patch.span } catch { case e => println(e.getStackTrace()) }
> 0

@adpi2
Copy link
Member Author

adpi2 commented Jul 12, 2023

Huh that's weird.

Is 0 the expected span?

@iusildra
Copy link
Contributor

Yes

@adpi2 adpi2 linked a pull request Jul 14, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working expression compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants