Skip to content

Commit b986c86

Browse files
committed
enable error backtraces
1 parent a50b9b9 commit b986c86

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
@@ -72,7 +72,7 @@ impl Error {
7272
/// [tracking]: https://github.com/rust-lang/rust/issues/53487
7373
#[cfg(backtrace)]
7474
pub fn backtrace(&self) -> &std::backtrace::Backtrace {
75-
self.error.downcast_ref::<E>()
75+
self.error.backtrace()
7676
}
7777

7878
/// Attempt to downcast the error object to a concrete type.
@@ -106,13 +106,13 @@ impl Error {
106106

107107
impl Display for Error {
108108
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
109-
write!(formatter, "{}", self.error)
109+
Display::fmt(&self.error, formatter)
110110
}
111111
}
112112

113113
impl Debug for Error {
114114
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
115-
write!(formatter, "{}", self.error)
115+
Debug::fmt(&self.error, formatter)
116116
}
117117
}
118118

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)