@@ -289,16 +289,34 @@ impl<I: Interner> ty::Binder<I, ExistentialPredicate<I>> {
289
289
pub struct ExistentialTraitRef < I : Interner > {
290
290
pub def_id : I :: DefId ,
291
291
pub args : I :: GenericArgs ,
292
+ /// This field exists to prevent the creation of `ExistentialTraitRef` without
293
+ /// calling [`ExistentialTraitRef::new_from_args`].
294
+ _use_existential_trait_ref_new_instead : ( ) ,
292
295
}
293
296
294
297
impl < I : Interner > ExistentialTraitRef < I > {
298
+ pub fn new_from_args ( interner : I , trait_def_id : I :: DefId , args : I :: GenericArgs ) -> Self {
299
+ interner. debug_assert_existential_args_compatible ( trait_def_id, args) ;
300
+ Self { def_id : trait_def_id, args, _use_existential_trait_ref_new_instead : ( ) }
301
+ }
302
+
303
+ pub fn new (
304
+ interner : I ,
305
+ trait_def_id : I :: DefId ,
306
+ args : impl IntoIterator < Item : Into < I :: GenericArg > > ,
307
+ ) -> Self {
308
+ let args = interner. mk_args_from_iter ( args. into_iter ( ) . map ( Into :: into) ) ;
309
+ Self :: new_from_args ( interner, trait_def_id, args)
310
+ }
311
+
295
312
pub fn erase_self_ty ( interner : I , trait_ref : TraitRef < I > ) -> ExistentialTraitRef < I > {
296
313
// Assert there is a Self.
297
314
trait_ref. args . type_at ( 0 ) ;
298
315
299
316
ExistentialTraitRef {
300
317
def_id : trait_ref. def_id ,
301
318
args : interner. mk_args ( & trait_ref. args . as_slice ( ) [ 1 ..] ) ,
319
+ _use_existential_trait_ref_new_instead : ( ) ,
302
320
}
303
321
}
304
322
@@ -336,9 +354,33 @@ pub struct ExistentialProjection<I: Interner> {
336
354
pub def_id : I :: DefId ,
337
355
pub args : I :: GenericArgs ,
338
356
pub term : I :: Term ,
357
+
358
+ /// This field exists to prevent the creation of `ExistentialProjection`
359
+ /// without using [`ExistentialProjection::new_from_args`].
360
+ use_existential_projection_new_instead : ( ) ,
339
361
}
340
362
341
363
impl < I : Interner > ExistentialProjection < I > {
364
+ pub fn new_from_args (
365
+ interner : I ,
366
+ def_id : I :: DefId ,
367
+ args : I :: GenericArgs ,
368
+ term : I :: Term ,
369
+ ) -> ExistentialProjection < I > {
370
+ interner. debug_assert_existential_args_compatible ( def_id, args) ;
371
+ Self { def_id, args, term, use_existential_projection_new_instead : ( ) }
372
+ }
373
+
374
+ pub fn new (
375
+ interner : I ,
376
+ def_id : I :: DefId ,
377
+ args : impl IntoIterator < Item : Into < I :: GenericArg > > ,
378
+ term : I :: Term ,
379
+ ) -> ExistentialProjection < I > {
380
+ let args = interner. mk_args_from_iter ( args. into_iter ( ) . map ( Into :: into) ) ;
381
+ Self :: new_from_args ( interner, def_id, args, term)
382
+ }
383
+
342
384
/// Extracts the underlying existential trait reference from this projection.
343
385
/// For example, if this is a projection of `exists T. <T as Iterator>::Item == X`,
344
386
/// then this function would return an `exists T. T: Iterator` existential trait
@@ -347,7 +389,7 @@ impl<I: Interner> ExistentialProjection<I> {
347
389
let def_id = interner. parent ( self . def_id ) ;
348
390
let args_count = interner. generics_of ( def_id) . count ( ) - 1 ;
349
391
let args = interner. mk_args ( & self . args . as_slice ( ) [ ..args_count] ) ;
350
- ExistentialTraitRef { def_id, args }
392
+ ExistentialTraitRef { def_id, args, _use_existential_trait_ref_new_instead : ( ) }
351
393
}
352
394
353
395
pub fn with_self_ty ( & self , interner : I , self_ty : I :: Ty ) -> ProjectionPredicate < I > {
@@ -372,6 +414,7 @@ impl<I: Interner> ExistentialProjection<I> {
372
414
def_id : projection_predicate. projection_term . def_id ,
373
415
args : interner. mk_args ( & projection_predicate. projection_term . args . as_slice ( ) [ 1 ..] ) ,
374
416
term : projection_predicate. term ,
417
+ use_existential_projection_new_instead : ( ) ,
375
418
}
376
419
}
377
420
}
0 commit comments