From 210d7fc00ef575586f4756b0ecbafc5b145954c1 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Sun, 8 Apr 2018 22:26:28 +0200 Subject: [PATCH 1/4] Only warn on erroneous promoted constants --- src/librustc_mir/interpret/const_eval.rs | 4 +- src/librustc_mir/interpret/mod.rs | 2 +- src/librustc_mir/monomorphize/collector.rs | 6 +-- src/librustc_mir/transform/const_prop.rs | 4 +- src/librustc_trans/mir/operand.rs | 9 +++- .../const-eval/conditional_array_execution.rs | 5 +- .../conditional_array_execution.stderr | 11 ++-- src/test/ui/const-eval/issue-43197.rs | 6 ++- src/test/ui/const-eval/issue-43197.stderr | 15 +++--- .../const-eval}/issue-44578.rs | 5 +- src/test/ui/const-eval/issue-44578.stderr | 14 ++++++ src/test/ui/const-eval/promoted_errors.rs | 28 +++++++++++ src/test/ui/const-eval/promoted_errors.stderr | 50 +++++++++++++++++++ 13 files changed, 129 insertions(+), 30 deletions(-) rename src/test/{compile-fail => ui/const-eval}/issue-44578.rs (87%) create mode 100644 src/test/ui/const-eval/issue-44578.stderr create mode 100644 src/test/ui/const-eval/promoted_errors.rs create mode 100644 src/test/ui/const-eval/promoted_errors.stderr diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index 954a3dbe5b9ab..6fa68eb6ff1f0 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -56,7 +56,7 @@ pub fn mk_eval_cx<'a, 'tcx>( Ok(ecx) } -pub fn eval_body_with_mir<'a, 'mir, 'tcx>( +pub fn eval_promoted<'a, 'mir, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, cid: GlobalId<'tcx>, mir: &'mir mir::Mir<'tcx>, @@ -66,7 +66,7 @@ pub fn eval_body_with_mir<'a, 'mir, 'tcx>( match res { Ok(val) => Some(val), Err(mut err) => { - ecx.report(&mut err, true, None); + ecx.report(&mut err, false, None); None } } diff --git a/src/librustc_mir/interpret/mod.rs b/src/librustc_mir/interpret/mod.rs index 147db3bdc0e7a..e0e05d6ae60ae 100644 --- a/src/librustc_mir/interpret/mod.rs +++ b/src/librustc_mir/interpret/mod.rs @@ -19,7 +19,7 @@ pub use self::place::{Place, PlaceExtra}; pub use self::memory::{Memory, MemoryKind, HasMemory}; pub use self::const_eval::{ - eval_body_with_mir, + eval_promoted, mk_borrowck_eval_cx, eval_body, CompileTimeEvaluator, diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index 7905fe998efc2..a3ef1db13483f 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -1146,7 +1146,7 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, param_substs: instance.substs, }.visit_mir(&mir); let param_env = ty::ParamEnv::reveal_all(); - for (i, promoted) in mir.promoted.iter().enumerate() { + for i in 0..mir.promoted.len() { use rustc_data_structures::indexed_vec::Idx; let cid = GlobalId { instance, @@ -1154,9 +1154,7 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, }; match tcx.const_eval(param_env.and(cid)) { Ok(val) => collect_const(tcx, val, instance.substs, output), - Err(err) => { - err.report(tcx, promoted.span, "promoted"); - } + Err(_) => {}, } } } diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index f133d6e9c6dee..a3d96f0c0739d 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -20,7 +20,7 @@ use rustc::mir::visit::{Visitor, PlaceContext}; use rustc::middle::const_val::ConstVal; use rustc::ty::{TyCtxt, self, Instance}; use rustc::mir::interpret::{Value, PrimVal, GlobalId}; -use interpret::{eval_body_with_mir, mk_borrowck_eval_cx, ValTy}; +use interpret::{eval_promoted, mk_borrowck_eval_cx, ValTy}; use transform::{MirPass, MirSource}; use syntax::codemap::Span; use rustc::ty::subst::Substs; @@ -161,7 +161,7 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> { }; // cannot use `const_eval` here, because that would require having the MIR // for the current function available, but we're producing said MIR right now - let (value, _, ty) = eval_body_with_mir(self.tcx, cid, self.mir, self.param_env)?; + let (value, _, ty) = eval_promoted(self.tcx, cid, self.mir, self.param_env)?; let val = (value, ty, c.span); trace!("evaluated {:?} to {:?}", c, val); Some(val) diff --git a/src/librustc_trans/mir/operand.rs b/src/librustc_trans/mir/operand.rs index 75df349de41ee..656ab95a28cf3 100644 --- a/src/librustc_trans/mir/operand.rs +++ b/src/librustc_trans/mir/operand.rs @@ -399,7 +399,14 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> { self.mir_constant_to_miri_value(bx, constant) .and_then(|c| OperandRef::from_const(bx, c, ty)) .unwrap_or_else(|err| { - err.report(bx.tcx(), constant.span, "const operand"); + match constant.literal { + mir::Literal::Promoted { .. } => { + // don't report errors inside promoteds, just warnings. + }, + mir::Literal::Value { .. } => { + err.report(bx.tcx(), constant.span, "const operand") + }, + } // We've errored, so we don't have to produce working code. let layout = bx.cx.layout_of(ty); PlaceRef::new_sized( diff --git a/src/test/ui/const-eval/conditional_array_execution.rs b/src/test/ui/const-eval/conditional_array_execution.rs index 324bcf60e8ff6..e059c354d65a7 100644 --- a/src/test/ui/const-eval/conditional_array_execution.rs +++ b/src/test/ui/const-eval/conditional_array_execution.rs @@ -8,11 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// compile-pass + const X: u32 = 5; const Y: u32 = 6; const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; //~^ WARN attempt to subtract with overflow fn main() { - println!("{}", FOO); //~ E0080 + println!("{}", FOO); + //~^ WARN constant evaluation error } diff --git a/src/test/ui/const-eval/conditional_array_execution.stderr b/src/test/ui/const-eval/conditional_array_execution.stderr index 9270dafbe651a..fd29990fec295 100644 --- a/src/test/ui/const-eval/conditional_array_execution.stderr +++ b/src/test/ui/const-eval/conditional_array_execution.stderr @@ -1,17 +1,14 @@ warning: attempt to subtract with overflow - --> $DIR/conditional_array_execution.rs:13:19 + --> $DIR/conditional_array_execution.rs:15:19 | LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; | ^^^^^ | = note: #[warn(const_err)] on by default -error[E0080]: constant evaluation error - --> $DIR/conditional_array_execution.rs:17:20 +warning: constant evaluation error + --> $DIR/conditional_array_execution.rs:19:20 | -LL | println!("{}", FOO); //~ E0080 +LL | println!("{}", FOO); | ^^^ referenced constant has errors -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/const-eval/issue-43197.rs b/src/test/ui/const-eval/issue-43197.rs index d5c4796d0b497..b27791ef5337b 100644 --- a/src/test/ui/const-eval/issue-43197.rs +++ b/src/test/ui/const-eval/issue-43197.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// compile-pass + #![feature(const_fn)] const fn foo(x: u32) -> u32 { @@ -20,6 +22,6 @@ fn main() { const Y: u32 = foo(0-1); //~^ WARN attempt to subtract with overflow println!("{} {}", X, Y); - //~^ ERROR constant evaluation error - //~| ERROR constant evaluation error + //~^ WARN constant evaluation error + //~| WARN constant evaluation error } diff --git a/src/test/ui/const-eval/issue-43197.stderr b/src/test/ui/const-eval/issue-43197.stderr index 2d51e6603b59b..5da47a85eb803 100644 --- a/src/test/ui/const-eval/issue-43197.stderr +++ b/src/test/ui/const-eval/issue-43197.stderr @@ -1,29 +1,26 @@ warning: attempt to subtract with overflow - --> $DIR/issue-43197.rs:18:20 + --> $DIR/issue-43197.rs:20:20 | LL | const X: u32 = 0-1; | ^^^ | = note: #[warn(const_err)] on by default -error[E0080]: constant evaluation error - --> $DIR/issue-43197.rs:22:23 +warning: constant evaluation error + --> $DIR/issue-43197.rs:24:23 | LL | println!("{} {}", X, Y); | ^ referenced constant has errors warning: attempt to subtract with overflow - --> $DIR/issue-43197.rs:20:24 + --> $DIR/issue-43197.rs:22:24 | LL | const Y: u32 = foo(0-1); | ^^^ -error[E0080]: constant evaluation error - --> $DIR/issue-43197.rs:22:26 +warning: constant evaluation error + --> $DIR/issue-43197.rs:24:26 | LL | println!("{} {}", X, Y); | ^ referenced constant has errors -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/compile-fail/issue-44578.rs b/src/test/ui/const-eval/issue-44578.rs similarity index 87% rename from src/test/compile-fail/issue-44578.rs rename to src/test/ui/const-eval/issue-44578.rs index 18cfb35113dc6..765113cfbb9e2 100644 --- a/src/test/compile-fail/issue-44578.rs +++ b/src/test/ui/const-eval/issue-44578.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// compile-pass + trait Foo { const AMT: usize; } @@ -30,5 +32,6 @@ impl Foo for u16 { } fn main() { - println!("{}", as Foo>::AMT); //~ E0080 + println!("{}", as Foo>::AMT); //~ WARN const_err + //~^ WARN const_err } diff --git a/src/test/ui/const-eval/issue-44578.stderr b/src/test/ui/const-eval/issue-44578.stderr new file mode 100644 index 0000000000000..01c6fa3623f8d --- /dev/null +++ b/src/test/ui/const-eval/issue-44578.stderr @@ -0,0 +1,14 @@ +warning: constant evaluation error + --> $DIR/issue-44578.rs:35:20 + | +LL | println!("{}", as Foo>::AMT); //~ WARN const_err + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors + | + = note: #[warn(const_err)] on by default + +warning: constant evaluation error + --> $DIR/issue-44578.rs:35:20 + | +LL | println!("{}", as Foo>::AMT); //~ WARN const_err + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors + diff --git a/src/test/ui/const-eval/promoted_errors.rs b/src/test/ui/const-eval/promoted_errors.rs new file mode 100644 index 0000000000000..c6d62e2057651 --- /dev/null +++ b/src/test/ui/const-eval/promoted_errors.rs @@ -0,0 +1,28 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-pass + +fn main() { + println!("{}", 0u32 - 1); + //~^ WARN const_err + //~| WARN const_err + let _x = 0u32 - 1; + //~^ WARN const_err + println!("{}", 1/(1-1)); + //~^ WARN const_err + //~| WARN const_err + let _x = 1/(1-1); + //~^ WARN const_err + //~| WARN const_err + println!("{}", 1/(false as u32)); + //~^ WARN const_err + let _x = 1/(false as u32); +} diff --git a/src/test/ui/const-eval/promoted_errors.stderr b/src/test/ui/const-eval/promoted_errors.stderr new file mode 100644 index 0000000000000..a5db8cc908331 --- /dev/null +++ b/src/test/ui/const-eval/promoted_errors.stderr @@ -0,0 +1,50 @@ +warning: constant evaluation error + --> $DIR/promoted_errors.rs:14:20 + | +LL | println!("{}", 0u32 - 1); + | ^^^^^^^^ attempted to do overflowing math + | + = note: #[warn(const_err)] on by default + +warning: constant evaluation error + --> $DIR/promoted_errors.rs:14:20 + | +LL | println!("{}", 0u32 - 1); + | ^^^^^^^^ attempted to do overflowing math + +warning: constant evaluation error + --> $DIR/promoted_errors.rs:17:14 + | +LL | let _x = 0u32 - 1; + | ^^^^^^^^ attempted to do overflowing math + +warning: attempt to divide by zero + --> $DIR/promoted_errors.rs:19:20 + | +LL | println!("{}", 1/(1-1)); + | ^^^^^^^ + +warning: constant evaluation error + --> $DIR/promoted_errors.rs:19:20 + | +LL | println!("{}", 1/(1-1)); + | ^^^^^^^ attempted to do overflowing math + +warning: attempt to divide by zero + --> $DIR/promoted_errors.rs:22:14 + | +LL | let _x = 1/(1-1); + | ^^^^^^^ + +warning: constant evaluation error + --> $DIR/promoted_errors.rs:22:14 + | +LL | let _x = 1/(1-1); + | ^^^^^^^ attempted to do overflowing math + +warning: constant evaluation error + --> $DIR/promoted_errors.rs:25:20 + | +LL | println!("{}", 1/(false as u32)); + | ^^^^^^^^^^^^^^^^ attempted to do overflowing math + From 6308cc8386fa1bb11cd87d4290da55da3b6cc186 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 23 Apr 2018 11:12:33 +0200 Subject: [PATCH 2/4] always optimize test The error messages differ between optimized and nonoptimized mode --- src/test/ui/const-eval/promoted_errors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/const-eval/promoted_errors.rs b/src/test/ui/const-eval/promoted_errors.rs index c6d62e2057651..dc30c7f9cce04 100644 --- a/src/test/ui/const-eval/promoted_errors.rs +++ b/src/test/ui/const-eval/promoted_errors.rs @@ -9,7 +9,7 @@ // except according to those terms. // compile-pass - +// compile-flags: -O fn main() { println!("{}", 0u32 - 1); //~^ WARN const_err From 731c7041563cbec5717ce9b7399ae1cdb88ed035 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 20 Apr 2018 14:18:29 +0200 Subject: [PATCH 3/4] Warn on all erroneous constants --- src/librustc_lint/builtin.rs | 68 +++++++++++++++++++ src/librustc_lint/lib.rs | 1 + src/test/compile-fail/array_const_index-0.rs | 1 + src/test/compile-fail/array_const_index-1.rs | 1 + src/test/compile-fail/const-err-early.rs | 18 ++--- src/test/compile-fail/const-err-multi.rs | 7 +- src/test/compile-fail/const-eval-overflow2.rs | 32 ++++----- .../compile-fail/const-eval-overflow2b.rs | 32 ++++----- .../compile-fail/const-eval-overflow2c.rs | 32 ++++----- src/test/compile-fail/const-slice-oob.rs | 1 + .../const-eval/conditional_array_execution.rs | 1 + .../conditional_array_execution.stderr | 8 ++- src/test/ui/const-eval/issue-43197.rs | 2 + src/test/ui/const-eval/issue-43197.stderr | 24 +++++-- src/test/ui/const-eval/pub_const_err.rs | 9 ++- src/test/ui/const-eval/pub_const_err.stderr | 24 +++++-- src/test/ui/const-eval/pub_const_err_bin.rs | 21 ++++++ .../ui/const-eval/pub_const_err_bin.stderr | 26 +++++++ 18 files changed, 233 insertions(+), 75 deletions(-) create mode 100644 src/test/ui/const-eval/pub_const_err_bin.rs create mode 100644 src/test/ui/const-eval/pub_const_err_bin.stderr diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index c0728cb2b6669..b19cd25c1e62d 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1430,3 +1430,71 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds { } } } + +/// Lint constants that are erroneous. +/// Without this lint, we might not get any diagnostic if the constant is +/// unused within this crate, even though downstream crates can't use it +/// without producing an error. +pub struct UnusedBrokenConst; + +impl LintPass for UnusedBrokenConst { + fn get_lints(&self) -> LintArray { + lint_array!() + } +} + +fn check_const(cx: &LateContext, body_id: hir::BodyId, what: &str) { + let def_id = cx.tcx.hir.body_owner_def_id(body_id); + let param_env = cx.tcx.param_env(def_id); + let cid = ::rustc::mir::interpret::GlobalId { + instance: ty::Instance::mono(cx.tcx, def_id), + promoted: None + }; + if let Err(err) = cx.tcx.const_eval(param_env.and(cid)) { + let span = cx.tcx.def_span(def_id); + let mut diag = cx.struct_span_lint( + CONST_ERR, + span, + &format!("this {} cannot be used", what), + ); + use rustc::middle::const_val::ConstEvalErrDescription; + match err.description() { + ConstEvalErrDescription::Simple(message) => { + diag.span_label(span, message); + } + ConstEvalErrDescription::Backtrace(miri, frames) => { + diag.span_label(span, format!("{}", miri)); + for frame in frames { + diag.span_label(frame.span, format!("inside call to `{}`", frame.location)); + } + } + } + diag.emit() + } +} + +struct UnusedBrokenConstVisitor<'a, 'tcx: 'a>(&'a LateContext<'a, 'tcx>); + +impl<'a, 'tcx, 'v> hir::intravisit::Visitor<'v> for UnusedBrokenConstVisitor<'a, 'tcx> { + fn visit_nested_body(&mut self, id: hir::BodyId) { + check_const(self.0, id, "array length"); + } + fn nested_visit_map<'this>(&'this mut self) -> hir::intravisit::NestedVisitorMap<'this, 'v> { + hir::intravisit::NestedVisitorMap::None + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedBrokenConst { + fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { + match it.node { + hir::ItemConst(_, body_id) => { + check_const(cx, body_id, "constant"); + }, + hir::ItemTy(ref ty, _) => hir::intravisit::walk_ty( + &mut UnusedBrokenConstVisitor(cx), + ty + ), + _ => {}, + } + } +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index d024adad9d030..a9012ff48268b 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -139,6 +139,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { UnionsWithDropFields, UnreachablePub, TypeAliasBounds, + UnusedBrokenConst, ); add_builtin_with_new!(sess, diff --git a/src/test/compile-fail/array_const_index-0.rs b/src/test/compile-fail/array_const_index-0.rs index 501c66e75cded..9b9deb4ca8d9d 100644 --- a/src/test/compile-fail/array_const_index-0.rs +++ b/src/test/compile-fail/array_const_index-0.rs @@ -12,6 +12,7 @@ const A: &'static [i32] = &[]; const B: i32 = (&A)[1]; //~^ ERROR constant evaluation error //~| index out of bounds: the len is 0 but the index is 1 +//~| WARN this constant cannot be used fn main() { let _ = B; diff --git a/src/test/compile-fail/array_const_index-1.rs b/src/test/compile-fail/array_const_index-1.rs index d3b43e83bfe52..46feb20cf11dd 100644 --- a/src/test/compile-fail/array_const_index-1.rs +++ b/src/test/compile-fail/array_const_index-1.rs @@ -12,6 +12,7 @@ const A: [i32; 0] = []; const B: i32 = A[1]; //~^ ERROR constant evaluation error //~| index out of bounds: the len is 0 but the index is 1 +//~| WARN this constant cannot be used fn main() { let _ = B; diff --git a/src/test/compile-fail/const-err-early.rs b/src/test/compile-fail/const-err-early.rs index 3de0f1ff61e1c..6caec159d019c 100644 --- a/src/test/compile-fail/const-err-early.rs +++ b/src/test/compile-fail/const-err-early.rs @@ -10,17 +10,17 @@ #![deny(const_err)] -pub const A: i8 = -std::i8::MIN; //~ ERROR E0080 -//~^ ERROR attempt to negate with overflow +pub const A: i8 = -std::i8::MIN; //~ ERROR const_err +//~^ ERROR this constant cannot be used //~| ERROR constant evaluation error -pub const B: u8 = 200u8 + 200u8; //~ ERROR E0080 -//~^ ERROR attempt to add with overflow -pub const C: u8 = 200u8 * 4; //~ ERROR E0080 -//~^ ERROR attempt to multiply with overflow -pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR E0080 -//~^ ERROR attempt to subtract with overflow +pub const B: u8 = 200u8 + 200u8; //~ ERROR const_err +//~^ ERROR this constant cannot be used +pub const C: u8 = 200u8 * 4; //~ ERROR const_err +//~^ ERROR this constant cannot be used +pub const D: u8 = 42u8 - (42u8 + 1); //~ ERROR const_err +//~^ ERROR this constant cannot be used pub const E: u8 = [5u8][1]; -//~^ ERROR E0080 +//~^ ERROR const_err fn main() { let _a = A; diff --git a/src/test/compile-fail/const-err-multi.rs b/src/test/compile-fail/const-err-multi.rs index d2355f57f1729..6f0281b8bd0ca 100644 --- a/src/test/compile-fail/const-err-multi.rs +++ b/src/test/compile-fail/const-err-multi.rs @@ -14,12 +14,13 @@ pub const A: i8 = -std::i8::MIN; //~^ ERROR E0080 //~| ERROR attempt to negate with overflow //~| ERROR constant evaluation error +//~| ERROR this constant cannot be used pub const B: i8 = A; -//~^ ERROR E0080 +//~^ ERROR const_err pub const C: u8 = A as u8; -//~^ ERROR E0080 +//~^ ERROR const_err pub const D: i8 = 50 - A; -//~^ ERROR E0080 +//~^ ERROR const_err fn main() { let _ = (A, B, C, D); diff --git a/src/test/compile-fail/const-eval-overflow2.rs b/src/test/compile-fail/const-eval-overflow2.rs index a0d8f9672c05e..faa8c3039b78c 100644 --- a/src/test/compile-fail/const-eval-overflow2.rs +++ b/src/test/compile-fail/const-eval-overflow2.rs @@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize}; use std::{u8, u16, u32, u64, usize}; const VALS_I8: (i8,) = + //~^ ERROR this constant cannot be used ( i8::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_I16: (i16,) = + //~^ ERROR this constant cannot be used ( i16::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_I32: (i32,) = + //~^ ERROR this constant cannot be used ( i32::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_I64: (i64,) = + //~^ ERROR this constant cannot be used ( i64::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_U8: (u8,) = + //~^ ERROR this constant cannot be used ( u8::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_U16: (u16,) = ( + //~^ ERROR this constant cannot be used u16::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_U32: (u32,) = ( + //~^ ERROR this constant cannot be used u32::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); const VALS_U64: (u64,) = + //~^ ERROR this constant cannot be used ( u64::MIN - 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to subtract with overflow + //~^ ERROR attempt to subtract with overflow ); fn main() { diff --git a/src/test/compile-fail/const-eval-overflow2b.rs b/src/test/compile-fail/const-eval-overflow2b.rs index 08128f90e532f..d827e680c5bbb 100644 --- a/src/test/compile-fail/const-eval-overflow2b.rs +++ b/src/test/compile-fail/const-eval-overflow2b.rs @@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize}; use std::{u8, u16, u32, u64, usize}; const VALS_I8: (i8,) = + //~^ ERROR this constant cannot be used ( i8::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_I16: (i16,) = + //~^ ERROR this constant cannot be used ( i16::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_I32: (i32,) = + //~^ ERROR this constant cannot be used ( i32::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_I64: (i64,) = + //~^ ERROR this constant cannot be used ( i64::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_U8: (u8,) = + //~^ ERROR this constant cannot be used ( u8::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_U16: (u16,) = ( + //~^ ERROR this constant cannot be used u16::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_U32: (u32,) = ( + //~^ ERROR this constant cannot be used u32::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); const VALS_U64: (u64,) = + //~^ ERROR this constant cannot be used ( u64::MAX + 1, - //~^ ERROR constant evaluation error - //~| ERROR attempt to add with overflow + //~^ ERROR attempt to add with overflow ); fn main() { diff --git a/src/test/compile-fail/const-eval-overflow2c.rs b/src/test/compile-fail/const-eval-overflow2c.rs index 31a1638cade17..2fd46b038ef28 100644 --- a/src/test/compile-fail/const-eval-overflow2c.rs +++ b/src/test/compile-fail/const-eval-overflow2c.rs @@ -22,57 +22,57 @@ use std::{i8, i16, i32, i64, isize}; use std::{u8, u16, u32, u64, usize}; const VALS_I8: (i8,) = + //~^ ERROR this constant cannot be used ( i8::MIN * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_I16: (i16,) = + //~^ ERROR this constant cannot be used ( i16::MIN * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_I32: (i32,) = + //~^ ERROR this constant cannot be used ( i32::MIN * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_I64: (i64,) = + //~^ ERROR this constant cannot be used ( i64::MIN * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_U8: (u8,) = + //~^ ERROR this constant cannot be used ( u8::MAX * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_U16: (u16,) = ( + //~^ ERROR this constant cannot be used u16::MAX * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_U32: (u32,) = ( + //~^ ERROR this constant cannot be used u32::MAX * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); const VALS_U64: (u64,) = + //~^ ERROR this constant cannot be used ( u64::MAX * 2, - //~^ ERROR constant evaluation error - //~| ERROR attempt to multiply with overflow + //~^ ERROR attempt to multiply with overflow ); fn main() { diff --git a/src/test/compile-fail/const-slice-oob.rs b/src/test/compile-fail/const-slice-oob.rs index 179ea9e853f3a..7da5a2f17eaf9 100644 --- a/src/test/compile-fail/const-slice-oob.rs +++ b/src/test/compile-fail/const-slice-oob.rs @@ -14,6 +14,7 @@ const FOO: &'static[u32] = &[1, 2, 3]; const BAR: u32 = FOO[5]; //~^ ERROR constant evaluation error [E0080] //~| index out of bounds: the len is 3 but the index is 5 +//~| WARN this constant cannot be used fn main() { let _ = BAR; diff --git a/src/test/ui/const-eval/conditional_array_execution.rs b/src/test/ui/const-eval/conditional_array_execution.rs index e059c354d65a7..dbddee862e022 100644 --- a/src/test/ui/const-eval/conditional_array_execution.rs +++ b/src/test/ui/const-eval/conditional_array_execution.rs @@ -14,6 +14,7 @@ const X: u32 = 5; const Y: u32 = 6; const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; //~^ WARN attempt to subtract with overflow +//~| WARN this constant cannot be used fn main() { println!("{}", FOO); diff --git a/src/test/ui/const-eval/conditional_array_execution.stderr b/src/test/ui/const-eval/conditional_array_execution.stderr index fd29990fec295..713b1b36c08b1 100644 --- a/src/test/ui/const-eval/conditional_array_execution.stderr +++ b/src/test/ui/const-eval/conditional_array_execution.stderr @@ -6,8 +6,14 @@ LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; | = note: #[warn(const_err)] on by default +warning: this constant cannot be used + --> $DIR/conditional_array_execution.rs:15:1 + | +LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow + warning: constant evaluation error - --> $DIR/conditional_array_execution.rs:19:20 + --> $DIR/conditional_array_execution.rs:20:20 | LL | println!("{}", FOO); | ^^^ referenced constant has errors diff --git a/src/test/ui/const-eval/issue-43197.rs b/src/test/ui/const-eval/issue-43197.rs index b27791ef5337b..097fba4d3c450 100644 --- a/src/test/ui/const-eval/issue-43197.rs +++ b/src/test/ui/const-eval/issue-43197.rs @@ -19,8 +19,10 @@ const fn foo(x: u32) -> u32 { fn main() { const X: u32 = 0-1; //~^ WARN attempt to subtract with overflow + //~| WARN this constant cannot be used const Y: u32 = foo(0-1); //~^ WARN attempt to subtract with overflow + //~| WARN this constant cannot be used println!("{} {}", X, Y); //~^ WARN constant evaluation error //~| WARN constant evaluation error diff --git a/src/test/ui/const-eval/issue-43197.stderr b/src/test/ui/const-eval/issue-43197.stderr index 5da47a85eb803..a22e8016296c4 100644 --- a/src/test/ui/const-eval/issue-43197.stderr +++ b/src/test/ui/const-eval/issue-43197.stderr @@ -6,20 +6,32 @@ LL | const X: u32 = 0-1; | = note: #[warn(const_err)] on by default -warning: constant evaluation error - --> $DIR/issue-43197.rs:24:23 +warning: this constant cannot be used + --> $DIR/issue-43197.rs:20:5 | -LL | println!("{} {}", X, Y); - | ^ referenced constant has errors +LL | const X: u32 = 0-1; + | ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow warning: attempt to subtract with overflow - --> $DIR/issue-43197.rs:22:24 + --> $DIR/issue-43197.rs:23:24 | LL | const Y: u32 = foo(0-1); | ^^^ +warning: this constant cannot be used + --> $DIR/issue-43197.rs:23:5 + | +LL | const Y: u32 = foo(0-1); + | ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow + +warning: constant evaluation error + --> $DIR/issue-43197.rs:26:23 + | +LL | println!("{} {}", X, Y); + | ^ referenced constant has errors + warning: constant evaluation error - --> $DIR/issue-43197.rs:24:26 + --> $DIR/issue-43197.rs:26:26 | LL | println!("{} {}", X, Y); | ^ referenced constant has errors diff --git a/src/test/ui/const-eval/pub_const_err.rs b/src/test/ui/const-eval/pub_const_err.rs index bdb9f5b19a885..c6bf07649af38 100644 --- a/src/test/ui/const-eval/pub_const_err.rs +++ b/src/test/ui/const-eval/pub_const_err.rs @@ -8,9 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![deny(const_err)] +// compile-pass #![crate_type = "lib"] pub const Z: u32 = 0 - 1; -//~^ ERROR attempt to subtract with overflow +//~^ WARN attempt to subtract with overflow +//~| WARN this constant cannot be used + +pub type Foo = [i32; 0 - 1]; +//~^ WARN attempt to subtract with overflow +//~| WARN this array length cannot be used diff --git a/src/test/ui/const-eval/pub_const_err.stderr b/src/test/ui/const-eval/pub_const_err.stderr index b77ec38ca1679..2981ac20cd981 100644 --- a/src/test/ui/const-eval/pub_const_err.stderr +++ b/src/test/ui/const-eval/pub_const_err.stderr @@ -1,14 +1,26 @@ -error: attempt to subtract with overflow +warning: attempt to subtract with overflow --> $DIR/pub_const_err.rs:15:20 | LL | pub const Z: u32 = 0 - 1; | ^^^^^ | -note: lint level defined here - --> $DIR/pub_const_err.rs:11:9 + = note: #[warn(const_err)] on by default + +warning: this constant cannot be used + --> $DIR/pub_const_err.rs:15:1 | -LL | #![deny(const_err)] - | ^^^^^^^^^ +LL | pub const Z: u32 = 0 - 1; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow -error: aborting due to previous error +warning: attempt to subtract with overflow + --> $DIR/pub_const_err.rs:19:22 + | +LL | pub type Foo = [i32; 0 - 1]; + | ^^^^^ + +warning: this array length cannot be used + --> $DIR/pub_const_err.rs:19:22 + | +LL | pub type Foo = [i32; 0 - 1]; + | ^^^^^ attempt to subtract with overflow diff --git a/src/test/ui/const-eval/pub_const_err_bin.rs b/src/test/ui/const-eval/pub_const_err_bin.rs new file mode 100644 index 0000000000000..d87cb7ed77079 --- /dev/null +++ b/src/test/ui/const-eval/pub_const_err_bin.rs @@ -0,0 +1,21 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-pass + +pub const Z: u32 = 0 - 1; +//~^ WARN attempt to subtract with overflow +//~| WARN this constant cannot be used + +pub type Foo = [i32; 0 - 1]; +//~^ WARN attempt to subtract with overflow +//~| WARN this array length cannot be used + +fn main() {} diff --git a/src/test/ui/const-eval/pub_const_err_bin.stderr b/src/test/ui/const-eval/pub_const_err_bin.stderr new file mode 100644 index 0000000000000..3e8966d854bf0 --- /dev/null +++ b/src/test/ui/const-eval/pub_const_err_bin.stderr @@ -0,0 +1,26 @@ +warning: attempt to subtract with overflow + --> $DIR/pub_const_err_bin.rs:13:20 + | +LL | pub const Z: u32 = 0 - 1; + | ^^^^^ + | + = note: #[warn(const_err)] on by default + +warning: this constant cannot be used + --> $DIR/pub_const_err_bin.rs:13:1 + | +LL | pub const Z: u32 = 0 - 1; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow + +warning: attempt to subtract with overflow + --> $DIR/pub_const_err_bin.rs:17:22 + | +LL | pub type Foo = [i32; 0 - 1]; + | ^^^^^ + +warning: this array length cannot be used + --> $DIR/pub_const_err_bin.rs:17:22 + | +LL | pub type Foo = [i32; 0 - 1]; + | ^^^^^ attempt to subtract with overflow + From 656d82534264cc5c4ec7ea2b11e1bb25500cff7d Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 25 Apr 2018 20:24:34 +0200 Subject: [PATCH 4/4] Rename ui test flag compile-pass to must-compile-successfully --- src/test/ui/const-eval/conditional_array_execution.rs | 2 +- src/test/ui/const-eval/issue-43197.rs | 2 +- src/test/ui/const-eval/issue-44578.rs | 2 +- src/test/ui/const-eval/promoted_errors.rs | 2 +- src/test/ui/const-eval/pub_const_err.rs | 2 +- src/test/ui/const-eval/pub_const_err_bin.rs | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/ui/const-eval/conditional_array_execution.rs b/src/test/ui/const-eval/conditional_array_execution.rs index dbddee862e022..9df2693d69140 100644 --- a/src/test/ui/const-eval/conditional_array_execution.rs +++ b/src/test/ui/const-eval/conditional_array_execution.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully const X: u32 = 5; const Y: u32 = 6; diff --git a/src/test/ui/const-eval/issue-43197.rs b/src/test/ui/const-eval/issue-43197.rs index 097fba4d3c450..c732300e43da7 100644 --- a/src/test/ui/const-eval/issue-43197.rs +++ b/src/test/ui/const-eval/issue-43197.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully #![feature(const_fn)] diff --git a/src/test/ui/const-eval/issue-44578.rs b/src/test/ui/const-eval/issue-44578.rs index 765113cfbb9e2..be6a2ed5f8841 100644 --- a/src/test/ui/const-eval/issue-44578.rs +++ b/src/test/ui/const-eval/issue-44578.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully trait Foo { const AMT: usize; diff --git a/src/test/ui/const-eval/promoted_errors.rs b/src/test/ui/const-eval/promoted_errors.rs index dc30c7f9cce04..dea84be37e1a7 100644 --- a/src/test/ui/const-eval/promoted_errors.rs +++ b/src/test/ui/const-eval/promoted_errors.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully // compile-flags: -O fn main() { println!("{}", 0u32 - 1); diff --git a/src/test/ui/const-eval/pub_const_err.rs b/src/test/ui/const-eval/pub_const_err.rs index c6bf07649af38..5c0666ba71e8b 100644 --- a/src/test/ui/const-eval/pub_const_err.rs +++ b/src/test/ui/const-eval/pub_const_err.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully #![crate_type = "lib"] diff --git a/src/test/ui/const-eval/pub_const_err_bin.rs b/src/test/ui/const-eval/pub_const_err_bin.rs index d87cb7ed77079..4000e8768d6f7 100644 --- a/src/test/ui/const-eval/pub_const_err_bin.rs +++ b/src/test/ui/const-eval/pub_const_err_bin.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// compile-pass +// must-compile-successfully pub const Z: u32 = 0 - 1; //~^ WARN attempt to subtract with overflow