@@ -378,11 +378,11 @@ fn parse_field(base_data_size: usize, field: &Field) -> Result<FieldDefinition>
378378 ( indexed_count - 1 ) * indexed_stride. unwrap ( ) + highest_bit_index_in_ranges;
379379 if number_of_bits_indexed > base_data_size {
380380 return Err ( Error :: new_spanned (
381- field. attrs . first ( ) ,
382- format ! (
383- "bitfield!: Array-field {} requires {number_of_bits_indexed} bits for the array, but only has ({})" , field_name, base_data_size
384- )
385- ) ) ;
381+ field. attrs . first ( ) ,
382+ format ! (
383+ "bitfield!: Array-field {} requires {number_of_bits_indexed} bits for the array, but only has ({})" , field_name, base_data_size
384+ )
385+ ) ) ;
386386 }
387387
388388 if indexed_count < 2 {
@@ -396,6 +396,8 @@ fn parse_field(base_data_size: usize, field: &Field) -> Result<FieldDefinition>
396396 }
397397 }
398398
399+ verify_bits_in_range ( field, field_name, & ranges, base_data_size) ?;
400+
399401 let ( custom_type, getter_type, setter_type) = if field_type_size_from_data_type. is_none ( ) {
400402 parse_enumeration ( ty, & primitive_type) ?
401403 } else {
@@ -430,11 +432,30 @@ fn parse_field(base_data_size: usize, field: &Field) -> Result<FieldDefinition>
430432 doc_comment,
431433 array : indexed_count. map ( |count| ( count, indexed_stride. unwrap ( ) ) ) ,
432434 field_type_size_from_data_type : field_type_size_from_data_type. map ( |v| v. 0 ) ,
433- is_signed : field_type_size_from_data_type. map_or ( false , |v| v. 1 ) ,
435+ is_signed : field_type_size_from_data_type. is_some_and ( |v| v. 1 ) ,
434436 unsigned_field_type,
435437 } )
436438}
437439
440+ fn verify_bits_in_range (
441+ field : & Field ,
442+ field_name : & Ident ,
443+ ranges : & [ Range < usize > ] ,
444+ base_data_size : usize ,
445+ ) -> syn:: Result < ( ) > {
446+ let highest_bit_index_in_ranges = ranges. iter ( ) . map ( |range| range. end ) . max ( ) . unwrap_or ( 0 ) ;
447+ if highest_bit_index_in_ranges > base_data_size {
448+ return Err ( Error :: new_spanned (
449+ field. attrs . first ( ) ,
450+ format ! (
451+ "bitfield!: Field {} requires {} bits, but only has ({})" ,
452+ field_name, highest_bit_index_in_ranges, base_data_size
453+ ) ,
454+ ) ) ;
455+ }
456+ Ok ( ( ) )
457+ }
458+
438459/// Parses the arguments of a field. At the beginning and after each comma, Reset is used. After
439460/// that, the various take_xxx functions are used to switch states.
440461#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
0 commit comments