diff options
Diffstat (limited to 'crates/hir_ty/src/infer/expr.rs')
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 41 |
1 files changed, 8 insertions, 33 deletions
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 0b36ac861..04fc2f12b 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -1,18 +1,13 @@ | |||
1 | //! Type inference for expressions. | 1 | //! Type inference for expressions. |
2 | 2 | ||
3 | use std::{ | 3 | use std::iter::{repeat, repeat_with}; |
4 | convert::TryInto, | ||
5 | iter::{repeat, repeat_with}, | ||
6 | }; | ||
7 | use std::{mem, sync::Arc}; | 4 | use std::{mem, sync::Arc}; |
8 | 5 | ||
9 | use chalk_ir::{cast::Cast, fold::Shift, ConstData, Mutability, TyVariableKind}; | 6 | use chalk_ir::{cast::Cast, fold::Shift, Mutability, TyVariableKind}; |
10 | use hir_def::{ | 7 | use hir_def::{ |
11 | builtin_type::BuiltinUint, | ||
12 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
13 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
14 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
15 | type_ref::ConstScalar, | ||
16 | AssocContainerId, FieldId, Lookup, | 11 | AssocContainerId, FieldId, Lookup, |
17 | }; | 12 | }; |
18 | use hir_expand::name::{name, Name}; | 13 | use hir_expand::name::{name, Name}; |
@@ -21,6 +16,7 @@ use syntax::ast::RangeOp; | |||
21 | 16 | ||
22 | use crate::{ | 17 | use crate::{ |
23 | autoderef, | 18 | autoderef, |
19 | consteval::{self, ExprEval}, | ||
24 | lower::lower_to_chalk_mutability, | 20 | lower::lower_to_chalk_mutability, |
25 | mapping::from_chalk, | 21 | mapping::from_chalk, |
26 | method_resolution, op, | 22 | method_resolution, op, |
@@ -28,9 +24,8 @@ use crate::{ | |||
28 | static_lifetime, to_chalk_trait_id, | 24 | static_lifetime, to_chalk_trait_id, |
29 | traits::FnTrait, | 25 | traits::FnTrait, |
30 | utils::{generics, Generics}, | 26 | utils::{generics, Generics}, |
31 | AdtId, Binders, CallableDefId, ConcreteConst, ConstValue, FnPointer, FnSig, FnSubst, | 27 | AdtId, Binders, CallableDefId, FnPointer, FnSig, FnSubst, InEnvironment, Interner, |
32 | InEnvironment, Interner, ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, | 28 | ProjectionTyExt, Rawness, Scalar, Substitution, TraitRef, Ty, TyBuilder, TyExt, TyKind, |
33 | TyBuilder, TyExt, TyKind, | ||
34 | }; | 29 | }; |
35 | 30 | ||
36 | use super::{ | 31 | use super::{ |
@@ -743,25 +738,11 @@ impl<'a> InferenceContext<'a> { | |||
743 | ); | 738 | ); |
744 | 739 | ||
745 | let repeat_expr = &self.body.exprs[*repeat]; | 740 | let repeat_expr = &self.body.exprs[*repeat]; |
746 | match repeat_expr { | 741 | repeat_expr.eval_usize() |
747 | Expr::Literal(Literal::Uint(v, None)) | ||
748 | | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => { | ||
749 | (*v).try_into().ok() | ||
750 | } | ||
751 | _ => None, | ||
752 | } | ||
753 | } | 742 | } |
754 | }; | 743 | }; |
755 | 744 | ||
756 | let cd = ConstData { | 745 | TyKind::Array(elem_ty, consteval::usize_const(len)).intern(&Interner) |
757 | ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner), | ||
758 | value: ConstValue::Concrete(chalk_ir::ConcreteConst { | ||
759 | interned: len | ||
760 | .map(|len| ConstScalar::Usize(len)) | ||
761 | .unwrap_or(ConstScalar::Unknown), | ||
762 | }), | ||
763 | }; | ||
764 | TyKind::Array(elem_ty, cd.intern(&Interner)).intern(&Interner) | ||
765 | } | 746 | } |
766 | Expr::Literal(lit) => match lit { | 747 | Expr::Literal(lit) => match lit { |
767 | Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), | 748 | Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), |
@@ -772,13 +753,7 @@ impl<'a> InferenceContext<'a> { | |||
772 | Literal::ByteString(bs) => { | 753 | Literal::ByteString(bs) => { |
773 | let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); | 754 | let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); |
774 | 755 | ||
775 | let len = ConstData { | 756 | let len = consteval::usize_const(Some(bs.len() as u64)); |
776 | ty: TyKind::Scalar(Scalar::Uint(UintTy::Usize)).intern(&Interner), | ||
777 | value: ConstValue::Concrete(ConcreteConst { | ||
778 | interned: ConstScalar::Usize(bs.len() as u64), | ||
779 | }), | ||
780 | } | ||
781 | .intern(&Interner); | ||
782 | 757 | ||
783 | let array_type = TyKind::Array(byte_type, len).intern(&Interner); | 758 | let array_type = TyKind::Array(byte_type, len).intern(&Interner); |
784 | TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner) | 759 | TyKind::Ref(Mutability::Not, static_lifetime(), array_type).intern(&Interner) |