Skip to content

Commit d9aa287

Browse files
committed
Auto merge of #86580 - BoxyUwU:cgd-subst-ice, r=nikomatsakis
dont provide fwd declared params to cg defaults Fixes #83938 ```rust #![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 {} } fn main() {} ``` This PR makes this code no longer ICE, it was ICE'ing previously because when building substs for `Bar<N1>` we would subst the anon ct: `ConstKind::Unevaluated({N + 1}, substs: [N, M])` with substs of `[N1]`. the anon const has forward declared params supplied though so we end up trying to substitute the provided `M` param which causes the ICE. This PR doesn't handle the predicates of the const so ```rust trait Foo<const N: usize> { const Assoc: usize; } pub struct Bar<const N: usize = { <()>::Assoc }> where (): Foo<N>; ``` Resolves to `<() as Foo<N>>::Assoc` which can allow for using fwd declared params indirectly. ```rust trait Foo<const N: usize> {} struct Bar<const N: usize = { 2 + 3 }> where (): Foo<N>; ``` This code also ICEs under this PR because instantiating the default's predicates causes an ICE as predicates_of contains predicates with fwd declared params PR was briefly discussed [in this zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/evil.20preds.20in.20param.20env.20.2386580)
2 parents bddb59c + d1e5e72 commit d9aa287

File tree

10 files changed

+201
-1
lines changed

10 files changed

+201
-1
lines changed

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,9 @@ pub type Lit = Spanned<LitKind>;
14221422
/// These are usually found nested inside types (e.g., array lengths)
14231423
/// or expressions (e.g., repeat counts), and also used to define
14241424
/// explicit discriminant values for enum variants.
1425+
///
1426+
/// You can check if this anon const is a default in a const param
1427+
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_hir_id(..)`
14251428
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
14261429
pub struct AnonConst {
14271430
pub hir_id: HirId,

compiler/rustc_middle/src/hir/map/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,19 @@ impl<'hir> Map<'hir> {
919919
pub fn node_to_string(&self, id: HirId) -> String {
920920
hir_id_to_string(self, id)
921921
}
922+
923+
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
924+
/// called with the HirId for the `{ ... }` anon const
925+
pub fn opt_const_param_default_param_hir_id(&self, anon_const: HirId) -> Option<HirId> {
926+
match self.get(self.get_parent_node(anon_const)) {
927+
Node::GenericParam(GenericParam {
928+
hir_id: param_id,
929+
kind: GenericParamKind::Const { .. },
930+
..
931+
}) => Some(*param_id),
932+
_ => None,
933+
}
934+
}
922935
}
923936

924937
impl<'hir> intravisit::Map<'hir> for Map<'hir> {

compiler/rustc_typeck/src/collect.rs

+68-1
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,52 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14371437
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
14381438
None
14391439
} else if tcx.lazy_normalization() {
1440+
if let Some(param_id) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) {
1441+
// If the def_id we are calling generics_of on is an anon ct default i.e:
1442+
//
1443+
// struct Foo<const N: usize = { .. }>;
1444+
// ^^^ ^ ^^^^^^ def id of this anon const
1445+
// ^ ^ param_id
1446+
// ^ parent_def_id
1447+
//
1448+
// then we only want to return generics for params to the left of `N`. If we don't do that we
1449+
// end up with that const looking like: `ty::ConstKind::Unevaluated(def_id, substs: [N#0])`.
1450+
//
1451+
// This causes ICEs (#86580) when building the substs for Foo in `fn foo() -> Foo { .. }` as
1452+
// we substitute the defaults with the partially built substs when we build the substs. Subst'ing
1453+
// the `N#0` on the unevaluated const indexes into the empty substs we're in the process of building.
1454+
//
1455+
// We fix this by having this function return the parent's generics ourselves and truncating the
1456+
// generics to only include non-forward declared params (with the exception of the `Self` ty)
1457+
//
1458+
// For the above code example that means we want `substs: []`
1459+
// For the following struct def we want `substs: [N#0]` when generics_of is called on
1460+
// the def id of the `{ N + 1 }` anon const
1461+
// struct Foo<const N: usize, const M: usize = { N + 1 }>;
1462+
//
1463+
// This has some implications for how we get the predicates available to the anon const
1464+
// see `explicit_predicates_of` for more information on this
1465+
let generics = tcx.generics_of(parent_def_id.to_def_id());
1466+
let param_def = tcx.hir().local_def_id(param_id).to_def_id();
1467+
let param_def_idx = generics.param_def_id_to_index[&param_def];
1468+
// In the above example this would be .params[..N#0]
1469+
let params = generics.params[..param_def_idx as usize].to_owned();
1470+
let param_def_id_to_index =
1471+
params.iter().map(|param| (param.def_id, param.index)).collect();
1472+
1473+
return ty::Generics {
1474+
// we set the parent of these generics to be our parent's parent so that we
1475+
// dont end up with substs: [N, M, N] for the const default on a struct like this:
1476+
// struct Foo<const N: usize, const M: usize = { ... }>;
1477+
parent: generics.parent,
1478+
parent_count: generics.parent_count,
1479+
params,
1480+
param_def_id_to_index,
1481+
has_self: generics.has_self,
1482+
has_late_bound_regions: generics.has_late_bound_regions,
1483+
};
1484+
}
1485+
14401486
// HACK(eddyb) this provides the correct generics when
14411487
// `feature(const_generics)` is enabled, so that const expressions
14421488
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
@@ -2355,7 +2401,8 @@ fn trait_explicit_predicates_and_bounds(
23552401
}
23562402

23572403
fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
2358-
if let DefKind::Trait = tcx.def_kind(def_id) {
2404+
let def_kind = tcx.def_kind(def_id);
2405+
if let DefKind::Trait = def_kind {
23592406
// Remove bounds on associated types from the predicates, they will be
23602407
// returned by `explicit_item_bounds`.
23612408
let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id.expect_local());
@@ -2400,6 +2447,26 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
24002447
}
24012448
}
24022449
} else {
2450+
if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
2451+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2452+
if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(hir_id) {
2453+
// In `generics_of` we set the generics' parent to be our parent's parent which means that
2454+
// we lose out on the predicates of our actual parent if we dont return those predicates here.
2455+
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
2456+
//
2457+
// struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
2458+
// ^^^ ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
2459+
// ^^^ explicit_predicates_of on
2460+
// parent item we dont have set as the
2461+
// parent of generics returned by `generics_of`
2462+
//
2463+
// In the above code we want the anon const to have predicates in its param env for `T: Trait`
2464+
let item_id = tcx.hir().get_parent_item(hir_id);
2465+
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
2466+
// In the above code example we would be calling `explicit_predicates_of(Foo)` here
2467+
return tcx.explicit_predicates_of(item_def_id);
2468+
}
2469+
}
24032470
gather_explicit_predicates_of(tcx, def_id)
24042471
}
24052472
}

