Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d9b4598

Browse files
committedJan 26, 2025
Auto merge of #135753 - compiler-errors:from-ty-const, r=oli-obk
Get rid of `mir::Const::from_ty_const` This function is strange, because it turns valtrees into `mir::Const::Value`, but the rest of the const variants stay as type system consts. All of the callsites except for one in `instsimplify` (array length simplification of `ptr_metadata` call) just go through the valtree arm of the function, so it's easier to just create a `mir::Const` directly for those. For the instsimplify case, if we have a type system const we should *keep* having a type system const, rather than turning it into a `mir::Const::Value`; it doesn't really matter in practice, though, bc `usize` has no padding, but it feels more principled.
2 parents c2270be + e9a5660 commit d9b4598

File tree

10 files changed

+21
-98
lines changed

10 files changed

+21
-98
lines changed
 

‎compiler/rustc_middle/src/mir/consts.rs

-11
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,6 @@ impl<'tcx> Const<'tcx> {
460460
Self::Val(val, ty)
461461
}
462462

463-
pub fn from_ty_const(c: ty::Const<'tcx>, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
464-
match c.kind() {
465-
ty::ConstKind::Value(ty, valtree) => {
466-
// Make sure that if `c` is normalized, then the return value is normalized.
467-
let const_val = tcx.valtree_to_const_val((ty, valtree));
468-
Self::Val(const_val, ty)
469-
}
470-
_ => Self::Ty(ty, c),
471-
}
472-
}
473-
474463
/// Return true if any evaluation of this constant always returns the same value,
475464
/// taking into account even pointer identity tests.
476465
pub fn is_deterministic(&self) -> bool {

‎compiler/rustc_middle/src/ty/util.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use tracing::{debug, instrument};
2020

2121
use super::TypingEnv;
2222
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
23+
use crate::mir;
2324
use crate::query::Providers;
2425
use crate::ty::fold::fold_regions;
2526
use crate::ty::layout::{FloatExt, IntegerExt};
@@ -1183,18 +1184,18 @@ impl<'tcx> Ty<'tcx> {
11831184

11841185
/// Returns the maximum value for the given numeric type (including `char`s)
11851186
/// or returns `None` if the type is not numeric.
1186-
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
1187+
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<mir::Const<'tcx>> {
11871188
let typing_env = TypingEnv::fully_monomorphized();
11881189
self.numeric_min_and_max_as_bits(tcx)
1189-
.map(|(_, max)| ty::Const::from_bits(tcx, max, typing_env, self))
1190+
.map(|(_, max)| mir::Const::from_bits(tcx, max, typing_env, self))
11901191
}
11911192

11921193
/// Returns the minimum value for the given numeric type (including `char`s)
11931194
/// or returns `None` if the type is not numeric.
1194-
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
1195+
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<mir::Const<'tcx>> {
11951196
let typing_env = TypingEnv::fully_monomorphized();
11961197
self.numeric_min_and_max_as_bits(tcx)
1197-
.map(|(min, _)| ty::Const::from_bits(tcx, min, typing_env, self))
1198+
.map(|(min, _)| mir::Const::from_bits(tcx, min, typing_env, self))
11981199
}
11991200

12001201
/// Checks whether values of this type `T` have a size known at

‎compiler/rustc_mir_transform/src/gvn.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1178,11 +1178,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
11781178
) if let ty::Slice(..) = to.builtin_deref(true).unwrap().kind()
11791179
&& let ty::Array(_, len) = from.builtin_deref(true).unwrap().kind() =>
11801180
{
1181-
return self.insert_constant(Const::from_ty_const(
1182-
*len,
1183-
self.tcx.types.usize,
1184-
self.tcx,
1185-
));
1181+
return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
11861182
}
11871183
_ => Value::UnaryOp(op, arg_index),
11881184
};
@@ -1492,11 +1488,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14921488
// Trivial case: we are fetching a statically known length.
14931489
let place_ty = place.ty(self.local_decls, self.tcx).ty;
14941490
if let ty::Array(_, len) = place_ty.kind() {
1495-
return self.insert_constant(Const::from_ty_const(
1496-
*len,
1497-
self.tcx.types.usize,
1498-
self.tcx,
1499-
));
1491+
return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
15001492
}
15011493

15021494
let mut inner = self.simplify_place_value(place, location)?;
@@ -1518,11 +1510,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
15181510
&& let Some(to) = to.builtin_deref(true)
15191511
&& let ty::Slice(..) = to.kind()
15201512
{
1521-
return self.insert_constant(Const::from_ty_const(
1522-
*len,
1523-
self.tcx.types.usize,
1524-
self.tcx,
1525-
));
1513+
return self.insert_constant(Const::Ty(self.tcx.types.usize, *len));
15261514
}
15271515

