Skip to content

Closures parsed as self-type annotation #1683

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
Blaisorblade opened this issue Nov 8, 2016 · 6 comments
Closed

Closures parsed as self-type annotation #1683

Blaisorblade opened this issue Nov 8, 2016 · 6 comments

Comments

@Blaisorblade
Copy link
Contributor

I'm not sure I care and I can't conceive a use case (at least, not without involving some weird macro), but the following is valid for scalac and rejected (at parse-time) by dotty:

class Foo {
  (a: Int, b: String) => a
}

The reason is that dotty parses (what looks like) an expression, then sees => and "reinterprets" the parsed expression as a self-type annotation.

I'm not so concerned about the feature, I'm more concerned about aligning the grammar with the implementation.

I noticed this while trying to document errors for self-type annotations.

So this gives something like:

> run -explain tests/Foo.scala
[...]
[info] Running dotty.tools.dotc.Main -explain tests/Foo.scala
-- Error: tests/Foo.scala ----------------------------------------------------------------------------------------------
2 |  (a: Int, b: String) => a
  |  ^^^^^^^^^^^^^^^^^^^
  |  not a legal self type clause
-- [E006] Missing Identifier Error: tests/Foo.scala --------------------------------------------------------------------
2 |  (a: Int, b: String) => a
  |                         ^
  |                         not found: a

Explanation
===========
An identifier for `a` is missing. This means that something
has either been misspelt or you're forgetting an import


two errors found
java.lang.RuntimeException: Nonzero exit code returned from runner: 1

BTW I don't like "missing identifier", I think it's trying to say "unbound identifier" (I have a local patch I'll send at some point).

@liufengyun
Copy link
Contributor

Thanks for reporting @Blaisorblade . This is related to #1424.

Unlike #1424, it seems the parser can do a better job here, as it's possible to know syntactically it's not a self-annotation, but a lambda def.

@Blaisorblade
Copy link
Contributor Author

Thanks I missed that! Also Martin there claims that handling this in Scalac is very tricky, so maybe it shouldn't be fixed. Making sure the grammar rejects this might be enough.

@odersky
Copy link
Contributor

odersky commented Nov 9, 2016

BTW I don't like "missing identifier", I think it's trying to say "unbound identifier" (I have a local patch I'll send at some point).

I don't like that either. But "unbound" is probably too academic for some. How about

No definition for `a` was found.

?

@odersky
Copy link
Contributor

odersky commented Nov 9, 2016

I forgot to say: "Unbound Identifier" looks fine as a name for this class of error messages. But the explanation should say something else.

@odersky
Copy link
Contributor

odersky commented Nov 9, 2016

I think we should not try to overengineer fixing this. The scalac code doing this is quite impenetrable. I think "not a legal self type clause" is perfectly fine as an error message.

@Blaisorblade
Copy link
Contributor Author

BTW I don't like "missing identifier", I think it's trying to say "unbound identifier" (I have a local patch I'll send at some point).

I don't like that either. But "unbound" is probably too academic for some. How about

No definition for a was found.
?

I was also concerned on unbound, I'll probably use "declaration" rather than "definition".

I think we should not try to overengineer fixing this. The scalac code doing this is quite impenetrable. I think "not a legal self type clause" is perfectly fine as an error message.

Agreed, especially since the grammar already excludes this (which I missed) and the current parser is already nontrivial. I'll close this as duplicate.

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

3 participants