@@ -269,26 +269,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
269269 self . lower_use_tree ( use_tree, & prefix, id, vis, ident, attrs)
270270 }
271271 ItemKind :: Static ( ref t, m, ref e) => {
272- let ty = self . lower_ty (
273- t,
274- if self . sess . features_untracked ( ) . impl_trait_in_bindings {
275- ImplTraitContext :: OpaqueTy ( None , hir:: OpaqueTyOrigin :: Misc )
276- } else {
277- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Binding )
278- } ,
279- ) ;
280- hir:: ItemKind :: Static ( ty, m, self . lower_const_body ( span, Some ( e) ) )
272+ let ( ty, body_id) = self . lower_const_item ( t, span, e. as_deref ( ) ) ;
273+ hir:: ItemKind :: Static ( ty, m, body_id)
281274 }
282275 ItemKind :: Const ( ref t, ref e) => {
283- let ty = self . lower_ty (
284- t,
285- if self . sess . features_untracked ( ) . impl_trait_in_bindings {
286- ImplTraitContext :: OpaqueTy ( None , hir:: OpaqueTyOrigin :: Misc )
287- } else {
288- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Binding )
289- } ,
290- ) ;
291- hir:: ItemKind :: Const ( ty, self . lower_const_body ( span, Some ( e) ) )
276+ let ( ty, body_id) = self . lower_const_item ( t, span, e. as_deref ( ) ) ;
277+ hir:: ItemKind :: Const ( ty, body_id)
292278 }
293279 ItemKind :: Fn ( FnSig { ref decl, header } , ref generics, ref body) => {
294280 let fn_def_id = self . resolver . definitions ( ) . local_def_id ( id) ;
@@ -457,6 +443,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
457443 // not cause an assertion failure inside the `lower_defaultness` function.
458444 }
459445
446+ fn lower_const_item (
447+ & mut self ,
448+ ty : & Ty ,
449+ span : Span ,
450+ body : Option < & Expr > ,
451+ ) -> ( & ' hir hir:: Ty < ' hir > , hir:: BodyId ) {
452+ let itctx = if self . sess . features_untracked ( ) . impl_trait_in_bindings {
453+ ImplTraitContext :: OpaqueTy ( None , hir:: OpaqueTyOrigin :: Misc )
454+ } else {
455+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Binding )
456+ } ;
457+ let ty = self . lower_ty ( ty, itctx) ;
458+ ( ty, self . lower_const_body ( span, body) )
459+ }
460+
460461 fn lower_use_tree (
461462 & mut self ,
462463 tree : & UseTree ,
@@ -678,11 +679,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
678679
679680 hir:: ForeignItemKind :: Fn ( fn_dec, fn_args, generics)
680681 }
681- ForeignItemKind :: Static ( ref t, m) => {
682+ ForeignItemKind :: Static ( ref t, m, _ ) => {
682683 let ty = self . lower_ty ( t, ImplTraitContext :: disallowed ( ) ) ;
683684 hir:: ForeignItemKind :: Static ( ty, m)
684685 }
685- ForeignItemKind :: Ty => hir:: ForeignItemKind :: Type ,
686+ ForeignItemKind :: Const ( ref t, _) => {
687+ // For recovery purposes.
688+ let ty = self . lower_ty ( t, ImplTraitContext :: disallowed ( ) ) ;
689+ hir:: ForeignItemKind :: Static ( ty, Mutability :: Not )
690+ }
691+ ForeignItemKind :: TyAlias ( ..) => hir:: ForeignItemKind :: Type ,
686692 ForeignItemKind :: Macro ( _) => panic ! ( "macro shouldn't exist here" ) ,
687693 } ,
688694 vis : self . lower_visibility ( & i. vis , None ) ,
@@ -759,32 +765,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
759765 let trait_item_def_id = self . resolver . definitions ( ) . local_def_id ( i. id ) ;
760766
761767 let ( generics, kind) = match i. kind {
762- AssocItemKind :: Const ( ref ty, ref default) => {
763- let generics = self . lower_generics ( & i . generics , ImplTraitContext :: disallowed ( ) ) ;
768+ AssocItemKind :: Static ( ref ty, _ , ref default) // Let's pretend this is a `const`.
769+ | AssocItemKind :: Const ( ref ty , ref default ) => {
764770 let ty = self . lower_ty ( ty, ImplTraitContext :: disallowed ( ) ) ;
765- (
766- generics,
767- hir:: TraitItemKind :: Const (
768- ty,
769- default. as_ref ( ) . map ( |x| self . lower_const_body ( i. span , Some ( x) ) ) ,
770- ) ,
771- )
771+ let body = default. as_ref ( ) . map ( |x| self . lower_const_body ( i. span , Some ( x) ) ) ;
772+ ( hir:: Generics :: empty ( ) , hir:: TraitItemKind :: Const ( ty, body) )
772773 }
773- AssocItemKind :: Fn ( ref sig, None ) => {
774+ AssocItemKind :: Fn ( ref sig, ref generics , None ) => {
774775 let names = self . lower_fn_params_to_names ( & sig. decl ) ;
775776 let ( generics, sig) =
776- self . lower_method_sig ( & i . generics , sig, trait_item_def_id, false , None ) ;
777+ self . lower_method_sig ( generics, sig, trait_item_def_id, false , None ) ;
777778 ( generics, hir:: TraitItemKind :: Method ( sig, hir:: TraitMethod :: Required ( names) ) )
778779 }
779- AssocItemKind :: Fn ( ref sig, Some ( ref body) ) => {
780+ AssocItemKind :: Fn ( ref sig, ref generics , Some ( ref body) ) => {
780781 let body_id = self . lower_fn_body_block ( i. span , & sig. decl , Some ( body) ) ;
781782 let ( generics, sig) =
782- self . lower_method_sig ( & i . generics , sig, trait_item_def_id, false , None ) ;
783+ self . lower_method_sig ( generics, sig, trait_item_def_id, false , None ) ;
783784 ( generics, hir:: TraitItemKind :: Method ( sig, hir:: TraitMethod :: Provided ( body_id) ) )
784785 }
785- AssocItemKind :: TyAlias ( ref bounds, ref default) => {
786+ AssocItemKind :: TyAlias ( ref generics , ref bounds, ref default) => {
786787 let ty = default. as_ref ( ) . map ( |x| self . lower_ty ( x, ImplTraitContext :: disallowed ( ) ) ) ;
787- let generics = self . lower_generics ( & i . generics , ImplTraitContext :: disallowed ( ) ) ;
788+ let generics = self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ;
788789 let kind = hir:: TraitItemKind :: Type (
789790 self . lower_param_bounds ( bounds, ImplTraitContext :: disallowed ( ) ) ,
790791 ty,
@@ -806,10 +807,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
806807 }
807808
808809 fn lower_trait_item_ref ( & mut self , i : & AssocItem ) -> hir:: TraitItemRef {
809- let ( kind, has_default) = match i. kind {
810- AssocItemKind :: Const ( _, ref default) => ( hir:: AssocItemKind :: Const , default. is_some ( ) ) ,
811- AssocItemKind :: TyAlias ( _, ref default) => ( hir:: AssocItemKind :: Type , default. is_some ( ) ) ,
812- AssocItemKind :: Fn ( ref sig, ref default) => {
810+ let ( kind, has_default) = match & i. kind {
811+ AssocItemKind :: Static ( _, _, default) // Let's pretend this is a `const` for recovery.
812+ | AssocItemKind :: Const ( _, default) => {
813+ ( hir:: AssocItemKind :: Const , default. is_some ( ) )
814+ }
815+ AssocItemKind :: TyAlias ( _, _, default) => ( hir:: AssocItemKind :: Type , default. is_some ( ) ) ,
816+ AssocItemKind :: Fn ( sig, _, default) => {
813817 ( hir:: AssocItemKind :: Method { has_self : sig. decl . has_self ( ) } , default. is_some ( ) )
814818 }
815819 AssocItemKind :: Macro ( ..) => unimplemented ! ( ) ,
@@ -832,22 +836,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
832836 let impl_item_def_id = self . resolver . definitions ( ) . local_def_id ( i. id ) ;
833837
834838 let ( generics, kind) = match i. kind {
835- AssocItemKind :: Const ( ref ty, ref expr) => {
836- let generics = self . lower_generics ( & i. generics , ImplTraitContext :: disallowed ( ) ) ;
839+ AssocItemKind :: Static ( ref ty, _, ref expr) | AssocItemKind :: Const ( ref ty, ref expr) => {
837840 let ty = self . lower_ty ( ty, ImplTraitContext :: disallowed ( ) ) ;
838841 (
839- generics ,
842+ hir :: Generics :: empty ( ) ,
840843 hir:: ImplItemKind :: Const ( ty, self . lower_const_body ( i. span , expr. as_deref ( ) ) ) ,
841844 )
842845 }
843- AssocItemKind :: Fn ( ref sig, ref body) => {
846+ AssocItemKind :: Fn ( ref sig, ref generics , ref body) => {
844847 self . current_item = Some ( i. span ) ;
845848 let asyncness = sig. header . asyncness ;
846849 let body_id =
847850 self . lower_maybe_async_body ( i. span , & sig. decl , asyncness, body. as_deref ( ) ) ;
848851 let impl_trait_return_allow = !self . is_in_trait_impl ;
849852 let ( generics, sig) = self . lower_method_sig (
850- & i . generics ,
853+ generics,
851854 sig,
852855 impl_item_def_id,
853856 impl_trait_return_allow,
@@ -856,8 +859,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
856859
857860 ( generics, hir:: ImplItemKind :: Method ( sig, body_id) )
858861 }
859- AssocItemKind :: TyAlias ( _, ref ty) => {
860- let generics = self . lower_generics ( & i . generics , ImplTraitContext :: disallowed ( ) ) ;
862+ AssocItemKind :: TyAlias ( ref generics , _, ref ty) => {
863+ let generics = self . lower_generics ( generics, ImplTraitContext :: disallowed ( ) ) ;
861864 let kind = match ty {
862865 None => {
863866 let ty = self . arena . alloc ( self . ty ( i. span , hir:: TyKind :: Err ) ) ;
@@ -901,14 +904,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
901904 vis : self . lower_visibility ( & i. vis , Some ( i. id ) ) ,
902905 defaultness : self . lower_defaultness ( i. defaultness , true /* [1] */ ) ,
903906 kind : match & i. kind {
904- AssocItemKind :: Const ( ..) => hir:: AssocItemKind :: Const ,
905- AssocItemKind :: TyAlias ( _, ty) => {
907+ AssocItemKind :: Static ( ..) // Let's pretend this is a `const` for recovery.
908+ | AssocItemKind :: Const ( ..) => hir:: AssocItemKind :: Const ,
909+ AssocItemKind :: TyAlias ( _, _, ty) => {
906910 match ty. as_deref ( ) . and_then ( |ty| ty. kind . opaque_top_hack ( ) ) {
907911 None => hir:: AssocItemKind :: Type ,
908912 Some ( _) => hir:: AssocItemKind :: OpaqueTy ,
909913 }
910914 }
911- AssocItemKind :: Fn ( sig, _) => {
915+ AssocItemKind :: Fn ( sig, _, _ ) => {
912916 hir:: AssocItemKind :: Method { has_self : sig. decl . has_self ( ) }
913917 }
914918 AssocItemKind :: Macro ( ..) => unimplemented ! ( ) ,
0 commit comments