Skip to content

Inject compiler_builtins during crate resolution #113634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions compiler/rustc_builtin_macros/src/standard_library_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_expand::expand::ExpansionConfig;
use rustc_session::Session;
use rustc_span::edition::Edition::*;
use rustc_span::hygiene::AstPass;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::DUMMY_SP;
use thin_vec::thin_vec;

Expand All @@ -17,17 +17,12 @@ pub fn inject(
let orig_num_items = krate.items.len();
let edition = sess.parse_sess.edition;

// the first name in this list is the crate name of the crate with the prelude
let names: &[Symbol] = if attr::contains_name(pre_configured_attrs, sym::no_core) {
let name = if attr::contains_name(pre_configured_attrs, sym::no_core) {
return 0;
} else if attr::contains_name(pre_configured_attrs, sym::no_std) {
if attr::contains_name(pre_configured_attrs, sym::compiler_builtins) {
&[sym::core]
} else {
&[sym::core, sym::compiler_builtins]
}
sym::core
} else {
&[sym::std]
sym::std
};

let expn_id = resolver.expansion_for_ast_pass(
Expand All @@ -42,27 +37,17 @@ pub fn inject(
let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
let cx = ExtCtxt::new(sess, ecfg, resolver, None);

// .rev() to preserve ordering above in combination with insert(0, ...)
for &name in names.iter().rev() {
let ident = if edition >= Edition2018 {
Ident::new(name, span)
} else {
Ident::new(name, call_site)
};
krate.items.insert(
0,
cx.item(
span,
ident,
thin_vec![cx.attr_word(sym::macro_use, span)],
ast::ItemKind::ExternCrate(None),
),
);
}

// The crates have been injected, the assumption is that the first one is
// the one with the prelude.
let name = names[0];
let ident =
if edition >= Edition2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if edition >= Edition2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
Ident::new(name, if edition >= Edition2018 { span } else { call_site });

krate.items.insert(
0,
cx.item(
span,
ident,
thin_vec![cx.attr_word(sym::macro_use, span)],
ast::ItemKind::ExternCrate(None),
),
);

let root = (edition == Edition2015).then_some(kw::PathRoot);

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_metadata/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ metadata_no_transitive_needs_dep =
metadata_non_ascii_name =
cannot load a crate with a non-ascii name `{$crate_name}`

metadata_not_compiler_builtins =
the crate `{$crate_name}` is not the compiler builtins crate

metadata_not_profiler_runtime =
the crate `{$crate_name}` is not a profiler runtime

Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,27 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime());
}

fn inject_compiler_builtins(&mut self, krate: &ast::Crate) {
// Don't inject compiler_builtins for core and compiler_builtins.
if attr::contains_name(&krate.attrs, sym::no_core)
|| attr::contains_name(&krate.attrs, sym::compiler_builtins)
{
return;
}

info!("loading compiler_builtins");

let Some(cnum) = self.resolve_crate(sym::compiler_builtins, DUMMY_SP, CrateDepKind::Implicit) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let Some(cnum) = self.resolve_crate(sym::compiler_builtins, DUMMY_SP, CrateDepKind::Implicit) else {
let Some(cnum) = self.resolve_crate(sym::compiler_builtins, DUMMY_SP, CrateDepKind::Explicit) else {

extern crate item from code will result in a call like this (see fn process_extern_crate).
So this change should make the behavior closer to whatever we've been doing previously, maybe it will fix the issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't help.

return;
};
let data = self.cstore.get_crate_data(cnum);

// Sanity check the loaded crate to ensure it is indeed compiler_builtins
if !data.is_compiler_builtins() {
self.sess.emit_err(errors::NotCompilerBuiltins { crate_name: sym::compiler_builtins });
}
}

fn inject_profiler_runtime(&mut self, krate: &ast::Crate) {
if self.sess.opts.unstable_opts.no_profiler_runtime
|| !(self.sess.instrument_coverage()
Expand Down Expand Up @@ -968,6 +989,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {

pub fn postprocess(&mut self, krate: &ast::Crate) {
self.inject_forced_externs();
self.inject_compiler_builtins(krate);
self.inject_profiler_runtime(krate);
self.inject_allocator_crate(krate);
self.inject_panic_runtime(krate);
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ pub struct NoPanicStrategy {
pub strategy: PanicStrategy,
}

#[derive(Diagnostic)]
#[diag(metadata_not_compiler_builtins)]
pub struct NotCompilerBuiltins {
pub crate_name: Symbol,
}

#[derive(Diagnostic)]
#[diag(metadata_profiler_builtins_needs_core)]
pub struct ProfilerBuiltinsNeedsCore;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,10 @@ impl CrateMetadata {
self.root.panic_runtime
}

pub(crate) fn is_compiler_builtins(&self) -> bool {
self.root.compiler_builtins
}

pub(crate) fn is_profiler_runtime(&self) -> bool {
self.root.profiler_runtime
}
Expand Down