Skip to content

Commit da9e8f4

Browse files
committed
only add builtins if not already included
Don't inject compiler_builtins in the AST
1 parent b15f5f0 commit da9e8f4

File tree

10 files changed

+78
-44
lines changed

10 files changed

+78
-44
lines changed

compiler/rustc_builtin_macros/src/standard_library_imports.rs

+13-36
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ pub fn inject(
1919
let edition = sess.psess.edition;
2020

2121
// the first name in this list is the crate name of the crate with the prelude
22-
let names: &[Symbol] = if attr::contains_name(pre_configured_attrs, sym::no_core) {
22+
let name: Symbol = if attr::contains_name(pre_configured_attrs, sym::no_core) {
2323
return 0;
2424
} else if attr::contains_name(pre_configured_attrs, sym::no_std) {
25-
if attr::contains_name(pre_configured_attrs, sym::compiler_builtins) {
26-
&[sym::core]
27-
} else {
28-
&[sym::core, sym::compiler_builtins]
29-
}
25+
sym::core
3026
} else {
31-
&[sym::std]
27+
sym::std
3228
};
3329

3430
let expn_id = resolver.expansion_for_ast_pass(
@@ -43,37 +39,18 @@ pub fn inject(
4339
let ecfg = ExpansionConfig::default("std_lib_injection".to_string(), features);
4440
let cx = ExtCtxt::new(sess, ecfg, resolver, None);
4541

46-
// .rev() to preserve ordering above in combination with insert(0, ...)
47-
for &name in names.iter().rev() {
48-
let ident_span = if edition >= Edition2018 { span } else { call_site };
49-
let item = if name == sym::compiler_builtins {
50-
// compiler_builtins is a private implementation detail. We only
51-
// need to insert it into the crate graph for linking and should not
52-
// expose any of its public API.
53-
//
54-
// FIXME(#113634) We should inject this during post-processing like
55-
// we do for the panic runtime, profiler runtime, etc.
56-
cx.item(
57-
span,
58-
Ident::new(kw::Underscore, ident_span),
59-
thin_vec![],
60-
ast::ItemKind::ExternCrate(Some(name)),
61-
)
62-
} else {
63-
cx.item(
64-
span,
65-
Ident::new(name, ident_span),
66-
thin_vec![cx.attr_word(sym::macro_use, span)],
67-
ast::ItemKind::ExternCrate(None),
68-
)
69-
};
70-
krate.items.insert(0, item);
71-
}
42+
let ident_span = if edition >= Edition2018 { span } else { call_site };
7243

73-
// The crates have been injected, the assumption is that the first one is
74-
// the one with the prelude.
75-
let name = names[0];
44+
let item = cx.item(
45+
span,
46+
Ident::new(name, ident_span),
47+
thin_vec![cx.attr_word(sym::macro_use, span)],
48+
ast::ItemKind::ExternCrate(None),
49+
);
50+
51+
krate.items.insert(0, item);
7652

53+
// Inject the relevant crate's prelude.
7754
let root = (edition == Edition2015).then_some(kw::PathRoot);
7855

7956
let import_path = root

compiler/rustc_metadata/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ metadata_crate_dep_rustc_driver =
4747
metadata_crate_location_unknown_type =
4848
extern location for {$crate_name} is of an unknown type: {$path}
4949
50+
metadata_crate_not_compiler_builtins =
51+
the crate `{$crate_name}` resolved as `compiler_builtins` but is not `#![compiler_builtins]`
52+
5053
metadata_crate_not_panic_runtime =
5154
the crate `{$crate_name}` is not a panic runtime
5255

compiler/rustc_metadata/src/creader.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
685685

686686
match result {
687687
(LoadResult::Previous(cnum), None) => {
688-
info!("library for `{}` was loaded previously", name);
688+
info!("library for `{}` was loaded previously, cnum {cnum}", name);
689689
// When `private_dep` is none, it indicates the directly dependent crate. If it is
690690
// not specified by `--extern` on command line parameters, it may be
691691
// `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
@@ -1031,6 +1031,39 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
10311031
}
10321032
}
10331033

1034+
fn inject_compiler_builtins(&mut self, krate: &ast::Crate) {
1035+
if attr::contains_name(&krate.attrs, sym::compiler_builtins)
1036+
|| attr::contains_name(&krate.attrs, sym::no_core)
1037+
{
1038+
// `compiler_builtins` does not get extern builtins, nor do `#![no_core]` crates
1039+
info!("`compiler_builtins` unneeded");
1040+
return;
1041+
}
1042+
1043+
for (cnum, cmeta) in self.cstore.iter_crate_data() {
1044+
if cmeta.is_compiler_builtins() {
1045+
info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
1046+
return;
1047+
}
1048+
}
1049+
1050+
let Ok(cnum) = self.maybe_resolve_crate(
1051+
sym::compiler_builtins,
1052+
CrateDepKind::Implicit,
1053+
CrateOrigin::Injected,
1054+
) else {
1055+
info!("`compiler_builtins` not resolved");
1056+
return;
1057+
};
1058+
1059+
let cmeta = self.cstore.get_crate_data(cnum);
1060+
1061+
// Sanity check the loaded crate to ensure it is indeed compiler_builtins
1062+
if !cmeta.is_compiler_builtins() {
1063+
self.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
1064+
}
1065+
}
1066+
10341067
fn inject_dependency_if(
10351068
&mut self,
10361069
krate: CrateNum,
@@ -1140,6 +1173,8 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
11401173
}
11411174

11421175
pub fn postprocess(&mut self, krate: &ast::Crate) {
1176+
info!("POSTPROCESS");
1177+
self.inject_compiler_builtins(krate);
11431178
self.inject_forced_externs();
11441179
self.inject_profiler_runtime();
11451180
self.inject_allocator_crate(krate);
@@ -1171,6 +1206,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
11711206
}
11721207
None => item.ident.name,
11731208
};
1209+
11741210
let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
11751211
CrateDepKind::MacrosOnly
11761212
} else {

compiler/rustc_metadata/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ pub struct CrateNotPanicRuntime {
332332
pub crate_name: Symbol,
333333
}
334334

335+
#[derive(Diagnostic)]
336+
#[diag(metadata_crate_not_compiler_builtins)]
337+
pub struct CrateNotCompilerBuiltins {
338+
pub crate_name: Symbol,
339+
}
340+
335341
#[derive(Diagnostic)]
336342
#[diag(metadata_no_panic_strategy)]
337343
pub struct NoPanicStrategy {

compiler/rustc_metadata/src/locator.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub(crate) struct CrateLocator<'a> {
260260
crate_rejections: CrateRejections,
261261
}
262262

263-
#[derive(Clone)]
263+
#[derive(Clone, Debug)]
264264
pub(crate) struct CratePaths {
265265
pub(crate) name: Symbol,
266266
source: CrateSource,
@@ -272,7 +272,7 @@ impl CratePaths {
272272
}
273273
}
274274

275-
#[derive(Copy, Clone, PartialEq)]
275+
#[derive(Copy, Clone, Debug, PartialEq)]
276276
pub(crate) enum CrateFlavor {
277277
Rlib,
278278
Rmeta,
@@ -893,13 +893,13 @@ fn get_flavor_from_path(path: &Path) -> CrateFlavor {
893893

894894
// ------------------------------------------ Error reporting -------------------------------------
895895

896-
#[derive(Clone)]
896+
#[derive(Clone, Debug)]
897897
struct CrateMismatch {
898898
path: PathBuf,
899899
got: String,
900900
}
901901

902-
#[derive(Clone, Default)]
902+
#[derive(Clone, Debug, Default)]
903903
struct CrateRejections {
904904
via_hash: Vec<CrateMismatch>,
905905
via_triple: Vec<CrateMismatch>,
@@ -912,6 +912,7 @@ struct CrateRejections {
912912
/// Candidate rejection reasons collected during crate search.
913913
/// If no candidate is accepted, then these reasons are presented to the user,
914914
/// otherwise they are ignored.
915+
#[derive(Debug)]
915916
pub(crate) struct CombinedLocatorError {
916917
crate_name: Symbol,
917918
dep_root: Option<CratePaths>,
@@ -921,6 +922,7 @@ pub(crate) struct CombinedLocatorError {
921922
crate_rejections: CrateRejections,
922923
}
923924

925+
#[derive(Debug)]
924926
pub(crate) enum CrateError {
925927
NonAsciiName(Symbol),
926928
ExternLocationNotExist(Symbol, PathBuf),

compiler/rustc_metadata/src/rmeta/decoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,10 @@ impl CrateMetadata {
19281928
self.root.profiler_runtime
19291929
}
19301930

1931+
pub(crate) fn is_compiler_builtins(&self) -> bool {
1932+
self.root.compiler_builtins
1933+
}
1934+
19311935
pub(crate) fn needs_allocator(&self) -> bool {
19321936
self.root.needs_allocator
19331937
}

compiler/rustc_middle/src/middle/stability.rs

+1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ impl<'tcx> TyCtxt<'tcx> {
408408
inspecting def_id={:?} span={:?} of stability={:?}",
409409
def_id, span, stability
410410
);
411+
tracing::info!("stability table: {:#?}", self.stability());
411412

412413
match stability {
413414
Some(Stability {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
error: `#[panic_handler]` function required, but not found
2+
3+
error: unwinding panics are not supported without std
4+
|
5+
= help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
6+
= note: since the core library is usually precompiled with panic="unwind", rebuilding your crate with panic="abort" may not be enough to fix the problem
7+
18
error: requires `sized` lang_item
29

3-
error: aborting due to 1 previous error
10+
error: aborting due to 3 previous errors
411

tests/ui/proc-macro/meta-macro-hygiene.stdout

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Respanned: TokenStream [Ident { ident: "$crate", span: $DIR/auxiliary/make-macro
2020
use core /* 0#1 */::prelude /* 0#1 */::rust_2018 /* 0#1 */::*;
2121
#[macro_use /* 0#1 */]
2222
extern crate core /* 0#1 */;
23-
extern crate compiler_builtins /* NNN */ as _ /* 0#1 */;
2423
// Don't load unnecessary hygiene information from std
2524
extern crate std /* 0#0 */;
2625

tests/ui/proc-macro/nonterminal-token-hygiene.stdout

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3939
use ::core /* 0#1 */::prelude /* 0#1 */::rust_2015 /* 0#1 */::*;
4040
#[macro_use /* 0#1 */]
4141
extern crate core /* 0#2 */;
42-
extern crate compiler_builtins /* NNN */ as _ /* 0#2 */;
4342
// Don't load unnecessary hygiene information from std
4443
extern crate std /* 0#0 */;
4544

0 commit comments

Comments
 (0)