15281516
// Fallback: a symbolic `Len`.

‎compiler/rustc_mir_transform/src/instsimplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
171171
if let Rvalue::Len(ref place) = *rvalue {
172172
let place_ty = place.ty(self.local_decls, self.tcx).ty;
173173
if let ty::Array(_, len) = *place_ty.kind() {
174-
let const_ = Const::from_ty_const(len, self.tcx.types.usize, self.tcx);
174+
let const_ = Const::Ty(self.tcx.types.usize, len);
175175
let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None };
176176
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
177177
}

‎compiler/rustc_pattern_analysis/src/rustc.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
794794
// fictitious values after `{u,i}size::MAX` (see [`IntRange::split`] for why we do
795795
// this). We show this to the user as `usize::MAX..` which is slightly incorrect but
796796
// probably clear enough.
797-
let c = ty.numeric_max_val(cx.tcx).unwrap();
798-
let value = mir::Const::from_ty_const(c, ty.0, cx.tcx);
799-
lo = PatRangeBoundary::Finite(value);
797+
lo = PatRangeBoundary::Finite(ty.numeric_max_val(cx.tcx).unwrap());
800798
}
801799
let hi = if let Some(hi) = range.hi.minus_one() {
802800
hi

‎compiler/rustc_smir/src/rustc_smir/context.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -451,16 +451,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
451451
let ty = ty::Ty::new_static_str(tcx);
452452
let bytes = value.as_bytes();
453453
let val_tree = ty::ValTree::from_raw_bytes(tcx, bytes);
454-
455-
let ct = ty::Const::new_value(tcx, val_tree, ty);
456-
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
454+
let val = tcx.valtree_to_const_val((ty, val_tree));
455+
mir::Const::from_value(val, ty).stable(&mut tables)
457456
}
458457

459458
fn new_const_bool(&self, value: bool) -> MirConst {
460459
let mut tables = self.0.borrow_mut();
461-
let ct = ty::Const::from_bool(tables.tcx, value);
462-
let ty = tables.tcx.types.bool;
463-
super::convert::mir_const_from_ty_const(&mut *tables, ct, ty)
460+
mir::Const::from_bool(tables.tcx, value).stable(&mut tables)
464461
}
465462

466463
fn try_new_const_uint(&self, value: u128, uint_ty: UintTy) -> Result<MirConst, Error> {
@@ -472,13 +469,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
472469
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
473470
.unwrap()
474471
.size;
475-
476-
// We don't use Const::from_bits since it doesn't have any error checking.
477472
let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
478473
Error::new(format!("Value overflow: cannot convert `{value}` to `{ty}`."))
479474
})?;
480-
let ct = ty::Const::new_value(tables.tcx, ValTree::from_scalar_int(scalar), ty);
481-
Ok(super::convert::mir_const_from_ty_const(&mut *tables, ct, ty))
475+
Ok(mir::Const::from_scalar(tcx, mir::interpret::Scalar::Int(scalar), ty)
476+
.stable(&mut tables))
482477
}
483478
fn try_new_ty_const_uint(
484479
&self,

‎compiler/rustc_smir/src/rustc_smir/convert/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ mod error;
99
mod mir;
1010
mod ty;
1111

12-
pub(crate) use ty::mir_const_from_ty_const;
13-
1412
impl<'tcx> Stable<'tcx> for rustc_hir::Safety {
1513
type T = stable_mir::mir::Safety;
1614
fn stable(&self, _: &mut Tables<'_>) -> Self::T {

‎compiler/rustc_smir/src/rustc_smir/convert/ty.rs

-42
Original file line numberDiff line numberDiff line change
@@ -414,48 +414,6 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
414414
}
415415
}
416416

417-
pub(crate) fn mir_const_from_ty_const<'tcx>(
418-
tables: &mut Tables<'tcx>,
419-
ty_const: ty::Const<'tcx>,
420-
ty: Ty<'tcx>,
421-
) -> stable_mir::ty::MirConst {
422-
let kind = match ty_const.kind() {
423-
ty::ConstKind::Value(ty, val) => {
424-
let val = match val {
425-
ty::ValTree::Leaf(scalar) => ty::ValTree::Leaf(scalar),
426-
ty::ValTree::Branch(branch) => {
427-
ty::ValTree::Branch(tables.tcx.lift(branch).unwrap())
428-
}
429-
};
430-
let ty = tables.tcx.lift(ty).unwrap();
431-
let const_val = tables.tcx.valtree_to_const_val((ty, val));
432-
if matches!(const_val, mir::ConstValue::ZeroSized) {
433-
stable_mir::ty::ConstantKind::ZeroSized
434-
} else {
435-
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
436-
ty, const_val, tables,
437-
))
438-
}
439-
}
440-
ty::ConstKind::Param(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
441-
ty::ConstKind::Error(_) => unreachable!(),
442-
ty::ConstKind::Infer(_) => unreachable!(),
443-
ty::ConstKind::Bound(_, _) => unimplemented!(),
444-
ty::ConstKind::Placeholder(_) => unimplemented!(),
445-
ty::ConstKind::Unevaluated(uv) => {
446-
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
447-
def: tables.const_def(uv.def),
448-
args: uv.args.stable(tables),
449-
promoted: None,
450-
})
451-
}
452-
ty::ConstKind::Expr(_) => unimplemented!(),
453-
};
454-
let stable_ty = tables.intern_ty(ty);
455-
let id = tables.intern_mir_const(mir::Const::Ty(ty, ty_const));
456-
stable_mir::ty::MirConst::new(kind, stable_ty, id)
457-
}
458-
459417
impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
460418
type T = stable_mir::ty::TyConst;
461419

