Skip to content

Commit 63aee75

Browse files
committed
Do not hard error on broken constants in type aliases
1 parent ef0d592 commit 63aee75

File tree

6 files changed

+74
-11
lines changed

6 files changed

+74
-11
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+13
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
312312
let emit_as_lint = if let Some(def) = def.as_local() {
313313
// (Associated) consts only emit a lint, since they might be unused.
314314
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
315+
|| {
316+
let mut did = def.did.to_def_id();
317+
loop {
318+
use rustc_middle::ty::DefIdTree;
319+
match tcx.parent(did) {
320+
Some(parent) => match tcx.def_kind(did) {
321+
DefKind::TyAlias => break true,
322+
_ => did = parent,
323+
},
324+
None => break false,
325+
}
326+
}
327+
}
315328
} else {
316329
// use of broken constant from other crate: always an error
317330
false

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/simple_fail.rs:9:48
2+
--> $DIR/simple_fail.rs:12:10
33
|
4-
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
5-
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
4+
LL | [u8; N - 1]: Sized,
5+
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
66

7-
error[E0080]: evaluation of constant value failed
7+
error: any use of this value will cause an error
88
--> $DIR/simple_fail.rs:6:33
99
|
1010
LL | type Arr<const N: usize> = [u8; N - 1];
1111
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
12+
|
13+
= note: `#[deny(const_err)]` on by default
14+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
15+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | type Arr<const N: usize> = [u8; N - 1];
88
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
99

1010
error: generic parameters may not be used in const operations
11-
--> $DIR/simple_fail.rs:9:48
11+
--> $DIR/simple_fail.rs:12:10
1212
|
13-
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
14-
| ^ cannot perform const operation using `N`
13+
LL | [u8; N - 1]: Sized,
14+
| ^ cannot perform const operation using `N`
1515
|
1616
= help: const parameters may only be used as standalone arguments, i.e. `N`
1717
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions

src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
#![cfg_attr(full, feature(const_evaluatable_checked))]
44
#![allow(incomplete_features)]
55

6-
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
6+
type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR any use of this value will cause an error
77
//[min]~^ ERROR generic parameters may not be used in const operations
8+
//[full]~^^ WARN this was previously accepted by the compiler but is being phased out
89

9-
fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
10-
//[min]~^ ERROR generic parameters may not be used in const operations
11-
//[full]~^^ ERROR evaluation of constant
10+
fn test<const N: usize>() -> Arr<N>
11+
where
12+
[u8; N - 1]: Sized,
13+
//[min]~^ ERROR generic parameters may not be used in const operations
14+
//[full]~^^ ERROR evaluation of constant
15+
{
1216
todo!()
1317
}
1418

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type Foo = [u8; 0 - 1];
2+
//~^ ERROR any use of this value will cause an error
3+
//~| WARN will become a hard error
4+
5+
fn foo() -> Foo {
6+
todo!()
7+
}
8+
struct Q<const N: usize>;
9+
type Bar = Q<{ 0 - 1 }>;
10+
//~^ ERROR any use of this value will cause an error
11+
//~| WARN will become a hard error
12+
13+
impl Default for Bar {
14+
fn default() -> Self {
15+
Q
16+
}
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/erroneous_const.rs:1:17
3+
|
4+
LL | type Foo = [u8; 0 - 1];
5+
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
6+
|
7+
= note: `#[deny(const_err)]` on by default
8+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
10+
11+
error: any use of this value will cause an error
12+
--> $DIR/erroneous_const.rs:9:16
13+
|
14+
LL | type Bar = Q<{ 0 - 1 }>;
15+
| --^^^^^--
16+
| |
17+
| attempt to compute `0_usize - 1_usize`, which would overflow
18+
|
19+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
20+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)