Skip to content
Open
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
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,15 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
source_info.span,
)
}
AssertKind::InvalidEnumConstruction(source) => {
AssertKind::InvalidEnumConstruction(ty, source) => {
let ty = codegen_operand(fx, ty).load_scalar(fx);
let source = codegen_operand(fx, source).load_scalar(fx);
let location = fx.get_caller_location(source_info).load_scalar(fx);

codegen_panic_inner(
fx,
rustc_hir::LangItem::PanicInvalidEnumConstruction,
&[source, location],
&[ty, source, location],
*unwind,
source_info.span,
)
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::{CachedLlbb, FunctionCx, LocalRef};
use crate::base::{self, is_call_from_compiler_builtins_to_upstream_monomorphization};
use crate::common::{self, IntPredicate};
use crate::errors::CompilerBuiltinsCannotCall;
use crate::mir::operand::OperandValue;
use crate::traits::*;
use crate::{MemFlags, meth};

Expand Down Expand Up @@ -761,11 +762,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// `#[track_caller]` adds an implicit argument.
(LangItem::PanicNullPointerDereference, vec![location])
}
AssertKind::InvalidEnumConstruction(source) => {
AssertKind::InvalidEnumConstruction(ty, source) => {
let source = self.codegen_operand(bx, source).immediate();
let OperandValue::Pair(ptr, len) = self.codegen_operand(bx, ty).val else {
panic!("What the hell?");
};
let ptr = ptr;
let len = len;
// It's `fn panic_invalid_enum_construction(source: u128)`,
// `#[track_caller]` adds an implicit argument.
(LangItem::PanicInvalidEnumConstruction, vec![source, location])
(LangItem::PanicInvalidEnumConstruction, vec![ptr, len, source, location])
}
_ => {
// It's `pub fn panic_...()` and `#[track_caller]` adds an implicit argument.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,9 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
found: eval_to_int(found)?,
},
NullPointerDereference => NullPointerDereference,
InvalidEnumConstruction(source) => InvalidEnumConstruction(eval_to_int(source)?),
InvalidEnumConstruction(ty, source) => {
InvalidEnumConstruction(eval_to_int(ty)?, eval_to_int(source)?)
}
};
Err(ConstEvalErrKind::AssertFailure(err)).into()
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ pub enum AssertKind<O> {
ResumedAfterDrop(CoroutineKind),
MisalignedPointerDereference { required: O, found: O },
NullPointerDereference,
InvalidEnumConstruction(O),
InvalidEnumConstruction(O, O),
}

#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<O> AssertKind<O> {
LangItem::PanicGenFnNonePanic
}
NullPointerDereference => LangItem::PanicNullPointerDereference,
InvalidEnumConstruction(_) => LangItem::PanicInvalidEnumConstruction,
InvalidEnumConstruction(..) => LangItem::PanicInvalidEnumConstruction,
ResumedAfterDrop(CoroutineKind::Coroutine(_)) => LangItem::PanicCoroutineResumedDrop,
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
LangItem::PanicAsyncFnResumedDrop
Expand Down Expand Up @@ -285,8 +285,11 @@ impl<O> AssertKind<O> {
)
}
NullPointerDereference => write!(f, "\"null pointer dereference occurred\""),
InvalidEnumConstruction(source) => {
write!(f, "\"trying to construct an enum from an invalid value {{}}\", {source:?}")
InvalidEnumConstruction(ty, source) => {
write!(
f,
"\"trying to construct an enum from an invalid value {{}}; type {{}}\", {source:?}, {ty:?}"
)
}
ResumedAfterReturn(CoroutineKind::Coroutine(_)) => {
write!(f, "\"coroutine resumed after completion\"")
Expand Down Expand Up @@ -379,7 +382,7 @@ impl<O> AssertKind<O> {
msg!("coroutine resumed after panicking")
}
NullPointerDereference => msg!("null pointer dereference occurred"),
InvalidEnumConstruction(_) => {
InvalidEnumConstruction(..) => {
msg!("trying to construct an enum from an invalid value `{$source}`")
}
ResumedAfterDrop(CoroutineKind::Desugared(CoroutineDesugaring::Async, _)) => {
Expand Down Expand Up @@ -437,8 +440,9 @@ impl<O> AssertKind<O> {
add!("required", format!("{required:#?}"));
add!("found", format!("{found:#?}"));
}
InvalidEnumConstruction(source) => {
InvalidEnumConstruction(ty, source) => {
add!("source", format!("{source:#?}"));
add!("ty", format!("{ty:#?}"));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ macro_rules! make_mir_visitor {
self.visit_operand(l, location);
self.visit_operand(r, location);
}
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) | InvalidEnumConstruction(op) => {
OverflowNeg(op) | DivisionByZero(op) | RemainderByZero(op) => {
self.visit_operand(op, location);
}
InvalidEnumConstruction(ty, op) => {
self.visit_operand(ty, location);
self.visit_operand(op, location);
}
ResumedAfterReturn(_) | ResumedAfterPanic(_) | NullPointerDereference | ResumedAfterDrop(_) => {
Expand Down
Loading
Loading