‎src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::diagnostics::span_lint_and_note;
33
use core::cmp::Ordering;
44
use rustc_hir::{Arm, Expr, PatKind, RangeEnd};
55
use rustc_lint::LateContext;
6-
use rustc_middle::mir;
76
use rustc_middle::ty::Ty;
87
use rustc_span::Span;
98

@@ -36,14 +35,12 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3635
let lhs_const = if let Some(lhs) = lhs {
3736
ConstEvalCtxt::new(cx).eval_pat_expr(lhs)?
3837
} else {
39-
let min_val_const = ty.numeric_min_val(cx.tcx)?;
40-
mir_to_const(cx.tcx, mir::Const::from_ty_const(min_val_const, ty, cx.tcx))?
38+
mir_to_const(cx.tcx, ty.numeric_min_val(cx.tcx)?)?
4139
};
4240
let rhs_const = if let Some(rhs) = rhs {
4341
ConstEvalCtxt::new(cx).eval_pat_expr(rhs)?
4442
} else {
45-
let max_val_const = ty.numeric_max_val(cx.tcx)?;
46-
mir_to_const(cx.tcx, mir::Const::from_ty_const(max_val_const, ty, cx.tcx))?
43+
mir_to_const(cx.tcx, ty.numeric_max_val(cx.tcx)?)?
4744
};
4845
let lhs_val = lhs_const.int_value(cx.tcx, ty)?;
4946
let rhs_val = rhs_const.int_value(cx.tcx, ty)?;

‎src/tools/clippy/clippy_utils/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ use rustc_hir::{
112112
use rustc_lexer::{TokenKind, tokenize};
113113
use rustc_lint::{LateContext, Level, Lint, LintContext};
114114
use rustc_middle::hir::place::PlaceBase;
115-
use rustc_middle::mir::Const;
116115
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
117116
use rustc_middle::ty::fast_reject::SimplifiedType;
118117
use rustc_middle::ty::layout::IntegerExt;
@@ -1584,8 +1583,8 @@ pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Opti
15841583
let start_is_none_or_min = start.is_none_or(|start| {
15851584
if let rustc_ty::Adt(_, subst) = ty.kind()
15861585
&& let bnd_ty = subst.type_at(0)
1587-
&& let Some(min_val) = bnd_ty.numeric_min_val(cx.tcx)
1588-
&& let Some(min_const) = mir_to_const(cx.tcx, Const::from_ty_const(min_val, bnd_ty, cx.tcx))
1586+
&& let Some(min_const) = bnd_ty.numeric_min_val(cx.tcx)
1587+
&& let Some(min_const) = mir_to_const(cx.tcx, min_const)
15891588
&& let Some(start_const) = ConstEvalCtxt::new(cx).eval(start)
15901589
{
15911590
start_const == min_const
@@ -1597,8 +1596,8 @@ pub fn is_range_full(cx: &LateContext<'_>, expr: &Expr<'_>, container_path: Opti
15971596
RangeLimits::Closed => {
15981597
if let rustc_ty::Adt(_, subst) = ty.kind()
15991598
&& let bnd_ty = subst.type_at(0)
1600-
&& let Some(max_val) = bnd_ty.numeric_max_val(cx.tcx)
1601-
&& let Some(max_const) = mir_to_const(cx.tcx, Const::from_ty_const(max_val, bnd_ty, cx.tcx))
1599+
&& let Some(max_const) = bnd_ty.numeric_max_val(cx.tcx)
1600+
&& let Some(max_const) = mir_to_const(cx.tcx, max_const)
16021601
&& let Some(end_const) = ConstEvalCtxt::new(cx).eval(end)
16031602
{
16041603
end_const == max_const

0 commit comments

Comments
 (0)
Failed to load comments.