Skip to content

Commit f008e88

Browse files
committed
Do not get proc_macro from the sysroot in rustc
With the stage0 refactor the proc_macro version found in the sysroot will no longer always match the proc_macro version that proc-macros get compiled with by the rustc executable that uses this proc_macro. This will cause problems as soon as the ABI of the bridge gets changed to implement new features or change the way existing features work. To fix this, this commit changes rustc crates to depend directly on the local version of proc_macro which will also be used in the sysroot that rustc will build.
1 parent b5eb989 commit f008e88

File tree

13 files changed

+39
-18
lines changed

13 files changed

+39
-18
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,13 @@ dependencies = [
28932893
"unicode-ident",
28942894
]
28952895

2896+
[[package]]
2897+
name = "proc_macro"
2898+
version = "0.0.0"
2899+
dependencies = [
2900+
"rustc-literal-escaper",
2901+
]
2902+
28962903
[[package]]
28972904
name = "psm"
28982905
version = "0.1.26"
@@ -3428,6 +3435,7 @@ dependencies = [
34283435
name = "rustc_builtin_macros"
34293436
version = "0.0.0"
34303437
dependencies = [
3438+
"proc_macro",
34313439
"rustc_ast",
34323440
"rustc_ast_pretty",
34333441
"rustc_attr_data_structures",
@@ -3719,6 +3727,7 @@ dependencies = [
37193727
name = "rustc_expand"
37203728
version = "0.0.0"
37213729
dependencies = [
3730+
"proc_macro",
37223731
"rustc_ast",
37233732
"rustc_ast_passes",
37243733
"rustc_ast_pretty",
@@ -4066,6 +4075,7 @@ dependencies = [
40664075
"libc",
40674076
"libloading",
40684077
"odht",
4078+
"proc_macro",
40694079
"rustc_abi",
40704080
"rustc_ast",
40714081
"rustc_attr_data_structures",

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ exclude = [
5454
"build",
5555
"compiler/rustc_codegen_cranelift",
5656
"compiler/rustc_codegen_gcc",
57+
# Otherwise bootstrap complains that this crate is included in two separate workspaces
58+
"library/proc_macro",
5759
"src/bootstrap",
5860
"tests/rustdoc-gui",
5961
# HACK(eddyb) This hardcodes the fact that our CI uses `/checkout/obj`.

compiler/rustc_builtin_macros/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ doctest = false
88

99
[dependencies]
1010
# tidy-alphabetical-start
11+
# We must use the proc_macro version that we will compile proc-macros against,
12+
# not the one from our own sysroot.
13+
proc_macro = { path = "../../library/proc_macro", default-features = false }
1114
rustc_ast = { path = "../rustc_ast" }
1215
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1316
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#![recursion_limit = "256"]
2121
// tidy-alphabetical-end
2222

23-
extern crate proc_macro;
24-
2523
use std::sync::Arc;
2624

2725
use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};

compiler/rustc_expand/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ doctest = false
99

1010
[dependencies]
1111
# tidy-alphabetical-start
12+
# We must use the proc_macro version that we will compile proc-macros against,
13+
# not the one from our own sysroot.
14+
proc_macro = { path = "../../library/proc_macro", default-features = false }
1215
rustc_ast = { path = "../rustc_ast" }
1316
rustc_ast_passes = { path = "../rustc_ast_passes" }
1417
rustc_ast_pretty = { path = "../rustc_ast_pretty" }

compiler/rustc_expand/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#![feature(yeet_expr)]
1515
// tidy-alphabetical-end
1616

17-
extern crate proc_macro as pm;
18-
1917
mod build;
2018
mod errors;
2119
// FIXME(Nilstrieb) Translate macro_rules diagnostics

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use rustc_ast as ast;
21
use rustc_ast::ptr::P;
32
use rustc_ast::tokenstream::TokenStream;
43
use rustc_errors::ErrorGuaranteed;
54
use rustc_parse::parser::{ForceCollect, Parser};
65
use rustc_session::config::ProcMacroExecutionStrategy;
76
use rustc_span::Span;
87
use rustc_span::profiling::SpannedEventArgRecorder;
8+
use {proc_macro as pm, rustc_ast as ast};
99

1010
use crate::base::{self, *};
1111
use crate::{errors, proc_macro_server};

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::ops::{Bound, Range};
22

33
use ast::token::IdentIsRaw;
4-
use pm::bridge::{
4+
use proc_macro::bridge::{
55
DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
66
};
7-
use pm::{Delimiter, Level};
7+
use proc_macro::{Delimiter, Level};
88
use rustc_ast as ast;
99
use rustc_ast::token;
1010
use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream};
@@ -66,7 +66,7 @@ impl FromInternal<token::LitKind> for LitKind {
6666
token::CStr => LitKind::CStr,
6767
token::CStrRaw(n) => LitKind::CStrRaw(n),
6868
token::Err(_guar) => {
69-
// This is the only place a `pm::bridge::LitKind::ErrWithGuar`
69+
// This is the only place a `proc_macro::bridge::LitKind::ErrWithGuar`
7070
// is constructed. Note that an `ErrorGuaranteed` is available,
7171
// as required. See the comment in `to_internal`.
7272
LitKind::ErrWithGuar
@@ -149,7 +149,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
149149
}
150150

151151
trees.push(TokenTree::Group(Group {
152-
delimiter: pm::Delimiter::from_internal(delim),
152+
delimiter: proc_macro::Delimiter::from_internal(delim),
153153
stream: Some(stream),
154154
span: DelimSpan {
155155
open: span.open,
@@ -270,7 +270,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
270270
let stream =
271271
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span);
272272
trees.push(TokenTree::Group(Group {
273-
delimiter: pm::Delimiter::None,
273+
delimiter: proc_macro::Delimiter::None,
274274
stream: Some(stream),
275275
span: DelimSpan::from_single(span),
276276
}))
@@ -302,7 +302,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
302302
trees.push(TokenTree::Punct(Punct { ch: b'!', joint: false, span }));
303303
}
304304
trees.push(TokenTree::Group(Group {
305-
delimiter: pm::Delimiter::Bracket,
305+
delimiter: proc_macro::Delimiter::Bracket,
306306
stream: Some(stream),
307307
span: DelimSpan::from_single(span),
308308
}));

compiler/rustc_metadata/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ edition = "2024"
88
bitflags = "2.4.1"
99
libloading = "0.8.0"
1010
odht = { version = "0.3.1", features = ["nightly"] }
11+
# We must use the proc_macro version that we will compile proc-macros against,
12+
# not the one from our own sysroot.
13+
proc_macro = { path = "../../library/proc_macro", default-features = false }
1114
rustc_abi = { path = "../rustc_abi" }
1215
rustc_ast = { path = "../rustc_ast" }
1316
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }

compiler/rustc_metadata/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#![feature(trusted_len)]
1717
// tidy-alphabetical-end
1818

19-
extern crate proc_macro;
20-
2119
pub use rmeta::provide;
2220

2321
mod dependency_format;

library/proc_macro/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ version = "0.0.0"
44
edition = "2024"
55

66
[dependencies]
7-
std = { path = "../std" }
7+
std = { path = "../std", optional = true }
88
# Workaround: when documenting this crate rustdoc will try to load crate named
99
# `core` when resolving doc links. Without this line a different `core` will be
1010
# loaded from sysroot causing duplicate lang items and other similar errors.
11-
core = { path = "../core" }
12-
rustc-literal-escaper = { version = "0.0.2", features = ["rustc-dep-of-std"] }
11+
core = { path = "../core", optional = true }
12+
rustc-literal-escaper = "0.0.2"
13+
14+
[features]
15+
default = ["rustc-dep-of-std"]
16+
rustc-dep-of-std = ["dep:core", "dep:std", "rustc-literal-escaper/rustc-dep-of-std"]

library/proc_macro/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![recursion_limit = "256"]
3333
#![allow(internal_features)]
3434
#![deny(ffi_unwind_calls)]
35+
#![allow(rustc::internal)]
3536
#![warn(rustdoc::unescaped_backticks)]
3637
#![warn(unreachable_pub)]
3738
#![deny(unsafe_op_in_unsafe_fn)]
@@ -95,7 +96,7 @@ pub fn is_available() -> bool {
9596
///
9697
/// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]`
9798
/// and `#[proc_macro_derive]` definitions.
98-
#[rustc_diagnostic_item = "TokenStream"]
99+
#[cfg_attr(feature = "rustc-dep-of-std", rustc_diagnostic_item = "TokenStream")]
99100
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
100101
#[derive(Clone)]
101102
pub struct TokenStream(Option<bridge::client::TokenStream>);

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,8 @@ impl Step for RustcDev {
776776
copy_src_dirs(
777777
builder,
778778
&builder.src,
779-
&["compiler"],
779+
// The compiler has a path dependency on proc_macro, so make sure to include it.
780+
&["compiler", "library/proc_macro"],
780781
&[],
781782
&tarball.image_dir().join("lib/rustlib/rustc-src/rust"),
782783
);

0 commit comments

Comments
 (0)