Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix suggestion to slice if scrutinee is a Result or Option #91343

Merged
merged 3 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2033,12 +2033,39 @@ 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
}
// FIXME: instead of checking for Vec only, we could check whether the
// type implements `Deref<Target=X>`; 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) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So...I think I'm learning something today about how Rust's SliceIndex works (at least, unless I've missed something). So...I guess when we do a_vec[..], what's actually happening is there is an implicit deref: (a_vec.deref())[..]) - because SliceIndex is only implemented for slice, then the implicit deref is basically "forced". (At least, I think this is the case...please someone correct me if I'm wrong.)

I bring this up because rather than checking for Vec exactly, we could check if the type impls Deref<Target=X>?

Of course, this could be done as followup. This looks good otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could in general be smarter above too for Result<T> to only suggest if T: Deref<Target=X>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be nicer, but I don't know how to check for such a trait bound here. Let's leave it to a follow-up (or do you know an easy way to implement this?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's leave this as a followup. Can you add a FIXME comment though?

{
Some(Applicability::MachineApplicable)
}
_ => Some(Applicability::MaybeIncorrect),
};

if let Some(applicability) = applicability {
err.span_suggestion(
span,
"consider slicing here",
format!("{}[..]", snippet),
applicability,
);
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/ui/typeck/issue-91328.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Regression test for issue #91328.

// run-rustfix

#![allow(dead_code)]

fn foo(r: Result<Vec<i32>, 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<Vec<i32>>) -> 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 baz(v: Vec<i32>) -> 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() {}
37 changes: 37 additions & 0 deletions src/test/ui/typeck/issue-91328.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Regression test for issue #91328.

// run-rustfix

#![allow(dead_code)]

fn foo(r: Result<Vec<i32>, 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<Vec<i32>>) -> 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 baz(v: Vec<i32>) -> 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() {}
30 changes: 30 additions & 0 deletions src/test/ui/typeck/issue-91328.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0529]: expected an array or slice, found `Vec<i32>`
--> $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<i32>`

error[E0529]: expected an array or slice, found `Vec<i32>`
--> $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<i32>`

error[E0529]: expected an array or slice, found `Vec<i32>`
--> $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<i32>`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0529`.