@@ -14,12 +14,9 @@ pub(super) use synthesized_protocol::SynthesizedProtocolType;
1414impl < ' db > Type < ' db > {
1515 pub ( crate ) fn instance ( db : & ' db dyn Db , class : ClassType < ' db > ) -> Self {
1616 if class. class_literal ( db) . 0 . is_protocol ( db) {
17- Self :: ProtocolInstance ( ProtocolInstanceType ( Protocol :: FromClass ( class) ) )
17+ Self :: ProtocolInstance ( ProtocolInstanceType :: from_class ( class) )
1818 } else {
19- Self :: NominalInstance ( NominalInstanceType {
20- class,
21- _phantom : PhantomData ,
22- } )
19+ Self :: NominalInstance ( NominalInstanceType :: from_class ( class) )
2320 }
2421 }
2522
@@ -34,9 +31,9 @@ impl<'db> Type<'db> {
3431 where
3532 M : IntoIterator < Item = ( & ' a str , Type < ' db > ) > ,
3633 {
37- Self :: ProtocolInstance ( ProtocolInstanceType ( Protocol :: Synthesized (
34+ Self :: ProtocolInstance ( ProtocolInstanceType :: synthesized (
3835 SynthesizedProtocolType :: new ( db, ProtocolInterface :: with_members ( db, members) ) ,
39- ) ) )
36+ ) )
4037 }
4138
4239 /// Return `true` if `self` conforms to the interface described by `protocol`.
@@ -51,7 +48,7 @@ impl<'db> Type<'db> {
5148 // TODO: this should consider the types of the protocol members
5249 // as well as whether each member *exists* on `self`.
5350 protocol
54- . 0
51+ . inner
5552 . interface ( db)
5653 . members ( db)
5754 . all ( |member| !self . member ( db, member. name ( ) ) . symbol . is_unbound ( ) )
@@ -69,6 +66,15 @@ pub struct NominalInstanceType<'db> {
6966}
7067
7168impl < ' db > NominalInstanceType < ' db > {
69+ // Keep this method private, so that the only way of constructing `NominalInstanceType`
70+ // instances is through the `Type::instance` constructor function.
71+ fn from_class ( class : ClassType < ' db > ) -> Self {
72+ Self {
73+ class,
74+ _phantom : PhantomData ,
75+ }
76+ }
77+
7278 pub ( super ) fn is_subtype_of ( self , db : & ' db dyn Db , other : Self ) -> bool {
7379 // N.B. The subclass relation is fully static
7480 self . class . is_subclass_of ( db, other. class )
@@ -131,10 +137,7 @@ impl<'db> NominalInstanceType<'db> {
131137 db : & ' db dyn Db ,
132138 type_mapping : TypeMapping < ' a , ' db > ,
133139 ) -> Self {
134- Self {
135- class : self . class . apply_type_mapping ( db, type_mapping) ,
136- _phantom : PhantomData ,
137- }
140+ Self :: from_class ( self . class . apply_type_mapping ( db, type_mapping) )
138141 }
139142
140143 pub ( super ) fn find_legacy_typevars (
@@ -155,21 +158,37 @@ impl<'db> From<NominalInstanceType<'db>> for Type<'db> {
155158/// A `ProtocolInstanceType` represents the set of all possible runtime objects
156159/// that conform to the interface described by a certain protocol.
157160#[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash , PartialOrd , Ord , salsa:: Update ) ]
158- pub struct ProtocolInstanceType < ' db > (
161+ pub struct ProtocolInstanceType < ' db > {
162+ pub ( super ) inner : Protocol < ' db > ,
163+
159164 // Keep the inner field here private,
160165 // so that the only way of constructing `ProtocolInstanceType` instances
161166 // is through the `Type::instance` constructor function.
162- Protocol < ' db > ,
163- ) ;
167+ _phantom : PhantomData < ( ) > ,
168+ }
164169
165170impl < ' db > ProtocolInstanceType < ' db > {
166- pub ( super ) fn inner ( self ) -> Protocol < ' db > {
167- self . 0
171+ // Keep this method private, so that the only way of constructing `ProtocolInstanceType`
172+ // instances is through the `Type::instance` constructor function.
173+ fn from_class ( class : ClassType < ' db > ) -> Self {
174+ Self {
175+ inner : Protocol :: FromClass ( class) ,
176+ _phantom : PhantomData ,
177+ }
178+ }
179+
180+ // Keep this method private, so that the only way of constructing `ProtocolInstanceType`
181+ // instances is through the `Type::instance` constructor function.
182+ fn synthesized ( synthesized : SynthesizedProtocolType < ' db > ) -> Self {
183+ Self {
184+ inner : Protocol :: Synthesized ( synthesized) ,
185+ _phantom : PhantomData ,
186+ }
168187 }
169188
170189 /// Return the meta-type of this protocol-instance type.
171190 pub ( super ) fn to_meta_type ( self , db : & ' db dyn Db ) -> Type < ' db > {
172- match self . 0 {
191+ match self . inner {
173192 Protocol :: FromClass ( class) => SubclassOfType :: from ( db, class) ,
174193
175194 // TODO: we can and should do better here.
@@ -197,35 +216,35 @@ impl<'db> ProtocolInstanceType<'db> {
197216 if object. satisfies_protocol ( db, self ) {
198217 return object;
199218 }
200- match self . 0 {
201- Protocol :: FromClass ( _) => Type :: ProtocolInstance ( Self ( Protocol :: Synthesized (
202- SynthesizedProtocolType :: new ( db, self . 0 . interface ( db) ) ,
203- ) ) ) ,
219+ match self . inner {
220+ Protocol :: FromClass ( _) => Type :: ProtocolInstance ( Self :: synthesized (
221+ SynthesizedProtocolType :: new ( db, self . inner . interface ( db) ) ,
222+ ) ) ,
204223 Protocol :: Synthesized ( _) => Type :: ProtocolInstance ( self ) ,
205224 }
206225 }
207226
208227 /// Replace references to `class` with a self-reference marker
209228 pub ( super ) fn replace_self_reference ( self , db : & ' db dyn Db , class : ClassLiteral < ' db > ) -> Self {
210- match self . 0 {
229+ match self . inner {
211230 Protocol :: FromClass ( class_type) if class_type. class_literal ( db) . 0 == class => {
212- ProtocolInstanceType ( Protocol :: Synthesized ( SynthesizedProtocolType :: new (
231+ ProtocolInstanceType :: synthesized ( SynthesizedProtocolType :: new (
213232 db,
214233 ProtocolInterface :: SelfReference ,
215- ) ) )
234+ ) )
216235 }
217236 _ => self ,
218237 }
219238 }
220239
221240 /// Return `true` if any of the members of this protocol type contain any `Todo` types.
222241 pub ( super ) fn contains_todo ( self , db : & ' db dyn Db ) -> bool {
223- self . 0 . interface ( db) . contains_todo ( db)
242+ self . inner . interface ( db) . contains_todo ( db)
224243 }
225244
226245 /// Return `true` if this protocol type is fully static.
227246 pub ( super ) fn is_fully_static ( self , db : & ' db dyn Db ) -> bool {
228- self . 0 . interface ( db) . is_fully_static ( db)
247+ self . inner . interface ( db) . is_fully_static ( db)
229248 }
230249
231250 /// Return `true` if this protocol type is a subtype of the protocol `other`.
@@ -238,9 +257,9 @@ impl<'db> ProtocolInstanceType<'db> {
238257 /// TODO: consider the types of the members as well as their existence
239258 pub ( super ) fn is_assignable_to ( self , db : & ' db dyn Db , other : Self ) -> bool {
240259 other
241- . 0
260+ . inner
242261 . interface ( db)
243- . is_sub_interface_of ( db, self . 0 . interface ( db) )
262+ . is_sub_interface_of ( db, self . inner . interface ( db) )
244263 }
245264
246265 /// Return `true` if this protocol type is equivalent to the protocol `other`.
@@ -269,7 +288,7 @@ impl<'db> ProtocolInstanceType<'db> {
269288 }
270289
271290 pub ( crate ) fn instance_member ( self , db : & ' db dyn Db , name : & str ) -> SymbolAndQualifiers < ' db > {
272- match self . inner ( ) {
291+ match self . inner {
273292 Protocol :: FromClass ( class) => class. instance_member ( db, name) ,
274293 Protocol :: Synthesized ( synthesized) => synthesized
275294 . interface ( )
@@ -287,13 +306,13 @@ impl<'db> ProtocolInstanceType<'db> {
287306 db : & ' db dyn Db ,
288307 type_mapping : TypeMapping < ' a , ' db > ,
289308 ) -> Self {
290- match self . 0 {
291- Protocol :: FromClass ( class) => Self ( Protocol :: FromClass (
292- class. apply_type_mapping ( db, type_mapping) ,
293- ) ) ,
294- Protocol :: Synthesized ( synthesized) => Self ( Protocol :: Synthesized (
295- synthesized. apply_type_mapping ( db, type_mapping) ,
296- ) ) ,
309+ match self . inner {
310+ Protocol :: FromClass ( class) => {
311+ Self :: from_class ( class. apply_type_mapping ( db, type_mapping) )
312+ }
313+ Protocol :: Synthesized ( synthesized) => {
314+ Self :: synthesized ( synthesized . apply_type_mapping ( db, type_mapping) )
315+ }
297316 }
298317 }
299318
@@ -302,7 +321,7 @@ impl<'db> ProtocolInstanceType<'db> {
302321 db : & ' db dyn Db ,
303322 typevars : & mut FxOrderSet < TypeVarInstance < ' db > > ,
304323 ) {
305- match self . 0 {
324+ match self . inner {
306325 Protocol :: FromClass ( class) => {
307326 class. find_legacy_typevars ( db, typevars) ;
308327 }
0 commit comments