From 692bc344d55cf9d86c60b06c92a70684d013c89f Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sat, 6 Jul 2024 03:07:46 +0000 Subject: Make parse error suggestions verbose and fix spans Go over all structured parser suggestions and make them verbose style. When suggesting to add or remove delimiters, turn them into multiple suggestion parts. --- compiler/rustc_parse/src/parser/diagnostics.rs | 35 ++++++++---------- compiler/rustc_parse/src/parser/expr.rs | 11 +++--- compiler/rustc_parse/src/parser/item.rs | 6 +++- compiler/rustc_parse/src/parser/pat.rs | 50 +++++++++++++++++--------- compiler/rustc_parse/src/parser/stmt.rs | 8 +++-- compiler/rustc_parse/src/parser/ty.rs | 23 ++++++------ 6 files changed, 78 insertions(+), 55 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 81d5f0fca0e..63e4d8a4352 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -3,8 +3,8 @@ use super::{ BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType, }; use crate::errors::{ - AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2, BadTypePlus, - BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained, + AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2, + BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, @@ -566,7 +566,10 @@ impl<'a> Parser<'a> { && expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq))) { // Likely typo: `=` → `==` in let expr or enum item - return Err(self.dcx().create_err(UseEqInstead { span: self.token.span })); + return Err(self.dcx().create_err(UseEqInstead { + span: self.token.span, + suggestion: self.token.span.with_lo(self.token.span.lo() + BytePos(1)), + })); } if self.token.is_keyword(kw::Move) && self.prev_token.is_keyword(kw::Async) { @@ -1151,7 +1154,7 @@ impl<'a> Parser<'a> { // Eat from where we started until the end token so that parsing can continue // as if we didn't have those extra angle brackets. self.eat_to_tokens(end); - let span = lo.until(self.token.span); + let span = lo.to(self.prev_token.span); let num_extra_brackets = number_of_gt + number_of_shr * 2; return Some(self.dcx().emit_err(UnmatchedAngleBrackets { span, num_extra_brackets })); @@ -1539,7 +1542,10 @@ impl<'a> Parser<'a> { pub(super) fn maybe_report_ambiguous_plus(&mut self, impl_dyn_multi: bool, ty: &Ty) { if impl_dyn_multi { - self.dcx().emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(ty), span: ty.span }); + self.dcx().emit_err(AmbiguousPlus { + span: ty.span, + suggestion: AddParen { lo: ty.span.shrink_to_lo(), hi: ty.span.shrink_to_hi() }, + }); } } @@ -1608,21 +1614,10 @@ impl<'a> Parser<'a> { let sum_span = ty.span.to(self.prev_token.span); let sub = match &ty.kind { - TyKind::Ref(lifetime, mut_ty) => { - let sum_with_parens = pprust::to_string(|s| { - s.s.word("&"); - s.print_opt_lifetime(lifetime); - s.print_mutability(mut_ty.mutbl, false); - s.popen(); - s.print_type(&mut_ty.ty); - if !bounds.is_empty() { - s.word(" + "); - s.print_type_bounds(&bounds); - } - s.pclose() - }); - - BadTypePlusSub::AddParen { sum_with_parens, span: sum_span } + TyKind::Ref(_lifetime, mut_ty) => { + let lo = mut_ty.ty.span.shrink_to_lo(); + let hi = self.prev_token.span.shrink_to_hi(); + BadTypePlusSub::AddParen { suggestion: AddParen { lo, hi } } } TyKind::Ptr(..) | TyKind::BareFn(..) => BadTypePlusSub::ForgotParen { span: sum_span }, _ => BadTypePlusSub::ExpectPath { span: sum_span }, diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index b2df9a14eb0..4bd20be4171 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -714,7 +714,7 @@ impl<'a> Parser<'a> { type_err.cancel(); self.dcx().emit_err(errors::MalformedLoopLabel { span: label.ident.span, - correct_label: label.ident, + suggestion: label.ident.span.shrink_to_lo(), }); return Ok(expr); } @@ -856,7 +856,7 @@ impl<'a> Parser<'a> { let hi = self.interpolated_or_expr_span(&expr); let span = lo.to(hi); if let Some(lt) = lifetime { - self.error_remove_borrow_lifetime(span, lt.ident.span); + self.error_remove_borrow_lifetime(span, lt.ident.span.until(expr.span)); } Ok((span, ExprKind::AddrOf(borrow_kind, mutbl, expr))) } @@ -1653,6 +1653,7 @@ impl<'a> Parser<'a> { let lo = label_.ident.span; let label = Some(label_); let ate_colon = self.eat(&token::Colon); + let tok_sp = self.token.span; let expr = if self.eat_keyword(kw::While) { self.parse_expr_while(label, lo) } else if self.eat_keyword(kw::For) { @@ -1747,7 +1748,7 @@ impl<'a> Parser<'a> { self.dcx().emit_err(errors::RequireColonAfterLabeledExpression { span: expr.span, label: lo, - label_end: lo.shrink_to_hi(), + label_end: lo.between(tok_sp), }); } @@ -2106,7 +2107,7 @@ impl<'a> Parser<'a> { self.bump(); self.dcx().emit_err(errors::FloatLiteralRequiresIntegerPart { span: token.span, - correct: pprust::token_to_string(token).into_owned(), + suggestion: token.span.shrink_to_lo(), }); } } @@ -2741,7 +2742,7 @@ impl<'a> Parser<'a> { if !attrs.is_empty() && let [x0 @ xn] | [x0, .., xn] = &*attrs.take_for_recovery(self.psess) { - let attributes = x0.span.to(xn.span); + let attributes = x0.span.until(branch_span); let last = xn.span; let ctx = if is_ctx_else { "else" } else { "if" }; self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse { diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 2c98feeece7..81e2ca2a490 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -2241,9 +2241,13 @@ impl<'a> Parser<'a> { let kw_token = self.token.clone(); let kw_str = pprust::token_to_string(&kw_token); let item = self.parse_item(ForceCollect::No)?; + let mut item = item.unwrap().span; + if self.token == token::Comma { + item = item.to(self.token.span); + } self.dcx().emit_err(errors::NestedAdt { span: kw_token.span, - item: item.unwrap().span, + item, kw_str, keyword: keyword.as_str(), }); diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 6f2b7177159..e4e89615d71 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -4,11 +4,11 @@ use crate::errors::{ DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt, ExpectedCommaAfterPatternField, GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern, - PatternOnWrongSideOfAt, RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, - TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, - UnexpectedExpressionInPattern, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat, - UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam, - UnexpectedVertVertInPattern, + ParenRangeSuggestion, PatternOnWrongSideOfAt, RemoveLet, RepeatedMutInPattern, + SwitchRefBoxOrder, TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, + TrailingVertNotAllowed, UnexpectedExpressionInPattern, UnexpectedLifetimeInPattern, + UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg, + UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens, }; use crate::parser::expr::{could_be_unclosed_char_literal, LhsExpr}; use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; @@ -24,7 +24,7 @@ use rustc_errors::{Applicability, Diag, PResult}; use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::{ErrorGuaranteed, Span}; +use rustc_span::{BytePos, ErrorGuaranteed, Span}; use thin_vec::{thin_vec, ThinVec}; #[derive(PartialEq, Copy, Clone)] @@ -236,11 +236,15 @@ impl<'a> Parser<'a> { if let PatKind::Or(pats) = &pat.kind { let span = pat.span; - let pat = pprust::pat_to_string(&pat); let sub = if pats.len() == 1 { - Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span, pat }) + Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { + span: span.with_hi(span.lo() + BytePos(1)), + }) } else { - Some(TopLevelOrPatternNotAllowedSugg::WrapInParens { span, pat }) + Some(TopLevelOrPatternNotAllowedSugg::WrapInParens { + span, + suggestion: WrapInParens { lo: span.shrink_to_lo(), hi: span.shrink_to_hi() }, + }) }; let err = self.dcx().create_err(match syntax_loc { @@ -599,7 +603,10 @@ impl<'a> Parser<'a> { self.bump(); // `...` // The user probably mistook `...` for a rest pattern `..`. - self.dcx().emit_err(DotDotDotRestPattern { span: lo }); + self.dcx().emit_err(DotDotDotRestPattern { + span: lo, + suggestion: lo.with_lo(lo.hi() - BytePos(1)), + }); PatKind::Rest } @@ -664,8 +671,13 @@ impl<'a> Parser<'a> { _ => return, } - self.dcx() - .emit_err(AmbiguousRangePattern { span: pat.span, pat: pprust::pat_to_string(pat) }); + self.dcx().emit_err(AmbiguousRangePattern { + span: pat.span, + suggestion: ParenRangeSuggestion { + lo: pat.span.shrink_to_lo(), + hi: pat.span.shrink_to_hi(), + }, + }); } /// Parse `&pat` / `&mut pat`. @@ -674,8 +686,11 @@ impl<'a> Parser<'a> { if let token::Lifetime(name) = self.token.kind { self.bump(); // `'a` - self.dcx() - .emit_err(UnexpectedLifetimeInPattern { span: self.prev_token.span, symbol: name }); + self.dcx().emit_err(UnexpectedLifetimeInPattern { + span: self.prev_token.span, + symbol: name, + suggestion: self.prev_token.span.until(self.token.span), + }); } let mutbl = self.parse_mutability(); @@ -913,10 +928,13 @@ impl<'a> Parser<'a> { self.dcx().emit_err(InclusiveRangeExtraEquals { span: span_with_eq }) } token::Gt if no_space => { - let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi(); + let after_pat = span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(); self.dcx().emit_err(InclusiveRangeMatchArrow { span, arrow: tok.span, after_pat }) } - _ => self.dcx().emit_err(InclusiveRangeNoEnd { span }), + _ => self.dcx().emit_err(InclusiveRangeNoEnd { + span, + suggestion: span.with_lo(span.hi() - BytePos(1)), + }), } } diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index d65f6ff68ee..70d41de00a7 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -430,8 +430,10 @@ impl<'a> Parser<'a> { let eq_consumed = match self.token.kind { token::BinOpEq(..) => { // Recover `let x = 1` as `let x = 1` - self.dcx() - .emit_err(errors::CompoundAssignmentExpressionInLet { span: self.token.span }); + self.dcx().emit_err(errors::CompoundAssignmentExpressionInLet { + span: self.token.span, + suggestion: self.token.span.with_hi(self.token.span.lo() + BytePos(1)), + }); self.bump(); true } @@ -717,7 +719,7 @@ impl<'a> Parser<'a> { e.cancel(); self.dcx().emit_err(MalformedLoopLabel { span: label.ident.span, - correct_label: label.ident, + suggestion: label.ident.span.shrink_to_lo(), }); *expr = labeled_expr; break 'break_recover None; diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 1e5b227aaa9..94321b1dddd 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -209,6 +209,7 @@ impl<'a> Parser<'a> { recover_qpath: RecoverQPath, recover_return_sign: RecoverReturnSign, ) -> PResult<'a, FnRetTy> { + let lo = self.prev_token.span; Ok(if self.eat(&token::RArrow) { // FIXME(Centril): Can we unconditionally `allow_plus`? let ty = self.parse_ty_common( @@ -224,7 +225,10 @@ impl<'a> Parser<'a> { // Don't `eat` to prevent `=>` from being added as an expected token which isn't // actually expected and could only confuse users self.bump(); - self.dcx().emit_err(ReturnTypesUseThinArrow { span: self.prev_token.span }); + self.dcx().emit_err(ReturnTypesUseThinArrow { + span: self.prev_token.span, + suggestion: lo.between(self.token.span), + }); let ty = self.parse_ty_common( allow_plus, AllowCVariadic::No, @@ -794,8 +798,11 @@ impl<'a> Parser<'a> { { if self.token.is_keyword(kw::Dyn) { // Account for `&dyn Trait + dyn Other`. - self.dcx().emit_err(InvalidDynKeyword { span: self.token.span }); self.bump(); + self.dcx().emit_err(InvalidDynKeyword { + span: self.prev_token.span, + suggestion: self.prev_token.span.until(self.token.span), + }); } bounds.push(self.parse_generic_bound()?); if allow_plus == AllowPlus::No || !self.eat_plus() { @@ -861,7 +868,7 @@ impl<'a> Parser<'a> { if has_parens { // FIXME(Centril): Consider not erroring here and accepting `('lt)` instead, // possibly introducing `GenericBound::Paren(P)`? - self.recover_paren_lifetime(lo, lt.ident.span)?; + self.recover_paren_lifetime(lo)?; } Ok(bound) } @@ -909,16 +916,12 @@ impl<'a> Parser<'a> { } /// Recover on `('lifetime)` with `(` already eaten. - fn recover_paren_lifetime(&mut self, lo: Span, lt_span: Span) -> PResult<'a, ()> { + fn recover_paren_lifetime(&mut self, lo: Span) -> PResult<'a, ()> { self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; let span = lo.to(self.prev_token.span); - let (sugg, snippet) = if let Ok(snippet) = self.span_to_snippet(lt_span) { - (Some(span), snippet) - } else { - (None, String::new()) - }; + let sugg = errors::RemoveParens { lo, hi: self.prev_token.span }; - self.dcx().emit_err(errors::ParenthesizedLifetime { span, sugg, snippet }); + self.dcx().emit_err(errors::ParenthesizedLifetime { span, sugg }); Ok(()) } -- cgit 1.4.1-3-g733a5 From b5f94c61f72cc85151dbd9bb1081fde2ab78f806 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 11 Jul 2024 22:03:08 +0000 Subject: Use more accurate span for `:` to `::` suggestion --- compiler/rustc_parse/src/errors.rs | 4 +++- compiler/rustc_parse/src/parser/path.rs | 2 ++ tests/ui/generics/single-colon-path-not-const-generics.stderr | 2 +- tests/ui/suggestions/type-ascription-instead-of-method.stderr | 2 +- tests/ui/suggestions/type-ascription-instead-of-path.stderr | 2 +- tests/ui/suggestions/type-ascription-instead-of-variant.stderr | 2 +- tests/ui/type/ascription/issue-47666.stderr | 2 +- tests/ui/type/ascription/issue-54516.stderr | 2 +- tests/ui/type/ascription/issue-60933.stderr | 2 +- 9 files changed, 12 insertions(+), 8 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index d2a765247ad..18a3153bcd4 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1562,9 +1562,11 @@ pub(crate) struct ExpectedFnPathFoundFnKeyword { #[diag(parse_path_single_colon)] pub(crate) struct PathSingleColon { #[primary_span] - #[suggestion(applicability = "machine-applicable", code = "::", style = "verbose")] pub span: Span, + #[suggestion(applicability = "machine-applicable", code = ":", style = "verbose")] + pub suggestion: Span, + #[note(parse_type_ascription_removed)] pub type_ascription: Option<()>, } diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 03c647dd527..b9014dea726 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -258,6 +258,7 @@ impl<'a> Parser<'a> { self.bump(); // bump past the colon self.dcx().emit_err(PathSingleColon { span: self.prev_token.span, + suggestion: self.prev_token.span.shrink_to_hi(), type_ascription: self .psess .unstable_features @@ -329,6 +330,7 @@ impl<'a> Parser<'a> { err.cancel(); err = self.dcx().create_err(PathSingleColon { span: self.token.span, + suggestion: self.prev_token.span.shrink_to_hi(), type_ascription: self .psess .unstable_features diff --git a/tests/ui/generics/single-colon-path-not-const-generics.stderr b/tests/ui/generics/single-colon-path-not-const-generics.stderr index 06203b5e6fe..4e695b2dcd6 100644 --- a/tests/ui/generics/single-colon-path-not-const-generics.stderr +++ b/tests/ui/generics/single-colon-path-not-const-generics.stderr @@ -10,7 +10,7 @@ LL | a: Vec, help: use a double colon instead | LL | a: Vec, - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/suggestions/type-ascription-instead-of-method.stderr b/tests/ui/suggestions/type-ascription-instead-of-method.stderr index 06f806b9016..0bef1c185db 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-method.stderr +++ b/tests/ui/suggestions/type-ascription-instead-of-method.stderr @@ -8,7 +8,7 @@ LL | let _ = Box:new("foo".to_string()); help: use a double colon instead | LL | let _ = Box::new("foo".to_string()); - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/suggestions/type-ascription-instead-of-path.stderr b/tests/ui/suggestions/type-ascription-instead-of-path.stderr index 9b09a60c161..8c16acff799 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-path.stderr +++ b/tests/ui/suggestions/type-ascription-instead-of-path.stderr @@ -8,7 +8,7 @@ LL | std:io::stdin(); help: use a double colon instead | LL | std::io::stdin(); - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/suggestions/type-ascription-instead-of-variant.stderr b/tests/ui/suggestions/type-ascription-instead-of-variant.stderr index 0ecdc82b541..f0b31722e40 100644 --- a/tests/ui/suggestions/type-ascription-instead-of-variant.stderr +++ b/tests/ui/suggestions/type-ascription-instead-of-variant.stderr @@ -8,7 +8,7 @@ LL | let _ = Option:Some(""); help: use a double colon instead | LL | let _ = Option::Some(""); - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/type/ascription/issue-47666.stderr b/tests/ui/type/ascription/issue-47666.stderr index d76d591ec33..fd825e86675 100644 --- a/tests/ui/type/ascription/issue-47666.stderr +++ b/tests/ui/type/ascription/issue-47666.stderr @@ -8,7 +8,7 @@ LL | let _ = Option:Some(vec![0, 1]); help: use a double colon instead | LL | let _ = Option::Some(vec![0, 1]); - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/type/ascription/issue-54516.stderr b/tests/ui/type/ascription/issue-54516.stderr index bea2c0d85b3..64fdc1fa24a 100644 --- a/tests/ui/type/ascription/issue-54516.stderr +++ b/tests/ui/type/ascription/issue-54516.stderr @@ -8,7 +8,7 @@ LL | println!("{}", std::mem:size_of::>()); help: use a double colon instead | LL | println!("{}", std::mem::size_of::>()); - | ~~ + | + error: aborting due to 1 previous error diff --git a/tests/ui/type/ascription/issue-60933.stderr b/tests/ui/type/ascription/issue-60933.stderr index a81238bd540..c68394d0504 100644 --- a/tests/ui/type/ascription/issue-60933.stderr +++ b/tests/ui/type/ascription/issue-60933.stderr @@ -8,7 +8,7 @@ LL | let _: usize = std::mem:size_of::(); help: use a double colon instead | LL | let _: usize = std::mem::size_of::(); - | ~~ + | + error: aborting due to 1 previous error -- cgit 1.4.1-3-g733a5 From 377d14be88200d122f699aa989e4d1256f2173d2 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 11 Jul 2024 22:07:11 +0000 Subject: More accurate incorrect use of `await` suggestion --- compiler/rustc_parse/messages.ftl | 6 +- compiler/rustc_parse/src/errors.rs | 23 ++++--- compiler/rustc_parse/src/parser/diagnostics.rs | 18 ++--- .../incorrect-syntax-suggestions.stderr | 80 +++++++++++++--------- tests/ui/parser/issues/issue-113203.stderr | 5 +- 5 files changed, 76 insertions(+), 56 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/messages.ftl b/compiler/rustc_parse/messages.ftl index fd96d95bc2e..c2201b1c41e 100644 --- a/compiler/rustc_parse/messages.ftl +++ b/compiler/rustc_parse/messages.ftl @@ -323,10 +323,10 @@ parse_incorrect_semicolon = .suggestion = remove this semicolon .help = {$name} declarations are not followed by a semicolon -parse_incorrect_use_of_await = - incorrect use of `await` +parse_incorrect_use_of_await = incorrect use of `await` .parentheses_suggestion = `await` is not a method call, remove the parentheses - .postfix_suggestion = `await` is a postfix operation + +parse_incorrect_use_of_await_postfix_suggestion = `await` is a postfix operation parse_incorrect_visibility_restriction = incorrect visibility restriction .help = some possible visibility restrictions are: diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 18a3153bcd4..eee17a73acf 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -103,19 +103,26 @@ pub(crate) struct IncorrectUseOfAwait { pub span: Span, } +#[derive(Subdiagnostic)] +#[multipart_suggestion( + parse_incorrect_use_of_await_postfix_suggestion, + applicability = "machine-applicable" +)] +pub(crate) struct AwaitSuggestion { + #[suggestion_part(code = "")] + pub removal: Span, + #[suggestion_part(code = ".await{question_mark}")] + pub dot_await: Span, + pub question_mark: &'static str, +} + #[derive(Diagnostic)] #[diag(parse_incorrect_use_of_await)] pub(crate) struct IncorrectAwait { #[primary_span] pub span: Span, - #[suggestion( - parse_postfix_suggestion, - style = "verbose", - code = "{expr}.await{question_mark}" - )] - pub sugg_span: (Span, Applicability), - pub expr: String, - pub question_mark: &'static str, + #[subdiagnostic] + pub suggestion: AwaitSuggestion, } #[derive(Diagnostic)] diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 63e4d8a4352..39f1d4e89ec 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -3,8 +3,8 @@ use super::{ BlockMode, CommaRecoveryMode, Parser, PathStyle, Restrictions, SemiColonMode, SeqSep, TokenType, }; use crate::errors::{ - AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, BadQPathStage2, - BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained, + AddParen, AmbiguousPlus, AsyncMoveBlockIn2015, AttributeOnParamType, AwaitSuggestion, + BadQPathStage2, BadTypePlus, BadTypePlusSub, ColonAsSemi, ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg, ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, @@ -1959,18 +1959,14 @@ impl<'a> Parser<'a> { is_question: bool, ) -> (Span, ErrorGuaranteed) { let span = lo.to(hi); - let applicability = match expr.kind { - ExprKind::Try(_) => Applicability::MaybeIncorrect, // `await ?` - _ => Applicability::MachineApplicable, - }; - let guar = self.dcx().emit_err(IncorrectAwait { span, - sugg_span: (span, applicability), - expr: self.span_to_snippet(expr.span).unwrap_or_else(|_| pprust::expr_to_string(expr)), - question_mark: if is_question { "?" } else { "" }, + suggestion: AwaitSuggestion { + removal: lo.until(expr.span), + dot_await: expr.span.shrink_to_hi(), + question_mark: if is_question { "?" } else { "" }, + }, }); - (span, guar) } diff --git a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr index ac1bc693fab..0ccde7d8709 100644 --- a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr +++ b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr @@ -6,8 +6,9 @@ LL | let _ = await bar(); | help: `await` is a postfix operation | -LL | let _ = bar().await; - | ~~~~~~~~~~~ +LL - let _ = await bar(); +LL + let _ = bar().await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:12:13 @@ -17,8 +18,9 @@ LL | let _ = await? bar(); | help: `await` is a postfix operation | -LL | let _ = bar().await?; - | ~~~~~~~~~~~~ +LL - let _ = await? bar(); +LL + let _ = bar().await?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:16:13 @@ -28,8 +30,9 @@ LL | let _ = await bar()?; | help: `await` is a postfix operation | -LL | let _ = bar()?.await; - | ~~~~~~~~~~~~ +LL - let _ = await bar()?; +LL + let _ = bar()?.await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:20:13 @@ -39,8 +42,9 @@ LL | let _ = await { bar() }; | help: `await` is a postfix operation | -LL | let _ = { bar() }.await; - | ~~~~~~~~~~~~~~~ +LL - let _ = await { bar() }; +LL + let _ = { bar() }.await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:24:13 @@ -50,8 +54,9 @@ LL | let _ = await(bar()); | help: `await` is a postfix operation | -LL | let _ = (bar()).await; - | ~~~~~~~~~~~~~ +LL - let _ = await(bar()); +LL + let _ = (bar()).await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:28:13 @@ -61,8 +66,9 @@ LL | let _ = await { bar() }?; | help: `await` is a postfix operation | -LL | let _ = { bar() }.await?; - | ~~~~~~~~~~~~~~~ +LL - let _ = await { bar() }?; +LL + let _ = { bar() }.await?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:32:14 @@ -72,8 +78,9 @@ LL | let _ = (await bar())?; | help: `await` is a postfix operation | -LL | let _ = (bar().await)?; - | ~~~~~~~~~~~ +LL - let _ = (await bar())?; +LL + let _ = (bar().await)?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:36:24 @@ -107,8 +114,9 @@ LL | let _ = await bar(); | help: `await` is a postfix operation | -LL | let _ = bar().await; - | ~~~~~~~~~~~ +LL - let _ = await bar(); +LL + let _ = bar().await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:56:13 @@ -118,8 +126,9 @@ LL | let _ = await? bar(); | help: `await` is a postfix operation | -LL | let _ = bar().await?; - | ~~~~~~~~~~~~ +LL - let _ = await? bar(); +LL + let _ = bar().await?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:60:13 @@ -129,8 +138,9 @@ LL | let _ = await bar()?; | help: `await` is a postfix operation | -LL | let _ = bar()?.await; - | ~~~~~~~~~~~~ +LL - let _ = await bar()?; +LL + let _ = bar()?.await; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:64:14 @@ -140,8 +150,9 @@ LL | let _ = (await bar())?; | help: `await` is a postfix operation | -LL | let _ = (bar().await)?; - | ~~~~~~~~~~~ +LL - let _ = (await bar())?; +LL + let _ = (bar().await)?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:68:24 @@ -175,8 +186,9 @@ LL | let _ = await!(bar()); | help: `await` is a postfix operation | -LL | let _ = bar().await; - | ~~~~~~~~~~~ +LL - let _ = await!(bar()); +LL + let _ = bar().await); + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:105:13 @@ -186,8 +198,9 @@ LL | let _ = await!(bar())?; | help: `await` is a postfix operation | -LL | let _ = bar().await?; - | ~~~~~~~~~~~ +LL - let _ = await!(bar())?; +LL + let _ = bar().await)?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:110:17 @@ -197,8 +210,9 @@ LL | let _ = await!(bar())?; | help: `await` is a postfix operation | -LL | let _ = bar().await?; - | ~~~~~~~~~~~ +LL - let _ = await!(bar())?; +LL + let _ = bar().await)?; + | error: incorrect use of `await` --> $DIR/incorrect-syntax-suggestions.rs:117:17 @@ -208,8 +222,9 @@ LL | let _ = await!(bar())?; | help: `await` is a postfix operation | -LL | let _ = bar().await?; - | ~~~~~~~~~~~ +LL - let _ = await!(bar())?; +LL + let _ = bar().await)?; + | error: expected expression, found `=>` --> $DIR/incorrect-syntax-suggestions.rs:124:25 @@ -227,8 +242,9 @@ LL | match await { await => () } | help: `await` is a postfix operation | -LL | match { await => () }.await - | ~~~~~~~~~~~~~~~~~~~~~ +LL - match await { await => () } +LL + match { await => () }.await + | error: expected one of `.`, `?`, `{`, or an operator, found `}` --> $DIR/incorrect-syntax-suggestions.rs:127:1 diff --git a/tests/ui/parser/issues/issue-113203.stderr b/tests/ui/parser/issues/issue-113203.stderr index f9c8ad91280..1ef20ddf726 100644 --- a/tests/ui/parser/issues/issue-113203.stderr +++ b/tests/ui/parser/issues/issue-113203.stderr @@ -6,8 +6,9 @@ LL | await {}() | help: `await` is a postfix operation | -LL | {}.await() - | ~~~~~~~~ +LL - await {}() +LL + {}.await() + | error: aborting due to 1 previous error -- cgit 1.4.1-3-g733a5 From b6f518877f910aae9e4a22f2df4fe55ea838cd72 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Fri, 12 Jul 2024 03:04:06 +0000 Subject: fix unused binding --- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 39f1d4e89ec..0da7fefe6ed 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1610,7 +1610,7 @@ impl<'a> Parser<'a> { } self.bump(); // `+` - let bounds = self.parse_generic_bounds()?; + let _bounds = self.parse_generic_bounds()?; let sum_span = ty.span.to(self.prev_token.span); let sub = match &ty.kind { -- cgit 1.4.1-3-g733a5