Skip to content

Commit 14dc9fc

Browse files
committed
Auto merge of #31232 - stepancheg:enum-univariant, r=nrc
``` enum Univariant { X = 17 } ``` Fixes #10292
2 parents 654f68d + 641267e commit 14dc9fc

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

src/librustc_typeck/check/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -4250,14 +4250,9 @@ pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
42504250
let def_id = ccx.tcx.map.local_def_id(id);
42514251
let hint = *ccx.tcx.lookup_repr_hints(def_id).get(0).unwrap_or(&attr::ReprAny);
42524252

4253-
if hint != attr::ReprAny && vs.len() <= 1 {
4254-
if vs.len() == 1 {
4255-
span_err!(ccx.tcx.sess, sp, E0083,
4256-
"unsupported representation for univariant enum");
4257-
} else {
4258-
span_err!(ccx.tcx.sess, sp, E0084,
4259-
"unsupported representation for zero-variant enum");
4260-
};
4253+
if hint != attr::ReprAny && vs.is_empty() {
4254+
span_err!(ccx.tcx.sess, sp, E0084,
4255+
"unsupported representation for zero-variant enum");
42614256
}
42624257

42634258
do_check(ccx, vs, id, hint);

src/librustc_typeck/diagnostics.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1062,13 +1062,6 @@ Note also that without a representation manually defined, the compiler will
10621062
optimize by using the smallest integer type possible.
10631063
"##,
10641064

1065-
E0083: r##"
1066-
At present, it's not possible to define a custom representation for an enum with
1067-
a single variant. As a workaround you can add a `Dummy` variant.
1068-
1069-
See: https://github.com/rust-lang/rust/issues/10292
1070-
"##,
1071-
10721065
E0084: r##"
10731066
It is impossible to define an integer type to be used to represent zero-variant
10741067
enum values because there are no zero-variant enum values. There is no way to
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2016 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+
12+
use std::mem;
13+
14+
// Univariant C-like enum
15+
#[repr(i32)]
16+
enum Univariant {
17+
X = 17
18+
}
19+
20+
#[repr(u16)]
21+
enum UnivariantWithoutDescr {
22+
Y
23+
}
24+
25+
pub fn main() {
26+
{
27+
assert_eq!(4, mem::size_of::<Univariant>());
28+
assert_eq!(17, Univariant::X as i32);
29+
30+
let enums: &[Univariant] =
31+
&[Univariant::X, Univariant::X, Univariant::X];
32+
let ints: &[i32] = unsafe { mem::transmute(enums) };
33+
// check it has the same memory layout as i32
34+
assert_eq!(&[17, 17, 17], ints);
35+
}
36+
37+
{
38+
assert_eq!(2, mem::size_of::<UnivariantWithoutDescr>());
39+
let descr = UnivariantWithoutDescr::Y as u16;
40+
41+
let enums: &[UnivariantWithoutDescr] =
42+
&[UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y, UnivariantWithoutDescr::Y];
43+
let ints: &[u16] = unsafe { mem::transmute(enums) };
44+
// check it has the same memory layout as u16
45+
assert_eq!(&[descr, descr, descr], ints);
46+
}
47+
}

0 commit comments

Comments
 (0)