diff --git a/bitbybit/src/bitfield/codegen.rs b/bitbybit/src/bitfield/codegen.rs index 570d3fc..8a01b74 100644 --- a/bitbybit/src/bitfield/codegen.rs +++ b/bitbybit/src/bitfield/codegen.rs @@ -459,7 +459,14 @@ pub fn make_builder( .filter(|def| def.setter_type.is_some()); let params = definitions .clone() - .map(|def| syn::parse_str::(format!("{}", def.field_name).as_str()).unwrap()) + .map(|def| { + // Special handling for reserved identifiers prefix. + let mut field_name = def.field_name.to_string(); + if field_name.starts_with("r#") { + field_name = field_name.strip_prefix("r#").unwrap().to_string(); + } + syn::parse_str::(&format!("__{}", field_name.to_uppercase())).unwrap() + }) .map(|name| quote! { const #name: bool }) .collect::>(); @@ -545,11 +552,17 @@ pub fn make_builder( result.push(quote!(false)); any_overlaps = true; } else { - let name = syn::parse_str::(format!("{}", def.field_name).as_str()) - .unwrap(); - params.push(quote! { const #name: bool }); - names.push(quote!({ #name })); - result.push(quote!({ #name })); + // Special handling for reserved identifiers prefix. + let mut field_name = def.field_name.to_string(); + if field_name.starts_with("r#") { + field_name = field_name.strip_prefix("r#").unwrap().to_string(); + } + let const_generics = + syn::parse_str::(&format!("__{}", field_name.to_uppercase())) + .unwrap(); + params.push(quote! { const #const_generics: bool }); + names.push(quote!({ #const_generics })); + result.push(quote!({ #const_generics })); } } } @@ -557,7 +570,6 @@ pub fn make_builder( let doc_comment = &field_definition.doc_comment; new_with_builder_chain.push(quote! { - #[allow(non_camel_case_types)] impl<#( #params, )*> #builder_struct_name<#( #names, )*> { #(#doc_comment)* pub const fn #with_name(&self, value: #argument_type) -> #builder_struct_name<#( #result, )*> { diff --git a/bitbybit/tests/builder_value_field.rs b/bitbybit/tests/builder_value_field.rs new file mode 100644 index 0000000..3028000 --- /dev/null +++ b/bitbybit/tests/builder_value_field.rs @@ -0,0 +1,10 @@ +// No issues with the private internal field value when a field is named value. +#[bitbybit::bitfield(u32, default = 0x0, debug)] +pub struct Data { + #[bit(15, rw)] + dparity: bool, + #[bits(0..=7, rw)] + value: u8, +} + +pub fn main() {} diff --git a/bitbybit/tests/no_compile/invalid_field_range.stderr b/bitbybit/tests/no_compile/invalid_field_range.stderr index a623808..4d5a746 100644 --- a/bitbybit/tests/no_compile/invalid_field_range.stderr +++ b/bitbybit/tests/no_compile/invalid_field_range.stderr @@ -14,7 +14,7 @@ error[E0599]: no method named `with_c` found for struct `PartialTest` | = note: the method was found for - - `PartialTest` + - `PartialTest` help: one of the expressions' fields has a method of the same name | 30 | Test::builder().with_a(u4::new(1)).with_b(u4::new(1)).value.with_c(true).build(); diff --git a/bitbybit/tests/no_compile/missing_fields_in_builder.stderr b/bitbybit/tests/no_compile/missing_fields_in_builder.stderr index 46fc7e2..15228a5 100644 --- a/bitbybit/tests/no_compile/missing_fields_in_builder.stderr +++ b/bitbybit/tests/no_compile/missing_fields_in_builder.stderr @@ -32,7 +32,7 @@ error[E0599]: no method named `with_bar` found for struct `PartialTest` | = note: the method was found for - - `PartialTest` + - `PartialTest<__FOO, false>` help: one of the expressions' fields has a method of the same name | 15 | Test::builder().with_bar(1).with_foo(1).value.with_bar(2).build(); diff --git a/bitbybit/tests/no_compile/overlapping_bitfield_u8_fields.stderr b/bitbybit/tests/no_compile/overlapping_bitfield_u8_fields.stderr index a4bb1e3..4ab2e89 100644 --- a/bitbybit/tests/no_compile/overlapping_bitfield_u8_fields.stderr +++ b/bitbybit/tests/no_compile/overlapping_bitfield_u8_fields.stderr @@ -34,7 +34,7 @@ error[E0599]: no method named `with_a` found for struct `PartialTestAllowed` | = note: the method was found for - - `PartialTestAllowed` + - `PartialTestAllowed<__UPPER, false, __C, __B, false>` help: one of the expressions' fields has a method of the same name | 48 | TestAllowed::builder().with_upper(u4::new(0)).with_lower(u12::new(0)).value.with_a(u2::new(0)).build(); diff --git a/bitbybit/tests/tests.rs b/bitbybit/tests/tests.rs index 8e61c69..1c413cc 100644 --- a/bitbybit/tests/tests.rs +++ b/bitbybit/tests/tests.rs @@ -6,6 +6,7 @@ fn all_tests() { t.pass("tests/basic.rs"); t.pass("tests/with_fields.rs"); t.pass("tests/correct.rs"); + t.pass("tests/builder_value_field.rs"); t.compile_fail("tests/no_compile/exhaustive_bitenum.rs"); t.compile_fail("tests/no_compile/non_exhaustive_bitenum.rs");