&mut
no longer peels off &
This commit is contained in:
parent
a32fb2f8aa
commit
eb91f3b051
@ -335,9 +335,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
match adjust_mode {
|
||||
AdjustMode::Pass => (expected, def_br, max_ref_mutbl),
|
||||
AdjustMode::Reset => (expected, ByRef::No, MutblCap::Mut),
|
||||
AdjustMode::Peel => {
|
||||
self.peel_off_references(pat, expected, def_br, Mutability::Mut, max_ref_mutbl)
|
||||
}
|
||||
AdjustMode::Peel => self.peel_off_references(pat, expected, def_br, max_ref_mutbl),
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,7 +406,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
pat: &'tcx Pat<'tcx>,
|
||||
expected: Ty<'tcx>,
|
||||
mut def_br: ByRef,
|
||||
max_peelable_mutability: Mutability,
|
||||
mut max_ref_mutability: MutblCap,
|
||||
) -> (Ty<'tcx>, ByRef, MutblCap) {
|
||||
let mut expected = self.try_structurally_resolve_type(pat.span, expected);
|
||||
@ -421,9 +418,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
//
|
||||
// See the examples in `ui/match-defbm*.rs`.
|
||||
let mut pat_adjustments = vec![];
|
||||
while let ty::Ref(_, inner_ty, inner_mutability) = *expected.kind()
|
||||
&& inner_mutability <= max_peelable_mutability
|
||||
{
|
||||
while let ty::Ref(_, inner_ty, inner_mutability) = *expected.kind() {
|
||||
debug!("inspecting {:?}", expected);
|
||||
|
||||
debug!("current discriminant is Ref, inserting implicit deref");
|
||||
@ -2129,32 +2124,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024;
|
||||
|
||||
if new_match_ergonomics {
|
||||
let pat_prefix_span =
|
||||
inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end));
|
||||
|
||||
if pat_mutbl == Mutability::Not {
|
||||
// Prevent the inner pattern from binding with `ref mut`.
|
||||
pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(
|
||||
inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end)),
|
||||
);
|
||||
pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span);
|
||||
}
|
||||
|
||||
if let ByRef::Yes(inh_mut) = pat_info.binding_mode
|
||||
&& pat_mutbl <= inh_mut
|
||||
{
|
||||
if let ByRef::Yes(inh_mut) = pat_info.binding_mode {
|
||||
// ref pattern consumes inherited reference
|
||||
|
||||
if pat_mutbl > inh_mut {
|
||||
// Tried to match inherited `ref` with `&mut`, which is an error
|
||||
let err_msg = "cannot match inherited `&` with `&mut` pattern";
|
||||
let err = if let Some(span) = pat_prefix_span {
|
||||
let mut err = self.dcx().struct_span_err(span, err_msg);
|
||||
err.span_suggestion_verbose(
|
||||
span,
|
||||
"replace this `&mut` pattern with `&`",
|
||||
"&",
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
err
|
||||
} else {
|
||||
self.dcx().struct_span_err(pat.span, err_msg)
|
||||
};
|
||||
err.emit();
|
||||
}
|
||||
|
||||
pat_info.binding_mode = ByRef::No;
|
||||
self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
|
||||
self.check_pat(inner, expected, pat_info);
|
||||
return expected;
|
||||
} else if pat_mutbl == Mutability::Mut {
|
||||
// `&mut` patterns pell off `&` references
|
||||
let (new_expected, new_bm, max_ref_mutbl) = self.peel_off_references(
|
||||
pat,
|
||||
expected,
|
||||
pat_info.binding_mode,
|
||||
Mutability::Not,
|
||||
pat_info.max_ref_mutbl,
|
||||
);
|
||||
expected = new_expected;
|
||||
pat_info.binding_mode = new_bm;
|
||||
pat_info.max_ref_mutbl = max_ref_mutbl;
|
||||
}
|
||||
} else {
|
||||
// Reset binding mode on old editions
|
||||
|
@ -23,9 +23,6 @@ pub fn main() {
|
||||
if let Some(Some(&x)) = &Some(&Some(0)) {
|
||||
let _: u32 = x;
|
||||
}
|
||||
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
|
||||
let _: u32 = x;
|
||||
}
|
||||
if let Some(&Some(&x)) = &mut Some(&Some(0)) {
|
||||
let _: u32 = x;
|
||||
}
|
||||
@ -35,9 +32,6 @@ pub fn main() {
|
||||
if let Some(&Some(&mut ref x)) = Some(&Some(&mut 0)) {
|
||||
let _: &u32 = x;
|
||||
}
|
||||
if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
|
||||
let _: &u32 = x;
|
||||
}
|
||||
if let &Some(Some(x)) = &Some(&mut Some(0)) {
|
||||
let _: &u32 = x;
|
||||
}
|
||||
@ -59,13 +53,4 @@ pub fn main() {
|
||||
if let Some(&Some(x)) = &mut Some(Some(0)) {
|
||||
let _: u32 = x;
|
||||
}
|
||||
|
||||
let &mut x = &&mut 0;
|
||||
let _: &u32 = x;
|
||||
|
||||
let &mut x = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
|
||||
let _: &u32 = x;
|
||||
|
||||
let &mut &mut &mut &mut x = &mut &&&&mut &&&mut &mut 0;
|
||||
let _: &u32 = x;
|
||||
}
|
||||
|
@ -5,26 +5,26 @@
|
||||
|
||||
pub fn main() {
|
||||
if let Some(&mut Some(&_)) = &Some(&Some(0)) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
if let Some(&Some(&mut _)) = &Some(&mut Some(0)) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
if let Some(&Some(x)) = &mut Some(&Some(0)) {
|
||||
let _: &mut u32 = x;
|
||||
//~^ ERROR: mismatched types
|
||||
}
|
||||
if let Some(&Some(&mut _)) = &mut Some(&Some(0)) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
if let Some(&mut Some(x)) = &Some(Some(0)) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
if let Some(&mut Some(x)) = &Some(Some(0)) {
|
||||
//~^ ERROR: mismatched types
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
|
||||
let &mut _ = &&0;
|
||||
@ -32,4 +32,21 @@ pub fn main() {
|
||||
|
||||
let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
|
||||
//~^ ERROR: mismatched types
|
||||
|
||||
if let Some(&mut Some(&_)) = &Some(&mut Some(0)) {
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
|
||||
if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
|
||||
//~^ ERROR: cannot match inherited `&` with `&mut` pattern
|
||||
}
|
||||
|
||||
let &mut _ = &&mut 0;
|
||||
//~^ ERROR: mismatched types
|
||||
|
||||
let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
|
||||
//~^ ERROR: mismatched types
|
||||
|
||||
let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0;
|
||||
//~^ ERROR: mismatched types
|
||||
}
|
||||
|
@ -1,24 +1,24 @@
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:7:17
|
||||
|
|
||||
LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) {
|
||||
| ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>`
|
||||
| |
|
||||
| expected `Option<{integer}>`, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected enum `Option<{integer}>`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(&_)) = &Some(&Some(0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:10:23
|
||||
|
|
||||
LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) {
|
||||
| ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>`
|
||||
| |
|
||||
| expected integer, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:14:27
|
||||
@ -31,49 +31,49 @@ LL | let _: &mut u32 = x;
|
||||
= note: expected mutable reference `&mut u32`
|
||||
found reference `&{integer}`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:17:23
|
||||
|
|
||||
LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) {
|
||||
| ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>`
|
||||
| |
|
||||
| expected integer, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:20:29
|
||||
|
|
||||
LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) {
|
||||
| ^^^^^^ ------------------------- this expression has type `&Option<Option<&mut Option<{integer}>>>`
|
||||
| |
|
||||
| expected integer, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(Some((&_)))) = &Some(Some(&mut Some(0))) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:23:17
|
||||
|
|
||||
LL | if let Some(&mut Some(x)) = &Some(Some(0)) {
|
||||
| ^^^^^^^^^^^^ -------------- this expression has type `&Option<Option<{integer}>>`
|
||||
| |
|
||||
| expected `Option<{integer}>`, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected enum `Option<{integer}>`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(x)) = &Some(Some(0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:26:17
|
||||
|
|
||||
LL | if let Some(&mut Some(x)) = &Some(Some(0)) {
|
||||
| ^^^^^^^^^^^^ -------------- this expression has type `&Option<Option<{integer}>>`
|
||||
| |
|
||||
| expected `Option<{integer}>`, found `&mut _`
|
||||
| ^^^^^
|
||||
|
|
||||
= note: expected enum `Option<{integer}>`
|
||||
found mutable reference `&mut _`
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(x)) = &Some(Some(0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:30:9
|
||||
@ -81,9 +81,9 @@ error[E0308]: mismatched types
|
||||
LL | let &mut _ = &&0;
|
||||
| ^^^^^^ --- this expression has type `&&{integer}`
|
||||
| |
|
||||
| expected integer, found `&mut _`
|
||||
| types differ in mutability
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
= note: expected reference `&&{integer}`
|
||||
found mutable reference `&mut _`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
@ -92,11 +92,66 @@ error[E0308]: mismatched types
|
||||
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
|
||||
| ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
|
||||
| |
|
||||
| expected integer, found `&mut _`
|
||||
| types differ in mutability
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
|
||||
found mutable reference `&mut _`
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:36:17
|
||||
|
|
||||
LL | if let Some(&mut Some(&_)) = &Some(&mut Some(0)) {
|
||||
| ^^^^^
|
||||
|
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
|
||||
| ~
|
||||
|
||||
error: cannot match inherited `&` with `&mut` pattern
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:40:22
|
||||
|
|
||||
LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
|
||||
| ^^^^^
|
||||
|
|
||||
help: replace this `&mut` pattern with `&`
|
||||
|
|
||||
LL | if let Some(Some(&x)) = &Some(Some(&mut 0)) {
|
||||
| ~
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:44:9
|
||||
|
|
||||
LL | let &mut _ = &&mut 0;
|
||||
| ^^^^^^ ------- this expression has type `&&mut {integer}`
|
||||
| |
|
||||
| types differ in mutability
|
||||
|
|
||||
= note: expected reference `&&mut {integer}`
|
||||
found mutable reference `&mut _`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:47:9
|
||||
|
|
||||
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
|
||||
| ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
|
||||
| |
|
||||
| types differ in mutability
|
||||
|
|
||||
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
|
||||
found mutable reference `&mut _`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:50:14
|
||||
|
|
||||
LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0;
|
||||
| ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}`
|
||||
| |
|
||||
| types differ in mutability
|
||||
|
|
||||
= note: expected reference `&&&&mut &&&mut &mut {integer}`
|
||||
found mutable reference `&mut _`
|
||||
|
||||
error: aborting due to 14 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user