@@ -288,31 +288,31 @@ enum ImplTraitPosition {
288
288
impl std:: fmt:: Display for ImplTraitPosition {
289
289
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
290
290
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 " ,
316
316
} ;
317
317
318
318
write ! ( f, "{name}" )
@@ -1002,8 +1002,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1002
1002
} else {
1003
1003
self . arena . alloc ( hir:: GenericArgs :: none ( ) )
1004
1004
} ;
1005
- let itctx_tait = & ImplTraitContext :: TypeAliasesOpaqueTy ;
1006
-
1007
1005
let kind = match & constraint. kind {
1008
1006
AssocConstraintKind :: Equality { term } => {
1009
1007
let term = match term {
@@ -1013,8 +1011,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1013
1011
hir:: TypeBindingKind :: Equality { term }
1014
1012
}
1015
1013
AssocConstraintKind :: Bound { bounds } => {
1014
+ enum DesugarKind < ' a > {
1015
+ ImplTrait ,
1016
+ Error ( & ' a ImplTraitPosition ) ,
1017
+ Bound ,
1018
+ }
1019
+
1016
1020
// 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 {
1018
1022
// We are in the return position:
1019
1023
//
1020
1024
// fn foo() -> impl Iterator<Item: Debug>
@@ -1023,7 +1027,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1023
1027
//
1024
1028
// fn foo() -> impl Iterator<Item = impl Debug>
1025
1029
ImplTraitContext :: ReturnPositionOpaqueTy { .. }
1026
- | ImplTraitContext :: TypeAliasesOpaqueTy { .. } => ( true , itctx ) ,
1030
+ | ImplTraitContext :: TypeAliasesOpaqueTy { .. } => DesugarKind :: ImplTrait ,
1027
1031
1028
1032
// We are in the argument position, but within a dyn type:
1029
1033
//
@@ -1032,15 +1036,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1032
1036
// so desugar to
1033
1037
//
1034
1038
// 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 ,
1036
1040
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
+ }
1044
1044
1045
1045
// We are in the parameter position, but not within a dyn type:
1046
1046
//
@@ -1049,35 +1049,46 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1049
1049
// so we leave it as is and this gets expanded in astconv to a bound like
1050
1050
// `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
1051
1051
// `impl Iterator`.
1052
- _ => ( false , itctx ) ,
1052
+ _ => DesugarKind :: Bound ,
1053
1053
} ;
1054
1054
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.
1072
1059
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 ( ) ;
1079
1061
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
+ }
1081
1092
}
1082
1093
}
1083
1094
} ;
0 commit comments