Skip to content

Commit 6968e53

Browse files
authored
Rollup merge of #64161 - estebank:point-variant, r=Centril
Point at variant on pattern field count mismatch
2 parents 37a022e + 24d0a01 commit 6968e53

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/librustc_typeck/check/pat.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -675,21 +675,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
675675
self.tcx.check_stability(variant.fields[i].did, Some(pat.hir_id), subpat.span);
676676
}
677677
} else {
678-
let subpats_ending = if subpats.len() == 1 { "" } else { "s" };
679-
let fields_ending = if variant.fields.len() == 1 { "" } else { "s" };
680-
struct_span_err!(tcx.sess, pat.span, E0023,
681-
"this pattern has {} field{}, but the corresponding {} has {} field{}",
682-
subpats.len(), subpats_ending, res.descr(),
683-
variant.fields.len(), fields_ending)
684-
.span_label(pat.span, format!("expected {} field{}, found {}",
685-
variant.fields.len(), fields_ending, subpats.len()))
686-
.emit();
678+
// Pattern has wrong number of fields.
679+
self.e0023(pat.span, res, &subpats, &variant.fields);
687680
on_error();
688681
return tcx.types.err;
689682
}
690683
pat_ty
691684
}
692685

686+
fn e0023(&self, pat_span: Span, res: Res, subpats: &'tcx [P<Pat>], fields: &[ty::FieldDef]) {
687+
let subpats_ending = if subpats.len() == 1 { "" } else { "s" };
688+
let fields_ending = if fields.len() == 1 { "" } else { "s" };
689+
let res_span = self.tcx.def_span(res.def_id());
690+
struct_span_err!(
691+
self.tcx.sess,
692+
pat_span,
693+
E0023,
694+
"this pattern has {} field{}, but the corresponding {} has {} field{}",
695+
subpats.len(),
696+
subpats_ending,
697+
res.descr(),
698+
fields.len(),
699+
fields_ending,
700+
)
701+
.span_label(pat_span, format!(
702+
"expected {} field{}, found {}",
703+
fields.len(),
704+
fields_ending,
705+
subpats.len(),
706+
))
707+
.span_label(res_span, format!("{} defined here", res.descr()))
708+
.emit();
709+
}
710+
693711
fn check_pat_tuple(
694712
&self,
695713
span: Span,

src/test/ui/error-codes/E0023.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
22
--> $DIR/E0023.rs:10:9
33
|
4+
LL | Apple(String, String),
5+
| --------------------- tuple variant defined here
6+
...
47
LL | Fruit::Apple(a) => {},
58
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
69

710
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
811
--> $DIR/E0023.rs:11:9
912
|
13+
LL | Apple(String, String),
14+
| --------------------- tuple variant defined here
15+
...
1016
LL | Fruit::Apple(a, b, c) => {},
1117
| ^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
1218

1319
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
1420
--> $DIR/E0023.rs:12:9
1521
|
22+
LL | Pear(u32),
23+
| --------- tuple variant defined here
24+
...
1625
LL | Fruit::Pear(1, 2) => {},
1726
| ^^^^^^^^^^^^^^^^^ expected 1 field, found 2
1827

src/test/ui/match/match-pattern-field-mismatch.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields
22
--> $DIR/match-pattern-field-mismatch.rs:10:11
33
|
4+
LL | Rgb(usize, usize, usize),
5+
| ------------------------ tuple variant defined here
6+
...
47
LL | Color::Rgb(_, _) => { }
58
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
69

src/test/ui/pattern/pat-tuple-overfield.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ LL | (1, 2, .., 3, 4) => {}
1919
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
2020
--> $DIR/pat-tuple-overfield.rs:10:9
2121
|
22+
LL | struct S(u8, u8, u8);
23+
| --------------------- tuple struct defined here
24+
...
2225
LL | S(1, 2, 3, 4) => {}
2326
| ^^^^^^^^^^^^^ expected 3 fields, found 4
2427

2528
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
2629
--> $DIR/pat-tuple-overfield.rs:12:9
2730
|
31+
LL | struct S(u8, u8, u8);
32+
| --------------------- tuple struct defined here
33+
...
2834
LL | S(1, 2, .., 3, 4) => {}
2935
| ^^^^^^^^^^^^^^^^^ expected 3 fields, found 4
3036

src/test/ui/pattern/pattern-error-continue.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ LL | A::D(_) => (),
1515
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
1616
--> $DIR/pattern-error-continue.rs:17:9
1717
|
18+
LL | B(isize, isize),
19+
| --------------- tuple variant defined here
20+
...
1821
LL | A::B(_, _, _) => (),
1922
| ^^^^^^^^^^^^^ expected 2 fields, found 3
2023

0 commit comments

Comments
 (0)