Add query for const_param_default
This commit is contained in:
parent
0e56a086f7
commit
9fe793ae5d
17 changed files with 108 additions and 27 deletions
|
@ -93,6 +93,12 @@ rustc_queries! {
|
|||
desc { |tcx| "computing the optional const parameter of `{}`", tcx.def_path_str(key.to_def_id()) }
|
||||
}
|
||||
|
||||
/// Given the def_id of a const-generic parameter, computes the associated default const
|
||||
/// parameter. i.e. `fn example<const N: usize=3>` called on N would return 3.
|
||||
query const_param_default(param: DefId) -> &'tcx ty::Const<'tcx> {
|
||||
desc { |tcx| "compute const default for a given parameter `{}`", tcx.def_path_str(param) }
|
||||
}
|
||||
|
||||
/// Records the type of every item.
|
||||
query type_of(key: DefId) -> Ty<'tcx> {
|
||||
desc { |tcx| "computing type of `{}`", tcx.def_path_str(key) }
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::ty::{self, Ty, TyCtxt};
|
|||
use crate::ty::{ParamEnv, ParamEnvAnd};
|
||||
use rustc_errors::ErrorReported;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_macros::HashStable;
|
||||
|
||||
mod int;
|
||||
|
@ -44,11 +44,7 @@ impl<'tcx> Const<'tcx> {
|
|||
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
||||
|
||||
let body_id = match tcx.hir().get(hir_id) {
|
||||
hir::Node::AnonConst(ac)
|
||||
| hir::Node::GenericParam(hir::GenericParam {
|
||||
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
|
||||
..
|
||||
}) => ac.body,
|
||||
hir::Node::AnonConst(ac) => ac.body,
|
||||
_ => span_bug!(
|
||||
tcx.def_span(def.did.to_def_id()),
|
||||
"from_anon_const can only process anonymous constants"
|
||||
|
@ -206,3 +202,19 @@ impl<'tcx> Const<'tcx> {
|
|||
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
let default_def_id = match tcx.hir().get(hir_id) {
|
||||
hir::Node::AnonConst(ac)
|
||||
| hir::Node::GenericParam(hir::GenericParam {
|
||||
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
|
||||
..
|
||||
}) => tcx.hir().local_def_id(ac.hir_id),
|
||||
_ => span_bug!(
|
||||
tcx.def_span(def_id),
|
||||
"const_param_defaults expected a generic parameter with a constant"
|
||||
),
|
||||
};
|
||||
Const::from_anon_const(tcx, default_def_id)
|
||||
}
|
||||
|
|
|
@ -120,8 +120,8 @@ impl<'tcx> Generics {
|
|||
for param in &self.params {
|
||||
match param.kind {
|
||||
GenericParamDefKind::Lifetime => (),
|
||||
GenericParamDefKind::Type { has_default, .. } |
|
||||
GenericParamDefKind::Const { has_default } => {
|
||||
GenericParamDefKind::Type { has_default, .. }
|
||||
| GenericParamDefKind::Const { has_default } => {
|
||||
own_defaults.types += has_default as usize;
|
||||
}
|
||||
}
|
||||
|
@ -146,7 +146,9 @@ impl<'tcx> Generics {
|
|||
pub fn own_requires_monomorphization(&self) -> bool {
|
||||
for param in &self.params {
|
||||
match param.kind {
|
||||
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => return true,
|
||||
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
|
||||
return true;
|
||||
}
|
||||
GenericParamDefKind::Lifetime => {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1949,6 +1949,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
|
|||
trait_impls_of: trait_def::trait_impls_of_provider,
|
||||
all_local_trait_impls: trait_def::all_local_trait_impls,
|
||||
type_uninhabited_from: inhabitedness::type_uninhabited_from,
|
||||
const_param_default: consts::const_param_default,
|
||||
..*providers
|
||||
};
|
||||
}
|
||||
|
|
|
@ -205,10 +205,7 @@ pub trait Printer<'tcx>: Sized {
|
|||
ty::GenericParamDefKind::Const { has_default } => {
|
||||
has_default
|
||||
&& substs[param.index as usize]
|
||||
== GenericArg::from(crate::ty::Const::from_anon_const(
|
||||
self.tcx(),
|
||||
param.def_id.expect_local(),
|
||||
))
|
||||
== GenericArg::from(self.tcx().const_param_default(param.def_id))
|
||||
}
|
||||
})
|
||||
.count();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue