Skip to content

Better error message when accessing private members #18686

Closed
@BalmungSan

Description

@BalmungSan

Compiler version

3.3.1

Minimized example

object Foo:
  private val Bar: Int = 3
end Foo

object Main:
  def main(args: Array[String]): Unit =
    println(Foo.Bar)
  end main
end Main

https://scastie.scala-lang.org/BalmungSan/l6omsTrKSaCu7fH2PCZSgg

Output Error/Warning message

value Bar cannot be accessed as a member of Foo.type from module class Main$.

Why this Error/Warning was not helpful

The original situation was actually accessing a Java field, and I had forgot that in Java public access requires the public keyword.
Thus, I was really confused about why I couldn't access a static int from my Scala code.
Furthermore, metals was very happy to suggest and autocomplete that field, which originally made me think that I had found some bug in the compiler, which was unable to see the field.

Suggested improvement

It could be made more helpful by explicitly stating that the reason why the field couldn't be accessed was because it is declared as private.

I know that is what "cannot be accessed" tries to express. But, under the pressure of some code not compiling at the end of the day it was not clear enough :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions