@@ -56,6 +56,7 @@ use rustc_type_ir::{
5656} ;
5757use span:: Edition ;
5858use stdx:: never;
59+ use thin_vec:: ThinVec ;
5960use triomphe:: Arc ;
6061
6162use crate :: {
@@ -489,26 +490,31 @@ pub struct InferenceResult<'db> {
489490 /// [`InferenceContext`] and store the tuples substitution there. This map is the reverse of
490491 /// that which allows us to resolve a [`TupleFieldId`]s type.
491492 tuple_field_access_types : FxHashMap < TupleId , Tys < ' db > > ,
492- /// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
493- diagnostics : Vec < InferenceDiagnostic < ' db > > ,
493+
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.
499499 pub ( crate ) type_of_pat : ArenaMap < PatId , Ty < ' db > > ,
500500 pub ( crate ) type_of_binding : ArenaMap < BindingId , Ty < ' db > > ,
501- pub ( crate ) type_of_type_placeholder : ArenaMap < TypeRefId , Ty < ' db > > ,
501+ pub ( crate ) type_of_type_placeholder : FxHashMap < TypeRefId , Ty < ' db > > ,
502502 pub ( crate ) type_of_opaque : FxHashMap < InternedOpaqueTyId , Ty < ' db > > ,
503- pub ( crate ) type_mismatches : FxHashMap < ExprOrPatId , TypeMismatch < ' db > > ,
503+
504+ pub ( crate ) type_mismatches : Option < Box < FxHashMap < ExprOrPatId , TypeMismatch < ' db > > > > ,
504505 /// Whether there are any type-mismatching errors in the result.
505506 // FIXME: This isn't as useful as initially thought due to us falling back placeholders to
506507 // `TyKind::Error`.
507508 // Which will then mark this field.
508509 pub ( crate ) has_errors : bool ,
510+ /// During inference this field is empty and [`InferenceContext::diagnostics`] is filled instead.
511+ diagnostics : ThinVec < InferenceDiagnostic < ' db > > ,
512+
509513 /// Interned `Error` type to return references to.
510514 // FIXME: Remove this.
511515 error_ty : Ty < ' db > ,
516+
517+ pub ( crate ) expr_adjustments : FxHashMap < ExprId , Box < [ Adjustment < ' db > ] > > ,
512518 /// Stores the types which were implicitly dereferenced in pattern binding modes.
513519 pub ( crate ) pat_adjustments : FxHashMap < PatId , Vec < Ty < ' db > > > ,
514520 /// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
@@ -525,10 +531,11 @@ pub struct InferenceResult<'db> {
525531 /// ```
526532 /// the first `rest` has implicit `ref` binding mode, but the second `rest` binding mode is `move`.
527533 pub ( crate ) binding_modes : ArenaMap < PatId , BindingMode > ,
528- pub ( crate ) expr_adjustments : FxHashMap < ExprId , Box < [ Adjustment < ' db > ] > > ,
534+
529535 pub ( crate ) closure_info : FxHashMap < InternedClosureId , ( Vec < CapturedItem < ' db > > , FnTrait ) > ,
530536 // FIXME: remove this field
531537 pub mutated_bindings_in_closure : FxHashSet < BindingId > ,
538+
532539 pub ( crate ) coercion_casts : FxHashSet < ExprId > ,
533540}
534541
@@ -595,25 +602,31 @@ impl<'db> InferenceResult<'db> {
595602 }
596603 }
597604 pub fn type_mismatch_for_expr ( & self , expr : ExprId ) -> Option < & TypeMismatch < ' db > > {
598- self . type_mismatches . get ( & expr. into ( ) )
605+ self . type_mismatches . as_deref ( ) ? . get ( & expr. into ( ) )
599606 }
600607 pub fn type_mismatch_for_pat ( & self , pat : PatId ) -> Option < & TypeMismatch < ' db > > {
601- self . type_mismatches . get ( & pat. into ( ) )
608+ self . type_mismatches . as_deref ( ) ? . get ( & pat. into ( ) )
602609 }
603610 pub fn type_mismatches ( & self ) -> impl Iterator < Item = ( ExprOrPatId , & TypeMismatch < ' db > ) > {
604- self . type_mismatches . iter ( ) . map ( |( expr_or_pat, mismatch) | ( * expr_or_pat, mismatch) )
611+ self . type_mismatches
612+ . as_deref ( )
613+ . into_iter ( )
614+ . flatten ( )
615+ . map ( |( expr_or_pat, mismatch) | ( * expr_or_pat, mismatch) )
605616 }
606617 pub fn expr_type_mismatches ( & self ) -> impl Iterator < Item = ( ExprId , & TypeMismatch < ' db > ) > {
607- self . type_mismatches . iter ( ) . filter_map ( |( expr_or_pat, mismatch) | match * expr_or_pat {
608- ExprOrPatId :: ExprId ( expr) => Some ( ( expr, mismatch) ) ,
609- _ => None ,
610- } )
618+ self . type_mismatches . as_deref ( ) . into_iter ( ) . flatten ( ) . filter_map (
619+ |( expr_or_pat, mismatch) | match * expr_or_pat {
620+ ExprOrPatId :: ExprId ( expr) => Some ( ( expr, mismatch) ) ,
621+ _ => None ,
622+ } ,
623+ )
611624 }
612625 pub fn placeholder_types ( & self ) -> impl Iterator < Item = ( TypeRefId , & Ty < ' db > ) > {
613- self . type_of_type_placeholder . iter ( )
626+ self . type_of_type_placeholder . iter ( ) . map ( | ( & type_ref , ty ) | ( type_ref , ty ) )
614627 }
615628 pub fn type_of_type_placeholder ( & self , type_ref : TypeRefId ) -> Option < Ty < ' db > > {
616- self . type_of_type_placeholder . get ( type_ref) . copied ( )
629+ self . type_of_type_placeholder . get ( & type_ref) . copied ( )
617630 }
618631 pub fn closure_info ( & self , closure : InternedClosureId ) -> & ( Vec < CapturedItem < ' db > > , FnTrait ) {
619632 self . closure_info . get ( & closure) . unwrap ( )
@@ -1063,13 +1076,14 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
10631076 type_of_type_placeholder. shrink_to_fit ( ) ;
10641077 type_of_opaque. shrink_to_fit ( ) ;
10651078
1066- * has_errors |= !type_mismatches. is_empty ( ) ;
1067-
1068- for mismatch in ( * type_mismatches) . values_mut ( ) {
1069- mismatch. expected = table. resolve_completely ( mismatch. expected ) ;
1070- mismatch. actual = table. resolve_completely ( mismatch. actual ) ;
1079+ if let Some ( type_mismatches) = type_mismatches {
1080+ * has_errors = true ;
1081+ for mismatch in type_mismatches. values_mut ( ) {
1082+ mismatch. expected = table. resolve_completely ( mismatch. expected ) ;
1083+ mismatch. actual = table. resolve_completely ( mismatch. actual ) ;
1084+ }
1085+ type_mismatches. shrink_to_fit ( ) ;
10711086 }
1072- type_mismatches. shrink_to_fit ( ) ;
10731087 diagnostics. retain_mut ( |diagnostic| {
10741088 use InferenceDiagnostic :: * ;
10751089 match diagnostic {
@@ -1520,7 +1534,10 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
15201534 ) -> Result < ( ) , ( ) > {
15211535 let result = self . demand_eqtype_fixme_no_diag ( expected, actual) ;
15221536 if result. is_err ( ) {
1523- self . result . type_mismatches . insert ( id, TypeMismatch { expected, actual } ) ;
1537+ self . result
1538+ . type_mismatches
1539+ . get_or_insert_default ( )
1540+ . insert ( id, TypeMismatch { expected, actual } ) ;
15241541 }
15251542 result
15261543 }
0 commit comments