Skip to content

Commit

Permalink
Rollup merge of rust-lang#130990 - RalfJung:mir-const-normalize, r=co…
Browse files Browse the repository at this point in the history
…mpiler-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`
  • Loading branch information
matthiaskrgr authored Sep 29, 2024
2 parents a061e56 + 7eedb68 commit a0ae32d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 39 deletions.
16 changes: 3 additions & 13 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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]
pub fn try_eval_scalar(
self,
Expand Down
18 changes: 6 additions & 12 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,23 +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()
.normalize(tcx, self.param_env);
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()
.normalize(tcx, self.param_env);
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 }
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/asm/const-error.rs
Original file line number Diff line number Diff line change
@@ -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<T>() {
unsafe {
// No error here, as this does not get monomorphized.
asm!("/* {} */", const 1 / 0);
//~^ ERROR evaluation of
}
}

Expand Down
9 changes: 0 additions & 9 deletions tests/ui/asm/const-error.stderr

This file was deleted.

0 comments on commit a0ae32d

Please sign in to comment.