Skip to content

Commit 8519577

Browse files
committed
"structural2021" ruleset: add fallback-to-outer (eat both) deref rule
1 parent ec8bf0f commit 8519577

File tree

5 files changed

+151
-179
lines changed

5 files changed

+151
-179
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ enum InheritedRefMatchRule {
235235
/// Whether to allow reference patterns to consume only an inherited reference when matching
236236
/// against a non-reference type. This is `false` for stable Rust.
237237
eat_inherited_ref_alone: bool,
238+
/// Whether to allow a `&mut` reference pattern to eat a `&` reference type if it's also
239+
/// able to consume a mutable inherited reference. This is `false` for stable Rust.
240+
fallback_to_outer: bool,
238241
},
239242
}
240243

@@ -261,12 +264,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
261264
} else {
262265
// Currently, matching against an inherited ref on edition 2024 is an error.
263266
// Use `EatBoth` as a fallback to be similar to stable Rust.
264-
InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false }
267+
InheritedRefMatchRule::EatBoth {
268+
eat_inherited_ref_alone: false,
269+
fallback_to_outer: false,
270+
}
265271
}
266272
} else {
273+
let has_structural_gate = self.tcx.features().ref_pat_eat_one_layer_2024_structural();
267274
InheritedRefMatchRule::EatBoth {
268-
eat_inherited_ref_alone: self.tcx.features().ref_pat_eat_one_layer_2024()
269-
|| self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
275+
eat_inherited_ref_alone: has_structural_gate
276+
|| self.tcx.features().ref_pat_eat_one_layer_2024(),
277+
fallback_to_outer: has_structural_gate,
270278
}
271279
}
272280
}
@@ -2366,12 +2374,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23662374
return expected;
23672375
}
23682376
}
2369-
InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: true } => {
2377+
InheritedRefMatchRule::EatBoth {
2378+
eat_inherited_ref_alone: true,
2379+
fallback_to_outer,
2380+
} => {
23702381
// Reset binding mode on old editions
23712382
pat_info.binding_mode = ByRef::No;
23722383

2373-
if let ty::Ref(_, _, _) = *expected.kind() {
2384+
if let ty::Ref(_, inner_ty, _) = *expected.kind() {
23742385
// Consume both the inherited and inner references.
2386+
if fallback_to_outer && inh_mut.is_mut() {
2387+
// If we can fall back to matching the inherited reference, the expected
2388+
// type is a reference type (of any mutability), and the inherited
2389+
// reference is mutable, we'll always be able to match. We handle that
2390+
// here to avoid adding fallback-to-outer to the common logic below.
2391+
// NB: This way of phrasing the logic will catch more cases than those
2392+
// that need to fall back to matching the inherited reference. However,
2393+
// as long as `&` patterns can match mutable (inherited) references
2394+
// (RFC 3627, Rule 5) this should be sound.
2395+
debug_assert!(ref_pat_matches_mut_ref);
2396+
self.check_pat(inner, inner_ty, pat_info);
2397+
return expected;
2398+
} else {
2399+
// Otherwise, use the common logic below for matching the inner
2400+
// reference type.
2401+
}
23752402
} else {
23762403
// The expected type isn't a reference type, so only match against the
23772404
// inherited reference.
@@ -2385,9 +2412,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23852412
return expected;
23862413
}
23872414
}
2388-
InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false } => {
2415+
rule @ InheritedRefMatchRule::EatBoth {
2416+
eat_inherited_ref_alone: false,
2417+
fallback_to_outer,
2418+
} => {
23892419
// Reset binding mode on stable Rust. This will be a type error below if
23902420
// `expected` is not a reference type.
2421+
debug_assert!(!fallback_to_outer, "typing rule `{rule:?}` is unimplemented.");
23912422
pat_info.binding_mode = ByRef::No;
23922423
self.add_rust_2024_migration_desugared_pat(
23932424
pat_info.top_info.hir_id,

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

+26-26
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,7 @@ LL + let [ref x] = &mut [&0];
102102
|
103103

104104
error[E0308]: mismatched types
105-
--> $DIR/well-typed-edition-2024.rs:108:10
106-
|
107-
LL | let [&mut ref mut x] = &mut [&0];
108-
| ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
109-
| |
110-
| types differ in mutability
111-
|
112-
= note: expected reference `&{integer}`
113-
found mutable reference `&mut _`
114-
note: to declare a mutable binding use: `mut x`
115-
--> $DIR/well-typed-edition-2024.rs:108:10
116-
|
117-
LL | let [&mut ref mut x] = &mut [&0];
118-
| ^^^^^^^^^^^^^^
119-
help: consider removing `&mut` from the pattern
120-
|
121-
LL - let [&mut ref mut x] = &mut [&0];
122-
LL + let [ref mut x] = &mut [&0];
123-
|
124-
125-
error[E0308]: mismatched types
126-
--> $DIR/well-typed-edition-2024.rs:114:10
105+
--> $DIR/well-typed-edition-2024.rs:117:10
127106
|
128107
LL | let [&mut mut x] = &mut [&0];
129108
| ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -133,7 +112,7 @@ LL | let [&mut mut x] = &mut [&0];
133112
= note: expected reference `&{integer}`
134113
found mutable reference `&mut _`
135114
note: to declare a mutable binding use: `mut x`
136-
--> $DIR/well-typed-edition-2024.rs:114:10
115+
--> $DIR/well-typed-edition-2024.rs:117:10
137116
|
138117
LL | let [&mut mut x] = &mut [&0];
139118
| ^^^^^^^^^^
@@ -144,7 +123,7 @@ LL + let [mut x] = &mut [&0];
144123
|
145124

146125
error[E0308]: mismatched types
147-
--> $DIR/well-typed-edition-2024.rs:120:10
126+
--> $DIR/well-typed-edition-2024.rs:123:10
148127
|
149128
LL | let [&mut &x] = &mut [&0];
150129
| ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -155,7 +134,7 @@ LL | let [&mut &x] = &mut [&0];
155134
found mutable reference `&mut _`
156135

157136
error[E0308]: mismatched types
158-
--> $DIR/well-typed-edition-2024.rs:126:10
137+
--> $DIR/well-typed-edition-2024.rs:129:10
159138
|
160139
LL | let [&mut &ref x] = &mut [&0];
161140
| ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -166,7 +145,7 @@ LL | let [&mut &ref x] = &mut [&0];
166145
found mutable reference `&mut _`
167146

168147
error[E0308]: mismatched types
169-
--> $DIR/well-typed-edition-2024.rs:132:10
148+
--> $DIR/well-typed-edition-2024.rs:135:10
170149
|
171150
LL | let [&mut &(mut x)] = &mut [&0];
172151
| ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -176,6 +155,27 @@ LL | let [&mut &(mut x)] = &mut [&0];
176155
= note: expected reference `&{integer}`
177156
found mutable reference `&mut _`
178157

158+
error[E0308]: mismatched types
159+
--> $DIR/well-typed-edition-2024.rs:109:14
160+
|
161+
LL | let [&mut ref mut x] = &mut [&0];
162+
| ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
163+
| |
164+
| types differ in mutability
165+
|
166+
= note: expected reference `&{integer}`
167+
found mutable reference `&mut _`
168+
note: to declare a mutable binding use: `mut x`
169+
--> $DIR/well-typed-edition-2024.rs:109:14
170+
|
171+
LL | let [&mut ref mut x] = &mut [&0];
172+
| ^^^^^^^^^^^^^^
173+
help: consider removing `&mut` from the pattern
174+
|
175+
LL - let [&mut ref mut x] = &mut [&0];
176+
LL + let [ref mut x] = &mut [&0];
177+
|
178+
179179
error: aborting due to 11 previous errors
180180

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

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

+25-22
Original file line numberDiff line numberDiff line change
@@ -91,47 +91,50 @@ pub fn main() {
9191
#[cfg(any(classic2024, structural2024))] let _: u32 = x;
9292
}
9393

94-
// Tests for eat-inner rulesets matching on the outer reference if matching on the inner
95-
// reference causes a mutability mismatch, i.e. `Deref(EatInner, FallbackToOuter)`:
94+
// Tests for eat-inner and eat-both rulesets matching on the outer reference if matching on the
95+
// inner reference causes a mutability mismatch. i.e. tests for "fallback-to-outer" deref rules.
9696
let [&mut x] = &mut [&0];
97-
//[stable2021,classic2021,structural2021]~^ mismatched types
98-
//[stable2021,classic2021,structural2021]~| types differ in mutability
99-
// TODO: on `structural2021` `x` should have type `u32`
97+
//[stable2021,classic2021]~^ mismatched types
98+
//[stable2021,classic2021]~| types differ in mutability
99+
#[cfg(structural2021)] let _: u32 = x;
100100
#[cfg(any(classic2024, structural2024))] let _: &u32 = x;
101101

102102
let [&mut ref x] = &mut [&0];
103-
//[stable2021,classic2021,structural2021]~^ mismatched types
104-
//[stable2021,classic2021,structural2021]~| types differ in mutability
105-
// TODO: on `structural2021` `x` should have type `&u32`
103+
//[stable2021,classic2021]~^ mismatched types
104+
//[stable2021,classic2021]~| types differ in mutability
105+
#[cfg(structural2021)] let _: &u32 = x;
106106
#[cfg(any(classic2024, structural2024))] let _: &&u32 = x;
107107

108-
let [&mut ref mut x] = &mut [&0];
109-
//[stable2021,classic2021,structural2021]~^ mismatched types
110-
//[stable2021,classic2021,structural2021]~| types differ in mutability
111-
// TODO: this should be a mut borrow behind shared borrow error on `structural2021`
112-
#[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x;
108+
fn borrowck_error_on_structural2021() {
109+
let [&mut ref mut x] = &mut [&0];
110+
//[stable2021,classic2021]~^ mismatched types
111+
//[stable2021,classic2021]~| types differ in mutability
112+
//[structural2021]~^^^ cannot borrow data in a `&` reference as mutable
113+
#[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x;
114+
}
115+
borrowck_error_on_structural2021();
113116

114117
let [&mut mut x] = &mut [&0];
115-
//[stable2021,classic2021,structural2021]~^ mismatched types
116-
//[stable2021,classic2021,structural2021]~| types differ in mutability
117-
// TODO: on `structural2021` `x` should have type `u32`
118+
//[stable2021,classic2021]~^ mismatched types
119+
//[stable2021,classic2021]~| types differ in mutability
120+
#[cfg(structural2021)] let _: u32 = x;
118121
#[cfg(any(classic2024, structural2024))] let _: &u32 = x;
119122

120123
let [&mut &x] = &mut [&0];
121124
//[stable2021,classic2021,structural2021]~^ mismatched types
122-
//[stable2021,classic2021,structural2021]~| types differ in mutability
123-
// TODO: [structural2021]~| expected integer, found `&_`
125+
//[stable2021,classic2021]~| types differ in mutability
126+
//[structural2021]~| expected integer, found `&_`
124127
#[cfg(any(classic2024, structural2024))] let _: u32 = x;
125128

126129
let [&mut &ref x] = &mut [&0];
127130
//[stable2021,classic2021,structural2021]~^ mismatched types
128-
//[stable2021,classic2021,structural2021]~| types differ in mutability
129-
// TODO: [structural2021]~| expected integer, found `&_`
131+
//[stable2021,classic2021]~| types differ in mutability
132+
//[structural2021]~| expected integer, found `&_`
130133
#[cfg(any(classic2024, structural2024))] let _: &u32 = x;
131134

132135
let [&mut &(mut x)] = &mut [&0];
133136
//[stable2021,classic2021,structural2021]~^ mismatched types
134-
//[stable2021,classic2021,structural2021]~| types differ in mutability
135-
// TODO: [structural2021]~| expected integer, found `&_`
137+
//[stable2021,classic2021]~| types differ in mutability
138+
//[structural2021]~| expected integer, found `&_`
136139
#[cfg(any(classic2024, structural2024))] let _: u32 = x;
137140
}

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

+26-26
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,7 @@ LL + let [ref x] = &mut [&0];
181181
|
182182

183183
error[E0308]: mismatched types
184-
--> $DIR/well-typed-edition-2024.rs:108:10
185-
|
186-
LL | let [&mut ref mut x] = &mut [&0];
187-
| ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
188-
| |
189-
| types differ in mutability
190-
|
191-
= note: expected reference `&{integer}`
192-
found mutable reference `&mut _`
193-
note: to declare a mutable binding use: `mut x`
194-
--> $DIR/well-typed-edition-2024.rs:108:10
195-
|
196-
LL | let [&mut ref mut x] = &mut [&0];
197-
| ^^^^^^^^^^^^^^
198-
help: consider removing `&mut` from the pattern
199-
|
200-
LL - let [&mut ref mut x] = &mut [&0];
201-
LL + let [ref mut x] = &mut [&0];
202-
|
203-
204-
error[E0308]: mismatched types
205-
--> $DIR/well-typed-edition-2024.rs:114:10
184+
--> $DIR/well-typed-edition-2024.rs:117:10
206185
|
207186
LL | let [&mut mut x] = &mut [&0];
208187
| ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -212,7 +191,7 @@ LL | let [&mut mut x] = &mut [&0];
212191
= note: expected reference `&{integer}`
213192
found mutable reference `&mut _`
214193
note: to declare a mutable binding use: `mut x`
215-
--> $DIR/well-typed-edition-2024.rs:114:10
194+
--> $DIR/well-typed-edition-2024.rs:117:10
216195
|
217196
LL | let [&mut mut x] = &mut [&0];
218197
| ^^^^^^^^^^
@@ -223,7 +202,7 @@ LL + let [mut x] = &mut [&0];
223202
|
224203

225204
error[E0308]: mismatched types
226-
--> $DIR/well-typed-edition-2024.rs:120:10
205+
--> $DIR/well-typed-edition-2024.rs:123:10
227206
|
228207
LL | let [&mut &x] = &mut [&0];
229208
| ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -234,7 +213,7 @@ LL | let [&mut &x] = &mut [&0];
234213
found mutable reference `&mut _`
235214

236215
error[E0308]: mismatched types
237-
--> $DIR/well-typed-edition-2024.rs:126:10
216+
--> $DIR/well-typed-edition-2024.rs:129:10
238217
|
239218
LL | let [&mut &ref x] = &mut [&0];
240219
| ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -245,7 +224,7 @@ LL | let [&mut &ref x] = &mut [&0];
245224
found mutable reference `&mut _`
246225

247226
error[E0308]: mismatched types
248-
--> $DIR/well-typed-edition-2024.rs:132:10
227+
--> $DIR/well-typed-edition-2024.rs:135:10
249228
|
250229
LL | let [&mut &(mut x)] = &mut [&0];
251230
| ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
@@ -255,6 +234,27 @@ LL | let [&mut &(mut x)] = &mut [&0];
255234
= note: expected reference `&{integer}`
256235
found mutable reference `&mut _`
257236

237+
error[E0308]: mismatched types
238+
--> $DIR/well-typed-edition-2024.rs:109:14
239+
|
240+
LL | let [&mut ref mut x] = &mut [&0];
241+
| ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]`
242+
| |
243+
| types differ in mutability
244+
|
245+
= note: expected reference `&{integer}`
246+
found mutable reference `&mut _`
247+
note: to declare a mutable binding use: `mut x`
248+
--> $DIR/well-typed-edition-2024.rs:109:14
249+
|
250+
LL | let [&mut ref mut x] = &mut [&0];
251+
| ^^^^^^^^^^^^^^
252+
help: consider removing `&mut` from the pattern
253+
|
254+
LL - let [&mut ref mut x] = &mut [&0];
255+
LL + let [ref mut x] = &mut [&0];
256+
|
257+
258258
error: aborting due to 17 previous errors
259259

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

0 commit comments

Comments
 (0)