Skip to content

Commit 1ef572a

Browse files
committed
the Const::eval_bits methods don't need to be given the Ty
1 parent a5d8b4d commit 1ef572a

File tree

13 files changed

+35
-48
lines changed

13 files changed

+35
-48
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,12 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
660660
}
661661
_ => match ct.ty().kind() {
662662
ty::Int(ity) => {
663-
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
663+
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
664664
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
665665
write!(output, "{val}")
666666
}
667667
ty::Uint(_) => {
668-
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
668+
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all());
669669
write!(output, "{val}")
670670
}
671671
ty::Bool => {

compiler/rustc_const_eval/src/transform/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl<'tcx> Validator<'_, 'tcx> {
554554
// Integer division: the RHS must be a non-zero const.
555555
let const_val = match rhs {
556556
Operand::Constant(c) => {
557-
c.literal.try_eval_bits(self.tcx, self.param_env, lhs_ty)
557+
c.literal.try_eval_bits(self.tcx, self.param_env)
558558
}
559559
_ => None,
560560
};

compiler/rustc_middle/src/mir/consts.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -323,23 +323,18 @@ impl<'tcx> ConstKind<'tcx> {
323323
}
324324

325325
#[inline]
326-
pub fn try_eval_bits(
327-
&self,
328-
tcx: TyCtxt<'tcx>,
329-
param_env: ty::ParamEnv<'tcx>,
330-
ty: Ty<'tcx>,
331-
) -> Option<u128> {
326+
pub fn try_eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<u128> {
332327
let int = self.try_eval_scalar_int(tcx, param_env)?;
333-
assert_eq!(self.ty(), ty);
334-
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
328+
let size =
329+
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(self.ty())).ok()?.size;
335330
int.to_bits(size).ok()
336331
}
337332

338333
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
339334
#[inline]
340-
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
341-
self.try_eval_bits(tcx, param_env, ty)
342-
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
335+
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> u128 {
336+
self.try_eval_bits(tcx, param_env)
337+
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", self.ty(), self))
343338
}
344339

345340
#[inline]

compiler/rustc_middle/src/ty/consts.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -339,24 +339,19 @@ impl<'tcx> Const<'tcx> {
339339
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
340340
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
341341
/// contains const generic parameters or pointers).
342-
pub fn try_eval_bits(
343-
self,
344-
tcx: TyCtxt<'tcx>,
345-
param_env: ParamEnv<'tcx>,
346-
ty: Ty<'tcx>,
347-
) -> Option<u128> {
342+
pub fn try_eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Option<u128> {
348343
let int = self.try_eval_scalar_int(tcx, param_env)?;
349-
assert_eq!(self.ty(), ty);
350-
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
344+
let size =
345+
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(self.ty())).ok()?.size;
351346
// if `ty` does not depend on generic parameters, use an empty param_env
352347
int.to_bits(size).ok()
353348
}
354349

355350
#[inline]
356351
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
357-
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
358-
self.try_eval_bits(tcx, param_env, ty)
359-
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
352+
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u128 {
353+
self.try_eval_bits(tcx, param_env)
354+
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", self.ty(), self))
360355
}
361356

362357
#[inline]

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
100100
expected: "constant pattern".to_string(),
101101
});
102102
};
103-
values.push(value.eval_bits(self.tcx, self.param_env, arm.pattern.ty));
103+
values.push(value.eval_bits(self.tcx, self.param_env));
104104
targets.push(self.parse_block(arm.body)?);
105105
}
106106

@@ -301,7 +301,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
301301
| ExprKind::NonHirLiteral { .. }
302302
| ExprKind::ConstBlock { .. } => Ok({
303303
let value = as_constant_inner(expr, |_| None, self.tcx);
304-
value.literal.eval_bits(self.tcx, self.param_env, value.ty())
304+
value.literal.eval_bits(self.tcx, self.param_env)
305305
}),
306306
)
307307
}

