Skip to content

Commit 2f730c1

Browse files
Match ergonomics 2024: Implement eat-one-layer
1 parent 1921968 commit 2f730c1

10 files changed

+472
-51
lines changed

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ declare_features! (
567567
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
568568
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
569569
(unstable, raw_ref_op, "1.41.0", Some(64490)),
570+
/// Makes `&` and `&mut` patterns eat only one layer of references in Rust 2024.
571+
(incomplete, ref_pat_eat_one_layer_2024, "CURRENT_RUSTC_VERSION", Some(123076)),
570572
/// Allows `&` and `&mut` patterns to consume match-ergonomics-inserted references.
571573
(incomplete, ref_pat_everywhere, "CURRENT_RUSTC_VERSION", Some(123076)),
572574
/// Allows using the `#[register_tool]` attribute.

compiler/rustc_hir_typeck/src/pat.rs

+69-51
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
294294
AdjustMode::Pass => (expected, def_bm, false),
295295
AdjustMode::Reset => (expected, INITIAL_BM, false),
296296
AdjustMode::ResetAndConsumeRef(mutbl) => {
297-
(expected, INITIAL_BM, def_bm.0 == ByRef::Yes(mutbl))
297+
let mutbls_match = def_bm.0 == ByRef::Yes(mutbl);
298+
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
299+
if mutbls_match {
300+
(expected, INITIAL_BM, true)
301+
} else {
302+
(expected, def_bm, false)
303+
}
304+
} else {
305+
(expected, INITIAL_BM, mutbls_match)
306+
}
298307
}
299308
AdjustMode::Peel => {
300309
let peeled = self.peel_off_references(pat, expected, def_bm);
@@ -2063,61 +2072,70 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20632072
pat_info: PatInfo<'tcx, '_>,
20642073
consumed_inherited_ref: bool,
20652074
) -> Ty<'tcx> {
2066-
let tcx = self.tcx;
2067-
let expected = self.shallow_resolve(expected);
2068-
let (ref_ty, inner_ty) = match self.check_dereferenceable(pat.span, expected, inner) {
2069-
Ok(()) => {
2070-
// `demand::subtype` would be good enough, but using `eqtype` turns
2071-
// out to be equally general. See (note_1) for details.
2072-
2073-
// Take region, inner-type from expected type if we can,
2074-
// to avoid creating needless variables. This also helps with
2075-
// the bad interactions of the given hack detailed in (note_1).
2076-
debug!("check_pat_ref: expected={:?}", expected);
2077-
match *expected.kind() {
2078-
ty::Ref(_, r_ty, r_mutbl) if r_mutbl == mutbl => (expected, r_ty),
2079-
_ => {
2080-
if consumed_inherited_ref && self.tcx.features().ref_pat_everywhere {
2081-
// We already matched against a match-ergonmics inserted reference,
2082-
// so we don't need to match against a reference from the original type.
2083-
// Save this infor for use in lowering later
2084-
self.typeck_results
2085-
.borrow_mut()
2086-
.skipped_ref_pats_mut()
2087-
.insert(pat.hir_id);
2088-
(expected, expected)
2089-
} else {
2090-
let inner_ty = self.next_ty_var(TypeVariableOrigin {
2091-
kind: TypeVariableOriginKind::TypeInference,
2092-
span: inner.span,
2093-
});
2094-
let ref_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
2095-
debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2096-
let err = self.demand_eqtype_pat_diag(
2097-
pat.span,
2098-
expected,
2099-
ref_ty,
2100-
pat_info.top_info,
2101-
);
2075+
if consumed_inherited_ref
2076+
&& pat.span.at_least_rust_2024()
2077+
&& self.tcx.features().ref_pat_eat_one_layer_2024
2078+
{
2079+
self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2080+
self.check_pat(inner, expected, pat_info);
2081+
expected
2082+
} else {
2083+
let tcx = self.tcx;
2084+
let expected = self.shallow_resolve(expected);
2085+
let (ref_ty, inner_ty) = match self.check_dereferenceable(pat.span, expected, inner) {
2086+
Ok(()) => {
2087+
// `demand::subtype` would be good enough, but using `eqtype` turns
2088+
// out to be equally general. See (note_1) for details.
2089+
2090+
// Take region, inner-type from expected type if we can,
2091+
// to avoid creating needless variables. This also helps with
2092+
// the bad interactions of the given hack detailed in (note_1).
2093+
debug!("check_pat_ref: expected={:?}", expected);
2094+
match *expected.kind() {
2095+
ty::Ref(_, r_ty, r_mutbl) if r_mutbl == mutbl => (expected, r_ty),
2096+
_ => {
2097+
if consumed_inherited_ref && self.tcx.features().ref_pat_everywhere {
2098+
// We already matched against a match-ergonmics inserted reference,
2099+
// so we don't need to match against a reference from the original type.
2100+
// Save this infor for use in lowering later
2101+
self.typeck_results
2102+
.borrow_mut()
2103+
.skipped_ref_pats_mut()
2104+
.insert(pat.hir_id);
2105+
(expected, expected)
2106+
} else {
2107+
let inner_ty = self.next_ty_var(TypeVariableOrigin {
2108+
kind: TypeVariableOriginKind::TypeInference,
2109+
span: inner.span,
2110+
});
2111+
let ref_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
2112+
debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2113+
let err = self.demand_eqtype_pat_diag(
2114+
pat.span,
2115+
expected,
2116+
ref_ty,
2117+
pat_info.top_info,
2118+
);
21022119

2103-
// Look for a case like `fn foo(&foo: u32)` and suggest
2104-
// `fn foo(foo: &u32)`
2105-
if let Some(mut err) = err {
2106-
self.borrow_pat_suggestion(&mut err, pat);
2107-
err.emit();
2120+
// Look for a case like `fn foo(&foo: u32)` and suggest
2121+
// `fn foo(foo: &u32)`
2122+
if let Some(mut err) = err {
2123+
self.borrow_pat_suggestion(&mut err, pat);
2124+
err.emit();
2125+
}
2126+
(ref_ty, inner_ty)
21082127
}
2109-
(ref_ty, inner_ty)
21102128
}
21112129
}
21122130
}
2113-
}
2114-
Err(guar) => {
2115-
let err = Ty::new_error(tcx, guar);
2116-
(err, err)
2117-
}
2118-
};
2119-
self.check_pat(inner, inner_ty, pat_info);
2120-
ref_ty
2131+
Err(guar) => {
2132+
let err = Ty::new_error(tcx, guar);
2133+
(err, err)
2134+
}
2135+
};
2136+
self.check_pat(inner, inner_ty, pat_info);
2137+
ref_ty
2138+
}
21212139
}
21222140

21232141
/// Create a reference type with a fresh region variable.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ symbols! {
14561456
receiver,
14571457
recursion_limit,
14581458
reexport_test_harness_main,
1459+
ref_pat_eat_one_layer_2024,
14591460
ref_pat_everywhere,
14601461
ref_unwind_safe_trait,
14611462
reference,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ edition: 2024
2+
//@ compile-flags: -Zunstable-options
3+
4+
pub fn main() {
5+
if let Some(Some(&x)) = &Some(&Some(0)) {
6+
//~^ ERROR: mismatched types
7+
let _: u32 = x;
8+
}
9+
if let Some(Some(&x)) = &Some(Some(&0)) {
10+
let _: &u32 = x;
11+
//~^ ERROR: mismatched types
12+
}
13+
if let Some(Some(&&x)) = &Some(Some(&0)) {
14+
//~^ ERROR: mismatched types
15+
let _: u32 = x;
16+
}
17+
if let Some(&Some(x)) = &Some(Some(0)) {
18+
//~^ ERROR: mismatched types
19+
let _: u32 = x;
20+
}
21+
if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
22+
//~^ ERROR: mismatched types
23+
let _: u32 = x;
24+
}
25+
if let Some(Some(&x)) = &Some(&Some(0)) {
26+
//~^ ERROR: mismatched types
27+
let _: u32 = x;
28+
}
29+
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
30+
//~^ ERROR: mismatched types
31+
let _: u32 = x;
32+
}
33+
if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
34+
//~^ ERROR: mismatched types
35+
let _: u32 = x;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:5:22
3+
|
4+
LL | if let Some(Some(&x)) = &Some(&Some(0)) {
5+
| ^^ --------------- this expression has type `&Option<&Option<{integer}>>`
6+
| |
7+
| expected integer, found `&_`
8+
|
9+
= note: expected type `{integer}`
10+
found reference `&_`
11+
help: consider removing `&` from the pattern
12+
|
13+
LL | if let Some(Some(x)) = &Some(&Some(0)) {
14+
| ~
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:10:23
18+
|
19+
LL | let _: &u32 = x;
20+
| ---- ^ expected `&u32`, found integer
21+
| |
22+
| expected due to this
23+
|
24+
help: consider borrowing here
25+
|
26+
LL | let _: &u32 = &x;
27+
| +
28+
29+
error[E0308]: mismatched types
30+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:13:23
31+
|
32+
LL | if let Some(Some(&&x)) = &Some(Some(&0)) {
33+
| ^^ --------------- this expression has type `&Option<Option<&{integer}>>`
34+
| |
35+
| expected integer, found `&_`
36+
|
37+
= note: expected type `{integer}`
38+
found reference `&_`
39+
help: consider removing `&` from the pattern
40+
|
41+
LL - if let Some(Some(&&x)) = &Some(Some(&0)) {
42+
LL + if let Some(Some(&x)) = &Some(Some(&0)) {
43+
|
44+
45+
error[E0308]: mismatched types
46+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:17:17
47+
|
48+
LL | if let Some(&Some(x)) = &Some(Some(0)) {
49+
| ^^^^^^^^ -------------- this expression has type `&Option<Option<{integer}>>`
50+
| |
51+
| expected `Option<{integer}>`, found `&_`
52+
|
53+
= note: expected enum `Option<{integer}>`
54+
found reference `&_`
55+
56+
error[E0308]: mismatched types
57+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:21:22
58+
|
59+
LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
60+
| ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>`
61+
| |
62+
| expected integer, found `&mut _`
63+
|
64+
= note: expected type `{integer}`
65+
found mutable reference `&mut _`
66+
note: to declare a mutable binding use: `mut x`
67+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:21:22
68+
|
69+
LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
70+
| ^^^^^^
71+
help: consider removing `&mut` from the pattern
72+
|
73+
LL | if let Some(Some(x)) = &mut Some(&mut Some(0)) {
74+
| ~
75+
76+
error[E0308]: mismatched types
77+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:25:22
78+
|
79+
LL | if let Some(Some(&x)) = &Some(&Some(0)) {
80+
| ^^ --------------- this expression has type `&Option<&Option<{integer}>>`
81+
| |
82+
| expected integer, found `&_`
83+
|
84+
= note: expected type `{integer}`
85+
found reference `&_`
86+
help: consider removing `&` from the pattern
87+
|
88+
LL | if let Some(Some(x)) = &Some(&Some(0)) {
89+
| ~
90+
91+
error[E0308]: mismatched types
92+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:29:27
93+
|
94+
LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
95+
| ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>`
96+
| |
97+
| expected integer, found `&_`
98+
|
99+
= note: expected type `{integer}`
100+
found reference `&_`
101+
help: consider removing `&` from the pattern
102+
|
103+
LL | if let Some(&mut Some(x)) = &Some(&mut Some(0)) {
104+
| ~
105+
106+
error[E0308]: mismatched types
107+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:33:23
108+
|
109+
LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
110+
| ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>`
111+
| |
112+
| expected integer, found `&mut _`
113+
|
114+
= note: expected type `{integer}`
115+
found mutable reference `&mut _`
116+
note: to declare a mutable binding use: `mut x`
117+
--> $DIR/feature-gate-ref_pat_eat_one_layer_2024.rs:33:23
118+
|
119+
LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
120+
| ^^^^^^
121+
help: consider removing `&mut` from the pattern
122+
|
123+
LL | if let Some(&Some(x)) = &mut Some(&Some(0)) {
124+
| ~
125+
126+
error: aborting due to 8 previous errors
127+
128+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//@ edition: 2021
2+
#![allow(incomplete_features)]
3+
#![feature(ref_pat_eat_one_layer_2024)]
4+
pub fn main() {
5+
if let Some(Some(&x)) = &Some(&Some(0)) {
6+
//~^ ERROR: mismatched types
7+
let _: u32 = x;
8+
}
9+
if let Some(Some(&x)) = &Some(Some(&0)) {
10+
let _: &u32 = x;
11+
//~^ ERROR: mismatched types
12+
}
13+
if let Some(Some(&&x)) = &Some(Some(&0)) {
14+
//~^ ERROR: mismatched types
15+
let _: u32 = x;
16+
}
17+
if let Some(&Some(x)) = &Some(Some(0)) {
18+
//~^ ERROR: mismatched types
19+
let _: u32 = x;
20+
}
21+
if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
22+
//~^ ERROR: mismatched types
23+
let _: u32 = x;
24+
}
25+
if let Some(Some(&x)) = &Some(&Some(0)) {
26+
//~^ ERROR: mismatched types
27+
let _: u32 = x;
28+
}
29+
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
30+
//~^ ERROR: mismatched types
31+
let _: u32 = x;
32+
}
33+
if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
34+
//~^ ERROR: mismatched types
35+
let _: u32 = x;
36+
}
37+
}

0 commit comments

Comments
 (0)