Skip to content

Commit 13718eb

Browse files
committed
Auto merge of #141595 - bjorn3:rustc_no_sysroot_proc_macro, r=onur-ozkan
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.
2 parents 8afd710 + 026baa1 commit 13718eb

File tree

15 files changed

+60
-20
lines changed

15 files changed

+60
-20
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,7 @@ dependencies = [
34453445
"rustc_macros",
34463446
"rustc_parse",
34473447
"rustc_parse_format",
3448+
"rustc_proc_macro",
34483449
"rustc_session",
34493450
"rustc_span",
34503451
"rustc_target",
@@ -3734,6 +3735,7 @@ dependencies = [
37343735
"rustc_lint_defs",
37353736
"rustc_macros",
37363737
"rustc_parse",
3738+
"rustc_proc_macro",
37373739
"rustc_serialize",
37383740
"rustc_session",
37393741
"rustc_span",
@@ -4082,6 +4084,7 @@ dependencies = [
40824084
"rustc_index",
40834085
"rustc_macros",
40844086
"rustc_middle",
4087+
"rustc_proc_macro",
40854088
"rustc_serialize",
40864089
"rustc_session",
40874090
"rustc_span",
@@ -4338,6 +4341,13 @@ dependencies = [
43384341
"tracing",
43394342
]
43404343

4344+
[[package]]
4345+
name = "rustc_proc_macro"
4346+
version = "0.0.0"
4347+
dependencies = [
4348+
"rustc-literal-escaper",
4349+
]
4350+
43414351
[[package]]
43424352
name = "rustc_query_impl"
43434353
version = "0.0.0"

compiler/rustc_builtin_macros/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ rustc_lint_defs = { path = "../rustc_lint_defs" }
2424
rustc_macros = { path = "../rustc_macros" }
2525
rustc_parse = { path = "../rustc_parse" }
2626
rustc_parse_format = { path = "../rustc_parse_format" }
27+
# We must use the proc_macro version that we will compile proc-macros against,
28+
# not the one from our own sysroot.
29+
rustc_proc_macro = { path = "../rustc_proc_macro" }
2730
rustc_session = { path = "../rustc_session" }
2831
rustc_span = { path = "../rustc_span" }
2932
rustc_target = { path = "../rustc_target" }

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 1 addition & 3 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};
@@ -140,7 +138,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
140138
CoercePointee: coerce_pointee::expand_deriving_coerce_pointee,
141139
}
142140

143-
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
141+
let client = rustc_proc_macro::bridge::client::Client::expand1(rustc_proc_macro::quote);
144142
register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
145143
let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
146144
register(sym::contracts_requires, requires);

compiler/rustc_expand/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ rustc_lexer = { path = "../rustc_lexer" }
2323
rustc_lint_defs = { path = "../rustc_lint_defs" }
2424
rustc_macros = { path = "../rustc_macros" }
2525
rustc_parse = { path = "../rustc_parse" }
26+
# We must use the proc_macro version that we will compile proc-macros against,
27+
# not the one from our own sysroot.
28+
rustc_proc_macro = { path = "../rustc_proc_macro" }
2629
rustc_serialize = { path = "../rustc_serialize" }
2730
rustc_session = { path = "../rustc_session" }
2831
rustc_span = { path = "../rustc_span" }

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 {rustc_ast as ast, rustc_proc_macro as pm};
99

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

compiler/rustc_expand/src/proc_macro_server.rs

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

33
use ast::token::IdentIsRaw;
4-
use pm::bridge::{
5-
DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
6-
};
7-
use pm::{Delimiter, Level};
84
use rustc_ast as ast;
95
use rustc_ast::token;
106
use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream};
@@ -15,6 +11,10 @@ use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan, PResult};
1511
use rustc_parse::lexer::nfc_normalize;
1612
use rustc_parse::parser::Parser;
1713
use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal};
14+
use rustc_proc_macro::bridge::{
15+
DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
16+
};
17+
use rustc_proc_macro::{Delimiter, Level};
1818
use rustc_session::parse::ParseSess;
1919
use rustc_span::def_id::CrateNum;
2020
use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
@@ -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 `rustc_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: rustc_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: rustc_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: rustc_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
@@ -23,6 +23,9 @@ rustc_hir_pretty = { path = "../rustc_hir_pretty" }
2323
rustc_index = { path = "../rustc_index" }
2424
rustc_macros = { path = "../rustc_macros" }
2525
rustc_middle = { path = "../rustc_middle" }
26+
# We must use the proc_macro version that we will compile proc-macros against,
27+
# not the one from our own sysroot.
28+
rustc_proc_macro = { path = "../rustc_proc_macro" }
2629
rustc_serialize = { path = "../rustc_serialize" }
2730
rustc_session = { path = "../rustc_session" }
2831
rustc_span = { path = "../rustc_span" }

compiler/rustc_metadata/src/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::str::FromStr;
77
use std::time::Duration;
88
use std::{cmp, env, iter};
99

10-
use proc_macro::bridge::client::ProcMacro;
1110
use rustc_ast::expand::allocator::{AllocatorKind, alloc_error_handler_name, global_fn_name};
1211
use rustc_ast::{self as ast, *};
1312
use rustc_data_structures::fx::FxHashSet;
@@ -23,6 +22,7 @@ use rustc_hir::definitions::Definitions;
2322
use rustc_index::IndexVec;
2423
use rustc_middle::bug;
2524
use rustc_middle::ty::{TyCtxt, TyCtxtFeed};
25+
use rustc_proc_macro::bridge::client::ProcMacro;
2626
use rustc_session::config::{
2727
self, CrateType, ExtendedTargetModifierInfo, ExternLocation, OptionsTargetModifiers,
2828
TargetModifier,

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;

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::sync::{Arc, OnceLock};
66
use std::{io, iter, mem};
77

88
pub(super) use cstore_impl::provide;
9-
use proc_macro::bridge::client::ProcMacro;
109
use rustc_ast as ast;
1110
use rustc_data_structures::fingerprint::Fingerprint;
1211
use rustc_data_structures::fx::FxIndexMap;
@@ -26,6 +25,7 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2625
use rustc_middle::ty::Visibility;
2726
use rustc_middle::ty::codec::TyDecoder;
2827
use rustc_middle::{bug, implement_ty_decoder};
28+
use rustc_proc_macro::bridge::client::ProcMacro;
2929
use rustc_serialize::opaque::MemDecoder;
3030
use rustc_serialize::{Decodable, Decoder};
3131
use rustc_session::Session;

compiler/rustc_proc_macro/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# We need to use a separate crate including library/proc_macro as opposed to a
2+
# direct path dependency on library/proc_macro because doing the latter will
3+
# cause two copies of libproc_macro.rlib to end up in the sysroot, breaking
4+
# proc-macro crates. In addition it confuses the workspace_members function of
5+
# bootstrap.
6+
7+
[package]
8+
name = "rustc_proc_macro"
9+
version = "0.0.0"
10+
edition = "2024"
11+
12+
[lib]
13+
path = "../../library/proc_macro/src/lib.rs"
14+
test = false
15+
doctest = false
16+
17+
[dependencies]
18+
rustc-literal-escaper = "0.0.2"
19+
20+
[features]
21+
rustc-dep-of-std = []

library/proc_macro/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ std = { path = "../std" }
1010
# loaded from sysroot causing duplicate lang items and other similar errors.
1111
core = { path = "../core" }
1212
rustc-literal-escaper = { version = "0.0.2", features = ["rustc-dep-of-std"] }
13+
14+
[features]
15+
default = ["rustc-dep-of-std"]
16+
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)] // Can't use FxHashMap when compiled as part of the standard library
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)