Auto merge of #79621 - usbalbin:constier_maybe_uninit, r=RalfJung
Constier maybe uninit I was playing around trying to make `[T; N]::zip()` in #79451 be `const fn`. One of the things I bumped into was `MaybeUninit::assume_init`. Is there any reason for the intrinsic `assert_inhabited<T>()` and therefore `MaybeUninit::assume_init` not being `const`? --- I have as best as I could tried to follow the instruction in [library/core/src/intrinsics.rs](https://github.com/rust-lang/rust/blob/master/library/core/src/intrinsics.rs#L11). I have no idea what I am doing but it seems to compile after some slight changes after the copy paste. Is this anywhere near how this should be done? Also any ideas for name of the feature gate? I guess `const_maybe_assume_init` is quite misleading since I have added some more methods. Should I add test? If so what should be tested?
This commit is contained in:
commit
39b841dfe3
12 changed files with 87 additions and 14 deletions
|
@ -20,6 +20,7 @@ pub enum ConstEvalErrKind {
|
|||
ModifiedGlobal,
|
||||
AssertFailure(AssertKind<ConstInt>),
|
||||
Panic { msg: Symbol, line: u32, col: u32, file: Symbol },
|
||||
Abort(String),
|
||||
}
|
||||
|
||||
// The errors become `MachineStop` with plain strings when being raised.
|
||||
|
@ -46,6 +47,7 @@ impl fmt::Display for ConstEvalErrKind {
|
|||
Panic { msg, line, col, file } => {
|
||||
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)
|
||||
}
|
||||
Abort(ref msg) => write!(f, "{}", msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -384,6 +384,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
|
|||
Err(ConstEvalErrKind::AssertFailure(err).into())
|
||||
}
|
||||
|
||||
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tcx, !> {
|
||||
Err(ConstEvalErrKind::Abort(msg).into())
|
||||
}
|
||||
|
||||
fn ptr_to_int(_mem: &Memory<'mir, 'tcx, Self>, _ptr: Pointer) -> InterpResult<'tcx, u64> {
|
||||
Err(ConstEvalErrKind::NeedsRfc("pointer-to-integer cast".to_string()).into())
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
None => match intrinsic_name {
|
||||
sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
|
||||
sym::unreachable => throw_ub!(Unreachable),
|
||||
sym::abort => M::abort(self)?,
|
||||
sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
|
||||
// Unsupported diverging intrinsic.
|
||||
_ => return Ok(false),
|
||||
},
|
||||
|
@ -407,6 +407,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
sym::transmute => {
|
||||
self.copy_op_transmute(args[0], dest)?;
|
||||
}
|
||||
sym::assert_inhabited => {
|
||||
let ty = instance.substs.type_at(0);
|
||||
let layout = self.layout_of(ty)?;
|
||||
|
||||
if layout.abi.is_uninhabited() {
|
||||
// The run-time intrinsic panics just to get a good backtrace; here we abort
|
||||
// since there is no problem showing a backtrace even for aborts.
|
||||
M::abort(self, format!("attempted to instantiate uninhabited type `{}`", ty))?;
|
||||
}
|
||||
}
|
||||
sym::simd_insert => {
|
||||
let index = u64::from(self.read_scalar(args[1])?.to_u32()?);
|
||||
let elem = args[2];
|
||||
|
|
|
@ -176,7 +176,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
|
|||
) -> InterpResult<'tcx>;
|
||||
|
||||
/// Called to evaluate `Abort` MIR terminator.
|
||||
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx, !> {
|
||||
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
|
||||
throw_unsup_format!("aborting execution is not supported")
|
||||
}
|
||||
|
||||
|
|
|
@ -110,7 +110,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||
}
|
||||
|
||||
Abort => {
|
||||
M::abort(self)?;
|
||||
M::abort(self, "the program aborted execution".to_owned())?;
|
||||
}
|
||||
|
||||
// When we encounter Resume, we've finished unwinding
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue