From c3491378e3420a19dc98708a2953df5c524c2681 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 6 Apr 2022 10:12:42 +0200 Subject: [PATCH] get rid of visit_constant in thir visitor --- compiler/rustc_middle/src/thir/visit.rs | 10 ++++++++-- compiler/rustc_middle/src/ty/util.rs | 14 ++++++++------ .../rustc_mir_build/src/thir/pattern/mod.rs | 6 ++---- .../src/traits/const_evaluatable.rs | 4 ---- .../src/matches/overlapping_arms.rs | 17 +++++++++-------- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_middle/src/thir/visit.rs b/compiler/rustc_middle/src/thir/visit.rs index ab9e9660633..ef7becde69a 100644 --- a/compiler/rustc_middle/src/thir/visit.rs +++ b/compiler/rustc_middle/src/thir/visit.rs @@ -1,7 +1,6 @@ use super::{ Arm, Block, Expr, ExprKind, Guard, InlineAsmOperand, Pat, PatKind, Stmt, StmtKind, Thir, }; -use crate::mir::ConstantKind; pub trait Visitor<'a, 'tcx: 'a>: Sized { fn thir(&self) -> &'a Thir<'tcx>; @@ -26,7 +25,14 @@ pub trait Visitor<'a, 'tcx: 'a>: Sized { walk_pat(self, pat); } - fn visit_constant(&mut self, _constant: ConstantKind<'tcx>) {} + // Note: We don't have visitors for `ty::Const` and `mir::ConstantKind` + // (even though these types occur in THIR) for consistency and to reduce confusion, + // since the lazy creation of constants during thir construction causes most + // 'constants' to not be of type `ty::Const` or `mir::ConstantKind` at that + // stage (they are mostly still identified by `DefId` or `hir::Lit`, see + // the variants `Literal`, `NonHirLiteral` and `NamedConst` in `thir::ExprKind`). + // You have to manually visit `ty::Const` and `mir::ConstantKind` through the + // other `visit*` functions. } pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Expr<'tcx>) { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 960f9c0acd4..7d9a917d04c 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -662,8 +662,8 @@ impl<'tcx> TypeFolder<'tcx> for OpaqueTypeExpander<'tcx> { impl<'tcx> Ty<'tcx> { /// Returns the maximum value for the given numeric type (including `char`s) /// or returns `None` if the type is not numeric. - pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option { - match self.kind() { + pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option> { + let val = match self.kind() { ty::Int(_) | ty::Uint(_) => { let (size, signed) = int_size_and_signed(tcx, self); let val = @@ -676,13 +676,14 @@ impl<'tcx> Ty<'tcx> { ty::FloatTy::F64 => rustc_apfloat::ieee::Double::INFINITY.to_bits(), }), _ => None, - } + }; + val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self))) } /// Returns the minimum value for the given numeric type (including `char`s) /// or returns `None` if the type is not numeric. - pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option { - match self.kind() { + pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option> { + let val = match self.kind() { ty::Int(_) | ty::Uint(_) => { let (size, signed) = int_size_and_signed(tcx, self); let val = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 }; @@ -694,7 +695,8 @@ impl<'tcx> Ty<'tcx> { ty::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(), }), _ => None, - } + }; + val.map(|v| Const::from_bits(tcx, v, ty::ParamEnv::empty().and(self))) } /// Checks whether values of this type `T` are *moved* or *copied* diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 59b9d2c9279..dbcd701c1ad 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -183,12 +183,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { Some((*lo, *hi)) } (Some(PatKind::Constant { value: lo }), None) => { - let hi = ty.numeric_max_val(self.tcx)?; - Some((*lo, ty::Const::from_bits(self.tcx, hi, ty::ParamEnv::empty().and(ty)))) + Some((*lo, ty.numeric_max_val(self.tcx)?)) } (None, Some(PatKind::Constant { value: hi })) => { - let lo = ty.numeric_min_val(self.tcx)?; - Some((ty::Const::from_bits(self.tcx, lo, ty::ParamEnv::empty().and(ty)), *hi)) + Some((ty.numeric_min_val(self.tcx)?, *hi)) } _ => None, } diff --git a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs index 498412b5cfd..b409bc3d64e 100644 --- a/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs +++ b/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs @@ -376,10 +376,6 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> { visit::walk_pat(self, pat); } } - - fn visit_constant(&mut self, ct: mir::ConstantKind<'tcx>) { - self.is_poly |= ct.has_param_types_or_consts(); - } } let mut is_poly_vis = IsThirPolymorphic { is_poly: false, thir: body }; diff --git a/src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs b/src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs index b5fa847451d..c0b3e95b185 100644 --- a/src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs +++ b/src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs @@ -1,4 +1,4 @@ -use clippy_utils::consts::{constant, constant_full_int, FullInt}; +use clippy_utils::consts::{constant, constant_full_int, miri_to_const, FullInt}; use clippy_utils::diagnostics::span_lint_and_note; use core::cmp::Ordering; use rustc_hir::{Arm, Expr, PatKind, RangeEnd}; @@ -32,15 +32,16 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>) .filter_map(|arm| { if let Arm { pat, guard: None, .. } = *arm { if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind { - let lhs_val = match lhs { - Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0.int_value(cx, ty)?, - None => FullInt::U(ty.numeric_min_val(cx.tcx)?), + let lhs_const = match lhs { + Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0, + None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?, }; - let rhs_val = match rhs { - Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0.int_value(cx, ty)?, - None => FullInt::U(ty.numeric_max_val(cx.tcx)?), + let rhs_const = match rhs { + Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0, + None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?, }; - + let lhs_val = lhs_const.int_value(cx, ty)?; + let rhs_val = rhs_const.int_value(cx, ty)?; let rhs_bound = match range_end { RangeEnd::Included => EndBound::Included(rhs_val), RangeEnd::Excluded => EndBound::Excluded(rhs_val),