From 8ec595ace38570ef33e8229f932028786f04ca2f Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Wed, 14 Sep 2022 01:08:32 -0500 Subject: [PATCH] Fix error reporting of bad count/offset/offset_after --- binrw/tests/ui/args_vec_mistakes.rs | 11 ++-- binrw/tests/ui/args_vec_mistakes.stderr | 51 +++++++++++++------ binrw/tests/ui/invalid_offset_type.stderr | 16 +++--- binrw_derive/src/binrw/codegen/mod.rs | 4 +- .../src/binrw/codegen/read_options/struct.rs | 6 +-- 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/binrw/tests/ui/args_vec_mistakes.rs b/binrw/tests/ui/args_vec_mistakes.rs index 4e03caac..2cc32e95 100644 --- a/binrw/tests/ui/args_vec_mistakes.rs +++ b/binrw/tests/ui/args_vec_mistakes.rs @@ -4,13 +4,13 @@ use binrw::BinRead; struct NoDefault; #[derive(BinRead)] -struct Foo(Vec); +struct MissingArgs(Vec); #[derive(BinRead)] -struct Bar(#[br(args((),))] Vec); +struct WrongType(#[br(args((),))] Vec); #[derive(BinRead)] -struct Baz(#[br(args { inner: () })] Vec); +struct MissingCount(#[br(args { inner: () })] Vec); #[derive(BinRead)] #[br(import(_a: NoDefault))] @@ -19,7 +19,10 @@ struct Inner { } #[derive(BinRead)] -struct Qux(#[br(count = 1)] Vec); +struct WrongCountType(#[br(count = Some(1))] Vec); + +#[derive(BinRead)] +struct MissingInnerArgs(#[br(count = 1)] Vec); fn main() { Vec::::read(&mut binrw::io::Cursor::new(b"")).unwrap(); diff --git a/binrw/tests/ui/args_vec_mistakes.stderr b/binrw/tests/ui/args_vec_mistakes.stderr index fac1f637..df05d066 100644 --- a/binrw/tests/ui/args_vec_mistakes.stderr +++ b/binrw/tests/ui/args_vec_mistakes.stderr @@ -1,8 +1,8 @@ error[E0277]: the trait bound `VecArgs<()>: Default` is not satisfied - --> tests/ui/args_vec_mistakes.rs:7:12 + --> tests/ui/args_vec_mistakes.rs:7:20 | -7 | struct Foo(Vec); - | ^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>` +7 | struct MissingArgs(Vec); + | ^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>` | = note: required for `VecArgs<()>` to implement `MissingArgsDirective` note: required by a bound in `Required::args` @@ -12,21 +12,21 @@ note: required by a bound in `Required::args` | ^^^^^^^^^^^^^^^^^^^^ required by this bound in `Required::args` error[E0308]: mismatched types - --> tests/ui/args_vec_mistakes.rs:10:17 + --> tests/ui/args_vec_mistakes.rs:10:23 | -10 | struct Bar(#[br(args((),))] Vec); - | ^^^^ ------- expected due to this - | | - | expected struct `VecArgs`, found tuple +10 | struct WrongType(#[br(args((),))] Vec); + | ^^^^ ------- expected due to this + | | + | expected struct `VecArgs`, found tuple | = note: expected struct `VecArgs<()>` found tuple `((),)` error[E0599]: the method `finalize` exists for struct `binrw::binread::impls::VecArgsBuilder<(), Needed, Satisfied>`, but its trait bounds were not satisfied - --> tests/ui/args_vec_mistakes.rs:13:17 + --> tests/ui/args_vec_mistakes.rs:13:26 | -13 | struct Baz(#[br(args { inner: () })] Vec); - | ^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(), Needed, Satisfied>` due to unsatisfied trait bounds +13 | struct MissingCount(#[br(args { inner: () })] Vec); + | ^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(), Needed, Satisfied>` due to unsatisfied trait bounds | ::: src/named_args.rs | @@ -37,11 +37,30 @@ error[E0599]: the method `finalize` exists for struct `binrw::binread::impls::Ve `Needed: SatisfiedOrOptional` = note: this error originates in the macro `binrw::args` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `usize: From>` is not satisfied + --> tests/ui/args_vec_mistakes.rs:22:36 + | +22 | struct WrongCountType(#[br(count = Some(1))] Vec); + | ^^^^^^^ the trait `From>` is not implemented for `usize` + | + = help: the following other types implement trait `From`: + > + > + > + > + > + > + > + > + and $N others + = note: required for `Option<{integer}>` to implement `Into` + = note: required for `usize` to implement `TryFrom>` + error[E0599]: the method `finalize` exists for struct `binrw::binread::impls::VecArgsBuilder<(NoDefault,), Satisfied, Needed>`, but its trait bounds were not satisfied - --> tests/ui/args_vec_mistakes.rs:22:29 + --> tests/ui/args_vec_mistakes.rs:25:42 | -22 | struct Qux(#[br(count = 1)] Vec); - | ^^^^^^^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(NoDefault,), Satisfied, Needed>` due to unsatisfied trait bounds +25 | struct MissingInnerArgs(#[br(count = 1)] Vec); + | ^^^^^^^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(NoDefault,), Satisfied, Needed>` due to unsatisfied trait bounds | ::: src/named_args.rs | @@ -54,9 +73,9 @@ error[E0599]: the method `finalize` exists for struct `binrw::binread::impls::Ve = note: this error originates in the macro `binrw::args` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the trait bound `VecArgs<()>: Default` is not satisfied - --> tests/ui/args_vec_mistakes.rs:25:5 + --> tests/ui/args_vec_mistakes.rs:28:5 | -25 | Vec::::read(&mut binrw::io::Cursor::new(b"")).unwrap(); +28 | Vec::::read(&mut binrw::io::Cursor::new(b"")).unwrap(); | ^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>` | = note: required for `VecArgs<()>` to implement `Required` diff --git a/binrw/tests/ui/invalid_offset_type.stderr b/binrw/tests/ui/invalid_offset_type.stderr index d80cf361..bf4f742d 100644 --- a/binrw/tests/ui/invalid_offset_type.stderr +++ b/binrw/tests/ui/invalid_offset_type.stderr @@ -1,11 +1,11 @@ error[E0308]: mismatched types --> tests/ui/invalid_offset_type.rs:6:19 | -3 | #[derive(BinRead)] - | ------- arguments to this function are incorrect -... 6 | #[br(offset = a)] - | ^ expected `u64`, found `u8` + | ^ + | | + | expected `u64`, found `u8` + | arguments to this function are incorrect | note: associated function defined here --> src/file_ptr.rs @@ -20,11 +20,11 @@ help: you can convert a `u8` to a `u64` error[E0308]: mismatched types --> tests/ui/invalid_offset_type.rs:8:25 | -3 | #[derive(BinRead)] - | ------- arguments to this function are incorrect -... 8 | #[br(offset_after = d)] - | ^ expected `u64`, found `u8` + | ^ + | | + | expected `u64`, found `u8` + | arguments to this function are incorrect | note: associated function defined here --> src/file_ptr.rs diff --git a/binrw_derive/src/binrw/codegen/mod.rs b/binrw_derive/src/binrw/codegen/mod.rs index 3e7c3a69..b79b0e02 100644 --- a/binrw_derive/src/binrw/codegen/mod.rs +++ b/binrw_derive/src/binrw/codegen/mod.rs @@ -229,13 +229,13 @@ fn directives_to_args(field: &StructField) -> TokenStream { let args = field .count .as_ref() - .map(|count| quote! { count: ((#count) as usize) }) + .map(|count| quote_spanned! { count.span()=> count: usize::try_from(#count).unwrap() }) .into_iter() .chain( field .offset .as_ref() - .map(|offset| quote! { offset: (#offset) }) + .map(|offset| quote_spanned! { offset.span()=> offset: #offset }) .into_iter(), ); quote! { #(#args,)* } diff --git a/binrw_derive/src/binrw/codegen/read_options/struct.rs b/binrw_derive/src/binrw/codegen/read_options/struct.rs index b5ba856e..b3293afc 100644 --- a/binrw_derive/src/binrw/codegen/read_options/struct.rs +++ b/binrw_derive/src/binrw/codegen/read_options/struct.rs @@ -179,14 +179,14 @@ impl<'field> AfterParseCallGenerator<'field> { let args_arg = if let Some(offset) = &self.field.offset_after { let offset = offset.as_ref(); if let Some(args_var) = args_var { - quote! {{ + quote_spanned_any! { offset.span()=> { let mut #TEMP = #args_var; #TEMP.offset = #offset; #TEMP }} } else { - quote! { - #ARGS_MACRO! { offset: (#offset) } + quote_spanned_any! { offset.span()=> + #ARGS_MACRO! { offset: #offset } } } } else {