Skip to content

Commit 5573275

Browse files
committed
Check for occupied niches
1 parent d4c86cf commit 5573275

File tree

37 files changed

+694
-47
lines changed

37 files changed

+694
-47
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/base.rs

+13
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
368368
source_info.span,
369369
);
370370
}
371+
AssertKind::OccupiedNiche { ref found, ref start, ref end } => {
372+
let found = codegen_operand(fx, found).load_scalar(fx);
373+
let start = codegen_operand(fx, start).load_scalar(fx);
374+
let end = codegen_operand(fx, end).load_scalar(fx);
375+
let location = fx.get_caller_location(source_info).load_scalar(fx);
376+
377+
codegen_panic_inner(
378+
fx,
379+
rustc_hir::LangItem::PanicOccupiedNiche,
380+
&[found, start, end, location],
381+
source_info.span,
382+
)
383+
}
371384
_ => {
372385
let msg_str = msg.description();
373386
codegen_panic(fx, msg_str, source_info);

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use rustc_hir::LangItem;
44
use rustc_middle::mir;
5-
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
5+
use rustc_middle::ty::{self, layout::TyAndLayout, GenericArg, Ty, TyCtxt};
66
use rustc_span::Span;
77

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

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

+28-12
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
572572
mergeable_succ: bool,
573573
) -> MergingSucc {
574574
let span = terminator.source_info.span;
575-
let cond = self.codegen_operand(bx, cond).immediate();
575+
let mut cond = self.codegen_operand(bx, cond).immediate();
576576
let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1);
577577

578578
// This case can currently arise only from functions marked
@@ -588,8 +588,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
588588
return helper.funclet_br(self, bx, target, mergeable_succ);
589589
}
590590

591-
// Pass the condition through llvm.expect for branch hinting.
592-
let cond = bx.expect(cond, expected);
591+
if bx.tcx().sess.opts.optimize != OptLevel::No {
592+
// Pass the condition through llvm.expect for branch hinting.
593+
cond = bx.expect(cond, expected);
594+
}
593595

594596
// Create the failure block and the conditional branch to it.
595597
let lltarget = helper.llbb_with_cleanup(self, target);
@@ -608,30 +610,40 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
608610
let location = self.get_caller_location(bx, terminator.source_info).immediate();
609611

610612
// Put together the arguments to the panic entry point.
611-
let (lang_item, args) = match msg {
613+
let (lang_item, args, generic) = match msg {
612614
AssertKind::BoundsCheck { ref len, ref index } => {
613615
let len = self.codegen_operand(bx, len).immediate();
614616
let index = self.codegen_operand(bx, index).immediate();
615617
// It's `fn panic_bounds_check(index: usize, len: usize)`,
616618
// and `#[track_caller]` adds an implicit third argument.
617-
(LangItem::PanicBoundsCheck, vec![index, len, location])
619+
(LangItem::PanicBoundsCheck, vec![index, len, location], None)
618620
}
619621
AssertKind::MisalignedPointerDereference { ref required, ref found } => {
620622
let required = self.codegen_operand(bx, required).immediate();
621623
let found = self.codegen_operand(bx, found).immediate();
622624
// It's `fn panic_misaligned_pointer_dereference(required: usize, found: usize)`,
623625
// and `#[track_caller]` adds an implicit third argument.
624-
(LangItem::PanicMisalignedPointerDereference, vec![required, found, location])
626+
(LangItem::PanicMisalignedPointerDereference, vec![required, found, location], None)
627+
}
628+
AssertKind::OccupiedNiche { ref found, ref start, ref end } => {
629+
let found = self.codegen_operand(bx, found);
630+
let generic_arg = ty::GenericArg::from(found.layout.ty);
631+
let found = found.immediate();
632+
let start = self.codegen_operand(bx, start).immediate();
633+
let end = self.codegen_operand(bx, end).immediate();
634+
// It's `fn panic_occupied_niche<T>(found: T, start: T, end: T)`,
635+
// and `#[track_caller]` adds an implicit fourth argument.
636+
(LangItem::PanicOccupiedNiche, vec![found, start, end, location], Some(generic_arg))
625637
}
626638
_ => {
627639
let msg = bx.const_str(msg.description());
628640
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
629641
// as two arguments, and `#[track_caller]` adds an implicit third argument.
630-
(LangItem::Panic, vec![msg.0, msg.1, location])
642+
(LangItem::Panic, vec![msg.0, msg.1, location], None)
631643
}
632644
};
633645

634-
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), lang_item);
646+
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), lang_item, generic);
635647

636648
// Codegen the actual panic invoke/call.
637649
let merging_succ = helper.do_call(self, bx, fn_abi, llfn, &args, None, unwind, &[], false);
@@ -650,7 +662,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
650662
self.set_debug_loc(bx, terminator.source_info);
651663

652664
// Obtain the panic entry point.
653-
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), reason.lang_item());
665+
let (fn_abi, llfn) = common::build_langcall(bx, Some(span), reason.lang_item(), None);
654666

