Skip to content

Commit c29011a

Browse files
committed
enable error backtraces
1 parent ccd7ecb commit c29011a

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

build.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::env;
2+
use std::fs;
3+
use std::path::Path;
4+
use std::process::{Command, ExitStatus, Stdio};
5+
6+
// This code exercises the surface area that we expect of the std Backtrace
7+
// type. If the current toolchain is able to compile it, we go ahead and use
8+
// backtrace in anyhow.
9+
const PROBE: &str = r#"
10+
#![feature(backtrace)]
11+
#![allow(dead_code)]
12+
13+
use std::backtrace::{Backtrace, BacktraceStatus};
14+
use std::error::Error;
15+
use std::fmt::{self, Display};
16+
17+
#[derive(Debug)]
18+
struct E;
19+
20+
impl Display for E {
21+
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
22+
unimplemented!()
23+
}
24+
}
25+
26+
impl Error for E {
27+
fn backtrace(&self) -> Option<&Backtrace> {
28+
let backtrace = Backtrace::capture();
29+
match backtrace.status() {
30+
BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}
31+
}
32+
unimplemented!()
33+
}
34+
}
35+
"#;
36+
37+
fn main() {
38+
match compile_probe() {
39+
Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),
40+
_ => {}
41+
}
42+
}
43+
44+
fn compile_probe() -> Option<ExitStatus> {
45+
let rustc = env::var_os("RUSTC")?;
46+
let out_dir = env::var_os("OUT_DIR")?;
47+
let probefile = Path::new(&out_dir).join("probe.rs");
48+
fs::write(&probefile, PROBE).ok()?;
49+
Command::new(rustc)
50+
.stderr(Stdio::null())
51+
.arg("--edition=2018")
52+
.arg("--crate-name=http_types_build")
53+
.arg("--crate-type=lib")
54+
.arg("--emit=metadata")
55+
.arg("--out-dir")
56+
.arg(out_dir)
57+
.arg(probefile)
58+
.status()
59+
.ok()
60+
}

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Error {
8686
/// [tracking]: https://github.com/rust-lang/rust/issues/53487
8787
#[cfg(backtrace)]
8888
pub fn backtrace(&self) -> &std::backtrace::Backtrace {
89-
self.error.downcast_ref::<E>()
89+
self.error.backtrace()
9090
}
9191

9292
/// Attempt to downcast the error object to a concrete type.
@@ -125,13 +125,13 @@ impl Error {
125125

126126
impl Display for Error {
127127
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
128-
write!(formatter, "{}", self.error)
128+
Display::fmt(&self.error, formatter)
129129
}
130130
}
131131

132132
impl Debug for Error {
133133
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
134-
write!(formatter, "{}", self.error)
134+
Debug::fmt(&self.error, formatter)
135135
}
136136
}
137137

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#![deny(missing_debug_implementations, nonstandard_style)]
9797
#![warn(missing_docs, unreachable_pub)]
9898
#![allow(clippy::new_without_default)]
99+
#![cfg_attr(backtrace, feature(backtrace))]
99100
#![cfg_attr(test, deny(warnings))]
100101
#![cfg_attr(feature = "docs", feature(doc_cfg))]
101102
#![doc(html_favicon_url = "https://yoshuawuyts.com/assets/http-rs/favicon.ico")]

0 commit comments

Comments
 (0)