-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for source forwarding in Error derive #293
Draft
MegaBluejay
wants to merge
11
commits into
JelteF:master
Choose a base branch
from
MegaBluejay:error-forward
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2f3f61f
Add support for forwarding source in Error derive
MegaBluejay 05fd28c
Add tests for `#[error(forward)]`
MegaBluejay a42e797
Update changelog
MegaBluejay 936464c
Remove field attr
MegaBluejay 4408d10
Update docs with forwarding details
MegaBluejay 4b0a08f
Add errors on invalid forward usage and compile_fail tests for them
MegaBluejay 9250cca
Add note about recommended forward usage
MegaBluejay b3e425c
Merge branch 'master' into error-forward
MegaBluejay 7688e66
Update forward tests to reflect provide API changes
MegaBluejay 2c43dd1
Add explicit source case to forward tests
MegaBluejay bc7973f
Merge branch 'master' into error-forward
MegaBluejay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use super::*; | ||
|
||
derive_display!(Inner); | ||
#[derive(Debug, Error)] | ||
struct Inner { | ||
source: SimpleErr, | ||
} | ||
|
||
derive_display!(StructAttr); | ||
#[derive(Debug, Error)] | ||
#[error(forward)] | ||
struct StructAttr { | ||
source: Inner, | ||
} | ||
|
||
#[test] | ||
fn struct_attr() { | ||
let err = StructAttr { | ||
source: Inner { source: SimpleErr }, | ||
}; | ||
|
||
assert!(err.source().is_some()); | ||
assert!(err.source().unwrap().is::<SimpleErr>()); | ||
} | ||
|
||
derive_display!(EnumAttr); | ||
#[derive(Debug, Error)] | ||
#[error(forward)] | ||
enum EnumAttr { | ||
A { | ||
source: Inner, | ||
}, | ||
B { | ||
#[error(source)] | ||
explicit_source: Inner, | ||
}, | ||
} | ||
|
||
#[test] | ||
fn enum_attr() { | ||
let err_a = EnumAttr::A { | ||
source: Inner { source: SimpleErr }, | ||
}; | ||
|
||
let err_b = EnumAttr::B { | ||
explicit_source: Inner { source: SimpleErr }, | ||
}; | ||
|
||
assert!(err_a.source().is_some()); | ||
assert!(err_a.source().unwrap().is::<SimpleErr>()); | ||
|
||
assert!(err_b.source().is_some()); | ||
assert!(err_b.source().unwrap().is::<SimpleErr>()); | ||
} | ||
|
||
derive_display!(VariantAttr); | ||
#[derive(Debug, Error)] | ||
enum VariantAttr { | ||
#[error(forward)] | ||
A { | ||
source: Inner, | ||
}, | ||
B { | ||
source: Inner, | ||
}, | ||
} | ||
|
||
#[test] | ||
fn variant_attr() { | ||
let err_a = VariantAttr::A { | ||
source: Inner { source: SimpleErr }, | ||
}; | ||
|
||
let err_b = VariantAttr::B { | ||
source: Inner { source: SimpleErr }, | ||
}; | ||
|
||
assert!(err_a.source().is_some()); | ||
assert!(err_a.source().unwrap().is::<SimpleErr>()); | ||
|
||
assert!(err_b.source().is_some()); | ||
assert!(err_b.source().unwrap().is::<Inner>()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy paste error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No that's correct,
provide
is forwarded only when the source is annotated with#[error(backtrace)]
.That behaviour is unchanged in this PR.
The reasoning for keeping a separate attribute is to make source forwarding work on stable, since there's no good way of generating feature-gated code.
I also think this is somewhat unintuitive, but I haven't come up with a better idea.
Perhaps it could make sense to disallow
#[error(backtrace)]
on the source field if there's no#[error(forward)]
?