Skip to content

Commit 644786e

Browse files
committed
compiletest: Support --extern options with proc-macro directive
So that `pub-priv1.rs` test does not have to (ab)use the aux-crate directive for this purpose. This is very edge-casey so I don't think we should document this in rustc-dev-guide. If someone eneds to do this they will look at the code and easily find the functionality. This is a bit hacky since `--extern priv:pm.rs` is not valid, but we can make our directives work however we want. And I think this is a fine pragmatic balance. Doing it "the right way" would be a lot of work for not much gain. Plus, that work can be done incrementally in small steps in the future.
1 parent 9f6cd6d commit 644786e

4 files changed

Lines changed: 58 additions & 27 deletions

File tree

src/tools/compiletest/src/directives/auxiliary.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ use crate::directives::DirectiveLine;
1010
/// The value of an `aux-crate` directive.
1111
#[derive(Clone, Debug, Default)]
1212
pub struct AuxCrate {
13+
/// With `aux-crate: noprelude:foo=bar.rs` this will be `noprelude`.
14+
pub extern_opts: Option<String>,
1315
/// With `aux-crate: foo=bar.rs` this will be `foo`.
14-
/// With `aux-crate: noprelude:foo=bar.rs` this will be `noprelude:foo`.
16+
/// With `aux-crate: noprelude:foo=bar.rs` this will be `foo`.
1517
pub name: String,
1618
/// With `aux-crate: foo=bar.rs` this will be `bar.rs`.
1719
pub path: String,
1820
}
1921

