separate const prop lint from optimizations
This commit is contained in:
parent
c99b42cf14
commit
5e4ff26618
28 changed files with 1558 additions and 238 deletions
|
@ -6,30 +6,25 @@ use std::cell::Cell;
|
||||||
use rustc_ast::Mutability;
|
use rustc_ast::Mutability;
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_hir::def::DefKind;
|
use rustc_hir::def::DefKind;
|
||||||
use rustc_hir::HirId;
|
|
||||||
use rustc_index::bit_set::BitSet;
|
use rustc_index::bit_set::BitSet;
|
||||||
use rustc_index::vec::IndexVec;
|
use rustc_index::vec::IndexVec;
|
||||||
use rustc_middle::mir::visit::{
|
use rustc_middle::mir::visit::{
|
||||||
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
|
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
|
||||||
};
|
};
|
||||||
use rustc_middle::mir::{
|
use rustc_middle::mir::{
|
||||||
AssertKind, BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind,
|
BasicBlock, BinOp, Body, Constant, ConstantKind, Local, LocalDecl, LocalKind, Location,
|
||||||
Location, Operand, Place, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
|
Operand, Place, Rvalue, SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnOp,
|
||||||
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
|
RETURN_PLACE,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
|
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
|
||||||
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
||||||
use rustc_middle::ty::{
|
use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeFoldable};
|
||||||
self, ConstInt, ConstKind, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, TypeFoldable,
|
|
||||||
};
|
|
||||||
use rustc_session::lint;
|
|
||||||
use rustc_span::{def_id::DefId, Span};
|
use rustc_span::{def_id::DefId, Span};
|
||||||
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
|
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
use rustc_trait_selection::traits;
|
use rustc_trait_selection::traits;
|
||||||
|
|
||||||
use crate::MirPass;
|
use crate::MirPass;
|
||||||
use rustc_const_eval::const_eval::ConstEvalErr;
|
|
||||||
use rustc_const_eval::interpret::{
|
use rustc_const_eval::interpret::{
|
||||||
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, CtfeValidationMode, Frame,
|
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, CtfeValidationMode, Frame,
|
||||||
ImmTy, Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, MemoryKind, OpTy,
|
ImmTy, Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, MemoryKind, OpTy,
|
||||||
|
@ -318,9 +313,8 @@ struct ConstPropagator<'mir, 'tcx> {
|
||||||
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
|
ecx: InterpCx<'mir, 'tcx, ConstPropMachine<'mir, 'tcx>>,
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
param_env: ParamEnv<'tcx>,
|
param_env: ParamEnv<'tcx>,
|
||||||
// FIXME(eddyb) avoid cloning these two fields more than once,
|
// FIXME(eddyb) avoid cloning this field more than once,
|
||||||
// by accessing them through `ecx` instead.
|
// by accessing it through `ecx` instead.
|
||||||
source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
|
|
||||||
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
local_decls: IndexVec<Local, LocalDecl<'tcx>>,
|
||||||
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
|
// Because we have `MutVisitor` we can't obtain the `SourceInfo` from a `Location`. So we store
|
||||||
// the last known `SourceInfo` here and just keep revisiting it.
|
// the last known `SourceInfo` here and just keep revisiting it.
|
||||||
|
@ -412,9 +406,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
ecx,
|
ecx,
|
||||||
tcx,
|
tcx,
|
||||||
param_env,
|
param_env,
|
||||||
// FIXME(eddyb) avoid cloning these two fields more than once,
|
// FIXME(eddyb) avoid cloning this field more than once,
|
||||||
// by accessing them through `ecx` instead.
|
// by accessing it through `ecx` instead.
|
||||||
source_scopes: body.source_scopes.clone(),
|
|
||||||
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
|
//FIXME(wesleywiser) we can't steal this because `Visitor::super_visit_body()` needs it
|
||||||
local_decls: body.local_decls.clone(),
|
local_decls: body.local_decls.clone(),
|
||||||
source_info: None,
|
source_info: None,
|
||||||
|
@ -445,10 +438,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
LocalState { value: LocalValue::Unallocated, layout: Cell::new(None) };
|
LocalState { value: LocalValue::Unallocated, layout: Cell::new(None) };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
|
|
||||||
source_info.scope.lint_root(&self.source_scopes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
|
fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
|
F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
|
||||||
|
@ -471,45 +460,13 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the value, if any, of evaluating `c`.
|
/// Returns the value, if any, of evaluating `c`.
|
||||||
fn eval_constant(&mut self, c: &Constant<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
fn eval_constant(&mut self, c: &Constant<'tcx>) -> Option<OpTy<'tcx>> {
|
||||||
// FIXME we need to revisit this for #67176
|
// FIXME we need to revisit this for #67176
|
||||||
if c.needs_subst() {
|
if c.needs_subst() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.ecx.mir_const_to_op(&c.literal, None) {
|
self.ecx.mir_const_to_op(&c.literal, None).ok()
|
||||||
Ok(op) => Some(op),
|
|
||||||
Err(error) => {
|
|
||||||
let tcx = self.ecx.tcx.at(c.span);
|
|
||||||
let err = ConstEvalErr::new(&self.ecx, error, Some(c.span));
|
|
||||||
if let Some(lint_root) = self.lint_root(source_info) {
|
|
||||||
let lint_only = match c.literal {
|
|
||||||
ConstantKind::Ty(ct) => match ct.val() {
|
|
||||||
// Promoteds must lint and not error as the user didn't ask for them
|
|
||||||
ConstKind::Unevaluated(ty::Unevaluated {
|
|
||||||
def: _,
|
|
||||||
substs: _,
|
|
||||||
promoted: Some(_),
|
|
||||||
}) => true,
|
|
||||||
// Out of backwards compatibility we cannot report hard errors in unused
|
|
||||||
// generic functions using associated constants of the generic parameters.
|
|
||||||
_ => c.literal.needs_subst(),
|
|
||||||
},
|
|
||||||
ConstantKind::Val(_, ty) => ty.needs_subst(),
|
|
||||||
};
|
|
||||||
if lint_only {
|
|
||||||
// Out of backwards compatibility we cannot report hard errors in unused
|
|
||||||
// generic functions using associated constants of the generic parameters.
|
|
||||||
err.report_as_lint(tcx, "erroneous constant used", lint_root, Some(c.span));
|
|
||||||
} else {
|
|
||||||
err.report_as_error(tcx, "erroneous constant used");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
err.report_as_error(tcx, "erroneous constant used");
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the value, if any, of evaluating `place`.
|
/// Returns the value, if any, of evaluating `place`.
|
||||||
|
@ -520,49 +477,22 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
|
|
||||||
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
|
/// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
|
||||||
/// or `eval_place`, depending on the variant of `Operand` used.
|
/// or `eval_place`, depending on the variant of `Operand` used.
|
||||||
fn eval_operand(&mut self, op: &Operand<'tcx>, source_info: SourceInfo) -> Option<OpTy<'tcx>> {
|
fn eval_operand(&mut self, op: &Operand<'tcx>) -> Option<OpTy<'tcx>> {
|
||||||
match *op {
|
match *op {
|
||||||
Operand::Constant(ref c) => self.eval_constant(c, source_info),
|
Operand::Constant(ref c) => self.eval_constant(c),
|
||||||
Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
|
Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report_assert_as_lint(
|
fn check_unary_op(&mut self, op: UnOp, arg: &Operand<'tcx>) -> Option<()> {
|
||||||
&self,
|
if self.use_ecx(|this| {
|
||||||
lint: &'static lint::Lint,
|
|
||||||
source_info: SourceInfo,
|
|
||||||
message: &'static str,
|
|
||||||
panic: AssertKind<impl std::fmt::Debug>,
|
|
||||||
) {
|
|
||||||
if let Some(lint_root) = self.lint_root(source_info) {
|
|
||||||
self.tcx.struct_span_lint_hir(lint, lint_root, source_info.span, |lint| {
|
|
||||||
let mut err = lint.build(message);
|
|
||||||
err.span_label(source_info.span, format!("{:?}", panic));
|
|
||||||
err.emit();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_unary_op(
|
|
||||||
&mut self,
|
|
||||||
op: UnOp,
|
|
||||||
arg: &Operand<'tcx>,
|
|
||||||
source_info: SourceInfo,
|
|
||||||
) -> Option<()> {
|
|
||||||
if let (val, true) = self.use_ecx(|this| {
|
|
||||||
let val = this.ecx.read_immediate(&this.ecx.eval_operand(arg, None)?)?;
|
let val = this.ecx.read_immediate(&this.ecx.eval_operand(arg, None)?)?;
|
||||||
let (_res, overflow, _ty) = this.ecx.overflowing_unary_op(op, &val)?;
|
let (_res, overflow, _ty) = this.ecx.overflowing_unary_op(op, &val)?;
|
||||||
Ok((val, overflow))
|
Ok(overflow)
|
||||||
})? {
|
})? {
|
||||||
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
|
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
|
||||||
// appropriate to use.
|
// appropriate to use.
|
||||||
assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
|
assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
|
||||||
self.report_assert_as_lint(
|
|
||||||
lint::builtin::ARITHMETIC_OVERFLOW,
|
|
||||||
source_info,
|
|
||||||
"this arithmetic operation will overflow",
|
|
||||||
AssertKind::OverflowNeg(val.to_const_int()),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -574,7 +504,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
op: BinOp,
|
op: BinOp,
|
||||||
left: &Operand<'tcx>,
|
left: &Operand<'tcx>,
|
||||||
right: &Operand<'tcx>,
|
right: &Operand<'tcx>,
|
||||||
source_info: SourceInfo,
|
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let r = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?));
|
let r = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(right, None)?));
|
||||||
let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?));
|
let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?));
|
||||||
|
@ -589,25 +518,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
let r_bits = r.to_scalar().ok();
|
let r_bits = r.to_scalar().ok();
|
||||||
let r_bits = r_bits.and_then(|r| r.to_bits(right_size).ok());
|
let r_bits = r_bits.and_then(|r| r.to_bits(right_size).ok());
|
||||||
if r_bits.map_or(false, |b| b >= left_size.bits() as u128) {
|
if r_bits.map_or(false, |b| b >= left_size.bits() as u128) {
|
||||||
debug!("check_binary_op: reporting assert for {:?}", source_info);
|
|
||||||
self.report_assert_as_lint(
|
|
||||||
lint::builtin::ARITHMETIC_OVERFLOW,
|
|
||||||
source_info,
|
|
||||||
"this arithmetic operation will overflow",
|
|
||||||
AssertKind::Overflow(
|
|
||||||
op,
|
|
||||||
match l {
|
|
||||||
Some(l) => l.to_const_int(),
|
|
||||||
// Invent a dummy value, the diagnostic ignores it anyway
|
|
||||||
None => ConstInt::new(
|
|
||||||
ScalarInt::try_from_uint(1_u8, left_size).unwrap(),
|
|
||||||
left_ty.is_signed(),
|
|
||||||
left_ty.is_ptr_sized_integral(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
r.to_const_int(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -618,12 +528,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
|
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
|
||||||
Ok(overflow)
|
Ok(overflow)
|
||||||
})? {
|
})? {
|
||||||
self.report_assert_as_lint(
|
|
||||||
lint::builtin::ARITHMETIC_OVERFLOW,
|
|
||||||
source_info,
|
|
||||||
"this arithmetic operation will overflow",
|
|
||||||
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -656,12 +560,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn const_prop(
|
fn const_prop(&mut self, rvalue: &Rvalue<'tcx>, place: Place<'tcx>) -> Option<()> {
|
||||||
&mut self,
|
|
||||||
rvalue: &Rvalue<'tcx>,
|
|
||||||
source_info: SourceInfo,
|
|
||||||
place: Place<'tcx>,
|
|
||||||
) -> Option<()> {
|
|
||||||
// Perform any special handling for specific Rvalue types.
|
// Perform any special handling for specific Rvalue types.
|
||||||
// Generally, checks here fall into one of two categories:
|
// Generally, checks here fall into one of two categories:
|
||||||
// 1. Additional checking to provide useful lints to the user
|
// 1. Additional checking to provide useful lints to the user
|
||||||
|
@ -676,11 +575,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
// lint.
|
// lint.
|
||||||
Rvalue::UnaryOp(op, arg) => {
|
Rvalue::UnaryOp(op, arg) => {
|
||||||
trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
|
trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
|
||||||
self.check_unary_op(*op, arg, source_info)?;
|
self.check_unary_op(*op, arg)?;
|
||||||
}
|
}
|
||||||
Rvalue::BinaryOp(op, box (left, right)) => {
|
Rvalue::BinaryOp(op, box (left, right)) => {
|
||||||
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
|
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
|
||||||
self.check_binary_op(*op, left, right, source_info)?;
|
self.check_binary_op(*op, left, right)?;
|
||||||
}
|
}
|
||||||
Rvalue::CheckedBinaryOp(op, box (left, right)) => {
|
Rvalue::CheckedBinaryOp(op, box (left, right)) => {
|
||||||
trace!(
|
trace!(
|
||||||
|
@ -689,7 +588,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
left,
|
left,
|
||||||
right
|
right
|
||||||
);
|
);
|
||||||
self.check_binary_op(*op, left, right, source_info)?;
|
self.check_binary_op(*op, left, right)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not try creating references (#67862)
|
// Do not try creating references (#67862)
|
||||||
|
@ -1071,7 +970,7 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
||||||
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
|
fn visit_constant(&mut self, constant: &mut Constant<'tcx>, location: Location) {
|
||||||
trace!("visit_constant: {:?}", constant);
|
trace!("visit_constant: {:?}", constant);
|
||||||
self.super_constant(constant, location);
|
self.super_constant(constant, location);
|
||||||
self.eval_constant(constant, self.source_info.unwrap());
|
self.eval_constant(constant);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
|
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
|
||||||
|
@ -1080,7 +979,7 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
||||||
self.source_info = Some(source_info);
|
self.source_info = Some(source_info);
|
||||||
if let StatementKind::Assign(box (place, ref mut rval)) = statement.kind {
|
if let StatementKind::Assign(box (place, ref mut rval)) = statement.kind {
|
||||||
let can_const_prop = self.ecx.machine.can_const_prop[place.local];
|
let can_const_prop = self.ecx.machine.can_const_prop[place.local];
|
||||||
if let Some(()) = self.const_prop(rval, source_info, place) {
|
if let Some(()) = self.const_prop(rval, place) {
|
||||||
// This will return None if the above `const_prop` invocation only "wrote" a
|
// This will return None if the above `const_prop` invocation only "wrote" a
|
||||||
// type whose creation requires no write. E.g. a generator whose initial state
|
// type whose creation requires no write. E.g. a generator whose initial state
|
||||||
// consists solely of uninitialized memory (so it doesn't capture any locals).
|
// consists solely of uninitialized memory (so it doesn't capture any locals).
|
||||||
|
@ -1164,57 +1063,12 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
||||||
self.source_info = Some(source_info);
|
self.source_info = Some(source_info);
|
||||||
self.super_terminator(terminator, location);
|
self.super_terminator(terminator, location);
|
||||||
match &mut terminator.kind {
|
match &mut terminator.kind {
|
||||||
TerminatorKind::Assert { expected, ref msg, ref mut cond, .. } => {
|
TerminatorKind::Assert { expected, ref mut cond, .. } => {
|
||||||
if let Some(ref value) = self.eval_operand(&cond, source_info) {
|
if let Some(ref value) = self.eval_operand(&cond) {
|
||||||
trace!("assertion on {:?} should be {:?}", value, expected);
|
trace!("assertion on {:?} should be {:?}", value, expected);
|
||||||
let expected = ScalarMaybeUninit::from(Scalar::from_bool(*expected));
|
let expected = ScalarMaybeUninit::from(Scalar::from_bool(*expected));
|
||||||
let value_const = self.ecx.read_scalar(&value).unwrap();
|
let value_const = self.ecx.read_scalar(&value).unwrap();
|
||||||
if expected != value_const {
|
if expected != value_const {
|
||||||
enum DbgVal<T> {
|
|
||||||
Val(T),
|
|
||||||
Underscore,
|
|
||||||
}
|
|
||||||
impl<T: std::fmt::Debug> std::fmt::Debug for DbgVal<T> {
|
|
||||||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Val(val) => val.fmt(fmt),
|
|
||||||
Self::Underscore => fmt.write_str("_"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut eval_to_int = |op| {
|
|
||||||
// This can be `None` if the lhs wasn't const propagated and we just
|
|
||||||
// triggered the assert on the value of the rhs.
|
|
||||||
self.eval_operand(op, source_info).map_or(DbgVal::Underscore, |op| {
|
|
||||||
DbgVal::Val(self.ecx.read_immediate(&op).unwrap().to_const_int())
|
|
||||||
})
|
|
||||||
};
|
|
||||||
let msg = match msg {
|
|
||||||
AssertKind::DivisionByZero(op) => {
|
|
||||||
Some(AssertKind::DivisionByZero(eval_to_int(op)))
|
|
||||||
}
|
|
||||||
AssertKind::RemainderByZero(op) => {
|
|
||||||
Some(AssertKind::RemainderByZero(eval_to_int(op)))
|
|
||||||
}
|
|
||||||
AssertKind::Overflow(bin_op @ (BinOp::Div | BinOp::Rem), op1, op2) => {
|
|
||||||
// Division overflow is *UB* in the MIR, and different than the
|
|
||||||
// other overflow checks.
|
|
||||||
Some(AssertKind::Overflow(
|
|
||||||
*bin_op,
|
|
||||||
eval_to_int(op1),
|
|
||||||
eval_to_int(op2),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
AssertKind::BoundsCheck { ref len, ref index } => {
|
|
||||||
let len = eval_to_int(len);
|
|
||||||
let index = eval_to_int(index);
|
|
||||||
Some(AssertKind::BoundsCheck { len, index })
|
|
||||||
}
|
|
||||||
// Remaining overflow errors are already covered by checks on the binary operators.
|
|
||||||
AssertKind::Overflow(..) | AssertKind::OverflowNeg(_) => None,
|
|
||||||
// Need proper const propagator for these.
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
// Poison all places this operand references so that further code
|
// Poison all places this operand references so that further code
|
||||||
// doesn't use the invalid value
|
// doesn't use the invalid value
|
||||||
match cond {
|
match cond {
|
||||||
|
@ -1223,14 +1077,6 @@ impl<'tcx> MutVisitor<'tcx> for ConstPropagator<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
Operand::Constant(_) => {}
|
Operand::Constant(_) => {}
|
||||||
}
|
}
|
||||||
if let Some(msg) = msg {
|
|
||||||
self.report_assert_as_lint(
|
|
||||||
lint::builtin::UNCONDITIONAL_PANIC,
|
|
||||||
source_info,
|
|
||||||
"this operation will panic at runtime",
|
|
||||||
msg,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if self.should_const_prop(value) {
|
if self.should_const_prop(value) {
|
||||||
if let ScalarMaybeUninit::Scalar(scalar) = value_const {
|
if let ScalarMaybeUninit::Scalar(scalar) = value_const {
|
||||||
|
|
1295
compiler/rustc_mir_transform/src/const_prop_lint.rs
Normal file
1295
compiler/rustc_mir_transform/src/const_prop_lint.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -49,6 +49,7 @@ pub mod cleanup_post_borrowck;
|
||||||
mod const_debuginfo;
|
mod const_debuginfo;
|
||||||
mod const_goto;
|
mod const_goto;
|
||||||
mod const_prop;
|
mod const_prop;
|
||||||
|
mod const_prop_lint;
|
||||||
mod coverage;
|
mod coverage;
|
||||||
mod deaggregator;
|
mod deaggregator;
|
||||||
mod deduplicate_blocks;
|
mod deduplicate_blocks;
|
||||||
|
@ -430,6 +431,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
|
||||||
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
|
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
|
||||||
// and it can help optimizations.
|
// and it can help optimizations.
|
||||||
&deaggregator::Deaggregator,
|
&deaggregator::Deaggregator,
|
||||||
|
&const_prop_lint::ConstProp,
|
||||||
];
|
];
|
||||||
|
|
||||||
pm::run_passes(tcx, body, post_borrowck_cleanup);
|
pm::run_passes(tcx, body, post_borrowck_cleanup);
|
||||||
|
|
|
@ -4,12 +4,12 @@ trait Foo {
|
||||||
const BAR: u32;
|
const BAR: u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR; //~ ERROR E0391
|
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
|
||||||
|
|
||||||
struct GlobalImplRef;
|
struct GlobalImplRef;
|
||||||
|
|
||||||
impl GlobalImplRef {
|
impl GlobalImplRef {
|
||||||
const BAR: u32 = IMPL_REF_BAR;
|
const BAR: u32 = IMPL_REF_BAR; //~ ERROR E0391
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
error[E0391]: cycle detected when simplifying constant for the type system `IMPL_REF_BAR`
|
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`
|
||||||
|
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
|
||||||
|
|
|
||||||
|
LL | const BAR: u32 = IMPL_REF_BAR;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: ...which requires normalizing `IMPL_REF_BAR`...
|
||||||
|
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
||||||
|
|
|
|
||||||
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
|
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
|
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
||||||
|
|
|
|
||||||
|
@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
||||||
|
|
|
|
||||||
LL | const BAR: u32 = IMPL_REF_BAR;
|
LL | const BAR: u32 = IMPL_REF_BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
= note: ...which requires normalizing `IMPL_REF_BAR`...
|
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`, completing the cycle
|
||||||
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
|
|
||||||
= note: cycle used when running analysis passes on this crate
|
= note: cycle used when running analysis passes on this crate
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
|
@ -5,10 +5,10 @@ trait Foo {
|
||||||
}
|
}
|
||||||
|
|
||||||
trait FooDefault {
|
trait FooDefault {
|
||||||
const BAR: u32 = DEFAULT_REF_BAR;
|
const BAR: u32 = DEFAULT_REF_BAR; //~ ERROR E0391
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR; //~ ERROR E0391
|
const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
|
||||||
|
|
||||||
struct GlobalDefaultRef;
|
struct GlobalDefaultRef;
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
error[E0391]: cycle detected when simplifying constant for the type system `DEFAULT_REF_BAR`
|
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
|
||||||
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
||||||
|
|
|
||||||
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
|
||||||
|
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
||||||
|
|
|
|
||||||
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
|
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
|
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
||||||
|
|
|
|
||||||
|
@ -35,8 +41,7 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
|
||||||
|
|
|
|
||||||
LL | const BAR: u32 = DEFAULT_REF_BAR;
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
|
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
|
||||||
= note: ...which again requires simplifying constant for the type system `DEFAULT_REF_BAR`, completing the cycle
|
|
||||||
= note: cycle used when running analysis passes on this crate
|
= note: cycle used when running analysis passes on this crate
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
|
@ -4,12 +4,12 @@ trait Foo {
|
||||||
const BAR: u32;
|
const BAR: u32;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR; //~ ERROR E0391
|
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
|
||||||
|
|
||||||
struct GlobalTraitRef;
|
struct GlobalTraitRef;
|
||||||
|
|
||||||
impl Foo for GlobalTraitRef {
|
impl Foo for GlobalTraitRef {
|
||||||
const BAR: u32 = TRAIT_REF_BAR;
|
const BAR: u32 = TRAIT_REF_BAR; //~ ERROR E0391
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
error[E0391]: cycle detected when simplifying constant for the type system `TRAIT_REF_BAR`
|
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`
|
||||||
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
|
||||||
|
|
|
||||||
|
LL | const BAR: u32 = TRAIT_REF_BAR;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: ...which requires normalizing `TRAIT_REF_BAR`...
|
||||||
|
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
||||||
|
|
|
|
||||||
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
|
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
|
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
|
||||||
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
||||||
|
|
|
|
||||||
|
@ -35,8 +41,7 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
||||||
|
|
|
|
||||||
LL | const BAR: u32 = TRAIT_REF_BAR;
|
LL | const BAR: u32 = TRAIT_REF_BAR;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
= note: ...which requires normalizing `TRAIT_REF_BAR`...
|
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`, completing the cycle
|
||||||
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
|
|
||||||
= note: cycle used when running analysis passes on this crate
|
= note: cycle used when running analysis passes on this crate
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
|
@ -1,18 +1,15 @@
|
||||||
// build-fail
|
// build-pass
|
||||||
// compile-flags: -Zmir-opt-level=3
|
// compile-flags: -Zmir-opt-level=3
|
||||||
|
// Overflow can't be detected by const prop
|
||||||
|
// could only be detected after optimizations
|
||||||
|
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = add(u8::MAX, 1);
|
let _ = add(u8::MAX, 1);
|
||||||
//~^ NOTE in this expansion of inlined source
|
|
||||||
//~| NOTE in this expansion of inlined source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn add(x: u8, y: u8) -> u8 {
|
fn add(x: u8, y: u8) -> u8 {
|
||||||
x + y
|
x + y
|
||||||
//~^ ERROR this arithmetic operation will overflow
|
|
||||||
//~| NOTE attempt to compute `u8::MAX + 1_u8`, which would overflow
|
|
||||||
//~| NOTE `#[deny(arithmetic_overflow)]` on by default
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
error: this arithmetic operation will overflow
|
|
||||||
--> $DIR/inline_spans.rs:14:5
|
|
||||||
|
|
|
||||||
LL | let _ = add(u8::MAX, 1);
|
|
||||||
| --------------- in this inlined function call
|
|
||||||
...
|
|
||||||
LL | x + y
|
|
||||||
| ^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
|
|
||||||
|
|
|
||||||
= note: `#[deny(arithmetic_overflow)]` on by default
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ LL | let x: &'static i32 = &X;
|
||||||
| ^ referenced constant has errors
|
| ^ referenced constant has errors
|
||||||
query stack during panic:
|
query stack during panic:
|
||||||
#0 [try_normalize_mir_const_after_erasing_regions] normalizing `main::promoted[1]`
|
#0 [try_normalize_mir_const_after_erasing_regions] normalizing `main::promoted[1]`
|
||||||
#1 [optimized_mir] optimizing MIR for `main`
|
#1 [mir_drops_elaborated_and_const_checked] elaborating drops for `main`
|
||||||
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
|
#2 [optimized_mir] optimizing MIR for `main`
|
||||||
|
#3 [collect_and_partition_mono_items] collect_and_partition_mono_items
|
||||||
end of query stack
|
end of query stack
|
||||||
|
|
|
@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
|
||||||
--> $DIR/issue-49296.rs:9:16
|
--> $DIR/issue-49296.rs:9:16
|
||||||
|
|
|
|
||||||
LL | const X: u64 = *wat(42);
|
LL | const X: u64 = *wat(42);
|
||||||
| ^^^^^^^^ pointer to alloc2 was dereferenced after this allocation got freed
|
| ^^^^^^^^ pointer to alloc3 was dereferenced after this allocation got freed
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,5 @@ impl PrintName {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = PrintName::VOID;
|
let _ = PrintName::VOID;
|
||||||
|
//~^ ERROR erroneous constant used [E0080]
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,12 @@ LL | const VOID: ! = panic!();
|
||||||
|
|
|
|
||||||
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: aborting due to previous error
|
error[E0080]: erroneous constant used
|
||||||
|
--> $DIR/panic-assoc-never-type.rs:15:13
|
||||||
|
|
|
||||||
|
LL | let _ = PrintName::VOID;
|
||||||
|
| ^^^^^^^^^^^^^^^ referenced constant has errors
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0080`.
|
For more information about this error, try `rustc --explain E0080`.
|
||||||
|
|
|
@ -1,3 +1,45 @@
|
||||||
|
warning: this arithmetic operation will overflow
|
||||||
|
--> $DIR/promoted_errors.rs:15:5
|
||||||
|
|
|
||||||
|
LL | 0 - 1
|
||||||
|
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:20
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:21:5
|
||||||
|
|
|
||||||
|
LL | 1 / 0
|
||||||
|
| ^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:41
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:27:5
|
||||||
|
|
|
||||||
|
LL | 1 / (1 - 1)
|
||||||
|
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:31:5
|
||||||
|
|
|
||||||
|
LL | 1 / (false as i32)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:35:5
|
||||||
|
|
|
||||||
|
LL | [1, 2, 3][4]
|
||||||
|
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:15:5
|
--> $DIR/promoted_errors.rs:15:5
|
||||||
|
|
|
|
||||||
|
@ -6,7 +48,7 @@ LL | 0 - 1
|
||||||
| |
|
| |
|
||||||
| attempt to compute `0_u32 - 1_u32`, which would overflow
|
| attempt to compute `0_u32 - 1_u32`, which would overflow
|
||||||
| inside `overflow` at $DIR/promoted_errors.rs:15:5
|
| inside `overflow` at $DIR/promoted_errors.rs:15:5
|
||||||
| inside `X` at $DIR/promoted_errors.rs:38:29
|
| inside `X` at $DIR/promoted_errors.rs:43:29
|
||||||
...
|
...
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:38:28
|
--> $DIR/promoted_errors.rs:43:28
|
||||||
|
|
|
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -41,5 +83,5 @@ LL | | };
|
||||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
= 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>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: 2 warnings emitted
|
warning: 7 warnings emitted
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,54 @@
|
||||||
|
warning: this arithmetic operation will overflow
|
||||||
|
--> $DIR/promoted_errors.rs:15:5
|
||||||
|
|
|
||||||
|
LL | 0 - 1
|
||||||
|
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:20
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:21:5
|
||||||
|
|
|
||||||
|
LL | 1 / 0
|
||||||
|
| ^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:41
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:27:5
|
||||||
|
|
|
||||||
|
LL | 1 / (1 - 1)
|
||||||
|
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:31:5
|
||||||
|
|
|
||||||
|
LL | 1 / (false as i32)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:35:5
|
||||||
|
|
|
||||||
|
LL | [1, 2, 3][4]
|
||||||
|
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:20:5
|
--> $DIR/promoted_errors.rs:21:5
|
||||||
|
|
|
|
||||||
LL | 1 / 0
|
LL | 1 / 0
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
| |
|
| |
|
||||||
| attempt to divide `1_i32` by zero
|
| attempt to divide `1_i32` by zero
|
||||||
| inside `div_by_zero1` at $DIR/promoted_errors.rs:20:5
|
| inside `div_by_zero1` at $DIR/promoted_errors.rs:21:5
|
||||||
| inside `X` at $DIR/promoted_errors.rs:41:29
|
| inside `X` at $DIR/promoted_errors.rs:46:29
|
||||||
...
|
...
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:41:28
|
--> $DIR/promoted_errors.rs:46:28
|
||||||
|
|
|
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -42,5 +84,5 @@ LL | | };
|
||||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
= 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>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: 2 warnings emitted
|
warning: 7 warnings emitted
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,45 @@
|
||||||
|
warning: this arithmetic operation will overflow
|
||||||
|
--> $DIR/promoted_errors.rs:15:5
|
||||||
|
|
|
||||||
|
LL | 0 - 1
|
||||||
|
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:20
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:21:5
|
||||||
|
|
|
||||||
|
LL | 1 / 0
|
||||||
|
| ^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/promoted_errors.rs:11:41
|
||||||
|
|
|
||||||
|
LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:27:5
|
||||||
|
|
|
||||||
|
LL | 1 / (1 - 1)
|
||||||
|
| ^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:31:5
|
||||||
|
|
|
||||||
|
LL | 1 / (false as i32)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^ attempt to divide `1_i32` by zero
|
||||||
|
|
||||||
|
warning: this operation will panic at runtime
|
||||||
|
--> $DIR/promoted_errors.rs:35:5
|
||||||
|
|
|
||||||
|
LL | [1, 2, 3][4]
|
||||||
|
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:15:5
|
--> $DIR/promoted_errors.rs:15:5
|
||||||
|
|
|
|
||||||
|
@ -6,7 +48,7 @@ LL | 0 - 1
|
||||||
| |
|
| |
|
||||||
| attempt to compute `0_u32 - 1_u32`, which would overflow
|
| attempt to compute `0_u32 - 1_u32`, which would overflow
|
||||||
| inside `overflow` at $DIR/promoted_errors.rs:15:5
|
| inside `overflow` at $DIR/promoted_errors.rs:15:5
|
||||||
| inside `X` at $DIR/promoted_errors.rs:38:29
|
| inside `X` at $DIR/promoted_errors.rs:43:29
|
||||||
...
|
...
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -26,7 +68,7 @@ LL | #![warn(const_err, arithmetic_overflow, unconditional_panic)]
|
||||||
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: any use of this value will cause an error
|
warning: any use of this value will cause an error
|
||||||
--> $DIR/promoted_errors.rs:38:28
|
--> $DIR/promoted_errors.rs:43:28
|
||||||
|
|
|
|
||||||
LL | / const X: () = {
|
LL | / const X: () = {
|
||||||
LL | | let _x: &'static u32 = &overflow();
|
LL | | let _x: &'static u32 = &overflow();
|
||||||
|
@ -41,5 +83,5 @@ LL | | };
|
||||||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
= 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>
|
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
|
||||||
|
|
||||||
warning: 2 warnings emitted
|
warning: 7 warnings emitted
|
||||||
|
|
||||||
|
|
|
@ -15,20 +15,25 @@ const fn overflow() -> u32 {
|
||||||
0 - 1
|
0 - 1
|
||||||
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
|
//[opt_with_overflow_checks,noopt]~^ WARN any use of this value will cause an error
|
||||||
//[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
|
//[opt_with_overflow_checks,noopt]~| WARN this was previously accepted by the compiler
|
||||||
|
//~^^^ WARN this arithmetic operation will overflow
|
||||||
}
|
}
|
||||||
const fn div_by_zero1() -> i32 {
|
const fn div_by_zero1() -> i32 {
|
||||||
1 / 0
|
1 / 0
|
||||||
//[opt]~^ WARN any use of this value will cause an error
|
//[opt]~^ WARN any use of this value will cause an error
|
||||||
//[opt]~| WARN this was previously accepted by the compiler but is being phased out
|
//[opt]~| WARN this was previously accepted by the compiler but is being phased out
|
||||||
|
//~^^^ WARN this operation will panic at runtime
|
||||||
}
|
}
|
||||||
const fn div_by_zero2() -> i32 {
|
const fn div_by_zero2() -> i32 {
|
||||||
1 / (1 - 1)
|
1 / (1 - 1)
|
||||||
|
//~^ WARN this operation will panic at runtime
|
||||||
}
|
}
|
||||||
const fn div_by_zero3() -> i32 {
|
const fn div_by_zero3() -> i32 {
|
||||||
1 / (false as i32)
|
1 / (false as i32)
|
||||||
|
//~^ WARN this operation will panic at runtime
|
||||||
}
|
}
|
||||||
const fn oob() -> i32 {
|
const fn oob() -> i32 {
|
||||||
[1, 2, 3][4]
|
[1, 2, 3][4]
|
||||||
|
//~^ WARN this operation will panic at runtime
|
||||||
}
|
}
|
||||||
|
|
||||||
// An unused constant containing failing promoteds.
|
// An unused constant containing failing promoteds.
|
||||||
|
|
|
@ -28,6 +28,7 @@ const fn read_field3() -> Field3 {
|
||||||
const FIELD3: Field3 = unsafe { UNION.field3 };
|
const FIELD3: Field3 = unsafe { UNION.field3 };
|
||||||
//~^ ERROR it is undefined behavior to use this value
|
//~^ ERROR it is undefined behavior to use this value
|
||||||
FIELD3
|
FIELD3
|
||||||
|
//~^ ERROR erroneous constant used [E0080]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -9,6 +9,12 @@ LL | const FIELD3: Field3 = unsafe { UNION.field3 };
|
||||||
__ __ __ __ __ __ __ __ │ ░░░░░░░░
|
__ __ __ __ __ __ __ __ │ ░░░░░░░░
|
||||||
}
|
}
|
||||||
|
|
||||||
error: aborting due to previous error
|
error[E0080]: erroneous constant used
|
||||||
|
--> $DIR/union-const-eval-field.rs:30:5
|
||||||
|
|
|
||||||
|
LL | FIELD3
|
||||||
|
| ^^^^^^ referenced constant has errors
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0080`.
|
For more information about this error, try `rustc --explain E0080`.
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
const fn foo() { (||{})() }
|
const fn foo() { (||{})() }
|
||||||
//~^ ERROR cannot call non-const closure
|
//~^ ERROR cannot call non-const closure
|
||||||
|
//~| ERROR erroneous constant used [const_err]
|
||||||
|
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||||
|
|
||||||
const fn bad(input: fn()) {
|
const fn bad(input: fn()) {
|
||||||
input()
|
input()
|
||||||
|
|
|
@ -8,11 +8,21 @@ LL | const fn foo() { (||{})() }
|
||||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||||
|
|
||||||
error: function pointers are not allowed in const fn
|
error: function pointers are not allowed in const fn
|
||||||
--> $DIR/issue-56164.rs:5:5
|
--> $DIR/issue-56164.rs:7:5
|
||||||
|
|
|
|
||||||
LL | input()
|
LL | input()
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: erroneous constant used
|
||||||
|
--> $DIR/issue-56164.rs:1:18
|
||||||
|
|
|
||||||
|
LL | const fn foo() { (||{})() }
|
||||||
|
| ^^^^^^ referenced constant has errors
|
||||||
|
|
|
||||||
|
= 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 3 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0015`.
|
For more information about this error, try `rustc --explain E0015`.
|
||||||
|
|
|
@ -8,7 +8,10 @@ static _FOO: () = panic!(true);
|
||||||
//~^ ERROR: argument to `panic!()` in a const context must have type `&str`
|
//~^ ERROR: argument to `panic!()` in a const context must have type `&str`
|
||||||
|
|
||||||
const fn _foo() {
|
const fn _foo() {
|
||||||
panic!(&1); //~ ERROR: argument to `panic!()` in a const context must have type `&str`
|
panic!(&1);
|
||||||
|
//~^ ERROR: argument to `panic!()` in a const context must have type `&str`
|
||||||
|
//~| ERROR: erroneous constant used [const_err]
|
||||||
|
//~| WARNING: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure that conforming panics don't cause an error
|
// ensure that conforming panics don't cause an error
|
||||||
|
|
|
@ -22,5 +22,15 @@ LL | panic!(&1);
|
||||||
|
|
|
|
||||||
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: erroneous constant used
|
||||||
|
--> $DIR/issue-66693.rs:11:12
|
||||||
|
|
|
||||||
|
LL | panic!(&1);
|
||||||
|
| ^^ referenced constant has errors
|
||||||
|
|
|
||||||
|
= 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 4 previous errors
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,14 @@
|
||||||
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
|
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
|
||||||
|
|
||||||
// build-pass
|
// build-pass
|
||||||
#[allow(arithmetic_overflow)]
|
// #![allow(arithmetic_overflow, unconditional_panic)]
|
||||||
|
|
||||||
const fn assert_static<T>(_: &'static T) {}
|
const fn assert_static<T>(_: &'static T) {}
|
||||||
|
|
||||||
const fn fail() -> i32 { 1/0 }
|
#[allow(unconditional_panic)]
|
||||||
|
const fn fail() -> i32 {
|
||||||
|
1/0
|
||||||
|
}
|
||||||
const C: i32 = {
|
const C: i32 = {
|
||||||
// Promoted that fails to evaluate in dead code -- this must work
|
// Promoted that fails to evaluate in dead code -- this must work
|
||||||
// (for backwards compatibility reasons).
|
// (for backwards compatibility reasons).
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
|
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
|
||||||
//~| NOTE ...which requires computing layout of `core::option::Option<S>`...
|
//~| NOTE ...which requires computing layout of `core::option::Option<S>`...
|
||||||
//~| NOTE ...which again requires computing layout of `S`, completing the cycle
|
//~| NOTE ...which again requires computing layout of `S`, completing the cycle
|
||||||
//~| NOTE cycle used when computing layout of `core::option::Option<S>`
|
|
||||||
|
|
||||||
// build-fail
|
// build-fail
|
||||||
|
|
||||||
|
@ -15,5 +14,6 @@ impl<T: ?Sized> Mirror for T {
|
||||||
struct S(Option<<S as Mirror>::It>);
|
struct S(Option<<S as Mirror>::It>);
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
//~^ NOTE cycle used when elaborating drops for `main`
|
||||||
let _s = S(None);
|
let _s = S(None);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,11 @@ error[E0391]: cycle detected when computing layout of `S`
|
||||||
= note: ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
|
= note: ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
|
||||||
= note: ...which requires computing layout of `core::option::Option<S>`...
|
= note: ...which requires computing layout of `core::option::Option<S>`...
|
||||||
= note: ...which again requires computing layout of `S`, completing the cycle
|
= note: ...which again requires computing layout of `S`, completing the cycle
|
||||||
= note: cycle used when computing layout of `core::option::Option<S>`
|
note: cycle used when elaborating drops for `main`
|
||||||
|
--> $DIR/issue-26548-recursion-via-normalize.rs:16:1
|
||||||
|
|
|
||||||
|
LL | fn main() {
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue