Skip to content

Commit c364a4e

Browse files
Assign E0784 to union expression error
1 parent 76d247c commit c364a4e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

compiler/rustc_error_codes/src/error_codes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ E0780: include_str!("./error_codes/E0780.md"),
478478
E0781: include_str!("./error_codes/E0781.md"),
479479
E0782: include_str!("./error_codes/E0782.md"),
480480
E0783: include_str!("./error_codes/E0783.md"),
481+
E0784: include_str!("./error_codes/E0784.md"),
481482
;
482483
// E0006, // merged with E0005
483484
// E0008, // cannot bind by-move into a pattern guard
@@ -636,7 +637,7 @@ E0783: include_str!("./error_codes/E0783.md"),
636637
E0711, // a feature has been declared with conflicting stability attributes
637638
E0717, // rustc_promotable without stability attribute
638639
// E0721, // `await` keyword
639-
// E0723, unstable feature in `const` context
640+
// E0723, unstable feature in `const` context
640641
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
641642
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
642643
E0772, // `'static' obligation coming from `impl dyn Trait {}` or `impl Foo for dyn Bar {}`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
A union expression does not have exactly one field.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0784
6+
union Bird {
7+
pigeon: u8,
8+
turtledove: u16,
9+
}
10+
11+
let bird = Bird {}; // error
12+
let bird = Bird { pigeon: 0, turtledove: 1 }; // error
13+
```
14+
15+
The key property of unions is that all fields of a union share common storage.
16+
As a result, writes to one field of a union can overwrite its other fields, and
17+
size of a union is determined by the size of its largest field.
18+
19+
You can find more information about the union types in the [Rust reference].
20+
21+
Working example:
22+
23+
```
24+
union Bird {
25+
pigeon: u8,
26+
turtledove: u16,
27+
}
28+
29+
let bird = Bird { pigeon: 0 }; // OK
30+
```
31+
32+
[Rust reference]: https://doc.rust-lang.org/reference/items/unions.html

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13041304
// Make sure the programmer specified correct number of fields.
13051305
if kind_name == "union" {
13061306
if ast_fields.len() != 1 {
1307-
tcx.sess.span_err(span, "union expressions should have exactly one field");
1307+
struct_span_err!(
1308+
tcx.sess,
1309+
span,
1310+
E0784,
1311+
"union expressions should have exactly one field",
1312+
)
1313+
.emit();
13081314
}
13091315
} else if check_completeness && !error_happened && !remaining_fields.is_empty() {
13101316
let no_accessible_remaining_fields = remaining_fields

0 commit comments

Comments
 (0)