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/src/lib.rs | 2 +- crates/hir_def/src/type_ref.rs | 8 ++++++++ crates/hir_ty/src/consteval.rs | 24 ++++++++---------------- crates/hir_ty/src/infer/expr.rs | 5 ++--- crates/hir_ty/src/lower.rs | 19 +++++++------------ 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d7065ff2b..7e8a5bca3 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -53,7 +53,7 @@ use hir_def::{ use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; use hir_ty::{ autoderef, - consteval::ConstExtension, + consteval::ConstExt, could_unify, method_resolution::{self, def_crates, TyFingerprint}, primitive::UintTy, diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 6a3259b27..9e44547cb 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -336,6 +336,14 @@ impl std::fmt::Display for ConstScalar { } impl ConstScalar { + /// Gets a target usize out of the ConstScalar + pub fn as_usize(&self) -> Option { + match self { + &ConstScalar::Usize(us) => Some(us), + _ => None, + } + } + // FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this // parse stage. fn usize_from_literal_expr(expr: ast::Expr) -> ConstScalar { 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