Skip to content

Commit

Permalink
Merge pull request #306 from shepmaster/location
Browse files Browse the repository at this point in the history
Add implicit data generation and use it for location tracking
  • Loading branch information
shepmaster committed Sep 26, 2021
2 parents addb824 + 7739ac2 commit ad7bec5
Show file tree
Hide file tree
Showing 24 changed files with 1,124 additions and 103 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -25,11 +25,14 @@ exclude = [
features = [ "std", "backtraces", "futures", "guide" ]

[features]
default = ["std"]
default = ["std", "rust_1_46"]

# Implement the `std::error::Error` trait.
std = []

# Add support for `#[track_caller]`
rust_1_46 = ["snafu-derive/rust_1_46"]

# Makes the backtrace type live
backtraces = ["std", "backtrace"]

Expand Down
Expand Up @@ -22,4 +22,16 @@ mod backtrace {
}
}

mod implicit {
use snafu::prelude::*;

#[derive(Debug, Snafu)]
enum EnumError {
AVariant {
#[snafu(implicit(false))]
not_location: u8,
},
}
}

fn main() {}
Expand Up @@ -9,3 +9,9 @@ error: `backtrace(false)` attribute is only valid on a field named "backtrace",
|
19 | #[snafu(backtrace(false))]
| ^^^^^^^^^^^^^^^^

error: `implicit(false)` attribute is only valid on a field named "location", not on other fields
--> $DIR/attribute-misuse-opt-out-wrong-field.rs:31:21
|
31 | #[snafu(implicit(false))]
| ^^^^^^^^^^^^^^^
3 changes: 3 additions & 0 deletions compatibility-tests/compile-fail/tests/ui/attribute-misuse.rs
Expand Up @@ -7,6 +7,7 @@ mod enum_misuse {
#[snafu(source(true))]
#[snafu(backtrace)]
#[snafu(context)]
#[snafu(implicit)]
enum EnumError {
AVariant,
}
Expand All @@ -22,6 +23,7 @@ mod variant_misuse {
#[snafu(source)]
#[snafu(backtrace)]
#[snafu(crate_root(XXXX))]
#[snafu(implicit)]
AVariant,
}
}
Expand Down Expand Up @@ -56,6 +58,7 @@ mod struct_misuse {
#[snafu(source(true))]
#[snafu(backtrace)]
#[snafu(context)]
#[snafu(implicit)]
struct StructError(Box<UsableError>);
}

Expand Down
74 changes: 46 additions & 28 deletions compatibility-tests/compile-fail/tests/ui/attribute-misuse.stderr
Expand Up @@ -28,86 +28,104 @@ error: `context` attribute is only valid on enum variants or structs with named
9 | #[snafu(context)]
| ^^^^^^^

error: `implicit` attribute is only valid on enum variant or struct fields with a name, not on an enum
--> $DIR/attribute-misuse.rs:10:13
|
10 | #[snafu(implicit)]
| ^^^^^^^^

error: `source` attribute is only valid on enum variant or struct fields with a name, not on an enum variant
--> $DIR/attribute-misuse.rs:21:46
--> $DIR/attribute-misuse.rs:22:46
|
21 | #[snafu(display("an error variant"), source(from(XXXX, Box::new)))]
22 | #[snafu(display("an error variant"), source(from(XXXX, Box::new)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `source` attribute is only valid on enum variant or struct fields with a name, not on an enum variant
--> $DIR/attribute-misuse.rs:22:17
--> $DIR/attribute-misuse.rs:23:17
|
22 | #[snafu(source)]
23 | #[snafu(source)]
| ^^^^^^

error: `backtrace` attribute is only valid on enum variant or struct fields with a name, not on an enum variant
--> $DIR/attribute-misuse.rs:23:17
--> $DIR/attribute-misuse.rs:24:17
|
23 | #[snafu(backtrace)]
24 | #[snafu(backtrace)]
| ^^^^^^^^^

error: `crate_root` attribute is only valid on an enum or a struct, not on an enum variant
--> $DIR/attribute-misuse.rs:24:17
--> $DIR/attribute-misuse.rs:25:17
|
24 | #[snafu(crate_root(XXXX))]
25 | #[snafu(crate_root(XXXX))]
| ^^^^^^^^^^^^^^^^

error: `implicit` attribute is only valid on enum variant or struct fields with a name, not on an enum variant
--> $DIR/attribute-misuse.rs:26:17
|
26 | #[snafu(implicit)]
| ^^^^^^^^

