Rollup merge of #71400 - dtolnay:isavailable, r=petrochenkov
proc_macro::is_available() This PR adds `proc_macro::is_available() -> bool` to determine whether proc_macro has been made accessible to the currently running program. The proc_macro crate is only intended for use inside the implementation of procedural macros. All the functions in the crate panic if invoked from outside of a procedural macro, such as from a build script or unit test or ordinary Rust binary. Unfortunately those panics made it impossible for libraries that are designed to support both macro and non-macro use cases (e.g. Syn) to be used from binaries that are compiled with panic=abort. In panic=unwind mode we're able to attempt a proc macro call inside catch_unwind and use libproc_macro's result if it succeeds, otherwise fall back to a non-macro alternative implementation. But in panic=abort there was no way to determine which implementation needs to be used. r? @eddyb attn: @petrochenkov @adetaylor ref: https://github.com/dtolnay/cxx/issues/130
This commit is contained in:
commit
0f806534c0
4 changed files with 56 additions and 0 deletions
14
src/test/ui/proc-macro/auxiliary/is-available.rs
Normal file
14
src/test/ui/proc-macro/auxiliary/is-available.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// force-host
|
||||
// no-prefer-dynamic
|
||||
|
||||
#![crate_type = "proc-macro"]
|
||||
#![feature(proc_macro_is_available)]
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::{Literal, TokenStream, TokenTree};
|
||||
|
||||
#[proc_macro]
|
||||
pub fn from_inside_proc_macro(_input: TokenStream) -> TokenStream {
|
||||
proc_macro::is_available().to_string().parse().unwrap()
|
||||
}
|
||||
17
src/test/ui/proc-macro/is-available.rs
Normal file
17
src/test/ui/proc-macro/is-available.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// run-pass
|
||||
|
||||
#![feature(proc_macro_hygiene, proc_macro_is_available)]
|
||||
|
||||
extern crate proc_macro;
|
||||
|
||||
// aux-build:is-available.rs
|
||||
extern crate is_available;
|
||||
|
||||
fn main() {
|
||||
let a = proc_macro::is_available();
|
||||
let b = is_available::from_inside_proc_macro!();
|
||||
let c = proc_macro::is_available();
|
||||
assert!(!a);
|
||||
assert!(b);
|
||||
assert!(!c);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue