@@ -1974,23 +1974,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1974
1974
///
1975
1975
/// Early-bound const parameters get lowered to [`ty::ConstKind::Param`]
1976
1976
/// and late-bound ones to [`ty::ConstKind::Bound`].
1977
- pub ( crate ) fn lower_const_param ( & self , hir_id : HirId ) -> Const < ' tcx > {
1977
+ pub ( crate ) fn lower_const_param ( & self , param_def_id : DefId , path_hir_id : HirId ) -> Const < ' tcx > {
1978
1978
let tcx = self . tcx ( ) ;
1979
- match tcx. named_bound_var ( hir_id) {
1980
- Some ( rbv:: ResolvedArg :: EarlyBound ( def_id) ) => {
1979
+
1980
+ match tcx. named_bound_var ( path_hir_id) {
1981
+ Some ( rbv:: ResolvedArg :: EarlyBound ( _) ) => {
1981
1982
// Find the name and index of the const parameter by indexing the generics of
1982
1983
// the parent item and construct a `ParamConst`.
1983
- let item_def_id = tcx. local_parent ( def_id ) ;
1984
+ let item_def_id = tcx. parent ( param_def_id ) ;
1984
1985
let generics = tcx. generics_of ( item_def_id) ;
1985
- let index = generics. param_def_id_to_index [ & def_id . to_def_id ( ) ] ;
1986
- let name = tcx. item_name ( def_id . to_def_id ( ) ) ;
1986
+ let index = generics. param_def_id_to_index [ & param_def_id ] ;
1987
+ let name = tcx. item_name ( param_def_id ) ;
1987
1988
ty:: Const :: new_param ( tcx, ty:: ParamConst :: new ( index, name) )
1988
1989
}
1989
1990
Some ( rbv:: ResolvedArg :: LateBound ( debruijn, index, _) ) => {
1990
1991
ty:: Const :: new_bound ( tcx, debruijn, ty:: BoundVar :: from_u32 ( index) )
1991
1992
}
1992
1993
Some ( rbv:: ResolvedArg :: Error ( guar) ) => ty:: Const :: new_error ( tcx, guar) ,
1993
- arg => bug ! ( "unexpected bound var resolution for {:?}: {arg:?}" , hir_id ) ,
1994
+ arg => bug ! ( "unexpected bound var resolution for {:?}: {arg:?}" , path_hir_id ) ,
1994
1995
}
1995
1996
}
1996
1997
@@ -2019,10 +2020,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2019
2020
fn lower_const_arg_path ( & self , qpath : hir:: QPath < ' tcx > , hir_id : HirId ) -> Const < ' tcx > {
2020
2021
let tcx = self . tcx ( ) ;
2021
2022
2022
- // TODO: handle path args properly
2023
2023
match qpath {
2024
2024
hir:: QPath :: Resolved ( _, & hir:: Path { res : Res :: Def ( DefKind :: ConstParam , did) , .. } ) => {
2025
- self . lower_const_arg_param ( did, hir_id)
2025
+ self . lower_const_param ( did, hir_id)
2026
2026
}
2027
2027
hir:: QPath :: Resolved (
2028
2028
_,
@@ -2032,31 +2032,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2032
2032
qpath. span ( ) ,
2033
2033
"fn's cannot be used as const args" ,
2034
2034
) ,
2035
- // hir::QPath::Resolved(_, path @ &hir::Path { res: Res::Def(_, did), .. }) => {
2036
- // let (item_segment, _) = path.segments.split_last().unwrap();
2037
- // let args = self.lower_generic_args_of_path_segment(path.span, did, item_segment);
2038
- // ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2039
- // }
2040
- // // TODO: type-relative paths
2041
- // _ => ty::Const::new_error_with_message(
2042
- // tcx,
2043
- // qpath.span(),
2044
- // "Const::lower_const_arg_path: invalid qpath",
2045
- // ),
2046
2035
hir:: QPath :: Resolved ( maybe_qself, path) => {
2047
2036
debug ! ( ?maybe_qself, ?path) ;
2048
2037
let opt_self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . lower_ty ( qself) ) ;
2049
2038
self . lower_const_path_resolved ( opt_self_ty, path, hir_id)
2050
2039
}
2051
-
2052
- // TODO: type-relative paths
2053
- // hir::QPath::TypeRelative(qself, segment) => {
2054
- // debug!(?qself, ?segment);
2055
- // let ty = self.lower_ty(qself);
2056
- // self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false)
2057
- // .map(|(ty, _, _)| ty)
2058
- // .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
2059
- // }
2060
2040
_ => ty:: Const :: new_error_with_message (
2061
2041
tcx,
2062
2042
qpath. span ( ) ,
@@ -2080,7 +2060,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2080
2060
path. segments . iter ( ) ,
2081
2061
GenericsArgsErrExtend :: Param ( def_id) ,
2082
2062
) ;
2083
- self . lower_const_arg_param ( def_id, hir_id)
2063
+ self . lower_const_param ( def_id, hir_id)
2084
2064
}
2085
2065
Res :: Def ( DefKind :: Const | DefKind :: Ctor ( _, CtorKind :: Const ) , did) => {
2086
2066
assert_eq ! ( opt_self_ty, None ) ;
@@ -2095,37 +2075,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2095
2075
) ;
2096
2076
ty:: Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( did, args) )
2097
2077
}
2098
- // TODO: DefKind::AssocConst?
2099
2078
_ => Const :: new_error (
2100
2079
tcx,
2101
2080
tcx. dcx ( ) . span_delayed_bug ( span, "invalid Res for const path" ) ,
2102
2081
) ,
2103
2082
}
2104
2083
}
2105
2084
2106
- /// Lower a const param to a [`Const`]. This is only meant as a helper for [`Self::lower_const_arg_path`].
2107
- /// FIXME: dedup with lower_const_param
2108
- fn lower_const_arg_param ( & self , param_def_id : DefId , path_hir_id : HirId ) -> Const < ' tcx > {
2109
- let tcx = self . tcx ( ) ;
2110
-
2111
- match tcx. named_bound_var ( path_hir_id) {
2112
- Some ( rbv:: ResolvedArg :: EarlyBound ( _) ) => {
2113
- // Find the name and index of the const parameter by indexing the generics of
2114
- // the parent item and construct a `ParamConst`.
2115
- let item_def_id = tcx. parent ( param_def_id) ;
2116
- let generics = tcx. generics_of ( item_def_id) ;
2117
- let index = generics. param_def_id_to_index [ & param_def_id] ;
2118
- let name = tcx. item_name ( param_def_id) ;
2119
- ty:: Const :: new_param ( tcx, ty:: ParamConst :: new ( index, name) )
2120
- }
2121
- Some ( rbv:: ResolvedArg :: LateBound ( debruijn, index, _) ) => {
2122
- ty:: Const :: new_bound ( tcx, debruijn, ty:: BoundVar :: from_u32 ( index) )
2123
- }
2124
- Some ( rbv:: ResolvedArg :: Error ( guar) ) => ty:: Const :: new_error ( tcx, guar) ,
2125
- arg => bug ! ( "unexpected bound var resolution for {:?}: {arg:?}" , path_hir_id) ,
2126
- }
2127
- }
2128
-
2129
2085
fn lower_delegation_ty ( & self , idx : hir:: InferDelegationKind ) -> Ty < ' tcx > {
2130
2086
let delegation_sig = self . tcx ( ) . inherit_sig_for_delegation_item ( self . item_def_id ( ) ) ;
2131
2087
match idx {
@@ -2327,7 +2283,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2327
2283
. type_of ( def_id)
2328
2284
. no_bound_vars ( )
2329
2285
. expect ( "const parameter types cannot be generic" ) ;
2330
- let ct = self . lower_const_param ( expr. hir_id ) ;
2286
+ let ct = self . lower_const_param ( def_id , expr. hir_id ) ;
2331
2287
( ct, ty)
2332
2288
}
2333
2289
0 commit comments