Skip to content

Fix #9844: Exclude private members of wrong classes from findMember #9847

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

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,22 @@ object Types {
go(tycon.resType).mapInfo(info =>
tycon.derivedLambdaAbstraction(tycon.paramNames, tycon.paramInfos, info).appliedTo(tp.args))

def goThis(tp: ThisType) = {
val d = go(tp.underlying)
if (d.exists) d
def goThis(tp: ThisType) =
val underlying = tp.underlying
val d = go(underlying)
if d.exists then
if underlying.isInstanceOf[AndType] then
// The underlying type of `this` is specified in a self type clause.
// In this case we need to exclude all private members from `d` which are
// not defined in the class of the `this` type. We could do this test
// always, but the restriction to test only if `underlying` is an AndType
// is made to save execution time in the common case. See i9844.scala for test cases.
def qualifies(sd: SingleDenotation) =
!sd.symbol.is(Private) || sd.symbol.owner == tp.cls
d match
case d: SingleDenotation => if qualifies(d) then d else NoDenotation
case d => d.filterWithPredicate(qualifies)
else d
else
// There is a special case to handle:
// trait Super { this: Sub => private class Inner {} println(this.Inner) }
Expand All @@ -716,7 +729,6 @@ object Types {
// As an example of this in the wild, see
// loadClassWithPrivateInnerAndSubSelf in ShowClassTests
go(tp.cls.typeRef) orElse d
}

def goParam(tp: TypeParamRef) = {
val next = tp.underlying
Expand Down
41 changes: 41 additions & 0 deletions tests/pos/i9844.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
object test1:
trait Foo[A]

trait Baz[A] {
trait Bar {
this: Foo[A] =>
def bar(a: A): Unit
}
}

object test2:

trait Foo:
private var f = "abc"

trait Baz {
trait Bam:
val f = 0
trait Bar extends Bam {
this: Foo =>
val g = f
val g1: Int = g
}
}

object test3:
object DetSkipOctree {
sealed trait Leaf [PL]
sealed trait Branch[PL]
}
trait DetSkipOctree[PL]

class Impl[PL] extends DetSkipOctree[PL] {
final type Leaf = DetSkipOctree.Leaf[PL]

protected trait LeftBranchImpl {
this: DetSkipOctree.Branch[PL] =>

def demoteLeaf(point: PL, leaf: Leaf): Unit = ???
}
}