Skip to content

Commit

Permalink
(Scala2) Delegate to Context#isAccessible for accessibity check
Browse files Browse the repository at this point in the history
  • Loading branch information
masonedmison committed Sep 12, 2024
1 parent f3657de commit 2279432
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ final class AutoImportsProvider(
sym.name.dropLocal.decoded == name

symbols.result().collect {
case sym if isExactMatch(sym, name) =>
case sym
if isExactMatch(sym, name) && context.isAccessible(sym, sym.info) =>
val pkg = sym.owner.fullName
val edits = importPosition match {
// if we are in import section just specify full name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ trait WorkspaceSymbolSearch { compiler: MetalsGlobal =>

var added = 0
for {
sym <- loadSymbolFromClassfile(top)
sym <- loadSymbolFromClassfile(top, context)
if context.lookupSymbol(sym.name, _ => true).symbol != sym
} {
if (visitMember(sym)) {
Expand Down Expand Up @@ -183,13 +183,11 @@ trait WorkspaceSymbolSearch { compiler: MetalsGlobal =>
}

private def loadSymbolFromClassfile(
classfile: SymbolSearchCandidate
classfile: SymbolSearchCandidate,
context: Context
): List[Symbol] = {
def isAccessible(sym: Symbol): Boolean = {
sym != NoSymbol && {
sym.info // needed to fill complete symbol
sym.isPublic
}
context.isAccessible(sym, sym.info)
}
try {
classfile match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,93 @@ class ImportMissingSymbolLspSuite
|}
|""".stripMargin,
)

check(
"i6732-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(
"i6732-negative (private)",
s"""|package example.a {
| private object A {
| val foo = "foo"
| }
|}
|package example.b {
| object B {
| val bar = <<A>>
| }
|}
|""".stripMargin,
"",
s"""|package example.a {
| private object A {
| val foo = "foo"
| }
|}
|package example.b {
| object B {
| val bar = A
| }
|}
|""".stripMargin,
expectNoDiagnostics = false,
filterAction = _.getTitle() == ImportMissingSymbol.title("A", "example.a"),
)

check(
"i6732-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 2279432

Please sign in to comment.