Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn inject(
for &name in names.iter().rev() {
let ident_span = if edition >= Edition2018 { span } else { call_site };
let item = if name == sym::compiler_builtins {
eprintln!("INJECTING BUILTINS");
// compiler_builtins is a private implementation detail. We only
// need to insert it into the crate graph for linking and should not
// expose any of its public API.
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 @@ -47,6 +47,9 @@ metadata_crate_dep_rustc_driver =
metadata_crate_location_unknown_type =
extern location for {$crate_name} is of an unknown type: {$path}
metadata_crate_not_compiler_builtins =
the crate `{$crate_name}` resolved as `compiler_builtins` but is not `#![compiler_builtins]`
metadata_crate_not_panic_runtime =
the crate `{$crate_name}` is not a panic runtime
Expand Down
43 changes: 42 additions & 1 deletion compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {

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

fn inject_compiler_builtins(&mut self, krate: &ast::Crate) {
if attr::contains_name(&krate.attrs, sym::compiler_builtins)
|| attr::contains_name(&krate.attrs, sym::no_core)
{
// `compiler_builtins` does not get extern builtins, nor do `#![no_core]` crates
info!("`compiler_builtins` unneeded");
return;
}

for (cnum, cmeta) in self.cstore.iter_crate_data() {
if cmeta.is_compiler_builtins() {
info!("`compiler_builtins` already exists (cnum = {cnum}); skipping injection");
return;
}
}

let Ok(cnum) = self.maybe_resolve_crate(
sym::compiler_builtins,
CrateDepKind::Implicit,
CrateOrigin::Injected,
) else {
info!("`compiler_builtins` not resolved");
return;
};

let cmeta = self.cstore.get_crate_data(cnum);

// Sanity check the loaded crate to ensure it is indeed compiler_builtins
if !cmeta.is_compiler_builtins() {
self.dcx().emit_err(errors::CrateNotCompilerBuiltins { crate_name: cmeta.name() });
}
}

fn inject_dependency_if(
&mut self,
krate: CrateNum,
Expand Down Expand Up @@ -1140,6 +1173,8 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
}

pub fn postprocess(&mut self, krate: &ast::Crate) {
info!("POSTPROCESS");
self.inject_compiler_builtins(krate);
self.inject_forced_externs();
self.inject_profiler_runtime();
self.inject_allocator_crate(krate);
Expand Down Expand Up @@ -1171,12 +1206,18 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
}
None => item.ident.name,
};

let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
CrateDepKind::MacrosOnly
} else {
CrateDepKind::Explicit
};

if name == sym::compiler_builtins {
info!("BUILTINS DETECTED dep_kind {dep_kind:?}");
return None;
Copy link
Member

Choose a reason for hiding this comment

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

Why are you ignoring explicit extern crate compiler_builtins;? I think this is the reason of the CI failure as the implicitly injected compiler_builtins dependency isn't checked for stability, yet explicit mentions should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's dumb. I did this as a more minimal step than removing it from standard_library_imports for experimentation, but you're right that this would catch the explicit extern as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that was it. I updated #136226 with the changes here, so will close this one.

}

let cnum = self.resolve_crate(name, item.span, dep_kind, CrateOrigin::AstExtern)?;

let path_len = definitions.def_path(def_id).data.len();
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 CrateNotPanicRuntime {
pub crate_name: Symbol,
}

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

#[derive(Diagnostic)]
#[diag(metadata_no_panic_strategy)]
pub struct NoPanicStrategy {
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub(crate) struct CrateLocator<'a> {
crate_rejections: CrateRejections,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct CratePaths {
pub(crate) name: Symbol,
source: CrateSource,
Expand All @@ -272,7 +272,7 @@ impl CratePaths {
}
}

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum CrateFlavor {
Rlib,
Rmeta,
Expand Down Expand Up @@ -893,13 +893,13 @@ fn get_flavor_from_path(path: &Path) -> CrateFlavor {

// ------------------------------------------ Error reporting -------------------------------------

#[derive(Clone)]
#[derive(Clone, Debug)]
struct CrateMismatch {
path: PathBuf,
got: String,
}

#[derive(Clone, Default)]
#[derive(Clone, Debug, Default)]
struct CrateRejections {
via_hash: Vec<CrateMismatch>,
via_triple: Vec<CrateMismatch>,
Expand All @@ -912,6 +912,7 @@ struct CrateRejections {
/// Candidate rejection reasons collected during crate search.
/// If no candidate is accepted, then these reasons are presented to the user,
/// otherwise they are ignored.
#[derive(Debug)]
pub(crate) struct CombinedLocatorError {
crate_name: Symbol,
dep_root: Option<CratePaths>,
Expand All @@ -921,6 +922,7 @@ pub(crate) struct CombinedLocatorError {
crate_rejections: CrateRejections,
}

#[derive(Debug)]
pub(crate) enum CrateError {
NonAsciiName(Symbol),
ExternLocationNotExist(Symbol, PathBuf),
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 @@ -1928,6 +1928,10 @@ impl CrateMetadata {
self.root.profiler_runtime
}

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

pub(crate) fn needs_allocator(&self) -> bool {
self.root.needs_allocator
}
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/proc-macro/issue-59191-replace-root-with-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
error: `#[panic_handler]` function required, but not found

error: unwinding panics are not supported without std
|
= help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
= 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

error: requires `sized` lang_item

error: aborting due to 1 previous error
error: aborting due to 3 previous errors

Loading