Skip to content

Commit

Permalink
Merge pull request #426 from shepmaster/std-backtrace
Browse files Browse the repository at this point in the history
Use `std::backtrace::Backtrace` as the default backtrace provider
  • Loading branch information
shepmaster committed Dec 5, 2023
2 parents a239d70 + ae4679e commit bea7f27
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 453 deletions.
32 changes: 13 additions & 19 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ stable_test_task:
primary_test_script:
- rustc --version
- cargo test --all
backtrace_shim_test_script:
- cargo test --manifest-path compatibility-tests/backtrace-shim/Cargo.toml
backtraces_impl_backtrace_crate_test_script:
- cargo test --manifest-path compatibility-tests/backtraces-impl-backtrace-crate/Cargo.toml
context_selectors_have_documentation_test_script:
Expand Down Expand Up @@ -63,8 +61,6 @@ doc_test_task:
- rustc +nightly --version
docs_script:
- cargo +nightly doc
backtraces_docs_script:
- cargo +nightly doc --features=backtraces
backtraces_impl_backtrace_crate_docs_script:
- cargo +nightly doc --features=backtraces-impl-backtrace-crate
futures_docs_script:
Expand All @@ -82,8 +78,6 @@ doc_tests_task:
fingerprint_script: cat Cargo.toml
version_information_script:
- rustc +nightly --version
backtraces_doctests_script:
- cargo +nightly test --doc --features=backtraces
backtraces_impl_backtrace_crate_doctests_script:
- cargo +nightly test --doc --features=backtraces-impl-backtrace-crate
futures_doctests_script:
Expand Down Expand Up @@ -171,35 +165,35 @@ nightly_test_task:
- cargo test
before_cache_script: rm -rf $CARGO_HOME/registry/index

std_backtraces_test_task:
name: "Rust Beta (backtraces)"
# Our Minimal Supported Rust Version (MSRV)
v1_56_test_task:
name: "Rust 1.56"
container:
image: rust:latest
image: rust:1.56
cpu: 1
memory: 2Gi
cargo_cache:
folder: $CARGO_HOME/registry
fingerprint_script: cat Cargo.toml
setup_script:
- rustup toolchain install beta
backtraces_impl_std_test_script:
- cd compatibility-tests/backtraces-impl-std && RUST_BACKTRACE=1 cargo test
backtraces_impl_std_docs_script:
- cargo +beta doc --features=backtraces-impl-std
primary_test_script:
- rustup self update
- cd compatibility-tests/v1_56/
- rustc --version
- cargo test
before_cache_script: rm -rf $CARGO_HOME/registry/index

v1_56_test_task:
name: "Rust 1.56"
v1_61_test_task:
name: "Rust 1.61"
container:
image: rust:1.56
image: rust:1.61
cpu: 1
memory: 2Gi
cargo_cache:
folder: $CARGO_HOME/registry
fingerprint_script: cat Cargo.toml
primary_test_script:
- rustup self update
- cd compatibility-tests/v1_56/
- cd compatibility-tests/v1_61/
- rustc --version
- cargo test
before_cache_script: rm -rf $CARGO_HOME/registry/index
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ exclude = [
]

[package.metadata.docs.rs]
# The backtraces-impl-* features are incompatible with each other
features = [ "std", "backtraces", "futures", "guide" ]
# We don't use `all-features` for a few reasons:
# - `backtraces-impl-backtrace-crate` replaces the default stdlib implementation
# - `unstable-*` features might break with little warning
# - `internal-dev-dependencies` is internal
features = ["futures", "guide"]

[features]
default = ["std", "rust_1_61"]
default = ["std", "rust_1_65"]

# Implement the `std::error::Error` trait.
std = []
Expand All @@ -37,14 +40,11 @@ unstable-core-error = []
# Add support for `Termination` for `Report`
rust_1_61 = ["snafu-derive/rust_1_61"]

# Makes the backtrace type live
backtraces = ["std", "backtrace"]
# `Backtrace` was stabilized
rust_1_65 = ["rust_1_61"]

# The backtrace type becomes `backtrace::Backtrace`
backtraces-impl-backtrace-crate = ["backtraces"]

# The backtrace type becomes `std::backtrace::Backtrace`
backtraces-impl-std = []
backtraces-impl-backtrace-crate = ["backtrace"]

