Skip to content

Commit

Permalink
Fix error reporting of bad count/offset/offset_after
Browse files Browse the repository at this point in the history
  • Loading branch information
csnover committed Nov 15, 2022
1 parent 448b0cd commit 8ec595a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
11 changes: 7 additions & 4 deletions binrw/tests/ui/args_vec_mistakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use binrw::BinRead;
struct NoDefault;

#[derive(BinRead)]
struct Foo(Vec<u8>);
struct MissingArgs(Vec<u8>);

#[derive(BinRead)]
struct Bar(#[br(args((),))] Vec<u8>);
struct WrongType(#[br(args((),))] Vec<u8>);

#[derive(BinRead)]
struct Baz(#[br(args { inner: () })] Vec<u8>);
struct MissingCount(#[br(args { inner: () })] Vec<u8>);

#[derive(BinRead)]
#[br(import(_a: NoDefault))]
Expand All @@ -19,7 +19,10 @@ struct Inner {
}

#[derive(BinRead)]
struct Qux(#[br(count = 1)] Vec<Inner>);
struct WrongCountType(#[br(count = Some(1))] Vec<u8>);

#[derive(BinRead)]
struct MissingInnerArgs(#[br(count = 1)] Vec<Inner>);

fn main() {
Vec::<u8>::read(&mut binrw::io::Cursor::new(b"")).unwrap();
Expand Down
51 changes: 35 additions & 16 deletions binrw/tests/ui/args_vec_mistakes.stderr
Original file line number Diff line number Diff line change
@@ -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<u8>);
| ^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>`
7 | struct MissingArgs(Vec<u8>);
| ^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>`
|
= note: required for `VecArgs<()>` to implement `MissingArgsDirective`
note: required by a bound in `Required::args`
Expand All @@ -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<u8>);
| ^^^^ ------- expected due to this
| |
| expected struct `VecArgs`, found tuple
10 | struct WrongType(#[br(args((),))] Vec<u8>);
| ^^^^ ------- 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<u8>);
| ^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(), Needed, Satisfied>` due to unsatisfied trait bounds
13 | struct MissingCount(#[br(args { inner: () })] Vec<u8>);
| ^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(), Needed, Satisfied>` due to unsatisfied trait bounds
|
::: src/named_args.rs
|
Expand All @@ -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<Option<{integer}>>` is not satisfied
--> tests/ui/args_vec_mistakes.rs:22:36
|
22 | struct WrongCountType(#[br(count = Some(1))] Vec<u8>);
| ^^^^^^^ the trait `From<Option<{integer}>>` is not implemented for `usize`
|
= help: the following other types implement trait `From<T>`:
<f32 as From<i16>>
<f32 as From<i8>>
<f32 as From<u16>>
<f32 as From<u8>>
<f64 as From<f32>>
<f64 as From<i16>>
<f64 as From<i32>>
<f64 as From<i8>>
and $N others
= note: required for `Option<{integer}>` to implement `Into<usize>`
= note: required for `usize` to implement `TryFrom<Option<{integer}>>`

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<Inner>);
| ^^^^^^^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(NoDefault,), Satisfied, Needed>` due to unsatisfied trait bounds
25 | struct MissingInnerArgs(#[br(count = 1)] Vec<Inner>);
| ^^^^^^^^^^ method cannot be called on `binrw::binread::impls::VecArgsBuilder<(NoDefault,), Satisfied, Needed>` due to unsatisfied trait bounds
|
::: src/named_args.rs
|
Expand All @@ -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::<u8>::read(&mut binrw::io::Cursor::new(b"")).unwrap();
28 | Vec::<u8>::read(&mut binrw::io::Cursor::new(b"")).unwrap();
| ^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `VecArgs<()>`
|
= note: required for `VecArgs<()>` to implement `Required`
Expand Down
16 changes: 8 additions & 8 deletions binrw/tests/ui/invalid_offset_type.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions binrw_derive/src/binrw/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,)* }
Expand Down
6 changes: 3 additions & 3 deletions binrw_derive/src/binrw/codegen/read_options/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8ec595a

Please sign in to comment.