Skip to content
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

Better error message when accessing private members #18686

Closed
BalmungSan opened this issue Oct 12, 2023 · 1 comment · Fixed by #18690
Closed

Better error message when accessing private members #18686

BalmungSan opened this issue Oct 12, 2023 · 1 comment · Fixed by #18690
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@BalmungSan
Copy link

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 :)

@BalmungSan BalmungSan added area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 12, 2023
@som-snytt
Copy link
Contributor

Tools should not suggest inaccessible things.

#17067

Also worth conjecturing that Scala's history with access primes us to be confused and wonder if it's a bug.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 13, 2023
Before, the error message showed `module class XYZ$`. Now it shows `object XYZ`.

This does not fix the issue scala#18686, it only improves a bit the current error message.
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 13, 2023
@bishabosha bishabosha removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Oct 13, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 16, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 16, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 17, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 17, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants