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

confusing "Not found" error when I change "def foo()" to "val foo()" #18684

Closed
som-snytt opened this issue Oct 12, 2023 · 1 comment · Fixed by #18725
Closed

confusing "Not found" error when I change "def foo()" to "val foo()" #18684

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

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.3/3.4

Minimized example

Welcome to Scala 3.4.0-RC1-bin-SNAPSHOT-git-d788ef2 (21, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> val s(): String = "hello, world"
-- [E006] Not Found Error: ---------------------------------------------------------------------------------------------
1 |val s(): String = "hello, world"
  |    ^
  |    Not found: s
  |
  | longer explanation available when compiling with `-explain`
1 error found
Welcome to Scala 3.3.1 (21, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> def s(): String = "hello, world"
def s(): String

scala> val s(): String = "hello, world"
-- [E100] Syntax Error: ------------------------------------------------------------------------------------------------
1 |val s(): String = "hello, world"
  |    ^
  |    method s must be called with () argument
  |
  | longer explanation available when compiling with `-explain`
-- Warning: ------------------------------------------------------------------------------------------------------------
1 |val s(): String = "hello, world"
  |                  ^^^^^^^^^^^^^^
  |          pattern's type Int does not match the right hand side expression's type String
  |
  |          If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |          which may result in a MatchError at runtime.
1 warning found
1 error found

Output Error/Warning message

-- [E006] Not Found Error: ---------------------------------------------------------------------------------------------
1 |val s(): String = "hello, world"
  |    ^
  |    Not found: s

Why this Error/Warning was not helpful

As explained at scala/bug#6564, when refactoring a def to a val, this is an easy mistake to make.

In addition, someone on chat suggested they weren't aware that "applies" in patterns did not require backquotes, so that it is not apparent (without research) that the error pertains to a pattern. That is, there is no distinguishing visual marker.

The confusion is even more confounding in REPL, as shown.

Suggested improvement

Mention that it is a pattern context. Or use pink squiggles instead of red. If you can notice that my val doesn't look like a pattern def, then say so.

Relatedly, I am unlikely to use a boolean extractor in a valdef, so that is an example of a pattern def that looks wrong.

scala> object s { def unapply(x: Any): Boolean = false }
// defined object s

scala> val s(): String = "hello, world"
1 warning found
-- Warning: ------------------------------------------------------------------------------------------------------------
1 |val s(): String = "hello, world"
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |    pattern binding uses refutable extractor `s`
  |
  |    If this usage is intentional, this can be communicated by adding `: @unchecked` after the expression,
  |    which may result in a MatchError at runtime.
scala.MatchError: hello, world (of class java.lang.String)
  ... 27 elided

At least it mentions here that it was a pattern.

@som-snytt som-snytt added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc stat:needs triage Every issue needs to have an "area" and "itype" label better-errors Issues concerned with improving confusing/unhelpful diagnostic messages labels Oct 12, 2023
@bishabosha bishabosha removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Oct 12, 2023
@bishabosha bishabosha changed the title Not found error when trying to define a variable confusing "Not found" error when I change "def foo()" to "val foo()" Oct 12, 2023
@som-snytt
Copy link
Contributor Author

The original title was more laden with irony, but the amended title is closer in spirit to the original ticket.

Worth adding that the OP expected a syntax error, such is the strong intuition that the compiler must raise red flags around code that is merely adjacent to correctness.

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