Skip to content

Commit 5cf889e

Browse files
committedDec 11, 2023
Fix cases where std accidentally relied on inline(never)
1 parent 2b399b5 commit 5cf889e

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed
 

‎compiler/rustc_mir_transform/src/cross_crate_inline.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2222
return false;
2323
}
2424

25+
// This just reproduces the logic from Instance::requires_inline.
26+
match tcx.def_kind(def_id) {
27+
DefKind::Ctor(..) | DefKind::Closure => return true,
28+
DefKind::Fn | DefKind::AssocFn => {}
29+
_ => return false,
30+
}
31+
32+
// From this point on, it is should be valid to return true or false.
33+
if tcx.sess.opts.unstable_opts.cross_crate_inline_threshold == InliningThreshold::Always {
34+
return true;
35+
}
36+
2537
// Obey source annotations first; this is important because it means we can use
2638
// #[inline(never)] to force code generation.
2739
match codegen_fn_attrs.inline {
@@ -30,13 +42,6 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
3042
_ => {}
3143
}
3244

33-
// This just reproduces the logic from Instance::requires_inline.
34-
match tcx.def_kind(def_id) {
35-
DefKind::Ctor(..) | DefKind::Closure => return true,
36-
DefKind::Fn | DefKind::AssocFn => {}
37-
_ => return false,
38-
}
39-
4045
// Don't do any inference when incremental compilation is enabled; the additional inlining that
4146
// inference permits also creates more work for small edits.
4247
if tcx.sess.opts.incremental.is_some() {

‎compiler/rustc_monomorphize/src/collector.rs

+12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ use rustc_middle::mir::visit::Visitor as MirVisitor;
176176
use rustc_middle::mir::{self, Location};
177177
use rustc_middle::query::TyCtxtAt;
178178
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
179+
use rustc_middle::ty::layout::ValidityRequirement;
179180
use rustc_middle::ty::print::with_no_trimmed_paths;
180181
use rustc_middle::ty::{
181182
self, AssocKind, GenericParamDefKind, Instance, InstanceDef, Ty, TyCtxt, TypeFoldable,
@@ -923,6 +924,17 @@ fn visit_instance_use<'tcx>(
923924
return;
924925
}
925926

927+
if let ty::InstanceDef::Intrinsic(def_id) = instance.def {
928+
let name = tcx.item_name(def_id);
929+
if let Some(_requirement) = ValidityRequirement::from_intrinsic(name) {
930+
let def_id = tcx.lang_items().get(LangItem::PanicNounwind).unwrap();
931+
let panic_instance = Instance::mono(tcx, def_id);
932+
if should_codegen_locally(tcx, &panic_instance) {
933+
output.push(create_fn_mono_item(tcx, panic_instance, source));
934+
}
935+
}
936+
}
937+
926938
match instance.def {
927939
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(_) => {
928940
if !is_direct_call {

‎compiler/rustc_monomorphize/src/partitioning.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ where
216216
// the bottom of the loop based on reachability.
217217
match mono_item.instantiation_mode(cx.tcx) {
218218
InstantiationMode::GloballyShared { .. } => {}
219-
InstantiationMode::LocalCopy => continue,
219+
InstantiationMode::LocalCopy => {
220+
if Some(mono_item.def_id()) != cx.tcx.lang_items().start_fn() {
221+
continue;
222+
}
223+
}
220224
}
221225

222226
let characteristic_def_id = characteristic_def_id_of_mono_item(cx.tcx, mono_item);

‎library/std/src/rt.rs

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ fn lang_start_internal(
155155
}
156156

157157
#[cfg(not(test))]
158-
#[inline(never)]
159158
#[lang = "start"]
160159
fn lang_start<T: crate::process::Termination + 'static>(
161160
main: fn() -> T,

0 commit comments

Comments
 (0)