-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)
- Loading branch information
Showing
10 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/ui/const-generics/defaults/cec-concrete-default.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![feature(const_generics, const_evaluatable_checked, const_generics_defaults)] | ||
#![allow(incomplete_features)] | ||
|
||
struct Foo<const N: usize, const M: usize = { N + 1 }>; | ||
fn no_constraining() -> Foo<10> { | ||
Foo::<10, 11> | ||
} | ||
|
||
pub fn different_than_default() -> Foo<10> { | ||
Foo::<10, 12> | ||
//~^ error: mismatched types | ||
} | ||
|
||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/const-generics/defaults/cec-concrete-default.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/cec-concrete-default.rs:10:5 | ||
| | ||
LL | Foo::<10, 12> | ||
| ^^^^^^^^^^^^^ expected `11_usize`, found `12_usize` | ||
| | ||
= note: expected type `11_usize` | ||
found type `12_usize` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
16 changes: 16 additions & 0 deletions
16
src/test/ui/const-generics/defaults/cec-generic-default-mismatched-types.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![feature(const_generics, const_evaluatable_checked, const_generics_defaults)] | ||
#![allow(incomplete_features)] | ||
|
||
struct Foo<const N: usize, const M: usize = { N + 1 }>; | ||
fn should_unify<const N: usize>() -> Foo<N> where [(); { N + 1 }]: { | ||
Foo::<N, { N + 1 }> | ||
} | ||
pub fn shouldnt_unify<const N: usize>() -> Foo<N> | ||
where | ||
[(); { N + 1 }]:, | ||
[(); { N + 2 }]:, { | ||
Foo::<N, { N + 2 }> | ||
//~^ error: mismatched types | ||
} | ||
|
||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/const-generics/defaults/cec-generic-default-mismatched-types.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/cec-generic-default-mismatched-types.rs:12:5 | ||
| | ||
LL | Foo::<N, { N + 2 }> | ||
| ^^^^^^^^^^^^^^^^^^^ expected `{ N + 1 }`, found `{ N + 2 }` | ||
| | ||
= note: expected type `{ N + 1 }` | ||
found type `{ N + 2 }` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
24 changes: 24 additions & 0 deletions
24
src/test/ui/const-generics/defaults/cec-generic-default.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)] | ||
#![allow(incomplete_features)] | ||
|
||
pub struct Foo<const N: usize, const M: usize = { N + 1 }>; | ||
pub fn needs_evaluatable_bound<const N1: usize>() -> Foo<N1> { | ||
//~^ error: unconstrained generic constant | ||
loop {} | ||
} | ||
pub fn has_evaluatable_bound<const N1: usize>() -> Foo<N1> where [(); N1 + 1]: { | ||
loop {} | ||
} | ||
|
||
type FooAlias<const N: usize, const NP: usize = { N + 1 }> = [(); NP]; | ||
fn needs_evaluatable_bound_alias<T, const N: usize>() -> FooAlias<N> | ||
{ | ||
//~^^ error: unconstrained generic constant | ||
todo!() | ||
} | ||
fn has_evaluatable_bound_alias<const N: usize>() -> FooAlias<N> | ||
where [(); N + 1]: { | ||
todo!() | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/const-generics/defaults/cec-generic-default.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error: unconstrained generic constant | ||
--> $DIR/cec-generic-default.rs:5:54 | ||
| | ||
LL | pub fn needs_evaluatable_bound<const N1: usize>() -> Foo<N1> { | ||
| ^^^^^^^ | ||
| | ||
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:` | ||
|
||
error: unconstrained generic constant | ||
--> $DIR/cec-generic-default.rs:14:58 | ||
| | ||
LL | fn needs_evaluatable_bound_alias<T, const N: usize>() -> FooAlias<N> | ||
| ^^^^^^^^^^^ | ||
| | ||
= help: try adding a `where` bound using this expression: `where [(); { N + 1 }]:` | ||
|
||
error: aborting due to 2 previous errors | ||
|