Skip to content

Commit

Permalink
Auto merge of #7700 - ehuss:proc-macro-extern-prelude, r=alexcrichton
Browse files Browse the repository at this point in the history
Add proc_macro to the extern prelude.

This makes it so that a proc-macro library can use the `proc_macro` crate without the `extern crate proc_macro;` item on the 2018 edition.  This is the Cargo half of rust-lang/rust#64882.
  • Loading branch information
bors committed Dec 16, 2019
2 parents d138d37 + 4d64eb9 commit ad4122a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct TargetInfo {
pub rustflags: Vec<String>,
/// Extra flags to pass to `rustdoc`, see `env_args`.
pub rustdocflags: Vec<String>,
// Remove this when it hits stable (1.41).
pub supports_pathless_extern: Option<bool>,
}

/// Kind of each file generated by a Unit, part of `FileType`.
Expand Down Expand Up @@ -101,6 +103,13 @@ impl TargetInfo {
.args(&rustflags)
.env_remove("RUSTC_LOG");

let mut pathless_test = process.clone();
pathless_test.args(&["--extern", "proc_macro"]);
let supports_pathless_extern = match kind {
CompileKind::Host => Some(rustc.cached_output(&pathless_test).is_ok()),
_ => None,
};

if let CompileKind::Target(target) = kind {
process.arg("--target").arg(target.rustc_target());
}
Expand Down Expand Up @@ -183,6 +192,7 @@ impl TargetInfo {
"RUSTDOCFLAGS",
)?,
cfg,
supports_pathless_extern,
})
}

Expand Down
11 changes: 11 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,17 @@ pub fn extern_args<'a>(
link_to(&dep, dep.extern_crate_name, dep.noprelude)?;
}
}
if unit.target.proc_macro()
&& cx
.bcx
.info(CompileKind::Host)
.supports_pathless_extern
.unwrap()
{
// Automatically import `proc_macro`.
result.push(OsString::from("--extern"));
result.push(OsString::from("proc_macro"));
}

Ok(result)
}
Expand Down
34 changes: 34 additions & 0 deletions tests/testsuite/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,37 @@ Caused by:
.with_status(101)
.run();
}

#[cargo_test]
fn proc_macro_extern_prelude() {
if !is_nightly() {
// remove once pathless `--extern` hits stable (1.41)
return;
}
// Check that proc_macro is in the extern prelude.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[lib]
proc-macro = true
"#,
)
.file(
"src/lib.rs",
r#"
use proc_macro::TokenStream;
#[proc_macro]
pub fn foo(input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
"#,
)
.build();
p.cargo("test").run();
p.cargo("doc").run();
}
5 changes: 5 additions & 0 deletions tests/testsuite/rustc_info_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use std::env;

#[cargo_test]
fn rustc_info_cache() {
if !cargo_test_support::is_nightly() {
// remove once pathless `--extern` hits stable (1.41)
return;
}

let p = project()
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();
Expand Down

0 comments on commit ad4122a

Please sign in to comment.