From 63aee758d60eef9a8123c385e1b15a5ddf584240 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 2 Mar 2021 13:06:40 +0000 Subject: [PATCH] Do not hard error on broken constants in type aliases --- .../rustc_mir/src/const_eval/eval_queries.rs | 13 +++++++++++ .../simple_fail.full.stderr | 12 ++++++---- .../simple_fail.min.stderr | 6 ++--- .../const_evaluatable_checked/simple_fail.rs | 12 ++++++---- src/test/ui/type-alias/erroneous_const.rs | 19 +++++++++++++++ src/test/ui/type-alias/erroneous_const.stderr | 23 +++++++++++++++++++ 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/type-alias/erroneous_const.rs create mode 100644 src/test/ui/type-alias/erroneous_const.stderr diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index fa234ff5feb64..27a2d644bbcab 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -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 diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr index acf0a52ce5be1..4fa6497202312 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.full.stderr @@ -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() -> Arr 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 = [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 error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr index fe5463f8acc4a..1a8750ec9cfa8 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.min.stderr @@ -8,10 +8,10 @@ LL | type Arr = [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() -> Arr 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 diff --git a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs index c9535d04244d8..4fb96ff123860 100644 --- a/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs +++ b/src/test/ui/const-generics/const_evaluatable_checked/simple_fail.rs @@ -3,12 +3,16 @@ #![cfg_attr(full, feature(const_evaluatable_checked))] #![allow(incomplete_features)] -type Arr = [u8; N - 1]; //[full]~ ERROR evaluation of constant +type Arr = [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() -> Arr where [u8; N - 1]: Sized { -//[min]~^ ERROR generic parameters may not be used in const operations -//[full]~^^ ERROR evaluation of constant +fn test() -> Arr +where + [u8; N - 1]: Sized, + //[min]~^ ERROR generic parameters may not be used in const operations + //[full]~^^ ERROR evaluation of constant +{ todo!() } diff --git a/src/test/ui/type-alias/erroneous_const.rs b/src/test/ui/type-alias/erroneous_const.rs new file mode 100644 index 0000000000000..3803b34d868c6 --- /dev/null +++ b/src/test/ui/type-alias/erroneous_const.rs @@ -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; +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() {} diff --git a/src/test/ui/type-alias/erroneous_const.stderr b/src/test/ui/type-alias/erroneous_const.stderr new file mode 100644 index 0000000000000..fe1aacf026dd7 --- /dev/null +++ b/src/test/ui/type-alias/erroneous_const.stderr @@ -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 + +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 + +error: aborting due to 2 previous errors +