@@ -54,10 +54,10 @@ use rustc_type_ir::{
5454 AliasTyKind , TypeFoldable ,
5555 inherent:: { AdtDef , IntoKind , Region as _, SliceLike , Ty as _} ,
5656} ;
57+ use salsa:: Update ;
5758use span:: Edition ;
5859use stdx:: never;
5960use thin_vec:: ThinVec ;
60- use triomphe:: Arc ;
6161
6262use crate :: {
6363 ImplTraitId , IncorrectGenericsLenKind , PathLoweringDiagnostic , TargetFeatures ,
@@ -95,7 +95,7 @@ use cast::{CastCheck, CastError};
9595pub ( crate ) use closure:: analysis:: { CaptureKind , CapturedItem , CapturedItemWithoutTy } ;
9696
9797/// The entry point of type inference.
98- pub ( crate ) fn infer_query ( db : & dyn HirDatabase , def : DefWithBodyId ) -> Arc < InferenceResult < ' _ > > {
98+ fn infer_query ( db : & dyn HirDatabase , def : DefWithBodyId ) -> InferenceResult < ' _ > {
9999 let _p = tracing:: info_span!( "infer_query" ) . entered ( ) ;
100100 let resolver = def. resolver ( db) ;
101101 let body = db. body ( def) ;
@@ -159,17 +159,14 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
159159
160160 ctx. handle_opaque_type_uses ( ) ;
161161
162- Arc :: new ( ctx. resolve_all ( ) )
162+ ctx. resolve_all ( )
163163}
164164
165- pub ( crate ) fn infer_cycle_result (
166- db : & dyn HirDatabase ,
167- _: DefWithBodyId ,
168- ) -> Arc < InferenceResult < ' _ > > {
169- Arc :: new ( InferenceResult {
165+ fn infer_cycle_result ( db : & dyn HirDatabase , _: DefWithBodyId ) -> InferenceResult < ' _ > {
166+ InferenceResult {
170167 has_errors : true ,
171168 ..InferenceResult :: new ( Ty :: new_error ( DbInterner :: new_no_crate ( db) , ErrorGuaranteed ) )
172- } )
169+ }
173170}
174171
175172/// Binding modes inferred for patterns.
@@ -199,7 +196,7 @@ pub enum InferenceTyDiagnosticSource {
199196 Signature ,
200197}
201198
202- #[ derive( Debug , PartialEq , Eq , Clone ) ]
199+ #[ derive( Debug , PartialEq , Eq , Clone , Update ) ]
203200pub enum InferenceDiagnostic < ' db > {
204201 NoSuchField {
205202 field : ExprOrPatId ,
@@ -293,7 +290,7 @@ pub enum InferenceDiagnostic<'db> {
293290}
294291
295292/// A mismatch between an expected and an inferred type.
296- #[ derive( Clone , PartialEq , Eq , Debug , Hash ) ]
293+ #[ derive( Clone , PartialEq , Eq , Debug , Hash , Update ) ]
297294pub struct TypeMismatch < ' db > {
298295 pub expected : Ty < ' db > ,
299296 pub actual : Ty < ' db > ,
@@ -339,7 +336,7 @@ pub struct TypeMismatch<'db> {
339336/// At some point, of course, `Box` should move out of the compiler, in which
340337/// case this is analogous to transforming a struct. E.g., Box<[i32; 4]> ->
341338/// Box<[i32]> is an `Adjust::Unsize` with the target `Box<[i32]>`.
342- #[ derive( Clone , Debug , PartialEq , Eq , Hash , TypeVisitable , TypeFoldable ) ]
339+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , TypeVisitable , TypeFoldable , Update ) ]
343340pub struct Adjustment < ' db > {
344341 #[ type_visitable( ignore) ]
345342 #[ type_foldable( identity) ]
@@ -476,9 +473,10 @@ pub enum PointerCast {
476473/// When you add a field that stores types (including `Substitution` and the like), don't forget
477474/// `resolve_completely()`'ing them in `InferenceContext::resolve_all()`. Inference variables must
478475/// not appear in the final inference result.
479- #[ derive( Clone , PartialEq , Eq , Debug ) ]
476+ #[ derive( Clone , PartialEq , Eq , Debug , Update ) ]
480477pub struct InferenceResult < ' db > {
481478 /// For each method call expr, records the function it resolves to.
479+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* expr id is technically update */ ) ) ) ]
482480 method_resolutions : FxHashMap < ExprId , ( FunctionId , GenericArgs < ' db > ) > ,
483481 /// For each field access expr, records the field it resolves to.
484482 field_resolutions : FxHashMap < ExprId , Either < FieldId , TupleFieldId > > ,
@@ -489,15 +487,20 @@ pub struct InferenceResult<'db> {
489487 /// Whenever a tuple field expression access a tuple field, we allocate a tuple id in
490488 /// [`InferenceContext`] and store the tuples substitution there. This map is the reverse of
491489 /// that which allows us to resolve a [`TupleFieldId`]s type.
492- tuple_field_access_types : FxHashMap < TupleId , Tys < ' db > > ,
490+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* thinvec is technically update */ ) ) ) ]
491+ tuple_field_access_types : ThinVec < Tys < ' db > > ,
493492
493+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* expr id is technically update */ ) ) ) ]
494494 pub ( crate ) type_of_expr : ArenaMap < ExprId , Ty < ' db > > ,
495495 /// For each pattern record the type it resolves to.
496496 ///
497497 /// **Note**: When a pattern type is resolved it may still contain
498498 /// unresolved or missing subpatterns or subpatterns of mismatched types.
499+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* pat id is technically update */ ) ) ) ]
499500 pub ( crate ) type_of_pat : ArenaMap < PatId , Ty < ' db > > ,
501+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* binding id is technically update */ ) ) ) ]
500502 pub ( crate ) type_of_binding : ArenaMap < BindingId , Ty < ' db > > ,
503+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* type ref id is technically update */ ) ) ) ]
501504 pub ( crate ) type_of_type_placeholder : FxHashMap < TypeRefId , Ty < ' db > > ,
502505 pub ( crate ) type_of_opaque : FxHashMap < InternedOpaqueTyId , Ty < ' db > > ,
503506
@@ -508,14 +511,17 @@ pub struct InferenceResult<'db> {
508511 // Which will then mark this field.
509512 pub ( crate ) has_errors : bool ,
510513 /// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
514+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* thinvec is technically update */ ) ) ) ]
511515 diagnostics : ThinVec < InferenceDiagnostic < ' db > > ,
512516
513517 /// Interned `Error` type to return references to.
514518 // FIXME: Remove this.
515519 error_ty : Ty < ' db > ,
516520
521+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* expr id is technically update */ ) ) ) ]
517522 pub ( crate ) expr_adjustments : FxHashMap < ExprId , Box < [ Adjustment < ' db > ] > > ,
518523 /// Stores the types which were implicitly dereferenced in pattern binding modes.
524+ #[ update( unsafe ( with( crate :: utils:: unsafe_update_eq /* pat id is technically update */ ) ) ) ]
519525 pub ( crate ) pat_adjustments : FxHashMap < PatId , Vec < Ty < ' db > > > ,
520526 /// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
521527 ///
@@ -539,6 +545,14 @@ pub struct InferenceResult<'db> {
539545 pub ( crate ) coercion_casts : FxHashSet < ExprId > ,
540546}
541547
548+ #[ salsa:: tracked]
549+ impl < ' db > InferenceResult < ' db > {
550+ #[ salsa:: tracked( returns( ref) , cycle_result = infer_cycle_result) ]
551+ pub fn for_body ( db : & ' db dyn HirDatabase , def : DefWithBodyId ) -> InferenceResult < ' db > {
552+ infer_query ( db, def)
553+ }
554+ }
555+
542556impl < ' db > InferenceResult < ' db > {
543557 fn new ( error_ty : Ty < ' db > ) -> Self {
544558 Self {
@@ -672,7 +686,7 @@ impl<'db> InferenceResult<'db> {
672686 }
673687
674688 pub fn tuple_field_access_type ( & self , id : TupleId ) -> Tys < ' db > {
675- self . tuple_field_access_types [ & id ]
689+ self . tuple_field_access_types [ id . 0 as usize ]
676690 }
677691
678692 pub fn pat_adjustment ( & self , id : PatId ) -> Option < & [ Ty < ' db > ] > {
@@ -1135,9 +1149,8 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
11351149 pat_adjustments. shrink_to_fit ( ) ;
11361150 result. tuple_field_access_types = tuple_field_accesses_rev
11371151 . into_iter ( )
1138- . enumerate ( )
1139- . map ( |( idx, subst) | ( TupleId ( idx as u32 ) , table. resolve_completely ( subst) ) )
1140- . inspect ( |( _, subst) | {
1152+ . map ( |subst| table. resolve_completely ( subst) )
1153+ . inspect ( |subst| {
11411154 * has_errors = * has_errors || subst. iter ( ) . any ( |ty| ty. references_non_lt_error ( ) ) ;
11421155 } )
11431156 . collect ( ) ;
0 commit comments