Skip to content

Commit

Permalink
Fix generated Error::provide and tests to reflect recent changes in…
Browse files Browse the repository at this point in the history
… nightly APIs (#294)

## Synopsis

Update derive and tests to work on the latest nightly with the changes
in [this PR](rust-lang/rust#113464).


## Solution

Use `error::{Request, request_ref, request_value}` instead of
`any::{Demand, request_ref, request_value}`.

Currently, `request_value` is not re-exported from `core::error` in
`std::error`, which means backtrace tests require
`#![feature(error_in_core)]`.

Presumably this is a bug, and will be fixed later.
  • Loading branch information
MegaBluejay authored Aug 15, 2023
1 parent 4aefdbb commit de4f141
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 190 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- The `Into` derive now uses `#[into(<types>)]` instead of `#[into(types(<types>))]`
and ignores field type itself.
- Importing a derive macro now also import its corresponding trait.
- The `Error` derive is updated with changes to the `error_generic_member_access` unstable feature for nightly users. ([#200](https://github.com/JelteF/derive_more/pull/200), [#294](https://github.com/JelteF/derive_more/pull/294))

### Added

Expand Down
11 changes: 6 additions & 5 deletions impl/doc/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ Backtraces don't work though, because the `Backtrace` type is only available in
## Example usage

```rust
# #![cfg_attr(nightly, feature(error_generic_member_access, provide_any))]
# #![cfg_attr(nightly, feature(error_generic_member_access, error_in_core))]
// Nightly requires enabling these features:
// #![feature(error_generic_member_access, provide_any)]
// #![feature(error_generic_member_access, error_in_core)]
# #[cfg(not(nightly))] fn main() {}
# #[cfg(nightly)] fn main() {
# use std::{any, error::Error as _, backtrace::Backtrace};
# use core::error::{request_ref, request_value, Error as __};
# use std::backtrace::Backtrace;
#
# use derive_more::{Display, Error, From};

Expand Down Expand Up @@ -129,7 +130,7 @@ enum CompoundError {
}

assert!(Simple.source().is_none());
assert!(any::request_ref::<Backtrace>(&Simple).is_none());
assert!(request_ref::<Backtrace>(&Simple).is_none());
assert!(WithSource::default().source().is_some());
assert!(WithExplicitSource::default().source().is_some());
assert!(Tuple::default().source().is_some());
Expand All @@ -139,7 +140,7 @@ let with_source_and_backtrace = WithSourceAndBacktrace {
backtrace: Backtrace::capture(),
};
assert!(with_source_and_backtrace.source().is_some());
assert!(any::request_ref::<Backtrace>(&with_source_and_backtrace).is_some());
assert!(request_ref::<Backtrace>(&with_source_and_backtrace).is_some());

assert!(CompoundError::Simple.source().is_none());
assert!(CompoundError::from(Simple).source().is_some());
Expand Down
14 changes: 7 additions & 7 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn expand(

let provide = provide.map(|provide| {
quote! {
fn provide<'_demand>(&'_demand self, demand: &mut ::core::any::Demand<'_demand>) {
fn provide<'_request>(&'_request self, request: &mut ::core::error::Request<'_request>) {
#provide
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let source_provider = self.source.map(|source| {
let source_expr = &self.data.members[source];
quote! {
::derive_more::Error::provide(&#source_expr, demand);
::derive_more::Error::provide(&#source_expr, request);
}
});
let backtrace_provider = self
Expand All @@ -215,7 +215,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
.then(|| {
let backtrace_expr = &self.data.members[backtrace];
quote! {
demand.provide_ref::<::std::backtrace::Backtrace>(&#backtrace_expr);
request.provide_ref::<::std::backtrace::Backtrace>(&#backtrace_expr);
}
});

Expand All @@ -235,7 +235,7 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
let pattern = self.data.matcher(&[source], &[quote! { source }]);
Some(quote! {
#pattern => {
::derive_more::Error::provide(source, demand);
::derive_more::Error::provide(source, request);
}
})
}
Expand All @@ -246,16 +246,16 @@ impl<'input, 'state> ParsedFields<'input, 'state> {
);
Some(quote! {
#pattern => {
demand.provide_ref::<::std::backtrace::Backtrace>(backtrace);
::derive_more::Error::provide(source, demand);
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
::derive_more::Error::provide(source, request);
}
})
}
None => {
let pattern = self.data.matcher(&[backtrace], &[quote! { backtrace }]);
Some(quote! {
#pattern => {
demand.provide_ref::<::std::backtrace::Backtrace>(backtrace);
request.provide_ref::<::std::backtrace::Backtrace>(backtrace);
}
})
}
Expand Down
80 changes: 40 additions & 40 deletions tests/error/nightly/derives_for_enums_with_backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]

use std::any;
use core::error::{request_ref, request_value};

use super::*;

Expand Down Expand Up @@ -146,7 +146,7 @@ impl TestErr {
}

fn get_source_backtrace(&self) -> &Backtrace {
any::request_ref(match self {
request_ref(match self {
Self::NamedExplicitBacktraceFromSource { err }
| Self::NamedExplicitDifferentSourceAndBacktrace { err, .. }
| Self::NamedImplicitDifferentSourceAndBacktrace { err, .. }
Expand All @@ -163,14 +163,14 @@ type MyBacktrace = Backtrace;

#[test]
fn unit() {
assert!(any::request_ref::<Backtrace>(&TestErr::Unit).is_none());
assert!(request_ref::<Backtrace>(&TestErr::Unit).is_none());
}

#[test]
fn named_implicit_no_backtrace() {
let err = TestErr::NamedImplicitNoBacktrace { field: 0 };

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
Expand All @@ -180,7 +180,7 @@ fn named_implicit_backtrace_by_field_name() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -191,7 +191,7 @@ fn named_implicit_backtrace_by_field_type() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -202,7 +202,7 @@ fn named_explicit_no_backtrace_by_field_name() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
Expand All @@ -212,7 +212,7 @@ fn named_explicit_no_backtrace_by_field_type() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
Expand All @@ -222,7 +222,7 @@ fn named_explicit_backtrace() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -233,7 +233,7 @@ fn named_explicit_no_backtrace_redundant() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
Expand All @@ -243,7 +243,7 @@ fn named_explicit_backtrace_by_field_name_redundant() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -254,7 +254,7 @@ fn named_explicit_backtrace_by_field_type_redundant() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -266,7 +266,7 @@ fn named_explicit_suppresses_implicit() {
field: 0,
};

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_unused_backtrace);
}
Expand All @@ -280,8 +280,8 @@ fn named_implicit_no_backtrace_from_source() {
};

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(any::request_value::<i32>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
assert!(request_value::<i32>(&err).is_none());
}

#[test]
Expand All @@ -293,8 +293,8 @@ fn named_explicit_no_backtrace_from_source() {
};

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(any::request_value::<i32>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
assert!(request_value::<i32>(&err).is_none());
}

#[test]
Expand All @@ -306,8 +306,8 @@ fn named_explicit_backtrace_from_source() {
};

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_source_backtrace);
}

Expand All @@ -321,8 +321,8 @@ fn named_implicit_different_source_and_backtrace() {
};

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_source_backtrace);
}
Expand All @@ -337,8 +337,8 @@ fn named_explicit_different_source_and_backtrace() {
};

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_source_backtrace);
}
Expand All @@ -347,29 +347,29 @@ fn named_explicit_different_source_and_backtrace() {
fn unnamed_implicit_no_backtrace() {
let err = TestErr::UnnamedImplicitNoBacktrace(0, 0);

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
fn unnamed_implicit_backtrace() {
let err = TestErr::UnnamedImplicitBacktrace(Backtrace::force_capture(), 0, 0);

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

#[test]
fn unnamed_explicit_no_backtrace() {
let err = TestErr::UnnamedExplicitNoBacktrace(Backtrace::force_capture(), 0);

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
fn unnamed_explicit_backtrace() {
let err = TestErr::UnnamedExplicitBacktrace(Backtrace::force_capture(), 0, 0);

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -378,15 +378,15 @@ fn unnamed_explicit_no_backtrace_redundant() {
let err =
TestErr::UnnamedExplicitNoBacktraceRedundant(Backtrace::force_capture(), 0);

assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
}

#[test]
fn unnamed_explicit_backtrace_redundant() {
let err =
TestErr::UnnamedExplicitBacktraceRedundant(Backtrace::force_capture(), 0, 0);

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
}

Expand All @@ -398,7 +398,7 @@ fn unnamed_explicit_suppresses_implicit() {
0,
);

assert!(any::request_ref::<Backtrace>(&err).is_some());
assert!(request_ref::<Backtrace>(&err).is_some());
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_unused_backtrace);
}
Expand All @@ -410,8 +410,8 @@ fn unnamed_implicit_no_backtrace_from_source() {
});

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(any::request_value::<i32>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
assert!(request_value::<i32>(&err).is_none());
}

#[test]
Expand All @@ -421,8 +421,8 @@ fn unnamed_explicit_no_backtrace_from_source() {
});

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_none());
assert!(any::request_value::<i32>(&err).is_none());
assert!(request_ref::<Backtrace>(&err).is_none());
assert!(request_value::<i32>(&err).is_none());
}

#[test]
Expand All @@ -432,8 +432,8 @@ fn unnamed_explicit_backtrace_from_source() {
});

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_source_backtrace);
}

Expand All @@ -447,8 +447,8 @@ fn unnamed_implicit_different_source_and_backtrace() {
);

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_source_backtrace);
}
Expand All @@ -463,8 +463,8 @@ fn unnamed_explicit_different_source_and_backtrace() {
);

assert!(err.source().is_some());
assert!(any::request_ref::<Backtrace>(&err).is_some());
assert_eq!(any::request_value::<i32>(&err), Some(42));
assert!(request_ref::<Backtrace>(&err).is_some());
assert_eq!(request_value::<i32>(&err), Some(42));
assert_bt!(==, err, .get_stored_backtrace);
assert_bt!(!=, err, .get_source_backtrace);
}
Loading

0 comments on commit de4f141

Please sign in to comment.