Skip to content

Do not hard error on broken constants in type aliases #82700

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/rustc_mir/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,19 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
let emit_as_lint = if let Some(def) = def.as_local() {
// (Associated) consts only emit a lint, since they might be unused.
matches!(tcx.def_kind(def.did.to_def_id()), DefKind::Const | DefKind::AssocConst)
|| {
let mut did = def.did.to_def_id();
loop {
use rustc_middle::ty::DefIdTree;
match tcx.parent(did) {
Some(parent) => match tcx.def_kind(did) {
DefKind::TyAlias => break true,
_ => did = parent,
},
None => break false,
}
}
}
} else {
// use of broken constant from other crate: always an error
false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
error[E0080]: evaluation of constant value failed
--> $DIR/simple_fail.rs:9:48
--> $DIR/simple_fail.rs:12:10
|
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
LL | [u8; N - 1]: Sized,
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow

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

error: aborting due to 2 previous errors

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

error: generic parameters may not be used in const operations
--> $DIR/simple_fail.rs:9:48
--> $DIR/simple_fail.rs:12:10
|
LL | fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
| ^ cannot perform const operation using `N`
LL | [u8; N - 1]: Sized,
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments, i.e. `N`
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#![cfg_attr(full, feature(const_evaluatable_checked))]
#![allow(incomplete_features)]

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

fn test<const N: usize>() -> Arr<N> where [u8; N - 1]: Sized {
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR evaluation of constant
fn test<const N: usize>() -> Arr<N>
where
[u8; N - 1]: Sized,
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR evaluation of constant
{
todo!()
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/type-alias/erroneous_const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Foo = [u8; 0 - 1];
//~^ ERROR any use of this value will cause an error
//~| WARN will become a hard error

fn foo() -> Foo {
todo!()
}
struct Q<const N: usize>;
type Bar = Q<{ 0 - 1 }>;
//~^ ERROR any use of this value will cause an error
//~| WARN will become a hard error

impl Default for Bar {
fn default() -> Self {
Q
}
}

fn main() {}
23 changes: 23 additions & 0 deletions src/test/ui/type-alias/erroneous_const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: any use of this value will cause an error
--> $DIR/erroneous_const.rs:1:17
|
LL | type Foo = [u8; 0 - 1];
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: any use of this value will cause an error
--> $DIR/erroneous_const.rs:9:16
|
LL | type Bar = Q<{ 0 - 1 }>;
| --^^^^^--
| |
| attempt to compute `0_usize - 1_usize`, which would overflow
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: aborting due to 2 previous errors