Skip to content

Commit

Permalink
Add errors on invalid forward usage and compile_fail tests for them
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaBluejay committed Aug 15, 2023
1 parent 92ae1cb commit 78d9d99
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
14 changes: 12 additions & 2 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{spanned::Spanned as _, Error, Result};

Expand Down Expand Up @@ -390,7 +390,12 @@ fn parse_fields_impl<'input, 'state, P>(
where
P: Fn(&str, &syn::Field, usize) -> bool,
{
let MultiFieldData { fields, infos, .. } = state.enabled_fields_data();
let MultiFieldData {
fields,
infos,
variant_info,
..
} = state.enabled_fields_data();

let iter = fields
.iter()
Expand Down Expand Up @@ -418,6 +423,11 @@ where

if let Some((index, _, _)) = source {
parsed_fields.source = Some(index);
} else if variant_info.forward {
return Err(syn::Error::new(
Span::call_site(),
"`#[error(forward)]` cannot be used when an error has no source",
));
}

if let Some((index, _, _)) = backtrace {
Expand Down
18 changes: 18 additions & 0 deletions tests/compile_fail/error/forward_no_source_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use derive_more::Error;

#[derive(Debug, Error)]
#[error(forward)]
enum Foo {
Bar,
Baz {
source: Box<dyn Error + Send + 'static>,
},
}

impl ::core::fmt::Display for Foo {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "")
}
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/compile_fail/error/forward_no_source_enum.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: `#[error(forward)]` cannot be used when an error has no source
--> tests/compile_fail/error/forward_no_source_enum.rs:3:17
|
3 | #[derive(Debug, Error)]
| ^^^^^
|
= note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)
13 changes: 13 additions & 0 deletions tests/compile_fail/error/forward_no_source_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use derive_more::Error;

#[derive(Debug, Error)]
#[error(forward)]
struct Foo;

impl ::core::fmt::Display for Foo {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "")
}
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/compile_fail/error/forward_no_source_struct.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: `#[error(forward)]` cannot be used when an error has no source
--> tests/compile_fail/error/forward_no_source_struct.rs:3:17
|
3 | #[derive(Debug, Error)]
| ^^^^^
|
= note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)
19 changes: 19 additions & 0 deletions tests/compile_fail/error/forward_no_source_variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use derive_more::Error;

#[derive(Debug, Error)]
enum Foo {
#[error(forward)]
Bar,
#[error(forward)]
Baz {
source: Box<dyn Error + Send + 'static>,
},
}

impl ::core::fmt::Display for Foo {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "")
}
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/compile_fail/error/forward_no_source_variant.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: `#[error(forward)]` cannot be used when an error has no source
--> tests/compile_fail/error/forward_no_source_variant.rs:3:17
|
3 | #[derive(Debug, Error)]
| ^^^^^
|
= note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 78d9d99

Please sign in to comment.