compiler/rustc_mir_build/src/build/matches/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1622,9 +1622,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16221622
// may want to add cases based on the candidates that are
16231623
// available
16241624
match test.kind {
1625-
TestKind::SwitchInt { switch_ty, ref mut options } => {
1625+
TestKind::SwitchInt { switch_ty: _, ref mut options } => {
16261626
for candidate in candidates.iter() {
1627-
if !self.add_cases_to_switch(&match_place, candidate, switch_ty, options) {
1627+
if !self.add_cases_to_switch(&match_place, candidate, options) {
16281628
break;
16291629
}
16301630
}

compiler/rustc_mir_build/src/build/matches/test.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8585
&mut self,
8686
test_place: &PlaceBuilder<'tcx>,
8787
candidate: &Candidate<'pat, 'tcx>,
88-
switch_ty: Ty<'tcx>,
8988
options: &mut FxIndexMap<ConstKind<'tcx>, u128>,
9089
) -> bool {
9190
let Some(match_pair) = candidate.match_pairs.iter().find(|mp| mp.place == *test_place)
@@ -95,9 +94,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9594

9695
match match_pair.pattern.kind {
9796
PatKind::Constant { value } => {
98-
options
99-
.entry(value)
100-
.or_insert_with(|| value.eval_bits(self.tcx, self.param_env, switch_ty));
97+
options.entry(value).or_insert_with(|| value.eval_bits(self.tcx, self.param_env));
10198
true
10299
}
103100
PatKind::Variant { .. } => {

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl IntRange {
146146
valtree.unwrap_leaf().to_bits(target_size).ok()
147147
},
148148
// This is a more general form of the previous case.
149-
_ => value.try_eval_bits(tcx, param_env, ty),
149+
_ => value.try_eval_bits(tcx, param_env),
150150
}?;
151151

152152
let val = val ^ bias;
@@ -1379,8 +1379,8 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
13791379
let ty = lo.ty();
13801380
ctor = if let Some(int_range) = IntRange::from_range(
13811381
cx.tcx,
1382-
lo.eval_bits(cx.tcx, cx.param_env, lo.ty()),
1383-
hi.eval_bits(cx.tcx, cx.param_env, hi.ty()),
1382+
lo.eval_bits(cx.tcx, cx.param_env),
1383+
hi.eval_bits(cx.tcx, cx.param_env),
13841384
ty,
13851385
&end,
13861386
) {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
131131
if let Some(hir::Expr { kind: hir::ExprKind::Lit(lit), .. }) = lo_expr
132132
&& let rustc_ast::ast::LitKind::Int(val, _) = lit.node
133133
{
134-
if lo.eval_bits(self.tcx, self.param_env, ty) != val {
134+
if lo.eval_bits(self.tcx, self.param_env) != val {
135135
lower_overflow = true;
136136
self.tcx.sess.emit_err(LiteralOutOfRange { span: lit.span, ty, max: max() });
137137
}
138138
}
139139
if let Some(hir::Expr { kind: hir::ExprKind::Lit(lit), .. }) = hi_expr
140140
&& let rustc_ast::ast::LitKind::Int(val, _) = lit.node
141141
{
142-
if hi.eval_bits(self.tcx, self.param_env, ty) != val {
142+
if hi.eval_bits(self.tcx, self.param_env) != val {
143143
higher_overflow = true;
144144
self.tcx.sess.emit_err(LiteralOutOfRange { span: lit.span, ty, max: max() });
145145
}
@@ -162,15 +162,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
162162
if let Some(hir::Expr { kind: hir::ExprKind::Lit(lit), .. }) = lo_expr
163163
&& let rustc_ast::ast::LitKind::Int(val, _) = lit.node
164164
{
165-
if lo.eval_bits(self.tcx, self.param_env, ty) != val {
165+
if lo.eval_bits(self.tcx, self.param_env) != val {
166166
lower_overflow = true;
167167
self.tcx.sess.emit_err(LiteralOutOfRange { span: lit.span, ty, max: max() });
168168
}
169169
}
170170
if let Some(hir::Expr { kind: hir::ExprKind::Lit(lit), .. }) = hi_expr
171171
&& let rustc_ast::ast::LitKind::Int(val, _) = lit.node
172172
{
173-
if hi.eval_bits(self.tcx, self.param_env, ty) != val {
173+
if hi.eval_bits(self.tcx, self.param_env) != val {
174174
higher_overflow = true;
175175
self.tcx.sess.emit_err(LiteralOutOfRange { span: lit.span, ty, max: max() });
176176
}
@@ -865,8 +865,8 @@ pub(crate) fn compare_const_vals<'tcx>(
865865
},
866866
}
867867

868-
let a = a.eval_bits(tcx, param_env, ty);
869-
let b = b.eval_bits(tcx, param_env, ty);
868+
let a = a.eval_bits(tcx, param_env);
869+
let b = b.eval_bits(tcx, param_env);
870870

871871
use rustc_apfloat::Float;
872872
match *ty.kind() {

compiler/rustc_mir_transform/src/const_goto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
9696
let (discr, targets) = target_bb_terminator.kind.as_switch()?;
9797
if discr.place() == Some(*place) {
9898
let switch_ty = place.ty(self.body.local_decls(), self.tcx).ty;
99+
debug_assert_eq!(switch_ty, _const.ty());
99100
// We now know that the Switch matches on the const place, and it is statementless
100101
// Now find which value in the Switch matches the const value.
101-
let const_value =
102-
_const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
102+
let const_value = _const.literal.try_eval_bits(self.tcx, self.param_env)?;
103103
let target_to_use_in_goto = targets.target_for_value(const_value);
104104
self.optimizations.push(OptimizationToApply {
105105
bb_with_goto: location.block,

compiler/rustc_mir_transform/src/simplify_branches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyConstCondition {
2323
TerminatorKind::SwitchInt {
2424
discr: Operand::Constant(ref c), ref targets, ..
2525
} => {
26-
let constant = c.literal.try_eval_bits(tcx, param_env, c.ty());
26+
let constant = c.literal.try_eval_bits(tcx, param_env);
2727
if let Some(constant) = constant {
2828
let target = targets.target_for_value(constant);
2929
TerminatorKind::Goto { target }

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ fn encode_const<'tcx>(
118118
// bool value false is encoded as 0 and true as 1.
119119
match c.ty().kind() {
120120
ty::Int(ity) => {
121-
let bits = c.eval_bits(tcx, ty::ParamEnv::reveal_all(), c.ty());
121+
let bits = c.eval_bits(tcx, ty::ParamEnv::reveal_all());
122122
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
123123
if val < 0 {
124124
s.push('n');
125125
}
126126
let _ = write!(s, "{val}");
127127
}
128128
ty::Uint(_) => {
129-
let val = c.eval_bits(tcx, ty::ParamEnv::reveal_all(), c.ty());
129+
let val = c.eval_bits(tcx, ty::ParamEnv::reveal_all());
130130
let _ = write!(s, "{val}");
131131
}
132132
ty::Bool => {

compiler/rustc_symbol_mangling/src/v0.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
594594
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
595595
self = ty.print(self)?;
596596

597-
let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ty);
597+
let mut bits = ct.eval_bits(self.tcx, ty::ParamEnv::reveal_all());
598598

599599
// Negative integer values are mangled using `n` as a "sign prefix".
600600
if let ty::Int(ity) = ty.kind() {

0 commit comments

Comments
 (0)