@@ -288,31 +288,31 @@ enum ImplTraitPosition {
288288impl std:: fmt:: Display for ImplTraitPosition {
289289 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
290290 let name = match self {
291- ImplTraitPosition :: Path => "path " ,
292- ImplTraitPosition :: Variable => "variable binding " ,
293- ImplTraitPosition :: Trait => "trait " ,
294- ImplTraitPosition :: AsyncBlock => "async block " ,
295- ImplTraitPosition :: Bound => "bound " ,
296- ImplTraitPosition :: Generic => "generic " ,
297- ImplTraitPosition :: ExternFnParam => "`extern fn` param " ,
298- ImplTraitPosition :: ClosureParam => "closure param " ,
299- ImplTraitPosition :: PointerParam => "`fn` pointer param " ,
300- ImplTraitPosition :: FnTraitParam => "`Fn` trait param " ,
301- ImplTraitPosition :: TraitParam => "trait method param " ,
302- ImplTraitPosition :: ImplParam => "`impl` method param " ,
303- ImplTraitPosition :: ExternFnReturn => "`extern fn` return" ,
304- ImplTraitPosition :: ClosureReturn => "closure return" ,
305- ImplTraitPosition :: PointerReturn => "`fn` pointer return" ,
306- ImplTraitPosition :: FnTraitReturn => "`Fn` trait return" ,
307- ImplTraitPosition :: TraitReturn => "trait method return" ,
308- ImplTraitPosition :: ImplReturn => "`impl` method return" ,
309- ImplTraitPosition :: GenericDefault => "generic parameter default " ,
310- ImplTraitPosition :: ConstTy => "const type " ,
311- ImplTraitPosition :: StaticTy => "static type " ,
312- ImplTraitPosition :: AssocTy => "associated type " ,
313- ImplTraitPosition :: FieldTy => "field type " ,
314- ImplTraitPosition :: Cast => "cast type " ,
315- ImplTraitPosition :: ImplSelf => "impl header " ,
291+ ImplTraitPosition :: Path => "paths " ,
292+ ImplTraitPosition :: Variable => "variable bindings " ,
293+ ImplTraitPosition :: Trait => "traits " ,
294+ ImplTraitPosition :: AsyncBlock => "async blocks " ,
295+ ImplTraitPosition :: Bound => "bounds " ,
296+ ImplTraitPosition :: Generic => "generics " ,
297+ ImplTraitPosition :: ExternFnParam => "`extern fn` params " ,
298+ ImplTraitPosition :: ClosureParam => "closure params " ,
299+ ImplTraitPosition :: PointerParam => "`fn` pointer params " ,
300+ ImplTraitPosition :: FnTraitParam => "`Fn` trait params " ,
301+ ImplTraitPosition :: TraitParam => "trait method params " ,
302+ ImplTraitPosition :: ImplParam => "`impl` method params " ,
303+ ImplTraitPosition :: ExternFnReturn => "`extern fn` return types " ,
304+ ImplTraitPosition :: ClosureReturn => "closure return types " ,
305+ ImplTraitPosition :: PointerReturn => "`fn` pointer return types " ,
306+ ImplTraitPosition :: FnTraitReturn => "`Fn` trait return types " ,
307+ ImplTraitPosition :: TraitReturn => "trait method return types " ,
308+ ImplTraitPosition :: ImplReturn => "`impl` method return types " ,
309+ ImplTraitPosition :: GenericDefault => "generic parameter defaults " ,
310+ ImplTraitPosition :: ConstTy => "const types " ,
311+ ImplTraitPosition :: StaticTy => "static types " ,
312+ ImplTraitPosition :: AssocTy => "associated types " ,
313+ ImplTraitPosition :: FieldTy => "field types " ,
314+ ImplTraitPosition :: Cast => "cast types " ,
315+ ImplTraitPosition :: ImplSelf => "impl headers " ,
316316 } ;
317317
318318 write ! ( f, "{name}" )
@@ -1002,8 +1002,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10021002 } else {
10031003 self . arena . alloc ( hir:: GenericArgs :: none ( ) )
10041004 } ;
1005- let itctx_tait = & ImplTraitContext :: TypeAliasesOpaqueTy ;
1006-
10071005 let kind = match & constraint. kind {
10081006 AssocConstraintKind :: Equality { term } => {
10091007 let term = match term {
@@ -1013,8 +1011,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10131011 hir:: TypeBindingKind :: Equality { term }
10141012 }
10151013 AssocConstraintKind :: Bound { bounds } => {
1014+ enum DesugarKind < ' a > {
1015+ ImplTrait ,
1016+ Error ( & ' a ImplTraitPosition ) ,
1017+ Bound ,
1018+ }
1019+
10161020 // Piggy-back on the `impl Trait` context to figure out the correct behavior.
1017- let ( desugar_to_impl_trait , itctx ) = match itctx {
1021+ let desugar_kind = match itctx {
10181022 // We are in the return position:
10191023 //
10201024 // fn foo() -> impl Iterator<Item: Debug>
@@ -1023,7 +1027,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10231027 //
10241028 // fn foo() -> impl Iterator<Item = impl Debug>
10251029 ImplTraitContext :: ReturnPositionOpaqueTy { .. }
1026- | ImplTraitContext :: TypeAliasesOpaqueTy { .. } => ( true , itctx ) ,
1030+ | ImplTraitContext :: TypeAliasesOpaqueTy { .. } => DesugarKind :: ImplTrait ,
10271031
10281032 // We are in the argument position, but within a dyn type:
10291033 //
@@ -1032,15 +1036,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10321036 // so desugar to
10331037 //
10341038 // fn foo(x: dyn Iterator<Item = impl Debug>)
1035- ImplTraitContext :: Universal if self . is_in_dyn_type => ( true , itctx ) ,
1039+ ImplTraitContext :: Universal if self . is_in_dyn_type => DesugarKind :: ImplTrait ,
10361040
1037- // In `type Foo = dyn Iterator<Item: Debug>` we desugar to
1038- // `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
1039- // "impl trait context" to permit `impl Debug` in this position (it desugars
1040- // then to an opaque type).
1041- //
1042- // FIXME: this is only needed until `impl Trait` is allowed in type aliases.
1043- ImplTraitContext :: Disallowed ( _) if self . is_in_dyn_type => ( true , itctx_tait) ,
1041+ ImplTraitContext :: Disallowed ( position) if self . is_in_dyn_type => {
1042+ DesugarKind :: Error ( position)
1043+ }
10441044
10451045 // We are in the parameter position, but not within a dyn type:
10461046 //
@@ -1049,35 +1049,46 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10491049 // so we leave it as is and this gets expanded in astconv to a bound like
10501050 // `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
10511051 // `impl Iterator`.
1052- _ => ( false , itctx ) ,
1052+ _ => DesugarKind :: Bound ,
10531053 } ;
10541054
1055- if desugar_to_impl_trait {
1056- // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1057- // constructing the HIR for `impl bounds...` and then lowering that.
1058-
1059- let impl_trait_node_id = self . next_node_id ( ) ;
1060-
1061- self . with_dyn_type_scope ( false , |this| {
1062- let node_id = this. next_node_id ( ) ;
1063- let ty = this. lower_ty (
1064- & Ty {
1065- id : node_id,
1066- kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1067- span : this. lower_span ( constraint. span ) ,
1068- tokens : None ,
1069- } ,
1070- itctx,
1071- ) ;
1055+ match desugar_kind {
1056+ DesugarKind :: ImplTrait => {
1057+ // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1058+ // constructing the HIR for `impl bounds...` and then lowering that.
10721059
1073- hir:: TypeBindingKind :: Equality { term : ty. into ( ) }
1074- } )
1075- } else {
1076- // Desugar `AssocTy: Bounds` into a type binding where the
1077- // later desugars into a trait predicate.
1078- let bounds = self . lower_param_bounds ( bounds, itctx) ;
1060+ let impl_trait_node_id = self . next_node_id ( ) ;
10791061
1080- hir:: TypeBindingKind :: Constraint { bounds }
1062+ self . with_dyn_type_scope ( false , |this| {
1063+ let node_id = this. next_node_id ( ) ;
1064+ let ty = this. lower_ty (
1065+ & Ty {
1066+ id : node_id,
1067+ kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1068+ span : this. lower_span ( constraint. span ) ,
1069+ tokens : None ,
1070+ } ,
1071+ itctx,
1072+ ) ;
1073+
1074+ hir:: TypeBindingKind :: Equality { term : ty. into ( ) }
1075+ } )
1076+ }
1077+ DesugarKind :: Bound => {
1078+ // Desugar `AssocTy: Bounds` into a type binding where the
1079+ // later desugars into a trait predicate.
1080+ let bounds = self . lower_param_bounds ( bounds, itctx) ;
1081+
1082+ hir:: TypeBindingKind :: Constraint { bounds }
1083+ }
1084+ DesugarKind :: Error ( position) => {
1085+ self . tcx . sess . emit_err ( errors:: MisplacedAssocTyBinding {
1086+ span : constraint. span ,
1087+ position : DiagnosticArgFromDisplay ( position) ,
1088+ } ) ;
1089+ let err_ty = & * self . arena . alloc ( self . ty ( constraint. span , hir:: TyKind :: Err ) ) ;
1090+ hir:: TypeBindingKind :: Equality { term : err_ty. into ( ) }
1091+ }
10811092 }
10821093 }
10831094 } ;
0 commit comments