error: `display` attribute is only valid on enum variants or structs with named fields, not on a field
--> $DIR/attribute-misuse.rs:35:21
--> $DIR/attribute-misuse.rs:37:21
|
35 | #[snafu(display("display should not work here"))]
37 | #[snafu(display("display should not work here"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `visibility` attribute is only valid on an enum, enum variants, or a struct with named fields, not on a field
--> $DIR/attribute-misuse.rs:36:21
--> $DIR/attribute-misuse.rs:38:21
|
36 | #[snafu(visibility(pub))]
38 | #[snafu(visibility(pub))]
| ^^^^^^^^^^^^^^^

error: Incompatible attributes [`source(false)`, `source(from)`] specified on a field
--> $DIR/attribute-misuse.rs:38:21
--> $DIR/attribute-misuse.rs:40:21
|
38 | #[snafu(source(from(XXXX, Box::new)))]
40 | #[snafu(source(from(XXXX, Box::new)))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `context` attribute is only valid on enum variants or structs with named fields, not on a field
--> $DIR/attribute-misuse.rs:39:21
--> $DIR/attribute-misuse.rs:41:21
|
39 | #[snafu(context)]
41 | #[snafu(context)]
| ^^^^^^^

error: `crate_root` attribute is only valid on an enum or a struct, not on a field
--> $DIR/attribute-misuse.rs:40:21
--> $DIR/attribute-misuse.rs:42:21
|
40 | #[snafu(crate_root(XXXX))]
42 | #[snafu(crate_root(XXXX))]
| ^^^^^^^^^^^^^^^^

error: `display` attribute is only valid on enum variants or structs with named fields, not on a tuple struct
--> $DIR/attribute-misuse.rs:53:13
--> $DIR/attribute-misuse.rs:55:13
|
53 | #[snafu(display("display should not work here"))]
55 | #[snafu(display("display should not work here"))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `visibility` attribute is only valid on an enum, enum variants, or a struct with named fields, not on a tuple struct
--> $DIR/attribute-misuse.rs:55:13
--> $DIR/attribute-misuse.rs:57:13
|
55 | #[snafu(visibility(pub))]
57 | #[snafu(visibility(pub))]
| ^^^^^^^^^^^^^^^

error: `source(bool)` attribute is only valid on enum variant or struct fields with a name, not on a tuple struct
--> $DIR/attribute-misuse.rs:56:13
--> $DIR/attribute-misuse.rs:58:13
|
56 | #[snafu(source(true))]
58 | #[snafu(source(true))]
| ^^^^^^^^^^^^

error: `backtrace` attribute is only valid on enum variant or struct fields with a name, not on a tuple struct
--> $DIR/attribute-misuse.rs:57:13
--> $DIR/attribute-misuse.rs:59:13
|
57 | #[snafu(backtrace)]
59 | #[snafu(backtrace)]
| ^^^^^^^^^

error: `context` attribute is only valid on enum variants or structs with named fields, not on a tuple struct
--> $DIR/attribute-misuse.rs:58:13
--> $DIR/attribute-misuse.rs:60:13
|
58 | #[snafu(context)]
60 | #[snafu(context)]
| ^^^^^^^

error: `implicit` attribute is only valid on enum variant or struct fields with a name, not on a tuple struct
--> $DIR/attribute-misuse.rs:61:13
|
61 | #[snafu(implicit)]
| ^^^^^^^^
@@ -0,0 +1,22 @@
use snafu::prelude::*;

#[derive(Debug)]
struct ImplicitData;

impl snafu::GenerateImplicitData for ImplicitData {
fn generate() -> Self {
Self
}
}

#[derive(Debug, Snafu)]
enum EnumError {
AVariant {
// Second attribute should be marked as duplicate
#[snafu(implicit)]
#[snafu(implicit)]
my_data: ImplicitData,
},
}

fn main() {}
@@ -0,0 +1,5 @@
error: Multiple `implicit` attributes are not supported on a field
--> $DIR/duplication-implicit-attributes.rs:17:17
|
17 | #[snafu(implicit)]
| ^^^^^^^^
@@ -1,4 +1,4 @@
error: expected one of: `backtrace`, `context`, `crate_root`, `display`, `source`, `visibility`, `whatever`
error: expected one of: `backtrace`, `context`, `crate_root`, `display`, `implicit`, `source`, `visibility`, `whatever`
--> $DIR/attribute-misuse.rs:5:13
|
5 | #[snafu(unknown_attribute)]
Expand Down
2 changes: 2 additions & 0 deletions compatibility-tests/futures/src/lib.rs
@@ -1,5 +1,7 @@
#![cfg(test)]

mod location;

mod api {
use futures::{stream, StreamExt, TryStream};
use snafu::prelude::*;
Expand Down

0 comments on commit ad7bec5

Please sign in to comment.