Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Confusing compiler errors when you forget the using keyword in given bar(using foo) #19402

Closed
raquo opened this issue Jan 9, 2024 · 1 comment · May be fixed by #19496
Closed

Confusing compiler errors when you forget the using keyword in given bar(using foo) #19402

raquo opened this issue Jan 9, 2024 · 1 comment · May be fixed by #19496
Labels
area:parser area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@raquo
Copy link
Contributor

raquo commented Jan 9, 2024

Compiler version

3.3.1, 3.4.0-RC1

Minimized example

object Example {

  class Bar(foo: Foo)
  
  class Foo
  
  given Foo = Foo()
  
  given bar(foo: Foo): Bar = Bar(foo) 
  
}

Output Error/Warning message

[error] -- [E040] Syntax Error: /.../Example.scala:11:11
[error] 11 |  given bar(foo: Foo): Bar = Bar(foo)
[error]    |           ^
[error]    |           ':' expected, but '(' found
[error] -- [E067] Syntax Error: /.../Example.scala:11:8
[error] 11 |  given bar(foo: Foo): Bar = Bar(foo)
[error]    |        ^
[error]    |Declaration of given instance bar not allowed here: only classes can have declared but undefined members

Why this Error/Warning was not helpful

I intended to write given bar(using foo: Foo): Bar = Bar(foo) but forgot the using keyword.

If I wasn't already familiar with these errors it would be hard to figure out what's wrong. The second error can be quite misleading if you don't pay attention to the reason ("only classes can have declared but undefined members"). And it's too easy to dismiss that reason because my given does appear to have a defined body.

Suggested improvement

Ideally I expect an error saying that given-s can't have non-implicit arguments, and that I need a using keyword. Or anything that comes closer to that than the status quo really.

@raquo raquo 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 Jan 9, 2024
@nicolasstucki nicolasstucki added area:parser Spree Suitable for a future Spree and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 9, 2024
i10416 added a commit to i10416/dotty that referenced this issue Jan 20, 2024
`given`-s can't have non-implicit arguments, but when users forget
adding a `using` modifier to given parameters, it showed unfriendly error
message; "only classes can have declared but undefined members".

The `termParamClauses` parser is aware of the owner of parameters and it raises
a syntax error when parameters in given definition do not have `using` modifier with clear reason,
but `in.token == LPAREN && in.lookahead.isIdent(nme.using)` in
Parsers.scala was too strict to delegate parameter clause handling to `termParamClauses`.

Note:
This change requires tests/neg/i8150.check to be updated because the original
test starts failing due to the syntax error rather than "refinement cannot be `given`" error.

`type T = {given A with A}` in i8150.check confirms that a valid given definition
in RHS of type alias still raises the original error; "refinement cannot be `given`" error.
@mbovel mbovel removed the Spree Suitable for a future Spree label Jan 30, 2024
WojciechMazur pushed a commit that referenced this issue Jul 1, 2024
WojciechMazur pushed a commit that referenced this issue Jul 2, 2024
WojciechMazur added a commit that referenced this issue Jul 2, 2024
…definitions" to LTS (#20929)

Backports #19714 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area:parser 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