1 use std::env;
2 use std::fs;
3 use std::path::Path;
4 use std::process::{Command, ExitStatus, Stdio};
5 use std::str;
6 
7 // This code exercises the surface area that we expect of the Error generic
8 // member access API. If the current toolchain is able to compile it, then
9 // thiserror is able to provide backtrace support.
10 const PROBE: &str = r#"
11     #![feature(error_generic_member_access)]
12 
13     use std::error::{Error, Request};
14     use std::fmt::{self, Debug, Display};
15 
16     struct MyError(Thing);
17     struct Thing;
18 
19     impl Debug for MyError {
20         fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
21             unimplemented!()
22         }
23     }
24 
25     impl Display for MyError {
26         fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
27             unimplemented!()
28         }
29     }
30 
31     impl Error for MyError {
32         fn provide<'a>(&'a self, request: &mut Request<'a>) {
33             request.provide_ref(&self.0);
34         }
35     }
36 "#;
37 
main()38 fn main() {
39     match compile_probe() {
40         Some(status) if status.success() => println!("cargo:rustc-cfg=error_generic_member_access"),
41         _ => {}
42     }
43 }
44 
compile_probe() -> Option<ExitStatus>45 fn compile_probe() -> Option<ExitStatus> {
46     if env::var_os("RUSTC_STAGE").is_some() {
47         // We are running inside rustc bootstrap. This is a highly non-standard
48         // environment with issues such as:
49         //
50         //     https://github.com/rust-lang/cargo/issues/11138
51         //     https://github.com/rust-lang/rust/issues/114839
52         //
53         // Let's just not use nightly features here.
54         return None;
55     }
56 
57     let rustc = env::var_os("RUSTC")?;
58     let out_dir = env::var_os("OUT_DIR")?;
59     let probefile = Path::new(&out_dir).join("probe.rs");
60     fs::write(&probefile, PROBE).ok()?;
61 
62     // Make sure to pick up Cargo rustc configuration.
63     let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
64         let mut cmd = Command::new(wrapper);
65         // The wrapper's first argument is supposed to be the path to rustc.
66         cmd.arg(rustc);
67         cmd
68     } else {
69         Command::new(rustc)
70     };
71 
72     cmd.stderr(Stdio::null())
73         .arg("--edition=2018")
74         .arg("--crate-name=thiserror_build")
75         .arg("--crate-type=lib")
76         .arg("--emit=metadata")
77         .arg("--out-dir")
78         .arg(out_dir)
79         .arg(probefile);
80 
81     if let Some(target) = env::var_os("TARGET") {
82         cmd.arg("--target").arg(target);
83     }
84 
85     // If Cargo wants to set RUSTFLAGS, use that.
86     if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
87         if !rustflags.is_empty() {
88             for arg in rustflags.split('\x1f') {
89                 cmd.arg(arg);
90             }
91         }
92     }
93 
94     cmd.status().ok()
95 }
96