From a53f280bfc00800f3c3224508612db9be74bd160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 8 Jan 2023 20:24:17 +0000 Subject: [PATCH 1/3] Suggest `;` after bare `match` expression E0308 Fix #72634. --- compiler/rustc_hir_typeck/src/_match.rs | 1 + .../rustc_infer/src/infer/error_reporting/mod.rs | 13 +++++++++++++ compiler/rustc_middle/src/traits/mod.rs | 1 + tests/ui/suggestions/issue-81839.stderr | 15 +++++++++++---- tests/ui/wf/wf-unsafe-trait-obj-match.stderr | 4 ++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/_match.rs b/compiler/rustc_hir_typeck/src/_match.rs index 81fe0cc489e33..6afdafb1b73b7 100644 --- a/compiler/rustc_hir_typeck/src/_match.rs +++ b/compiler/rustc_hir_typeck/src/_match.rs @@ -121,6 +121,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { prior_arm_ty, prior_arm_span, scrut_span: scrut.span, + scrut_hir_id: scrut.hir_id, source: match_src, prior_arms: other_arms.clone(), opt_suggest_box_span, diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 12dcb7118203a..aa9980a3cf0ac 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -775,6 +775,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { ref prior_arms, opt_suggest_box_span, scrut_span, + scrut_hir_id, .. }) => match source { hir::MatchSource::TryDesugar(scrut_hir_id) => { @@ -842,6 +843,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { ) { err.subdiagnostic(subdiag); } + if let Some(hir::Node::Expr(m)) = self.tcx.hir().find_parent(scrut_hir_id) + && let Some(hir::Node::Stmt(stmt)) = self.tcx.hir().find_parent(m.hir_id) + && let hir::StmtKind::Expr(_) = stmt.kind + { + err.span_suggestion_verbose( + stmt.span.shrink_to_hi(), + "consider using a semicolon here, but this will discard any values \ + in the match arms", + ";", + Applicability::MaybeIncorrect, + ); + } if let Some(ret_sp) = opt_suggest_box_span { // Get return type span and point to it. self.suggest_boxing_for_return_impl_trait( diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 99b750c9afc8c..071937c69dd32 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -541,6 +541,7 @@ pub struct MatchExpressionArmCause<'tcx> { pub prior_arm_ty: Ty<'tcx>, pub prior_arm_span: Span, pub scrut_span: Span, + pub scrut_hir_id: hir::HirId, pub source: hir::MatchSource, pub prior_arms: Vec, pub opt_suggest_box_span: Option, diff --git a/tests/ui/suggestions/issue-81839.stderr b/tests/ui/suggestions/issue-81839.stderr index 6d0a0c7b3faa2..238ee637c7d4b 100644 --- a/tests/ui/suggestions/issue-81839.stderr +++ b/tests/ui/suggestions/issue-81839.stderr @@ -4,15 +4,22 @@ error[E0308]: `match` arms have incompatible types LL | / match num { LL | | 1 => { LL | | cx.answer_str("hi"); - | | -------------------- - | | | | - | | | help: consider removing this semicolon - | | this is found to be of type `()` + | | -------------------- this is found to be of type `()` LL | | } LL | | _ => cx.answer_str("hi"), | | ^^^^^^^^^^^^^^^^^^^ expected `()`, found future LL | | } | |_____- `match` arms have incompatible types + | +help: consider removing this semicolon + | +LL - cx.answer_str("hi"); +LL + cx.answer_str("hi") + | +help: consider using a semicolon here, but this will discard any values in the match arms + | +LL | }; + | + error: aborting due to previous error diff --git a/tests/ui/wf/wf-unsafe-trait-obj-match.stderr b/tests/ui/wf/wf-unsafe-trait-obj-match.stderr index 66504e440600e..d5b23572ff577 100644 --- a/tests/ui/wf/wf-unsafe-trait-obj-match.stderr +++ b/tests/ui/wf/wf-unsafe-trait-obj-match.stderr @@ -11,6 +11,10 @@ LL | | } | = note: expected reference `&S` found reference `&R` +help: consider using a semicolon here, but this will discard any values in the match arms + | +LL | }; + | + error[E0038]: the trait `Trait` cannot be made into an object --> $DIR/wf-unsafe-trait-obj-match.rs:26:21 From 35f03b763eb40a10bfad50285c9b1128fec6d96a Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Fri, 20 Oct 2023 14:52:56 +0200 Subject: [PATCH 2/3] Move `invalid-llvm-passes` test to `invalid-compile-flags` folder Nowadays there is an `invalid-compile-flags` folder, thus move this one there. Signed-off-by: Miguel Ojeda --- .../ui/{invalid => invalid-compile-flags}/invalid-llvm-passes.rs | 0 .../{invalid => invalid-compile-flags}/invalid-llvm-passes.stderr | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{invalid => invalid-compile-flags}/invalid-llvm-passes.rs (100%) rename tests/ui/{invalid => invalid-compile-flags}/invalid-llvm-passes.stderr (100%) diff --git a/tests/ui/invalid/invalid-llvm-passes.rs b/tests/ui/invalid-compile-flags/invalid-llvm-passes.rs similarity index 100% rename from tests/ui/invalid/invalid-llvm-passes.rs rename to tests/ui/invalid-compile-flags/invalid-llvm-passes.rs diff --git a/tests/ui/invalid/invalid-llvm-passes.stderr b/tests/ui/invalid-compile-flags/invalid-llvm-passes.stderr similarity index 100% rename from tests/ui/invalid/invalid-llvm-passes.stderr rename to tests/ui/invalid-compile-flags/invalid-llvm-passes.stderr From ccc4638d734c745a5ec16fdcce33aac4e8c3b1c0 Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Sat, 21 Oct 2023 13:16:09 +0200 Subject: [PATCH 3/3] fix spans for removing `.await` on `for` expressions --- .../src/traits/error_reporting/suggestions.rs | 2 +- tests/ui/async-await/unnecessary-await.rs | 5 +++++ tests/ui/async-await/unnecessary-await.stderr | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 12a88ac323926..78b466907b32b 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1644,7 +1644,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { // use nth(1) to skip one layer of desugaring from `IntoIter::into_iter` if let Some((_, hir::Node::Expr(await_expr))) = hir.parent_iter(*hir_id).nth(1) - && let Some(expr_span) = expr.span.find_ancestor_inside(await_expr.span) + && let Some(expr_span) = expr.span.find_ancestor_inside_same_ctxt(await_expr.span) { let removal_span = self .tcx diff --git a/tests/ui/async-await/unnecessary-await.rs b/tests/ui/async-await/unnecessary-await.rs index cd1e28714322a..93b68f018e4ca 100644 --- a/tests/ui/async-await/unnecessary-await.rs +++ b/tests/ui/async-await/unnecessary-await.rs @@ -31,4 +31,9 @@ async fn with_macros() { f!(()); } +// Regression test for issue #117014. +async fn desugaring_span_ctxt() { + for x in [] {}.await //~ ERROR `()` is not a future +} + fn main() {} diff --git a/tests/ui/async-await/unnecessary-await.stderr b/tests/ui/async-await/unnecessary-await.stderr index 9a2a035b2dd08..620370a6113a3 100644 --- a/tests/ui/async-await/unnecessary-await.stderr +++ b/tests/ui/async-await/unnecessary-await.stderr @@ -49,6 +49,19 @@ LL | f!(()); = note: required for `()` to implement `IntoFuture` = note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 3 previous errors +error[E0277]: `()` is not a future + --> $DIR/unnecessary-await.rs:36:20 + | +LL | for x in [] {}.await + | -^^^^^ + | || + | |`()` is not a future + | help: remove the `.await` + | + = help: the trait `Future` is not implemented for `()` + = note: () must be a future or must implement `IntoFuture` to be awaited + = note: required for `()` to implement `IntoFuture` + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0277`.