@@ -31,6 +31,7 @@ use crate::{
3131 PatId , RecordFieldPat , RecordSpread , Statement ,
3232 } ,
3333 nameres:: { DefMap , block_def_map} ,
34+ signatures:: VariantFields ,
3435 type_ref:: { LifetimeRef , LifetimeRefId , PathId , TypeRef , TypeRefId } ,
3536} ;
3637
@@ -95,14 +96,16 @@ pub type LifetimeSource = InFile<LifetimePtr>;
9596/// Used by signature/body inference to determine the expected type for each
9697/// const expression root.
9798#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
98- pub enum ConstExprOrigin {
99+ pub enum RootExprOrigin {
99100 /// Array length expression: `[T; <expr>]` — expected type is `usize`.
100101 ArrayLength ,
101102 /// Const parameter default value: `const N: usize = <expr>`.
102103 ConstParam ( crate :: hir:: generics:: LocalTypeOrConstParamId ) ,
103104 /// Const generic argument in a path: `SomeType::<{ <expr> }>` or `some_fn::<{ <expr> }>()`.
104105 /// Determining the expected type requires path resolution, so it is deferred.
105106 GenericArgsPath ,
107+ /// The root expression of a body.
108+ BodyRoot ,
106109}
107110
108111// We split the store into types-only and expressions, because most stores (e.g. generics)
@@ -125,11 +128,8 @@ struct ExpressionOnlyStore {
125128 /// to variables and have hygiene (some refer to items, we don't know at this stage).
126129 ident_hygiene : FxHashMap < ExprOrPatId , HygieneId > ,
127130
128- /// Maps const expression roots to their origin.
129- ///
130- /// Populated during lowering. Used by signature inference to determine expected types,
131- /// and by `signature_const_expr_roots()` to enumerate roots for scope computation.
132- const_expr_origins : ThinVec < ( ExprId , ConstExprOrigin ) > ,
131+ /// Maps expression roots to their origin.
132+ expr_roots : SmallVec < [ ( ExprId , RootExprOrigin ) ; 1 ] > ,
133133}
134134
135135#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -243,7 +243,7 @@ pub struct ExpressionStoreBuilder {
243243 pub types : Arena < TypeRef > ,
244244 block_scopes : Vec < BlockId > ,
245245 ident_hygiene : FxHashMap < ExprOrPatId , HygieneId > ,
246- pub const_expr_origins : Option < ThinVec < ( ExprId , ConstExprOrigin ) > > ,
246+ pub inference_roots : Option < SmallVec < [ ( ExprId , RootExprOrigin ) ; 1 ] > > ,
247247
248248 // AST expressions can create patterns in destructuring assignments. Therefore, `ExprSource` can also map
249249 // to `PatId`, and `PatId` can also map to `ExprSource` (the other way around is unaffected).
@@ -315,7 +315,7 @@ impl ExpressionStoreBuilder {
315315 mut bindings,
316316 mut binding_owners,
317317 mut ident_hygiene,
318- mut const_expr_origins ,
318+ inference_roots : mut expr_roots ,
319319 mut types,
320320 mut lifetimes,
321321
@@ -375,7 +375,7 @@ impl ExpressionStoreBuilder {
375375
376376 let store = {
377377 let expr_only = if has_exprs {
378- if let Some ( const_expr_origins) = & mut const_expr_origins {
378+ if let Some ( const_expr_origins) = & mut expr_roots {
379379 const_expr_origins. shrink_to_fit ( ) ;
380380 }
381381 Some ( Box :: new ( ExpressionOnlyStore {
@@ -386,7 +386,7 @@ impl ExpressionStoreBuilder {
386386 binding_owners,
387387 block_scopes : block_scopes. into_boxed_slice ( ) ,
388388 ident_hygiene,
389- const_expr_origins : const_expr_origins . unwrap_or_default ( ) ,
389+ expr_roots : expr_roots . unwrap_or_default ( ) ,
390390 } ) )
391391 } else {
392392 None
@@ -448,6 +448,9 @@ impl ExpressionStore {
448448 }
449449 }
450450 ExpressionStoreOwnerId :: Body ( body) => & Body :: of ( db, body) . store ,
451+ ExpressionStoreOwnerId :: VariantFields ( variant_id) => {
452+ & VariantFields :: of ( db, variant_id) . store
453+ }
451454 }
452455 }
453456
@@ -505,30 +508,27 @@ impl ExpressionStore {
505508 let ( store, sm) = Body :: with_source_map ( db, body) ;
506509 ( & store. store , & sm. store )
507510 }
511+ ExpressionStoreOwnerId :: VariantFields ( variant_id) => {
512+ let ( store, sm) = VariantFields :: with_source_map ( db, variant_id) ;
513+ ( & store. store , sm)
514+ }
508515 }
509516 }
510517
511- /// Returns all const expression root `ExprId`s found in this store.
512- ///
513- /// Used to compute expression scopes for signature stores.
514- pub fn signature_const_expr_roots ( & self ) -> impl Iterator < Item = ExprId > {
518+ /// Returns all expression root `ExprId`s found in this store.
519+ pub fn expr_roots ( & self ) -> impl Iterator < Item = ExprId > {
515520 self . const_expr_origins ( ) . iter ( ) . map ( |& ( id, _) | id)
516521 }
517522
518523 /// Like [`Self::signature_const_expr_roots`], but also returns the origin
519- /// of each const expression.
520- ///
521- /// This is used by signature inference to determine the expected type for
522- /// each root expression.
523- pub fn signature_const_expr_roots_with_origins (
524- & self ,
525- ) -> impl Iterator < Item = ( ExprId , ConstExprOrigin ) > {
524+ /// of each expression.
525+ pub fn expr_roots_with_origins ( & self ) -> impl Iterator < Item = ( ExprId , RootExprOrigin ) > {
526526 self . const_expr_origins ( ) . iter ( ) . map ( |& ( id, origin) | ( id, origin) )
527527 }
528528
529529 /// Returns the map of const expression roots to their origins.
530- pub fn const_expr_origins ( & self ) -> & [ ( ExprId , ConstExprOrigin ) ] {
531- self . expr_only . as_ref ( ) . map_or ( & [ ] , |it| & it. const_expr_origins )
530+ pub fn const_expr_origins ( & self ) -> & [ ( ExprId , RootExprOrigin ) ] {
531+ self . expr_only . as_ref ( ) . map_or ( & [ ] , |it| & it. expr_roots )
532532 }
533533
534534 /// Returns an iterator over all block expressions in this store that define inner items.
0 commit comments