Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/ui/union/field_checks.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//@ reference: items.union.field-restrictions
//@ reference: items.union.field-copy
//@ reference: items.union.field-references
//@ reference: items.union.field-manually-drop
//@ reference: items.union.field-tuple
//@ reference: type.union.constraint
Comment on lines +1 to +6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on items.union.drop?

use std::mem::ManuallyDrop;

union U1 { // OK
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/union/field_checks.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:24:5
--> $DIR/field_checks.rs:30:5
|
LL | a: String,
| ^^^^^^^^^
Expand All @@ -11,7 +11,7 @@ LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ +

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:28:5
--> $DIR/field_checks.rs:34:5
|
LL | a: std::cell::RefCell<i32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | a: std::mem::ManuallyDrop<std::cell::RefCell<i32>>,
| +++++++++++++++++++++++ +

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:32:5
--> $DIR/field_checks.rs:38:5
|
LL | a: T,
| ^^^^
Expand All @@ -35,7 +35,7 @@ LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ +

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:44:5
--> $DIR/field_checks.rs:50:5
|
LL | nest: U5,
| ^^^^^^^^
Expand All @@ -47,7 +47,7 @@ LL | nest: std::mem::ManuallyDrop<U5>,
| +++++++++++++++++++++++ +

error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:48:5
--> $DIR/field_checks.rs:54:5
|
LL | nest: [U5; 0],
| ^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/union/union-backcomp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ run-pass
//@ reference: lex.keywords.weak.union

#![allow(path_statements)]
#![allow(dead_code)]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/union/union-copy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ reference: lang-types.copy.constraint
#[derive(Clone)]
union U {
a: u8
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/union/union-copy.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0204]: the trait `Copy` cannot be implemented for this type
--> $DIR/union-copy.rs:12:15
--> $DIR/union-copy.rs:13:15
|
LL | a: std::mem::ManuallyDrop<String>
| --------------------------------- this field does not implement `Copy`
Expand All @@ -8,7 +8,7 @@ LL | impl Copy for W {}
| ^
|
note: the `Copy` impl for `ManuallyDrop<String>` requires that `String: Copy`
--> $DIR/union-copy.rs:8:8
--> $DIR/union-copy.rs:9:8
|
LL | a: std::mem::ManuallyDrop<String>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/union/union-drop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ run-pass
//@ reference: items.union.drop

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on destructors.manually-suppressing?


#![allow(dead_code)]
#![allow(unused_variables)]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/union/union-empty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ reference: items.union.fieldless
union U {} //~ ERROR unions cannot have zero fields

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/union/union-empty.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unions cannot have zero fields
--> $DIR/union-empty.rs:1:1
--> $DIR/union-empty.rs:2:1
|
LL | union U {}
| ^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/union/union-fields-2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ reference: items.union.init.intro

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on items.union.pattern.one-field, patterns.struct.constraint-union, and expr.struct.field.union-constraint?

union U {
a: u8,
b: u16,
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/union/union-fields-2.stderr
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
error[E0784]: union expressions should have exactly one field
--> $DIR/union-fields-2.rs:7:13
--> $DIR/union-fields-2.rs:8:13
|
LL | let u = U {};
| ^

error[E0784]: union expressions should have exactly one field
--> $DIR/union-fields-2.rs:9:13
--> $DIR/union-fields-2.rs:10:13
|
LL | let u = U { a: 0, b: 1 };
| ^

error[E0560]: union `U` has no field named `c`
--> $DIR/union-fields-2.rs:10:29
--> $DIR/union-fields-2.rs:11:29
|
LL | let u = U { a: 0, b: 1, c: 2 };
| ^ `U` does not have this field
|
= note: all struct fields are already assigned

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

error[E0784]: union expressions should have exactly one field
--> $DIR/union-fields-2.rs:12:13
--> $DIR/union-fields-2.rs:13:13
|
LL | let u = U { ..u };
| ^

error[E0436]: functional record update syntax requires a struct
--> $DIR/union-fields-2.rs:12:19
--> $DIR/union-fields-2.rs:13:19
|
LL | let u = U { ..u };
| ^

error: union patterns should have exactly one field
--> $DIR/union-fields-2.rs:15:9
--> $DIR/union-fields-2.rs:16:9
|
LL | let U {} = u;
| ^^^^

error: union patterns should have exactly one field
--> $DIR/union-fields-2.rs:17:9
--> $DIR/union-fields-2.rs:18:9
|
LL | let U { a, b } = u;
| ^^^^^^^^^^

error: union patterns should have exactly one field
--> $DIR/union-fields-2.rs:18:9
--> $DIR/union-fields-2.rs:19:9
|
LL | let U { a, b, c } = u;
| ^^^^^^^^^^^^^

error[E0026]: union `U` does not have a field named `c`
--> $DIR/union-fields-2.rs:18:19
--> $DIR/union-fields-2.rs:19:19
|
LL | let U { a, b, c } = u;
| ^ union `U` does not have this field

error: union patterns should have exactly one field
--> $DIR/union-fields-2.rs:20:9
--> $DIR/union-fields-2.rs:21:9
|
LL | let U { .. } = u;
| ^^^^^^^^

error: `..` cannot be used in union patterns
--> $DIR/union-fields-2.rs:20:9
--> $DIR/union-fields-2.rs:21:9
|
LL | let U { .. } = u;
| ^^^^^^^^

error: `..` cannot be used in union patterns
--> $DIR/union-fields-2.rs:22:9
--> $DIR/union-fields-2.rs:23:9
|
LL | let U { a, .. } = u;
| ^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/union/union-overwrite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//@ run-pass
//@ reference: items.union.common-storage
//@ reference: items.union.fields.read
//@ reference: type.union.constraint

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what sense is this file testing type.union.constraint?


#[repr(C)]
#[derive(Copy, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/union/union-pat-refutability.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//@ run-pass
//@ reference: items.union.pattern

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//@ reference: items.union.pattern

The cite of the parent rule should be dropped; the child rules are cited and the tooling flags this.

//@ reference: items.union.pattern.intro
//@ reference: items.union.pattern.safety
//@ reference: items.union.pattern.subpattern
Comment on lines +3 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on patterns.struct.refutable?

@ehuss ehuss Jul 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items.union.pattern.subpattern has been deleted in rust-lang/reference#2303.

View changes since the review


#![allow(dead_code)]

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/union/union-unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//@ reference: items.union.fields.read-safety
//@ reference: safety.unsafe-union-access
//@ reference: type.union.safety
Comment on lines +2 to +3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on items.union.fields.write-safety and items.union.pattern.safety?

use std::cell::RefCell;
use std::mem::ManuallyDrop;
use std::ops::Deref;
Expand Down
28 changes: 14 additions & 14 deletions tests/ui/union/union-unsafe.stderr
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:36:6
--> $DIR/union-unsafe.rs:39:6
|
LL | *(u.p) = 13;
| ^^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:50:26
--> $DIR/union-unsafe.rs:53:26
|
LL | let _p = &raw const *(u.p);
| ^^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:62:6
--> $DIR/union-unsafe.rs:65:6
|
LL | *u3.a = T::default();
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:68:6
--> $DIR/union-unsafe.rs:71:6
|
LL | *u3.a = T::default();
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:76:13
--> $DIR/union-unsafe.rs:79:13
|
LL | let a = u1.a;
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:91:29
--> $DIR/union-unsafe.rs:94:29
|
LL | let _a = &raw const vec[u4.a];
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:93:14
--> $DIR/union-unsafe.rs:96:14
|
LL | let U1 { a } = u1;
| ^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:94:20
--> $DIR/union-unsafe.rs:97:20
|
LL | if let U1 { a: 12 } = u1 {}
| ^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:95:25
--> $DIR/union-unsafe.rs:98:25
|
LL | if let Some(U1 { a: 13 }) = Some(u1) {}
| ^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:100:6
--> $DIR/union-unsafe.rs:103:6
|
LL | *u2.a = String::from("new");
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:104:6
--> $DIR/union-unsafe.rs:107:6
|
LL | *u3.a = 1;
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:108:6
--> $DIR/union-unsafe.rs:111:6
|
LL | *u3.a = String::from("new");
| ^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:117:28
--> $DIR/union-unsafe.rs:120:28
|
LL | let _p = &raw const (**a.b).c;
| ^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior

error[E0133]: access to union field is unsafe and requires unsafe function or block
--> $DIR/union-unsafe.rs:148:27
--> $DIR/union-unsafe.rs:151:27
|
LL | let _p = &raw const (*a.b).c;
| ^^^ access to union field
Expand Down
Loading