@@ -5,7 +5,7 @@ use rustc_ast::*;
5
5
use rustc_errors:: ErrorGuaranteed ;
6
6
use rustc_hir:: def:: { DefKind , Res } ;
7
7
use rustc_hir:: def_id:: { CRATE_DEF_ID , LocalDefId } ;
8
- use rustc_hir:: { self as hir, DistributedSlice , HirId , LifetimeSource , PredicateOrigin } ;
8
+ use rustc_hir:: { self as hir, DistributedSlice , HirId , InvalidDistributedSliceDeclaration , LifetimeSource , PredicateOrigin } ;
9
9
use rustc_index:: { IndexSlice , IndexVec } ;
10
10
use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
11
11
use rustc_span:: edit_distance:: find_best_match_for_name;
@@ -14,6 +14,8 @@ use smallvec::{SmallVec, smallvec};
14
14
use thin_vec:: ThinVec ;
15
15
use tracing:: instrument;
16
16
17
+ use crate :: errors:: DistributedSliceWithInitializer ;
18
+
17
19
use super :: errors:: {
18
20
InvalidAbi , InvalidAbiSuggestion , MisplacedRelaxTraitBound , TupleStructWithDefault ,
19
21
} ;
@@ -154,6 +156,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154
156
) -> DistributedSlice {
155
157
match distributed_slice {
156
158
ast:: DistributedSlice :: None => DistributedSlice :: None ,
159
+ ast:: DistributedSlice :: Err ( _) => DistributedSlice :: None ,
157
160
ast:: DistributedSlice :: Declaration ( span, _) => {
158
161
DistributedSlice :: Declaration ( self . lower_span ( * span) )
159
162
}
@@ -733,13 +736,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
733
736
let ty = self . lower_ty (
734
737
ty,
735
738
ImplTraitContext :: Disallowed ( ImplTraitPosition :: StaticTy ) ,
736
- false ,
739
+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
737
740
) ;
738
741
let safety = self . lower_safety ( * safety, hir:: Safety :: Unsafe ) ;
739
742
if define_opaque. is_some ( ) {
740
743
self . dcx ( ) . span_err ( i. span , "foreign statics cannot define opaque types" ) ;
741
744
}
742
- ( ident, hir:: ForeignItemKind :: Static ( ty, * mutability, safety) )
745
+ ( ident, hir:: ForeignItemKind :: Static ( ty, * mutability, safety, if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
746
+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
747
+ } else {
748
+ InvalidDistributedSliceDeclaration :: No
749
+ } ) )
743
750
}
744
751
ForeignItemKind :: TyAlias ( box TyAlias { ident, .. } ) => {
745
752
( ident, hir:: ForeignItemKind :: Type )
@@ -861,6 +868,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
861
868
ty,
862
869
expr,
863
870
define_opaque,
871
+ distributed_slice,
864
872
..
865
873
} ) => {
866
874
let ( generics, kind) = self . lower_generics (
@@ -871,12 +879,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
871
879
let ty = this. lower_ty (
872
880
ty,
873
881
ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ,
874
- false ,
882
+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
875
883
) ;
876
884
let body =
877
885
expr. as_ref ( ) . map ( |x| this. lower_const_body ( i. span , Some ( x) , None ) ) ;
878
886
879
- hir:: TraitItemKind :: Const ( ty, body)
887
+ hir:: TraitItemKind :: Const ( ty, body, if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
888
+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
889
+ } else {
890
+ InvalidDistributedSliceDeclaration :: No
891
+ } )
880
892
} ,
881
893
) ;
882
894
@@ -1058,6 +1070,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1058
1070
ty,
1059
1071
expr,
1060
1072
define_opaque,
1073
+ distributed_slice,
1061
1074
..
1062
1075
} ) => (
1063
1076
* ident,
@@ -1069,11 +1082,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
1069
1082
let ty = this. lower_ty (
1070
1083
ty,
1071
1084
ImplTraitContext :: Disallowed ( ImplTraitPosition :: ConstTy ) ,
1072
- false ,
1085
+ matches ! ( distributed_slice , ast :: DistributedSlice :: Err ( _ ) ) ,
1073
1086
) ;
1074
1087
let body = this. lower_const_body ( i. span , expr. as_deref ( ) , None ) ;
1075
1088
this. lower_define_opaque ( hir_id, & define_opaque) ;
1076
- hir:: ImplItemKind :: Const ( ty, body)
1089
+ hir:: ImplItemKind :: Const ( ty, body, if let ast:: DistributedSlice :: Err ( eg) = distributed_slice {
1090
+ InvalidDistributedSliceDeclaration :: Yes ( * eg)
1091
+ } else {
1092
+ InvalidDistributedSliceDeclaration :: No
1093
+ } )
1077
1094
} ,
1078
1095
) ,
1079
1096
) ,
@@ -1369,7 +1386,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
1369
1386
span : this. lower_span ( span) ,
1370
1387
}
1371
1388
}
1372
- ( Some ( expr) , Some ( _) ) => panic ! ( "distributed slice with initializer" ) ,
1389
+ ( Some ( _) , Some ( node_id) ) => {
1390
+ let eg = this. tcx . dcx ( ) . emit_err ( DistributedSliceWithInitializer {
1391
+ span,
1392
+ } ) ;
1393
+
1394
+ let expr_hir_id = this. lower_node_id ( node_id) ;
1395
+ hir:: Expr {
1396
+ hir_id : expr_hir_id,
1397
+ kind : rustc_hir:: ExprKind :: Err ( eg) ,
1398
+ span : this. lower_span ( span) ,
1399
+ }
1400
+ } ,
1373
1401
( None , None ) => {
1374
1402
this. expr_err ( span, this. dcx ( ) . span_delayed_bug ( span, "no block" ) )
1375
1403
}
0 commit comments