Skip to content

Commit

Permalink
Account for access boundaries as part of accessibility check
Browse files Browse the repository at this point in the history
Add a test that demontrates the original reported bug.
  • Loading branch information
masonedmison committed Sep 11, 2024
1 parent 1785fe5 commit 9b586a8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ final class AutoImportsProvider(
val isSeen = mutable.Set.empty[String]
val symbols = List.newBuilder[Symbol]

def isAccessible(sym: Symbol) = {
def matches(_this: Symbol, that: Symbol): Boolean =
_this.fullName == that.fullName
lazy val symAccessBoundary = sym.privateWithin
@scala.annotation.tailrec
def withinAccessBoundary(_sym: Symbol): Boolean = {
val enclosingPkg = _sym.enclosingPackage
if (enclosingPkg == NoSymbol) false
else if (matches(enclosingPkg, symAccessBoundary)) true
else withinAccessBoundary(enclosingPkg)
}
// Is the enclosing package of the context owner
// within sym's access boundary?
isPublic(sym) || (sym.hasAccessBoundary && withinAccessBoundary(
context.owner
))
}

def isPublic(sym: Symbol) =
sym != NoSymbol && {
sym.info // needed to fill complete symbol
Expand All @@ -38,7 +56,8 @@ final class AutoImportsProvider(

def visit(sym: Symbol): Boolean = {
val id = sym.fullName
if (!isSeen(id) && isPublic(sym)) {
if (!isSeen(id) && isAccessible(sym)) {
// if (!isSeen(id)) {
isSeen += id
symbols += sym
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,38 @@ class ImportMissingSymbolLspSuite
)

check(
"6730-negative",
"6730-happy",
s"""|package example.a {
| private [example] object A {
| val foo = "foo"
| }
|}
|package example.b {
| object B {
| val bar = <<A>>.foo
| }
|}
|""".stripMargin,
s"""|${ImportMissingSymbol.title("A", "example.a")}
|${CreateNewSymbol.title("A")}
|""".stripMargin,
s"""|package example.a {
| private [example] object A {
| val foo = "foo"
| }
|}
|package example.b {
|
| import _root_.example.a.A
| object B {
| val bar = A.foo
| }
|}
|""".stripMargin,
)

check(
"6730-negative (private)",
s"""|package example.a {
| private object A {
| val foo = "foo"
Expand All @@ -314,4 +345,33 @@ class ImportMissingSymbolLspSuite
expectNoDiagnostics = false,
filterAction = _.getTitle() == ImportMissingSymbol.title("A", "example.a"),
)

check(
"6730-negative (private with out of path access boundary)",
s"""|package exampleA.a {
| private [exampleA] object A {
| val foo = "foo"
| }
|}
|package exampleB.b {
| object B {
| val bar = <<A>>
| }
|}
|""".stripMargin,
"",
s"""|package exampleA.a {
| private [exampleA] object A {
| val foo = "foo"
| }
|}
|package exampleB.b {
| object B {
| val bar = A
| }
|}
|""".stripMargin,
expectNoDiagnostics = false,
filterAction = _.getTitle() == ImportMissingSymbol.title("A", "example.a"),
)
}

0 comments on commit 9b586a8

Please sign in to comment.