From 98e20c097cbb402ee2c6f4056fe8b4acca9679f9 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 15 Sep 2022 15:18:23 +0800 Subject: fix #101797: Suggest associated const for incorrect use of let in traits --- compiler/rustc_parse/src/parser/item.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index b190a7062de..045dc7357f1 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -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, -- cgit 1.4.1-3-g733a5 From 4bf7d2ca913179e3d4c71871a112820624c1e379 Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 16 Sep 2022 01:09:26 +0800 Subject: tweak suggestion --- compiler/rustc_parse/src/parser/item.rs | 2 +- src/test/ui/parser/suggest-assoc-const.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 045dc7357f1..e55b5ce71cd 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -705,7 +705,7 @@ impl<'a> Parser<'a> { if is_let { err.span_suggestion( non_item_span, - "considering use `const` instead of `let` for associated const", + "consider using `const` instead of `let` for associated const", "const", Applicability::MachineApplicable, ); diff --git a/src/test/ui/parser/suggest-assoc-const.stderr b/src/test/ui/parser/suggest-assoc-const.stderr index b92e399c905..2ddfa07c5be 100644 --- a/src/test/ui/parser/suggest-assoc-const.stderr +++ b/src/test/ui/parser/suggest-assoc-const.stderr @@ -2,7 +2,7 @@ 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` + | ^^^ help: consider using `const` instead of `let` for associated const: `const` error: aborting due to previous error -- cgit 1.4.1-3-g733a5