@@ -5,7 +5,10 @@ use rustc_ast::*;
55use rustc_errors:: ErrorGuaranteed ;
66use rustc_hir:: def:: { DefKind , Res } ;
77use rustc_hir:: def_id:: { CRATE_DEF_ID , LocalDefId } ;
8- use rustc_hir:: { self as hir, DistributedSlice , HirId , LifetimeSource , PredicateOrigin } ;
8+ use rustc_hir:: {
9+ self as hir, DistributedSlice , HirId , InvalidDistributedSliceDeclaration , LifetimeSource ,
10+ PredicateOrigin ,
11+ } ;
912use rustc_index:: { IndexSlice , IndexVec } ;
1013use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
1114use rustc_span:: edit_distance:: find_best_match_for_name;
@@ -22,6 +25,7 @@ use super::{
2225 AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
2326 ResolverAstLoweringExt ,
2427} ;
28+ use crate :: errors:: DistributedSliceWithInitializer ;
2529
2630pub ( super ) struct ItemLowerer < ' a , ' hir > {
2731 pub ( super ) tcx : TyCtxt < ' hir > ,
@@ -154,6 +158,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154158 ) -> DistributedSlice {
155159 match distributed_slice {
156160 ast:: DistributedSlice :: None => DistributedSlice :: None ,
161+ ast:: DistributedSlice :: Err ( _) => DistributedSlice :: None ,
157162 ast:: DistributedSlice :: Declaration ( span, _) => {
158163 DistributedSlice :: Declaration ( self . lower_span ( * span) )
159164 }
@@ -733,13 +738,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
733738 let ty = self . lower_ty (
734739 ty,
735740 ImplTraitContext :: Disallowed ( ImplTraitPosition :: StaticTy ) ,
736- false ,
741+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
737742 ) ;
738743 let safety = self . lower_safety ( * safety, hir:: Safety :: Unsafe ) ;
739744 if define_opaque. is_some ( ) {
740745 self . dcx ( ) . span_err ( i. span , "foreign statics cannot define opaque types" ) ;
741746 }
742- ( ident, hir:: ForeignItemKind :: Static ( ty, * mutability, safety) )
747+ (
748+ ident,
749+ hir:: ForeignItemKind :: Static (
750+ ty,
751+ * mutability,
752+ safety,
753+ if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
754+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
755+ } else {
756+ InvalidDistributedSliceDeclaration :: No
757+ } ,
758+ ) ,
759+ )
743760 }
744761 ForeignItemKind :: TyAlias ( box TyAlias { ident, .. } ) => {
745762 ( ident, hir:: ForeignItemKind :: Type )
@@ -861,6 +878,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
861878 ty,
862879 expr,
863880 define_opaque,
881+ distributed_slice,
864882 ..
865883 } ) => {
866884 let ( generics, kind) = self . lower_generics (
@@ -871,12 +889,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
871889 let ty = this. lower_ty (
872890 ty,
873891 ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ,
874- false ,
892+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
875893 ) ;
876894 let body =
877895 expr. as_ref ( ) . map ( |x| this. lower_const_body ( i. span , Some ( x) , None ) ) ;
878896
879- hir:: TraitItemKind :: Const ( ty, body)
897+ hir:: TraitItemKind :: Const (
898+ ty,
899+ body,
900+ if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
901+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
902+ } else {
903+ InvalidDistributedSliceDeclaration :: No
904+ } ,
905+ )
880906 } ,
881907 ) ;
882908
@@ -1058,6 +1084,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10581084 ty,
10591085 expr,
10601086 define_opaque,
1087+ distributed_slice,
10611088 ..
10621089 } ) => (
10631090 * ident,
@@ -1069,11 +1096,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
10691096 let ty = this. lower_ty (
10701097 ty,
10711098 ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ,
1072- false ,
1099+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
10731100 ) ;
10741101 let body = this. lower_const_body ( i. span , expr. as_deref ( ) , None ) ;
10751102 this. lower_define_opaque ( hir_id, & define_opaque) ;
1076- hir:: ImplItemKind :: Const ( ty, body)
1103+ hir:: ImplItemKind :: Const (
1104+ ty,
1105+ body,
1106+ if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
1107+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
1108+ } else {
1109+ InvalidDistributedSliceDeclaration :: No
1110+ } ,
1111+ )
10771112 } ,
10781113 ) ,
10791114 ) ,
@@ -1369,7 +1404,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
13691404 span : this. lower_span ( span) ,
13701405 }
13711406 }
1372- ( Some ( expr) , Some ( _) ) => panic ! ( "distributed slice with initializer" ) ,
1407+ ( Some ( _) , Some ( node_id) ) => {
1408+ let eg = this. tcx . dcx ( ) . emit_err ( DistributedSliceWithInitializer { span } ) ;
1409+
1410+ let expr_hir_id = this. lower_node_id ( node_id) ;
1411+ hir:: Expr {
1412+ hir_id : expr_hir_id,
1413+ kind : rustc_hir:: ExprKind :: Err ( eg) ,
1414+ span : this. lower_span ( span) ,
1415+ }
1416+ }
13731417 ( None , None ) => {
13741418 this. expr_err ( span, this. dcx ( ) . span_delayed_bug ( span, "no block" ) )
13751419 }
0 commit comments