From c15ef58f4f67ee9ae02773642145650495f9f413 Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Mon, 29 Nov 2021 01:50:12 +0100 Subject: [PATCH 1/3] Fix suggestion to slice if scrutinee is a `Result` or `Option` --- compiler/rustc_typeck/src/check/pat.rs | 38 +++++++++++++++++++++----- src/test/ui/typeck/issue-91328.fixed | 27 ++++++++++++++++++ src/test/ui/typeck/issue-91328.rs | 27 ++++++++++++++++++ src/test/ui/typeck/issue-91328.stderr | 21 ++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/typeck/issue-91328.fixed create mode 100644 src/test/ui/typeck/issue-91328.rs create mode 100644 src/test/ui/typeck/issue-91328.stderr diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index 17b97d4cad1d4..9c2182a87ff1c 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -15,7 +15,7 @@ use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS; use rustc_span::hygiene::DesugaringKind; use rustc_span::lev_distance::find_best_match_for_name; use rustc_span::source_map::{Span, Spanned}; -use rustc_span::symbol::Ident; +use rustc_span::symbol::{sym, Ident}; use rustc_span::{BytePos, MultiSpan, DUMMY_SP}; use rustc_trait_selection::autoderef::Autoderef; use rustc_trait_selection::traits::{ObligationCause, Pattern}; @@ -2033,12 +2033,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { if let (Some(span), true) = (ti.span, ti.origin_expr) { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { - err.span_suggestion( - span, - "consider slicing here", - format!("{}[..]", snippet), - Applicability::MachineApplicable, - ); + let applicability = match self.resolve_vars_if_possible(ti.expected).kind() { + ty::Adt(adt_def, _) + if self.tcx.is_diagnostic_item(sym::Option, adt_def.did) + || self.tcx.is_diagnostic_item(sym::Result, adt_def.did) => + { + // Slicing won't work here, but `.as_deref()` might (issue #91328). + err.span_suggestion( + span, + "consider using `as_deref` here", + format!("{}.as_deref()", snippet), + Applicability::MaybeIncorrect, + ); + None + } + ty::Adt(adt_def, _) + if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did) => + { + Some(Applicability::MachineApplicable) + } + _ => Some(Applicability::MaybeIncorrect), + }; + + if let Some(applicability) = applicability { + err.span_suggestion( + span, + "consider slicing here", + format!("{}[..]", snippet), + applicability, + ); + } } } } diff --git a/src/test/ui/typeck/issue-91328.fixed b/src/test/ui/typeck/issue-91328.fixed new file mode 100644 index 0000000000000..48dc26718da31 --- /dev/null +++ b/src/test/ui/typeck/issue-91328.fixed @@ -0,0 +1,27 @@ +// Regression test for issue #91328. + +// run-rustfix + +#![allow(dead_code)] + +fn foo(r: Result, i32>) -> i32 { + match r.as_deref() { + //~^ HELP: consider using `as_deref` here + Ok([a, b]) => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + +fn bar(o: Option>) -> i32 { + match o.as_deref() { + //~^ HELP: consider using `as_deref` here + Some([a, b]) => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + +fn main() {} diff --git a/src/test/ui/typeck/issue-91328.rs b/src/test/ui/typeck/issue-91328.rs new file mode 100644 index 0000000000000..de5deb9534902 --- /dev/null +++ b/src/test/ui/typeck/issue-91328.rs @@ -0,0 +1,27 @@ +// Regression test for issue #91328. + +// run-rustfix + +#![allow(dead_code)] + +fn foo(r: Result, i32>) -> i32 { + match r { + //~^ HELP: consider using `as_deref` here + Ok([a, b]) => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + +fn bar(o: Option>) -> i32 { + match o { + //~^ HELP: consider using `as_deref` here + Some([a, b]) => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + +fn main() {} diff --git a/src/test/ui/typeck/issue-91328.stderr b/src/test/ui/typeck/issue-91328.stderr new file mode 100644 index 0000000000000..4a9ddb2b92717 --- /dev/null +++ b/src/test/ui/typeck/issue-91328.stderr @@ -0,0 +1,21 @@ +error[E0529]: expected an array or slice, found `Vec` + --> $DIR/issue-91328.rs:10:12 + | +LL | match r { + | - help: consider using `as_deref` here: `r.as_deref()` +LL | +LL | Ok([a, b]) => a + b, + | ^^^^^^ pattern cannot match with input type `Vec` + +error[E0529]: expected an array or slice, found `Vec` + --> $DIR/issue-91328.rs:20:14 + | +LL | match o { + | - help: consider using `as_deref` here: `o.as_deref()` +LL | +LL | Some([a, b]) => a + b, + | ^^^^^^ pattern cannot match with input type `Vec` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0529`. From 0363f11ff03269d84eedb50d2130000fdfeceeb0 Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Sat, 15 Jan 2022 21:33:11 +0100 Subject: [PATCH 2/3] Add match on `Vec<_>` to `ui/typeck/issue-91328.rs` test --- src/test/ui/typeck/issue-91328.fixed | 10 ++++++++++ src/test/ui/typeck/issue-91328.rs | 10 ++++++++++ src/test/ui/typeck/issue-91328.stderr | 11 ++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/test/ui/typeck/issue-91328.fixed b/src/test/ui/typeck/issue-91328.fixed index 48dc26718da31..81b6a99607215 100644 --- a/src/test/ui/typeck/issue-91328.fixed +++ b/src/test/ui/typeck/issue-91328.fixed @@ -24,4 +24,14 @@ fn bar(o: Option>) -> i32 { } } +fn baz(v: Vec) -> i32 { + match v[..] { + //~^ HELP: consider slicing here + [a, b] => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + fn main() {} diff --git a/src/test/ui/typeck/issue-91328.rs b/src/test/ui/typeck/issue-91328.rs index de5deb9534902..e938d8f5c9f04 100644 --- a/src/test/ui/typeck/issue-91328.rs +++ b/src/test/ui/typeck/issue-91328.rs @@ -24,4 +24,14 @@ fn bar(o: Option>) -> i32 { } } +fn baz(v: Vec) -> i32 { + match v { + //~^ HELP: consider slicing here + [a, b] => a + b, + //~^ ERROR: expected an array or slice + //~| NOTE: pattern cannot match with input type + _ => 42, + } +} + fn main() {} diff --git a/src/test/ui/typeck/issue-91328.stderr b/src/test/ui/typeck/issue-91328.stderr index 4a9ddb2b92717..96ad00cde4f7b 100644 --- a/src/test/ui/typeck/issue-91328.stderr +++ b/src/test/ui/typeck/issue-91328.stderr @@ -16,6 +16,15 @@ LL | LL | Some([a, b]) => a + b, | ^^^^^^ pattern cannot match with input type `Vec` -error: aborting due to 2 previous errors +error[E0529]: expected an array or slice, found `Vec` + --> $DIR/issue-91328.rs:30:9 + | +LL | match v { + | - help: consider slicing here: `v[..]` +LL | +LL | [a, b] => a + b, + | ^^^^^^ pattern cannot match with input type `Vec` + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0529`. From 95344c02fd72511d3c87eb36f2fb36e28562934a Mon Sep 17 00:00:00 2001 From: Fabian Wolff Date: Mon, 31 Jan 2022 20:34:26 +0100 Subject: [PATCH 3/3] Add FIXME comment --- compiler/rustc_typeck/src/check/pat.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index 9c2182a87ff1c..62cd9c64a4a7c 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -2047,6 +2047,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); None } + // FIXME: instead of checking for Vec only, we could check whether the + // type implements `Deref`; see + // https://github.com/rust-lang/rust/pull/91343#discussion_r761466979 ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did) => {