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

Rollup of 8 pull requests #94072

Merged
merged 18 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6fd5cf5
Add documentation to more `From::from` implementations.
kpreid Oct 13, 2021
96d96a7
Use `optflag` for `--report-time`
smoelius Jan 30, 2022
d9592d9
Fix suggestion to slice if scurtinee is a reference to `Result` or `O…
ChayimFriedman2 Feb 14, 2022
8610edd
Suggest deriving required supertraits
rukai Feb 6, 2022
ae21240
Make implementation generic
rukai Feb 12, 2022
1973f27
Cleanup uses
rukai Feb 16, 2022
91adb6c
Correctly mark the span of captured arguments in `format_args!()`
ChayimFriedman2 Feb 16, 2022
6d2cdbe
Add mentions to `Copy` for `union` fields
danielhenrymantilla Feb 15, 2022
65fc705
Do not suggest "is a function" for free variables
notriddle Feb 13, 2022
8630085
Update dist-x86_64-musl to Ubuntu 20.04
nikic Feb 16, 2022
1cc0ae4
Rollup merge of #89869 - kpreid:from-doc, r=yaahc
matthiaskrgr Feb 17, 2022
d855121
Rollup merge of #93479 - smoelius:master, r=yaahc
matthiaskrgr Feb 17, 2022
351aa1b
Rollup merge of #93693 - rukai:91550, r=davidtwco
matthiaskrgr Feb 17, 2022
2c0df80
Rollup merge of #93981 - ChayimFriedman2:slice-pat-reference-option-r…
matthiaskrgr Feb 17, 2022
3e72705
Rollup merge of #93996 - notriddle:notriddle/magically-becomes-a-func…
matthiaskrgr Feb 17, 2022
a1a750b
Rollup merge of #94030 - ChayimFriedman2:issue-94010, r=petrochenkov
matthiaskrgr Feb 17, 2022
91f70a8
Rollup merge of #94031 - danielhenrymantilla:diagnostics/union-drop-s…
matthiaskrgr Feb 17, 2022
aa83189
Rollup merge of #94064 - nikic:update-musl-image, r=Mark-Simulacrum
matthiaskrgr Feb 17, 2022
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
56 changes: 32 additions & 24 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,34 +2029,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.help("the semantics of slice patterns changed recently; see issue #62254");
}
} else if Autoderef::new(&self.infcx, self.param_env, self.body_id, span, expected_ty, span)
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..)))
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
{
if let (Some(span), true) = (ti.span, ti.origin_expr) {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
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) =>
{
Some(Applicability::MachineApplicable)
let applicability = Autoderef::new(
&self.infcx,
self.param_env,
self.body_id,
span,
self.resolve_vars_if_possible(ti.expected),
span,
)
.find_map(|(ty, _)| {
match ty.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,
);
Some(None)
}

ty::Slice(..) | ty::Array(..) => {
Some(Some(Applicability::MachineApplicable))
}

_ => None,
}
_ => Some(Applicability::MaybeIncorrect),
};
})
.unwrap_or(Some(Applicability::MaybeIncorrect));

if let Some(applicability) = applicability {
err.span_suggestion(
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/typeck/issue-91328.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ fn baz(v: Vec<i32>) -> i32 {
}
}

fn qux(a: &Option<Box<[i32; 2]>>) -> i32 {
match a.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() {}
10 changes: 10 additions & 0 deletions src/test/ui/typeck/issue-91328.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ fn baz(v: Vec<i32>) -> i32 {
}
}

fn qux(a: &Option<Box<[i32; 2]>>) -> i32 {
match a {
//~^ 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() {}
11 changes: 10 additions & 1 deletion src/test/ui/typeck/issue-91328.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ LL |
LL | [a, b] => a + b,
| ^^^^^^ pattern cannot match with input type `Vec<i32>`

error: aborting due to 3 previous errors
error[E0529]: expected an array or slice, found `Box<[i32; 2]>`
--> $DIR/issue-91328.rs:40:14
|
LL | match a {
| - help: consider using `as_deref` here: `a.as_deref()`
LL |
LL | Some([a, b]) => a + b,
| ^^^^^^ pattern cannot match with input type `Box<[i32; 2]>`

error: aborting due to 4 previous errors

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