Skip to content

Commit

Permalink
Clarify error messages when using attributes in wrong place
Browse files Browse the repository at this point in the history
  • Loading branch information
tjkirch committed Jul 12, 2019
1 parent d311b5b commit fe33842
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 23 deletions.
36 changes: 36 additions & 0 deletions compatibility-tests/compile-fail/tests/ui/attribute-misuse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
mod enum_misuse {
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(display("display should not work here"))]
#[snafu(source(from(XXXX, Box::new)))]
#[snafu(source(true))]
#[snafu(backtrace)]
enum EnumError {
#[snafu(display("an error variant"))]
#[snafu(source(from(XXXX, Box::new)))]
#[snafu(backtrace)]
AVariant {
#[snafu(display("display should not work here"))]
#[snafu(visibility(pub))]
a: String,
},
}
}

mod struct_misuse {
use snafu::Snafu;

#[derive(Debug, Snafu)]
enum UsableError {}

#[derive(Debug, Snafu)]
#[snafu(display("display should not work here"))]
#[snafu(source(from(UsableError, Box::new)))]
#[snafu(visibility(pub))]
#[snafu(source(true))]
#[snafu(backtrace)]
struct StructError(Box<UsableError>);
}

fn main() {}
71 changes: 71 additions & 0 deletions compatibility-tests/compile-fail/tests/ui/attribute-misuse.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
error: 'display' attribute is only valid on variants of an enum deriving Snafu; attempted to place on this enum:
--> $DIR/attribute-misuse.rs:9:10
|
9 | enum EnumError {
| ^^^^^^^^^

error: 'source(from)' attribute is only valid on individual fields; attempted to place on this enum:
--> $DIR/attribute-misuse.rs:9:10
|
9 | enum EnumError {
| ^^^^^^^^^

error: 'source(bool)' attribute is only valid on individual fields; attempted to place on this enum:
--> $DIR/attribute-misuse.rs:9:10
|
9 | enum EnumError {
| ^^^^^^^^^

error: 'backtrace' attribute is only valid on individual fields; attempted to place on this enum:
--> $DIR/attribute-misuse.rs:9:10
|
9 | enum EnumError {
| ^^^^^^^^^

error: 'source' attribute is only valid on individual fields; attempted to place on this variant:
--> $DIR/attribute-misuse.rs:13:9
|
13 | AVariant {
| ^^^^^^^^

error: 'backtrace' attribute is only valid on individual fields; attempted to place on this variant:
--> $DIR/attribute-misuse.rs:13:9
|
13 | AVariant {
| ^^^^^^^^

error: 'display' attribute is only valid on variants of an enum deriving Snafu; attempted to place on this field:
--> $DIR/attribute-misuse.rs:16:13
|
16 | a: String,
| ^

error: 'visibility' attribute is only valid on an enum deriving Snafu and its variants; attempted to place on this field:
--> $DIR/attribute-misuse.rs:16:13
|
16 | a: String,
| ^

error: 'display' attribute is only valid on variants of an enum deriving Snafu; attempted to place on this struct:
--> $DIR/attribute-misuse.rs:33:12
|
33 | struct StructError(Box<UsableError>);
| ^^^^^^^^^^^

error: 'visibility' attribute is only valid on an enum deriving Snafu and its variants; attempted to place on this struct:
--> $DIR/attribute-misuse.rs:33:12
|
33 | struct StructError(Box<UsableError>);
| ^^^^^^^^^^^

error: 'source(bool)' attribute is only valid on individual fields; attempted to place on this struct:
--> $DIR/attribute-misuse.rs:33:12
|
33 | struct StructError(Box<UsableError>);
| ^^^^^^^^^^^

error: 'backtrace' attribute is only valid on individual fields; attempted to place on this struct:
--> $DIR/attribute-misuse.rs:33:12
|
33 | struct StructError(Box<UsableError>);
| ^^^^^^^^^^^
Loading

0 comments on commit fe33842

Please sign in to comment.