Skip to content

Commit 485927d

Browse files
daxpeddaModProg
authored andcommitted
Account for unknown size with repr(C)
1 parent fc06965 commit 485927d

File tree

3 files changed

+10
-66
lines changed

3 files changed

+10
-66
lines changed

src/item.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ impl Item<'_> {
9898
/// Type of discriminant used.
9999
#[cfg_attr(test, derive(Debug))]
100100
pub enum Discriminant {
101+
/// The enum uses the default representation but has a non-unit variant or
102+
/// an enum with a C representation without an integer representation.
103+
Unknown,
101104
/// The enum has only a single variant.
102105
Single,
103106
/// The enum uses the default representation and has only unit variants.
104107
UnitDefault,
105-
/// The enum uses the default representation but has a non-unit variant.
106-
Default,
107108
/// The enum uses a non-default representation and has only unit variants.
108109
UnitRepr(Representation),
109110
/// The enum uses a non-default representation and has a non-unit variant.
@@ -118,6 +119,7 @@ impl Discriminant {
118119
}
119120

120121
let mut has_repr = None;
122+
let mut is_c = false;
121123

122124
for attr in attrs {
123125
if attr.path().is_ident("repr") {
@@ -126,7 +128,9 @@ impl Discriminant {
126128
list.parse_args_with(Punctuated::<Ident, Token![,]>::parse_terminated)?;
127129

128130
for ident in list {
129-
if let Some(repr) = Representation::parse(&ident) {
131+
if ident == "C" {
132+
is_c = true;
133+
} else if let Some(repr) = Representation::parse(&ident) {
130134
has_repr = Some(repr);
131135
break;
132136
}
@@ -145,10 +149,10 @@ impl Discriminant {
145149
} else {
146150
Self::Repr(repr)
147151
}
148-
} else if is_unit {
152+
} else if is_unit && !is_c {
149153
Self::UnitDefault
150154
} else {
151-
Self::Default
155+
Self::Unknown
152156
})
153157
}
154158
}

src/trait_/common_ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn build_ord_signature(item: &Item, trait_: &DeriveTrait, body: &TokenStream
128128
__other as isize,
129129
)
130130
},
131-
Discriminant::Default => {
131+
Discriminant::Unknown => {
132132
build_recursive_order(trait_, variants, &incomparable)
133133
}
134134
Discriminant::UnitRepr(repr) => quote! {

tests/discriminant.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -128,66 +128,6 @@ fn repr_with_discriminants_values() {
128128
assert_eq!(5, unsafe { *<*const _>::from(&Test::C).cast::<u8>() });
129129
}
130130

131-
#[test]
132-
fn repr_c() {
133-
#[repr(C)]
134-
enum Test {
135-
A,
136-
B,
137-
C,
138-
}
139-
140-
#[cfg(feature = "nightly")]
141-
{
142-
assert_eq!(0_isize, discriminant_value(&Test::A));
143-
assert_eq!(1_isize, discriminant_value(&Test::B));
144-
assert_eq!(2_isize, discriminant_value(&Test::C));
145-
}
146-
assert_eq!(0, Test::A as isize);
147-
assert_eq!(1, Test::B as isize);
148-
assert_eq!(2, Test::C as isize);
149-
}
150-
151-
#[test]
152-
fn repr_c_with_discriminants() {
153-
#[repr(C)]
154-
enum Test {
155-
A = 3,
156-
B,
157-
C,
158-
}
159-
160-
#[cfg(feature = "nightly")]
161-
{
162-
assert_eq!(3_isize, discriminant_value(&Test::A));
163-
assert_eq!(4_isize, discriminant_value(&Test::B));
164-
assert_eq!(5_isize, discriminant_value(&Test::C));
165-
}
166-
assert_eq!(3, Test::A as isize);
167-
assert_eq!(4, Test::B as isize);
168-
assert_eq!(5, Test::C as isize);
169-
}
170-
171-
#[test]
172-
fn repr_c_with_values() {
173-
#[repr(C)]
174-
enum Test {
175-
A(u8),
176-
B,
177-
C,
178-
}
179-
180-
#[cfg(feature = "nightly")]
181-
{
182-
assert_eq!(0_isize, discriminant_value(&Test::A(0)));
183-
assert_eq!(1_isize, discriminant_value(&Test::B));
184-
assert_eq!(2_isize, discriminant_value(&Test::C));
185-
}
186-
assert_eq!(0, unsafe { *<*const _>::from(&Test::A(0)).cast::<isize>() });
187-
assert_eq!(1, unsafe { *<*const _>::from(&Test::B).cast::<isize>() });
188-
assert_eq!(2, unsafe { *<*const _>::from(&Test::C).cast::<isize>() });
189-
}
190-
191131
#[test]
192132
fn repr_with_repr_c_with_values() {
193133
#[repr(C, u8)]

0 commit comments

Comments
 (0)