Skip to content

Commit edae6ed

Browse files
committed
Add tests for lint on type dependent on consts
1 parent c1d65ea commit edae6ed

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
108108
}
109109

110110
let future_compat_lint = || {
111+
if tcx.features().generic_const_exprs {
112+
return;
113+
}
111114
if let Some(local_def_id) = uv.def.did.as_local() {
112115
infcx.tcx.struct_span_lint_hir(
113116
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: generic parameters may not be used in const operations
2+
--> $DIR/dependence_lint.rs:13:32
3+
|
4+
LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
5+
| ^ cannot perform const operation using `T`
6+
|
7+
= note: type parameters may not be used in const expressions
8+
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
9+
10+
error: generic parameters may not be used in const operations
11+
--> $DIR/dependence_lint.rs:20:37
12+
|
13+
LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
14+
| ^ cannot perform const operation using `T`
15+
|
16+
= note: type parameters may not be used in const expressions
17+
= help: use `#![feature(generic_const_exprs)]` to allow generic const expressions
18+
19+
warning: cannot use constants which depend on generic parameters in types
20+
--> $DIR/dependence_lint.rs:9:9
21+
|
22+
LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
23+
| ^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: `#[warn(const_evaluatable_unchecked)]` on by default
26+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
27+
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
28+
29+
warning: cannot use constants which depend on generic parameters in types
30+
--> $DIR/dependence_lint.rs:16:9
31+
|
32+
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
36+
= note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>
37+
38+
error: aborting due to 2 previous errors; 2 warnings emitted
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: overly complex generic constant
2+
--> $DIR/dependence_lint.rs:16:9
3+
|
4+
LL | [0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
6+
|
7+
= help: consider moving this anonymous constant into a `const` function
8+
9+
error: overly complex generic constant
10+
--> $DIR/dependence_lint.rs:20:17
11+
|
12+
LL | let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ control flow is not supported in generic constants
14+
|
15+
= help: consider moving this anonymous constant into a `const` function
16+
17+
error: unconstrained generic constant
18+
--> $DIR/dependence_lint.rs:13:12
19+
|
20+
LL | let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
22+
|
23+
= help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:`
24+
25+
error: unconstrained generic constant
26+
--> $DIR/dependence_lint.rs:9:9
27+
|
28+
LL | [0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
29+
| ^^^^^^^^^^^^^^^^^^^
30+
|
31+
= help: try adding a `where` bound using this expression: `where [(); size_of::<*mut T>()]:`
32+
33+
error: aborting due to 4 previous errors
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// revisions: full gce
2+
3+
#![cfg_attr(gce, feature(generic_const_exprs))]
4+
#![allow(incomplete_features)]
5+
6+
use std::mem::size_of;
7+
8+
fn foo<T>() {
9+
[0; size_of::<*mut T>()]; // lint on stable, error with `generic_const_exprs`
10+
//[gce]~^ ERROR unconstrained
11+
//[full]~^^ WARNING cannot use constants
12+
//[full]~| WARNING this was previously accepted
13+
let _: [u8; size_of::<*mut T>()]; // error on stable, error with gce
14+
//[full]~^ ERROR generic parameters may not be used
15+
//[gce]~^^ ERROR unconstrained generic
16+
[0; if false { size_of::<T>() } else { 3 }]; // lint on stable, error with gce
17+
//[gce]~^ ERROR overly complex
18+
//[full]~^^ WARNING cannot use constants
19+
//[full]~| WARNING this was previously accepted
20+
let _: [u8; if true { size_of::<T>() } else { 3 }]; // error on stable, error with gce
21+
//[full]~^ ERROR generic parameters may not be used
22+
//[gce]~^^ ERROR overly complex
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
#![feature(generic_const_exprs)]
3+
#![allow(incomplete_features)]
4+
5+
fn two_args<const N: usize, const M: usize>() -> [u8; M + 2] {
6+
[0; M + 2]
7+
}
8+
9+
fn yay<const N: usize>() -> [u8; 4] {
10+
two_args::<N, 2>() // no lint
11+
}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)