From de0ed9860d86c3b905a967b1a7b5243499d32d67 Mon Sep 17 00:00:00 2001 From: Jade Date: Sat, 15 May 2021 18:51:18 -0700 Subject: Address final feedback * rename ConstExtension->ConstExt * refactor a manual construction of a Const --- crates/hir_ty/src/consteval.rs | 24 ++++++++---------------- crates/hir_ty/src/infer/expr.rs | 5 ++--- crates/hir_ty/src/lower.rs | 19 +++++++------------ 3 files changed, 17 insertions(+), 31 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/consteval.rs b/crates/hir_ty/src/consteval.rs index a4a430f30..e3ceb3d62 100644 --- a/crates/hir_ty/src/consteval.rs +++ b/crates/hir_ty/src/consteval.rs @@ -11,12 +11,12 @@ use hir_def::{ use crate::{Const, ConstData, ConstValue, Interner, TyKind}; /// Extension trait for [`Const`] -pub trait ConstExtension { +pub trait ConstExt { /// Is a [`Const`] unknown? fn is_unknown(&self) -> bool; } -impl ConstExtension for Const { +impl ConstExt for Const { fn is_unknown(&self) -> bool { match self.data(&Interner).value { // interned Unknown @@ -35,20 +35,12 @@ impl ConstExtension for Const { } } -/// Extension trait for [`Expr`] -pub trait ExprEval { - /// Attempts to evaluate the expression as a target usize. - fn eval_usize(&self) -> Option; -} - -impl ExprEval for Expr { - // FIXME: support more than just evaluating literals - fn eval_usize(&self) -> Option { - match self { - Expr::Literal(Literal::Uint(v, None)) - | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(), - _ => None, - } +// FIXME: support more than just evaluating literals +pub fn eval_usize(expr: &Expr) -> Option { + match expr { + Expr::Literal(Literal::Uint(v, None)) + | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(), + _ => None, } } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 04fc2f12b..b6b5a1b75 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -15,8 +15,7 @@ use stdx::always; use syntax::ast::RangeOp; use crate::{ - autoderef, - consteval::{self, ExprEval}, + autoderef, consteval, lower::lower_to_chalk_mutability, mapping::from_chalk, method_resolution, op, @@ -738,7 +737,7 @@ impl<'a> InferenceContext<'a> { ); let repeat_expr = &self.body.exprs[*repeat]; - repeat_expr.eval_usize() + consteval::eval_usize(repeat_expr) } }; diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index f7015e5ff..bd8bb6028 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -9,9 +9,7 @@ use std::cell::{Cell, RefCell}; use std::{iter, sync::Arc}; use base_db::CrateId; -use chalk_ir::{ - cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety, Scalar, UintTy, -}; +use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety}; use hir_def::{ adt::StructKind, body::{Expander, LowerCtx}, @@ -31,16 +29,17 @@ use stdx::impl_from; use syntax::ast; use crate::{ + consteval, db::HirDatabase, mapping::ToChalk, static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, utils::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, }, - AliasEq, AliasTy, Binders, BoundVar, CallableSig, ConstData, ConstValue, DebruijnIndex, DynTy, - FnPointer, FnSig, FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, - QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, - Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, + AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, + FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, + QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, + TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, }; #[derive(Debug)] @@ -176,11 +175,7 @@ impl<'a> TyLoweringContext<'a> { TypeRef::Array(inner, len) => { let inner_ty = self.lower_ty(inner); - let const_len = ConstData { - ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner), - value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: *len }), - } - .intern(&Interner); + let const_len = consteval::usize_const(len.as_usize()); TyKind::Array(inner_ty, const_len).intern(&Interner) } -- cgit v1.2.3