Skip to content

Commit

Permalink
Add error checks for duplicate attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkirch committed Jul 9, 2019
1 parent 1100c87 commit da2d4ac
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
28 changes: 28 additions & 0 deletions compatibility-tests/compile-fail/tests/ui/attribute-duplication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod enum_duplication {
use snafu::Snafu;

#[derive(Debug, Snafu)]
enum EnumError {
#[snafu(display("an error variant"))]
AVariant {
#[snafu(source(from(EnumError, Box::new)))]
#[snafu(backtrace(delegate))]
source: Box<EnumError>,
#[snafu(source)]
#[snafu(backtrace(delegate))]
source2: String,
#[snafu(source)]
#[snafu(backtrace(delegate))]
source3: String,

#[snafu(backtrace)]
backtrace1: String,
#[snafu(backtrace)]
backtrace2: String,
#[snafu(backtrace)]
backtrace3: String,
},
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error: Only one field can be designated as 'source'; found a duplicate here:
--> $DIR/attribute-duplication.rs:16:13
|
16 | source3: String,
| ^^^^^^^

error: Only one field can be designated as 'source'; found a duplicate here:
--> $DIR/attribute-duplication.rs:13:13
|
13 | source2: String,
| ^^^^^^^

error: Only one field can be designated as 'backtrace'; found a duplicate here:
--> $DIR/attribute-duplication.rs:23:13
|
23 | backtrace3: String,
| ^^^^^^^^^^

error: Only one field can be designated as 'backtrace'; found a duplicate here:
--> $DIR/attribute-duplication.rs:21:13
|
21 | backtrace2: String,
| ^^^^^^^^^^

error: Only one field can be designated as 'backtrace(delegate)'; found a duplicate here:
--> $DIR/attribute-duplication.rs:16:13
|
16 | source3: String,
| ^^^^^^^

error: Only one field can be designated as 'backtrace(delegate)'; found a duplicate here:
--> $DIR/attribute-duplication.rs:13:13
|
13 | source2: String,
| ^^^^^^^

error: Cannot have 'backtrace' and 'backtrace(delegate)' fields in the same enum
--> $DIR/attribute-duplication.rs:19:13
|
19 | backtrace1: String,
| ^^^^^^^^^^

error: Cannot have 'backtrace' and 'backtrace(delegate)' fields in the same enum
--> $DIR/attribute-duplication.rs:10:13
|
10 | source: Box<EnumError>,
| ^^^^^^
39 changes: 35 additions & 4 deletions snafu-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,46 @@ fn parse_snafu_enum(
}
}

// Reverse the lists of fields where we may find duplicates so that we can call the
// second (and later) items the "duplicates" without uglier indexing.
source_fields.reverse();
backtrace_fields.reverse();
backtrace_delegates.reverse();

let source_field = source_fields.pop();
// Report a warning if there are multiple?
for extra_field in source_fields.into_iter() {
errors.push(syn::Error::new(
extra_field.name.span(),
"Only one field can be designated as 'source'; found a duplicate here:",
));
}

let backtrace_field = backtrace_fields.pop();
// Report a warning if there are multiple?
for extra_field in backtrace_fields.into_iter() {
errors.push(syn::Error::new(
extra_field.name.span(),
"Only one field can be designated as 'backtrace'; found a duplicate here:",
));
}

let backtrace_delegate = backtrace_delegates.pop();
// Report a warning if there are multiple?
// Report a warning if delegating and our own?
for extra_field in backtrace_delegates.into_iter() {
errors.push(syn::Error::new(
extra_field.name.span(),
"Only one field can be designated as 'backtrace(delegate)'; found a duplicate here:",
));
}

if backtrace_field.is_some() && backtrace_delegate.is_some() {
errors.push(syn::Error::new(
backtrace_field.as_ref().unwrap().name.span(),
"Cannot have 'backtrace' and 'backtrace(delegate)' fields in the same enum",
));
errors.push(syn::Error::new(
backtrace_delegate.as_ref().unwrap().name.span(),
"Cannot have 'backtrace' and 'backtrace(delegate)' fields in the same enum",
));
}

Ok(VariantInfo {
name,
Expand Down

0 comments on commit da2d4ac

Please sign in to comment.