Skip to content

[Don't merge][test] mir niche checks #116414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ jobs:
- name: x86_64-gnu-tools
env:
DEPLOY_TOOLSTATES_JSON: toolstates-linux.json
NO_DOWNLOAD_CI_LLVM: 1
CUSTOM_MINGW: 1
os: ubuntu-20.04-8core-32gb
- name: dist-x86_64-apple
env:
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
shallow = true
[submodule "src/llvm-project"]
path = src/llvm-project
url = https://github.com/rust-lang/llvm-project.git
url = https://github.com/DianQK/llvm-project.git
branch = rustc/17.0-2023-09-19
shallow = true
[submodule "src/doc/embedded-book"]
Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,29 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
source_info.span,
);
}
AssertKind::OccupiedNiche {
ref found,
ref start,
ref end,
ref type_name,
ref offset,
ref niche_ty,
} => {
let found = codegen_operand(fx, found).load_scalar(fx);
let start = codegen_operand(fx, start).load_scalar(fx);
let end = codegen_operand(fx, end).load_scalar(fx);
let type_name = fx.anonymous_str(type_name);
let offset = codegen_operand(fx, offset).load_scalar(fx);
let niche_ty = fx.anonymous_str(niche_ty);
let location = fx.get_caller_location(source_info).load_scalar(fx);

codegen_panic_inner(
fx,
rustc_hir::LangItem::PanicOccupiedNiche,
&[found, start, end, type_name, offset, niche_ty, location],
source_info.span,
)
}
_ => {
let msg_str = msg.description();
codegen_panic(fx, msg_str, source_info);
Expand Down
29 changes: 29 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,35 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// and `#[track_caller]` adds an implicit third argument.
(LangItem::PanicMisalignedPointerDereference, vec![required, found, location])
}
AssertKind::OccupiedNiche {
ref found,
ref start,
ref end,
ref type_name,
ref offset,
ref niche_ty,
} => {
let found = self.codegen_operand(bx, found).immediate();
let start = self.codegen_operand(bx, start).immediate();
let end = self.codegen_operand(bx, end).immediate();
let type_name = bx.const_str(type_name);
let offset = self.codegen_operand(bx, offset).immediate();
let niche_ty = bx.const_str(niche_ty);
(
LangItem::PanicOccupiedNiche,
vec![
found,
start,
end,
type_name.0,
type_name.1,
offset,
niche_ty.0,
niche_ty.1,
location,
],
)
}
_ => {
let msg = bx.const_str(msg.description());
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
found: eval_to_int(found)?,
}
}
OccupiedNiche {
ref found,
ref start,
ref end,
ref type_name,
ref offset,
ref niche_ty,
} => OccupiedNiche {
found: eval_to_int(found)?,
start: eval_to_int(start)?,
end: eval_to_int(end)?,
type_name: type_name.clone(),
offset: eval_to_int(offset)?,
niche_ty: niche_ty.clone(),
},
};
Err(ConstEvalErrKind::AssertFailure(err).into())
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ language_item_table! {
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
PanicMisalignedPointerDereference, sym::panic_misaligned_pointer_dereference, panic_misaligned_pointer_dereference_fn, Target::Fn, GenericRequirement::Exact(0);
PanicOccupiedNiche, sym::panic_occupied_niche, panic_occupied_niche_fn, Target::Fn, GenericRequirement::Exact(0);
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ middle_assert_generator_resume_after_return = generator resumed after completion
middle_assert_misaligned_ptr_deref =
misaligned pointer dereference: address must be a multiple of {$required} but is {$found}

middle_assert_occupied_niche =
occupied niche: {$found} must be in {$start}..={$end}

middle_assert_op_overflow =
attempt to compute `{$left} {$op} {$right}`, which would overflow

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ pub enum AssertKind<O> {
ResumedAfterReturn(GeneratorKind),
ResumedAfterPanic(GeneratorKind),
MisalignedPointerDereference { required: O, found: O },
OccupiedNiche { found: O, start: O, end: O, type_name: String, offset: O, niche_ty: String },
}

#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
Expand Down
19 changes: 17 additions & 2 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<O> AssertKind<O> {
ResumedAfterReturn(GeneratorKind::Async(_)) => "`async fn` resumed after completion",
ResumedAfterPanic(GeneratorKind::Gen) => "generator resumed after panicking",
ResumedAfterPanic(GeneratorKind::Async(_)) => "`async fn` resumed after panicking",
BoundsCheck { .. } | MisalignedPointerDereference { .. } => {
BoundsCheck { .. } | MisalignedPointerDereference { .. } | OccupiedNiche { .. } => {
bug!("Unexpected AssertKind")
}
}
Expand Down Expand Up @@ -206,6 +206,13 @@ impl<O> AssertKind<O> {
"\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}"
)
}
OccupiedNiche { found, start, end, type_name, offset, niche_ty } => {
write!(
f,
"\"occupied niche: {{}} must be in {{}}..={{}} in a {{}} at offset {{}} with type {{}}\" {:?} {:?} {:?} {:?} {:?} {:?}",
found, start, end, type_name, offset, niche_ty
)
}
_ => write!(f, "\"{}\"", self.description()),
}
}
Expand All @@ -232,8 +239,8 @@ impl<O> AssertKind<O> {
ResumedAfterReturn(GeneratorKind::Gen) => middle_assert_generator_resume_after_return,
ResumedAfterPanic(GeneratorKind::Async(_)) => middle_assert_async_resume_after_panic,
ResumedAfterPanic(GeneratorKind::Gen) => middle_assert_generator_resume_after_panic,

MisalignedPointerDereference { .. } => middle_assert_misaligned_ptr_deref,
OccupiedNiche { .. } => middle_assert_occupied_niche,
}
}

Expand Down Expand Up @@ -270,6 +277,14 @@ impl<O> AssertKind<O> {
add!("required", format!("{required:#?}"));
add!("found", format!("{found:#?}"));
}
OccupiedNiche { found, start, end, type_name, offset, niche_ty } => {
add!("found", format!("{found:?}"));
add!("start", format!("{start:?}"));
add!("end", format!("{end:?}"));
add!("type_name", format!("{type_name}"));
add!("offset", format!("{offset:?}"));
add!("niche_ty", format!("{niche_ty}"));
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ macro_rules! make_mir_visitor {
self.visit_operand(required, location);
self.visit_operand(found, location);
}
OccupiedNiche { found, start, end, type_name: _, offset, niche_ty: _ } => {
self.visit_operand(found, location);
self.visit_operand(start, location);
self.visit_operand(end, location);
self.visit_operand(offset, location);
}
}
}

Expand Down
Loading