From f830fe313ba8b23fe882589ba8dcbbd5739137e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 14 Nov 2023 00:46:37 +0000 Subject: [PATCH 01/10] Detect more `=>` typos Handle and recover `match expr { pat >= { arm } }`. --- compiler/rustc_ast/src/token.rs | 3 ++- compiler/rustc_parse/src/parser/expr.rs | 19 +++++++------- .../issues/recover-ge-as-fat-arrow.fixed | 7 ++++++ .../parser/issues/recover-ge-as-fat-arrow.rs | 7 ++++++ .../issues/recover-ge-as-fat-arrow.stderr | 25 +++++++++++++++++++ 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed create mode 100644 tests/ui/parser/issues/recover-ge-as-fat-arrow.rs create mode 100644 tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index a6ee93e8a6bac..707238e6c0275 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -388,7 +388,8 @@ impl TokenKind { match *self { Comma => Some(vec![Dot, Lt, Semi]), Semi => Some(vec![Colon, Comma]), - FatArrow => Some(vec![Eq, RArrow]), + Colon => Some(vec![Semi]), + FatArrow => Some(vec![Eq, RArrow, Ge, Gt]), _ => None, } } diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 235b28b6e26e2..bfd7e8ef4d0ab 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2904,15 +2904,16 @@ impl<'a> Parser<'a> { "=>", Applicability::MachineApplicable, ); - err.emit(); - this.bump(); - } else if matches!( - (&this.prev_token.kind, &this.token.kind), - (token::DotDotEq, token::Gt) - ) { - // `error_inclusive_range_match_arrow` handles cases like `0..=> {}`, - // so we suppress the error here - err.delay_as_bug(); + if matches!( + (&this.prev_token.kind, &this.token.kind), + (token::DotDotEq, token::Gt) + ) { + // `error_inclusive_range_match_arrow` handles cases like `0..=> {}`, + // so we suppress the error here + err.delay_as_bug(); + } else { + err.emit(); + } this.bump(); } else { return Err(err); diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed b/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed new file mode 100644 index 0000000000000..7b73dfb02df89 --- /dev/null +++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed @@ -0,0 +1,7 @@ +// run-rustfix +fn main() { + match 1 { + 1 => {} //~ ERROR + _ => { let _: u16 = 2u16; } //~ ERROR + } +} diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs b/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs new file mode 100644 index 0000000000000..92143fcf3f767 --- /dev/null +++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs @@ -0,0 +1,7 @@ +// run-rustfix +fn main() { + match 1 { + 1 >= {} //~ ERROR + _ => { let _: u16 = 2u8; } //~ ERROR + } +} diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr b/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr new file mode 100644 index 0000000000000..2df5cca24f06c --- /dev/null +++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr @@ -0,0 +1,25 @@ +error: expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`, found `>=` + --> $DIR/recover-ge-as-fat-arrow.rs:4:11 + | +LL | 1 >= {} + | ^^ + | | + | expected one of `...`, `..=`, `..`, `=>`, `if`, or `|` + | help: use a fat arrow to start a match arm: `=>` + +error[E0308]: mismatched types + --> $DIR/recover-ge-as-fat-arrow.rs:5:29 + | +LL | _ => { let _: u16 = 2u8; } + | --- ^^^ expected `u16`, found `u8` + | | + | expected due to this + | +help: change the type of the numeric literal from `u8` to `u16` + | +LL | _ => { let _: u16 = 2u16; } + | ~~~ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. From a7e035ab899be54fda12c6bae6ef7ecbcce0aaea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 16 Nov 2023 01:41:58 +0000 Subject: [PATCH 02/10] Point at impl self ty on type error involving `Self` When encountering a type error involving a `Self` literal, point at the self type of the enclosing `impl`. CC #76086. --- compiler/rustc_hir_typeck/src/coercion.rs | 38 +++++++++++++++++++ .../struct-path-self-type-mismatch.stderr | 4 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 6c03bc3b57ac6..58f4e07e9446e 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1738,6 +1738,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // label pointing out the cause for the type coercion will be wrong // as prior return coercions would not be relevant (#57664). let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) { + self.explain_self_literal(fcx, &mut err, expr); let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id); if let (Some(cond_expr), true, false) = ( @@ -1810,6 +1811,43 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { err } + fn explain_self_literal( + &self, + fcx: &FnCtxt<'_, 'tcx>, + err: &mut Diagnostic, + expr: &'tcx hir::Expr<'tcx>, + ) { + match expr.peel_drop_temps().kind { + hir::ExprKind::Struct( + hir::QPath::Resolved( + None, + hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, .. }, + ), + .., + ) + | hir::ExprKind::Call( + hir::Expr { + kind: + hir::ExprKind::Path(hir::QPath::Resolved( + None, + hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, .. }, + )), + .. + }, + .., + ) => { + if let Some(hir::Node::Item(hir::Item { + kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }), + .. + })) = fcx.tcx.hir().get_if_local(*alias_to) + { + err.span_label(self_ty.span, "this is the type of the `Self` literal"); + } + } + _ => {} + } + } + /// Checks whether the return type is unsized via an obligation, which makes /// sure we consider `dyn Trait: Sized` where clauses, which are trivially /// false but technically valid for typeck. diff --git a/tests/ui/structs/struct-path-self-type-mismatch.stderr b/tests/ui/structs/struct-path-self-type-mismatch.stderr index cddc1356194bf..f4ca485108307 100644 --- a/tests/ui/structs/struct-path-self-type-mismatch.stderr +++ b/tests/ui/structs/struct-path-self-type-mismatch.stderr @@ -24,7 +24,9 @@ error[E0308]: mismatched types --> $DIR/struct-path-self-type-mismatch.rs:13:9 | LL | impl Foo { - | - found type parameter + | - ------ this is the type of the `Self` literal + | | + | found type parameter LL | fn new(u: U) -> Foo { | - ------ expected `Foo` because of return type | | From d8456855f5d65a2ed26b09a82c750535a09becdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 16 Nov 2023 02:27:55 +0000 Subject: [PATCH 03/10] Suggest replacing `Self` with the right type on type error When encountering a type error caused by the use of `Self`, suggest using the actual type name instead. ``` error[E0308]: mismatched types --> $DIR/struct-path-self-type-mismatch.rs:13:9 | LL | impl Foo { | - ------ this is the type of the `Self` literal | | | found type parameter LL | fn new(u: U) -> Foo { | - ------ expected `Foo` because of return type | | | expected type parameter LL | / Self { LL | | LL | | inner: u LL | | LL | | } | |_________^ expected `Foo`, found `Foo` | = note: expected struct `Foo` found struct `Foo` = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters help: use the type name directly | LL | Foo:: { | ~~~~~~~~ ``` Fix #76086. --- compiler/rustc_hir_typeck/src/coercion.rs | 27 ++++++++++++++++--- .../struct-path-self-type-mismatch.stderr | 4 +++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 58f4e07e9446e..8dbf01e973544 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -36,7 +36,9 @@ //! ``` use crate::FnCtxt; -use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; +use rustc_errors::{ + struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, +}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; @@ -1738,7 +1740,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // label pointing out the cause for the type coercion will be wrong // as prior return coercions would not be relevant (#57664). let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) { - self.explain_self_literal(fcx, &mut err, expr); + self.explain_self_literal(fcx, &mut err, expr, expected, found); let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id); if let (Some(cond_expr), true, false) = ( @@ -1816,12 +1818,14 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { fcx: &FnCtxt<'_, 'tcx>, err: &mut Diagnostic, expr: &'tcx hir::Expr<'tcx>, + expected: Ty<'tcx>, + found: Ty<'tcx>, ) { match expr.peel_drop_temps().kind { hir::ExprKind::Struct( hir::QPath::Resolved( None, - hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, .. }, + hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. }, ), .., ) @@ -1830,7 +1834,11 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { kind: hir::ExprKind::Path(hir::QPath::Resolved( None, - hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, .. }, + hir::Path { + res: hir::def::Res::SelfTyAlias { alias_to, .. }, + span, + .. + }, )), .. }, @@ -1843,6 +1851,17 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { { err.span_label(self_ty.span, "this is the type of the `Self` literal"); } + if let ty::Adt(e_def, e_args) = expected.kind() + && let ty::Adt(f_def, _f_args) = found.kind() + && e_def == f_def + { + err.span_suggestion_verbose( + *span, + "use the type name directly", + fcx.tcx.value_path_str_with_args(*alias_to, e_args), + Applicability::MaybeIncorrect, + ); + } } _ => {} } diff --git a/tests/ui/structs/struct-path-self-type-mismatch.stderr b/tests/ui/structs/struct-path-self-type-mismatch.stderr index f4ca485108307..bbe5bae29bbe1 100644 --- a/tests/ui/structs/struct-path-self-type-mismatch.stderr +++ b/tests/ui/structs/struct-path-self-type-mismatch.stderr @@ -42,6 +42,10 @@ LL | | } found struct `Foo` = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters +help: use the type name directly + | +LL | Foo:: { + | ~~~~~~~~ error: aborting due to 3 previous errors From ae20897b30620731d10c914ec8125f3da91f0f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 6 Nov 2023 19:10:37 +0000 Subject: [PATCH 04/10] Move tests to subdirectory --- tests/ui/parser/{ => recover}/recover-assoc-const-constraint.rs | 0 .../ui/parser/{ => recover}/recover-assoc-const-constraint.stderr | 0 tests/ui/parser/{ => recover}/recover-assoc-eq-missing-term.rs | 0 .../ui/parser/{ => recover}/recover-assoc-eq-missing-term.stderr | 0 .../ui/parser/{ => recover}/recover-assoc-lifetime-constraint.rs | 0 .../parser/{ => recover}/recover-assoc-lifetime-constraint.stderr | 0 tests/ui/parser/{ => recover}/recover-const-async-fn-ptr.rs | 0 tests/ui/parser/{ => recover}/recover-const-async-fn-ptr.stderr | 0 tests/ui/parser/{ => recover}/recover-enum.rs | 0 tests/ui/parser/{ => recover}/recover-enum.stderr | 0 tests/ui/parser/{ => recover}/recover-enum2.rs | 0 tests/ui/parser/{ => recover}/recover-enum2.stderr | 0 .../recover-field-extra-angle-brackets-in-struct-with-a-field.rs | 0 ...cover-field-extra-angle-brackets-in-struct-with-a-field.stderr | 0 .../ui/parser/{ => recover}/recover-field-extra-angle-brackets.rs | 0 .../{ => recover}/recover-field-extra-angle-brackets.stderr | 0 tests/ui/parser/{ => recover}/recover-field-semi.rs | 0 tests/ui/parser/{ => recover}/recover-field-semi.stderr | 0 tests/ui/parser/{ => recover}/recover-fn-ptr-with-generics.rs | 0 tests/ui/parser/{ => recover}/recover-fn-ptr-with-generics.stderr | 0 tests/ui/parser/{ => recover}/recover-fn-trait-from-fn-kw.rs | 0 tests/ui/parser/{ => recover}/recover-fn-trait-from-fn-kw.stderr | 0 .../parser/{ => recover}/recover-for-loop-parens-around-head.rs | 0 .../{ => recover}/recover-for-loop-parens-around-head.stderr | 0 tests/ui/parser/{ => recover}/recover-from-bad-variant.rs | 0 tests/ui/parser/{ => recover}/recover-from-bad-variant.stderr | 0 tests/ui/parser/{ => recover}/recover-from-homoglyph.rs | 0 tests/ui/parser/{ => recover}/recover-from-homoglyph.stderr | 0 .../ui/parser/{ => recover}/recover-labeled-non-block-expr.fixed | 0 tests/ui/parser/{ => recover}/recover-labeled-non-block-expr.rs | 0 .../ui/parser/{ => recover}/recover-labeled-non-block-expr.stderr | 0 .../parser/{ => recover}/recover-missing-semi-before-item.fixed | 0 tests/ui/parser/{ => recover}/recover-missing-semi-before-item.rs | 0 .../parser/{ => recover}/recover-missing-semi-before-item.stderr | 0 tests/ui/parser/{ => recover}/recover-missing-semi.rs | 0 tests/ui/parser/{ => recover}/recover-missing-semi.stderr | 0 tests/ui/parser/{ => recover}/recover-quantified-closure.rs | 0 tests/ui/parser/{ => recover}/recover-quantified-closure.stderr | 0 tests/ui/parser/{ => recover}/recover-range-pats.rs | 0 tests/ui/parser/{ => recover}/recover-range-pats.stderr | 0 tests/ui/parser/{ => recover}/recover-ref-dyn-mut.rs | 0 tests/ui/parser/{ => recover}/recover-ref-dyn-mut.stderr | 0 tests/ui/parser/{ => recover}/recover-struct.rs | 0 tests/ui/parser/{ => recover}/recover-struct.stderr | 0 tests/ui/parser/{ => recover}/recover-tuple-pat.rs | 0 tests/ui/parser/{ => recover}/recover-tuple-pat.stderr | 0 tests/ui/parser/{ => recover}/recover-tuple.rs | 0 tests/ui/parser/{ => recover}/recover-tuple.stderr | 0 tests/ui/parser/{ => recover}/recover-unticked-labels.fixed | 0 tests/ui/parser/{ => recover}/recover-unticked-labels.rs | 0 tests/ui/parser/{ => recover}/recover-unticked-labels.stderr | 0 .../recover-where-clause-before-tuple-struct-body-0.fixed | 0 .../recover-where-clause-before-tuple-struct-body-0.rs | 0 .../recover-where-clause-before-tuple-struct-body-0.stderr | 0 .../recover-where-clause-before-tuple-struct-body-1.rs | 0 .../recover-where-clause-before-tuple-struct-body-1.stderr | 0 56 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/parser/{ => recover}/recover-assoc-const-constraint.rs (100%) rename tests/ui/parser/{ => recover}/recover-assoc-const-constraint.stderr (100%) rename tests/ui/parser/{ => recover}/recover-assoc-eq-missing-term.rs (100%) rename tests/ui/parser/{ => recover}/recover-assoc-eq-missing-term.stderr (100%) rename tests/ui/parser/{ => recover}/recover-assoc-lifetime-constraint.rs (100%) rename tests/ui/parser/{ => recover}/recover-assoc-lifetime-constraint.stderr (100%) rename tests/ui/parser/{ => recover}/recover-const-async-fn-ptr.rs (100%) rename tests/ui/parser/{ => recover}/recover-const-async-fn-ptr.stderr (100%) rename tests/ui/parser/{ => recover}/recover-enum.rs (100%) rename tests/ui/parser/{ => recover}/recover-enum.stderr (100%) rename tests/ui/parser/{ => recover}/recover-enum2.rs (100%) rename tests/ui/parser/{ => recover}/recover-enum2.stderr (100%) rename tests/ui/parser/{ => recover}/recover-field-extra-angle-brackets-in-struct-with-a-field.rs (100%) rename tests/ui/parser/{ => recover}/recover-field-extra-angle-brackets-in-struct-with-a-field.stderr (100%) rename tests/ui/parser/{ => recover}/recover-field-extra-angle-brackets.rs (100%) rename tests/ui/parser/{ => recover}/recover-field-extra-angle-brackets.stderr (100%) rename tests/ui/parser/{ => recover}/recover-field-semi.rs (100%) rename tests/ui/parser/{ => recover}/recover-field-semi.stderr (100%) rename tests/ui/parser/{ => recover}/recover-fn-ptr-with-generics.rs (100%) rename tests/ui/parser/{ => recover}/recover-fn-ptr-with-generics.stderr (100%) rename tests/ui/parser/{ => recover}/recover-fn-trait-from-fn-kw.rs (100%) rename tests/ui/parser/{ => recover}/recover-fn-trait-from-fn-kw.stderr (100%) rename tests/ui/parser/{ => recover}/recover-for-loop-parens-around-head.rs (100%) rename tests/ui/parser/{ => recover}/recover-for-loop-parens-around-head.stderr (100%) rename tests/ui/parser/{ => recover}/recover-from-bad-variant.rs (100%) rename tests/ui/parser/{ => recover}/recover-from-bad-variant.stderr (100%) rename tests/ui/parser/{ => recover}/recover-from-homoglyph.rs (100%) rename tests/ui/parser/{ => recover}/recover-from-homoglyph.stderr (100%) rename tests/ui/parser/{ => recover}/recover-labeled-non-block-expr.fixed (100%) rename tests/ui/parser/{ => recover}/recover-labeled-non-block-expr.rs (100%) rename tests/ui/parser/{ => recover}/recover-labeled-non-block-expr.stderr (100%) rename tests/ui/parser/{ => recover}/recover-missing-semi-before-item.fixed (100%) rename tests/ui/parser/{ => recover}/recover-missing-semi-before-item.rs (100%) rename tests/ui/parser/{ => recover}/recover-missing-semi-before-item.stderr (100%) rename tests/ui/parser/{ => recover}/recover-missing-semi.rs (100%) rename tests/ui/parser/{ => recover}/recover-missing-semi.stderr (100%) rename tests/ui/parser/{ => recover}/recover-quantified-closure.rs (100%) rename tests/ui/parser/{ => recover}/recover-quantified-closure.stderr (100%) rename tests/ui/parser/{ => recover}/recover-range-pats.rs (100%) rename tests/ui/parser/{ => recover}/recover-range-pats.stderr (100%) rename tests/ui/parser/{ => recover}/recover-ref-dyn-mut.rs (100%) rename tests/ui/parser/{ => recover}/recover-ref-dyn-mut.stderr (100%) rename tests/ui/parser/{ => recover}/recover-struct.rs (100%) rename tests/ui/parser/{ => recover}/recover-struct.stderr (100%) rename tests/ui/parser/{ => recover}/recover-tuple-pat.rs (100%) rename tests/ui/parser/{ => recover}/recover-tuple-pat.stderr (100%) rename tests/ui/parser/{ => recover}/recover-tuple.rs (100%) rename tests/ui/parser/{ => recover}/recover-tuple.stderr (100%) rename tests/ui/parser/{ => recover}/recover-unticked-labels.fixed (100%) rename tests/ui/parser/{ => recover}/recover-unticked-labels.rs (100%) rename tests/ui/parser/{ => recover}/recover-unticked-labels.stderr (100%) rename tests/ui/parser/{ => recover}/recover-where-clause-before-tuple-struct-body-0.fixed (100%) rename tests/ui/parser/{ => recover}/recover-where-clause-before-tuple-struct-body-0.rs (100%) rename tests/ui/parser/{ => recover}/recover-where-clause-before-tuple-struct-body-0.stderr (100%) rename tests/ui/parser/{ => recover}/recover-where-clause-before-tuple-struct-body-1.rs (100%) rename tests/ui/parser/{ => recover}/recover-where-clause-before-tuple-struct-body-1.stderr (100%) diff --git a/tests/ui/parser/recover-assoc-const-constraint.rs b/tests/ui/parser/recover/recover-assoc-const-constraint.rs similarity index 100% rename from tests/ui/parser/recover-assoc-const-constraint.rs rename to tests/ui/parser/recover/recover-assoc-const-constraint.rs diff --git a/tests/ui/parser/recover-assoc-const-constraint.stderr b/tests/ui/parser/recover/recover-assoc-const-constraint.stderr similarity index 100% rename from tests/ui/parser/recover-assoc-const-constraint.stderr rename to tests/ui/parser/recover/recover-assoc-const-constraint.stderr diff --git a/tests/ui/parser/recover-assoc-eq-missing-term.rs b/tests/ui/parser/recover/recover-assoc-eq-missing-term.rs similarity index 100% rename from tests/ui/parser/recover-assoc-eq-missing-term.rs rename to tests/ui/parser/recover/recover-assoc-eq-missing-term.rs diff --git a/tests/ui/parser/recover-assoc-eq-missing-term.stderr b/tests/ui/parser/recover/recover-assoc-eq-missing-term.stderr similarity index 100% rename from tests/ui/parser/recover-assoc-eq-missing-term.stderr rename to tests/ui/parser/recover/recover-assoc-eq-missing-term.stderr diff --git a/tests/ui/parser/recover-assoc-lifetime-constraint.rs b/tests/ui/parser/recover/recover-assoc-lifetime-constraint.rs similarity index 100% rename from tests/ui/parser/recover-assoc-lifetime-constraint.rs rename to tests/ui/parser/recover/recover-assoc-lifetime-constraint.rs diff --git a/tests/ui/parser/recover-assoc-lifetime-constraint.stderr b/tests/ui/parser/recover/recover-assoc-lifetime-constraint.stderr similarity index 100% rename from tests/ui/parser/recover-assoc-lifetime-constraint.stderr rename to tests/ui/parser/recover/recover-assoc-lifetime-constraint.stderr diff --git a/tests/ui/parser/recover-const-async-fn-ptr.rs b/tests/ui/parser/recover/recover-const-async-fn-ptr.rs similarity index 100% rename from tests/ui/parser/recover-const-async-fn-ptr.rs rename to tests/ui/parser/recover/recover-const-async-fn-ptr.rs diff --git a/tests/ui/parser/recover-const-async-fn-ptr.stderr b/tests/ui/parser/recover/recover-const-async-fn-ptr.stderr similarity index 100% rename from tests/ui/parser/recover-const-async-fn-ptr.stderr rename to tests/ui/parser/recover/recover-const-async-fn-ptr.stderr diff --git a/tests/ui/parser/recover-enum.rs b/tests/ui/parser/recover/recover-enum.rs similarity index 100% rename from tests/ui/parser/recover-enum.rs rename to tests/ui/parser/recover/recover-enum.rs diff --git a/tests/ui/parser/recover-enum.stderr b/tests/ui/parser/recover/recover-enum.stderr similarity index 100% rename from tests/ui/parser/recover-enum.stderr rename to tests/ui/parser/recover/recover-enum.stderr diff --git a/tests/ui/parser/recover-enum2.rs b/tests/ui/parser/recover/recover-enum2.rs similarity index 100% rename from tests/ui/parser/recover-enum2.rs rename to tests/ui/parser/recover/recover-enum2.rs diff --git a/tests/ui/parser/recover-enum2.stderr b/tests/ui/parser/recover/recover-enum2.stderr similarity index 100% rename from tests/ui/parser/recover-enum2.stderr rename to tests/ui/parser/recover/recover-enum2.stderr diff --git a/tests/ui/parser/recover-field-extra-angle-brackets-in-struct-with-a-field.rs b/tests/ui/parser/recover/recover-field-extra-angle-brackets-in-struct-with-a-field.rs similarity index 100% rename from tests/ui/parser/recover-field-extra-angle-brackets-in-struct-with-a-field.rs rename to tests/ui/parser/recover/recover-field-extra-angle-brackets-in-struct-with-a-field.rs diff --git a/tests/ui/parser/recover-field-extra-angle-brackets-in-struct-with-a-field.stderr b/tests/ui/parser/recover/recover-field-extra-angle-brackets-in-struct-with-a-field.stderr similarity index 100% rename from tests/ui/parser/recover-field-extra-angle-brackets-in-struct-with-a-field.stderr rename to tests/ui/parser/recover/recover-field-extra-angle-brackets-in-struct-with-a-field.stderr diff --git a/tests/ui/parser/recover-field-extra-angle-brackets.rs b/tests/ui/parser/recover/recover-field-extra-angle-brackets.rs similarity index 100% rename from tests/ui/parser/recover-field-extra-angle-brackets.rs rename to tests/ui/parser/recover/recover-field-extra-angle-brackets.rs diff --git a/tests/ui/parser/recover-field-extra-angle-brackets.stderr b/tests/ui/parser/recover/recover-field-extra-angle-brackets.stderr similarity index 100% rename from tests/ui/parser/recover-field-extra-angle-brackets.stderr rename to tests/ui/parser/recover/recover-field-extra-angle-brackets.stderr diff --git a/tests/ui/parser/recover-field-semi.rs b/tests/ui/parser/recover/recover-field-semi.rs similarity index 100% rename from tests/ui/parser/recover-field-semi.rs rename to tests/ui/parser/recover/recover-field-semi.rs diff --git a/tests/ui/parser/recover-field-semi.stderr b/tests/ui/parser/recover/recover-field-semi.stderr similarity index 100% rename from tests/ui/parser/recover-field-semi.stderr rename to tests/ui/parser/recover/recover-field-semi.stderr diff --git a/tests/ui/parser/recover-fn-ptr-with-generics.rs b/tests/ui/parser/recover/recover-fn-ptr-with-generics.rs similarity index 100% rename from tests/ui/parser/recover-fn-ptr-with-generics.rs rename to tests/ui/parser/recover/recover-fn-ptr-with-generics.rs diff --git a/tests/ui/parser/recover-fn-ptr-with-generics.stderr b/tests/ui/parser/recover/recover-fn-ptr-with-generics.stderr similarity index 100% rename from tests/ui/parser/recover-fn-ptr-with-generics.stderr rename to tests/ui/parser/recover/recover-fn-ptr-with-generics.stderr diff --git a/tests/ui/parser/recover-fn-trait-from-fn-kw.rs b/tests/ui/parser/recover/recover-fn-trait-from-fn-kw.rs similarity index 100% rename from tests/ui/parser/recover-fn-trait-from-fn-kw.rs rename to tests/ui/parser/recover/recover-fn-trait-from-fn-kw.rs diff --git a/tests/ui/parser/recover-fn-trait-from-fn-kw.stderr b/tests/ui/parser/recover/recover-fn-trait-from-fn-kw.stderr similarity index 100% rename from tests/ui/parser/recover-fn-trait-from-fn-kw.stderr rename to tests/ui/parser/recover/recover-fn-trait-from-fn-kw.stderr diff --git a/tests/ui/parser/recover-for-loop-parens-around-head.rs b/tests/ui/parser/recover/recover-for-loop-parens-around-head.rs similarity index 100% rename from tests/ui/parser/recover-for-loop-parens-around-head.rs rename to tests/ui/parser/recover/recover-for-loop-parens-around-head.rs diff --git a/tests/ui/parser/recover-for-loop-parens-around-head.stderr b/tests/ui/parser/recover/recover-for-loop-parens-around-head.stderr similarity index 100% rename from tests/ui/parser/recover-for-loop-parens-around-head.stderr rename to tests/ui/parser/recover/recover-for-loop-parens-around-head.stderr diff --git a/tests/ui/parser/recover-from-bad-variant.rs b/tests/ui/parser/recover/recover-from-bad-variant.rs similarity index 100% rename from tests/ui/parser/recover-from-bad-variant.rs rename to tests/ui/parser/recover/recover-from-bad-variant.rs diff --git a/tests/ui/parser/recover-from-bad-variant.stderr b/tests/ui/parser/recover/recover-from-bad-variant.stderr similarity index 100% rename from tests/ui/parser/recover-from-bad-variant.stderr rename to tests/ui/parser/recover/recover-from-bad-variant.stderr diff --git a/tests/ui/parser/recover-from-homoglyph.rs b/tests/ui/parser/recover/recover-from-homoglyph.rs similarity index 100% rename from tests/ui/parser/recover-from-homoglyph.rs rename to tests/ui/parser/recover/recover-from-homoglyph.rs diff --git a/tests/ui/parser/recover-from-homoglyph.stderr b/tests/ui/parser/recover/recover-from-homoglyph.stderr similarity index 100% rename from tests/ui/parser/recover-from-homoglyph.stderr rename to tests/ui/parser/recover/recover-from-homoglyph.stderr diff --git a/tests/ui/parser/recover-labeled-non-block-expr.fixed b/tests/ui/parser/recover/recover-labeled-non-block-expr.fixed similarity index 100% rename from tests/ui/parser/recover-labeled-non-block-expr.fixed rename to tests/ui/parser/recover/recover-labeled-non-block-expr.fixed diff --git a/tests/ui/parser/recover-labeled-non-block-expr.rs b/tests/ui/parser/recover/recover-labeled-non-block-expr.rs similarity index 100% rename from tests/ui/parser/recover-labeled-non-block-expr.rs rename to tests/ui/parser/recover/recover-labeled-non-block-expr.rs diff --git a/tests/ui/parser/recover-labeled-non-block-expr.stderr b/tests/ui/parser/recover/recover-labeled-non-block-expr.stderr similarity index 100% rename from tests/ui/parser/recover-labeled-non-block-expr.stderr rename to tests/ui/parser/recover/recover-labeled-non-block-expr.stderr diff --git a/tests/ui/parser/recover-missing-semi-before-item.fixed b/tests/ui/parser/recover/recover-missing-semi-before-item.fixed similarity index 100% rename from tests/ui/parser/recover-missing-semi-before-item.fixed rename to tests/ui/parser/recover/recover-missing-semi-before-item.fixed diff --git a/tests/ui/parser/recover-missing-semi-before-item.rs b/tests/ui/parser/recover/recover-missing-semi-before-item.rs similarity index 100% rename from tests/ui/parser/recover-missing-semi-before-item.rs rename to tests/ui/parser/recover/recover-missing-semi-before-item.rs diff --git a/tests/ui/parser/recover-missing-semi-before-item.stderr b/tests/ui/parser/recover/recover-missing-semi-before-item.stderr similarity index 100% rename from tests/ui/parser/recover-missing-semi-before-item.stderr rename to tests/ui/parser/recover/recover-missing-semi-before-item.stderr diff --git a/tests/ui/parser/recover-missing-semi.rs b/tests/ui/parser/recover/recover-missing-semi.rs similarity index 100% rename from tests/ui/parser/recover-missing-semi.rs rename to tests/ui/parser/recover/recover-missing-semi.rs diff --git a/tests/ui/parser/recover-missing-semi.stderr b/tests/ui/parser/recover/recover-missing-semi.stderr similarity index 100% rename from tests/ui/parser/recover-missing-semi.stderr rename to tests/ui/parser/recover/recover-missing-semi.stderr diff --git a/tests/ui/parser/recover-quantified-closure.rs b/tests/ui/parser/recover/recover-quantified-closure.rs similarity index 100% rename from tests/ui/parser/recover-quantified-closure.rs rename to tests/ui/parser/recover/recover-quantified-closure.rs diff --git a/tests/ui/parser/recover-quantified-closure.stderr b/tests/ui/parser/recover/recover-quantified-closure.stderr similarity index 100% rename from tests/ui/parser/recover-quantified-closure.stderr rename to tests/ui/parser/recover/recover-quantified-closure.stderr diff --git a/tests/ui/parser/recover-range-pats.rs b/tests/ui/parser/recover/recover-range-pats.rs similarity index 100% rename from tests/ui/parser/recover-range-pats.rs rename to tests/ui/parser/recover/recover-range-pats.rs diff --git a/tests/ui/parser/recover-range-pats.stderr b/tests/ui/parser/recover/recover-range-pats.stderr similarity index 100% rename from tests/ui/parser/recover-range-pats.stderr rename to tests/ui/parser/recover/recover-range-pats.stderr diff --git a/tests/ui/parser/recover-ref-dyn-mut.rs b/tests/ui/parser/recover/recover-ref-dyn-mut.rs similarity index 100% rename from tests/ui/parser/recover-ref-dyn-mut.rs rename to tests/ui/parser/recover/recover-ref-dyn-mut.rs diff --git a/tests/ui/parser/recover-ref-dyn-mut.stderr b/tests/ui/parser/recover/recover-ref-dyn-mut.stderr similarity index 100% rename from tests/ui/parser/recover-ref-dyn-mut.stderr rename to tests/ui/parser/recover/recover-ref-dyn-mut.stderr diff --git a/tests/ui/parser/recover-struct.rs b/tests/ui/parser/recover/recover-struct.rs similarity index 100% rename from tests/ui/parser/recover-struct.rs rename to tests/ui/parser/recover/recover-struct.rs diff --git a/tests/ui/parser/recover-struct.stderr b/tests/ui/parser/recover/recover-struct.stderr similarity index 100% rename from tests/ui/parser/recover-struct.stderr rename to tests/ui/parser/recover/recover-struct.stderr diff --git a/tests/ui/parser/recover-tuple-pat.rs b/tests/ui/parser/recover/recover-tuple-pat.rs similarity index 100% rename from tests/ui/parser/recover-tuple-pat.rs rename to tests/ui/parser/recover/recover-tuple-pat.rs diff --git a/tests/ui/parser/recover-tuple-pat.stderr b/tests/ui/parser/recover/recover-tuple-pat.stderr similarity index 100% rename from tests/ui/parser/recover-tuple-pat.stderr rename to tests/ui/parser/recover/recover-tuple-pat.stderr diff --git a/tests/ui/parser/recover-tuple.rs b/tests/ui/parser/recover/recover-tuple.rs similarity index 100% rename from tests/ui/parser/recover-tuple.rs rename to tests/ui/parser/recover/recover-tuple.rs diff --git a/tests/ui/parser/recover-tuple.stderr b/tests/ui/parser/recover/recover-tuple.stderr similarity index 100% rename from tests/ui/parser/recover-tuple.stderr rename to tests/ui/parser/recover/recover-tuple.stderr diff --git a/tests/ui/parser/recover-unticked-labels.fixed b/tests/ui/parser/recover/recover-unticked-labels.fixed similarity index 100% rename from tests/ui/parser/recover-unticked-labels.fixed rename to tests/ui/parser/recover/recover-unticked-labels.fixed diff --git a/tests/ui/parser/recover-unticked-labels.rs b/tests/ui/parser/recover/recover-unticked-labels.rs similarity index 100% rename from tests/ui/parser/recover-unticked-labels.rs rename to tests/ui/parser/recover/recover-unticked-labels.rs diff --git a/tests/ui/parser/recover-unticked-labels.stderr b/tests/ui/parser/recover/recover-unticked-labels.stderr similarity index 100% rename from tests/ui/parser/recover-unticked-labels.stderr rename to tests/ui/parser/recover/recover-unticked-labels.stderr diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed similarity index 100% rename from tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed rename to tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.fixed diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs similarity index 100% rename from tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs rename to tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.rs diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr similarity index 100% rename from tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr rename to tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-0.stderr diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-1.rs similarity index 100% rename from tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs rename to tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-1.rs diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr b/tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-1.stderr similarity index 100% rename from tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr rename to tests/ui/parser/recover/recover-where-clause-before-tuple-struct-body-1.stderr From 1c6bd0b12b7b0017beaa8e39c48f999d20c0ad8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 6 Nov 2023 19:47:38 +0000 Subject: [PATCH 05/10] Smaller span for unnessary `mut` suggestion --- compiler/rustc_parse/src/errors.rs | 3 +-- compiler/rustc_parse/src/parser/pat.rs | 10 +++++----- tests/ui/parser/issues/issue-32501.stderr | 2 +- .../issue-65122-mac-invoc-in-mut-patterns.stderr | 4 ++-- tests/ui/parser/mut-patterns.stderr | 4 ++-- tests/ui/self/self_type_keyword.stderr | 2 +- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 8ab1ec298a1c7..99e66fddc705b 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -2278,9 +2278,8 @@ pub(crate) enum InvalidMutInPattern { #[note(parse_note_mut_pattern_usage)] NonIdent { #[primary_span] - #[suggestion(code = "{pat}", applicability = "machine-applicable")] + #[suggestion(code = "", applicability = "machine-applicable")] span: Span, - pat: String, }, } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 0a4c7c17d06a0..f6e1a21bd2607 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -638,13 +638,13 @@ impl<'a> Parser<'a> { /// Error on `mut $pat` where `$pat` is not an ident. fn ban_mut_general_pat(&self, lo: Span, pat: &Pat, changed_any_binding: bool) { - let span = lo.to(pat.span); - let pat = pprust::pat_to_string(&pat); - self.sess.emit_err(if changed_any_binding { - InvalidMutInPattern::NestedIdent { span, pat } + InvalidMutInPattern::NestedIdent { + span: lo.to(pat.span), + pat: pprust::pat_to_string(&pat), + } } else { - InvalidMutInPattern::NonIdent { span, pat } + InvalidMutInPattern::NonIdent { span: lo.until(pat.span) } }); } diff --git a/tests/ui/parser/issues/issue-32501.stderr b/tests/ui/parser/issues/issue-32501.stderr index d53302449a806..df12f7768d421 100644 --- a/tests/ui/parser/issues/issue-32501.stderr +++ b/tests/ui/parser/issues/issue-32501.stderr @@ -2,7 +2,7 @@ error: `mut` must be followed by a named binding --> $DIR/issue-32501.rs:7:9 | LL | let mut _ = 0; - | ^^^^^ help: remove the `mut` prefix: `_` + | ^^^^ help: remove the `mut` prefix | = note: `mut` may be followed by `variable` and `variable @ pattern` diff --git a/tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr b/tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr index 8c032e588e31d..2bd87ee0c38fa 100644 --- a/tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr +++ b/tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr @@ -2,7 +2,7 @@ error: `mut` must be followed by a named binding --> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:6:13 | LL | let mut $eval = (); - | ^^^^^^^^^ help: remove the `mut` prefix: `does_not_exist!()` + | ^^^^ help: remove the `mut` prefix ... LL | mac1! { does_not_exist!() } | --------------------------- in this macro invocation @@ -25,7 +25,7 @@ error: `mut` must be followed by a named binding --> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:13 | LL | let mut $eval = (); - | ^^^ help: remove the `mut` prefix: `does_not_exist!()` + | ^^^ help: remove the `mut` prefix ... LL | mac2! { does_not_exist!() } | --------------------------- in this macro invocation diff --git a/tests/ui/parser/mut-patterns.stderr b/tests/ui/parser/mut-patterns.stderr index f179d8c9e0a83..66985c9f5e4f8 100644 --- a/tests/ui/parser/mut-patterns.stderr +++ b/tests/ui/parser/mut-patterns.stderr @@ -2,7 +2,7 @@ error: `mut` must be followed by a named binding --> $DIR/mut-patterns.rs:9:9 | LL | let mut _ = 0; - | ^^^^^ help: remove the `mut` prefix: `_` + | ^^^^ help: remove the `mut` prefix | = note: `mut` may be followed by `variable` and `variable @ pattern` @@ -10,7 +10,7 @@ error: `mut` must be followed by a named binding --> $DIR/mut-patterns.rs:10:9 | LL | let mut (_, _) = (0, 0); - | ^^^^^^^^^^ help: remove the `mut` prefix: `(_, _)` + | ^^^^ help: remove the `mut` prefix | = note: `mut` may be followed by `variable` and `variable @ pattern` diff --git a/tests/ui/self/self_type_keyword.stderr b/tests/ui/self/self_type_keyword.stderr index 6e65fae808ded..fed853a7e1fd8 100644 --- a/tests/ui/self/self_type_keyword.stderr +++ b/tests/ui/self/self_type_keyword.stderr @@ -14,7 +14,7 @@ error: `mut` must be followed by a named binding --> $DIR/self_type_keyword.rs:16:9 | LL | mut Self => (), - | ^^^^^^^^ help: remove the `mut` prefix: `Self` + | ^^^^ help: remove the `mut` prefix | = note: `mut` may be followed by `variable` and `variable @ pattern` From 8e7d0702a2dfc3e2fe4331af609cda9a92a4ef9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 6 Nov 2023 19:50:00 +0000 Subject: [PATCH 06/10] Add test for parens around match arm pattern and condition --- .../recover-parens-around-match-arm-head.rs | 14 ++++++ ...ecover-parens-around-match-arm-head.stderr | 49 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/ui/parser/recover/recover-parens-around-match-arm-head.rs create mode 100644 tests/ui/parser/recover/recover-parens-around-match-arm-head.stderr diff --git a/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs b/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs new file mode 100644 index 0000000000000..0c348e27e9a1c --- /dev/null +++ b/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs @@ -0,0 +1,14 @@ +fn main() { + let val = 42; + let x = match val { + (0 if true) => { + //~^ ERROR expected identifier, found keyword `if` + //~| ERROR expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `if` + //~| ERROR expected one of `)`, `,`, `@`, or `|`, found keyword `true` + //~| ERROR mismatched types + 42u8 + } + _ => 0u8, + }; + let _y: u32 = x; //~ ERROR mismatched types +} \ No newline at end of file diff --git a/tests/ui/parser/recover/recover-parens-around-match-arm-head.stderr b/tests/ui/parser/recover/recover-parens-around-match-arm-head.stderr new file mode 100644 index 0000000000000..6542f440e4b09 --- /dev/null +++ b/tests/ui/parser/recover/recover-parens-around-match-arm-head.stderr @@ -0,0 +1,49 @@ +error: expected identifier, found keyword `if` + --> $DIR/recover-parens-around-match-arm-head.rs:4:12 + | +LL | (0 if true) => { + | ^^ expected identifier, found keyword + +error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `if` + --> $DIR/recover-parens-around-match-arm-head.rs:4:12 + | +LL | (0 if true) => { + | -^^ expected one of `)`, `,`, `...`, `..=`, `..`, or `|` + | | + | help: missing `,` + +error: expected one of `)`, `,`, `@`, or `|`, found keyword `true` + --> $DIR/recover-parens-around-match-arm-head.rs:4:15 + | +LL | (0 if true) => { + | -^^^^ expected one of `)`, `,`, `@`, or `|` + | | + | help: missing `,` + +error[E0308]: mismatched types + --> $DIR/recover-parens-around-match-arm-head.rs:4:9 + | +LL | let x = match val { + | --- this expression has type `{integer}` +LL | (0 if true) => { + | ^^^^^^^^^^^ expected integer, found `(_, _, _)` + | + = note: expected type `{integer}` + found tuple `(_, _, _)` + +error[E0308]: mismatched types + --> $DIR/recover-parens-around-match-arm-head.rs:13:19 + | +LL | let _y: u32 = x; + | --- ^ expected `u32`, found `u8` + | | + | expected due to this + | +help: you can convert a `u8` to a `u32` + | +LL | let _y: u32 = x.into(); + | +++++++ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0308`. From 4e26f9c335e3ea5eb88d1d583df87e4ea89d77a6 Mon Sep 17 00:00:00 2001 From: Zephyr <90854121+WhistlingZephyr@users.noreply.github.com> Date: Thu, 16 Nov 2023 23:57:09 +0530 Subject: [PATCH 07/10] fix: Update CONTRIBUTING.md recommend -> recommended --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dfaa70bb9db00..eeff563d8ecdf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ standard library in the [Standard library developers Guide][std-dev-guide], comm ## About the [rustc-dev-guide] The [rustc-dev-guide] is meant to help document how rustc –the Rust compiler– works, -as well as to help new contributors get involved in rustc development. It is recommend +as well as to help new contributors get involved in rustc development. It is recommended to read and understand the [rustc-dev-guide] before making a contribution. This guide talks about the different bots in the Rust ecosystem, the Rust development tools, bootstrapping, the compiler architecture, source code representation, and more. From 776146e9ebb6bbe17a37bfad955f3dac95317275 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 16 Nov 2023 10:42:23 -0800 Subject: [PATCH 08/10] bootstrap: only show PGO warnings when verbose Building rustc with `--rust-profile-use` is currently dumping a lot of warnings of "no profile data available for function" from `rustc_smir` and `stable_mir`. These simply aren't exercised by the current profile- gathering steps, but that's to be expected for new or experimental functionality. I think for most people, these warnings will be just noise, so it makes sense to only have them in verbose builds. --- src/bootstrap/src/core/build_steps/compile.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/compile.rs b/src/bootstrap/src/core/build_steps/compile.rs index af69860df1c56..51e4195827fc6 100644 --- a/src/bootstrap/src/core/build_steps/compile.rs +++ b/src/bootstrap/src/core/build_steps/compile.rs @@ -887,7 +887,9 @@ impl Step for Rustc { } else if let Some(path) = &builder.config.rust_profile_use { if compiler.stage == 1 { cargo.rustflag(&format!("-Cprofile-use={path}")); - cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function"); + if builder.is_verbose() { + cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function"); + } true } else { false From 1e8c0951e93566312240ab0f80c8387e2b549a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 16 Nov 2023 21:30:26 +0000 Subject: [PATCH 09/10] review comment: move error logic to different method --- compiler/rustc_hir_typeck/src/coercion.rs | 59 +---------------------- compiler/rustc_hir_typeck/src/demand.rs | 54 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 8dbf01e973544..6c03bc3b57ac6 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -36,9 +36,7 @@ //! ``` use crate::FnCtxt; -use rustc_errors::{ - struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan, -}; +use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; @@ -1740,7 +1738,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { // label pointing out the cause for the type coercion will be wrong // as prior return coercions would not be relevant (#57664). let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) { - self.explain_self_literal(fcx, &mut err, expr, expected, found); let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id); if let (Some(cond_expr), true, false) = ( @@ -1813,60 +1810,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { err } - fn explain_self_literal( - &self, - fcx: &FnCtxt<'_, 'tcx>, - err: &mut Diagnostic, - expr: &'tcx hir::Expr<'tcx>, - expected: Ty<'tcx>, - found: Ty<'tcx>, - ) { - match expr.peel_drop_temps().kind { - hir::ExprKind::Struct( - hir::QPath::Resolved( - None, - hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. }, - ), - .., - ) - | hir::ExprKind::Call( - hir::Expr { - kind: - hir::ExprKind::Path(hir::QPath::Resolved( - None, - hir::Path { - res: hir::def::Res::SelfTyAlias { alias_to, .. }, - span, - .. - }, - )), - .. - }, - .., - ) => { - if let Some(hir::Node::Item(hir::Item { - kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }), - .. - })) = fcx.tcx.hir().get_if_local(*alias_to) - { - err.span_label(self_ty.span, "this is the type of the `Self` literal"); - } - if let ty::Adt(e_def, e_args) = expected.kind() - && let ty::Adt(f_def, _f_args) = found.kind() - && e_def == f_def - { - err.span_suggestion_verbose( - *span, - "use the type name directly", - fcx.tcx.value_path_str_with_args(*alias_to, e_args), - Applicability::MaybeIncorrect, - ); - } - } - _ => {} - } - } - /// Checks whether the return type is unsized via an obligation, which makes /// sure we consider `dyn Trait: Sized` where clauses, which are trivially /// false but technically valid for typeck. diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 77d6183f862f5..386e4b4642a82 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -31,6 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } self.annotate_alternative_method_deref(err, expr, error); + self.explain_self_literal(err, expr, expected, expr_ty); // Use `||` to give these suggestions a precedence let suggested = self.suggest_missing_parentheses(err, expr) @@ -1027,6 +1028,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } + fn explain_self_literal( + &self, + err: &mut Diagnostic, + expr: &hir::Expr<'tcx>, + expected: Ty<'tcx>, + found: Ty<'tcx>, + ) { + match expr.peel_drop_temps().kind { + hir::ExprKind::Struct( + hir::QPath::Resolved( + None, + hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. }, + ), + .., + ) + | hir::ExprKind::Call( + hir::Expr { + kind: + hir::ExprKind::Path(hir::QPath::Resolved( + None, + hir::Path { + res: hir::def::Res::SelfTyAlias { alias_to, .. }, + span, + .. + }, + )), + .. + }, + .., + ) => { + if let Some(hir::Node::Item(hir::Item { + kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }), + .. + })) = self.tcx.hir().get_if_local(*alias_to) + { + err.span_label(self_ty.span, "this is the type of the `Self` literal"); + } + if let ty::Adt(e_def, e_args) = expected.kind() + && let ty::Adt(f_def, _f_args) = found.kind() + && e_def == f_def + { + err.span_suggestion_verbose( + *span, + "use the type name directly", + self.tcx.value_path_str_with_args(*alias_to, e_args), + Applicability::MaybeIncorrect, + ); + } + } + _ => {} + } + } + fn note_wrong_return_ty_due_to_generic_arg( &self, err: &mut Diagnostic, From 099eb409322acdc59fc6af6cf3a935ce6a0217f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 6 Nov 2023 19:56:45 +0000 Subject: [PATCH 10/10] Fix code indentation --- compiler/rustc_parse/src/parser/mod.rs | 4 ++-- .../ui/parser/recover/recover-parens-around-match-arm-head.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 2fe706943687d..833381a650344 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -830,8 +830,8 @@ impl<'a> Parser<'a> { // https://github.com/rust-lang/rust/issues/72373 if self.prev_token.is_ident() && self.token.kind == token::DotDot { let msg = format!( - "if you meant to bind the contents of \ - the rest of the array pattern into `{}`, use `@`", + "if you meant to bind the contents of the rest of the array \ + pattern into `{}`, use `@`", pprust::token_to_string(&self.prev_token) ); expect_err diff --git a/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs b/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs index 0c348e27e9a1c..9ed733bf07947 100644 --- a/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs +++ b/tests/ui/parser/recover/recover-parens-around-match-arm-head.rs @@ -11,4 +11,4 @@ fn main() { _ => 0u8, }; let _y: u32 = x; //~ ERROR mismatched types -} \ No newline at end of file +}