Skip to content

Access to protected fields of a self-type is inconsistent #18245

@coreywoodfield

Description

@coreywoodfield

Compiler version

3.3.0

Minimized code

Test.scala

package test.base

class Test {
  protected def foo(): Unit = ()
}

SamePackageSubclass.scala

package test.base

class SamePackageSubclass { this: Test =>
  foo()
}

SubPackageSubclass.scala

package test.base.sub

import test.base.Test

class SubPackageSubclass { this: Test =>
  foo()
}

DiffPackageSubclass.scala

package test.other

import test.base.Test

class DiffPackageSubclass { this: Test =>
  foo()
}

Output

-- Error: DiffPackageSubclass.scala:6:2 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6 |  foo()
  |  ^^^
  |  illegal access to protected method foo in class Test from class DiffPackageSub
1 error found

Expectation

I would expect all three classes to be able to access foo or all three classes to not be able to access foo. Instead, the classes that are within/under the original classes package are able to access foo and the one outside is not.

In scala 2, having a self type gave you access to the protected members of that self type. In scala 3, having a self type only gives you access to protected members of that self type if you're in the same package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions