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

WIP: Make tests and examples compile with Rust 1.10 #104

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/quickstart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ fn run() -> Result<()> {
use std::fs::File;

// This operation will fail
File::open("tretrete")
.chain_err(|| "unable to open tretrete file")?;
try!(File::open("tretrete").chain_err(|| "unable to open tretrete file"));

Ok(())
}
Expand Down
18 changes: 6 additions & 12 deletions examples/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@ fn main() {
println!(" ErrorKind::Msg: {}", size_of_val(&msg));
println!(" String: {}", size_of::<String>());
println!(" State: {}", size_of::<error_chain::State>());
let state = error_chain::State::default();
println!(" State.next_error: {}", size_of_val(&state.next_error));
#[cfg(feature = "backtrace")]
{
let state = error_chain::State {
next_error: None,
backtrace: None,
};
println!(" State.next_error: {}", size_of_val(&state.next_error));
fn size_of_backtrace() {
let state = error_chain::State::default();
println!(" State.backtrace: {}", size_of_val(&state.backtrace));
}
#[cfg(not(feature = "backtrace"))]
{
let state = error_chain::State {
next_error: None,
};
println!(" State.next_error: {}", size_of_val(&state.next_error));
}
fn size_of_backtrace() {}
size_of_backtrace();
}
49 changes: 27 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,42 +455,47 @@ pub struct State {
}

impl Default for State {
#[cfg(feature = "backtrace")]
fn default() -> State {
#[cfg(feature = "backtrace")]
let state = State {
State {
next_error: None,
backtrace: make_backtrace(),
};
#[cfg(not(feature = "backtrace"))]
let state = State { next_error: None };
state
}
}

#[cfg(not(feature = "backtrace"))]
fn default() -> State {
State { next_error: None }
}
}

impl State {
/// Creates a new State type
#[cfg(feature = "backtrace")]
pub fn new<CE: ChainedError>(e: Box<error::Error + Send>) -> State {
#[cfg(feature = "backtrace")]
let state = {
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
State {
next_error: Some(e),
backtrace: backtrace,
}
};
#[cfg(not(feature = "backtrace"))]
let state = State { next_error: Some(e) };
let backtrace = CE::extract_backtrace(&*e).or_else(make_backtrace);
State {
next_error: Some(e),
backtrace: backtrace,
}
}

/// Creates a new State type
#[cfg(not(feature = "backtrace"))]
pub fn new<CE: ChainedError>(e: Box<error::Error + Send>) -> State {
State { next_error: Some(e) }
}

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

/// Returns the inner backtrace if present.
#[cfg(not(feature = "backtrace"))]
pub fn backtrace(&self) -> Option<&Backtrace> {
#[cfg(feature = "backtrace")]
let b = self.backtrace.as_ref().map(|v| &**v);
#[cfg(not(feature = "backtrace"))]
let b = None;
b
None
}
}

Expand Down
14 changes: 11 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,18 @@ fn has_backtrace_depending_on_env() {

#[test]
fn chain_err() {
use std::fmt;
use std::env;

error_chain! {
foreign_links {
Fmt(fmt::Error);
Var(env::VarError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in the commit message: in Rust 1.10, fmt::Error doesn't impl Error.

}
errors {
Test
}
}

let _: Result<()> = Err(fmt::Error).chain_err(|| "");
let _: Result<()> = Err(env::VarError::NotPresent).chain_err(|| "");
let _: Result<()> = Err(Error::from_kind(ErrorKind::Test)).chain_err(|| "");
}

Expand All @@ -262,6 +262,8 @@ fn links() {
Test(test::Error, test::ErrorKind);
}
}

{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in the commit message: this works around a Rust 1.10 bug (fixed in 1.11 via rust-lang/rust#34436 ), which causes Rust to think that an item-generating macro invoked at the end of a function can only generate expressions instead (and then producing piles of errors when the macro attempts to generate items instead of expressions).

}

#[cfg(test)]
Expand Down Expand Up @@ -435,6 +437,8 @@ fn documentation() {
Variant
}
}

{}
}

#[cfg(test)]
Expand Down Expand Up @@ -469,6 +473,8 @@ fn rustup_regression() {
}
}
}

{}
}

#[test]
Expand Down Expand Up @@ -503,6 +509,8 @@ fn error_first() {

foreign_links { }
}

{}
}

#[test]
Expand Down