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

Suggest associated const for incorrect use of let in traits #101843

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,22 @@ impl<'a> Parser<'a> {
let semicolon_span = self.token.span;
// We have to bail or we'll potentially never make progress.
let non_item_span = self.token.span;
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
let is_let = self.token.is_keyword(kw::Let);

let mut err = self.struct_span_err(non_item_span, "non-item in item list");
err.span_label(open_brace_span, "item list starts here")
.span_label(non_item_span, "non-item starts here")
.span_label(self.prev_token.span, "item list ends here");
self.consume_block(Delimiter::Brace, ConsumeClosingDelim::Yes);
if is_let {
err.span_suggestion(
non_item_span,
"consider using `const` instead of `let` for associated const",
"const",
Applicability::MachineApplicable,
);
} else {
err.span_label(open_brace_span, "item list starts here")
.span_label(non_item_span, "non-item starts here")
.span_label(self.prev_token.span, "item list ends here");
}
if is_unnecessary_semicolon {
err.span_suggestion(
semicolon_span,
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/parser/suggest-assoc-const.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Issue: 101797, Suggest associated const for incorrect use of let in traits
// run-rustfix
trait Trait {
const _X: i32;
//~^ ERROR non-item in item list
}

fn main() {

}
10 changes: 10 additions & 0 deletions src/test/ui/parser/suggest-assoc-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Issue: 101797, Suggest associated const for incorrect use of let in traits
// run-rustfix
trait Trait {
let _X: i32;
//~^ ERROR non-item in item list
}

fn main() {

}
8 changes: 8 additions & 0 deletions src/test/ui/parser/suggest-assoc-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: non-item in item list
--> $DIR/suggest-assoc-const.rs:4:5
|
LL | let _X: i32;
| ^^^ help: consider using `const` instead of `let` for associated const: `const`

error: aborting due to previous error