Skip to content

Commit 39f8046

Browse files
committed
Check for occupied niches
1 parent 85e449a commit 39f8046

38 files changed

+1963
-16
lines changed

Diff for: compiler/rustc_codegen_ssa/src/common.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use rustc_hir::LangItem;
44
use rustc_middle::mir;
55
use rustc_middle::ty::Instance;
6-
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
6+
use rustc_middle::ty::{self, layout::TyAndLayout, GenericArg, Ty, TyCtxt};
77
use rustc_span::Span;
88

99
use crate::base;
@@ -121,10 +121,15 @@ pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
121121
bx: &Bx,
122122
span: Option<Span>,
123123
li: LangItem,
124+
generic: Option<GenericArg<'tcx>>,
124125
) -> (Bx::FnAbiOfResult, Bx::Value, Instance<'tcx>) {
125126
let tcx = bx.tcx();
126127
let def_id = tcx.require_lang_item(li, span);
127-
let instance = ty::Instance::mono(tcx, def_id);
128+
let instance = if let Some(arg) = generic {
129+
ty::Instance::new(def_id, tcx.mk_args(&[arg]))
130+
} else {
131+
ty::Instance::mono(tcx, def_id)
132+
};
128133
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance), instance)
129134
}
130135

Diff for: compiler/rustc_codegen_ssa/src/mir/block.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
682682
}
683683
};
684684

685-
let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), lang_item);
685+
let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), lang_item, None);
686686

687687
// Codegen the actual panic invoke/call.
688688
let merging_succ =
@@ -702,7 +702,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
702702
self.set_debug_loc(bx, terminator.source_info);
703703

704704
// Obtain the panic entry point.
705-
let (fn_abi, llfn, instance) = common::build_langcall(bx, Some(span), reason.lang_item());
705+
let (fn_abi, llfn, instance) =
706+
common::build_langcall(bx, Some(span), reason.lang_item(), None);
706707

707708
// Codegen the actual panic invoke/call.
708709
let merging_succ = helper.do_call(
@@ -764,8 +765,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
764765
let msg = bx.const_str(&msg_str);
765766

766767
// Obtain the panic entry point.
767-
let (fn_abi, llfn, instance) =
768-
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
768+
let (fn_abi, llfn, instance) = common::build_langcall(
769+
bx,
770+
Some(source_info.span),
771+
LangItem::PanicNounwind,
772+
None,
773+
);
769774

770775
// Codegen the actual panic invoke/call.
771776
helper.do_call(
@@ -1284,6 +1289,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12841289
) -> MergingSucc {
12851290
debug!("codegen_terminator: {:?}", terminator);
12861291

1292+
if bx.tcx().may_insert_niche_checks() {
1293+
if let mir::TerminatorKind::Return = terminator.kind {
1294+
let op = mir::Operand::Copy(mir::Place::return_place());
1295+
let ty = op.ty(self.mir, bx.tcx());
1296+
let ty = self.monomorphize(ty);
1297+
if let Some(niche) = bx.layout_of(ty).largest_niche {
1298+
self.codegen_niche_check(bx, op, niche, terminator.source_info);
1299+
}
1300+
}
1301+
}
1302+
12871303
let helper = TerminatorCodegenHelper { bb, terminator };
12881304

12891305
let mergeable_succ = || {
@@ -1552,7 +1568,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15521568
tuple.layout.fields.count()
15531569
}
15541570

1555-
fn get_caller_location(
1571+
pub fn get_caller_location(
15561572
&mut self,
15571573
bx: &mut Bx,
15581574
source_info: mir::SourceInfo,
@@ -1693,12 +1709,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16931709

16941710
self.set_debug_loc(&mut bx, mir::SourceInfo::outermost(self.mir.span));
16951711

1696-
let (fn_abi, fn_ptr, instance) = common::build_langcall(&bx, None, reason.lang_item());
1712+
let (fn_abi, fn_ptr, instance) =
1713+
common::build_langcall(&bx, None, reason.lang_item(), None);
16971714
if is_call_from_compiler_builtins_to_upstream_monomorphization(bx.tcx(), instance) {
16981715
bx.abort();
16991716
} else {
17001717
let fn_ty = bx.fn_decl_backend_type(fn_abi);
1701-
17021718
let llret = bx.call(fn_ty, None, Some(fn_abi), fn_ptr, &[], funclet.as_ref());
17031719
bx.apply_attrs_to_cleanup_callsite(llret);
17041720
}

Diff for: compiler/rustc_codegen_ssa/src/mir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod coverageinfo;
1818
pub mod debuginfo;
1919
mod intrinsic;
2020
mod locals;
21+
mod niche_check;
2122
pub mod operand;
2223
pub mod place;
2324
mod rvalue;

0 commit comments

Comments
 (0)