# The std::error::Error provider API will be implemented.
unstable-provider-api = ["snafu-derive/unstable-provider-api"]
Expand Down
1 change: 0 additions & 1 deletion compatibility-tests/backtrace-shim/src/lib.rs

This file was deleted.

35 changes: 0 additions & 35 deletions compatibility-tests/backtrace-shim/tests/backtrace.rs

This file was deleted.

64 changes: 0 additions & 64 deletions compatibility-tests/backtrace-shim/tests/backtrace_delegation.rs

This file was deleted.

20 changes: 0 additions & 20 deletions compatibility-tests/backtrace-shim/tests/whatever_nested.rs

This file was deleted.

8 changes: 0 additions & 8 deletions compatibility-tests/backtraces-impl-std/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion compatibility-tests/backtraces-impl-std/rust-toolchain

This file was deleted.

37 changes: 0 additions & 37 deletions compatibility-tests/backtraces-impl-std/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion compatibility-tests/report-provider-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
snafu = { path = "../..", features = ["backtraces-impl-std", "unstable-provider-api"] }
snafu = { path = "../..", features = ["unstable-provider-api"] }
2 changes: 1 addition & 1 deletion compatibility-tests/v1_56/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ authors = ["Jake Goulding <jake.goulding@gmail.com>"]
edition = "2018"

[dependencies]
snafu = { path = "../..", default-features = false, features = ["backtraces"] }
snafu = { path = "../..", default-features = false, features = ["std"] }
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "backtrace-shim"
name = "v1_61"
version = "0.1.0"
authors = ["Jake Goulding <jake.goulding@gmail.com>"]
edition = "2018"

[dependencies]
snafu = { path = "../..", features = ["backtraces"] }
snafu = { path = "../..", default-features = false, features = ["std", "rust_1_61"] }
1 change: 1 addition & 0 deletions compatibility-tests/v1_61/rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.61
58 changes: 58 additions & 0 deletions compatibility-tests/v1_61/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![cfg(test)]

use snafu::{prelude::*, Backtrace, ErrorCompat};

type AnotherError = Box<dyn std::error::Error>;

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Invalid user {}:\n{}", user_id, backtrace))]
InvalidUser { user_id: i32, backtrace: Backtrace },
WithSource {
source: AnotherError,
backtrace: Backtrace,
},
WithSourceAndOtherInfo {
user_id: i32,
source: AnotherError,
backtrace: Backtrace,
},
}

type Result<T, E = Error> = std::result::Result<T, E>;

fn example(user_id: i32) -> Result<()> {
ensure!(user_id >= 42, InvalidUserSnafu { user_id });
Ok(())
}

#[test]
fn display_can_access_backtrace() {
let e = example(0).unwrap_err();
let text = e.to_string();
assert!(
text.contains("disabled backtrace"),
"{:?} does not contain expected text",
text
);
}

fn trigger() -> Result<(), AnotherError> {
Err("boom".into())
}

#[test]
fn errors_with_sources_can_have_backtraces() {
let e = trigger().context(WithSourceSnafu).unwrap_err();
let backtrace = ErrorCompat::backtrace(&e).unwrap();
assert!(backtrace.to_string().contains("disabled backtrace"));
}

#[test]
fn errors_with_sources_and_other_info_can_have_backtraces() {
let e = trigger()
.context(WithSourceAndOtherInfoSnafu { user_id: 42 })
.unwrap_err();
let backtrace = ErrorCompat::backtrace(&e).unwrap();
assert!(backtrace.to_string().contains("disabled backtrace"));
}
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[build]
command = """
rustup install nightly --profile minimal && \
cargo +nightly doc --no-deps --features=std,backtraces,futures,guide
cargo +nightly doc --no-deps --features=futures,guide
"""
publish = "target/doc"

Expand Down
13 changes: 13 additions & 0 deletions src/backtrace_impl_backtrace_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub use backtrace::Backtrace;

impl crate::GenerateImplicitData for Backtrace {
fn generate() -> Self {
Backtrace::new()
}
}

impl crate::AsBacktrace for Backtrace {
fn as_backtrace(&self) -> Option<&Backtrace> {
Some(self)
}
}
File renamed without changes.
Loading

0 comments on commit bea7f27

Please sign in to comment.