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

Ambiguity between type ascription and absolute paths #64242

Open
matklad opened this issue Sep 6, 2019 · 9 comments
Open

Ambiguity between type ascription and absolute paths #64242

matklad opened this issue Sep 6, 2019 · 9 comments
Labels
A-parser Area: The parsing of Rust source code to an AST T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@matklad
Copy link
Member

matklad commented Sep 6, 2019

I am not sure, but I think the following program should be valid:

type X = ();

fn main() {
    let ():::X = ();
}

It currently fails with

error: expected identifier, found `:`
 --> /home/matklad/tmp/main.rs:4:13
  |
4 |     let ():::X = ();
  |             ^ expected identifier

because we don't try to decompose :: token.

I am not sure what should be the behavior of

type X = ();

fn main() {
    let Y:::X = ();
}

but I lean towards allowing it as well, looks like it requires only constant look-ahead to handle.

Anyway, rg ':::' --type rust shows that we don't have a test here.

@petrochenkov what are your thoughts on this?

@matklad
Copy link
Member Author

matklad commented Sep 6, 2019

сс @rust-lang/wg-grammar

@Centril Centril added A-parser Area: The parsing of Rust source code to an AST T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Sep 6, 2019
@petrochenkov
Copy link
Contributor

what are your thoughts on this?

I was aware of this, but never planned making a fix due to low priority.
It's quite possible that some other multi-character punctuation is not broken into parts when it should.
As you could notice things like this are fixed on "on demand" basis.
Hopefully, issues like this will disappear as a class after migrating to the single-character punctuation model.

@Centril
Copy link
Contributor

Centril commented Sep 6, 2019

I am not sure, but I think the following program should be valid:

According to libsyntax (fn parse_path), the grammar is:

    /// `path = [::] segment+`
    /// `segment = ident | ident[::]<args> | ident[::](args) [-> type]`

A path cannot end with :: and so an identifier is correctly expected but not found.

I am not sure what should be the behavior of

Unless I'm missing something fundamental here, the same issue as above applies and this should remain an error.

@matklad
Copy link
Member Author

matklad commented Sep 6, 2019

@Centril I think you are trying to read this as :: :, while I read it as : :: I believe (): ::X and Y: ::X are unambiguous parses, it's just that the current implementation is to eager to eat ::.

I've checked with syn, and it parses the first example but not the second one.

@matklad
Copy link
Member Author

matklad commented Sep 6, 2019

Hopefully, issues like this will disappear as a class after migrating to the single-character punctuation model.

Yeah, that's exactly how I found this. My fear here though is that a genuine ambiguity might lurk in there...

@Centril
Copy link
Contributor

Centril commented Sep 6, 2019

@Centril I think you are trying to read this as :: :, while I read it as : :: I believe (): ::X and Y: ::X are unambiguous parses, it's just that the current implementation is to eager to eat ::.

Oh I see. What's the ambiguity referenced in the title then?

@matklad
Copy link
Member Author

matklad commented Sep 6, 2019

@Centril that was a non-formal use of the word, in a sense that rustc's parser can confuse the two. I am almost sure that there's no ambiguity on the grammar level here with tokens-with-jointness model, but I want to double check

@Centril
Copy link
Contributor

Centril commented Sep 7, 2019

@matklad I see. For now, I think it would be good to add a test documenting the current behavior to prevent accidents.

matklad added a commit to matklad/rust-analyzer that referenced this issue Sep 10, 2019
@GrigorenkoPV
Copy link
Contributor

GrigorenkoPV commented Sep 22, 2024

After #130673, the parser now recovers from ::: to just :: in some places. Not here though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants