-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add tests for some const generics issues #88602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,33 @@ | ||
#![feature(generic_const_exprs, array_map)] | ||
#![allow(incomplete_features)] | ||
|
||
pub struct ConstCheck<const CHECK: bool>; | ||
|
||
pub trait True {} | ||
impl True for ConstCheck<true> {} | ||
|
||
pub trait OrdesDec { | ||
type Newlen; | ||
type Output; | ||
|
||
fn pop(self) -> (Self::Newlen, Self::Output); | ||
} | ||
|
||
impl<T, const N: usize> OrdesDec for [T; N] | ||
where | ||
ConstCheck<{N > 1}>: True, | ||
[T; N - 1]: Sized, | ||
{ | ||
type Newlen = [T; N - 1]; | ||
type Output = T; | ||
|
||
fn pop(self) -> (Self::Newlen, Self::Output) { | ||
let mut iter = IntoIter::new(self); | ||
//~^ ERROR: failed to resolve: use of undeclared type `IntoIter` | ||
let end = iter.next_back().unwrap(); | ||
let new = [(); N - 1].map(move |()| iter.next().unwrap()); | ||
(new, end) | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,21 @@ | ||
error[E0433]: failed to resolve: use of undeclared type `IntoIter` | ||
--> $DIR/issue-82956.rs:25:24 | ||
| | ||
LL | let mut iter = IntoIter::new(self); | ||
| ^^^^^^^^ not found in this scope | ||
| | ||
help: consider importing one of these items | ||
| | ||
LL | use std::array::IntoIter; | ||
| | ||
LL | use std::collections::binary_heap::IntoIter; | ||
| | ||
LL | use std::collections::btree_map::IntoIter; | ||
| | ||
LL | use std::collections::btree_set::IntoIter; | ||
| | ||
and 8 other candidates | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0433`. |
This file contains hidden or 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 @@ | ||
#![allow(incomplete_features)] | ||
#![feature(generic_const_exprs)] | ||
|
||
trait Bar<const N: usize> {} | ||
|
||
trait Foo<'a> { | ||
const N: usize; | ||
type Baz: Bar<{ Self::N }>; | ||
//~^ ERROR: unconstrained generic constant | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,10 @@ | ||
error: unconstrained generic constant | ||
--> $DIR/issue-84659.rs:8:15 | ||
| | ||
LL | type Baz: Bar<{ Self::N }>; | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: try adding a `where` bound using this expression: `where [(); { Self::N }]:` | ||
|
||
error: aborting due to previous error | ||
|
This file contains hidden or 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,20 @@ | ||
#![feature(generic_const_exprs)] | ||
#![allow(incomplete_features)] | ||
|
||
pub trait X { | ||
const Y: usize; | ||
} | ||
|
||
fn z<T>(t: T) | ||
where | ||
T: X, | ||
[(); T::Y]: , | ||
{ | ||
} | ||
|
||
fn unit_literals() { | ||
z(" "); | ||
//~^ ERROR: the trait bound `&str: X` is not satisfied | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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[E0277]: the trait bound `&str: X` is not satisfied | ||
--> $DIR/issue-86530.rs:16:7 | ||
| | ||
LL | z(" "); | ||
| ^^^ the trait `X` is not implemented for `&str` | ||
| | ||
note: required by a bound in `z` | ||
--> $DIR/issue-86530.rs:10:8 | ||
| | ||
LL | fn z<T>(t: T) | ||
| - required by a bound in this | ||
LL | where | ||
LL | T: X, | ||
| ^ required by this bound in `z` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains hidden or 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,19 @@ | ||
// run-pass | ||
#![feature(adt_const_params, generic_const_exprs)] | ||
#![allow(incomplete_features)] | ||
|
||
pub trait Foo { | ||
const ASSOC_C: usize; | ||
fn foo() where [(); Self::ASSOC_C]:; | ||
} | ||
|
||
struct Bar<const N: &'static ()>; | ||
impl<const N: &'static ()> Foo for Bar<N> { | ||
const ASSOC_C: usize = 3; | ||
|
||
fn foo() where [u8; Self::ASSOC_C]: { | ||
let _: [u8; Self::ASSOC_C] = loop {}; | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,20 @@ | ||
// run-pass | ||
#![feature(adt_const_params, generic_const_exprs)] | ||
#![allow(incomplete_features, unused_variables)] | ||
|
||
struct F<const S: &'static str>; | ||
impl<const S: &'static str> X for F<{ S }> { | ||
const W: usize = 3; | ||
|
||
fn d(r: &[u8; Self::W]) -> F<{ S }> { | ||
let x: [u8; Self::W] = [0; Self::W]; | ||
F | ||
} | ||
} | ||
|
||
pub trait X { | ||
const W: usize; | ||
fn d(r: &[u8; Self::W]) -> Self; | ||
} | ||
|
||
fn main() {} |
This file contains hidden or 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,32 @@ | ||
trait Trait<const N: usize> { | ||
const Assoc: usize; | ||
} | ||
|
||
impl<const N: usize> Trait<N> for () { | ||
const Assoc: usize = 1; | ||
} | ||
|
||
|
||
pub const fn foo<const N: usize>() where (): Trait<N> { | ||
let bar = [(); <()>::Assoc]; | ||
//~^ error: constant expression depends on a generic parameter | ||
} | ||
|
||
trait Trait2<const N: usize> { | ||
const Assoc2: usize; | ||
} | ||
|
||
impl<const N: usize> Trait2<N> for () { | ||
const Assoc2: usize = N - 1; | ||
} | ||
|
||
|
||
pub const fn foo2<const N: usize>() where (): Trait2<N> { | ||
let bar2 = [(); <()>::Assoc2]; | ||
//~^ error: constant expression depends on a generic parameter | ||
} | ||
|
||
fn main() { | ||
foo::<0>(); | ||
foo2::<0>(); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/const-generics/sneaky-array-repeat-expr.stderr
This file contains hidden or 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: constant expression depends on a generic parameter | ||
--> $DIR/sneaky-array-repeat-expr.rs:11:20 | ||
| | ||
LL | let bar = [(); <()>::Assoc]; | ||
| ^^^^^^^^^^^ | ||
| | ||
= note: this may fail depending on what value the parameter takes | ||
|
||
error: constant expression depends on a generic parameter | ||
--> $DIR/sneaky-array-repeat-expr.rs:25:21 | ||
| | ||
LL | let bar2 = [(); <()>::Assoc2]; | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: this may fail depending on what value the parameter takes | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does importing the trait break the ICE? If not wouldn't be including it easier for this testcase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm honestly not sure if it would cause this to not repro the ICE or not. I don't really have a way of verifying...