@@ -53,10 +53,8 @@ use rustc_data_structures::sync::Lrc;
5353use rustc_errors:: { DiagArgFromDisplay , DiagCtxtHandle , StashKey } ;
5454use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
5555use rustc_hir:: def_id:: { LocalDefId , LocalDefIdMap , CRATE_DEF_ID , LOCAL_CRATE } ;
56- use rustc_hir:: { self as hir, ConstArgKind } ;
57- use rustc_hir:: {
58- ConstArg , GenericArg , HirId , ItemLocalMap , MissingLifetimeKind , ParamName , TraitCandidate ,
59- } ;
56+ use rustc_hir:: { self as hir} ;
57+ use rustc_hir:: { GenericArg , HirId , ItemLocalMap , MissingLifetimeKind , ParamName , TraitCandidate } ;
6058use rustc_index:: { Idx , IndexSlice , IndexVec } ;
6159use rustc_macros:: extension;
6260use rustc_middle:: span_bug;
@@ -1064,7 +1062,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10641062 AssocItemConstraintKind :: Equality { term } => {
10651063 let term = match term {
10661064 Term :: Ty ( ty) => self . lower_ty ( ty, itctx) . into ( ) ,
1067- Term :: Const ( c) => self . lower_anon_const ( c) . into ( ) ,
1065+ Term :: Const ( c) => self . lower_anon_const_to_anon_const ( c) . into ( ) ,
10681066 } ;
10691067 hir:: AssocItemConstraintKind :: Equality { term }
10701068 }
@@ -1170,53 +1168,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11701168 ty,
11711169 ) ;
11721170
1173- // Construct an AnonConst where the expr is the "ty"'s path.
1174-
1175- let parent_def_id = self . current_def_id_parent ;
1176- let node_id = self . next_node_id ( ) ;
1177- let span = self . lower_span ( ty. span ) ;
1178-
1179- // Add a definition for the in-band const def.
1180- let def_id = self . create_def (
1181- parent_def_id,
1182- node_id,
1183- kw:: Empty ,
1184- DefKind :: AnonConst ,
1185- span,
1186- ) ;
1187-
1188- let path_expr = Expr {
1189- id : ty. id ,
1190- kind : ExprKind :: Path ( None , path. clone ( ) ) ,
1191- span,
1192- attrs : AttrVec :: new ( ) ,
1193- tokens : None ,
1194- } ;
1195-
1196- let ct = self . with_new_scopes ( span, |this| {
1197- self . arena . alloc ( hir:: AnonConst {
1198- def_id,
1199- hir_id : this. lower_node_id ( node_id) ,
1200- body : this
1201- . lower_const_body ( path_expr. span , Some ( & path_expr) ) ,
1202- span,
1203- } )
1204- } ) ;
1205- return GenericArg :: Const ( ConstArg {
1206- kind : ConstArgKind :: Anon ( ct) ,
1207- is_desugared_from_effects : false ,
1208- } ) ;
1171+ let ct =
1172+ self . lower_const_path_to_const_arg ( path, res, ty. id , ty. span ) ;
1173+ return GenericArg :: Const ( ct) ;
12091174 }
12101175 }
12111176 }
12121177 _ => { }
12131178 }
12141179 GenericArg :: Type ( self . lower_ty ( ty, itctx) )
12151180 }
1216- ast:: GenericArg :: Const ( ct) => GenericArg :: Const ( ConstArg {
1217- kind : ConstArgKind :: Anon ( self . lower_anon_const ( ct) ) ,
1218- is_desugared_from_effects : false ,
1219- } ) ,
1181+ ast:: GenericArg :: Const ( ct) => GenericArg :: Const ( self . lower_anon_const_to_const_arg ( ct) ) ,
12201182 }
12211183 }
12221184
@@ -1375,7 +1337,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13751337 TyKind :: Array ( ty, length) => {
13761338 hir:: TyKind :: Array ( self . lower_ty ( ty, itctx) , self . lower_array_length ( length) )
13771339 }
1378- TyKind :: Typeof ( expr) => hir:: TyKind :: Typeof ( self . lower_anon_const ( expr) ) ,
1340+ TyKind :: Typeof ( expr) => hir:: TyKind :: Typeof ( self . lower_anon_const_to_anon_const ( expr) ) ,
13791341 TyKind :: TraitObject ( bounds, kind) => {
13801342 let mut lifetime_bound = None ;
13811343 let ( bounds, lifetime_bound) = self . with_dyn_type_scope ( true , |this| {
@@ -2242,7 +2204,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22422204 false
22432205 }
22442206 } )
2245- . map ( |def| self . lower_anon_const ( def) ) ;
2207+ . map ( |def| self . lower_anon_const_to_anon_const ( def) ) ;
22462208
22472209 (
22482210 hir:: ParamName :: Plain ( self . lower_ident ( param. ident ) ) ,
@@ -2380,14 +2342,66 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23802342 "using `_` for array lengths is unstable" ,
23812343 )
23822344 . stash ( c. value . span , StashKey :: UnderscoreForArrayLengths ) ;
2383- hir:: ArrayLen :: Body ( self . lower_anon_const ( c) )
2345+ hir:: ArrayLen :: Body ( self . lower_anon_const_to_anon_const ( c) )
23842346 }
23852347 }
2386- _ => hir:: ArrayLen :: Body ( self . lower_anon_const ( c) ) ,
2348+ _ => hir:: ArrayLen :: Body ( self . lower_anon_const_to_anon_const ( c) ) ,
2349+ }
2350+ }
2351+
2352+ fn lower_const_path_to_const_arg (
2353+ & mut self ,
2354+ path : & Path ,
2355+ _res : Res < NodeId > ,
2356+ ty_id : NodeId ,
2357+ span : Span ,
2358+ ) -> & ' hir hir:: ConstArg < ' hir > {
2359+ // Construct an AnonConst where the expr is the "ty"'s path.
2360+
2361+ let parent_def_id = self . current_def_id_parent ;
2362+ let node_id = self . next_node_id ( ) ;
2363+ let span = self . lower_span ( span) ;
2364+
2365+ // Add a definition for the in-band const def.
2366+ let def_id = self . create_def ( parent_def_id, node_id, kw:: Empty , DefKind :: AnonConst , span) ;
2367+
2368+ let path_expr = Expr {
2369+ id : ty_id,
2370+ kind : ExprKind :: Path ( None , path. clone ( ) ) ,
2371+ span,
2372+ attrs : AttrVec :: new ( ) ,
2373+ tokens : None ,
2374+ } ;
2375+
2376+ let ct = self . with_new_scopes ( span, |this| {
2377+ self . arena . alloc ( hir:: AnonConst {
2378+ def_id,
2379+ hir_id : this. lower_node_id ( node_id) ,
2380+ body : this. lower_const_body ( path_expr. span , Some ( & path_expr) ) ,
2381+ span,
2382+ } )
2383+ } ) ;
2384+
2385+ self . arena . alloc ( hir:: ConstArg {
2386+ kind : hir:: ConstArgKind :: Anon ( ct) ,
2387+ is_desugared_from_effects : false ,
2388+ } )
2389+ }
2390+
2391+ fn lower_anon_const_to_const_arg ( & mut self , anon : & AnonConst ) -> & ' hir hir:: ConstArg < ' hir > {
2392+ self . arena . alloc ( self . lower_anon_const_to_const_arg_direct ( anon) )
2393+ }
2394+
2395+ #[ instrument( level = "debug" , skip( self ) ) ]
2396+ fn lower_anon_const_to_const_arg_direct ( & mut self , anon : & AnonConst ) -> hir:: ConstArg < ' hir > {
2397+ let lowered_anon = self . lower_anon_const_to_anon_const ( anon) ;
2398+ hir:: ConstArg {
2399+ kind : hir:: ConstArgKind :: Anon ( lowered_anon) ,
2400+ is_desugared_from_effects : false ,
23872401 }
23882402 }
23892403
2390- fn lower_anon_const ( & mut self , c : & AnonConst ) -> & ' hir hir:: AnonConst {
2404+ fn lower_anon_const_to_anon_const ( & mut self , c : & AnonConst ) -> & ' hir hir:: AnonConst {
23912405 self . arena . alloc ( self . with_new_scopes ( c. value . span , |this| hir:: AnonConst {
23922406 def_id : this. local_def_id ( c. id ) ,
23932407 hir_id : this. lower_node_id ( c. id ) ,
0 commit comments