Skip to content

Commit

Permalink
bugfix: don't propose inaccessible named arg defaults
Browse files Browse the repository at this point in the history
Directly accessing `CompletionResult.ScopeMembers.results` bypasses
accessibility and similar checks. Going through `matchingResults`
ensures we get the same validation we'd get in other scope-based
completion contexts.

Before this patch, the newly added test would propose the invalid
completion

    foo(arg = somePackagePrivStr)
  • Loading branch information
harpocrates committed Feb 8, 2025
1 parent 06590c5 commit 92b983e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ trait ArgCompletions { this: MetalsGlobal =>
completions match {
case members: CompletionResult.ScopeMembers
if paramType != definitions.AnyTpe =>
members.results
members
.matchingResults(_ => _ => true)
.collect {
case mem
if mem.sym.tpe <:< paramType && notNothingOrNull(
Expand Down
24 changes: 24 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionArgSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1290,4 +1290,28 @@ class CompletionArgSuite extends BaseCompletionSuite {
topLines = Some(1)
)

check(
"inaccessible-defaults",
"""|/a/src/main/scala/A/package.scala
|package object A {
| private[A] val somePackagePrivStr = "foo"
| private val somePrivStr = "foo"
| val somePubStr = "bar"
|}
|/a/src/main/scala/B/Testing.scala
|package B
|
|import A._
|object Testing {
| def foo(arg: String): Unit = ()
| foo(@@)
|}
|""".stripMargin,
"""|arg = : String
|arg = somePubStr : String
|somePubStr: String
|""".stripMargin,
topLines = Some(3)
)

}

0 comments on commit 92b983e

Please sign in to comment.