summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_monomorphize/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /compiler/rustc_monomorphize/src
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_monomorphize/src')
-rw-r--r--compiler/rustc_monomorphize/src/collector.rs51
-rw-r--r--compiler/rustc_monomorphize/src/errors.rs4
-rw-r--r--compiler/rustc_monomorphize/src/lib.rs4
-rw-r--r--compiler/rustc_monomorphize/src/partitioning.rs20
-rw-r--r--compiler/rustc_monomorphize/src/polymorphize.rs4
5 files changed, 53 insertions, 30 deletions
diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs
index 65bdcf107..a68bfcd06 100644
--- a/compiler/rustc_monomorphize/src/collector.rs
+++ b/compiler/rustc_monomorphize/src/collector.rs
@@ -176,6 +176,7 @@ use rustc_middle::mir::visit::Visitor as MirVisitor;
use rustc_middle::mir::{self, Location};
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
+use rustc_middle::ty::layout::ValidityRequirement;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{
self, AssocKind, GenericParamDefKind, Instance, InstanceDef, Ty, TyCtxt, TypeFoldable,
@@ -370,7 +371,7 @@ fn collect_items_rec<'tcx>(
// current step of mono items collection.
//
// FIXME: don't rely on global state, instead bubble up errors. Note: this is very hard to do.
- let error_count = tcx.sess.diagnostic().err_count();
+ let error_count = tcx.sess.dcx().err_count();
match starting_item.node {
MonoItem::Static(def_id) => {
@@ -385,8 +386,8 @@ fn collect_items_rec<'tcx>(
recursion_depth_reset = None;
if let Ok(alloc) = tcx.eval_static_initializer(def_id) {
- for &id in alloc.inner().provenance().ptrs().values() {
- collect_alloc(tcx, id, &mut used_items);
+ for &prov in alloc.inner().provenance().ptrs().values() {
+ collect_alloc(tcx, prov.alloc_id(), &mut used_items);
}
}
@@ -458,7 +459,7 @@ fn collect_items_rec<'tcx>(
// Check for PMEs and emit a diagnostic if one happened. To try to show relevant edges of the
// mono item graph.
- if tcx.sess.diagnostic().err_count() > error_count
+ if tcx.sess.dcx().err_count() > error_count
&& starting_item.node.is_generic_fn(tcx)
&& starting_item.node.is_user_defined()
{
@@ -740,7 +741,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
) => {
let fn_ty = operand.ty(self.body, self.tcx);
let fn_ty = self.monomorphize(fn_ty);
- visit_fn_use(self.tcx, fn_ty, false, span, &mut self.output);
+ visit_fn_use(self.tcx, fn_ty, false, span, self.output);
}
mir::Rvalue::Cast(
mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)),
@@ -816,7 +817,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
let callee_ty = func.ty(self.body, tcx);
let callee_ty = self.monomorphize(callee_ty);
self.check_fn_args_move_size(callee_ty, args, location);
- visit_fn_use(self.tcx, callee_ty, true, source, &mut self.output)
+ visit_fn_use(self.tcx, callee_ty, true, source, self.output)
}
mir::TerminatorKind::Drop { ref place, .. } => {
let ty = place.ty(self.body, self.tcx).ty;
@@ -828,7 +829,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
match *op {
mir::InlineAsmOperand::SymFn { ref value } => {
let fn_ty = self.monomorphize(value.const_.ty());
- visit_fn_use(self.tcx, fn_ty, false, source, &mut self.output);
+ visit_fn_use(self.tcx, fn_ty, false, source, self.output);
}
mir::InlineAsmOperand::SymStatic { def_id } => {
let instance = Instance::mono(self.tcx, def_id);
@@ -844,6 +845,9 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
mir::TerminatorKind::Assert { ref msg, .. } => {
let lang_item = match &**msg {
mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck,
+ mir::AssertKind::MisalignedPointerDereference { .. } => {
+ LangItem::PanicMisalignedPointerDereference
+ }
_ => LangItem::Panic,
};
push_mono_lang_item(self, lang_item);
@@ -920,6 +924,21 @@ fn visit_instance_use<'tcx>(
return;
}
+ // The intrinsics assert_inhabited, assert_zero_valid, and assert_mem_uninitialized_valid will
+ // be lowered in codegen to nothing or a call to panic_nounwind. So if we encounter any
+ // of those intrinsics, we need to include a mono item for panic_nounwind, else we may try to
+ // codegen a call to that function without generating code for the function itself.
+ if let ty::InstanceDef::Intrinsic(def_id) = instance.def {
+ let name = tcx.item_name(def_id);
+ if let Some(_requirement) = ValidityRequirement::from_intrinsic(name) {
+ let def_id = tcx.lang_items().get(LangItem::PanicNounwind).unwrap();
+ let panic_instance = Instance::mono(tcx, def_id);
+ if should_codegen_locally(tcx, &panic_instance) {
+ output.push(create_fn_mono_item(tcx, panic_instance, source));
+ }
+ }
+ }
+
match instance.def {
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(_) => {
if !is_direct_call {
@@ -1118,7 +1137,7 @@ fn create_mono_items_for_vtable_methods<'tcx>(
) {
assert!(!trait_ty.has_escaping_bound_vars() && !impl_ty.has_escaping_bound_vars());
- if let ty::Dynamic(ref trait_ty, ..) = trait_ty.kind() {
+ if let ty::Dynamic(trait_ty, ..) = trait_ty.kind() {
if let Some(principal) = trait_ty.principal() {
let poly_trait_ref = principal.with_self_ty(tcx, impl_ty);
assert!(!poly_trait_ref.has_escaping_bound_vars());
@@ -1191,7 +1210,7 @@ impl<'v> RootCollector<'_, 'v> {
// but even just declaring them must collect the items they refer to
if let Ok(val) = self.tcx.const_eval_poly(id.owner_id.to_def_id()) {
- collect_const_value(self.tcx, val, &mut self.output);
+ collect_const_value(self.tcx, val, self.output);
}
}
DefKind::Impl { .. } => {
@@ -1363,9 +1382,9 @@ fn collect_alloc<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut MonoIt
}
GlobalAlloc::Memory(alloc) => {
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
- for &inner in alloc.inner().provenance().ptrs().values() {
+ for &prov in alloc.inner().provenance().ptrs().values() {
rustc_data_structures::stack::ensure_sufficient_stack(|| {
- collect_alloc(tcx, inner, output);
+ collect_alloc(tcx, prov.alloc_id(), output);
});
}
}
@@ -1422,14 +1441,14 @@ fn collect_used_items<'tcx>(
// and abort compilation if any of them errors.
MirUsedCollector {
tcx,
- body: &body,
+ body: body,
output,
instance,
move_size_spans: vec![],
visiting_call_terminator: false,
skip_move_check_fns: None,
}
- .visit_body(&body);
+ .visit_body(body);
}
#[instrument(skip(tcx, output), level = "debug")]
@@ -1440,12 +1459,12 @@ fn collect_const_value<'tcx>(
) {
match value {
mir::ConstValue::Scalar(Scalar::Ptr(ptr, _size)) => {
- collect_alloc(tcx, ptr.provenance, output)
+ collect_alloc(tcx, ptr.provenance.alloc_id(), output)
}
mir::ConstValue::Indirect { alloc_id, .. } => collect_alloc(tcx, alloc_id, output),
mir::ConstValue::Slice { data, meta: _ } => {
- for &id in data.inner().provenance().ptrs().values() {
- collect_alloc(tcx, id, output);
+ for &prov in data.inner().provenance().ptrs().values() {
+ collect_alloc(tcx, prov.alloc_id(), output);
}
}
_ => {}
diff --git a/compiler/rustc_monomorphize/src/errors.rs b/compiler/rustc_monomorphize/src/errors.rs
index d242a7bae..247b22455 100644
--- a/compiler/rustc_monomorphize/src/errors.rs
+++ b/compiler/rustc_monomorphize/src/errors.rs
@@ -51,9 +51,9 @@ impl IntoDiagnostic<'_> for UnusedGenericParamsHint {
#[track_caller]
fn into_diagnostic(
self,
- handler: &'_ rustc_errors::Handler,
+ dcx: &'_ rustc_errors::DiagCtxt,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
- let mut diag = handler.struct_err(fluent::monomorphize_unused_generic_params);
+ let mut diag = dcx.struct_err(fluent::monomorphize_unused_generic_params);
diag.set_span(self.span);
for (span, name) in self.param_spans.into_iter().zip(self.param_names) {
// FIXME: I can figure out how to do a label with a fluent string with a fixed message,
diff --git a/compiler/rustc_monomorphize/src/lib.rs b/compiler/rustc_monomorphize/src/lib.rs
index 5f05020ac..2f5f2d15c 100644
--- a/compiler/rustc_monomorphize/src/lib.rs
+++ b/compiler/rustc_monomorphize/src/lib.rs
@@ -10,8 +10,6 @@ extern crate tracing;
#[macro_use]
extern crate rustc_middle;
-use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
-use rustc_fluent_macro::fluent_messages;
use rustc_hir::lang_items::LangItem;
use rustc_middle::query::{Providers, TyCtxtAt};
use rustc_middle::traits;
@@ -24,7 +22,7 @@ mod partitioning;
mod polymorphize;
mod util;
-fluent_messages! { "../messages.ftl" }
+rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
fn custom_coerce_unsize_info<'tcx>(
tcx: TyCtxtAt<'tcx>,
diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs
index 4009e2892..d47d3e5e7 100644
--- a/compiler/rustc_monomorphize/src/partitioning.rs
+++ b/compiler/rustc_monomorphize/src/partitioning.rs
@@ -212,11 +212,17 @@ where
let cgu_name_cache = &mut FxHashMap::default();
for mono_item in mono_items {
- // Handle only root items directly here. Inlined items are handled at
- // the bottom of the loop based on reachability.
+ // Handle only root (GloballyShared) items directly here. Inlined (LocalCopy) items
+ // are handled at the bottom of the loop based on reachability, with one exception.
+ // The #[lang = "start"] item is the program entrypoint, so there are no calls to it in MIR.
+ // So even if its mode is LocalCopy, we need to treat it like a root.
match mono_item.instantiation_mode(cx.tcx) {
InstantiationMode::GloballyShared { .. } => {}
- InstantiationMode::LocalCopy => continue,
+ InstantiationMode::LocalCopy => {
+ if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
+ continue;
+ }
+ }
}
let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);
@@ -436,12 +442,12 @@ fn merge_codegen_units<'tcx>(
for cgu in codegen_units.iter_mut() {
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
- cgu.set_name(Symbol::intern(&new_cgu_name));
+ cgu.set_name(Symbol::intern(new_cgu_name));
} else {
// If we don't require CGU names to be human-readable,
// we use a fixed length hash of the composite CGU name
// instead.
- let new_cgu_name = CodegenUnit::mangle_name(&new_cgu_name);
+ let new_cgu_name = CodegenUnit::mangle_name(new_cgu_name);
cgu.set_name(Symbol::intern(&new_cgu_name));
}
}
@@ -883,7 +889,7 @@ fn mono_item_visibility<'tcx>(
}
fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
- if !tcx.sess.target.default_hidden_visibility {
+ if !tcx.sess.default_hidden_visibility() {
return Visibility::Default;
}
@@ -1140,7 +1146,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
// Output monomorphization stats per def_id
if let SwitchWithOptPath::Enabled(ref path) = tcx.sess.opts.unstable_opts.dump_mono_stats {
if let Err(err) =
- dump_mono_items_stats(tcx, &codegen_units, path, tcx.crate_name(LOCAL_CRATE))
+ dump_mono_items_stats(tcx, codegen_units, path, tcx.crate_name(LOCAL_CRATE))
{
tcx.sess.emit_fatal(CouldntDumpMonoStats { error: err.to_string() });
}
diff --git a/compiler/rustc_monomorphize/src/polymorphize.rs b/compiler/rustc_monomorphize/src/polymorphize.rs
index a3b35eab4..91abbb216 100644
--- a/compiler/rustc_monomorphize/src/polymorphize.rs
+++ b/compiler/rustc_monomorphize/src/polymorphize.rs
@@ -131,7 +131,7 @@ fn mark_used_by_default_parameters<'tcx>(
unused_parameters: &mut UnusedGenericParams,
) {
match tcx.def_kind(def_id) {
- DefKind::Closure | DefKind::Coroutine => {
+ DefKind::Closure => {
for param in &generics.params {
debug!(?param, "(closure/gen)");
unused_parameters.mark_used(param.index);
@@ -248,7 +248,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
if local == Local::from_usize(1) {
let def_kind = self.tcx.def_kind(self.def_id);
- if matches!(def_kind, DefKind::Closure | DefKind::Coroutine) {
+ if matches!(def_kind, DefKind::Closure) {
// Skip visiting the closure/coroutine that is currently being processed. This only
// happens because the first argument to the closure is a reference to itself and
// that will call `visit_args`, resulting in each generic parameter captured being