Skip to content

Commit 996d72b

Browse files
committed
compiletest: Support --extern modifiers 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 needs 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 approach. 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 if wanted.
1 parent db6bc0f commit 996d72b

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ pub struct AuxCrate {
2626
}
2727

2828
/// The value of a `proc-macro` directive.
29-
#[derive(Clone, Debug, Default)]
29+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
3030
pub(crate) struct ProcMacro {
31+
/// Contains `--extern` modifiers, if any. See the tracking issue for more
32+
/// info: <https://github.com/rust-lang/rust/issues/98405>
33+
/// With `proc-macro: noprelude:bar.rs` this will be `noprelude`.
34+
pub extern_modifiers: Option<String>,
3135
/// With `proc-macro: bar.rs` this will be `bar.rs`.
3236
pub path: String,
3337
}
@@ -108,5 +112,17 @@ fn parse_aux_crate(r: String) -> AuxCrate {
108112
}
109113

110114
fn parse_proc_macro(r: String) -> ProcMacro {
111-
ProcMacro { path: r.trim().to_string() }
115+
let r = r.trim();
116+
117+
// Matches:
118+
// path
119+
// modifiers:path
120+
let caps = static_regex!(r"^(?:(?<modifiers>[^=]*?):)?(?<path>.*)$")
121+
.captures(r)
122+
.expect("can never fail");
123+
124+
let modifiers = caps.name("modifiers").map(|m| m.as_str().to_string());
125+
let path = caps["path"].to_string();
126+
127+
ProcMacro { extern_modifiers: modifiers, path }
112128
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ fn test_aux_crate_value_with_modifiers() {
2525
fn test_aux_crate_value_invalid() {
2626
parse_aux_crate("foo.rs".to_string());
2727
}
28+
29+
#[test]
30+
fn test_proc_macro_value_no_modifiers() {
31+
assert_eq!(
32+
ProcMacro { extern_modifiers: None, path: "foo.rs".to_string() },
33+
parse_proc_macro("foo.rs".to_string())
34+
);
35+
}
36+
37+
#[test]
38+
fn test_proc_macro_value_with_modifiers() {
39+
assert_eq!(
40+
ProcMacro { extern_modifiers: Some("noprelude".to_string()), path: "foo.rs".to_string() },
41+
parse_proc_macro("noprelude:foo.rs".to_string())
42+
);
43+
}

src/tools/compiletest/src/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ impl<'test> TestCx<'test> {
13021302
let crate_name = path_to_crate_name(&proc_macro.path);
13031303
add_extern(
13041304
rustc,
1305-
None, // `extern_modifiers`
1305+
proc_macro.extern_modifiers.as_deref(),
13061306
&crate_name,
13071307
&proc_macro.path,
13081308
AuxType::ProcMacro,

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)