Skip to content

Commit 102b912

Browse files
committed
let-else: build out ref/ref mut tests, with/without explicit annotations
expands issue 89960
1 parent 61bcd8d commit 102b912

5 files changed

+208
-19
lines changed

src/test/ui/let-else/issue-89960.rs

-7
This file was deleted.

src/test/ui/let-else/issue-89960.stderr

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// check-pass
2+
3+
#![feature(let_else)]
4+
#![allow(unused_variables)]
5+
6+
fn ref_() {
7+
let bytes: Vec<u8> = b"Hello"[..].to_vec();
8+
let some = Some(bytes);
9+
10+
let Some(ref a) = Some(()) else { return };
11+
12+
// | ref | type annotation | & |
13+
// | --- | --------------- | - |
14+
// | x | x | | error
15+
// | x | x | x | error
16+
// | | x | | error
17+
// | | x | x | error
18+
// | x | | |
19+
let Some(ref a) = some else { return }; // OK
20+
let b: &[u8] = a;
21+
22+
// | x | | x |
23+
let Some(ref a) = &some else { return }; // OK
24+
let b: &[u8] = a;
25+
26+
27+
// | | | x |
28+
let Some(a) = &some else { return }; // OK
29+
let b: &[u8] = a;
30+
31+
let Some(a): Option<&[u8]> = some.as_deref() else { return }; // OK
32+
let b: &[u8] = a;
33+
let Some(ref a): Option<&[u8]> = some.as_deref() else { return }; // OK
34+
let b: &[u8] = a;
35+
}
36+
37+
fn ref_mut() {
38+
// This `ref mut` case had an ICE, see issue #89960
39+
let Some(ref mut a) = Some(()) else { return };
40+
41+
let bytes: Vec<u8> = b"Hello"[..].to_vec();
42+
let mut some = Some(bytes);
43+
44+
// | ref mut | type annotation | &mut |
45+
// | ------- | --------------- | ---- |
46+
// | x | x | | error
47+
// | x | x | x | error
48+
// | | x | | error
49+
// | | x | x | error
50+
// | x | | |
51+
let Some(ref mut a) = some else { return }; // OK
52+
let b: &mut [u8] = a;
53+
54+
// | x | | x |
55+
let Some(ref mut a) = &mut some else { return }; // OK
56+
let b: &mut [u8] = a;
57+
58+
// | | | x |
59+
let Some(a) = &mut some else { return }; // OK
60+
let b: &mut [u8] = a;
61+
62+
let Some(a): Option<&mut [u8]> = some.as_deref_mut() else { return }; // OK
63+
let b: &mut [u8] = a;
64+
let Some(ref mut a): Option<&mut [u8]> = some.as_deref_mut() else { return }; // OK
65+
let b: &mut [u8] = a;
66+
}
67+
68+
fn main() {
69+
ref_();
70+
ref_mut();
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![feature(let_else)]
2+
#![allow(unused_variables)]
3+
4+
fn ref_() {
5+
let bytes: Vec<u8> = b"Hello"[..].to_vec();
6+
let some = Some(bytes);
7+
8+
let Some(ref a) = Some(()) else { return };
9+
10+
// | ref | type annotation | & |
11+
// | --- | --------------- | - |
12+
// | x | | | OK
13+
// | x | | x | OK
14+
// | | | x | OK
15+
// | x | x | |
16+
let Some(ref a): Option<&[u8]> = some else { return }; //~ ERROR mismatched types
17+
let b: & [u8] = a;
18+
19+
// | x | x | x |
20+
let Some(ref a): Option<&[u8]> = &some else { return }; //~ ERROR mismatched types
21+
let b: & [u8] = a;
22+
23+
// | | x | |
24+
let Some(a): Option<&[u8]> = some else { return }; //~ ERROR mismatched types
25+
let b: &[u8] = a;
26+
// | | x | x |
27+
let Some(a): Option<&[u8]> = &some else { return }; //~ ERROR mismatched types
28+
let b: &[u8] = a;
29+
}
30+
31+
fn ref_mut() {
32+
// This `ref mut` case had an ICE, see issue #89960
33+
let Some(ref mut a) = Some(()) else { return };
34+
35+
let bytes: Vec<u8> = b"Hello"[..].to_vec();
36+
let mut some = Some(bytes);
37+
38+
// | ref mut | type annotation | &mut |
39+
// | ------- | --------------- | ---- |
40+
// | x | | | OK
41+
// | x | | x | OK
42+
// | | | x | OK
43+
// | x | x | |
44+
let Some(ref mut a): Option<&mut [u8]> = some else { return }; //~ ERROR mismatched types
45+
let b: &mut [u8] = a;
46+
47+
// | x | x | x | (nope)
48+
let Some(ref mut a): Option<&mut [u8]> = &mut some else { return }; //~ ERROR mismatched types
49+
let b: &mut [u8] = a;
50+
51+
// | | x | |
52+
let Some(a): Option<&mut [u8]> = some else { return }; //~ ERROR mismatched types
53+
let b: &mut [u8] = a;
54+
// | | x | x |
55+
let Some(a): Option<&mut [u8]> = &mut some else { return }; //~ ERROR mismatched types
56+
let b: &mut [u8] = a;
57+
}
58+
59+
fn main() {
60+
ref_();
61+
ref_mut();
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/let-else-ref-bindings.rs:16:38
3+
|
4+
LL | let Some(ref a): Option<&[u8]> = some else { return };
5+
| ^^^^ expected `&[u8]`, found struct `Vec`
6+
|
7+
= note: expected enum `Option<&[u8]>`
8+
found enum `Option<Vec<u8>>`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/let-else-ref-bindings.rs:20:38
12+
|
13+
LL | let Some(ref a): Option<&[u8]> = &some else { return };
14+
| ^^^^^ expected enum `Option`, found `&Option<Vec<u8>>`
15+
|
16+
= note: expected enum `Option<&[u8]>`
17+
found reference `&Option<Vec<u8>>`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/let-else-ref-bindings.rs:24:34
21+
|
22+
LL | let Some(a): Option<&[u8]> = some else { return };
23+
| ^^^^ expected `&[u8]`, found struct `Vec`
24+
|
25+
= note: expected enum `Option<&[u8]>`
26+
found enum `Option<Vec<u8>>`
27+
28+
error[E0308]: mismatched types
29+
--> $DIR/let-else-ref-bindings.rs:27:34
30+
|
31+
LL | let Some(a): Option<&[u8]> = &some else { return };
32+
| ^^^^^ expected enum `Option`, found `&Option<Vec<u8>>`
33+
|
34+
= note: expected enum `Option<&[u8]>`
35+
found reference `&Option<Vec<u8>>`
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/let-else-ref-bindings.rs:44:46
39+
|
40+
LL | let Some(ref mut a): Option<&mut [u8]> = some else { return };
41+
| ^^^^ expected `&mut [u8]`, found struct `Vec`
42+
|
43+
= note: expected enum `Option<&mut [u8]>`
44+
found enum `Option<Vec<u8>>`
45+
46+
error[E0308]: mismatched types
47+
--> $DIR/let-else-ref-bindings.rs:48:46
48+
|
49+
LL | let Some(ref mut a): Option<&mut [u8]> = &mut some else { return };
50+
| ^^^^^^^^^ expected enum `Option`, found mutable reference
51+
|
52+
= note: expected enum `Option<&mut [u8]>`
53+
found mutable reference `&mut Option<Vec<u8>>`
54+
55+
error[E0308]: mismatched types
56+
--> $DIR/let-else-ref-bindings.rs:52:38
57+
|
58+
LL | let Some(a): Option<&mut [u8]> = some else { return };
59+
| ^^^^ expected `&mut [u8]`, found struct `Vec`
60+
|
61+
= note: expected enum `Option<&mut [u8]>`
62+
found enum `Option<Vec<u8>>`
63+
64+
error[E0308]: mismatched types
65+
--> $DIR/let-else-ref-bindings.rs:55:38
66+
|
67+
LL | let Some(a): Option<&mut [u8]> = &mut some else { return };
68+
| ^^^^^^^^^ expected enum `Option`, found mutable reference
69+
|
70+
= note: expected enum `Option<&mut [u8]>`
71+
found mutable reference `&mut Option<Vec<u8>>`
72+
73+
error: aborting due to 8 previous errors
74+
75+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)