@@ -195,6 +195,12 @@ enum ImplTraitContext<'a> {
195195 /// (e.g., for consts and statics).
196196 Existential ( Option < DefId > /* fn def-ID */ ) ,
197197
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+
198204 /// `impl Trait` is not accepted in this position.
199205 Disallowed ( ImplTraitPosition ) ,
200206}
@@ -217,6 +223,7 @@ impl<'a> ImplTraitContext<'a> {
217223 match self {
218224 Universal ( params) => Universal ( params) ,
219225 Existential ( fn_def_id) => Existential ( * fn_def_id) ,
226+ AssociatedTy => AssociatedTy ,
220227 Disallowed ( pos) => Disallowed ( * pos) ,
221228 }
222229 }
@@ -1537,6 +1544,16 @@ impl<'a> LoweringContext<'a> {
15371544 } ) ,
15381545 ) )
15391546 }
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+ }
15401557 ImplTraitContext :: Disallowed ( pos) => {
15411558 let allowed_in = if self . sess . features_untracked ( )
15421559 . impl_trait_in_bindings {
@@ -1640,8 +1657,8 @@ impl<'a> LoweringContext<'a> {
16401657 } )
16411658 }
16421659
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.
16451662 fn generate_existential_type (
16461663 & mut self ,
16471664 exist_ty_node_id : NodeId ,
@@ -2226,8 +2243,9 @@ impl<'a> LoweringContext<'a> {
22262243 (
22272244 hir:: GenericArgs {
22282245 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 ( ) ,
22312249 parenthesized : false ,
22322250 } ,
22332251 !has_types && param_mode == ParamMode :: Optional
@@ -3257,13 +3275,13 @@ impl<'a> LoweringContext<'a> {
32573275 ItemKind :: ForeignMod ( ref nm) => hir:: ItemKind :: ForeignMod ( self . lower_foreign_mod ( nm) ) ,
32583276 ItemKind :: GlobalAsm ( ref ga) => hir:: ItemKind :: GlobalAsm ( self . lower_global_asm ( ga) ) ,
32593277 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 ) ,
32623280 ) ,
32633281 ItemKind :: Existential ( ref b, ref generics) => hir:: ItemKind :: Existential (
32643282 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 ) ,
32673285 impl_trait_fn : None ,
32683286 origin : hir:: ExistTyOrigin :: ExistentialType ,
32693287 } ,
@@ -3276,20 +3294,20 @@ impl<'a> LoweringContext<'a> {
32763294 . map ( |x| self . lower_variant ( x) )
32773295 . collect ( ) ,
32783296 } ,
3279- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3297+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
32803298 ) ,
32813299 ItemKind :: Struct ( ref struct_def, ref generics) => {
32823300 let struct_def = self . lower_variant_data ( struct_def) ;
32833301 hir:: ItemKind :: Struct (
32843302 struct_def,
3285- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3303+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
32863304 )
32873305 }
32883306 ItemKind :: Union ( ref vdata, ref generics) => {
32893307 let vdata = self . lower_variant_data ( vdata) ;
32903308 hir:: ItemKind :: Union (
32913309 vdata,
3292- self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ,
3310+ self . lower_generics ( generics, ImplTraitContext :: AssociatedTy ) ,
32933311 )
32943312 }
32953313 ItemKind :: Impl (
@@ -3656,15 +3674,17 @@ impl<'a> LoweringContext<'a> {
36563674 ) ;
36573675 ( generics, hir:: TraitItemKind :: Method ( sig, hir:: TraitMethod :: Provided ( body_id) ) )
36583676 }
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 ) ,
36633681 default
36643682 . as_ref ( )
36653683 . map ( |x| self . lower_ty ( x, ImplTraitContext :: disallowed ( ) ) ) ,
3666- ) ,
3667- ) ,
3684+ ) ;
3685+
3686+ ( generics, node)
3687+ } ,
36683688 TraitItemKind :: Macro ( ..) => bug ! ( "macro item shouldn't exist at this point" ) ,
36693689 } ;
36703690
0 commit comments