Respect inferred enum repr in Rust code generator
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index fc129c6..f28d955 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -143,6 +143,7 @@
fn expand_enum(enm: &Enum) -> TokenStream {
let ident = &enm.ident;
let doc = &enm.doc;
+ let repr = enm.repr;
let variants = enm.variants.iter().map(|variant| {
let variant_ident = &variant.ident;
let discriminant = &variant.discriminant;
@@ -155,7 +156,7 @@
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct #ident {
- pub repr: u32,
+ pub repr: #repr,
}
#[allow(non_upper_case_globals)]
diff --git a/syntax/tokens.rs b/syntax/tokens.rs
index 9a1fee0..4ed264a 100644
--- a/syntax/tokens.rs
+++ b/syntax/tokens.rs
@@ -1,6 +1,6 @@
use crate::syntax::atom::Atom::*;
use crate::syntax::{
- Derive, Enum, ExternFn, ExternType, Receiver, Ref, Signature, Slice, Struct, Ty1, Type,
+ Atom, Derive, Enum, ExternFn, ExternType, Receiver, Ref, Signature, Slice, Struct, Ty1, Type,
TypeAlias, Var,
};
use proc_macro2::{Ident, Span, TokenStream};
@@ -75,6 +75,12 @@
}
}
+impl ToTokens for Atom {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ Ident::new(self.as_ref(), Span::call_site()).to_tokens(tokens);
+ }
+}
+
impl ToTokens for ExternType {
fn to_tokens(&self, tokens: &mut TokenStream) {
// Notional token range for error reporting purposes.
diff --git a/tests/ui/enum_match_without_wildcard.stderr b/tests/ui/enum_match_without_wildcard.stderr
index 93a8957..a5a0215 100644
--- a/tests/ui/enum_match_without_wildcard.stderr
+++ b/tests/ui/enum_match_without_wildcard.stderr
@@ -1,11 +1,11 @@
-error[E0004]: non-exhaustive patterns: `A { repr: 2u32..=std::u32::MAX }` not covered
+error[E0004]: non-exhaustive patterns: `A { repr: 2u8..=std::u8::MAX }` not covered
--> $DIR/enum_match_without_wildcard.rs:12:11
|
1 | #[cxx::bridge]
| -------------- `ffi::A` defined here
...
12 | match a {
- | ^ pattern `A { repr: 2u32..=std::u32::MAX }` not covered
+ | ^ pattern `A { repr: 2u8..=std::u8::MAX }` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `ffi::A`