1
Fork 0

Rollup merge of #130990 - RalfJung:mir-const-normalize, r=compiler-errors

try to get rid of mir::Const::normalize

It was easy to make this compile, let's see if anything breaks...

r? `@compiler-errors`
This commit is contained in:
Matthias Krüger 2024-09-29 20:17:37 +02:00 committed by GitHub
commit a0ae32d6a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 14 additions and 39 deletions

View file

@ -221,7 +221,9 @@ pub enum Const<'tcx> {
} }
impl<'tcx> Const<'tcx> { impl<'tcx> Const<'tcx> {
pub fn identity_unevaluated( /// Creates an unevaluated const from a `DefId` for a const item.
/// The binders of the const item still need to be instantiated.
pub fn from_unevaluated(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
def_id: DefId, def_id: DefId,
) -> ty::EarlyBinder<'tcx, Const<'tcx>> { ) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
@ -329,18 +331,6 @@ impl<'tcx> Const<'tcx> {
} }
} }
/// Normalizes the constant to a value or an error if possible.
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, DUMMY_SP) {
Ok(val) => Self::Val(val, self.ty()),
Err(ErrorHandled::Reported(guar, _span)) => {
Self::Ty(Ty::new_error(tcx, guar.into()), ty::Const::new_error(tcx, guar.into()))
}
Err(ErrorHandled::TooGeneric(_span)) => self,
}
}
#[inline] #[inline]
pub fn try_eval_scalar( pub fn try_eval_scalar(
self, self,

View file

@ -699,23 +699,17 @@ impl<'tcx> Cx<'tcx> {
} }
} }
hir::InlineAsmOperand::Const { ref anon_const } => { hir::InlineAsmOperand::Const { ref anon_const } => {
let value = mir::Const::identity_unevaluated( let value =
tcx, mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
anon_const.def_id.to_def_id(), .instantiate_identity();
)
.instantiate_identity()
.normalize(tcx, self.param_env);
let span = tcx.def_span(anon_const.def_id); let span = tcx.def_span(anon_const.def_id);
InlineAsmOperand::Const { value, span } InlineAsmOperand::Const { value, span }
} }
hir::InlineAsmOperand::SymFn { ref anon_const } => { hir::InlineAsmOperand::SymFn { ref anon_const } => {
let value = mir::Const::identity_unevaluated( let value =
tcx, mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
anon_const.def_id.to_def_id(), .instantiate_identity();
)
.instantiate_identity()
.normalize(tcx, self.param_env);
let span = tcx.def_span(anon_const.def_id); let span = tcx.def_span(anon_const.def_id);
InlineAsmOperand::SymFn { value, span } InlineAsmOperand::SymFn { value, span }

View file

@ -516,9 +516,7 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
// Avoid handling them, though this could be extended in the future. // Avoid handling them, though this could be extended in the future.
return; return;
} }
let Some(value) = let Some(value) = value.const_.try_eval_scalar_int(self.tcx, self.param_env) else {
value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int()
else {
return; return;
}; };
let conds = conditions.map(self.arena, |c| Condition { let conds = conditions.map(self.arena, |c| Condition {

View file

@ -1,14 +1,16 @@
//@ only-x86_64 //@ only-x86_64
//@ needs-asm-support //@ needs-asm-support
//@ check-pass
// Test to make sure that we emit const errors eagerly for inline asm // Test to make sure that we emit const errors late for inline asm,
// which is consistent with inline const blocks.
use std::arch::asm; use std::arch::asm;
fn test<T>() { fn test<T>() {
unsafe { unsafe {
// No error here, as this does not get monomorphized.
asm!("/* {} */", const 1 / 0); asm!("/* {} */", const 1 / 0);
//~^ ERROR evaluation of
} }
} }

View file

@ -1,9 +0,0 @@
error[E0080]: evaluation of `test::<T>::{constant#0}` failed
--> $DIR/const-error.rs:10:32
|
LL | asm!("/* {} */", const 1 / 0);
| ^^^^^ attempt to divide `1_i32` by zero
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0080`.