Skip to content

Commit

Permalink
implemented backtrace, still need a test
Browse files Browse the repository at this point in the history
  • Loading branch information
thenorili committed Nov 26, 2023
1 parent da84e8c commit 7603e3c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
37 changes: 20 additions & 17 deletions eyre/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@ use std::path::Path;
use std::process::{Command, ExitStatus};
use std::str;

// This code exercises the surface area that we expect of the std Backtrace
// type. If the current toolchain is able to compile it, we go ahead and use
// backtrace in eyre.
// This code exercises the surface area that we expect of the Error generic
// member access API. If the current toolchain is able to compile it, then
// anyhow is able to provide backtrace support.
const BACKTRACE_PROBE: &str = r#"
#![feature(backtrace)]
#![allow(dead_code)]
#![feature(error_generic_member_access)]
use std::backtrace::{Backtrace, BacktraceStatus};
use std::error::Error;
use std::fmt::{self, Display};
use std::backtrace::Backtrace;
use std::error::{self, Error, Request};
use std::fmt::{self, Debug, Display};
#[derive(Debug)]
struct E;
struct MyError(Thing);
struct Thing;
impl Display for E {
impl Debug for MyError {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
unimplemented!()
}
}
impl Error for E {
fn backtrace(&self) -> Option<&Backtrace> {
let backtrace = Backtrace::capture();
match backtrace.status() {
BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}
}
impl Display for MyError {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
unimplemented!()
}
}
impl Error for MyError {
fn provide<'a>(&'a self, request: &mut Request<'a>) {
request.provide_ref(&self.0);
}
}
const _: fn(&dyn Error) -> Option<&Backtrace> = |err| error::request_ref::<Backtrace>(err);
"#;

const TRACK_CALLER_PROBE: &str = r#"
Expand Down
2 changes: 1 addition & 1 deletion eyre/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) enum Backtrace {}
#[cfg(backtrace)]
macro_rules! backtrace_if_absent {
($err:expr) => {
match $err.backtrace() {
match std::error::request_ref::<std::backtrace::Backtrace>($err as &dyn std::error::Error) {
Some(_) => None,
None => Some(Backtrace::capture()),
}
Expand Down
6 changes: 3 additions & 3 deletions eyre/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{ContextCompat, Report, StdError, WrapErr};
use core::fmt::{self, Debug, Display, Write};

#[cfg(backtrace)]
use std::backtrace::Backtrace;
use std::error::Request;

mod ext {
use super::*;
Expand Down Expand Up @@ -144,8 +144,8 @@ where
E: StdError + 'static,
{
#[cfg(backtrace)]
fn backtrace(&self) -> Option<&Backtrace> {
self.error.backtrace()
fn provide<'a>(&'a self, request: &mut Request<'a>) {
StdError::provide(&self.error, request);
}

fn source(&self) -> Option<&(dyn StdError + 'static)> {
Expand Down
6 changes: 4 additions & 2 deletions eyre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
unused_parens,
while_true
)]
#![cfg_attr(backtrace, feature(backtrace))]
#![cfg_attr(backtrace, feature(error_generic_member_access))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![allow(
clippy::needless_doctest_main,
Expand Down Expand Up @@ -789,6 +789,8 @@ impl EyreHandler for DefaultHandler {
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
use core::fmt::Write as _;
#[cfg(backtrace)]
use std::error::request_ref;

if f.alternate() {
return core::fmt::Debug::fmt(error, f);
Expand Down Expand Up @@ -824,7 +826,7 @@ impl EyreHandler for DefaultHandler {
let backtrace = self
.backtrace
.as_ref()
.or_else(|| error.backtrace())
.or_else(|| request_ref::<Backtrace>(error))
.expect("backtrace capture failed");
if let BacktraceStatus::Captured = backtrace.status() {
write!(f, "\n\nStack backtrace:\n{}", backtrace)?;
Expand Down
7 changes: 5 additions & 2 deletions eyre/src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::StdError;
use core::fmt::{self, Debug, Display};

#[cfg(backtrace)]
use std::error::Request;

#[repr(transparent)]
pub(crate) struct DisplayError<M>(pub(crate) M);

Expand Down Expand Up @@ -83,8 +86,8 @@ impl Display for BoxedError {

impl StdError for BoxedError {
#[cfg(backtrace)]
fn backtrace(&self) -> Option<&crate::backtrace::Backtrace> {
self.0.backtrace()
fn provide<'a>(&'a self, request: &mut Request<'a>) {
self.0.provide(request);
}

fn source(&self) -> Option<&(dyn StdError + 'static)> {
Expand Down

0 comments on commit 7603e3c

Please sign in to comment.