Skip to content

Commit cf8d85e

Browse files
saethlindianqk
authored andcommitted
Check for occupied niches
(cherry picked from commit d22a3fa)
1 parent 6e09cff commit cf8d85e

File tree

34 files changed

+733
-15
lines changed

34 files changed

+733
-15
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,29 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
380380
source_info.span,
381381
);
382382
}
383+
AssertKind::OccupiedNiche {
384+
ref found,
385+
ref start,
386+
ref end,
387+
ref type_name,
388+
ref offset,
389+
ref niche_ty,
390+
} => {
391+
let found = codegen_operand(fx, found).load_scalar(fx);
392+
let start = codegen_operand(fx, start).load_scalar(fx);
393+
let end = codegen_operand(fx, end).load_scalar(fx);
394+
let type_name = fx.anonymous_str(type_name);
395+
let offset = codegen_operand(fx, offset).load_scalar(fx);
396+
let niche_ty = fx.anonymous_str(niche_ty);
397+
let location = fx.get_caller_location(source_info).load_scalar(fx);
398+
399+
codegen_panic_inner(
400+
fx,
401+
rustc_hir::LangItem::PanicOccupiedNiche,
402+
&[found, start, end, type_name, offset, niche_ty, location],
403+
source_info.span,
404+
)
405+
}
383406
_ => {
384407
let msg_str = msg.description();
385408
codegen_panic(fx, msg_str, source_info);

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,35 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
628628
// and `#[track_caller]` adds an implicit third argument.
629629
(LangItem::PanicMisalignedPointerDereference, vec![required, found, location])
630630
}
631+
AssertKind::OccupiedNiche {
632+
ref found,
633+
ref start,
634+
ref end,
635+
ref type_name,
636+
ref offset,
637+
ref niche_ty,
638+
} => {
639+
let found = self.codegen_operand(bx, found).immediate();
640+
let start = self.codegen_operand(bx, start).immediate();
641+
let end = self.codegen_operand(bx, end).immediate();
642+
let type_name = bx.const_str(type_name);
643+
let offset = self.codegen_operand(bx, offset).immediate();
644+
let niche_ty = bx.const_str(niche_ty);
645+
(
646+
LangItem::PanicOccupiedNiche,
647+
vec![
648+
found,
649+
start,
650+
end,
651+
type_name.0,
652+
type_name.1,
653+
offset,
654+
niche_ty.0,
655+
niche_ty.1,
656+
location,
657+
],
658+
)
659+
}
631660
_ => {
632661
let msg = bx.const_str(msg.description());
633662
// It's `pub fn panic(expr: &str)`, with the wide reference being passed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
587587
found: eval_to_int(found)?,
588588
}
589589
}
590+
OccupiedNiche {
591+
ref found,
592+
ref start,
593+
ref end,
594+
ref type_name,
595+
ref offset,
596+
ref niche_ty,
597+
} => OccupiedNiche {
598+
found: eval_to_int(found)?,
599+
start: eval_to_int(start)?,
600+
end: eval_to_int(end)?,
601+
type_name: type_name.clone(),
602+
offset: eval_to_int(offset)?,
603+
niche_ty: niche_ty.clone(),
604+
},
590605
};
591606
Err(ConstEvalErrKind::AssertFailure(err).into())
592607
}

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
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(0);
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;

compiler/rustc_middle/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ middle_assert_generator_resume_after_return = generator resumed after completion
1515
middle_assert_misaligned_ptr_deref =
1616
misaligned pointer dereference: address must be a multiple of {$required} but is {$found}
1717
18+
middle_assert_occupied_niche =
19+
occupied niche: {$found} must be in {$start}..={$end}
20+
1821
middle_assert_op_overflow =
1922
attempt to compute `{$left} {$op} {$right}`, which would overflow
2023

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ pub enum AssertKind<O> {
881881
ResumedAfterReturn(GeneratorKind),
882882
ResumedAfterPanic(GeneratorKind),
883883
MisalignedPointerDereference { required: O, found: O },
884+
OccupiedNiche { found: O, start: O, end: O, type_name: String, offset: O, niche_ty: String },
884885
}
885886

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

compiler/rustc_middle/src/mir/terminator.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<O> AssertKind<O> {
143143
ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
144144
ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
145145
ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
146-
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
146+
BoundsCheck { .. } | MisalignedPointerDereference { .. } | OccupiedNiche { .. } => {
147147
bug!("Unexpected AssertKind")
148148
}
149149
}
@@ -206,6 +206,13 @@ impl<O> AssertKind<O> {
206206
"\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}"
207207
)
208208
}
209+
OccupiedNiche { found, start, end, type_name, offset, niche_ty } => {
210+
write!(
211+
f,
212+
"\"occupied niche: {{}} must be in {{}}..={{}} in a {{}} at offset {{}} with type {{}}\" {:?} {:?} {:?} {:?} {:?} {:?}",
213+
found, start, end, type_name, offset, niche_ty
214+
)
215+
}
209216
_ => write!(f, "\"{}\"", self.description()),
210217
}
211218
}
@@ -232,8 +239,8 @@ impl<O> AssertKind<O> {
232239
ResumedAfterReturn(GeneratorKind::Gen) => middle_assert_generator_resume_after_return,
233240
ResumedAfterPanic(GeneratorKind::Async(_)) => middle_assert_async_resume_after_panic,
234241
ResumedAfterPanic(GeneratorKind::Gen) => middle_assert_generator_resume_after_panic,
235-
236242
MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref,
243+
OccupiedNiche { .. } => middle_assert_occupied_niche,
237244
}
238245
}
239246

@@ -270,6 +277,14 @@ impl<O> AssertKind<O> {
270277
add!("required", format!("{required:#?}"));
271278
add!("found", format!("{found:#?}"));
272279
}
280+
OccupiedNiche { found, start, end, type_name, offset, niche_ty } => {
281+
add!("found", format!("{found:?}"));
282+
add!("start", format!("{start:?}"));
283+
add!("end", format!("{end:?}"));
284+
add!("type_name", format!("{type_name}"));
285+
add!("offset", format!("{offset:?}"));
286+
add!("niche_ty", format!("{niche_ty}"));
287+
}
273288
}
274289
}
275290
}

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ macro_rules! make_mir_visitor {
623623
self.visit_operand(required, location);
624624
self.visit_operand(found, location);
625625
}
626+
OccupiedNiche { found, start, end, type_name: _, offset, niche_ty: _ } => {
627+
self.visit_operand(found, location);
628+
self.visit_operand(start, location);
629+
self.visit_operand(end, location);
630+
self.visit_operand(offset, location);
631+
}
626632
}
627633
}
628634

0 commit comments

Comments
 (0)