@@ -289,16 +289,34 @@ impl<I: Interner> ty::Binder<I, ExistentialPredicate<I>> {
289289pub struct ExistentialTraitRef < I : Interner > {
290290 pub def_id : I :: DefId ,
291291 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 : ( ) ,
292295}
293296
294297impl < 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+
295312 pub fn erase_self_ty ( interner : I , trait_ref : TraitRef < I > ) -> ExistentialTraitRef < I > {
296313 // Assert there is a Self.
297314 trait_ref. args . type_at ( 0 ) ;
298315
299316 ExistentialTraitRef {
300317 def_id : trait_ref. def_id ,
301318 args : interner. mk_args ( & trait_ref. args . as_slice ( ) [ 1 ..] ) ,
319+ _use_existential_trait_ref_new_instead : ( ) ,
302320 }
303321 }
304322
@@ -336,9 +354,33 @@ pub struct ExistentialProjection<I: Interner> {
336354 pub def_id : I :: DefId ,
337355 pub args : I :: GenericArgs ,
338356 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 : ( ) ,
339361}
340362
341363impl < 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+
342384 /// Extracts the underlying existential trait reference from this projection.
343385 /// For example, if this is a projection of `exists T. <T as Iterator>::Item == X`,
344386 /// then this function would return an `exists T. T: Iterator` existential trait
@@ -347,7 +389,7 @@ impl<I: Interner> ExistentialProjection<I> {
347389 let def_id = interner. parent ( self . def_id ) ;
348390 let args_count = interner. generics_of ( def_id) . count ( ) - 1 ;
349391 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 : ( ) }
351393 }
352394
353395 pub fn with_self_ty ( & self , interner : I , self_ty : I :: Ty ) -> ProjectionPredicate < I > {
@@ -372,6 +414,7 @@ impl<I: Interner> ExistentialProjection<I> {
372414 def_id : projection_predicate. projection_term . def_id ,
373415 args : interner. mk_args ( & projection_predicate. projection_term . args . as_slice ( ) [ 1 ..] ) ,
374416 term : projection_predicate. term ,
417+ use_existential_projection_new_instead : ( ) ,
375418 }
376419 }
377420}
0 commit comments