Skip to content

Commit 05dcb78

Browse files
committed
Dont provide all parent generics to cgdefaults
1 parent a84d1b2 commit 05dcb78

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

compiler/rustc_middle/src/ty/consts.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ impl<'tcx> Const<'tcx> {
8585
_ => expr,
8686
};
8787

88-
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
88+
use hir::{
89+
def::DefKind::ConstParam, def::Res, ExprKind, GenericParam, GenericParamKind, Node,
90+
Path, QPath,
91+
};
8992
let val = match expr.kind {
9093
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
9194
// Find the name and index of the const parameter by indexing the generics of
@@ -100,7 +103,33 @@ impl<'tcx> Const<'tcx> {
100103
}
101104
_ => ty::ConstKind::Unevaluated(ty::Unevaluated {
102105
def: def.to_global(),
103-
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
106+
substs: {
107+
let ct_hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
108+
let parent_id = tcx.hir().get_parent_node(ct_hir_id);
109+
match tcx.hir().get(parent_id) {
110+
// If this anon ct is a cg default we should only provide non-fwd declared params
111+
// https://github.com/rust-lang/rust/issues/83938
112+
Node::GenericParam(GenericParam {
113+
hir_id: param_id,
114+
kind: GenericParamKind::Const { .. },
115+
..
116+
}) => {
117+
let item_id = tcx.hir().get_parent_node(*param_id);
118+
let item_def_id = tcx.hir().local_def_id(item_id);
119+
let generics = tcx.generics_of(item_def_id.to_def_id());
120+
let param_def = tcx.hir().local_def_id(*param_id).to_def_id();
121+
let param_def_idx = generics.param_def_id_to_index[&param_def];
122+
let substs = generics
123+
.params
124+
.iter()
125+
.map(|param| tcx.mk_param_from_def(param))
126+
.take(param_def_idx as usize)
127+
.collect::<smallvec::SmallVec<[_; 8]>>();
128+
tcx.intern_substs(&substs)
129+
}
130+
_ => InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
131+
}
132+
},
104133
promoted: None,
105134
}),
106135
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
2+
#![allow(incomplete_features)]
3+
4+
pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
5+
pub fn foo<const N1: usize>() -> Bar<N1> {
6+
loop {}
7+
}
8+
//~^ error: unconstrained generic constant
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: unconstrained generic constant
2+
--> $DIR/cec-build-subst-ice.rs:5:34
3+
|
4+
LL | pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
5+
| --------- required by this bound in `Bar`
6+
LL | pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }
7+
| ^^^^^^^
8+
|
9+
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:`
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)