Skip to content

Commit 3e7908f

Browse files
committed
Auto merge of #42068 - petrochenkov:ustab, r=nikomatsakis
Stabilize unions with `Copy` fields and no destructor What else is needed: - [x] Documentation (rust-lang/reference#57). - [x] Making assignments to `Copy` union fields safe (#42083). - [ ] Backport? (The "stabilization decision" is from [Apr 13](#32836 (comment)), it's just this PR is late.) cc #32836 r? @nikomatsakis
2 parents 5579677 + 73c73e4 commit 3e7908f

32 files changed

+42
-62
lines changed

src/libcollections/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
#![feature(trusted_len)]
6666
#![feature(unicode)]
6767
#![feature(unique)]
68-
#![feature(untagged_unions)]
6968
#![cfg_attr(not(test), feature(str_checked_slicing))]
7069
#![cfg_attr(test, feature(rand, test))]
7170
#![feature(offset_to)]

src/librustc/middle/stability.rs

+21
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,27 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
625625
}
626626
}
627627

628+
// There's no good place to insert stability check for non-Copy unions,
629+
// so semi-randomly perform it here in stability.rs
630+
hir::ItemUnion(..) if !self.tcx.sess.features.borrow().untagged_unions => {
631+
let def_id = self.tcx.hir.local_def_id(item.id);
632+
let adt_def = self.tcx.adt_def(def_id);
633+
let ty = self.tcx.type_of(def_id);
634+
635+
if adt_def.has_dtor(self.tcx) {
636+
emit_feature_err(&self.tcx.sess.parse_sess,
637+
"untagged_unions", item.span, GateIssue::Language,
638+
"unions with `Drop` implementations are unstable");
639+
} else {
640+
let param_env = self.tcx.param_env(def_id);
641+
if !param_env.can_type_implement_copy(self.tcx, ty, item.span).is_ok() {
642+
emit_feature_err(&self.tcx.sess.parse_sess,
643+
"untagged_unions", item.span, GateIssue::Language,
644+
"unions with non-`Copy` fields are unstable");
645+
}
646+
}
647+
}
648+
628649
_ => (/* pass */)
629650
}
630651
intravisit::walk_item(self, item);

src/librustc_data_structures/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#![feature(nonzero)]
3030
#![feature(unboxed_closures)]
3131
#![feature(fn_traits)]
32-
#![feature(untagged_unions)]
3332
#![feature(associated_consts)]
3433
#![feature(unsize)]
3534
#![feature(i128_type)]

src/libsyntax/feature_gate.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1207,12 +1207,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
12071207
}
12081208
}
12091209

