Skip to content

Commit c5cdc49

Browse files
committed
Auto merge of rust-lang#104862 - saethlin:mir-niche-checks, r=<try>
Check for occupied niches Implementation of rust-lang/compiler-team#624 r? `@ghost`
2 parents 7db4a89 + e332f83 commit c5cdc49

File tree

34 files changed

+733
-15
lines changed

34 files changed

+733
-15
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+23
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,29 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
368368
source_info.span,
369369
);
370370
}
371+
AssertKind::OccupiedNiche {
372+
ref found,
373+
ref start,
374+
ref end,
375+
ref type_name,
376+
ref offset,
377+
ref niche_ty,
378+
} => {
379+
let found = codegen_operand(fx, found).load_scalar(fx);
380+
let start = codegen_operand(fx, start).load_scalar(fx);
381+
let end = codegen_operand(fx, end).load_scalar(fx);
382+
let type_name = fx.anonymous_str(type_name);
383+
let offset = codegen_operand(fx, offset).load_scalar(fx);
384+
let niche_ty = fx.anonymous_str(niche_ty);
385+
let location = fx.get_caller_location(source_info).load_scalar(fx);
386+
387+
codegen_panic_inner(
388+
fx,
389+
rustc_hir::LangItem::PanicOccupiedNiche,
390+
&[found, start, end, type_name, offset, niche_ty, location],
391+
source_info.span,
392+
)
393+
}
371394
_ => {
372395
let msg_str = msg.description();
373396
codegen_panic(fx, msg_str, source_info);

compiler/rustc_codegen_ssa/src/mir/block.rs

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

compiler/rustc_const_eval/src/const_eval/machine.rs

+15
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
542542
found: eval_to_int(found)?,
543543
}
544544
}
545+
OccupiedNiche {
546+
ref found,
547+
ref start,
548+
ref end,
549+
ref type_name,
550+
ref offset,
551+
ref niche_ty,
552+
} => OccupiedNiche {
553+
found: eval_to_int(found)?,
554+
start: eval_to_int(start)?,
555+
end: eval_to_int(end)?,
556+
type_name: type_name.clone(),
557+
offset: eval_to_int(offset)?,
558+
niche_ty: niche_ty.clone(),
559+
},
545560
};
546561
Err(ConstEvalErrKind::AssertFailure(err).into())
547562
}

compiler/rustc_hir/src/lang_items.rs

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

compiler/rustc_middle/messages.ftl

+3
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

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

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

compiler/rustc_middle/src/mir/terminator.rs

+17-2
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

+6
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ macro_rules! make_mir_visitor {
625625
self.visit_operand(required, location);
626626
self.visit_operand(found, location);
627627
}
628+
OccupiedNiche { found, start, end, type_name: _, offset, niche_ty: _ } => {
629+
self.visit_operand(found, location);
630+
self.visit_operand(start, location);
631+
self.visit_operand(end, location);
632+
self.visit_operand(offset, location);
633+
}
628634
}
629635
}
630636

0 commit comments

Comments
 (0)