Skip to content

Commit a56f9ad

Browse files
committed
remove Rule 3 from ref_pat_eat_one_layer_2024
The debug assertion ensuring that the pattern mutability cap holds assumes the presence of Rule 3, so it now checks for that. I considered going back to only tracking the mutability cap when Rule 3 is present, but since the mutability cap is used in Rule 5's implementation too, the debug assertion would still need to check which typing rules are present. This also required some changes to tests: - `ref_pat_eat_one_layer_2021.rs` had a test for Rule 3; I'll be handling tests for earlier editions in a later commit, so as a stopgap I've #[cfg]ed it out. - One test case had to be moved from `well-typed-edition-2024.rs` to `borrowck-errors.rs` in order to get borrowck to run on it and emit an error.
1 parent f3d1d47 commit a56f9ad

File tree

7 files changed

+22
-34
lines changed

7 files changed

+22
-34
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
243243
fn downgrade_mut_inside_shared(&self) -> bool {
244244
// NB: RFC 3627 proposes stabilizing Rule 3 in all editions. If we adopt the same behavior
245245
// across all editions, this may be removed.
246-
self.tcx.features().ref_pat_eat_one_layer_2024()
247-
|| self.tcx.features().ref_pat_eat_one_layer_2024_structural()
246+
self.tcx.features().ref_pat_eat_one_layer_2024_structural()
248247
}
249248

250249
/// Experimental pattern feature: when do reference patterns match against inherited references?
@@ -425,7 +424,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
425424
max_ref_mutbl: MutblCap,
426425
) -> (Ty<'tcx>, ByRef, MutblCap) {
427426
#[cfg(debug_assertions)]
428-
if def_br == ByRef::Yes(Mutability::Mut) && max_ref_mutbl != MutblCap::Mut {
427+
if def_br == ByRef::Yes(Mutability::Mut)
428+
&& max_ref_mutbl != MutblCap::Mut
429+
&& self.downgrade_mut_inside_shared()
430+
{
429431
span_bug!(pat.span, "Pattern mutability cap violated!");
430432
}
431433
match adjust_mode {

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ error[E0596]: cannot borrow data in a `&` reference as mutable
1919
LL | let &ref mut x = &0;
2020
| ^^^^^^^^^ cannot borrow as mutable
2121

22-
error: aborting due to 2 previous errors
22+
error[E0596]: cannot borrow data in a `&` reference as mutable
23+
--> $DIR/borrowck-errors.rs:17:23
24+
|
25+
LL | if let &Some(Some(x)) = &Some(&mut Some(0)) {
26+
| ^ cannot borrow as mutable
27+
28+
error: aborting due to 3 previous errors
2329

2430
Some errors have detailed explanations: E0507, E0596.
2531
For more information about an error, try `rustc --explain E0507`.

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ pub fn main() {
1313

1414
let &ref mut x = &0;
1515
//~^ cannot borrow data in a `&` reference as mutable [E0596]
16+
17+
if let &Some(Some(x)) = &Some(&mut Some(0)) {
18+
//[classic]~^ ERROR: cannot borrow data in a `&` reference as mutable
19+
let _: &u32 = x;
20+
}
1621
}

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr

+1-25
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,6 @@ help: replace this `&mut` pattern with `&`
4646
LL | if let Some(&Some(&_)) = &Some(&Some(0)) {
4747
| ~
4848

49-
error[E0308]: mismatched types
50-
--> $DIR/pattern-errors.rs:31:23
51-
|
52-
LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) {
53-
| ^^^^^
54-
|
55-
= note: cannot match inherited `&` with `&mut` pattern
56-
help: replace this `&mut` pattern with `&`
57-
|
58-
LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
59-
| ~
60-
6149
error[E0308]: mismatched types
6250
--> $DIR/pattern-errors.rs:34:23
6351
|
@@ -70,18 +58,6 @@ help: replace this `&mut` pattern with `&`
7058
LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) {
7159
| ~
7260

73-
error[E0308]: mismatched types
74-
--> $DIR/pattern-errors.rs:37:29
75-
|
76-
LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) {
77-
| ^^^^^
78-
|
79-
= note: cannot match inherited `&` with `&mut` pattern
80-
help: replace this `&mut` pattern with `&`
81-
|
82-
LL | if let Some(&Some(Some((&_)))) = &Some(Some(&mut Some(0))) {
83-
| ~
84-
8561
error[E0308]: mismatched types
8662
--> $DIR/pattern-errors.rs:40:17
8763
|
@@ -106,6 +82,6 @@ help: replace this `&mut` pattern with `&`
10682
LL | if let Some(&Some(x)) = &Some(Some(0)) {
10783
| ~
10884

109-
error: aborting due to 9 previous errors
85+
error: aborting due to 7 previous errors
11086

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

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ pub fn main() {
2929
//~^ ERROR: mismatched types
3030
}
3131
if let Some(&Some(&mut _)) = &Some(&mut Some(0)) {
32-
//~^ ERROR: mismatched types
32+
//[structural]~^ ERROR: mismatched types
3333
}
3434
if let Some(&Some(&mut _)) = &mut Some(&Some(0)) {
3535
//~^ ERROR: mismatched types
3636
}
3737
if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) {
38-
//~^ ERROR: mismatched types
38+
//[structural]~^ ERROR: mismatched types
3939
}
4040
if let Some(&mut Some(x)) = &Some(Some(0)) {
4141
//~^ ERROR: mismatched types

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))]
77

88
pub fn main() {
9+
#[cfg(structural)]
910
if let &Some(Some(x)) = &Some(&mut Some(0)) {
1011
let _: &u32 = x;
1112
}
13+
1214
if let Some(&x) = Some(&mut 0) {
1315
let _: u32 = x;
1416
}

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ pub fn main() {
3535
if let Some(&Some(&mut ref x)) = Some(&Some(&mut 0)) {
3636
let _: &u32 = x;
3737
}
38-
if let &Some(Some(x)) = &Some(&mut Some(0)) {
39-
let _: &u32 = x;
40-
}
4138
if let Some(&Some(&x)) = &Some(&mut Some(0)) {
4239
let _: u32 = x;
4340
}

0 commit comments

Comments
 (0)