fix #101797: Suggest associated const for incorrect use of let in traits

This commit is contained in:
yukang 2022-09-15 15:18:23 +08:00
parent c3f59295fe
commit 98e20c097c
4 changed files with 43 additions and 4 deletions

View file

@ -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,

View file

@ -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() {
}

View file

@ -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() {
}

View file

@ -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