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

inlined given resulting in runtime error java.lang.NoSuchFieldError: $outer #13334

Closed
soronpo opened this issue Aug 19, 2021 · 5 comments · Fixed by #17505
Closed

inlined given resulting in runtime error java.lang.NoSuchFieldError: $outer #13334

soronpo opened this issue Aug 19, 2021 · 5 comments · Fixed by #17505

Comments

@soronpo
Copy link
Contributor

soronpo commented Aug 19, 2021

Compiler version

v3.0.2-RC1 (also tried on master branch)

Minimized code

trait DFC
given DFC = new DFC {}

trait TC
object TC:
  def foo()(using DFC): Unit = {}

  inline given (using DFC): TC = new TC:
    foo()

class Foo(using DFC):
  summon[TC]

val top = new Foo

Output

See scastie: https://scastie.scala-lang.org/yZG6miffRf6f4O6od68i2w

java.lang.NoSuchFieldError: $outer
	at Playground$$anon$2.<init>(main.scala:11)
	at Playground$Foo.<init>(main.scala:11)
	at Playground$.<clinit>(main.scala:16)
	at Main$.<clinit>(main.scala:20)
	at Main.main(main.scala)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sbt.Run.invokeMain(Run.scala:133)
	at sbt.Run.execute$1(Run.scala:82)
	at sbt.Run.$anonfun$runWithLoader$5(Run.scala:110)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:17)
	at sbt.TrapExit$App.run(TrapExit.scala:258)
	at java.lang.Thread.run(Thread.java:748)

Expectation

No runtime error.

@dwijnand
Copy link
Member

dwijnand commented Aug 19, 2021

Compiling with -Ycheck:all shows:

checking tests/run/i13334.scala after phase inlining
exception while typing Foo.this.x$1 of class class dotty.tools.dotc.ast.Trees$Ident # -1
exception while typing Foo.this.x$1 of class class dotty.tools.dotc.ast.Trees$Inlined # -1
exception while typing TC.foo()(Foo.this.x$1) of class class dotty.tools.dotc.ast.Trees$Apply # -1
...
Exception in thread "main" java.lang.AssertionError: assertion failed: bad type (Foo.this.x$1 : DFC) for Foo.this.x$1 # -1
	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
	at dotty.tools.dotc.transform.TreeChecker$Checker.typedIdent(TreeChecker.scala:363)
	at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2664)
	at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
	at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:118)

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Aug 26, 2021

Minimized

class Bar

inline def bar(i: Int): Bar =
  new Bar:
    def j = i

class Foo(x: Int):
  def foo = bar(x)

Workaround

+ val i2 = i
  new Bar:
-    def j = i
+    def j = i2

@odersky
Copy link
Contributor

odersky commented Dec 10, 2021

It seems inlining local classes has lots of problems. Can we outlaw it completely?

@nicolasstucki
Copy link
Contributor

This is the foundation we need to specialize by inclining. Probably many libraries use it already.

odersky added a commit to dotty-staging/dotty that referenced this issue Feb 25, 2022
Fixes a part of scala#13334 which now works for `transparent inline`.
odersky added a commit to dotty-staging/dotty that referenced this issue Feb 25, 2022
Fixes a part of scala#13334 which now works for `transparent inline`.
@odersky
Copy link
Contributor

odersky commented Feb 25, 2022

After #14568 the "bad type problem" is fixed. But we now get a new problem for the original code from @soronpo:

*** error while checking i13334a.scala after phase inlining ***
exception occurred while compiling i13334a.scala
java.lang.AssertionError: assertion failed: wrong source set for Baz.this.x$1 # -1 of class dotty.tools.dotc.ast.Trees$Select, set to library/src/scala/runtime/stdLibPatches/Predef.scala but context had i13334a.scala
 private[this] <paramaccessor> <synthetic> given <touched> while compiling i13334a.scala
Exception in thread "main" java.lang.AssertionError: assertion failed: wrong source set for Baz.this.x$1 # -1 of class dotty.tools.dotc.ast.Trees$Select, set to library/src/scala/runtime/stdLibPatches/Predef.scala but context had i13334a.scala

I verified that the same error occurs even without #14568 if we disable the "bad type problem" check.

@odersky odersky assigned nicolasstucki and unassigned odersky Feb 25, 2022
ckipp01 added a commit to ckipp01/dotty that referenced this issue May 15, 2023
nicolasstucki added a commit that referenced this issue May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants