Skip to content

Commit 76769ab

Browse files
authored
Merge pull request #59 from scala/backport-lts-3.3-21411
Backport "Fix enclosingClass from returning refinement classes" to 3.3 LTS
2 parents a907d2e + 0d1f4a5 commit 76769ab

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1154,10 +1154,10 @@ object SymDenotations {
11541154
final def enclosingClass(using Context): Symbol = {
11551155
def enclClass(sym: Symbol, skip: Boolean): Symbol = {
11561156
def newSkip = sym.is(JavaStaticTerm)
1157-
if (!sym.exists)
1157+
if !sym.exists then
11581158
NoSymbol
1159-
else if (sym.isClass)
1160-
if (skip) enclClass(sym.owner, newSkip) else sym
1159+
else if sym.isClass then
1160+
if skip || sym.isRefinementClass then enclClass(sym.owner, newSkip) else sym
11611161
else
11621162
enclClass(sym.owner, skip || newSkip)
11631163
}

compiler/src/dotty/tools/dotc/transform/ProtectedAccessors.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ object ProtectedAccessors {
4444
/** Do we need a protected accessor for accessing sym from the current context's owner? */
4545
def needsAccessor(sym: Symbol)(using Context): Boolean =
4646
needsAccessorIfNotInSubclass(sym) &&
47-
!ctx.owner.enclosingClass.derivesFrom(sym.owner)
47+
!needsAccessorIsSubclass(sym)
48+
49+
def needsAccessorIsSubclass(sym: Symbol)(using Context): Boolean =
50+
ctx.owner.enclosingClass.derivesFrom(sym.owner)
4851
}
4952

5053
class ProtectedAccessors extends MiniPhase {

compiler/src/dotty/tools/dotc/transform/SuperAccessors.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class SuperAccessors(thisPhase: DenotTransformer) {
195195
* Otherwise, we need to go through an accessor,
196196
* which the implementing class will provide an implementation for.
197197
*/
198-
if ctx.owner.enclosingClass.derivesFrom(sym.owner) then
198+
if ProtectedAccessors.needsAccessorIsSubclass(sym) then
199199
if sym.is(JavaDefined) then
200200
report.error(em"${ctx.owner} accesses protected $sym inside a concrete trait method: use super.${sel.name} instead", sel.srcPos)
201201
sel

tests/pos/i20952.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package object packer: // the super class needs to be in a different package
2+
class SuperClass():
3+
protected val problem: Any = ??? // needs to be protected
4+
5+
class SuperClass():
6+
protected val problem: Any = ??? // needs to be protected
7+
8+
// type Target = SuperClass // passes
9+
type Target = packer.SuperClass // error
10+
11+
trait Child extends Target:
12+
13+
val aliased: problem.type = problem
14+
type Alias = problem.type
15+
16+
val newProblem: Any {val prog: problem.type} = ??? // error
17+
val newProblem2: Any {val prog: Alias} = ??? // passes
18+
val newProblem3: Any {val prog: aliased.type} = ??? // passes
19+
20+
class ChildImpl extends Target with Child // concrete implementation is needed

0 commit comments

Comments
 (0)