@@ -195,6 +195,12 @@ enum ImplTraitContext<'a> {
195
195
/// (e.g., for consts and statics).
196
196
Existential ( Option < DefId > /* fn def-ID */ ) ,
197
197
198
+ /// Treat `impl Trait` as a bound on the associated type applied to the trait.
199
+ /// Example: `trait Foo { type Bar: Iterator<Item = impl Debug>; }` is conceptually
200
+ /// equivalent to `trait Foo where <Self::Bar as Iterator>::Item: Debug
201
+ /// { type Bar: Iterator; }`.
202
+ AssociatedTy ,
203
+
198
204
/// `impl Trait` is not accepted in this position.
199
205
Disallowed ( ImplTraitPosition ) ,
200
206
}
@@ -217,6 +223,7 @@ impl<'a> ImplTraitContext<'a> {
217
223
match self {
218
224
Universal ( params) => Universal ( params) ,
219
225
Existential ( fn_def_id) => Existential ( * fn_def_id) ,
226
+ AssociatedTy => AssociatedTy ,
220
227
Disallowed ( pos) => Disallowed ( * pos) ,
221
228
}
222
229
}
@@ -1537,6 +1544,16 @@ impl<'a> LoweringContext<'a> {
1537
1544
} ) ,
1538
1545
) )
1539
1546
}
1547
+ ImplTraitContext :: AssociatedTy => {
1548
+ let hir_bounds = self . lower_param_bounds (
1549
+ bounds,
1550
+ ImplTraitContext :: AssociatedTy ,
1551
+ ) ;
1552
+
1553
+ hir:: TyKind :: AssocTyExistential (
1554
+ hir_bounds,
1555
+ )
1556
+ }
1540
1557
ImplTraitContext :: Disallowed ( pos) => {
1541
1558
let allowed_in = if self . sess . features_untracked ( )
1542
1559
. impl_trait_in_bindings {
@@ -1640,8 +1657,8 @@ impl<'a> LoweringContext<'a> {
1640
1657
} )
1641
1658
}
1642
1659
1643
- /// Registers a new existential type with the proper `NodeId`ss and
1644
- /// returns the lowered node ID for the existential type.
1660
+ /// Registers a new existential type with the proper `NodeId`s and
1661
+ /// returns the lowered node- ID for the existential type.
1645
1662
fn generate_existential_type (
1646
1663
& mut self ,
1647
1664
exist_ty_node_id : NodeId ,
@@ -2226,8 +2243,9 @@ impl<'a> LoweringContext<'a> {
2226
2243
(
2227
2244
hir:: GenericArgs {
2228
2245
args : args. iter ( ) . map ( |a| self . lower_generic_arg ( a, itctx. reborrow ( ) ) ) . collect ( ) ,
2229
- bindings : constraints. iter ( ) . map (
2230
- |b| self . lower_assoc_ty_constraint ( b, itctx. reborrow ( ) ) ) . collect ( ) ,
2246
+ bindings : constraints. iter ( )
2247
+ . map ( |b| self . lower_assoc_ty_constraint ( b, itctx. reborrow ( ) ) )
2248
+ . collect ( ) ,
2231
2249
parenthesized : false ,
2232
2250
} ,
2233
2251
!has_types && param_mode == ParamMode :: Optional
@@ -3257,13 +3275,13 @@ impl<'a> LoweringContext<'a> {
3257
3275
ItemKind :: ForeignMod ( ref nm) => hir:: ItemKind :: ForeignMod ( self . lower_foreign_mod ( nm) ) ,
3258
3276
ItemKind :: GlobalAsm ( ref ga) => hir:: ItemKind :: GlobalAsm ( self . lower_global_asm ( ga) ) ,
3259
3277
ItemKind :: Ty ( ref t, ref generics) => hir:: ItemKind :: Ty (
3260
- self . lower_ty ( t, ImplTraitContext :: disallowed ( ) ) ,
3261
- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3278
+ self . lower_ty ( t, ImplTraitContext :: AssociatedTy ) ,
3279
+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
3262
3280
) ,
3263
3281
ItemKind :: Existential ( ref b, ref generics) => hir:: ItemKind :: Existential (
3264
3282
hir:: ExistTy {
3265
- generics : self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3266
- bounds : self . lower_param_bounds ( b, ImplTraitContext :: Existential ( None ) ) ,
3283
+ generics : self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
3284
+ bounds : self . lower_param_bounds ( b, ImplTraitContext :: AssociatedTy ) ,
3267
3285
impl_trait_fn : None ,
3268
3286
origin : hir:: ExistTyOrigin :: ExistentialType ,
3269
3287
} ,
@@ -3276,20 +3294,20 @@ impl<'a> LoweringContext<'a> {
3276
3294
. map ( |x| self . lower_variant ( x) )
3277
3295
. collect ( ) ,
3278
3296
} ,
3279
- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3297
+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
3280
3298
) ,
3281
3299
ItemKind :: Struct ( ref struct_def, ref generics) => {
3282
3300
let struct_def = self . lower_variant_data ( struct_def) ;
3283
3301
hir:: ItemKind :: Struct (
3284
3302
struct_def,
3285
- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3303
+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
3286
3304
)
3287
3305
}
3288
3306
ItemKind :: Union ( ref vdata, ref generics) => {
3289
3307
let vdata = self . lower_variant_data ( vdata) ;
3290
3308
hir:: ItemKind :: Union (
3291
3309
vdata,
3292
- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3310
+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
3293
3311
)
3294
3312
}
3295
3313
ItemKind :: Impl (
@@ -3656,15 +3674,17 @@ impl<'a> LoweringContext<'a> {
3656
3674
) ;
3657
3675
( generics, hir:: TraitItemKind :: Method ( sig, hir:: TraitMethod :: Provided ( body_id) ) )
3658
3676
}
3659
- TraitItemKind :: Type ( ref bounds, ref default) => (
3660
- self . lower_generics ( & i. generics , ImplTraitContext :: disallowed ( ) ) ,
3661
- hir:: TraitItemKind :: Type (
3662
- self . lower_param_bounds ( bounds, ImplTraitContext :: disallowed ( ) ) ,
3677
+ TraitItemKind :: Type ( ref bounds, ref default) => {
3678
+ let generics = self . lower_generics ( & i. generics , ImplTraitContext :: AssociatedTy ) ;
3679
+ let node = hir:: TraitItemKind :: Type (
3680
+ self . lower_param_bounds ( bounds, ImplTraitContext :: AssociatedTy ) ,
3663
3681
default
3664
3682
. as_ref ( )
3665
3683
. map ( |x| self . lower_ty ( x, ImplTraitContext :: disallowed ( ) ) ) ,
3666
- ) ,
3667
- ) ,
3684
+ ) ;
3685
+
3686
+ ( generics, node)
3687
+ } ,
3668
3688
TraitItemKind :: Macro ( ..) => bug ! ( "macro item shouldn't exist at this point" ) ,
3669
3689
} ;
3670
3690
0 commit comments