Skip to content

Commit

Permalink
Merge pull request #130 from dtolnay/ensure
Browse files Browse the repository at this point in the history
Use concat/stringify macros by absolute path
  • Loading branch information
dtolnay authored Dec 6, 2020
2 parents cb57a41 + 435f5ec commit cdfd375
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
30 changes: 25 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use std::fs;
use std::path::Path;
use std::process::{Command, ExitStatus, Stdio};
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
Expand Down Expand Up @@ -35,12 +36,20 @@ const PROBE: &str = r#"
"#;

fn main() {
if !cfg!(feature = "std") {
return;
if cfg!(feature = "std") {
match compile_probe() {
Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),
_ => {}
}
}
match compile_probe() {
Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),
_ => {}

let rustc = match rustc_minor_version() {
Some(rustc) => rustc,
None => return,
};

if rustc < 38 {
println!("cargo:rustc-cfg=anyhow_no_macro_reexport");
}
}

Expand All @@ -61,3 +70,14 @@ fn compile_probe() -> Option<ExitStatus> {
.status()
.ok()
}

fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,27 @@ pub mod private {
{
Error::from_adhoc(message, backtrace!())
}

#[cfg(anyhow_no_macro_reexport)]
pub use crate::{__anyhow_concat as concat, __anyhow_stringify as stringify};
#[cfg(not(anyhow_no_macro_reexport))]
pub use core::{concat, stringify};

#[cfg(anyhow_no_macro_reexport)]
#[doc(hidden)]
#[macro_export]
macro_rules! __anyhow_concat {
($($tt:tt)*) => {
concat!($($tt)*)
};
}

#[cfg(anyhow_no_macro_reexport)]
#[doc(hidden)]
#[macro_export]
macro_rules! __anyhow_stringify {
($($tt:tt)*) => {
stringify!($($tt)*)
};
}
}
5 changes: 4 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ macro_rules! bail {
#[macro_export]
macro_rules! ensure {
($cond:expr $(,)?) => {
$crate::ensure!($cond, concat!("Condition failed: `", stringify!($cond), "`"))
$crate::ensure!(
$cond,
$crate::private::concat!("Condition failed: `", $crate::private::stringify!($cond), "`"),
)
};
($cond:expr, $msg:literal $(,)?) => {
if !$cond {
Expand Down

0 comments on commit cdfd375

Please sign in to comment.