Skip to content

Commit 169b83c

Browse files
Aaron1011Mark-Simulacrum
authored andcommitted
Account for version number in NtIdent hack
Issue #74616 tracks a backwards-compatibility hack for certain macros. This has is implemented by hard-coding the filenames and macro names of certain code that we want to continue to compile. However, the initial implementation of the hack was based on the directory structure when building the crate from its repository (e.g. `js-sys/src/lib.rs`). When the crate is build as a dependency, it will include a version number from the clone from the cargo registry (e.g. `js-sys-0.3.17/src/lib.rs`), which would fail the check. This commit modifies the backwards-compatibility hack to check that desired crate name (`js-sys` or `time-macros-impl`) is a prefix of the proper part of the path. See #76070 (comment) for more details.
1 parent ed2eb80 commit 169b83c

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

src/librustc_ast/token.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,19 @@ impl Nonterminal {
821821
if let ExpnKind::Macro(_, macro_name) = orig_span.ctxt().outer_expn_data().kind {
822822
let filename = source_map.span_to_filename(orig_span);
823823
if let FileName::Real(RealFileName::Named(path)) = filename {
824-
if (path.ends_with("time-macros-impl/src/lib.rs")
825-
&& macro_name == sym::impl_macros)
826-
|| (path.ends_with("js-sys/src/lib.rs") && macro_name == sym::arrays)
824+
let matches_prefix = |prefix| {
825+
// Check for a path that ends with 'prefix*/src/lib.rs'
826+
let mut iter = path.components().rev();
827+
iter.next().and_then(|p| p.as_os_str().to_str()) == Some("lib.rs")
828+
&& iter.next().and_then(|p| p.as_os_str().to_str()) == Some("src")
829+
&& iter
830+
.next()
831+
.and_then(|p| p.as_os_str().to_str())
832+
.map_or(false, |p| p.starts_with(prefix))
833+
};
834+
835+
if (macro_name == sym::impl_macros && matches_prefix("time-macros-impl"))
836+
|| (macro_name == sym::arrays && matches_prefix("js-sys"))
827837
{
828838
let snippet = source_map.span_to_snippet(orig_span);
829839
if snippet.as_deref() == Ok("$name") {

src/test/ui/proc-macro/group-compat-hack/group-compat-hack.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,37 @@ extern crate std;
1313
// place of a `None`-delimited group. This allows us to maintain
1414
// backwards compatibility for older versions of these crates.
1515

16-
include!("js-sys/src/lib.rs");
17-
include!("time-macros-impl/src/lib.rs");
16+
mod no_version {
17+
include!("js-sys/src/lib.rs");
18+
include!("time-macros-impl/src/lib.rs");
1819

19-
macro_rules! other {
20-
($name:ident) => {
21-
#[my_macro] struct Three($name);
20+
macro_rules! other {
21+
($name:ident) => {
22+
#[my_macro] struct Three($name);
23+
}
2224
}
25+
26+
struct Foo;
27+
impl_macros!(Foo);
28+
arrays!(Foo);
29+
other!(Foo);
2330
}
2431

25-
fn main() {
32+
mod with_version {
33+
include!("js-sys-0.3.17/src/lib.rs");
34+
include!("time-macros-impl-0.1.0/src/lib.rs");
35+
36+
macro_rules! other {
37+
($name:ident) => {
38+
#[my_macro] struct Three($name);
39+
}
40+
}
41+
2642
struct Foo;
2743
impl_macros!(Foo);
2844
arrays!(Foo);
2945
other!(Foo);
3046
}
47+
48+
49+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/time-macros-impl/src/lib.rs:5:21: 5:27 (#5) }, Ident { ident: "One", span: $DIR/time-macros-impl/src/lib.rs:5:28: 5:31 (#5) }, Group { delimiter: Parenthesis, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:27:18: 27:21 (#0) }], span: $DIR/time-macros-impl/src/lib.rs:5:31: 5:38 (#5) }, Punct { ch: ';', spacing: Alone, span: $DIR/time-macros-impl/src/lib.rs:5:38: 5:39 (#5) }]
22
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/js-sys/src/lib.rs:5:21: 5:27 (#9) }, Ident { ident: "Two", span: $DIR/js-sys/src/lib.rs:5:28: 5:31 (#9) }, Group { delimiter: Parenthesis, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:28:13: 28:16 (#0) }], span: $DIR/js-sys/src/lib.rs:5:31: 5:38 (#9) }, Punct { ch: ';', spacing: Alone, span: $DIR/js-sys/src/lib.rs:5:38: 5:39 (#9) }]
3-
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/group-compat-hack.rs:21:21: 21:27 (#13) }, Ident { ident: "Three", span: $DIR/group-compat-hack.rs:21:28: 21:33 (#13) }, Group { delimiter: Parenthesis, stream: TokenStream [Group { delimiter: None, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:29:12: 29:15 (#0) }], span: $DIR/group-compat-hack.rs:21:34: 21:39 (#13) }], span: $DIR/group-compat-hack.rs:21:33: 21:40 (#13) }, Punct { ch: ';', spacing: Alone, span: $DIR/group-compat-hack.rs:21:40: 21:41 (#13) }]
3+
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/group-compat-hack.rs:22:25: 22:31 (#13) }, Ident { ident: "Three", span: $DIR/group-compat-hack.rs:22:32: 22:37 (#13) }, Group { delimiter: Parenthesis, stream: TokenStream [Group { delimiter: None, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:29:12: 29:15 (#0) }], span: $DIR/group-compat-hack.rs:22:38: 22:43 (#13) }], span: $DIR/group-compat-hack.rs:22:37: 22:44 (#13) }, Punct { ch: ';', spacing: Alone, span: $DIR/group-compat-hack.rs:22:44: 22:45 (#13) }]
4+
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/time-macros-impl-0.1.0/src/lib.rs:5:21: 5:27 (#19) }, Ident { ident: "One", span: $DIR/time-macros-impl-0.1.0/src/lib.rs:5:28: 5:31 (#19) }, Group { delimiter: Parenthesis, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:43:18: 43:21 (#0) }], span: $DIR/time-macros-impl-0.1.0/src/lib.rs:5:31: 5:38 (#19) }, Punct { ch: ';', spacing: Alone, span: $DIR/time-macros-impl-0.1.0/src/lib.rs:5:38: 5:39 (#19) }]
5+
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/js-sys-0.3.17/src/lib.rs:5:21: 5:27 (#23) }, Ident { ident: "Two", span: $DIR/js-sys-0.3.17/src/lib.rs:5:28: 5:31 (#23) }, Group { delimiter: Parenthesis, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:44:13: 44:16 (#0) }], span: $DIR/js-sys-0.3.17/src/lib.rs:5:31: 5:38 (#23) }, Punct { ch: ';', spacing: Alone, span: $DIR/js-sys-0.3.17/src/lib.rs:5:38: 5:39 (#23) }]
6+
Called proc_macro_hack with TokenStream [Ident { ident: "struct", span: $DIR/group-compat-hack.rs:38:25: 38:31 (#27) }, Ident { ident: "Three", span: $DIR/group-compat-hack.rs:38:32: 38:37 (#27) }, Group { delimiter: Parenthesis, stream: TokenStream [Group { delimiter: None, stream: TokenStream [Ident { ident: "Foo", span: $DIR/group-compat-hack.rs:45:12: 45:15 (#0) }], span: $DIR/group-compat-hack.rs:38:38: 38:43 (#27) }], span: $DIR/group-compat-hack.rs:38:37: 38:44 (#27) }, Punct { ch: ';', spacing: Alone, span: $DIR/group-compat-hack.rs:38:44: 38:45 (#27) }]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ignore-test this is not a test
2+
3+
macro_rules! arrays {
4+
($name:ident) => {
5+
#[my_macro] struct Two($name);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ignore-test this is not a test
2+
3+
macro_rules! impl_macros {
4+
($name:ident) => {
5+
#[my_macro] struct One($name);
6+
}
7+
}

0 commit comments

Comments
 (0)