@@ -2401,26 +2401,6 @@ pub struct Const<'tcx> {
2401
2401
#[ cfg( target_arch = "x86_64" ) ]
2402
2402
static_assert_size ! ( Const <' _>, 48 ) ;
2403
2403
2404
- /// Returns the `DefId` of the constant parameter that the provided expression is a path to.
2405
- fn const_param_def_id ( expr : & hir:: Expr < ' _ > ) -> Option < DefId > {
2406
- // Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2407
- // currently have to be wrapped in curly brackets, so it's necessary to special-case.
2408
- let expr = match & expr. kind {
2409
- hir:: ExprKind :: Block ( block, _) if block. stmts . is_empty ( ) && block. expr . is_some ( ) => {
2410
- block. expr . as_ref ( ) . unwrap ( )
2411
- }
2412
- _ => expr,
2413
- } ;
2414
-
2415
- match & expr. kind {
2416
- hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, path) ) => match path. res {
2417
- hir:: def:: Res :: Def ( hir:: def:: DefKind :: ConstParam , did) => Some ( did) ,
2418
- _ => None ,
2419
- } ,
2420
- _ => None ,
2421
- }
2422
- }
2423
-
2424
2404
impl < ' tcx > Const < ' tcx > {
2425
2405
/// Literals and const generic parameters are eagerly converted to a constant, everything else
2426
2406
/// becomes `Unevaluated`.
@@ -2456,20 +2436,36 @@ impl<'tcx> Const<'tcx> {
2456
2436
}
2457
2437
}
2458
2438
2459
- let kind = if let Some ( def_id) = const_param_def_id ( expr) {
2460
- // Find the name and index of the const parameter by indexing the generics of the
2461
- // parent item and construct a `ParamConst`.
2462
- let hir_id = tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
2463
- let item_id = tcx. hir ( ) . get_parent_node ( hir_id) ;
2464
- let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
2465
- let generics = tcx. generics_of ( item_def_id) ;
2466
- let index = generics. param_def_id_to_index [ & tcx. hir ( ) . local_def_id ( hir_id) ] ;
2467
- let name = tcx. hir ( ) . name ( hir_id) ;
2468
- ty:: ConstKind :: Param ( ty:: ParamConst :: new ( index, name) )
2469
- } else {
2470
- ty:: ConstKind :: Unevaluated ( def_id, InternalSubsts :: identity_for_item ( tcx, def_id) , None )
2439
+ // Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2440
+ // currently have to be wrapped in curly brackets, so it's necessary to special-case.
2441
+ let expr = match & expr. kind {
2442
+ hir:: ExprKind :: Block ( block, _) if block. stmts . is_empty ( ) && block. expr . is_some ( ) => {
2443
+ block. expr . as_ref ( ) . unwrap ( )
2444
+ }
2445
+ _ => expr,
2471
2446
} ;
2472
- tcx. mk_const ( ty:: Const { val : kind, ty } )
2447
+
2448
+ use hir:: { def:: DefKind :: ConstParam , def:: Res , ExprKind , Path , QPath } ;
2449
+ let val = match expr. kind {
2450
+ ExprKind :: Path ( QPath :: Resolved ( _, & Path { res : Res :: Def ( ConstParam , def_id) , .. } ) ) => {
2451
+ // Find the name and index of the const parameter by indexing the generics of
2452
+ // the parent item and construct a `ParamConst`.
2453
+ let hir_id = tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
2454
+ let item_id = tcx. hir ( ) . get_parent_node ( hir_id) ;
2455
+ let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
2456
+ let generics = tcx. generics_of ( item_def_id) ;
2457
+ let index = generics. param_def_id_to_index [ & tcx. hir ( ) . local_def_id ( hir_id) ] ;
2458
+ let name = tcx. hir ( ) . name ( hir_id) ;
2459
+ ty:: ConstKind :: Param ( ty:: ParamConst :: new ( index, name) )
2460
+ }
2461
+ _ => ty:: ConstKind :: Unevaluated (
2462
+ def_id,
2463
+ InternalSubsts :: identity_for_item ( tcx, def_id) ,
2464
+ None ,
2465
+ ) ,
2466
+ } ;
2467
+
2468
+ tcx. mk_const ( ty:: Const { val, ty } )
2473
2469
}
2474
2470
2475
2471
#[ inline]
0 commit comments