Skip to content

Commit 02aa6b6

Browse files
committed
Make all nightly go through the module that was probed
1 parent bfb89ef commit 02aa6b6

File tree

8 files changed

+74
-48
lines changed

8 files changed

+74
-48
lines changed

build.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ compile_error! {
1515
fn main() {
1616
let mut error_generic_member_access = false;
1717
if cfg!(feature = "std") {
18-
println!("cargo:rerun-if-changed=build/probe.rs");
18+
println!("cargo:rerun-if-changed=src/nightly.rs");
1919

2020
let consider_rustc_bootstrap;
2121
if compile_probe(false) {
@@ -68,6 +68,7 @@ fn main() {
6868
};
6969

7070
if rustc >= 80 {
71+
println!("cargo:rustc-check-cfg=cfg(anyhow_build_probe)");
7172
println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");
7273
println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");
7374
println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_unwind_safe)");
@@ -128,7 +129,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
128129
let rustc = cargo_env_var("RUSTC");
129130
let out_dir = cargo_env_var("OUT_DIR");
130131
let out_subdir = Path::new(&out_dir).join("probe");
131-
let probefile = Path::new("build").join("probe.rs");
132+
let probefile = Path::new("src").join("nightly.rs");
132133

133134
if let Err(err) = fs::create_dir(&out_subdir) {
134135
if err.kind() != ErrorKind::AlreadyExists {
@@ -152,6 +153,7 @@ fn compile_probe(rustc_bootstrap: bool) -> bool {
152153
}
153154

154155
cmd.stderr(Stdio::null())
156+
.arg("--cfg=anyhow_build_probe")
155157
.arg("--edition=2018")
156158
.arg("--crate-name=anyhow")
157159
.arg("--crate-type=lib")

build/probe.rs

-35
This file was deleted.

src/backtrace.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ macro_rules! backtrace {
3838
#[cfg(error_generic_member_access)]
3939
macro_rules! backtrace_if_absent {
4040
($err:expr) => {
41-
match core::error::request_ref::<std::backtrace::Backtrace>($err as &dyn core::error::Error)
42-
{
41+
match $crate::nightly::request_ref_backtrace($err as &dyn core::error::Error) {
4342
Some(_) => None,
4443
None => backtrace!(),
4544
}

src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::convert::Infallible;
44
use core::fmt::{self, Debug, Display, Write};
55

66
#[cfg(error_generic_member_access)]
7-
use core::error::Request;
7+
use crate::nightly::{self, Request};
88

99
mod ext {
1010
use super::*;
@@ -145,7 +145,7 @@ where
145145

146146
#[cfg(error_generic_member_access)]
147147
fn provide<'a>(&'a self, request: &mut Request<'a>) {
148-
StdError::provide(&self.error, request);
148+
nightly::provide(&self.error, request);
149149
}
150150
}
151151

src/error.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::backtrace::Backtrace;
22
use crate::chain::Chain;
3+
#[cfg(error_generic_member_access)]
4+
use crate::nightly::{self, Request};
35
#[cfg(any(feature = "std", not(anyhow_no_core_error), anyhow_no_ptr_addr_of))]
46
use crate::ptr::Mut;
57
use crate::ptr::{Own, Ref};
68
use crate::{Error, StdError};
79
use alloc::boxed::Box;
810
use core::any::TypeId;
9-
#[cfg(error_generic_member_access)]
10-
use core::error::{self, Request};
1111
use core::fmt::{self, Debug, Display};
1212
use core::mem::ManuallyDrop;
1313
#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
@@ -995,7 +995,7 @@ impl ErrorImpl {
995995
.as_ref()
996996
.or_else(|| {
997997
#[cfg(error_generic_member_access)]
998-
return error::request_ref::<Backtrace>(unsafe { Self::error(this) });
998+
return nightly::request_ref_backtrace(unsafe { Self::error(this) });
999999
#[cfg(not(error_generic_member_access))]
10001000
return unsafe { (vtable(this.ptr).object_backtrace)(this) };
10011001
})
@@ -1005,9 +1005,9 @@ impl ErrorImpl {
10051005
#[cfg(error_generic_member_access)]
10061006
unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {
10071007
if let Some(backtrace) = unsafe { &this.deref().backtrace } {
1008-
request.provide_ref(backtrace);
1008+
nightly::provide_ref_backtrace(request, backtrace);
10091009
}
1010-
unsafe { Self::error(this) }.provide(request);
1010+
nightly::provide(unsafe { Self::error(this) }, request);
10111011
}
10121012

10131013
#[cold]

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ mod error;
260260
mod fmt;
261261
mod kind;
262262
mod macros;
263+
#[cfg(error_generic_member_access)]
264+
mod nightly;
263265
mod ptr;
264266
mod wrapper;
265267

src/nightly.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This code exercises the surface area that we expect of the Error generic
2+
// member access API. If the current toolchain is able to compile it, then
3+
// anyhow is able to provide backtrace support.
4+
5+
#![cfg_attr(anyhow_build_probe, feature(error_generic_member_access))]
6+
7+
use core::error::{self, Error};
8+
use std::backtrace::Backtrace;
9+
10+
pub use core::error::Request;
11+
12+
#[cfg(anyhow_build_probe)]
13+
const _: () = {
14+
use core::fmt::{self, Debug, Display};
15+
16+
struct MyError(Backtrace);
17+
18+
impl Debug for MyError {
19+
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
20+
unimplemented!()
21+
}
22+
}
23+
24+
impl Display for MyError {
25+
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
26+
unimplemented!()
27+
}
28+
}
29+
30+
impl Error for MyError {
31+
fn provide<'a>(&'a self, request: &mut Request<'a>) {
32+
provide_ref_backtrace(request, &self.0);
33+
}
34+
}
35+
};
36+
37+
// Include in sccache cache key.
38+
#[cfg(anyhow_build_probe)]
39+
const _: Option<&str> = option_env!("RUSTC_BOOTSTRAP");
40+
41+
pub fn request_ref_backtrace(err: &dyn Error) -> Option<&Backtrace> {
42+
request_ref::<Backtrace>(err)
43+
}
44+
45+
fn request_ref<'a, T>(err: &'a (impl Error + ?Sized)) -> Option<&'a T>
46+
where
47+
T: 'static + ?Sized,
48+
{
49+
error::request_ref::<T>(err)
50+
}
51+
52+
pub fn provide_ref_backtrace<'a>(request: &mut Request<'a>, backtrace: &'a Backtrace) {
53+
Request::provide_ref(request, backtrace);
54+
}
55+
56+
pub fn provide<'a>(err: &'a (impl Error + ?Sized), request: &mut Request<'a>) {
57+
Error::provide(err, request);
58+
}

src/wrapper.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::fmt::{self, Debug, Display};
55
use alloc::boxed::Box;
66

77
#[cfg(error_generic_member_access)]
8-
use core::error::Request;
8+
use crate::nightly::{self, Request};
99

1010
#[repr(transparent)]
1111
pub struct MessageError<M>(pub M);
@@ -79,6 +79,6 @@ impl StdError for BoxedError {
7979

8080
#[cfg(error_generic_member_access)]
8181
fn provide<'a>(&'a self, request: &mut Request<'a>) {
82-
self.0.provide(request);
82+
nightly::provide(&*self.0, request);
8383
}
8484
}

0 commit comments

Comments
 (0)