X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fconsts.rs;h=c64c00134e89a1e77dadadf5bc306c536aa1b45d;hb=2153abb4124fd3dca018d4adb4e79693f1a9fedd;hp=9bb7b540d68fde94e2fa22116cf57e84921d5629;hpb=91a1cd562a1d19a4a5fba6c7905b5d8c3051da85;p=rust.git diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 9bb7b540d68..c64c00134e8 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -2,14 +2,14 @@ use crate::utils::{clip, higher, sext, unsext}; use if_chain::if_chain; -use rustc::ty::subst::{Subst, SubstsRef}; -use rustc::ty::{self, Ty, TyCtxt}; -use rustc::{bug, span_bug}; use rustc_ast::ast::{FloatTy, LitFloatType, LitKind}; use rustc_data_structures::sync::Lrc; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, QPath, UnOp}; use rustc_lint::LateContext; +use rustc_middle::ty::subst::{Subst, SubstsRef}; +use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::{bug, span_bug}; use rustc_span::symbol::Symbol; use std::cmp::Ordering::{self, Equal}; use std::convert::TryInto; @@ -231,7 +231,7 @@ pub fn expr(&mut self, e: &Expr<'_>) -> Option { ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple), ExprKind::Repeat(ref value, _) => { let n = match self.tables.expr_ty(e).kind { - ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env), + ty::Array(_, n) => n.try_eval_usize(self.lcx.tcx, self.lcx.param_env)?, _ => span_bug!(e.span, "typeck error"), }; self.expr(value).map(|v| Constant::Repeat(Box::new(v), n)) @@ -320,7 +320,7 @@ fn multi(&mut self, vec: &[Expr<'_>]) -> Option> { fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'cc>) -> Option { let res = self.tables.qpath_res(qpath, id); match res { - Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => { + Res::Def(DefKind::Const | DefKind::AssocConst, def_id) => { let substs = self.tables.node_substs(id); let substs = if self.substs.is_empty() { substs @@ -333,7 +333,7 @@ fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'cc>) -> Option, right: &Expr<'_>) -> Option) -> Option { - use rustc::mir::interpret::{ConstValue, Scalar}; + use rustc_middle::mir::interpret::{ConstValue, Scalar}; match result.val { ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data: d, .. })) => match result.ty.kind { ty::Bool => Some(Constant::Bool(d == 1)), @@ -492,6 +492,41 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option { }, _ => None, }, + ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty.kind { + ty::Array(sub_type, len) => match sub_type.kind { + ty::Float(FloatTy::F32) => match miri_to_const(len) { + Some(Constant::Int(len)) => alloc + .inspect_with_undef_and_ptr_outside_interpreter(0..(4 * len as usize)) + .to_owned() + .chunks(4) + .map(|chunk| { + Some(Constant::F32(f32::from_le_bytes( + chunk.try_into().expect("this shouldn't happen"), + ))) + }) + .collect::>>() + .map(Constant::Vec), + _ => None, + }, + ty::Float(FloatTy::F64) => match miri_to_const(len) { + Some(Constant::Int(len)) => alloc + .inspect_with_undef_and_ptr_outside_interpreter(0..(8 * len as usize)) + .to_owned() + .chunks(8) + .map(|chunk| { + Some(Constant::F64(f64::from_le_bytes( + chunk.try_into().expect("this shouldn't happen"), + ))) + }) + .collect::>>() + .map(Constant::Vec), + _ => None, + }, + // FIXME: implement other array type conversions. + _ => None, + }, + _ => None, + }, // FIXME: implement other conversions. _ => None, }