@@ -6,8 +6,8 @@ use rustc_errors::ErrorGuaranteed;
6
6
use rustc_hir:: def:: { DefKind , Res } ;
7
7
use rustc_hir:: def_id:: { CRATE_DEF_ID , LocalDefId } ;
8
8
use rustc_hir:: {
9
- self as hir, DistributedSlice , HirId , InvalidDistributedSliceDeclaration , LifetimeSource ,
10
- PredicateOrigin ,
9
+ self as hir, DistributedSlice , DistributedSliceAdditionManyKind , HirId ,
10
+ InvalidDistributedSliceDeclaration , LifetimeSource , PredicateOrigin ,
11
11
} ;
12
12
use rustc_index:: { IndexSlice , IndexVec } ;
13
13
use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
@@ -25,7 +25,7 @@ use super::{
25
25
AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
26
26
ResolverAstLoweringExt ,
27
27
} ;
28
- use crate :: errors:: DistributedSliceWithInitializer ;
28
+ use crate :: errors:: { DistributedSliceElementsWrongExpr , DistributedSliceWithInitializer } ;
29
29
30
30
pub ( super ) struct ItemLowerer < ' a , ' hir > {
31
31
pub ( super ) tcx : TyCtxt < ' hir > ,
@@ -155,6 +155,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
155
155
fn lower_distributed_slice (
156
156
& mut self ,
157
157
distributed_slice : & ast:: DistributedSlice ,
158
+ expr : Option < & Expr > ,
158
159
) -> DistributedSlice {
159
160
match distributed_slice {
160
161
ast:: DistributedSlice :: None => DistributedSlice :: None ,
@@ -179,6 +180,58 @@ impl<'hir> LoweringContext<'_, 'hir> {
179
180
180
181
DistributedSlice :: Addition ( local)
181
182
}
183
+ ast:: DistributedSlice :: AdditionMany { declaration, id } => {
184
+ let Some ( res) = self . resolver . get_partial_res ( * id) else {
185
+ self . dcx ( ) . span_delayed_bug ( declaration. span , "should have errored in resolve" ) ;
186
+ return DistributedSlice :: None ;
187
+ } ;
188
+
189
+ let Some ( did) = res. expect_full_res ( ) . opt_def_id ( ) else {
190
+ self . dcx ( ) . span_delayed_bug ( declaration. span , "should have errored in resolve" ) ;
191
+ return DistributedSlice :: None ;
192
+ } ;
193
+
194
+ let Some ( local) = did. as_local ( ) else {
195
+ panic ! ( "adding to slice outside local crate" ) ;
196
+ } ;
197
+
198
+ let initializer = expr
199
+ . expect ( "generated by `distributed_slice_elements!` and always has an expr" ) ;
200
+
201
+ DistributedSlice :: AdditionMany (
202
+ local,
203
+ match & initializer. kind {
204
+ ExprKind :: Array ( elems) => {
205
+ DistributedSliceAdditionManyKind :: ArrayLit { length : elems. len ( ) }
206
+ }
207
+ ExprKind :: Path ( _, _) => {
208
+ let Some ( res) = self . resolver . get_partial_res ( initializer. id ) else {
209
+ self . dcx ( ) . span_delayed_bug (
210
+ declaration. span ,
211
+ "should have errored in resolve" ,
212
+ ) ;
213
+ return DistributedSlice :: None ;
214
+ } ;
215
+
216
+ let Some ( did) = res. expect_full_res ( ) . opt_def_id ( ) else {
217
+ self . dcx ( ) . span_delayed_bug (
218
+ declaration. span ,
219
+ "should have errored in resolve" ,
220
+ ) ;
221
+ return DistributedSlice :: None ;
222
+ } ;
223
+
224
+ DistributedSliceAdditionManyKind :: Path { res : did }
225
+ }
226
+ _ => {
227
+ let eg = self . dcx ( ) . emit_err ( DistributedSliceElementsWrongExpr {
228
+ span : initializer. span ,
229
+ } ) ;
230
+ DistributedSliceAdditionManyKind :: Err ( eg)
231
+ }
232
+ } ,
233
+ )
234
+ }
182
235
}
183
236
}
184
237
@@ -225,7 +278,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
225
278
ident,
226
279
ty,
227
280
body_id,
228
- self . lower_distributed_slice ( distributed_slice) ,
281
+ self . lower_distributed_slice ( distributed_slice, e . as_deref ( ) ) ,
229
282
)
230
283
}
231
284
ItemKind :: Const ( box ast:: ConstItem {
@@ -258,7 +311,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
258
311
generics,
259
312
ty,
260
313
body_id,
261
- self . lower_distributed_slice ( distributed_slice) ,
314
+ self . lower_distributed_slice ( distributed_slice, expr . as_deref ( ) ) ,
262
315
)
263
316
}
264
317
ItemKind :: Fn ( box Fn {
0 commit comments