@@ -23,10 +23,10 @@ mod pat;
2323mod path;
2424pub ( crate ) mod unify;
2525
26- use std:: { cell:: OnceCell , convert:: identity, iter, ops:: Index } ;
26+ use std:: { cell:: OnceCell , convert:: identity, iter, ops:: Index , sync :: LazyLock } ;
2727
2828use chalk_ir:: {
29- DebruijnIndex , Mutability , Safety , Scalar , TyKind , TypeFlags , Variance ,
29+ DebruijnIndex , Mutability , Safety , TyKind , TypeFlags , Variance ,
3030 cast:: Cast ,
3131 fold:: TypeFoldable ,
3232 interner:: HasInterner ,
@@ -195,6 +195,10 @@ pub enum InferenceTyDiagnosticSource {
195195 Signature ,
196196}
197197
198+ pub ( crate ) static ERROR_TY : LazyLock < Ty > = LazyLock :: new ( || TyKind :: Error . intern ( Interner ) ) ;
199+ pub ( crate ) static UNIT_TY : LazyLock < Ty > =
200+ LazyLock :: new ( || TyKind :: Tuple ( 0 , Substitution :: empty ( Interner ) ) . intern ( Interner ) ) ;
201+
198202#[ derive( Debug ) ]
199203pub ( crate ) struct TypeError ;
200204pub ( crate ) type InferResult < T > = Result < InferOk < T > , TypeError > ;
@@ -299,24 +303,6 @@ pub struct TypeMismatch {
299303 pub actual : Ty ,
300304}
301305
302- #[ derive( Clone , PartialEq , Eq , Debug ) ]
303- struct InternedStandardTypes {
304- unknown : Ty ,
305- bool_ : Ty ,
306- unit : Ty ,
307- never : Ty ,
308- }
309-
310- impl Default for InternedStandardTypes {
311- fn default ( ) -> Self {
312- InternedStandardTypes {
313- unknown : TyKind :: Error . intern ( Interner ) ,
314- bool_ : TyKind :: Scalar ( Scalar :: Bool ) . intern ( Interner ) ,
315- unit : TyKind :: Tuple ( 0 , Substitution :: empty ( Interner ) ) . intern ( Interner ) ,
316- never : TyKind :: Never . intern ( Interner ) ,
317- }
318- }
319- }
320306/// Represents coercing a value to a different type of value.
321307///
322308/// We transform values by following a number of `Adjust` steps in order.
@@ -475,8 +461,6 @@ pub struct InferenceResult {
475461 // Which will then mark this field.
476462 pub ( crate ) has_errors : bool ,
477463 /// Interned common types to return references to.
478- // FIXME: Move this into `InferenceContext`
479- standard_types : InternedStandardTypes ,
480464 /// Stores the types which were implicitly dereferenced in pattern binding modes.
481465 pub pat_adjustments : FxHashMap < PatId , Vec < Ty > > ,
482466 /// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
@@ -564,31 +548,31 @@ impl Index<ExprId> for InferenceResult {
564548 type Output = Ty ;
565549
566550 fn index ( & self , expr : ExprId ) -> & Ty {
567- self . type_of_expr . get ( expr) . unwrap_or ( & self . standard_types . unknown )
551+ self . type_of_expr . get ( expr) . unwrap_or_else ( || & ERROR_TY )
568552 }
569553}
570554
571555impl Index < PatId > for InferenceResult {
572556 type Output = Ty ;
573557
574558 fn index ( & self , pat : PatId ) -> & Ty {
575- self . type_of_pat . get ( pat) . unwrap_or ( & self . standard_types . unknown )
559+ self . type_of_pat . get ( pat) . unwrap_or_else ( || & ERROR_TY )
576560 }
577561}
578562
579563impl Index < ExprOrPatId > for InferenceResult {
580564 type Output = Ty ;
581565
582566 fn index ( & self , id : ExprOrPatId ) -> & Ty {
583- self . type_of_expr_or_pat ( id) . unwrap_or ( & self . standard_types . unknown )
567+ self . type_of_expr_or_pat ( id) . unwrap_or_else ( || & ERROR_TY )
584568 }
585569}
586570
587571impl Index < BindingId > for InferenceResult {
588572 type Output = Ty ;
589573
590574 fn index ( & self , b : BindingId ) -> & Ty {
591- self . type_of_binding . get ( b) . unwrap_or ( & self . standard_types . unknown )
575+ self . type_of_binding . get ( b) . unwrap_or_else ( || & ERROR_TY )
592576 }
593577}
594578
@@ -767,7 +751,6 @@ impl<'db> InferenceContext<'db> {
767751 type_of_for_iterator,
768752 type_mismatches,
769753 has_errors,
770- standard_types : _,
771754 pat_adjustments,
772755 binding_modes : _,
773756 expr_adjustments,
@@ -1022,7 +1005,7 @@ impl<'db> InferenceContext<'db> {
10221005 return_ty
10231006 }
10241007 }
1025- None => self . result . standard_types . unit . clone ( ) ,
1008+ None => UNIT_TY . clone ( ) ,
10261009 } ;
10271010
10281011 self . return_ty = self . normalize_associated_types_in ( return_ty) ;
@@ -1400,7 +1383,7 @@ impl<'db> InferenceContext<'db> {
14001383 }
14011384
14021385 fn err_ty ( & self ) -> Ty {
1403- self . result . standard_types . unknown . clone ( )
1386+ TyKind :: Error . intern ( Interner )
14041387 }
14051388
14061389 fn make_body_lifetime ( & mut self , lifetime_ref : LifetimeRefId ) -> Lifetime {
0 commit comments