-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow different reprs to still work #687
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7fa2798
Allow different reprs to still work
jsdw d6edb07
fmt
jsdw fe89db6
clippy
jsdw d123be3
clippy fix
jsdw 7775bd8
Fix a comment typo
jsdw 22b56bc
clarify a comment
jsdw b1499bc
Bump version to 3.7.2
jsdw 2883ce5
fmt
jsdw 260e04b
Improve error when discriminant indexes outside of 0..=255 range
jsdw e64cc39
Fix UI tests and check i32 repr too
jsdw 0028a39
fmt
jsdw e5ad77e
clippy
jsdw 89091b6
fix ui tests for Rust 1.81
jsdw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,7 @@ pub fn const_eval_check_variant_indexes( | |
/// is found, fall back to the discriminant or just the variant index. | ||
pub fn variant_index(v: &Variant, i: usize) -> TokenStream { | ||
// first look for an attribute | ||
let index = find_meta_item(v.attrs.iter(), |meta| { | ||
let mut index = find_meta_item(v.attrs.iter(), |meta| { | ||
if let Meta::NameValue(ref nv) = meta { | ||
if nv.path.is_ident("index") { | ||
if let Expr::Lit(ExprLit { lit: Lit::Int(ref v), .. }) = nv.value { | ||
|
@@ -112,13 +112,21 @@ pub fn variant_index(v: &Variant, i: usize) -> TokenStream { | |
None | ||
}); | ||
|
||
// then fallback to discriminant or just index | ||
index.map(|i| quote! { #i }).unwrap_or_else(|| { | ||
v.discriminant | ||
.as_ref() | ||
.map(|(_, expr)| quote! { #expr }) | ||
.unwrap_or_else(|| quote! { #i }) | ||
}) | ||
// if no attribute, we fall back to an explicit discriminant (ie 'enum A { Foo = 1u32 }'). | ||
if index.is_none() { | ||
if let Some((_, Expr::Lit(ExprLit { lit: Lit::Int(disc_lit), .. }))) = &v.discriminant { | ||
index = disc_lit.base10_parse::<usize>().ok() | ||
} | ||
} | ||
|
||
// fall back to the variant index if no attribute or explicit discriminant is found. | ||
let index = index.unwrap_or(i); | ||
|
||
// output the index with no suffix (ie 1 rather than 1usize) so that these can be quoted into | ||
// an array of usizes and will work regardless of what type the discriminant values actually | ||
// are. | ||
let s = proc_macro2::Literal::usize_unsuffixed(index); | ||
pkhry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
quote! { #s } | ||
} | ||
|
||
/// Look for a `#[codec(encoded_as = "SomeType")]` outer attribute on the given | ||
|
@@ -386,6 +394,12 @@ pub fn check_attributes(input: &DeriveInput) -> syn::Result<()> { | |
check_field_attribute(attr)?; | ||
} | ||
} | ||
// While we're checking things, also ensure that | ||
// any explicit discriminants are within 0..=255 | ||
let discriminant = variant.discriminant.as_ref().map(|(_, d)| d); | ||
if let Some(expr) = discriminant { | ||
check_variant_discriminant(expr)?; | ||
} | ||
}, | ||
Data::Union(_) => (), | ||
} | ||
|
@@ -465,6 +479,21 @@ fn check_variant_attribute(attr: &Attribute) -> syn::Result<()> { | |
} | ||
} | ||
|
||
// Ensure a variant discriminant, if provided, can be parsed into | ||
// something in the range 0..255. | ||
fn check_variant_discriminant(discriminant: &Expr) -> syn::Result<()> { | ||
if let Expr::Lit(ExprLit { lit: Lit::Int(lit_int), .. }) = discriminant { | ||
lit_int.base10_parse::<u8>().map(|_| ()).map_err(|_| { | ||
syn::Error::new(lit_int.span(), "Discriminant index must be in the range 0..255") | ||
}) | ||
} else { | ||
Err(syn::Error::new( | ||
discriminant.span(), | ||
"Discriminant must be an integer literal in the range 0..255", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forcing this to be an integer is a breaking change. Potential fix: #695 |
||
)) | ||
} | ||
} | ||
|
||
// Only `#[codec(dumb_trait_bound)]` is accepted as top attribute | ||
fn check_top_attribute(attr: &Attribute) -> syn::Result<()> { | ||
let top_error = "Invalid attribute: only `#[codec(dumb_trait_bound)]`, \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ignoring the discriminant when defined with an expression.