Repository: sharksforarms/deku
Pull request #150 (2020-12-14 18:29:29)

feat: add namespacing to internal variables
Fixes https://github.com/sharksforarms/deku/issues/111


Patch:
diff --git a/deku-derive/src/lib.rs b/deku-derive/src/lib.rs
index 878cada..51137b5 100644
--- a/deku-derive/src/lib.rs
+++ b/deku-derive/src/lib.rs
@@ -429,7 +429,7 @@ struct DekuReceiver {
     // TODO: The type of it should be
     //       `syn::punctuated::Punctuated<syn::FnArg, syn::token::Comma>`
     //       https://github.com/TedDriggs/darling/pull/98
-    #[darling(default)]
+    #[darling(default, map = "apply_replacements")]
     ctx: Option<syn::LitStr>,
 
     /// default context passed to the field
@@ -460,9 +460,32 @@ struct DekuReceiver {
     bytes: Option<usize>,
 }
 
+fn apply_replacements(input: Option<syn::LitStr>) -> Option<syn::LitStr> {
+    input.map(|v| {
+        if v.value().contains("__deku_") {
+            panic!(
+                "error: attribute cannot contain `__deku_` these are internal variables. Please use the `deku::` instead."
+            );
+        }
+
+        let v_str = v
+            .value()
+            .replace("deku::input", "__deku_input") // part of the public API `from_bytes`
+            .replace("deku::input_bits", "__deku_input_bits") // part of the public API `read`
+            .replace("deku::output", "__deku_output") // part of the public API `write`
+            .replace("deku::rest", "__deku_rest")
+            .replace("deku::bit_offset", "__deku_bit_offset")
+            .replace("deku::byte_offset", "__deku_byte_offset");
+
+        syn::LitStr::new(&v_str, v.span())
+    })
+}
+
 /// Parse a TokenStream from an Option<LitStr>
+/// Also replaces any namespaced variables to internal variables found in `input`
 fn option_as_tokenstream(input: Option<syn::LitStr>) -> Option<TokenStream> {
     input.map(|v| {
+        let v = apply_replacements(Some(v)).unwrap();
         v.parse::<TokenStream>()
             .expect("could not parse token stream")
     })
@@ -529,7 +552,7 @@ struct DekuFieldReceiver {
     /// A comma separated argument list.
     // TODO: The type of it should be `Punctuated<Expr, Comma>`
     //       https://github.com/TedDriggs/darling/pull/98
-    #[darling(default)]
+    #[darling(default, map = "apply_replacements")]
     ctx: Option<syn::LitStr>,
 
     /// map field when updating struct
diff --git a/deku-derive/src/macros/deku_read.rs b/deku-derive/src/macros/deku_read.rs
index d50266e..fef1827 100644
--- a/deku-derive/src/macros/deku_read.rs
+++ b/deku-derive/src/macros/deku_read.rs
@@ -52,20 +52,20 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
         let from_bytes_body = wrap_default_ctx(
             quote! {
                 use core::convert::TryFrom;
-                let input_bits = input.0.view_bits::<Msb0>();
+                let __deku_input_bits = __deku_input.0.view_bits::<Msb0>();
 
-                let mut rest = input_bits;
-                rest = &rest[input.1..];
+                let mut __deku_rest = __deku_input_bits;
+                __deku_rest = &__deku_rest[__deku_input.1..];
 
                 #magic_read
 
                 #(#field_reads)*
                 let value = #initialize_struct;
 
-                let pad = 8 * ((rest.len() + 7) / 8) - rest.len();
-                let read_idx = input_bits.len() - (rest.len() + pad);
+                let pad = 8 * ((__deku_rest.len() + 7) / 8) - __deku_rest.len();
+                let read_idx = __deku_input_bits.len() - (__deku_rest.len() + pad);
 
-                Ok(((input_bits[read_idx..].as_slice(), pad), value))
+                Ok(((__deku_input_bits[read_idx..].as_slice(), pad), value))
             },
             &input.ctx,
             &input.ctx_default,
@@ -80,19 +80,19 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
     let read_body = quote! {
         use core::convert::TryFrom;
-        let mut rest = input_bits;
+        let mut __deku_rest = __deku_input_bits;
 
         #magic_read
 
         #(#field_reads)*
         let value = #initialize_struct;
 
-        Ok((rest, value))
+        Ok((__deku_rest, value))
     };
 
     tokens.extend(quote! {
         impl #imp DekuRead<#ctx_types> for #ident #wher {
-            fn read<'a>(input_bits: &'a BitSlice<Msb0, u8>, #ctx_arg) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
+            fn read<'a>(__deku_input_bits: &'a BitSlice<Msb0, u8>, #ctx_arg) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
                 #read_body
             }
         }
@@ -103,7 +103,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
         tokens.extend(quote! {
             impl #imp DekuRead for #ident #wher {
-                fn read<'a>(input_bits: &'a BitSlice<Msb0, u8>, _: ()) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
+                fn read<'a>(__deku_input_bits: &'a BitSlice<Msb0, u8>, _: ()) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
                     #read_body
                 }
             }
@@ -190,7 +190,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
             // if we're consuming an id, set the rest to new_rest before reading the variant
             let new_rest = if consume_id {
                 quote! {
-                    rest = new_rest;
+                    __deku_rest = new_rest;
                 }
             } else {
                 quote! {}
@@ -229,11 +229,11 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
     let variant_id_read = if id.is_some() {
         quote! {
-            let (new_rest, variant_id) = (rest, #id);
+            let (new_rest, variant_id) = (__deku_rest, #id);
         }
     } else if id_type.is_some() {
         quote! {
-            let (new_rest, variant_id) = #id_type::read(rest, (#id_args))?;
+            let (new_rest, variant_id) = #id_type::read(__deku_rest, (#id_args))?;
         }
     } else {
         // either `id` or `type` needs to be specified
@@ -255,19 +255,19 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
         let from_bytes_body = wrap_default_ctx(
             quote! {
                 use core::convert::TryFrom;
-                let input_bits = input.0.view_bits::<Msb0>();
+                let __deku_input_bits = __deku_input.0.view_bits::<Msb0>();
 
-                let mut rest = input_bits;
-                rest = &rest[input.1..];
+                let mut __deku_rest = __deku_input_bits;
+                __deku_rest = &__deku_rest[__deku_input.1..];
 
                 #magic_read
 
                 #variant_read
 
-                let pad = 8 * ((rest.len() + 7) / 8) - rest.len();
-                let read_idx = input_bits.len() - (rest.len() + pad);
+                let pad = 8 * ((__deku_rest.len() + 7) / 8) - __deku_rest.len();
+                let read_idx = __deku_input_bits.len() - (__deku_rest.len() + pad);
 
-                Ok(((input_bits[read_idx..].as_slice(), pad), value))
+                Ok(((__deku_input_bits[read_idx..].as_slice(), pad), value))
             },
             &input.ctx,
             &input.ctx_default,
@@ -282,19 +282,19 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
     let read_body = quote! {
         use core::convert::TryFrom;
-        let mut rest = input_bits;
+        let mut __deku_rest = __deku_input_bits;
 
         #magic_read
 
         #variant_read
 
-        Ok((rest, value))
+        Ok((__deku_rest, value))
     };
 
     tokens.extend(quote! {
         #[allow(non_snake_case)]
         impl #imp DekuRead<#ctx_types> for #ident #wher {
-            fn read<'a>(input_bits: &'a BitSlice<Msb0, u8>, #ctx_arg) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
+            fn read<'a>(__deku_input_bits: &'a BitSlice<Msb0, u8>, #ctx_arg) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
                 #read_body
             }
         }
@@ -306,7 +306,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
         tokens.extend(quote! {
             #[allow(non_snake_case)]
             impl #imp DekuRead for #ident #wher {
-                fn read<'a>(input_bits: &'a BitSlice<Msb0, u8>, _: ()) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
+                fn read<'a>(__deku_input_bits: &'a BitSlice<Msb0, u8>, _: ()) -> Result<(&'a BitSlice<Msb0, u8>, Self), DekuError> {
                     #read_body
                 }
             }
@@ -323,12 +323,12 @@ fn emit_magic_read(input: &DekuData) -> Result<TokenStream, syn::Error> {
             let magic = #magic;
 
             for byte in magic {
-                let (new_rest, read_byte) = u8::read(rest, ())?;
+                let (new_rest, read_byte) = u8::read(__deku_rest, ())?;
                 if *byte != read_byte {
                     return Err(DekuError::Parse(format!("Missing magic value {:?}", #magic)));
                 }
 
-                rest = new_rest;
+                __deku_rest = new_rest;
             }
         }
     } else {
@@ -368,10 +368,10 @@ fn emit_bit_byte_offsets(
     // determine if we should include `bit_offset` and `byte_offset`
     let byte_offset = if fields
         .iter()
-        .any(|v| token_contains_string(v, "byte_offset"))
+        .any(|v| token_contains_string(v, "__deku_byte_offset"))
     {
         Some(quote! {
-            let byte_offset = bit_offset / 8;
+            let __deku_byte_offset = __deku_bit_offset / 8;
         })
     } else {
         None
@@ -379,11 +379,11 @@ fn emit_bit_byte_offsets(
 
     let bit_offset = if fields
         .iter()
-        .any(|v| token_contains_string(v, "bit_offset"))
+        .any(|v| token_contains_string(v, "__deku_bit_offset"))
         || byte_offset.is_some()
     {
         Some(quote! {
-            let bit_offset = usize::try_from(input_bits.offset_from(rest))?;
+            let __deku_bit_offset = usize::try_from(__deku_input_bits.offset_from(__deku_rest))?;
         })
     } else {
         None
@@ -445,29 +445,29 @@ fn emit_field_read(
             quote! {
                 {
                     use core::borrow::Borrow;
-                    DekuRead::read(rest, (deku::ctx::Limit::new_count(usize::try_from(*((#field_count).borrow()))?), (#read_args)))
+                    DekuRead::read(__deku_rest, (deku::ctx::Limit::new_count(usize::try_from(*((#field_count).borrow()))?), (#read_args)))
                 }
             }
         } else if let Some(field_bits) = &f.bits_read {
             quote! {
                 {
                     use core::borrow::Borrow;
-                    DekuRead::read(rest, (deku::ctx::Limit::new_size(deku::ctx::Size::Bits(usize::try_from(*((#field_bits).borrow()))?)), (#read_args)))
+                    DekuRead::read(__deku_rest, (deku::ctx::Limit::new_size(deku::ctx::Size::Bits(usize::try_from(*((#field_bits).borrow()))?)), (#read_args)))
                 }
             }
         } else if let Some(field_bytes) = &f.bytes_read {
             quote! {
                 {
                     use core::borrow::Borrow;
-                    DekuRead::read(rest, (deku::ctx::Limit::new_size(deku::ctx::Size::Bytes(usize::try_from(*((#field_bytes).borrow()))?)), (#read_args)))
+                    DekuRead::read(__deku_rest, (deku::ctx::Limit::new_size(deku::ctx::Size::Bytes(usize::try_from(*((#field_bytes).borrow()))?)), (#read_args)))
                 }
             }
         } else if let Some(field_until) = &f.until {
             // We wrap the input into another closure here to enforce that it is actually a callable
             // Otherwise, an incorrectly passed-in integer could unexpectedly convert into a `Count` limit
-            quote! {DekuRead::read(rest, (deku::ctx::Limit::new_until(#field_until), (#read_args)))}
+            quote! {DekuRead::read(__deku_rest, (deku::ctx::Limit::new_until(#field_until), (#read_args)))}
         } else {
-            quote! {DekuRead::read(rest, (#read_args))}
+            quote! {DekuRead::read(__deku_rest, (#read_args))}
         }
     };
 
@@ -475,7 +475,7 @@ fn emit_field_read(
         let (new_rest, value) = #field_read_func?;
         let value: #field_type = #field_map(value)?;
 
-        rest = new_rest;
+        __deku_rest = new_rest;
 
         value
     };
@@ -538,7 +538,7 @@ pub fn emit_from_bytes(
     quote! {
         impl #imp DekuContainerRead for #ident #wher {
             #[allow(non_snake_case)]
-            fn from_bytes(input: (&[u8], usize)) -> Result<((&[u8], usize), Self), DekuError> {
+            fn from_bytes(__deku_input: (&[u8], usize)) -> Result<((&[u8], usize), Self), DekuError> {
                 #body
             }
         }
diff --git a/deku-derive/src/macros/deku_write.rs b/deku-derive/src/macros/deku_write.rs
index ac11902..c01ab19 100644
--- a/deku-derive/src/macros/deku_write.rs
+++ b/deku-derive/src/macros/deku_write.rs
@@ -46,12 +46,13 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
             quote! {
                 match *self {
                     #destructured => {
-                        let mut acc: BitVec<Msb0, u8> = BitVec::new();
-                        let output = &mut acc;
+                        let mut __deku_acc: BitVec<Msb0, u8> = BitVec::new();
+                        let __deku_output = &mut __deku_acc;
+
                         #magic_write
                         #(#field_writes)*
 
-                        Ok(acc)
+                    Ok(__deku_acc)
                     }
                 }
             },
@@ -118,7 +119,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
         impl #imp DekuWrite<#ctx_types> for #ident #wher {
             #[allow(unused_variables)]
-            fn write(&self, output: &mut BitVec<Msb0, u8>, #ctx_arg) -> Result<(), DekuError> {
+            fn write(&self, __deku_output: &mut BitVec<Msb0, u8>, #ctx_arg) -> Result<(), DekuError> {
                 #write_body
             }
         }
@@ -130,7 +131,7 @@ fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> {
         tokens.extend(quote! {
             impl #imp DekuWrite for #ident #wher {
                 #[allow(unused_variables)]
-                fn write(&self, output: &mut BitVec<Msb0, u8>, _: ()) -> Result<(), DekuError> {
+                fn write(&self, __deku_output: &mut BitVec<Msb0, u8>, _: ()) -> Result<(), DekuError> {
                     #write_body
                 }
             }
@@ -192,14 +193,14 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
             if let Some(variant_id) = &variant.id {
                 quote! {
                     let mut variant_id: #id_type = #variant_id;
-                    variant_id.write(output, (#id_args))?;
+                    variant_id.write(__deku_output, (#id_args))?;
                 }
             } else if variant.id_pat.is_some() {
                 quote! {}
             } else if variant.fields.style.is_unit() {
                 quote! {
                     let mut variant_id: #id_type = Self::#variant_ident as #id_type;
-                    variant_id.write(output, (#id_args))?;
+                    variant_id.write(__deku_output, (#id_args))?;
                 }
             } else {
                 return Err(syn::Error::new(
@@ -246,8 +247,8 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
     if input.ctx.is_none() || (input.ctx.is_some() && input.ctx_default.is_some()) {
         let to_bits_body = wrap_default_ctx(
             quote! {
-                let mut acc: BitVec<Msb0, u8> = BitVec::new();
-                let output = &mut acc;
+                let mut __deku_acc: BitVec<Msb0, u8> = BitVec::new();
+                let __deku_output = &mut __deku_acc;
 
                 #magic_write
 
@@ -255,7 +256,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
                     #(#variant_writes),*
                 }
 
-                Ok(acc)
+                Ok(__deku_acc)
             },
             &input.ctx,
             &input.ctx_default,
@@ -322,7 +323,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
 
         impl #imp DekuWrite<#ctx_types> for #ident #wher {
             #[allow(unused_variables)]
-            fn write(&self, output: &mut BitVec<Msb0, u8>, #ctx_arg) -> Result<(), DekuError> {
+            fn write(&self, __deku_output: &mut BitVec<Msb0, u8>, #ctx_arg) -> Result<(), DekuError> {
                 #write_body
             }
         }
@@ -334,7 +335,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
         tokens.extend(quote! {
             impl #imp DekuWrite for #ident #wher {
                 #[allow(unused_variables)]
-                fn write(&self, output: &mut BitVec<Msb0, u8>, _: ()) -> Result<(), DekuError> {
+                fn write(&self, __deku_output: &mut BitVec<Msb0, u8>, _: ()) -> Result<(), DekuError> {
                     #write_body
                 }
             }
@@ -348,7 +349,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
 fn emit_magic_write(input: &DekuData) -> Result<TokenStream, syn::Error> {
     let tokens = if let Some(magic) = &input.magic {
         quote! {
-            #magic.write(output, ())?;
+            #magic.write(__deku_output, ())?;
         }
     } else {
         quote! {}
@@ -415,10 +416,10 @@ fn emit_bit_byte_offsets(
     // determine if we should include `bit_offset` and `byte_offset`
     let byte_offset = if fields
         .iter()
-        .any(|v| token_contains_string(v, "byte_offset"))
+        .any(|v| token_contains_string(v, "__deku_byte_offset"))
     {
         Some(quote! {
-            let byte_offset = bit_offset / 8;
+            let __deku_byte_offset = __deku_bit_offset / 8;
         })
     } else {
         None
@@ -426,11 +427,11 @@ fn emit_bit_byte_offsets(
 
     let bit_offset = if fields
         .iter()
-        .any(|v| token_contains_string(v, "bit_offset"))
+        .any(|v| token_contains_string(v, "__deku_bit_offset"))
         || byte_offset.is_some()
     {
         Some(quote! {
-            let bit_offset = output.len();
+            let __deku_bit_offset = __deku_output.len();
         })
     } else {
         None
@@ -459,7 +460,7 @@ fn emit_field_write(
     } else {
         let write_args = gen_field_args(field_endian, f.bits, f.bytes, f.ctx.as_ref())?;
 
-        quote! { #object_prefix #field_ident.write(output, (#write_args)) }
+        quote! { #object_prefix #field_ident.write(__deku_output, (#write_args)) }
     };
 
     let field_write_normal = quote! {
diff --git a/examples/custom_reader_and_writer.rs b/examples/custom_reader_and_writer.rs
index 0046db8..28aacc7 100644
--- a/examples/custom_reader_and_writer.rs
+++ b/examples/custom_reader_and_writer.rs
@@ -51,8 +51,8 @@ struct DekuTest {
     field_a: u8,
 
     #[deku(
-        reader = "bit_flipper_read(*field_a, rest, Size::Bits(8))",
-        writer = "bit_flipper_write(*field_a, *field_b, output, Size::Bits(8))"
+        reader = "bit_flipper_read(*field_a, deku::rest, Size::Bits(8))",
+        writer = "bit_flipper_write(*field_a, *field_b, deku::output, Size::Bits(8))"
     )]
     field_b: u8,
 }
diff --git a/src/attributes.rs b/src/attributes.rs
index 6f78fa4..48fda55 100644
--- a/src/attributes.rs
+++ b/src/attributes.rs
@@ -545,8 +545,8 @@ Example:
 # #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
 struct DekuTest {
     #[deku(
-        reader = "DekuTest::read(rest)",
-        writer = "DekuTest::write(output, &self.field_a)"
+        reader = "DekuTest::read(deku::rest)",
+        writer = "DekuTest::write(deku::output, &self.field_a)"
     )]
     field_a: String,
 }
diff --git a/src/lib.rs b/src/lib.rs
index 4a9c029..3a2745a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -212,9 +212,9 @@ assert_eq!(value.a, 0x01);
 assert_eq!(value.sub.b, 0x01 + 0x02)
 ```
 
-# Internal and previously read fields
+# Internal variables and previously read fields
 
-Along similar lines to Context variables, previously read variables
+Along similar lines to [Context](#Context) variables, previously read variables
 are exposed and can be referenced:
 
 Example:
@@ -229,21 +229,21 @@ struct DekuTest {
 }
 ```
 
-The following variables are reserved keywords which can be used in attributes accepting
+The following variables are internals which can be used in attributes accepting
 tokens such as `reader`, `writer`, `map`, `count`, etc.
 
 These are provided as a convenience to the user.
 
 Always included:
-- `input: (&[u8], usize)` - The initial input byte slice and bit offset
+- `deku::input: (&[u8], usize)` - The initial input byte slice and bit offset
 (available when using [from_bytes](crate::DekuContainerRead::from_bytes))
-- `input_bits: &BitSlice<Msb0, u8>` - The initial input in bits
-- `rest: &BitSlice<Msb0, u8>` - Remaining bits to read
-- `output: &mut BitSlice<Msb0, u8>` - The output bit stream
+- `deku::input_bits: &BitSlice<Msb0, u8>` - The initial input in bits
+- `deku::rest: &BitSlice<Msb0, u8>` - Remaining bits to read
+- `deku::output: &mut BitSlice<Msb0, u8>` - The output bit stream
 
 Conditionally included if referenced:
-- `bit_offset: usize` - Current bit offset from the input
-- `byte_offset: usize` - Current byte offset from the input
+- `deku::bit_offset: usize` - Current bit offset from the input
+- `deku::byte_offset: usize` - Current byte offset from the input
 
 Example:
 ```rust
@@ -253,7 +253,7 @@ Example:
 pub struct EncodedString {
     encoding: u8,
 
-    #[deku(count = "size as usize - byte_offset")]
+    #[deku(count = "size as usize - deku::byte_offset")]
     data: Vec<u8>
 }
 ```


Test patch:
diff --git a/tests/test_attributes/test_ctx.rs b/tests/test_attributes/test_ctx.rs
index 50933ce..88d6c46 100644
--- a/tests/test_attributes/test_ctx.rs
+++ b/tests/test_attributes/test_ctx.rs
@@ -11,8 +11,8 @@ fn test_ctx_struct() {
     #[deku(ctx = "a: u8, b: u8")]
     struct SubTypeNeedCtx {
         #[deku(
-            reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c) as usize))})(rest)",
-            writer = "(|c|{u8::write(&(c-a-b), output, ())})(self.i as u8)"
+            reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c) as usize))})(deku::rest)",
+            writer = "(|c|{u8::write(&(c-a-b), deku::output, ())})(self.i as u8)"
         )]
         i: usize,
     }
@@ -51,8 +51,8 @@ fn test_top_level_ctx_enum() {
         #[deku(id = "1")]
         VariantA(
             #[deku(
-                reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c)))})(rest)",
-                writer = "(|c|{u8::write(&(c-a-b), output, ())})(field_0)"
+                reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c)))})(deku::rest)",
+                writer = "(|c|{u8::write(&(c-a-b), deku::output, ())})(field_0)"
             )]
             u8,
         ),
@@ -76,8 +76,8 @@ fn test_top_level_ctx_enum_default() {
         #[deku(id = "1")]
         VariantA(
             #[deku(
-                reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c)))})(rest)",
-                writer = "(|c|{u8::write(&(c-a-b), output, ())})(field_0)"
+                reader = "(|rest|{u8::read(rest,()).map(|(slice,c)|(slice,(a+b+c)))})(deku::rest)",
+                writer = "(|c|{u8::write(&(c-a-b), deku::output, ())})(field_0)"
             )]
             u8,
         ),
diff --git a/tests/test_compile/cases/internal_variables.rs b/tests/test_compile/cases/internal_variables.rs
index fd3f9d8..a39ddbf 100644
--- a/tests/test_compile/cases/internal_variables.rs
+++ b/tests/test_compile/cases/internal_variables.rs
@@ -3,49 +3,49 @@ use deku::prelude::*;
 #[derive(DekuRead, DekuWrite)]
 struct TestCount {
     field_a: u8,
-    #[deku(count = "byte_offset")]
+    #[deku(count = "deku::byte_offset")]
     field_b: Vec<u8>
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestBitRead {
     field_a: u8,
-    #[deku(bits_read = "bit_offset")]
+    #[deku(bits_read = "deku::bit_offset")]
     field_b: Vec<u8>
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestBytesRead {
     field_a: u8,
-    #[deku(bytes_read = "bit_offset")]
+    #[deku(bytes_read = "deku::bit_offset")]
     field_b: Vec<u8>
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestUntil {
     field_a: u8,
-    #[deku(until = "|v| *v as usize == bit_offset")]
+    #[deku(until = "|v| *v as usize == deku::bit_offset")]
     field_b: Vec<u8>
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestCond {
     field_a: u8,
-    #[deku(cond = "bit_offset == *field_a as usize")]
+    #[deku(cond = "deku::bit_offset == *field_a as usize")]
     field_b: u8
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestDefault {
     field_a: u8,
-    #[deku(skip, default = "byte_offset")]
+    #[deku(skip, default = "deku::byte_offset")]
     field_b: usize
 }
 
 #[derive(DekuRead, DekuWrite)]
 struct TestMap {
     field_a: u8,
-    #[deku(map = "|v: u8| -> Result<_, DekuError> { Ok(v as usize + byte_offset) }")]
+    #[deku(map = "|v: u8| -> Result<_, DekuError> { Ok(v as usize + deku::byte_offset) }")]
     field_b: usize
 }
 
@@ -58,7 +58,7 @@ fn dummy_reader(
 #[derive(DekuRead, DekuWrite)]
 struct TestReader {
     field_a: u8,
-    #[deku(reader = "dummy_reader(byte_offset, rest)")]
+    #[deku(reader = "dummy_reader(deku::byte_offset, deku::rest)")]
     field_b: usize
 }
 
@@ -69,7 +69,7 @@ struct ChildCtx {
 #[derive(DekuRead, DekuWrite)]
 struct TestCtx {
     field_a: u8,
-    #[deku(ctx = "byte_offset, bit_offset")]
+    #[deku(ctx = "deku::byte_offset, deku::bit_offset")]
     field_b: ChildCtx
 }
 
@@ -82,10 +82,15 @@ fn dummy_writer(
 #[derive(DekuRead, DekuWrite)]
 struct TestWriter {
     field_a: u8,
-    #[deku(writer = "dummy_writer(byte_offset, output)")]
+    #[deku(writer = "dummy_writer(deku::byte_offset, deku::output)")]
     field_b: usize
 }
 
-fn main() {
-    fail
+#[derive(DekuRead, DekuWrite)]
+struct FailInternal {
+    field_a: u8,
+    #[deku(cond = "__deku_bit_offset == *field_a as usize")]
+    field_b: u8
 }
+
+fn main() {}
diff --git a/tests/test_compile/cases/internal_variables.stderr b/tests/test_compile/cases/internal_variables.stderr
index c9a887a..44a6360 100644
--- a/tests/test_compile/cases/internal_variables.stderr
+++ b/tests/test_compile/cases/internal_variables.stderr
@@ -1,5 +1,15 @@
-error[E0425]: cannot find value `fail` in this scope
-  --> $DIR/internal_variables.rs:90:5
+error: proc-macro derive panicked
+  --> $DIR/internal_variables.rs:89:20
    |
-90 |     fail
-   |     ^^^^ not found in this scope
+89 | #[derive(DekuRead, DekuWrite)]
+   |                    ^^^^^^^^^
+   |
+   = help: message: error: attribute cannot contain `__deku_` these are internal variables. Please use the `deku::` instead.
+
+error: proc-macro derive panicked
+  --> $DIR/internal_variables.rs:89:10
+   |
+89 | #[derive(DekuRead, DekuWrite)]
+   |          ^^^^^^^^
+   |
+   = help: message: error: attribute cannot contain `__deku_` these are internal variables. Please use the `deku::` instead.
diff --git a/tests/test_compile/cases/temp_field.rs b/tests/test_compile/cases/temp_field.rs
index d328839..8578df9 100644
--- a/tests/test_compile/cases/temp_field.rs
+++ b/tests/test_compile/cases/temp_field.rs
@@ -6,3 +6,5 @@ struct Test1 {
     #[deku(temp)]
     field_a: u8,
 }
+
+fn main() {}
diff --git a/tests/test_compile/cases/temp_field.stderr b/tests/test_compile/cases/temp_field.stderr
index 2aabbdc..bb276ac 100644
--- a/tests/test_compile/cases/temp_field.stderr
+++ b/tests/test_compile/cases/temp_field.stderr
@@ -1,15 +1,3 @@
-error[E0601]: `main` function not found in crate `$CRATE`
- --> $DIR/temp_field.rs:1:1
-  |
-1 | / use deku::prelude::*;
-2 | |
-3 | | // wrong way to use temp
-4 | | #[derive(DekuRead, DekuWrite)]
-... |
-7 | |     field_a: u8,
-8 | | }
-  | |_^ consider adding a `main` function to `$DIR/tests/test_compile/cases/temp_field.rs`
-
 error[E0063]: missing field `field_a` in initializer of `Test1`
  --> $DIR/temp_field.rs:4:10
   |