Skip to content

Commit

Permalink
fix: enum discriminant overflow (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salka1988 authored Nov 6, 2023
1 parent 3579738 commit 8062ff6
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/fuels-macros/src/derive/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ pub(crate) fn extract_variants(
contents: impl IntoIterator<Item = Variant>,
fuels_core_path: TokenStream,
) -> Result<ExtractedVariants> {
let mut discriminant = 0;
let variants = contents
.into_iter()
.map(|variant| -> Result<_> {
.enumerate()
.map(|(discriminant, variant)| -> Result<_> {
let is_unit = matches!(variant.fields, Fields::Unit);
if has_ignore_attr(&variant.attrs) {
Ok(ExtractedVariant::Ignored {
Expand All @@ -75,15 +75,16 @@ pub(crate) fn extract_variants(
} else {
validate_variant_type(&variant)?;

let current_discriminant = discriminant;
discriminant += 1;
let discriminant = discriminant.try_into().map_err(|_| {
Error::new_spanned(&variant.ident, "Enums can not have more than 256 variants")
})?;

Ok(ExtractedVariant::Normal {
info: VariantInfo {
name: variant.ident,
is_unit,
},
discriminant: current_discriminant,
discriminant,
})
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
use fuels_macros::Tokenizable;

#[derive(Tokenizable)]
enum MaxedOutVariantsEnum {
Variant0(u8),
Variant1(u8),
Variant2(u8),
Variant3(u8),
Variant4(u8),
Variant5(u8),
Variant6(u8),
Variant7(u8),
Variant8(u8),
Variant9(u8),
Variant10(u8),
Variant11(u8),
Variant12(u8),
Variant13(u8),
Variant14(u8),
Variant15(u8),
Variant16(u8),
Variant17(u8),
Variant18(u8),
Variant19(u8),
Variant20(u8),
Variant21(u8),
Variant22(u8),
Variant23(u8),
Variant24(u8),
Variant25(u8),
Variant26(u8),
Variant27(u8),
Variant28(u8),
Variant29(u8),
Variant30(u8),
Variant31(u8),
Variant32(u8),
Variant33(u8),
Variant34(u8),
Variant35(u8),
Variant36(u8),
Variant37(u8),
Variant38(u8),
Variant39(u8),
Variant40(u8),
Variant41(u8),
Variant42(u8),
Variant43(u8),
Variant44(u8),
Variant45(u8),
Variant46(u8),
Variant47(u8),
Variant48(u8),
Variant49(u8),
Variant50(u8),
Variant51(u8),
Variant52(u8),
Variant53(u8),
Variant54(u8),
Variant55(u8),
Variant56(u8),
Variant57(u8),
Variant58(u8),
Variant59(u8),
Variant60(u8),
Variant61(u8),
Variant62(u8),
Variant63(u8),
Variant64(u8),
Variant65(u8),
Variant66(u8),
Variant67(u8),
Variant68(u8),
Variant69(u8),
Variant70(u8),
Variant71(u8),
Variant72(u8),
Variant73(u8),
Variant74(u8),
Variant75(u8),
Variant76(u8),
Variant77(u8),
Variant78(u8),
Variant79(u8),
Variant80(u8),
Variant81(u8),
Variant82(u8),
Variant83(u8),
Variant84(u8),
Variant85(u8),
Variant86(u8),
Variant87(u8),
Variant88(u8),
Variant89(u8),
Variant90(u8),
Variant91(u8),
Variant92(u8),
Variant93(u8),
Variant94(u8),
Variant95(u8),
Variant96(u8),
Variant97(u8),
Variant98(u8),
Variant99(u8),
Variant100(u8),
Variant101(u8),
Variant102(u8),
Variant103(u8),
Variant104(u8),
Variant105(u8),
Variant106(u8),
Variant107(u8),
Variant108(u8),
Variant109(u8),
Variant110(u8),
Variant111(u8),
Variant112(u8),
Variant113(u8),
Variant114(u8),
Variant115(u8),
Variant116(u8),
Variant117(u8),
Variant118(u8),
Variant119(u8),
Variant120(u8),
Variant121(u8),
Variant122(u8),
Variant123(u8),
Variant124(u8),
Variant125(u8),
Variant126(u8),
Variant127(u8),
Variant128(u8),
Variant129(u8),
Variant130(u8),
Variant131(u8),
Variant132(u8),
Variant133(u8),
Variant134(u8),
Variant135(u8),
Variant136(u8),
Variant137(u8),
Variant138(u8),
Variant139(u8),
Variant140(u8),
Variant141(u8),
Variant142(u8),
Variant143(u8),
Variant144(u8),
Variant145(u8),
Variant146(u8),
Variant147(u8),
Variant148(u8),
Variant149(u8),
Variant150(u8),
Variant151(u8),
Variant152(u8),
Variant153(u8),
Variant154(u8),
Variant155(u8),
Variant156(u8),
Variant157(u8),
Variant158(u8),
Variant159(u8),
Variant160(u8),
Variant161(u8),
Variant162(u8),
Variant163(u8),
Variant164(u8),
Variant165(u8),
Variant166(u8),
Variant167(u8),
Variant168(u8),
Variant169(u8),
Variant170(u8),
Variant171(u8),
Variant172(u8),
Variant173(u8),
Variant174(u8),
Variant175(u8),
Variant176(u8),
Variant177(u8),
Variant178(u8),
Variant179(u8),
Variant180(u8),
Variant181(u8),
Variant182(u8),
Variant183(u8),
Variant184(u8),
Variant185(u8),
Variant186(u8),
Variant187(u8),
Variant188(u8),
Variant189(u8),
Variant190(u8),
Variant191(u8),
Variant192(u8),
Variant193(u8),
Variant194(u8),
Variant195(u8),
Variant196(u8),
Variant197(u8),
Variant198(u8),
Variant199(u8),
Variant200(u8),
Variant201(u8),
Variant202(u8),
Variant203(u8),
Variant204(u8),
Variant205(u8),
Variant206(u8),
Variant207(u8),
Variant208(u8),
Variant209(u8),
Variant210(u8),
Variant211(u8),
Variant212(u8),
Variant213(u8),
Variant214(u8),
Variant215(u8),
Variant216(u8),
Variant217(u8),
Variant218(u8),
Variant219(u8),
Variant220(u8),
Variant221(u8),
Variant222(u8),
Variant223(u8),
Variant224(u8),
Variant225(u8),
Variant226(u8),
Variant227(u8),
Variant228(u8),
Variant229(u8),
Variant230(u8),
Variant231(u8),
Variant232(u8),
Variant233(u8),
Variant234(u8),
Variant235(u8),
Variant236(u8),
Variant237(u8),
Variant238(u8),
Variant239(u8),
Variant240(u8),
Variant241(u8),
Variant242(u8),
Variant243(u8),
Variant244(u8),
Variant245(u8),
Variant246(u8),
Variant247(u8),
Variant248(u8),
Variant249(u8),
Variant250(u8),
Variant251(u8),
Variant252(u8),
Variant253(u8),
Variant254(u8),
Variant255(u8),
Variant256(u8),
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Enums can not have more than 256 variants
--> tests/ui/derive/tokenizable/enum_size_not_supported.rs:261:5
|
261 | Variant256(u8),
| ^^^^^^^^^^
Loading

0 comments on commit 8062ff6

Please sign in to comment.