Skip to content

Commit

Permalink
fix #101797: Suggest associated const for incorrect use of let in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Sep 15, 2022
1 parent c3f5929 commit 98e20c0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
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,
"considering use `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: considering use `const` instead of `let` for associated const: `const`

error: aborting due to previous error

0 comments on commit 98e20c0

Please sign in to comment.