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

Parser is too eager to parse infix expressions #10144

Closed
scabug opened this issue Jan 11, 2017 · 3 comments
Closed

Parser is too eager to parse infix expressions #10144

scabug opened this issue Jan 11, 2017 · 3 comments

Comments

@scabug
Copy link

scabug commented Jan 11, 2017

Scalac manages to parse both of the following expressions:

scala> a.b(c).d(e) //print
   a.b(c).d(e) // : <error>

scala> a b(c).d(e) //print
   a.b(c.d(e)) // : <error>

I think it is pretty clear that the second one will almost always be because of a wrong keystroke or an oversight. But scalac manages to parse it, and completely ignores the parenthesis to the right of c, resulting in something that the author probably didn't intend (or he would have written it that way).

Some convoluted example to show this can lead to unexpected results:

scala> object a { def b(x: Any) = hasD }; object hasD { def d(x: Any) = x }; object e; val c = hasD
defined object a
defined object hasD
defined object e
c: hasD.type = hasD$@4bb2ee58

scala> a b(c).d(e)
res1: hasD.type = hasD$@4bb2ee58

scala> a.b(c).d(e)
res2: Any = e$@756c6202

Example it can lead to very confusing compiler errors: http://stackoverflow.com/questions/41600172/scala-multiple-type-parameter-that-are-defined-later

@scabug
Copy link
Author

scabug commented Jan 11, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10144?orig=1
Reporter: Jasper-M
Affected Versions: 2.10.6, 2.11.8, 2.12.1

@scabug
Copy link
Author

scabug commented Jan 12, 2017

@som-snytt said:
Counter-example is "hello" +("world").length, but wasn't there talk about an annotation to specify if infix is recommended or allowed?

Maybe an Abide rule could be endowed with suitable heuristics.

@scabug
Copy link
Author

scabug commented Jan 12, 2017

@adriaanm said:
There's certainly scope for a linter/style checker to flag infix method calls as suspicious if the method name is not in a whitelist of infix operator names, but infix syntax is not something we can remove outright. True, a + (c).d(e) is more likely to be as intended than a b (c).d(e), but we cannot distinguish them during parsing.

@scabug scabug closed this as completed Jan 12, 2017
@scabug scabug added the quickfix label Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant