forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better diagnostic for missing where clause
Previously, it's not clear what exactly should be added in the suggested where clause, so this adds an example to demonstrate.
- Loading branch information
1 parent
04caa63
commit 6525671
Showing
7 changed files
with
93 additions
and
18 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#![crate_type = "lib"] | ||
#![feature(const_generics, const_evaluatable_checked)] | ||
#![allow(incomplete_features)] | ||
|
||
const fn complex_maths<T>(n : usize) -> usize { | ||
2 * n + 1 | ||
} | ||
|
||
struct Example<T, const N: usize> { | ||
a: [f32; N], | ||
b: [f32; complex_maths::<T>(N)], | ||
//~^ ERROR unconstrained | ||
c: T, | ||
} |
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 @@ | ||
error: unconstrained generic constant | ||
--> $DIR/needs_where_clause.rs:11:6 | ||
| | ||
LL | b: [f32; complex_maths::<T>(N)], | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: try adding a `where` bound using this expression: where [u8; complex_maths::<T>(N)]: Sized | ||
--> $DIR/needs_where_clause.rs:11:12 | ||
| | ||
LL | b: [f32; complex_maths::<T>(N)], | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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,29 @@ | ||
#![feature(const_generics, const_evaluatable_checked)] | ||
#![allow(incomplete_features, unused)] | ||
|
||
const fn complex_maths(n : usize) -> usize { | ||
2 * n + 1 | ||
} | ||
|
||
pub struct Example<const N: usize> { | ||
a: [f32; N], | ||
b: [f32; complex_maths(N)], | ||
//~^ ERROR unconstrained generic | ||
} | ||
|
||
impl<const N: usize> Example<N> { | ||
pub fn new() -> Self { | ||
Self { | ||
a: [0.; N], | ||
b: [0.; complex_maths(N)], | ||
} | ||
} | ||
} | ||
|
||
impl Example<2> { | ||
pub fn sum(&self) -> f32 { | ||
self.a.iter().sum::<f32>() + self.b.iter().sum::<f32>() | ||
} | ||
} | ||
|
||
fn main() {} |
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 @@ | ||
error: unconstrained generic constant | ||
--> $DIR/no_where_clause.rs:10:6 | ||
| | ||
LL | b: [f32; complex_maths(N)], | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: try adding a `where` bound using this expression: where [u8; complex_maths(N)]: Sized | ||
--> $DIR/no_where_clause.rs:10:12 | ||
| | ||
LL | b: [f32; complex_maths(N)], | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|