Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 100cd04

Browse files
authored
Merge pull request #59 from dtolnay/extern
Allow macro calls anywhere in declaration crate
2 parents 9fea585 + b928826 commit 100cd04

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

build.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::env;
2+
use std::process::Command;
3+
use std::str;
4+
5+
fn main() {
6+
let compiler = match rustc_version() {
7+
Some(compiler) => compiler,
8+
None => return,
9+
};
10+
11+
if compiler.minor < 33 {
12+
println!("cargo:rustc-cfg=proc_macro_hack_no_crate_as_underscore");
13+
}
14+
}
15+
16+
struct Compiler {
17+
minor: u32,
18+
}
19+
20+
fn rustc_version() -> Option<Compiler> {
21+
let rustc = env::var_os("RUSTC")?;
22+
let output = Command::new(rustc).arg("--version").output().ok()?;
23+
let version = str::from_utf8(&output.stdout).ok()?;
24+
let mut pieces = version.split('.');
25+
if pieces.next() != Some("rustc 1") {
26+
return None;
27+
}
28+
let minor = pieces.next()?.parse().ok()?;
29+
Some(Compiler { minor })
30+
}

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ fn expand_export(export: Export, args: ExportArgs) -> TokenStream {
332332
});
333333
}
334334

335+
let extern_crate = if cfg!(proc_macro_hack_no_crate_as_underscore) {
336+
quote!()
337+
} else {
338+
quote! {
339+
#[allow(unused_imports)]
340+
#[macro_use]
341+
extern crate #from as _;
342+
}
343+
};
344+
335345
if export.macros.len() != 1 {
336346
export_dispatch = quote!({#export_dispatch});
337347
export_call_site = quote!({#export_call_site});
@@ -357,6 +367,8 @@ fn expand_export(export: Export, args: ExportArgs) -> TokenStream {
357367
};
358368

359369
let expanded = quote! {
370+
#extern_crate
371+
360372
#[doc(hidden)]
361373
#vis use #from::#actual_names;
362374

0 commit comments

Comments
 (0)