Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 339af70

Browse files
committed
Make error_chain with default-features = false work with Rust 1.10
error_chain uses attributes on statements, stablized in Rust 1.13. Factor them out of functions in the non-backtrace codepath, making it possible to build a crate that uses error_chain with Rust 1.10. The backtrace feature still requires Rust 1.13 for attributes on statements, due to the impl_extract_backtrace macro, which needs to emit a function with one block for each linked error type, some of which may have attributes on them. And any code using compile-time conditionals in attributes on variants within an error_chain! invocation (which includes many of the tests and examples) will produce an error due to rust-lang/rust#22250 (fixed in Rust 1.11). But a crate using error_chain with default-features = false and avoiding such compile-time conditionals will now compile.
1 parent 74021c5 commit 339af70

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/lib.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -455,42 +455,47 @@ pub struct State {
455455
}
456456

457457
impl Default for State {
458+
#[cfg(feature = "backtrace")]
458459
fn default() -> State {
459-
#[cfg(feature = "backtrace")]
460-
let state = State {
460+
State {
461461
next_error: None,
462462
backtrace: make_backtrace(),
463-
};
464-
#[cfg(not(feature = "backtrace"))]
465-
let state = State { next_error: None };
466-
state
463+
}
464+
}
465+
466+
#[cfg(not(feature = "backtrace"))]
467+
fn default() -> State {
468+
State { next_error: None }
467469
}
468470
}
469471

470472
impl State {
471473
/// Creates a new State type
474+
#[cfg(feature = "backtrace")]
472475
pub fn new<CE: ChainedError>(e: Box<error::Error + Send>) -> State {
473-
#[cfg(feature = "backtrace")]
474-
let state = {
475-
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
476-
State {
477-
next_error: Some(e),
478-
backtrace: backtrace,
479-
}
480-
};
481-
#[cfg(not(feature = "backtrace"))]
482-
let state = State { next_error: Some(e) };
476+
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
477+
State {
478+
next_error: Some(e),
479+
backtrace: backtrace,
480+
}
481+
}
482+
483+
/// Creates a new State type
484+
#[cfg(not(feature = "backtrace"))]
485+
pub fn new<CE: ChainedError>(e: Box<error::Error + Send>) -> State {
486+
State { next_error: Some(e) }
487+
}
483488

484-
state
489+
/// Returns the inner backtrace if present.
490+
#[cfg(feature = "backtrace")]
491+
pub fn backtrace(&self) -> Option<&Backtrace> {
492+
self.backtrace.as_ref().map(|v| &**v)
485493
}
486494

487495
/// Returns the inner backtrace if present.
496+
#[cfg(not(feature = "backtrace"))]
488497
pub fn backtrace(&self) -> Option<&Backtrace> {
489-
#[cfg(feature = "backtrace")]
490-
let b = self.backtrace.as_ref().map(|v| &**v);
491-
#[cfg(not(feature = "backtrace"))]
492-
let b = None;
493-
b
498+
None
494499
}
495500
}
496501

0 commit comments

Comments
 (0)