@@ -6,8 +6,8 @@ use rustc_errors::ErrorGuaranteed;
66use rustc_hir:: def:: { DefKind , Res } ;
77use rustc_hir:: def_id:: { CRATE_DEF_ID , LocalDefId } ;
88use rustc_hir:: {
9- self as hir, DistributedSlice , HirId , InvalidDistributedSliceDeclaration , LifetimeSource ,
10- PredicateOrigin ,
9+ self as hir, DistributedSlice , DistributedSliceAdditionManyKind , HirId ,
10+ InvalidDistributedSliceDeclaration , LifetimeSource , PredicateOrigin ,
1111} ;
1212use rustc_index:: { IndexSlice , IndexVec } ;
1313use rustc_middle:: ty:: { ResolverAstLowering , TyCtxt } ;
@@ -25,7 +25,7 @@ use super::{
2525 AstOwner , FnDeclKind , ImplTraitContext , ImplTraitPosition , LoweringContext , ParamMode ,
2626 ResolverAstLoweringExt ,
2727} ;
28- use crate :: errors:: DistributedSliceWithInitializer ;
28+ use crate :: errors:: { DistributedSliceElementsWrongExpr , DistributedSliceWithInitializer } ;
2929
3030pub ( super ) struct ItemLowerer < ' a , ' hir > {
3131 pub ( super ) tcx : TyCtxt < ' hir > ,
@@ -155,6 +155,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
155155 fn lower_distributed_slice (
156156 & mut self ,
157157 distributed_slice : & ast:: DistributedSlice ,
158+ expr : Option < & Expr > ,
158159 ) -> DistributedSlice {
159160 match distributed_slice {
160161 ast:: DistributedSlice :: None => DistributedSlice :: None ,
@@ -179,6 +180,58 @@ impl<'hir> LoweringContext<'_, 'hir> {
179180
180181 DistributedSlice :: Addition ( local)
181182 }
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+ }
182235 }
183236 }
184237
@@ -225,7 +278,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
225278 ident,
226279 ty,
227280 body_id,
228- self . lower_distributed_slice ( distributed_slice) ,
281+ self . lower_distributed_slice ( distributed_slice, e . as_deref ( ) ) ,
229282 )
230283 }
231284 ItemKind :: Const ( box ast:: ConstItem {
@@ -258,7 +311,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
258311 generics,
259312 ty,
260313 body_id,
261- self . lower_distributed_slice ( distributed_slice) ,
314+ self . lower_distributed_slice ( distributed_slice, expr . as_deref ( ) ) ,
262315 )
263316 }
264317 ItemKind :: Fn ( box Fn {
0 commit comments