@@ -71,15 +71,24 @@ impl TupleLength {
7171/// # Ordering
7272/// Ordering is based on the tuple's salsa-assigned id and not on its elements.
7373/// The id may change between runs, or when the tuple was garbage collected and recreated.
74- #[ salsa:: interned( debug) ]
74+ #[ salsa:: interned( debug, constructor=new_internal ) ]
7575#[ derive( PartialOrd , Ord ) ]
7676pub struct TupleType < ' db > {
7777 #[ returns( ref) ]
7878 pub ( crate ) tuple : TupleSpec < ' db > ,
7979}
8080
8181impl < ' db > Type < ' db > {
82- pub ( crate ) fn tuple < T > ( db : & ' db dyn Db , tuple_key : T ) -> Self
82+ pub ( crate ) fn tuple ( _db : & ' db dyn Db , tuple : Option < TupleType < ' db > > ) -> Self {
83+ let Some ( tuple) = tuple else {
84+ return Type :: Never ;
85+ } ;
86+ Self :: Tuple ( tuple)
87+ }
88+ }
89+
90+ impl < ' db > TupleType < ' db > {
91+ pub ( crate ) fn new < T > ( db : & ' db dyn Db , tuple_key : T ) -> Option < Self >
8392 where
8493 T : Borrow < TupleSpec < ' db > > + Hash + salsa:: plumbing:: interned:: Lookup < TupleSpec < ' db > > ,
8594 TupleSpec < ' db > : salsa:: plumbing:: interned:: HashEqLike < T > ,
@@ -88,7 +97,7 @@ impl<'db> Type<'db> {
8897 // possible to instantiate the tuple as a whole.
8998 let tuple = tuple_key. borrow ( ) ;
9099 if tuple. fixed_elements ( ) . any ( Type :: is_never) {
91- return Type :: Never ;
100+ return None ;
92101 }
93102
94103 // If the variable-length portion is Never, it can only be instantiated with zero elements.
@@ -98,24 +107,25 @@ impl<'db> Type<'db> {
98107 let tuple = TupleSpec :: Fixed ( FixedLengthTuple :: from_elements (
99108 tuple. prefix . iter ( ) . chain ( & tuple. suffix ) . copied ( ) ,
100109 ) ) ;
101- return Self :: Tuple ( TupleType :: new :: < _ , TupleSpec < ' db > > ( db, tuple) ) ;
110+ return Some ( TupleType :: new_internal :: < _ , TupleSpec < ' db > > ( db, tuple) ) ;
102111 }
103112 }
104113
105- Self :: Tuple ( TupleType :: new ( db, tuple_key) )
114+ Some ( TupleType :: new_internal ( db, tuple_key) )
106115 }
107- }
108116
109- impl < ' db > TupleType < ' db > {
110117 pub ( crate ) fn empty ( db : & ' db dyn Db ) -> Type < ' db > {
111- Type :: tuple ( db, TupleSpec :: from ( FixedLengthTuple :: empty ( ) ) )
118+ Type :: tuple (
119+ db,
120+ TupleType :: new ( db, TupleSpec :: from ( FixedLengthTuple :: empty ( ) ) ) ,
121+ )
112122 }
113123
114124 pub ( crate ) fn from_elements (
115125 db : & ' db dyn Db ,
116126 types : impl IntoIterator < Item = Type < ' db > > ,
117127 ) -> Type < ' db > {
118- Type :: tuple ( db, TupleSpec :: from_elements ( types) )
128+ Type :: tuple ( db, TupleType :: new ( db , TupleSpec :: from_elements ( types) ) )
119129 }
120130
121131 #[ cfg( test) ]
@@ -125,11 +135,14 @@ impl<'db> TupleType<'db> {
125135 variable : Type < ' db > ,
126136 suffix : impl IntoIterator < Item = Type < ' db > > ,
127137 ) -> Type < ' db > {
128- Type :: tuple ( db, VariableLengthTuple :: mixed ( prefix, variable, suffix) )
138+ Type :: tuple (
139+ db,
140+ TupleType :: new ( db, VariableLengthTuple :: mixed ( prefix, variable, suffix) ) ,
141+ )
129142 }
130143
131144 pub ( crate ) fn homogeneous ( db : & ' db dyn Db , element : Type < ' db > ) -> Type < ' db > {
132- Type :: tuple ( db, TupleSpec :: homogeneous ( element) )
145+ Type :: tuple ( db, TupleType :: new ( db , TupleSpec :: homogeneous ( element) ) )
133146 }
134147
135148 pub ( crate ) fn to_class_type ( self , db : & ' db dyn Db ) -> Option < ClassType < ' db > > {
@@ -149,19 +162,19 @@ impl<'db> TupleType<'db> {
149162 ///
150163 /// See [`Type::normalized`] for more details.
151164 #[ must_use]
152- pub ( crate ) fn normalized ( self , db : & ' db dyn Db ) -> Self {
165+ pub ( crate ) fn normalized ( self , db : & ' db dyn Db ) -> Option < Self > {
153166 TupleType :: new ( db, self . tuple ( db) . normalized ( db) )
154167 }
155168
156- pub ( crate ) fn materialize ( self , db : & ' db dyn Db , variance : TypeVarVariance ) -> Self {
169+ pub ( crate ) fn materialize ( self , db : & ' db dyn Db , variance : TypeVarVariance ) -> Option < Self > {
157170 TupleType :: new ( db, self . tuple ( db) . materialize ( db, variance) )
158171 }
159172
160173 pub ( crate ) fn apply_type_mapping < ' a > (
161174 self ,
162175 db : & ' db dyn Db ,
163176 type_mapping : & TypeMapping < ' a , ' db > ,
164- ) -> Self {
177+ ) -> Option < Self > {
165178 TupleType :: new ( db, self . tuple ( db) . apply_type_mapping ( db, type_mapping) )
166179 }
167180
0 commit comments