33use std:: iter:: { repeat, repeat_with} ;
44use std:: { mem, sync:: Arc } ;
55
6- use chalk_ir:: { cast:: Cast , fold:: Shift , Mutability , TyVariableKind } ;
6+ use chalk_ir:: { cast:: Cast , fold:: Shift , ConstData , Mutability , TyVariableKind } ;
77use hir_def:: {
88 expr:: { Array , BinaryOp , Expr , ExprId , Literal , Statement , UnaryOp } ,
99 path:: { GenericArg , GenericArgs } ,
@@ -15,15 +15,17 @@ use stdx::always;
1515use syntax:: ast:: RangeOp ;
1616
1717use crate :: {
18- autoderef, dummy_usize_const,
18+ autoderef,
19+ consts:: ConstScalar ,
20+ dummy_usize_const,
1921 lower:: lower_to_chalk_mutability,
2022 mapping:: from_chalk,
2123 method_resolution, op,
2224 primitive:: { self , UintTy } ,
2325 static_lifetime, to_chalk_trait_id,
2426 traits:: FnTrait ,
2527 utils:: { generics, Generics } ,
26- AdtId , Binders , CallableDefId , FnPointer , FnSig , FnSubst , InEnvironment , Interner ,
28+ AdtId , Binders , CallableDefId , ConstValue , FnPointer , FnSig , FnSubst , InEnvironment , Interner ,
2729 ProjectionTyExt , Rawness , Scalar , Substitution , TraitRef , Ty , TyBuilder , TyExt , TyKind ,
2830} ;
2931
@@ -717,11 +719,12 @@ impl<'a> InferenceContext<'a> {
717719 _ => self . table . new_type_var ( ) ,
718720 } ;
719721
720- match array {
722+ let len = match array {
721723 Array :: ElementList ( items) => {
722724 for expr in items. iter ( ) {
723725 self . infer_expr_coerce ( * expr, & Expectation :: has_type ( elem_ty. clone ( ) ) ) ;
724726 }
727+ Some ( items. len ( ) )
725728 }
726729 Array :: Repeat { initializer, repeat } => {
727730 self . infer_expr_coerce (
@@ -734,10 +737,20 @@ impl<'a> InferenceContext<'a> {
734737 TyKind :: Scalar ( Scalar :: Uint ( UintTy :: Usize ) ) . intern ( & Interner ) ,
735738 ) ,
736739 ) ;
740+ // FIXME: support length for Repeat array expressions
741+ None
737742 }
738- }
743+ } ;
739744
740- TyKind :: Array ( elem_ty, dummy_usize_const ( ) ) . intern ( & Interner )
745+ let cd = ConstData {
746+ ty : TyKind :: Scalar ( Scalar :: Uint ( UintTy :: Usize ) ) . intern ( & Interner ) ,
747+ value : ConstValue :: Concrete ( chalk_ir:: ConcreteConst {
748+ interned : len
749+ . map ( |len| ConstScalar :: Usize ( len) )
750+ . unwrap_or ( ConstScalar :: Unknown ) ,
751+ } ) ,
752+ } ;
753+ TyKind :: Array ( elem_ty, cd. intern ( & Interner ) ) . intern ( & Interner )
741754 }
742755 Expr :: Literal ( lit) => match lit {
743756 Literal :: Bool ( ..) => TyKind :: Scalar ( Scalar :: Bool ) . intern ( & Interner ) ,
@@ -747,6 +760,7 @@ impl<'a> InferenceContext<'a> {
747760 }
748761 Literal :: ByteString ( ..) => {
749762 let byte_type = TyKind :: Scalar ( Scalar :: Uint ( UintTy :: U8 ) ) . intern ( & Interner ) ;
763+
750764 let array_type =
751765 TyKind :: Array ( byte_type, dummy_usize_const ( ) ) . intern ( & Interner ) ;
752766 TyKind :: Ref ( Mutability :: Not , static_lifetime ( ) , array_type) . intern ( & Interner )
0 commit comments