Skip to content

Incorrect access check error extending Java class without specifying type param #12379

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

Closed
lrytz opened this issue Apr 22, 2021 · 4 comments
Closed

Comments

@lrytz
Copy link
Member

lrytz commented Apr 22, 2021

package p;
public class A<T> {
  protected A() {}
}
class C extends p.A

gives

Test.scala:1: error: constructor A in class A cannot be accessed in package <empty> from package <empty>
 Access to protected constructor A not permitted because
 enclosing package <empty> is not a subclass of
 class A in package p where target is defined
class C extends p.A
                  ^
1 error

Note that Scala 3 doesn't allow missing type parameters in parent types

scala> class Ba[T]; class Ex extends Ba // allowed in Scala 2
1 |class Ba[T]; class Ex extends Ba
  |                              ^^
  |                              Missing type parameter for Ba
@lrytz lrytz added this to the Backlog milestone Apr 22, 2021
@som-snytt
Copy link

Oh man this was hugely annoying! I wanted to spend 5 minutes trying something out in Scala, and instead it was [more than that] to figure out this message.

The comment implies "it doesn't happen in dotty" but of course it's not labeled fixed in dotty because:

15 |    val walker = new SimpleFileVisitor {
   |                     ^^^^^^^^^^^^^^^^^
   |constructor SimpleFileVisitor cannot be accessed as a member of java.nio.file.SimpleFileVisitor from class $anon.
   | Access to protected constructor SimpleFileVisitor not permitted because enclosing package object unzipper$package
   | is not a subclass of class SimpleFileVisitor in package java.nio.file where target is defined

It's even more annoying that I saw the recent duplicate and I'd already had morning coffee albeit after poor sleep and it still didn't pop out at me.

@unkarjedy
Copy link

Faced the same issue.
Specifying explicit type parameter helped as a workaround.

NOTE 1: the issue is not reproduced with public constructor
NOTE 2: the issue is not reproduced if the classes are located in the same package

@unkarjedy
Copy link

unkarjedy commented Sep 4, 2023

Related #10122, #6617?

@som-snytt
Copy link

Dotty has parity with Scala 2 as of 3.2.2.

As noted, access check fails during inference when different packages, protected ctor, for either Java or Scala parent. The problem must lie in the the funny context you get, which includes the class params but is not enclosed by the class.

Closing as duplicate.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
@SethTisue SethTisue removed this from the Backlog milestone Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants