@@ -184,14 +184,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
184
184
185
185
self . lower_use_tree ( use_tree, & prefix, id, vis_span, attrs)
186
186
}
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
+ } ) => {
188
194
debug_assert_ne ! ( ident. name, kw:: Empty ) ;
189
195
let ident = self . lower_ident ( ident) ;
190
196
let ( ty, body_id) =
191
197
self . lower_const_item ( t, span, e. as_deref ( ) , ImplTraitPosition :: StaticTy ) ;
198
+ self . lower_define_opaque ( hir_id, define_opaque) ;
192
199
hir:: ItemKind :: Static ( ident, ty, * m, body_id)
193
200
}
194
- ItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, .. } ) => {
201
+ ItemKind :: Const ( box ast:: ConstItem { generics, ty, expr, define_opaque , .. } ) => {
195
202
debug_assert_ne ! ( ident. name, kw:: Empty ) ;
196
203
let ident = self . lower_ident ( ident) ;
197
204
let ( generics, ( ty, body_id) ) = self . lower_generics (
@@ -202,6 +209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
202
209
this. lower_const_item ( ty, span, expr. as_deref ( ) , ImplTraitPosition :: ConstTy )
203
210
} ,
204
211
) ;
212
+ self . lower_define_opaque ( hir_id, & define_opaque) ;
205
213
hir:: ItemKind :: Const ( ident, ty, generics, body_id)
206
214
}
207
215
ItemKind :: Fn ( box Fn {
@@ -239,7 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
239
247
header : this. lower_fn_header ( * header, hir:: Safety :: Safe , attrs) ,
240
248
span : this. lower_span ( * fn_sig_span) ,
241
249
} ;
242
- this. lower_define_opaque ( hir_id, & define_opaque) ;
250
+ this. lower_define_opaque ( hir_id, define_opaque) ;
243
251
let ident = this. lower_ident ( ident) ;
244
252
hir:: ItemKind :: Fn {
245
253
ident,
@@ -645,7 +653,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
645
653
owner_id,
646
654
ident : self . lower_ident ( i. ident ) ,
647
655
kind : match & i. kind {
648
- ForeignItemKind :: Fn ( box Fn { sig, generics, .. } ) => {
656
+ ForeignItemKind :: Fn ( box Fn { sig, generics, define_opaque , .. } ) => {
649
657
let fdec = & sig. decl ;
650
658
let itctx = ImplTraitContext :: Universal ;
651
659
let ( generics, ( decl, fn_args) ) =
@@ -666,17 +674,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
666
674
// Unmarked safety in unsafe block defaults to unsafe.
667
675
let header = self . lower_fn_header ( sig. header , hir:: Safety :: Unsafe , attrs) ;
668
676
677
+ if define_opaque. is_some ( ) {
678
+ self . dcx ( ) . span_err ( i. span , "foreign functions cannot define opaque types" ) ;
679
+ }
680
+
669
681
hir:: ForeignItemKind :: Fn (
670
682
hir:: FnSig { header, decl, span : self . lower_span ( sig. span ) } ,
671
683
fn_args,
672
684
generics,
673
685
)
674
686
}
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
+ } ) => {
676
694
let ty = self
677
695
. lower_ty ( ty, ImplTraitContext :: Disallowed ( ImplTraitPosition :: StaticTy ) ) ;
678
696
let safety = self . lower_safety ( * safety, hir:: Safety :: Unsafe ) ;
679
697
698
+ if define_opaque. is_some ( ) {
699
+ self . dcx ( ) . span_err ( i. span , "foreign statics cannot define opaque types" ) ;
700
+ }
701
+
680
702
hir:: ForeignItemKind :: Static ( ty, * mutability, safety)
681
703
}
682
704
ForeignItemKind :: TyAlias ( ..) => hir:: ForeignItemKind :: Type ,
@@ -784,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
784
806
let trait_item_def_id = hir_id. expect_owner ( ) ;
785
807
786
808
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 , .. } ) => {
788
810
let ( generics, kind) = self . lower_generics (
789
811
generics,
790
812
i. id ,
@@ -797,6 +819,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
797
819
hir:: TraitItemKind :: Const ( ty, body)
798
820
} ,
799
821
) ;
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
+
800
834
( generics, kind, expr. is_some ( ) )
801
835
}
802
836
AssocItemKind :: Fn ( box Fn { sig, generics, body : None , define_opaque, .. } ) => {
@@ -938,18 +972,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
938
972
let attrs = self . lower_attrs ( hir_id, & i. attrs , i. span ) ;
939
973
940
974
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
+ ) ,
953
989
AssocItemKind :: Fn ( box Fn { sig, generics, body, contract, define_opaque, .. } ) => {
954
990
let body_id = self . lower_maybe_coroutine_body (
955
991
sig. span ,
0 commit comments