aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty
diff options
context:
space:
mode:
authorJade <[email protected]>2021-05-16 02:51:18 +0100
committerJade <[email protected]>2021-05-16 02:51:18 +0100
commitde0ed9860d86c3b905a967b1a7b5243499d32d67 (patch)
tree6c98701c319b32a52fdd6d39ce85c590aaf254f9 /crates/hir_ty
parent78d6b88f211cc9faf88815ce7fb1a91546cfce15 (diff)
Address final feedback
* rename ConstExtension->ConstExt * refactor a manual construction of a Const
Diffstat (limited to 'crates/hir_ty')
-rw-r--r--crates/hir_ty/src/consteval.rs24
-rw-r--r--crates/hir_ty/src/infer/expr.rs5
-rw-r--r--crates/hir_ty/src/lower.rs19
3 files changed, 17 insertions, 31 deletions
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::{
11use crate::{Const, ConstData, ConstValue, Interner, TyKind}; 11use crate::{Const, ConstData, ConstValue, Interner, TyKind};
12 12
13/// Extension trait for [`Const`] 13/// Extension trait for [`Const`]
14pub trait ConstExtension { 14pub trait ConstExt {
15 /// Is a [`Const`] unknown? 15 /// Is a [`Const`] unknown?
16 fn is_unknown(&self) -> bool; 16 fn is_unknown(&self) -> bool;
17} 17}
18 18
19impl ConstExtension for Const { 19impl ConstExt for Const {
20 fn is_unknown(&self) -> bool { 20 fn is_unknown(&self) -> bool {
21 match self.data(&Interner).value { 21 match self.data(&Interner).value {
22 // interned Unknown 22 // interned Unknown
@@ -35,20 +35,12 @@ impl ConstExtension for Const {
35 } 35 }
36} 36}
37 37
38/// Extension trait for [`Expr`] 38// FIXME: support more than just evaluating literals
39pub trait ExprEval { 39pub fn eval_usize(expr: &Expr) -> Option<u64> {
40 /// Attempts to evaluate the expression as a target usize. 40 match expr {
41 fn eval_usize(&self) -> Option<u64>; 41 Expr::Literal(Literal::Uint(v, None))
42} 42 | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
43 43 _ => None,
44impl ExprEval for Expr {
45 // FIXME: support more than just evaluating literals
46 fn eval_usize(&self) -> Option<u64> {
47 match self {
48 Expr::Literal(Literal::Uint(v, None))
49 | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
50 _ => None,
51 }
52 } 44 }
53} 45}
54 46
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;
15use syntax::ast::RangeOp; 15use syntax::ast::RangeOp;
16 16
17use crate::{ 17use crate::{
18 autoderef, 18 autoderef, consteval,
19 consteval::{self, ExprEval},
20 lower::lower_to_chalk_mutability, 19 lower::lower_to_chalk_mutability,
21 mapping::from_chalk, 20 mapping::from_chalk,
22 method_resolution, op, 21 method_resolution, op,
@@ -738,7 +737,7 @@ impl<'a> InferenceContext<'a> {
738 ); 737 );
739 738
740 let repeat_expr = &self.body.exprs[*repeat]; 739 let repeat_expr = &self.body.exprs[*repeat];
741 repeat_expr.eval_usize() 740 consteval::eval_usize(repeat_expr)
742 } 741 }
743 }; 742 };
744 743
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};
9use std::{iter, sync::Arc}; 9use std::{iter, sync::Arc};
10 10
11use base_db::CrateId; 11use base_db::CrateId;
12use chalk_ir::{ 12use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety};
13 cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety, Scalar, UintTy,
14};
15use hir_def::{ 13use hir_def::{
16 adt::StructKind, 14 adt::StructKind,
17 body::{Expander, LowerCtx}, 15 body::{Expander, LowerCtx},
@@ -31,16 +29,17 @@ use stdx::impl_from;
31use syntax::ast; 29use syntax::ast;
32 30
33use crate::{ 31use crate::{
32 consteval,
34 db::HirDatabase, 33 db::HirDatabase,
35 mapping::ToChalk, 34 mapping::ToChalk,
36 static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx, 35 static_lifetime, to_assoc_type_id, to_chalk_trait_id, to_placeholder_idx,
37 utils::{ 36 utils::{
38 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics, 37 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, Generics,
39 }, 38 },
40 AliasEq, AliasTy, Binders, BoundVar, CallableSig, ConstData, ConstValue, DebruijnIndex, DynTy, 39 AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
41 FnPointer, FnSig, FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, 40 FnSubst, ImplTraitId, Interner, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause,
42 QuantifiedWhereClause, QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, 41 QuantifiedWhereClauses, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution,
43 Substitution, TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause, 42 TraitEnvironment, TraitRef, TraitRefExt, Ty, TyBuilder, TyKind, WhereClause,
44}; 43};
45 44
46#[derive(Debug)] 45#[derive(Debug)]
@@ -176,11 +175,7 @@ impl<'a> TyLoweringContext<'a> {
176 TypeRef::Array(inner, len) => { 175 TypeRef::Array(inner, len) => {
177 let inner_ty = self.lower_ty(inner); 176 let inner_ty = self.lower_ty(inner);
178 177
179 let const_len = ConstData { 178 let const_len = consteval::usize_const(len.as_usize());
180 ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner),
181 value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: *len }),
182 }
183 .intern(&Interner);
184 179
185 TyKind::Array(inner_ty, const_len).intern(&Interner) 180 TyKind::Array(inner_ty, const_len).intern(&Interner)
186 } 181 }