diff options
-rw-r--r-- | crates/hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/type_ref.rs | 8 | ||||
-rw-r--r-- | crates/hir_ty/src/consteval.rs | 24 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 5 | ||||
-rw-r--r-- | 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::{ | |||
53 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | 53 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; |
54 | use hir_ty::{ | 54 | use hir_ty::{ |
55 | autoderef, | 55 | autoderef, |
56 | consteval::ConstExtension, | 56 | consteval::ConstExt, |
57 | could_unify, | 57 | could_unify, |
58 | method_resolution::{self, def_crates, TyFingerprint}, | 58 | method_resolution::{self, def_crates, TyFingerprint}, |
59 | primitive::UintTy, | 59 | 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 { | |||
336 | } | 336 | } |
337 | 337 | ||
338 | impl ConstScalar { | 338 | impl ConstScalar { |
339 | /// Gets a target usize out of the ConstScalar | ||
340 | pub fn as_usize(&self) -> Option<u64> { | ||
341 | match self { | ||
342 | &ConstScalar::Usize(us) => Some(us), | ||
343 | _ => None, | ||
344 | } | ||
345 | } | ||
346 | |||
339 | // FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this | 347 | // FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this |
340 | // parse stage. | 348 | // parse stage. |
341 | fn usize_from_literal_expr(expr: ast::Expr) -> ConstScalar { | 349 | 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::{ | |||
11 | use crate::{Const, ConstData, ConstValue, Interner, TyKind}; | 11 | use crate::{Const, ConstData, ConstValue, Interner, TyKind}; |
12 | 12 | ||
13 | /// Extension trait for [`Const`] | 13 | /// Extension trait for [`Const`] |
14 | pub trait ConstExtension { | 14 | pub 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 | ||
19 | impl ConstExtension for Const { | 19 | impl 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 |
39 | pub trait ExprEval { | 39 | pub 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, | |
44 | impl 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; | |||
15 | use syntax::ast::RangeOp; | 15 | use syntax::ast::RangeOp; |
16 | 16 | ||
17 | use crate::{ | 17 | use 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}; | |||
9 | use std::{iter, sync::Arc}; | 9 | use std::{iter, sync::Arc}; |
10 | 10 | ||
11 | use base_db::CrateId; | 11 | use base_db::CrateId; |
12 | use chalk_ir::{ | 12 | use chalk_ir::{cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety}; |
13 | cast::Cast, fold::Shift, interner::HasInterner, Mutability, Safety, Scalar, UintTy, | ||
14 | }; | ||
15 | use hir_def::{ | 13 | use hir_def::{ |
16 | adt::StructKind, | 14 | adt::StructKind, |
17 | body::{Expander, LowerCtx}, | 15 | body::{Expander, LowerCtx}, |
@@ -31,16 +29,17 @@ use stdx::impl_from; | |||
31 | use syntax::ast; | 29 | use syntax::ast; |
32 | 30 | ||
33 | use crate::{ | 31 | use 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 | } |