Dont provide all parent generics to cgdefaults

This commit is contained in:
Ellen 2021-06-23 18:18:32 +01:00
parent a84d1b21ae
commit 05dcb7874a
3 changed files with 53 additions and 2 deletions

View file

@ -85,7 +85,10 @@ impl<'tcx> Const<'tcx> {
_ => expr, _ => expr,
}; };
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath}; use hir::{
def::DefKind::ConstParam, def::Res, ExprKind, GenericParam, GenericParamKind, Node,
Path, QPath,
};
let val = match expr.kind { let val = match expr.kind {
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
// Find the name and index of the const parameter by indexing the generics of // Find the name and index of the const parameter by indexing the generics of
@ -100,7 +103,33 @@ impl<'tcx> Const<'tcx> {
} }
_ => ty::ConstKind::Unevaluated(ty::Unevaluated { _ => ty::ConstKind::Unevaluated(ty::Unevaluated {
def: def.to_global(), def: def.to_global(),
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()), substs: {
let ct_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
let parent_id = tcx.hir().get_parent_node(ct_hir_id);
match tcx.hir().get(parent_id) {
// If this anon ct is a cg default we should only provide non-fwd declared params
// https://github.com/rust-lang/rust/issues/83938
Node::GenericParam(GenericParam {
hir_id: param_id,
kind: GenericParamKind::Const { .. },
..
}) => {
let item_id = tcx.hir().get_parent_node(*param_id);
let item_def_id = tcx.hir().local_def_id(item_id);
let generics = tcx.generics_of(item_def_id.to_def_id());
let param_def = tcx.hir().local_def_id(*param_id).to_def_id();
let param_def_idx = generics.param_def_id_to_index[&param_def];
let substs = generics
.params
.iter()
.map(|param| tcx.mk_param_from_def(param))
.take(param_def_idx as usize)
.collect::<smallvec::SmallVec<[_; 8]>>();
tcx.intern_substs(&substs)
}
_ => InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
}
},
promoted: None, promoted: None,
}), }),
}; };

View file

@ -0,0 +1,10 @@
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
#![allow(incomplete_features)]
pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
pub fn foo<const N1: usize>() -> Bar<N1> {
loop {}
}
//~^ error: unconstrained generic constant
fn main() {}

View file

@ -0,0 +1,12 @@
error: unconstrained generic constant
--> $DIR/cec-build-subst-ice.rs:5:34
|
LL | pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
| --------- required by this bound in `Bar`
LL | pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }
| ^^^^^^^
|
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:`
error: aborting due to previous error