Skip to content

Commit 8fcfd6e

Browse files
committed
Add missing const edge case
1 parent 7b0e554 commit 8fcfd6e

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

compiler/rustc_typeck/src/expr_use_visitor.rs

+7
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
267267
if let ty::Adt(def, _) = place_ty.kind() {
268268
if def.variants.len() > 1 {
269269
needs_to_be_read = true;
270+
} else if let Some(variant) = def.variants.iter().next() {
271+
// If the pat kind is a Path we want to check whether the
272+
// variant contains at least one field. If that's the case,
273+
// we want to borrow discr.
274+
if matches!(pat.kind, PatKind::Path(..)) && variant.fields.len() > 0 {
275+
needs_to_be_read = true;
276+
}
270277
}
271278
} else {
272279
// If it is not ty::Adt, then it should be read
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// edition:2021
2+
3+
#[derive(Copy, Clone, PartialEq, Eq)]
4+
pub struct Opcode(pub u8);
5+
6+
impl Opcode {
7+
pub const OP1: Opcode = Opcode(0x1);
8+
}
9+
10+
pub fn example1(msg_type: Opcode) -> impl FnMut(&[u8]) {
11+
move |i| match msg_type {
12+
//~^ ERROR: non-exhaustive patterns: `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
13+
Opcode::OP1 => unimplemented!(),
14+
}
15+
}
16+
17+
#[derive(Copy, Clone, PartialEq, Eq)]
18+
pub struct Opcode2(Opcode);
19+
20+
impl Opcode2 {
21+
pub const OP2: Opcode2 = Opcode2(Opcode(0x1));
22+
}
23+
24+
25+
pub fn example2(msg_type: Opcode2) -> impl FnMut(&[u8]) {
26+
27+
move |i| match msg_type {
28+
//~^ ERROR: non-exhaustive patterns: `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
29+
Opcode2::OP2=> unimplemented!(),
30+
}
31+
}
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0004]: non-exhaustive patterns: `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
2+
--> $DIR/issue-88331.rs:11:20
3+
|
4+
LL | pub struct Opcode(pub u8);
5+
| -------------------------- `Opcode` defined here
6+
...
7+
LL | move |i| match msg_type {
8+
| ^^^^^^^^ patterns `Opcode(0_u8)` and `Opcode(2_u8..=u8::MAX)` not covered
9+
|
10+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
11+
= note: the matched value is of type `Opcode`
12+
13+
error[E0004]: non-exhaustive patterns: `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
14+
--> $DIR/issue-88331.rs:27:20
15+
|
16+
LL | pub struct Opcode2(Opcode);
17+
| --------------------------- `Opcode2` defined here
18+
...
19+
LL | move |i| match msg_type {
20+
| ^^^^^^^^ patterns `Opcode2(Opcode(0_u8))` and `Opcode2(Opcode(2_u8..=u8::MAX))` not covered
21+
|
22+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
23+
= note: the matched value is of type `Opcode2`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)