compiler/rustc_typeck/src/outlives/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ 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+
if let Some(_) = tcx.hir().opt_const_param_default_param_hir_id(id) {
26+
// In `generics_of` we set the generics' parent to be our parent's parent which means that
27+
// we lose out on the predicates of our actual parent if we dont return those predicates here.
28+
// (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
29+
//
30+
// struct Foo<'a, 'b, const N: usize = { ... }>(&'a &'b ());
31+
// ^^^ ^^^^^^^ the def id we are calling
32+
// ^^^ inferred_outlives_of on
33+
// parent item we dont have set as the
34+
// parent of generics returned by `generics_of`
35+
//
36+
// In the above code we want the anon const to have predicates in its param env for `'b: 'a`
37+
let item_id = tcx.hir().get_parent_item(id);
38+
let item_def_id = tcx.hir().local_def_id(item_id).to_def_id();
39+
// In the above code example we would be calling `inferred_outlives_of(Foo)` here
40+
return tcx.inferred_outlives_of(item_def_id);
41+
}
42+
}
43+
2344
match tcx.hir().get(id) {
2445
Node::Item(item) => match item.kind {
2546
hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(const_generics, const_evaluatable_checked, const_generics_defaults)]
2+
#![allow(incomplete_features)]
3+
4+
struct Foo<const N: usize, const M: usize = { N + 1 }>;
5+
fn no_constraining() -> Foo<10> {
6+
Foo::<10, 11>
7+
}
8+
9+
pub fn different_than_default() -> Foo<10> {
10+
Foo::<10, 12>
11+
//~^ error: mismatched types
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cec-concrete-default.rs:10:5
3+
|
4+
LL | Foo::<10, 12>
5+
| ^^^^^^^^^^^^^ expected `11_usize`, found `12_usize`
6+
|
7+
= note: expected type `11_usize`
8+
found type `12_usize`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(const_generics, const_evaluatable_checked, const_generics_defaults)]
2+
#![allow(incomplete_features)]
3+
4+
struct Foo<const N: usize, const M: usize = { N + 1 }>;
5+
fn should_unify<const N: usize>() -> Foo<N> where [(); { N + 1 }]: {
6+
Foo::<N, { N + 1 }>
7+
}
8+
pub fn shouldnt_unify<const N: usize>() -> Foo<N>
9+
where
10+
[(); { N + 1 }]:,
11+
[(); { N + 2 }]:, {
12+
Foo::<N, { N + 2 }>
13+
//~^ error: mismatched types
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cec-generic-default-mismatched-types.rs:12:5
3+
|
4+
LL | Foo::<N, { N + 2 }>
5+
| ^^^^^^^^^^^^^^^^^^^ expected `{ N + 1 }`, found `{ N + 2 }`
6+
|
7+
= note: expected type `{ N + 1 }`
8+
found type `{ N + 2 }`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
2+
#![allow(incomplete_features)]
3+
4+
pub struct Foo<const N: usize, const M: usize = { N + 1 }>;
5+
pub fn needs_evaluatable_bound<const N1: usize>() -> Foo<N1> {
6+
//~^ error: unconstrained generic constant
7+
loop {}
8+
}
9+
pub fn has_evaluatable_bound<const N1: usize>() -> Foo<N1> where [(); N1 + 1]: {
10+
loop {}
11+
}
12+
13+
type FooAlias<const N: usize, const NP: usize = { N + 1 }> = [(); NP];
14+
fn needs_evaluatable_bound_alias<T, const N: usize>() -> FooAlias<N>
15+
{
16+
//~^^ error: unconstrained generic constant
17+
todo!()
18+
}
19+
fn has_evaluatable_bound_alias<const N: usize>() -> FooAlias<N>
20+
where [(); N + 1]: {
21+
todo!()
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unconstrained generic constant
2+
--> $DIR/cec-generic-default.rs:5:54
3+
|
4+
LL | pub fn needs_evaluatable_bound<const N1: usize>() -> Foo<N1> {
5+
| ^^^^^^^
6+
|
7+
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:`
8+
9+
error: unconstrained generic constant
10+
--> $DIR/cec-generic-default.rs:14:58
11+
|
12+
LL | fn needs_evaluatable_bound_alias<T, const N: usize>() -> FooAlias<N>
13+
| ^^^^^^^^^^^
14+
|
15+
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)