22+
/// The value of a `proc-macro` directive.
23+
#[derive(Clone, Debug, Default)]
24+
pub(crate) struct ProcMacro {
25+
/// With `proc-macro: bar.rs` this will be `bar.rs`.
26+
pub name: String,
27+
/// With `proc-macro: noprelude:bar.rs` this will be `noprelude`.
28+
pub extern_opts: Option<String>,
29+
}
30+
2031
/// Properties parsed from `aux-*` test directives.
2132
#[derive(Clone, Debug, Default)]
2233
pub(crate) struct AuxProps {
@@ -29,7 +40,7 @@ pub(crate) struct AuxProps {
2940
/// to build and pass with the `--extern` flag.
3041
pub(crate) crates: Vec<AuxCrate>,
3142
/// Same as `builds`, but for proc-macros.
32-
pub(crate) proc_macros: Vec<String>,
43+
pub(crate) proc_macros: Vec<ProcMacro>,
3344
/// Similar to `builds`, but also uses the resulting dylib as a
3445
/// `-Zcodegen-backend` when compiling the test file.
3546
pub(crate) codegen_backend: Option<String>,
@@ -45,7 +56,7 @@ impl AuxProps {
4556
.chain(builds.iter().map(String::as_str))
4657
.chain(bins.iter().map(String::as_str))
4758
.chain(crates.iter().map(|c| c.path.as_str()))
48-
.chain(proc_macros.iter().map(String::as_str))
59+
.chain(proc_macros.iter().map(|p| p.name.as_str()))
4960
.chain(codegen_backend.iter().map(String::as_str))
5061
}
5162
}
@@ -66,17 +77,29 @@ pub(super) fn parse_and_update_aux(
6677
config.push_name_value_directive(ln, AUX_BUILD, &mut aux.builds, |r| r.trim().to_string());
6778
config.push_name_value_directive(ln, AUX_BIN, &mut aux.bins, |r| r.trim().to_string());
6879
config.push_name_value_directive(ln, AUX_CRATE, &mut aux.crates, parse_aux_crate);
69-
config
70-
.push_name_value_directive(ln, PROC_MACRO, &mut aux.proc_macros, |r| r.trim().to_string());
80+
config.push_name_value_directive(ln, PROC_MACRO, &mut aux.proc_macros, parse_proc_macro);
81+
7182
if let Some(r) = config.parse_name_value_directive(ln, AUX_CODEGEN_BACKEND) {
7283
aux.codegen_backend = Some(r.trim().to_owned());
7384
}
7485
}
7586

7687
fn parse_aux_crate(r: String) -> AuxCrate {
7788
let mut parts = r.trim().splitn(2, '=');
78-
AuxCrate {
79-
name: parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string(),
80-
path: parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string(),
81-
}
89+
let opts_and_name = parts.next().expect("missing aux-crate name (e.g. log=log.rs)").to_string();
90+
let path = parts.next().expect("missing aux-crate value (e.g. log=log.rs)").to_string();
91+
let (opts, name) = match opts_and_name.split_once(':') {
92+
None => (None, opts_and_name),
93+
Some((opts, name)) => (Some(opts.to_string()), name.to_string()),
94+
};
95+
AuxCrate { extern_opts: opts, name, path }
96+
}
97+
98+
fn parse_proc_macro(r: String) -> ProcMacro {
99+
let (opts, path): (Option<String>, String) = match r.trim().split_once(':') {
100+
None => (None, r.to_string()),
101+
Some((opts, name)) => (Some(opts.to_string()), name.to_string()),
102+
};
103+
104+
ProcMacro { name: path.to_string(), extern_opts: opts }
82105
}

src/tools/compiletest/src/runtest.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,23 +1277,36 @@ impl<'test> TestCx<'test> {
12771277
.replace('-', "_")
12781278
};
12791279

1280-
let add_extern =
1281-
|rustc: &mut Command, aux_name: &str, aux_path: &str, aux_type: AuxType| {
1282-
let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1283-
if let Some(lib_name) = lib_name {
1284-
rustc.arg("--extern").arg(format!("{}={}/{}", aux_name, aux_dir, lib_name));
1285-
}
1286-
};
1280+
let add_extern = |rustc: &mut Command,
1281+
extern_opts: Option<&str>,
1282+
aux_name: &str,
1283+
aux_path: &str,
1284+
aux_type: AuxType| {
1285+
let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1286+
if let Some(lib_name) = lib_name {
1287+
let opts_and_name = match extern_opts {
1288+
Some(opts) => format!("{opts}:{aux_name}"),
1289+
None => aux_name.to_string(),
1290+
};
1291+
rustc.arg("--extern").arg(format!("{opts_and_name}={aux_dir}/{lib_name}",));
1292+
}
1293+
};
12871294

1288-
for AuxCrate { name, path } in &self.props.aux.crates {
1295+
for AuxCrate { extern_opts, name, path } in &self.props.aux.crates {
12891296
let aux_type = self.build_auxiliary(&path, &aux_dir, None);
1290-
add_extern(rustc, name, path, aux_type);
1297+
add_extern(rustc, extern_opts.as_deref(), name, path, aux_type);
12911298
}
12921299

12931300
for proc_macro in &self.props.aux.proc_macros {
1294-
self.build_auxiliary(proc_macro, &aux_dir, Some(AuxType::ProcMacro));
1295-
let crate_name = path_to_crate_name(proc_macro);
1296-
add_extern(rustc, &crate_name, proc_macro, AuxType::ProcMacro);
1301+
self.build_auxiliary(&proc_macro.name, &aux_dir, Some(AuxType::ProcMacro));
1302+
let crate_name = path_to_crate_name(&proc_macro.name);
1303+
add_extern(
1304+
rustc,
1305+
proc_macro.extern_opts.as_deref(),
1306+
&crate_name,
1307+
&proc_macro.name,
1308+
AuxType::ProcMacro,
1309+
);
12971310
}
12981311

12991312
// Build any `//@ aux-codegen-backend`, and pass the resulting library

tests/ui/privacy/pub-priv-dep/auxiliary/pm.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
//@ force-host
2-
//@ no-prefer-dynamic
3-
4-
#![crate_type = "proc-macro"]
5-
61
extern crate proc_macro;
72
use proc_macro::TokenStream;
83

tests/ui/privacy/pub-priv-dep/pub-priv1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ aux-crate:priv:priv_dep=priv_dep.rs
22
//@ aux-build:pub_dep.rs
3-
//@ aux-crate:priv:pm=pm.rs
3+
//@ proc-macro:priv:pm.rs
44
//@ compile-flags: -Zunstable-options
55

66
// Basic behavior check of exported_private_dependencies from either a public

0 commit comments

Comments
 (0)