scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.84k stars 1.05k forks source link

Unable to extend protected nested class #21631

Open WojciechMazur opened 2 weeks ago

WojciechMazur commented 2 weeks ago

Based on Open CB failure in playframework/playframework - builds logs Extracted from similar issue: https://github.com/scala/scala3/issues/21599#issuecomment-2367548970 - original issue was based on access to private members and was concluded to be working as expected. This case however involves protected members - these AFAIK are considered as public at the bytecode level

Compiler version

3.6.0-RC1-bin-20240915-ad8c21a-NIGHTLY Bisect points to e7d479f80a29c6ae21d0755047e563963b87ac82 / #21362

If you're not sure what version you're using, run print scalaVersion from sbt (if you're running scalac manually, use scalac -version instead).

Minimized code

When creating a fix for playframework I've seen one interesting issue in their Netty integration

// AbstractChannel.java
public abstract class AbstractChannel {
    protected AbstractChannel() {}
    protected abstract AbstractUnsafe newUnsafe();
    protected abstract class AbstractUnsafe {
        public abstract void connect();
    }
}
// test.scala
class Channel extends AbstractChannel() {
  override def newUnsafe(): AbstractChannel#AbstractUnsafe = new AbstractUnsafe {
    override def connect(): Unit = ???
  }
}

Output

Fails with

[error] ./test.scala:2:66
[error] Unable to emit reference to constructor AbstractUnsafe in class AbstractUnsafe, class AbstractUnsafe is not accessible in anonymous class AbstractChannel.this.AbstractUnsafe {...}
[error]   override def newUnsafe(): AbstractChannel#AbstractUnsafe = new AbstractUnsafe {
[error]                                                                  ^^^^^^^^^^^^^^
Error compiling project (Scala 3.6.0-RC1-bin-20240922-22ed2fb-NIGHTLY, JVM (17))

Expectation

Should compile

Gedochao commented 2 weeks ago

cc @eed3si9n @hamzaremmal @dwijnand

dwijnand commented 2 weeks ago

From the comment

It compiles with --server=false only because it's not actually being compiled - it's not emitting bytecode for Java class

It sounds like at the very least there's a difference between java source parsing and java bytecode parsing.

Gedochao commented 2 weeks ago

It sounds like at the very least there's a difference between java source parsing and java bytecode parsing.

Scala CLI currently doesn't support Scala/Java mixed compilation with --server=false. Refer to:

So it's unrelated, I think.