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

let's not needed in struct field definitions #101789

Merged
merged 1 commit into from
Oct 11, 2022
Merged

let's not needed in struct field definitions #101789

merged 1 commit into from
Oct 11, 2022

Conversation

gimbling-away
Copy link
Contributor

Fixes #101683

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 14, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 14, 2022
@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

You will have to run ./x.py test src/test/ui --bless to update the checked test output.

@@ -1725,7 +1725,12 @@ impl<'a> Parser<'a> {
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
err
} else {
self.expected_ident_found()
let mut err = self.expected_ident_found();
if ident.name == kw::Let {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we would do N=1 lookahead to check if the next element is an identifier, and then do self.bump(); (to consume the let) and then get that ident, emit the error and return Ok(ident) (so that the parser can properly recover and continue creating the struct with the expected field name, and even emit errors if the mistyped field isn't included elsewhere). In all other cases, we keep the current behavior (returning err) and let the other parser recovery kick in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to extend the existing test to include something like

struct S {
    let s: (),
    y: (),
}
fn main() {
    let _ = S { // should complain about missing `y`
        s: (),
    };
    let _ = S {
        y: (),  // should complain about missing `s`
    };
    let _ = S {
        let s: (),  // should complain about missing `y` and the spurious `let`
    };
    let _ = S {
        let y: (), // should complain about missing `s` and the spurious `let`
    };
    let _ = S {
        let s = (),  // should complain about missing `y`, the `:` to `=` typo and the spurious `let`
    };
    let _ = S {
        let y = (), // should complain about missing `s`, the `:` to `=` typo and the spurious `let`
    };
}

Keep in mind that the later four recoveries are not included in this PR (but I would like to have it in the future).

@gimbling-away
Copy link
Contributor Author

You will have to run ./x.py test src/test/ui --bless to update the checked test output.

Yah, will bless it a in a while. Currently struggling with git branches again... (why does github not allow multiple forks )😮‍💨

@gimbling-away
Copy link
Contributor Author

@rustbot author

cc @estebank Finally got some time and implemented what you described. Currently it's borked though and a test breaks. Any idea why? 🤔

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 24, 2022
@Rageking8
Copy link
Contributor

Rageking8 commented Sep 24, 2022

compiler/rustc_parse/src/parser/item.rs at line 1735:
                 err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
                 err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
                 err
-            }
-
-            else {
+            } else {
                 let mut err = self.expected_ident_found();
                 if self.check_ident() {
                     self.bump();

(From CI logs)

CI seems to be complaining about the code formatting, maybe try blessing again with tidy?

Not a hundred percent sure though.

@gimbling-away
Copy link
Contributor Author

compiler/rustc_parse/src/parser/item.rs at line 1735:
                 err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks");
                 err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information");
                 err
-            }
-
-            else {
+            } else {
                 let mut err = self.expected_ident_found();
                 if self.check_ident() {
                     self.bump();

(From CI logs)

CI seems to be complaining about the code formatting, maybe try blessing again with tidy.

Oh ye, forgot to tidy, aside from that I meant the ICE causing test bork

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@gimbling-away
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 2, 2022
@rust-log-analyzer

This comment has been minimized.

Co-authored-by: jyn514 <jyn514@gmail.com>
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
@Dylan-DPC
Copy link
Member

@bors r=estebank

@bors
Copy link
Contributor

bors commented Oct 10, 2022

📌 Commit 6071b4b has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 10, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 10, 2022
`let`'s not needed in struct field definitions

Fixes rust-lang#101683
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Oct 10, 2022
`let`'s not needed in struct field definitions

Fixes rust-lang#101683
@@ -1788,7 +1788,23 @@ impl<'a> Parser<'a> {
}
}
} else {
self.expected_ident_found()
let mut err = self.expected_ident_found();
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably could've been self.token.is_keyword(kw::Let), see Token::is_keyword

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this could've used Parser::eat_keyword_noexpect to avoid the bump call below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, will send an improvement PR later on. 👍🏻

if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" {
self.bump(); // `let`
let span = self.prev_token.span.until(self.token.span);
err.span_suggestion(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Xiretza this is a good example of a #[derive(Subdiagnostic)] candidate that both emits notes and a suggestion!

Comment on lines +1804 to +1805
self.bump();
return Ok(ident);
Copy link
Member

@compiler-errors compiler-errors Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gimbles for future reference, why is this bumping twice? This should perhaps be:

Suggested change
self.bump();
return Ok(ident);
let ident = self.parse_ident()?;
return Ok(ident);

Otherwise, it seems to me like it's returning let as the struct field name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the earlier reviews on wonky spans ^^

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 11, 2022
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#101360 (Point out incompatible closure bounds)
 - rust-lang#101789 (`let`'s not needed in struct field definitions)
 - rust-lang#102846 (update to syn-1.0.102)
 - rust-lang#102871 (rustdoc: clean up overly complex `.trait-impl` CSS selectors)
 - rust-lang#102876 (suggest candidates for unresolved import)
 - rust-lang#102888 (Improve rustdoc-gui search-color test)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 01a2246 into rust-lang:master Oct 11, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recover from accidental usage of let in struct definition
10 participants