Avoid some packing/unpacking of the AssertLint enum
This commit is contained in:
parent
249ec9f08b
commit
3419273f1f
2 changed files with 41 additions and 49 deletions
|
@ -20,7 +20,7 @@ use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, Va
|
||||||
use crate::const_prop::CanConstProp;
|
use crate::const_prop::CanConstProp;
|
||||||
use crate::const_prop::ConstPropMachine;
|
use crate::const_prop::ConstPropMachine;
|
||||||
use crate::const_prop::ConstPropMode;
|
use crate::const_prop::ConstPropMode;
|
||||||
use crate::errors::AssertLint;
|
use crate::errors::{AssertLint, AssertLintKind};
|
||||||
use crate::MirLint;
|
use crate::MirLint;
|
||||||
|
|
||||||
pub struct ConstPropLint;
|
pub struct ConstPropLint;
|
||||||
|
@ -300,9 +300,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn report_assert_as_lint(&self, source_info: &SourceInfo, lint: AssertLint<impl Debug>) {
|
fn report_assert_as_lint(
|
||||||
|
&self,
|
||||||
|
location: Location,
|
||||||
|
lint_kind: AssertLintKind,
|
||||||
|
assert_kind: AssertKind<impl Debug>,
|
||||||
|
) {
|
||||||
|
let source_info = self.body.source_info(location);
|
||||||
if let Some(lint_root) = self.lint_root(*source_info) {
|
if let Some(lint_root) = self.lint_root(*source_info) {
|
||||||
self.tcx.emit_node_span_lint(lint.lint(), lint_root, source_info.span, lint);
|
let span = source_info.span;
|
||||||
|
self.tcx.emit_node_span_lint(
|
||||||
|
lint_kind.lint(),
|
||||||
|
lint_root,
|
||||||
|
span,
|
||||||
|
AssertLint { span, assert_kind, lint_kind },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,13 +328,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
// `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");
|
||||||
let source_info = self.body.source_info(location);
|
|
||||||
self.report_assert_as_lint(
|
self.report_assert_as_lint(
|
||||||
source_info,
|
location,
|
||||||
AssertLint::ArithmeticOverflow(
|
AssertLintKind::ArithmeticOverflow,
|
||||||
source_info.span,
|
AssertKind::OverflowNeg(val.to_const_int()),
|
||||||
AssertKind::OverflowNeg(val.to_const_int()),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -354,7 +363,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
let r_bits = r.to_scalar().to_bits(right_size).ok();
|
let r_bits = r.to_scalar().to_bits(right_size).ok();
|
||||||
if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
|
if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
|
||||||
debug!("check_binary_op: reporting assert for {:?}", location);
|
debug!("check_binary_op: reporting assert for {:?}", location);
|
||||||
let source_info = self.body.source_info(location);
|
|
||||||
let panic = AssertKind::Overflow(
|
let panic = AssertKind::Overflow(
|
||||||
op,
|
op,
|
||||||
match l {
|
match l {
|
||||||
|
@ -368,10 +376,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
},
|
},
|
||||||
r.to_const_int(),
|
r.to_const_int(),
|
||||||
);
|
);
|
||||||
self.report_assert_as_lint(
|
self.report_assert_as_lint(location, AssertLintKind::ArithmeticOverflow, panic);
|
||||||
source_info,
|
|
||||||
AssertLint::ArithmeticOverflow(source_info.span, panic),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -382,13 +387,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
|
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
|
||||||
Ok(overflow)
|
Ok(overflow)
|
||||||
})? {
|
})? {
|
||||||
let source_info = self.body.source_info(location);
|
|
||||||
self.report_assert_as_lint(
|
self.report_assert_as_lint(
|
||||||
source_info,
|
location,
|
||||||
AssertLint::ArithmeticOverflow(
|
AssertLintKind::ArithmeticOverflow,
|
||||||
source_info.span,
|
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
|
||||||
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -529,11 +531,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
|
||||||
// Need proper const propagator for these.
|
// Need proper const propagator for these.
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
let source_info = self.body.source_info(location);
|
self.report_assert_as_lint(location, AssertLintKind::UnconditionalPanic, msg);
|
||||||
self.report_assert_as_lint(
|
|
||||||
source_info,
|
|
||||||
AssertLint::UnconditionalPanic(source_info.span, msg),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|
|
@ -201,45 +201,39 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum AssertLint<P> {
|
pub(crate) struct AssertLint<P> {
|
||||||
ArithmeticOverflow(Span, AssertKind<P>),
|
pub span: Span,
|
||||||
UnconditionalPanic(Span, AssertKind<P>),
|
pub assert_kind: AssertKind<P>,
|
||||||
|
pub lint_kind: AssertLintKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) enum AssertLintKind {
|
||||||
|
ArithmeticOverflow,
|
||||||
|
UnconditionalPanic,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, P: std::fmt::Debug> DecorateLint<'a, ()> for AssertLint<P> {
|
impl<'a, P: std::fmt::Debug> DecorateLint<'a, ()> for AssertLint<P> {
|
||||||
fn decorate_lint<'b>(self, diag: &'b mut DiagnosticBuilder<'a, ()>) {
|
fn decorate_lint<'b>(self, diag: &'b mut DiagnosticBuilder<'a, ()>) {
|
||||||
let span = self.span();
|
let message = self.assert_kind.diagnostic_message();
|
||||||
let assert_kind = self.panic();
|
self.assert_kind.add_args(&mut |name, value| {
|
||||||
let message = assert_kind.diagnostic_message();
|
|
||||||
assert_kind.add_args(&mut |name, value| {
|
|
||||||
diag.arg(name, value);
|
diag.arg(name, value);
|
||||||
});
|
});
|
||||||
diag.span_label(span, message);
|
diag.span_label(self.span, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn msg(&self) -> DiagnosticMessage {
|
fn msg(&self) -> DiagnosticMessage {
|
||||||
match self {
|
match self.lint_kind {
|
||||||
AssertLint::ArithmeticOverflow(..) => fluent::mir_transform_arithmetic_overflow,
|
AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
|
||||||
AssertLint::UnconditionalPanic(..) => fluent::mir_transform_operation_will_panic,
|
AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P> AssertLint<P> {
|
impl AssertLintKind {
|
||||||
pub fn lint(&self) -> &'static Lint {
|
pub fn lint(&self) -> &'static Lint {
|
||||||
match self {
|
match self {
|
||||||
AssertLint::ArithmeticOverflow(..) => lint::builtin::ARITHMETIC_OVERFLOW,
|
AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
|
||||||
AssertLint::UnconditionalPanic(..) => lint::builtin::UNCONDITIONAL_PANIC,
|
AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn span(&self) -> Span {
|
|
||||||
match self {
|
|
||||||
AssertLint::ArithmeticOverflow(sp, _) | AssertLint::UnconditionalPanic(sp, _) => *sp,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn panic(self) -> AssertKind<P> {
|
|
||||||
match self {
|
|
||||||
AssertLint::ArithmeticOverflow(_, p) | AssertLint::UnconditionalPanic(_, p) => p,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue