Skip to content

Inconsistent parsing of symbols wrapped in backticks #15547

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

Closed
nrinaudo opened this issue Jun 29, 2022 · 4 comments
Closed

Inconsistent parsing of symbols wrapped in backticks #15547

nrinaudo opened this issue Jun 29, 2022 · 4 comments

Comments

@nrinaudo
Copy link

Compiler version

3.1.2

Minimized code

def `=`: String = "="
def `+`: String = "+"

val withEq: String   = `=` + "3"
val withPlus: String = `+` + "3"

Output

Compile error on the last line:

; expected but string constant found

See https://scastie.scala-lang.org/WCnbXR7RRzOuc8zYkrlxUQ for a "live" reproduction

Expectation

Either a compile error for both withEq and withPlus, or none, but this seems inconsistent.

@nrinaudo nrinaudo added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 29, 2022
@prolativ
Copy link
Contributor

Not sure of the source of the discrepancy but I'm getting slightly different errors with the same compiler version when trying to compile this locally

[error] Bug.scala:5:30: end of toplevel definition expected but string literal found
[error] val withPlus: String = `+` + "3"
[error]                              ^
[error] Bug.scala:5:24: value unary_+ is not a member of String
[error] val withPlus: String = `+` + "3"
[error]                        ^^^^^   

The point is that + is not really treated as a keyword by the parser while = is. This means that + and + are equivalent so you could write e.g.

def + : String = "+"

instead of

def `+`: String = "+"

while

def = : String = "="

would be syntactically incorrect.
Going further all of

+ + "3"
`+` + "3"
+ `+` "3"
`+` `+` "3"

mean the same and the first + is being interpreted as a prefix operator here.
This seems consistent with the behaviour in Scala 2.13 and I'm not sure if this could be changed. @odersky WDYT?

@prolativ prolativ added stat:needs info and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 29, 2022
@odersky
Copy link
Contributor

odersky commented Jun 29, 2022

I can't reproduce this on latest main. Everything compiles. I remember we had a PR recently that changed the status of backticked unary operators. Maybe that fixed it.

@prolativ
Copy link
Contributor

Indeed seems to be fixed by #15198

@som-snytt
Copy link
Contributor

was

PackageDef(Ident(<empty>), List(DefDef(f, List(), TypeTree(), PrefixOp(Ident(+), Ident(+)))))

now

PackageDef(Ident(<empty>), List(DefDef(f, List(), TypeTree(), InfixOp(Ident(+), Ident(+), Literal("*")))))

Noting that -Vprint:parser -Ystop-after:parser does not work as one might expect. (Result is empty.)

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

4 participants