Skip to content

Commit c349137

Browse files
committed
get rid of visit_constant in thir visitor
1 parent 14e3d03 commit c349137

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::{
22
Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir,
33
};
4-
use crate::mir::ConstantKind;
54

65
pub trait Visitor<'a, 'tcx: 'a>: Sized {
76
fn thir(&self) -> &'a Thir<'tcx>;
@@ -26,7 +25,14 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized {
2625
walk_pat(self, pat);
2726
}
2827

29-
fn visit_constant(&mut self, _constant: ConstantKind<'tcx>) {}
28+
// Note: We don't have visitors for `ty::Const` and `mir::ConstantKind`
29+
// (even though these types occur in THIR) for consistency and to reduce confusion,
30+
// since the lazy creation of constants during thir construction causes most
31+
// 'constants' to not be of type `ty::Const` or `mir::ConstantKind` at that
32+
// stage (they are mostly still identified by `DefId` or `hir::Lit`, see
33+
// the variants `Literal`, `NonHirLiteral` and `NamedConst` in `thir::ExprKind`).
34+
// You have to manually visit `ty::Const` and `mir::ConstantKind` through the
35+
// other `visit*` functions.
3036
}
3137

3238
pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) {

compiler/rustc_middle/src/ty/util.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ impl<'tcx> TypeFolder<'tcx> for OpaqueTypeExpander<'tcx> {
662662
impl<'tcx> Ty<'tcx> {
663663
/// Returns the maximum value for the given numeric type (including `char`s)
664664
/// or returns `None` if the type is not numeric.
665-
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<u128> {
666-
match self.kind() {
665+
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<Const<'tcx>> {
666+
let val = match self.kind() {
667667
ty::Int(_) | ty::Uint(_) => {
668668
let (size, signed) = int_size_and_signed(tcx, self);
669669
let val =
@@ -676,13 +676,14 @@ impl<'tcx> Ty<'tcx> {
676676
ty::FloatTy::F64 => rustc_apfloat::ieee::Double::INFINITY.to_bits(),
677677
}),
678678
_ => None,
679-
}
679+
};
680+
val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
680681
}
681682

682683
/// Returns the minimum value for the given numeric type (including `char`s)
683684
/// or returns `None` if the type is not numeric.
684-
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<u128> {
685-
match self.kind() {
685+
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<Const<'tcx>> {
686+
let val = match self.kind() {
686687
ty::Int(_) | ty::Uint(_) => {
687688
let (size, signed) = int_size_and_signed(tcx, self);
688689
let val = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 };
@@ -694,7 +695,8 @@ impl<'tcx> Ty<'tcx> {
694695
ty::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(),
695696
}),
696697
_ => None,
697-
}
698+
};
699+
val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self)))
698700
}
699701

700702
/// Checks whether values of this type `T` are *moved* or *copied*

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
183183
Some((*lo, *hi))
184184
}
185185
(Some(PatKind::Constant { value: lo }), None) => {
186-
let hi = ty.numeric_max_val(self.tcx)?;
187-
Some((*lo, ty::Const::from_bits(self.tcx, hi, ty::ParamEnv::empty().and(ty))))
186+
Some((*lo, ty.numeric_max_val(self.tcx)?))
188187
}
189188
(None, Some(PatKind::Constant { value: hi })) => {
190-
let lo = ty.numeric_min_val(self.tcx)?;
191-
Some((ty::Const::from_bits(self.tcx, lo, ty::ParamEnv::empty().and(ty)), *hi))
189+
Some((ty.numeric_min_val(self.tcx)?, *hi))
192190
}
193191
_ => None,
194192
}

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,6 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
376376
visit::walk_pat(self, pat);
377377
}
378378
}
379-
380-
fn visit_constant(&mut self, ct: mir::ConstantKind<'tcx>) {
381-
self.is_poly |= ct.has_param_types_or_consts();
382-
}
383379
}
384380

385381
let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body };

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{constant, constant_full_int, FullInt};
1+
use clippy_utils::consts::{constant, constant_full_int, miri_to_const, FullInt};
22
use clippy_utils::diagnostics::span_lint_and_note;
33
use core::cmp::Ordering;
44
use rustc_hir::{Arm, Expr, PatKind, RangeEnd};
@@ -32,15 +32,16 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3232
.filter_map(|arm| {
3333
if let Arm { pat, guard: None, .. } = *arm {
3434
if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
35-
let lhs_val = match lhs {
36-
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0.int_value(cx, ty)?,
37-
None => FullInt::U(ty.numeric_min_val(cx.tcx)?),
35+
let lhs_const = match lhs {
36+
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0,
37+
None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?,
3838
};
39-
let rhs_val = match rhs {
40-
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0.int_value(cx, ty)?,
41-
None => FullInt::U(ty.numeric_max_val(cx.tcx)?),
39+
let rhs_const = match rhs {
40+
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0,
41+
None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?,
4242
};
43-
43+
let lhs_val = lhs_const.int_value(cx, ty)?;
44+
let rhs_val = rhs_const.int_value(cx, ty)?;
4445
let rhs_bound = match range_end {
4546
RangeEnd::Included => EndBound::Included(rhs_val),
4647
RangeEnd::Excluded => EndBound::Excluded(rhs_val),

0 commit comments

Comments
 (0)