Skip to content

Commit 442e627

Browse files
committed
Auto merge of #87697 - GuillaumeGomez:add-e0784, r=nagisa
Assign E0784 error code for union expression errors
2 parents 4e886d6 + e500cd2 commit 442e627

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

compiler/rustc_error_codes/src/error_codes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ E0780: include_str!("./error_codes/E0780.md"),
479479
E0781: include_str!("./error_codes/E0781.md"),
480480
E0782: include_str!("./error_codes/E0782.md"),
481481
E0783: include_str!("./error_codes/E0783.md"),
482+
E0784: include_str!("./error_codes/E0784.md"),
482483
;
483484
// E0006, // merged with E0005
484485
// 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

src/test/ui/union/union-fields-2.mirunsafeck.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: union expressions should have exactly one field
1+
error[E0784]: union expressions should have exactly one field
22
--> $DIR/union-fields-2.rs:10:13
33
|
44
LL | let u = U {};
55
| ^
66

7-
error: union expressions should have exactly one field
7+
error[E0784]: union expressions should have exactly one field
88
--> $DIR/union-fields-2.rs:12:13
99
|
1010
LL | let u = U { a: 0, b: 1 };
@@ -18,13 +18,13 @@ LL | let u = U { a: 0, b: 1, c: 2 };
1818
|
1919
= note: available fields are: `a`, `b`
2020

21-
error: union expressions should have exactly one field
21+
error[E0784]: union expressions should have exactly one field
2222
--> $DIR/union-fields-2.rs:13:13
2323
|
2424
LL | let u = U { a: 0, b: 1, c: 2 };
2525
| ^
2626

27-
error: union expressions should have exactly one field
27+
error[E0784]: union expressions should have exactly one field
2828
--> $DIR/union-fields-2.rs:15:13
2929
|
3030
LL | let u = U { ..u };
@@ -80,5 +80,5 @@ LL | let U { a, .. } = u;
8080

8181
error: aborting due to 13 previous errors
8282

83-
Some errors have detailed explanations: E0026, E0436, E0560.
83+
Some errors have detailed explanations: E0026, E0436, E0560, E0784.
8484
For more information about an error, try `rustc --explain E0026`.

src/test/ui/union/union-fields-2.thirunsafeck.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: union expressions should have exactly one field
1+
error[E0784]: union expressions should have exactly one field
22
--> $DIR/union-fields-2.rs:10:13
33
|
44
LL | let u = U {};
55
| ^
66

7-
error: union expressions should have exactly one field
7+
error[E0784]: union expressions should have exactly one field
88
--> $DIR/union-fields-2.rs:12:13
99
|
1010
LL | let u = U { a: 0, b: 1 };
@@ -18,13 +18,13 @@ LL | let u = U { a: 0, b: 1, c: 2 };
1818
|
1919
= note: available fields are: `a`, `b`
2020

21-
error: union expressions should have exactly one field
21+
error[E0784]: union expressions should have exactly one field
2222
--> $DIR/union-fields-2.rs:13:13
2323
|
2424
LL | let u = U { a: 0, b: 1, c: 2 };
2525
| ^
2626

27-
error: union expressions should have exactly one field
27+
error[E0784]: union expressions should have exactly one field
2828
--> $DIR/union-fields-2.rs:15:13
2929
|
3030
LL | let u = U { ..u };
@@ -80,5 +80,5 @@ LL | let U { a, .. } = u;
8080

8181
error: aborting due to 13 previous errors
8282

83-
Some errors have detailed explanations: E0026, E0436, E0560.
83+
Some errors have detailed explanations: E0026, E0436, E0560, E0784.
8484
For more information about an error, try `rustc --explain E0026`.

0 commit comments

Comments
 (0)