@@ -184,14 +184,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
184184
185185 self . lower_use_tree ( use_tree, & prefix, id, vis_span, attrs)
186186 }
187- ItemKind :: Static ( box ast:: StaticItem { ty : t, safety : _, mutability : m, expr : e } ) => {
187+ ItemKind :: Static ( box ast:: StaticItem {
188+ ty : t,
189+ safety : _,
190+ mutability : m,
191+ expr : e,
192+ define_opaque,
193+ } ) => {
188194 debug_assert_ne ! ( ident. name, kw:: Empty ) ;
189195 let ident = self . lower_ident ( ident) ;
190196 let ( ty, body_id) =
191197 self . lower_const_item ( t, span, e. as_deref ( ) , ImplTraitPosition :: StaticTy ) ;
198+ self . lower_define_opaque ( hir_id, define_opaque) ;
192199 hir:: ItemKind :: Static ( ident, ty, * m, body_id)
193200 }
194- ItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, .. } ) => {
201+ ItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, define_opaque , .. } ) => {
195202 debug_assert_ne ! ( ident. name, kw:: Empty ) ;
196203 let ident = self . lower_ident ( ident) ;
197204 let ( generics, ( ty, body_id) ) = self . lower_generics (
@@ -202,6 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
202209 this. lower_const_item ( ty, span, expr. as_deref ( ) , ImplTraitPosition :: ConstTy )
203210 } ,
204211 ) ;
212+ self . lower_define_opaque ( hir_id, & define_opaque) ;
205213 hir:: ItemKind :: Const ( ident, ty, generics, body_id)
206214 }
207215 ItemKind :: Fn ( box Fn {
@@ -239,7 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
239247 header : this. lower_fn_header ( * header, hir:: Safety :: Safe , attrs) ,
240248 span : this. lower_span ( * fn_sig_span) ,
241249 } ;
242- this. lower_define_opaque ( hir_id, & define_opaque) ;
250+ this. lower_define_opaque ( hir_id, define_opaque) ;
243251 let ident = this. lower_ident ( ident) ;
244252 hir:: ItemKind :: Fn {
245253 ident,
@@ -645,7 +653,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
645653 owner_id,
646654 ident : self . lower_ident ( i. ident ) ,
647655 kind : match & i. kind {
648- ForeignItemKind :: Fn ( box Fn { sig, generics, .. } ) => {
656+ ForeignItemKind :: Fn ( box Fn { sig, generics, define_opaque , .. } ) => {
649657 let fdec = & sig. decl ;
650658 let itctx = ImplTraitContext :: Universal ;
651659 let ( generics, ( decl, fn_args) ) =
@@ -666,17 +674,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
666674 // Unmarked safety in unsafe block defaults to unsafe.
667675 let header = self . lower_fn_header ( sig. header , hir:: Safety :: Unsafe , attrs) ;
668676
677+ if define_opaque. is_some ( ) {
678+ self . dcx ( ) . span_err ( i. span , "foreign functions cannot define opaque types" ) ;
679+ }
680+
669681 hir:: ForeignItemKind :: Fn (
670682 hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } ,
671683 fn_args,
672684 generics,
673685 )
674686 }
675- ForeignItemKind :: Static ( box StaticItem { ty, mutability, expr : _, safety } ) => {
687+ ForeignItemKind :: Static ( box StaticItem {
688+ ty,
689+ mutability,
690+ expr : _,
691+ safety,
692+ define_opaque,
693+ } ) => {
676694 let ty = self
677695 . lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: StaticTy ) ) ;
678696 let safety = self . lower_safety ( * safety, hir:: Safety :: Unsafe ) ;
679697
698+ if define_opaque. is_some ( ) {
699+ self . dcx ( ) . span_err ( i. span , "foreign statics cannot define opaque types" ) ;
700+ }
701+
680702 hir:: ForeignItemKind :: Static ( ty, * mutability, safety)
681703 }
682704 ForeignItemKind :: TyAlias ( ..) => hir:: ForeignItemKind :: Type ,
@@ -784,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
784806 let trait_item_def_id = hir_id. expect_owner ( ) ;
785807
786808 let ( generics, kind, has_default) = match & i. kind {
787- AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => {
809+ AssocItemKind :: Const ( box ConstItem { generics, ty, expr, define_opaque , .. } ) => {
788810 let ( generics, kind) = self . lower_generics (
789811 generics,
790812 i. id ,
@@ -797,6 +819,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
797819 hir:: TraitItemKind :: Const ( ty, body)
798820 } ,
799821 ) ;
822+
823+ if define_opaque. is_some ( ) {
824+ if expr. is_some ( ) {
825+ self . lower_define_opaque ( hir_id, & define_opaque) ;
826+ } else {
827+ self . dcx ( ) . span_err (
828+ i. span ,
829+ "only trait consts with default bodies can define opaque types" ,
830+ ) ;
831+ }
832+ }
833+
800834 ( generics, kind, expr. is_some ( ) )
801835 }
802836 AssocItemKind :: Fn ( box Fn { sig, generics, body : None , define_opaque, .. } ) => {
@@ -938,18 +972,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
938972 let attrs = self . lower_attrs ( hir_id, & i. attrs , i. span ) ;
939973
940974 let ( generics, kind) = match & i. kind {
941- AssocItemKind :: Const ( box ConstItem { generics, ty, expr, .. } ) => self . lower_generics (
942- generics,
943- i. id ,
944- ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
945- |this| {
946- let ty =
947- this. lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ) ;
948- let body = this. lower_const_body ( i. span , expr. as_deref ( ) ) ;
949-
950- hir:: ImplItemKind :: Const ( ty, body)
951- } ,
952- ) ,
975+ AssocItemKind :: Const ( box ConstItem { generics, ty, expr, define_opaque, .. } ) => self
976+ . lower_generics (
977+ generics,
978+ i. id ,
979+ ImplTraitContext :: Disallowed ( ImplTraitPosition :: Generic ) ,
980+ |this| {
981+ let ty = this
982+ . lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ) ;
983+ let body = this. lower_const_body ( i. span , expr. as_deref ( ) ) ;
984+ this. lower_define_opaque ( hir_id, & define_opaque) ;
985+
986+ hir:: ImplItemKind :: Const ( ty, body)
987+ } ,
988+ ) ,
953989 AssocItemKind :: Fn ( box Fn { sig, generics, body, contract, define_opaque, .. } ) => {
954990 let body_id = self . lower_maybe_coroutine_body (
955991 sig. span ,
0 commit comments