refined icon indicating copy to clipboard operation
refined copied to clipboard

Creating new refinement type crashes with: exception during macro expansion

Open umbreak opened this issue 6 years ago • 5 comments

I created a simple project which reproduces that error.

When defining my own refined type and using it in the code it fails with the following error at compile time:

[error].../Const.scala:6:32: exception during macro expansion: 
[error] java.lang.ClassNotFoundException: com.test.refined.Refinements$
[error]         at scala.reflect.internal.util.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:64)
[error]         at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[error]         at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error]         at __wrapper$3$b819929bb64041e38491d57bb6d81ca3.__wrapper$3$b819929bb64041e38491d57bb6d81ca3$.wrapper(<no source file>:6)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.lang.reflect.Method.invoke(Method.java:498)
[error]         at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.$anonfun$compile$11(ToolBoxFactory.scala:279)
[error]         at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.eval(ToolBoxFactory.scala:448)
[error]         at scala.reflect.macros.contexts.Evals.eval(Evals.scala:20)
[error]         at scala.reflect.macros.contexts.Evals.eval$(Evals.scala:14)
[error]         at scala.reflect.macros.contexts.Context.eval(Context.scala:6)
[error]         at eu.timepit.refined.macros.MacroUtils.$anonfun$eval$1(MacroUtils.scala:24)
[error]         at scala.Option.getOrElse(Option.scala:121)
[error]         at eu.timepit.refined.macros.MacroUtils.tryN(MacroUtils.scala:28)
[error]         at eu.timepit.refined.macros.MacroUtils.tryN$(MacroUtils.scala:27)
[error]         at eu.timepit.refined.macros.RefineMacro.tryN(RefineMacro.scala:11)
[error]         at eu.timepit.refined.macros.MacroUtils.eval(MacroUtils.scala:24)
[error]         at eu.timepit.refined.macros.MacroUtils.eval$(MacroUtils.scala:17)
[error]         at eu.timepit.refined.macros.RefineMacro.eval(RefineMacro.scala:11)
[error]         at eu.timepit.refined.macros.RefineMacro.$anonfun$validateInstance$1(RefineMacro.scala:57)
[error]         at scala.Option.getOrElse(Option.scala:121)
[error]         at eu.timepit.refined.macros.RefineMacro.validateInstance(RefineMacro.scala:57)
[error]         at eu.timepit.refined.macros.RefineMacro.impl(RefineMacro.scala:27)
[error]   val someCurie = Curie("one", "http://example.com/a")

The error seems to be related to the fact that when Const object gets called, apparently Refinements is not compiled yet.

I could fix it by separating the custom refinements into another module, as shown in this branch.

However...that seems overkilling. Is there a better way to deal with this?

umbreak avatar Mar 02 '18 08:03 umbreak

I tried to move some files around but had no luck avoiding this exception. :-( Moving Refinements to another module seems to be safest fix.

Different objects in docs/macro_pitfalls.md work because they are compiled and evaluated in the REPL and there every line is compiled separately.

fthomas avatar Mar 03 '18 12:03 fthomas

Ok thanks for taking a look at it.

Then I guess it would be useful for anyone else to add this on the macro_pitfalls documentation.

umbreak avatar Mar 03 '18 12:03 umbreak

Yes, that would be helpful.

fthomas avatar Mar 03 '18 22:03 fthomas

Documentation has been updated in #451.

fthomas avatar Mar 09 '18 14:03 fthomas

In some cases, even that fix I documented does not work. In the same scala project I was testing (branch fix), I just updated the Akka HTTP version on the build.sbt and afterwards it would not compile.

I wonder what could be the cause of this and how I could work around it or if it can be solved. I'd like to help but I don't know much about the marcos...

umbreak avatar Mar 20 '18 12:03 umbreak