@@ -2155,6 +2155,17 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
21552155 result_ty
21562156 }
21572157
2158+ /// Returns the `DefId` of the constant parameter that the provided expression is a path to.
2159+ pub fn const_param_def_id ( & self , expr : & hir:: Expr ) -> Option < DefId > {
2160+ match & expr. node {
2161+ ExprKind :: Path ( hir:: QPath :: Resolved ( _, path) ) => match path. res {
2162+ Res :: Def ( DefKind :: ConstParam , did) => Some ( did) ,
2163+ _ => None ,
2164+ } ,
2165+ _ => None ,
2166+ }
2167+ }
2168+
21582169 pub fn ast_const_to_const (
21592170 & self ,
21602171 ast_const : & hir:: AnonConst ,
@@ -2185,19 +2196,17 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
21852196 }
21862197 }
21872198
2188- if let ExprKind :: Path ( ref qpath) = expr. node {
2189- if let hir:: QPath :: Resolved ( _, ref path) = qpath {
2190- if let Res :: Def ( DefKind :: ConstParam , def_id) = path. res {
2191- let node_id = tcx. hir ( ) . as_local_node_id ( def_id) . unwrap ( ) ;
2192- let item_id = tcx. hir ( ) . get_parent_node ( node_id) ;
2193- let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
2194- let generics = tcx. generics_of ( item_def_id) ;
2195- let index = generics. param_def_id_to_index [ & tcx. hir ( ) . local_def_id ( node_id) ] ;
2196- let name = tcx. hir ( ) . name ( node_id) . as_interned_str ( ) ;
2197- const_. val = ConstValue :: Param ( ty:: ParamConst :: new ( index, name) ) ;
2198- }
2199- }
2200- } ;
2199+ if let Some ( def_id) = self . const_param_def_id ( expr) {
2200+ // Find the name and index of the const parameter by indexing the generics of the
2201+ // parent item and construct a `ParamConst`.
2202+ let node_id = tcx. hir ( ) . as_local_node_id ( def_id) . unwrap ( ) ;
2203+ let item_id = tcx. hir ( ) . get_parent_node ( node_id) ;
2204+ let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
2205+ let generics = tcx. generics_of ( item_def_id) ;
2206+ let index = generics. param_def_id_to_index [ & tcx. hir ( ) . local_def_id ( node_id) ] ;
2207+ let name = tcx. hir ( ) . name ( node_id) . as_interned_str ( ) ;
2208+ const_. val = ConstValue :: Param ( ty:: ParamConst :: new ( index, name) ) ;
2209+ }
22012210
22022211 tcx. mk_const ( const_)
22032212 }
0 commit comments