Skip to content

Commit 01799b7

Browse files
Rollup merge of #44786 - thombles:tk/i41314, r=petrochenkov
Improve diagnostics when attempting to match tuple enum variant with struct pattern Adds an extra note as below to explain that a tuple pattern was probably intended. ``` error[E0026]: variant `X::Y` does not have a field named `data` --> src/main.rs:18:16 | 18 | X::Y { data } => println!("The data is {}", data) | ^^^^ variant `X::Y` does not have field `data` error[E0027]: pattern does not mention field `0` --> src/main.rs:18:9 | 18 | X::Y { data } => println!("The data is {}", data) | ^^^^^^^^^^^^^ missing field `0` | = note: trying to match a tuple variant with a struct variant pattern ``` Solution for #41314.
2 parents c63546f + def660c commit 01799b7

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/librustc_typeck/check/_match.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
787787
for field in variant.fields
788788
.iter()
789789
.filter(|field| !used_fields.contains_key(&field.name)) {
790-
struct_span_err!(tcx.sess, span, E0027,
791-
"pattern does not mention field `{}`",
792-
field.name)
793-
.span_label(span, format!("missing field `{}`", field.name))
794-
.emit();
790+
let mut diag = struct_span_err!(tcx.sess, span, E0027,
791+
"pattern does not mention field `{}`",
792+
field.name);
793+
diag.span_label(span, format!("missing field `{}`", field.name));
794+
if variant.ctor_kind == CtorKind::Fn {
795+
diag.note("trying to match a tuple variant with a struct variant pattern");
796+
}
797+
diag.emit();
795798
}
796799
}
797800
}

src/test/ui/type-check/issue-41314.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum X {
12+
Y(u32)
13+
}
14+
15+
fn main() {
16+
match X::Y(0) {
17+
X::Y { number } => {}
18+
}
19+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0026]: variant `X::Y` does not have a field named `number`
2+
--> $DIR/issue-41314.rs:17:16
3+
|
4+
17 | X::Y { number } => {}
5+
| ^^^^^^ variant `X::Y` does not have field `number`
6+
7+
error[E0027]: pattern does not mention field `0`
8+
--> $DIR/issue-41314.rs:17:9
9+
|
10+
17 | X::Y { number } => {}
11+
| ^^^^^^^^^^^^^^^ missing field `0`
12+
|
13+
= note: trying to match a tuple variant with a struct variant pattern
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)