1210-
ast::ItemKind::Union(..) => {
1211-
gate_feature_post!(&self, untagged_unions,
1212-
i.span,
1213-
"unions are unstable and possibly buggy");
1214-
}
1215-
12161210
ast::ItemKind::DefaultImpl(..) => {
12171211
gate_feature_post!(&self, optin_builtin_traits,
12181212
i.span,

src/test/compile-fail/borrowck/borrowck-union-borrow-nested.rs

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-tidy-linelength
12-
13-
#![feature(untagged_unions)]
14-
1511
#[derive(Clone, Copy)]
1612
struct S {
1713
a: u8,

src/test/compile-fail/borrowck/borrowck-union-borrow.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// ignore-tidy-linelength
1212

13-
#![feature(untagged_unions)]
14-
1513
#[derive(Clone, Copy)]
1614
union U {
1715
a: u8,

src/test/compile-fail/borrowck/borrowck-union-uninitialized.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
struct S {
1412
a: u8,
1513
}

src/test/compile-fail/privacy/union-field-privacy-1.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
mod m {
1412
pub union U {
1513
pub a: u8,

src/test/compile-fail/privacy/union-field-privacy-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
mod m {
1412
pub union U {
1513
pub a: u8,

src/test/compile-fail/union/union-const-eval.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
a: usize,
1513
b: usize,

src/test/compile-fail/union/union-const-pat.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
a: usize,
1513
b: usize,

src/test/compile-fail/union/union-derive.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// Most traits cannot be derived for unions.
1212

13-
#![feature(untagged_unions)]
14-
1513
#[derive(
1614
PartialEq, //~ ERROR this trait cannot be derived for unions
1715
PartialOrd, //~ ERROR this trait cannot be derived for unions

src/test/compile-fail/union/union-empty.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {} //~ ERROR unions cannot have zero fields
1412

1513
fn main() {}

src/test/compile-fail/union/union-feature-gate.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,28 @@
1010

1111
// gate-test-untagged_unions
1212

13-
union U { //~ ERROR unions are unstable and possibly buggy
13+
union U1 { // OK
1414
a: u8,
1515
}
1616

17+
union U2<T: Copy> { // OK
18+
a: T,
19+
}
20+
21+
union U3 { //~ ERROR unions with non-`Copy` fields are unstable
22+
a: String,
23+
}
24+
25+
union U4<T> { //~ ERROR unions with non-`Copy` fields are unstable
26+
a: T,
27+
}
28+
29+
union U5 { //~ ERROR unions with `Drop` implementations are unstable
30+
a: u8,
31+
}
32+
33+
impl Drop for U5 {
34+
fn drop(&mut self) {}
35+
}
36+
1737
fn main() {}

src/test/compile-fail/union/union-fields.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
a: u8,
1513
b: u16,

src/test/compile-fail/union/union-generic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
use std::rc::Rc;
1412

1513
union U<T: Copy> {

src/test/compile-fail/union/union-lint-dead-code.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
1211
#![deny(dead_code)]
1312

1413
union Foo {

src/test/compile-fail/union/union-repr-c.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
1211
#![allow(unused)]
1312
#![deny(improper_ctypes)]
1413

src/test/compile-fail/union/union-suggest-field.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
principal: u8,
1513
}

src/test/debuginfo/union-smoke.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#![allow(unused)]
3535
#![feature(omit_gdb_pretty_printer_section)]
3636
#![omit_gdb_pretty_printer_section]
37-
#![feature(untagged_unions)]
3837

3938
union U {
4039
a: (u8, u8),

src/test/run-pass/union/auxiliary/union.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
pub union U {
1412
pub a: u8,
1513
pub b: u16,

src/test/run-pass/union/union-backcomp.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
macro_rules! union {
1412
() => (struct S;)
1513
}

src/test/run-pass/union/union-basic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
// FIXME: This test case makes little-endian assumptions.
1414
// ignore-s390x
1515

16-
#![feature(untagged_unions)]
17-
1816
extern crate union;
1917
use std::mem::{size_of, align_of, zeroed};
2018

src/test/run-pass/union/union-c-interop.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
#[derive(Clone, Copy)]
1412
#[repr(C)]
1513
struct LARGE_INTEGER_U {

src/test/run-pass/union/union-const-trans.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
a: u64,
1513
b: u64,

src/test/run-pass/union/union-inherent-method.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
union U {
1412
a: u8,
1513
}

src/test/run-pass/union/union-macro.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
macro_rules! duplicate {
1412
($i: item) => {
1513
mod m1 {

src/test/run-pass/union/union-pat-refutability.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
#[repr(u32)]
1412
enum Tag { I, F }
1513

src/test/run-pass/union/union-trait-impl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
use std::fmt;
1412

1513
union U {

src/test/run-pass/union/union-transmute.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
extern crate core;
1412
use core::f32;
1513

src/test/rustdoc/union.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(untagged_unions)]
12-
1311
// @has union/union.U.html
1412
pub union U {
1513
// @has - //pre "pub a: u8"

src/test/ui/print_type_sizes/packed.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
// aligned (while on most it is 8-byte aligned) and so the resulting
1919
// padding and overall computed sizes can be quite different.
2020

21-
#![feature(untagged_unions)]
22-
2321
#![allow(dead_code)]
2422

2523
#[derive(Default)]

0 commit comments

Comments
 (0)