From 921a5ef6d7d2f965dab5b1f8b896dfe042db231b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 28 Sep 2024 21:15:18 +0200 Subject: [PATCH 1/3] try to get rid of mir::Const::normalize --- compiler/rustc_middle/src/mir/consts.rs | 12 ------------ compiler/rustc_mir_build/src/thir/cx/expr.rs | 6 ++---- compiler/rustc_mir_transform/src/jump_threading.rs | 4 +--- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 89d4c46016006..4e5d11831a4c4 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -329,18 +329,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] pub fn try_eval_scalar( self, diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index fbd45f59a4fb4..f83c785256892 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -703,8 +703,7 @@ impl<'tcx> Cx<'tcx> { tcx, anon_const.def_id.to_def_id(), ) - .instantiate_identity() - .normalize(tcx, self.param_env); + .instantiate_identity(); let span = tcx.def_span(anon_const.def_id); InlineAsmOperand::Const { value, span } @@ -714,8 +713,7 @@ impl<'tcx> Cx<'tcx> { tcx, anon_const.def_id.to_def_id(), ) - .instantiate_identity() - .normalize(tcx, self.param_env); + .instantiate_identity(); let span = tcx.def_span(anon_const.def_id); InlineAsmOperand::SymFn { value, span } diff --git a/compiler/rustc_mir_transform/src/jump_threading.rs b/compiler/rustc_mir_transform/src/jump_threading.rs index 9d85b5ba5a71c..91fbc91e1e727 100644 --- a/compiler/rustc_mir_transform/src/jump_threading.rs +++ b/compiler/rustc_mir_transform/src/jump_threading.rs @@ -516,9 +516,7 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> { // Avoid handling them, though this could be extended in the future. return; } - let Some(value) = - value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int() - else { + let Some(value) = value.const_.try_eval_scalar_int(self.tcx, self.param_env) else { return; }; let conds = conditions.map(self.arena, |c| Condition { From c55c4c9f9de86034a942909d657e75ffb5674a73 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 28 Sep 2024 21:28:03 +0200 Subject: [PATCH 2/3] tweak Const::identity_unevaluated name and docs --- compiler/rustc_middle/src/mir/consts.rs | 4 +++- compiler/rustc_mir_build/src/thir/cx/expr.rs | 16 ++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 4e5d11831a4c4..b34f5b48b782a 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -221,7 +221,9 @@ pub enum 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>, def_id: DefId, ) -> ty::EarlyBinder<'tcx, Const<'tcx>> { diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index f83c785256892..2ffad0b4834a0 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -699,21 +699,17 @@ impl<'tcx> Cx<'tcx> { } } hir::InlineAsmOperand::Const { ref anon_const } => { - let value = mir::Const::identity_unevaluated( - tcx, - anon_const.def_id.to_def_id(), - ) - .instantiate_identity(); + let value = + mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id()) + .instantiate_identity(); let span = tcx.def_span(anon_const.def_id); InlineAsmOperand::Const { value, span } } hir::InlineAsmOperand::SymFn { ref anon_const } => { - let value = mir::Const::identity_unevaluated( - tcx, - anon_const.def_id.to_def_id(), - ) - .instantiate_identity(); + let value = + mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id()) + .instantiate_identity(); let span = tcx.def_span(anon_const.def_id); InlineAsmOperand::SymFn { value, span } From 7eedb6846cb76f76738c4ba51095a9742cb3ffd3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 29 Sep 2024 08:49:37 +0200 Subject: [PATCH 3/3] adjust test --- tests/ui/asm/const-error.rs | 6 ++++-- tests/ui/asm/const-error.stderr | 9 --------- 2 files changed, 4 insertions(+), 11 deletions(-) delete mode 100644 tests/ui/asm/const-error.stderr diff --git a/tests/ui/asm/const-error.rs b/tests/ui/asm/const-error.rs index 40d0590c33e9d..8c722906284ff 100644 --- a/tests/ui/asm/const-error.rs +++ b/tests/ui/asm/const-error.rs @@ -1,14 +1,16 @@ //@ only-x86_64 //@ 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; fn test() { unsafe { + // No error here, as this does not get monomorphized. asm!("/* {} */", const 1 / 0); - //~^ ERROR evaluation of } } diff --git a/tests/ui/asm/const-error.stderr b/tests/ui/asm/const-error.stderr deleted file mode 100644 index 02e54457e89b9..0000000000000 --- a/tests/ui/asm/const-error.stderr +++ /dev/null @@ -1,9 +0,0 @@ -error[E0080]: evaluation of `test::::{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`.