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 parsing error when defining a val named _root_ with a type #18020

Open
Linyxus opened this issue Jun 20, 2023 · 4 comments
Open

Confusing parsing error when defining a val named _root_ with a type #18020

Linyxus opened this issue Jun 20, 2023 · 4 comments

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Jun 20, 2023

Compiler version

main (and also in Scala 2)

Minimized code

def foo: Unit =
  val _root_: String = "abc"

Output

-- [E032] Syntax Error: issues/i17757.scala:2:21 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 |  val _root_: String = "abc"
  |                     ^
  |                     pattern expected
  |
  | longer explanation available when compiling with `-explain`

Expectation

If you leave out the type, it will compile:

val _root_ = "abc"

So this one with a type annotation should compile as well?

Found together with @jan-pieter @nmcb when working on #18019

@Linyxus Linyxus added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 20, 2023
@odersky odersky added area:typer Spree Suitable for a future Spree and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 24, 2023
@odersky
Copy link
Contributor

odersky commented Jun 24, 2023

_root_ should not be used as a name of anything but a package. I guess we should enforce that. See #18050 for a related issue. Could be a good issue for a spree:

@scala-center-bot
Copy link

This issue was picked for the Issue Spree No. 33 of 11 July 2023 which takes place in 7 days. @dwijnand, @nmcb, @jan-pieter will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@dwijnand
Copy link
Member

This isn't specific to _root_ or to colon, you get the same error with:

scala> val foo String = "abc"
-- [E032] Syntax Error: --------------------------------------------------------
1 |val foo String = "abc"
  |               ^
  |               pattern expected
  |
  | longer explanation available when compiling with `-explain`

But perhaps we can get the parser to realise that when it fails to find the infix op, too look if the first part (the LHS of the infix op), ends in a : and suggest a space.

Alternatively, it would be nice if val foo_: String = ... and ???: String just worked without the space...

@som-snytt
Copy link
Contributor

Compare #16072

smarter added a commit that referenced this issue Jul 14, 2023
References #18020, didn't actually fix that, because it addressed
aspects of using `_root_` in definitions. Adding more details to that
ticket.
Fixes #17757
Fixes #18050
Includes the code in scala/bug#12508
@mbovel mbovel removed the Spree Suitable for a future Spree label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants