Skip to content

Commit db062de

Browse files
committed
Auto merge of #90406 - nbdd0121:panic, r=cjgillot
Collect `panic/panic_bounds_check` during monomorphization This would prevent link time errors if these functions are `#[inline]` (e.g. when `panic_immediate_abort` is used). Fix #90405 Fix rust-lang/cargo#10019 `@rustbot` label: T-compiler A-codegen
2 parents e9b0d99 + 4d619d9 commit db062de

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

compiler/rustc_hir/src/lang_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ language_item_table! {
281281
// in the sense that a crate is not required to have it defined to use it, but a final product
282282
// is required to define it somewhere. Additionally, there are restrictions on crates that use
283283
// a weak lang item, but do not have it defined.
284-
Panic, sym::panic, panic_fn, Target::Fn, GenericRequirement::None;
284+
Panic, sym::panic, panic_fn, Target::Fn, GenericRequirement::Exact(0);
285285
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn, GenericRequirement::None;
286286
PanicDisplay, sym::panic_display, panic_display, Target::Fn, GenericRequirement::None;
287287
PanicStr, sym::panic_str, panic_str, Target::Fn, GenericRequirement::None;
288288
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
289-
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::None;
289+
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
290290
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
291291
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
292292
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;

compiler/rustc_monomorphize/src/collector.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,22 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
806806
}
807807
}
808808
}
809+
mir::TerminatorKind::Assert { ref msg, .. } => {
810+
let lang_item = match msg {
811+
mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck,
812+
_ => LangItem::Panic,
813+
};
814+
let instance = Instance::mono(tcx, tcx.require_lang_item(lang_item, Some(source)));
815+
if should_codegen_locally(tcx, &instance) {
816+
self.output.push(create_fn_mono_item(tcx, instance, source));
817+
}
818+
}
809819
mir::TerminatorKind::Goto { .. }
810820
| mir::TerminatorKind::SwitchInt { .. }
811821
| mir::TerminatorKind::Resume
812822
| mir::TerminatorKind::Abort
813823
| mir::TerminatorKind::Return
814-
| mir::TerminatorKind::Unreachable
815-
| mir::TerminatorKind::Assert { .. } => {}
824+
| mir::TerminatorKind::Unreachable => {}
816825
mir::TerminatorKind::GeneratorDrop
817826
| mir::TerminatorKind::Yield { .. }
818827
| mir::TerminatorKind::FalseEdge { .. }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// compile-flags:-Zprint-mono-items=lazy
2+
3+
// rust-lang/rust#90405
4+
// Ensure implicit panic calls are collected
5+
6+
#![feature(lang_items)]
7+
#![feature(no_core)]
8+
#![crate_type = "lib"]
9+
#![no_core]
10+
#![no_std]
11+
12+
#[lang = "panic_location"]
13+
struct Location<'a> {
14+
_file: &'a str,
15+
_line: u32,
16+
_col: u32,
17+
}
18+
19+
#[lang = "panic"]
20+
#[inline]
21+
#[track_caller]
22+
fn panic(_: &'static str) -> ! {
23+
loop {}
24+
}
25+
26+
#[lang = "sized"]
27+
trait Sized {}
28+
29+
#[lang = "copy"]
30+
trait Copy {}
31+
32+
#[lang = "freeze"]
33+
trait Freeze {}
34+
35+
impl Copy for i32 {}
36+
37+
#[lang = "div"]
38+
trait Div<Rhs = Self> {
39+
type Output;
40+
fn div(self, rhs: Rhs) -> Self::Output;
41+
}
42+
43+
impl Div for i32 {
44+
type Output = i32;
45+
fn div(self, rhs: i32) -> i32 {
46+
self / rhs
47+
}
48+
}
49+
50+
#[allow(unconditional_panic)]
51+
pub fn foo() {
52+
// This implicitly generates a panic call.
53+
let _ = 1 / 0;
54+
}
55+
56+
//~ MONO_ITEM fn foo
57+
//~ MONO_ITEM fn <i32 as Div>::div
58+
//~ MONO_ITEM fn panic

0 commit comments

Comments
 (0)