Skip to content

Commit b44be27

Browse files
committed
Moves changes to explicit_preds_of/inferred_outlives_of/generics_of
1 parent 05dcb78 commit b44be27

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

compiler/rustc_middle/src/ty/consts.rs

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

88-
use hir::{
89-
def::DefKind::ConstParam, def::Res, ExprKind, GenericParam, GenericParamKind, Node,
90-
Path, QPath,
91-
};
88+
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
9289
let val = match expr.kind {
9390
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
9491
// Find the name and index of the const parameter by indexing the generics of
@@ -103,33 +100,7 @@ impl<'tcx> Const<'tcx> {
103100
}
104101
_ => ty::ConstKind::Unevaluated(ty::Unevaluated {
105102
def: def.to_global(),
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-
},
103+
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
133104
promoted: None,
134105
}),
135106
};

compiler/rustc_typeck/src/collect.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,32 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14411441
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
14421442
None
14431443
} else if tcx.lazy_normalization() {
1444+
// Only provide backwards declared generics to cg defaults (#83938)
1445+
if let Node::GenericParam(GenericParam {
1446+
hir_id: param_id,
1447+
kind: GenericParamKind::Const { .. },
1448+
..
1449+
}) = tcx.hir().get(tcx.hir().get_parent_node(hir_id))
1450+
{
1451+
let item_id = tcx.hir().get_parent_node(*param_id);
1452+
let item_def_id = tcx.hir().local_def_id(item_id);
1453+
let generics = tcx.generics_of(item_def_id.to_def_id());
1454+
let param_def = tcx.hir().local_def_id(*param_id).to_def_id();
1455+
let param_def_idx = generics.param_def_id_to_index[&param_def];
1456+
let params = generics.params[..param_def_idx as usize].to_owned();
1457+
let param_def_id_to_index =
1458+
params.iter().map(|param| (param.def_id, param.index)).collect();
1459+
1460+
return ty::Generics {
1461+
parent: generics.parent,
1462+
parent_count: generics.parent_count,
1463+
params,
1464+
param_def_id_to_index,
1465+
has_self: generics.has_self,
1466+
has_late_bound_regions: generics.has_late_bound_regions,
1467+
};
1468+
}
1469+
14441470
// HACK(eddyb) this provides the correct generics when
14451471
// `feature(const_generics)` is enabled, so that const expressions
14461472
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
@@ -2359,7 +2385,8 @@ fn trait_explicit_predicates_and_bounds(
23592385
}
23602386

23612387
fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
2362-
if let DefKind::Trait = tcx.def_kind(def_id) {
2388+
let def_kind = tcx.def_kind(def_id);
2389+
if let DefKind::Trait = def_kind {
23632390
// Remove bounds on associated types from the predicates, they will be
23642391
// returned by `explicit_item_bounds`.
23652392
let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id.expect_local());
@@ -2404,6 +2431,21 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
24042431
}
24052432
}
24062433
} else {
2434+
if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
2435+
// Provide predicates of parent item of cg defaults manually
2436+
// as generics_of doesn't return a parent for the generics
2437+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2438+
if let Node::GenericParam(hir::GenericParam {
2439+
hir_id: param_id,
2440+
kind: hir::GenericParamKind::Const { .. },
2441+
..
2442+
}) = tcx.hir().get(tcx.hir().get_parent_node(hir_id))
2443+
{
2444+
let item_id = tcx.hir().get_parent_node(*param_id);
2445+
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
2446+
return tcx.explicit_predicates_of(item_def_id);
2447+
}
2448+
}
24072449
gather_explicit_predicates_of(tcx, def_id)
24082450
}
24092451
}

compiler/rustc_typeck/src/outlives/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ pub fn provide(providers: &mut Providers) {
2020
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] {
2121
let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
2222

23+
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization()
24+
{
25+
// Provide inferred outlive preds of parent item of cg defaults manually
26+
// as generics_of doesn't return a parent for the generics
27+
if let Node::GenericParam(hir::GenericParam {
28+
hir_id: param_id,
29+
kind: hir::GenericParamKind::Const { .. },
30+
..
31+
}) = tcx.hir().get(tcx.hir().get_parent_node(id))
32+
{
33+
let item_id = tcx.hir().get_parent_node(*param_id);
34+
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
35+
return tcx.inferred_outlives_of(item_def_id);
36+
}
37+
}
38+
2339
match tcx.hir().get(id) {
2440
Node::Item(item) => match item.kind {
2541
hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {

src/test/ui/const-generics/defaults/cec-build-subst-ice.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@ LL | pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }
88
|
99
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:`
1010

11-
error: aborting due to previous error
11+
error: unconstrained generic constant
12+
--> $DIR/cec-build-subst-ice.rs:15:8
13+
|
14+
LL | type Alias<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
15+
| ---------- required by this bound in `Alias::{constant#0}`
16+
LL | fn alias<T, const N: usize>(_: [T; N], _: T)
17+
LL | -> Alias<T, N>
18+
| ^^^^^^^^^^^
19+
|
20+
= help: try adding a `where` bound using this expression: `where [(); {N+1usize}]:`
1221

0 commit comments

Comments
 (0)