Skip to content

Commit 97bf386

Browse files
& constrains inference
1 parent 27717e6 commit 97bf386

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

text/3627-match-ergonomics-2024.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,58 @@ let &ref mut foo = &mut 42;
165165
let _: &mut u8 = foo;
166166
```
167167

168+
However, if the type of the scrutinee is unknown, an `&` pattern will still
169+
constrain inference to force it to be a shared reference.
170+
171+
```rust
172+
//! All editions
173+
fn generic<R: Ref>() -> (R, bool) {
174+
R::meow()
175+
}
176+
177+
trait Ref: Sized {
178+
fn meow() -> (Self, bool);
179+
}
180+
181+
impl Ref for &'static [(); 0] {
182+
fn meow() -> (Self, bool) {
183+
(&[], false)
184+
}
185+
}
186+
187+
impl Ref for &'static mut [(); 0] {
188+
fn meow() -> (Self, bool) {
189+
(&mut [], true)
190+
}
191+
}
192+
193+
fn main() {
194+
let (&_, b) = generic();
195+
assert!(!b);
196+
}
197+
```
198+
199+
```rust
200+
//! All editions
201+
fn generic<R: Ref>() -> R {
202+
R::meow()
203+
}
204+
205+
trait Ref: Sized {
206+
fn meow() -> Self;
207+
}
208+
209+
impl Ref for &'static mut [(); 0] {
210+
fn meow() -> Self {
211+
&mut []
212+
}
213+
}
214+
215+
fn main() {
216+
let &_ = generic(); //~ERROR[E0277]: the trait bound `&_: Ref` is not satisfied
217+
}
218+
```
219+
168220
## Edition 2024: `&` and `&mut` can match against inherited references
169221

170222
When the default binding mode is `ref` or `ref mut`, `&` and `&mut` patterns can

0 commit comments

Comments
 (0)