655667
// Codegen the actual panic invoke/call.
656668
let merging_succ = helper.do_call(
@@ -711,8 +723,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
711723
let msg = bx.const_str(&msg_str);
712724

713725
// Obtain the panic entry point.
714-
let (fn_abi, llfn) =
715-
common::build_langcall(bx, Some(source_info.span), LangItem::PanicNounwind);
726+
let (fn_abi, llfn) = common::build_langcall(
727+
bx,
728+
Some(source_info.span),
729+
LangItem::PanicNounwind,
730+
None,
731+
);
716732

717733
// Codegen the actual panic invoke/call.
718734
helper.do_call(
@@ -1586,7 +1602,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15861602

15871603
self.set_debug_loc(&mut bx, mir::SourceInfo::outermost(self.mir.span));
15881604

1589-
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, reason.lang_item());
1605+
let (fn_abi, fn_ptr) = common::build_langcall(&bx, None, reason.lang_item(), None);
15901606
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
15911607

15921608
let llret = bx.call(fn_ty, None, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());

Diff for: compiler/rustc_const_eval/src/const_eval/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
561561
found: eval_to_int(found)?,
562562
}
563563
}
564+
OccupiedNiche { ref found, ref start, ref end } => OccupiedNiche {
565+
found: eval_to_int(found)?,
566+
start: eval_to_int(start)?,
567+
end: eval_to_int(end)?,
568+
},
564569
};
565570
Err(ConstEvalErrKind::AssertFailure(err).into())
566571
}

Diff for: compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ language_item_table! {
234234
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
235235
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
236236
PanicMisalignedPointerDereference, sym::panic_misaligned_pointer_dereference, panic_misaligned_pointer_dereference_fn, Target::Fn, GenericRequirement::Exact(0);
237+
PanicOccupiedNiche, sym::panic_occupied_niche, panic_occupied_niche_fn, Target::Fn, GenericRequirement::Exact(1);
237238
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
238239
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
239240
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;

Diff for: compiler/rustc_middle/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ middle_assert_gen_resume_after_panic = `gen` fn or block cannot be further itera
1717
middle_assert_misaligned_ptr_deref =
1818
misaligned pointer dereference: address must be a multiple of {$required} but is {$found}
1919
20+
middle_assert_occupied_niche =
21+
occupied niche: {$found} must be in {$start}..={$end}
22+
2023
middle_assert_op_overflow =
2124
attempt to compute `{$left} {$op} {$right}`, which would overflow
2225

Diff for: compiler/rustc_middle/src/mir/syntax.rs

+1
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ pub enum AssertKind<O> {
886886
ResumedAfterReturn(CoroutineKind),
887887
ResumedAfterPanic(CoroutineKind),
888888
MisalignedPointerDereference { required: O, found: O },
889+
OccupiedNiche { found: O, start: O, end: O },
889890
}
890891

891892
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]

Diff for: compiler/rustc_middle/src/mir/terminator.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<O> AssertKind<O> {
157157
"`gen fn` should just keep returning `None` after panicking"
158158
}
159159

160-
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
160+
BoundsCheck { .. } | MisalignedPointerDereference { .. } | OccupiedNiche { .. } => {
161161
bug!("Unexpected AssertKind")
162162
}
163163
}
@@ -220,6 +220,13 @@ impl<O> AssertKind<O> {
220220
"\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}"
221221
)
222222
}
223+
OccupiedNiche { found, start, end } => {
224+
write!(
225+
f,
226+
"\"occupied niche: {{}} must be in {{}}..={{}}\", {:?}, {:?}, {:?}",
227+
found, start, end
228+
)
229+
}
223230
_ => write!(f, "\"{}\"", self.description()),
224231
}
225232
}
@@ -254,8 +261,8 @@ impl<O> AssertKind<O> {
254261
ResumedAfterPanic(CoroutineKind::Coroutine) => {
255262
middle_assert_coroutine_resume_after_panic
256263
}
257-
258264
MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref,
265+
OccupiedNiche { .. } => middle_assert_occupied_niche,
259266
}
260267
}
261268

@@ -292,6 +299,11 @@ impl<O> AssertKind<O> {
292299
add!("required", format!("{required:#?}"));
293300
add!("found", format!("{found:#?}"));
294301
}
302+
OccupiedNiche { found, start, end } => {
303+
add!("found", format!("{found:?}"));
304+
add!("start", format!("{start:?}"));
305+
add!("end", format!("{end:?}"));
306+
}
295307
}
296308
}
297309
}

Diff for: compiler/rustc_middle/src/mir/visit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ macro_rules! make_mir_visitor {
625625
self.visit_operand(required, location);
626626
self.visit_operand(found, location);
627627
}
628+
OccupiedNiche { found, start, end } => {
629+
self.visit_operand(found, location);
630+
self.visit_operand(start, location);
631+
self.visit_operand(end, location);
632+
}
628633
}
629634
}
630635

0 commit comments

Comments
 (0)