Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1010,6 +1010,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