Skip to content

Commit 44d699f

Browse files
committed
Avoid some packing/unpacking of the AssertLint enum
1 parent 4ca5201 commit 44d699f

File tree

2 files changed

+41
-49
lines changed

2 files changed

+41
-49
lines changed

compiler/rustc_mir_transform/src/const_prop_lint.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_target::abi::{Abi, FieldIdx, HasDataLayout, Size, TargetDataLayout, Va
2020
use crate::const_prop::CanConstProp;
2121
use crate::const_prop::ConstPropMachine;
2222
use crate::const_prop::ConstPropMode;
23-
use crate::errors::AssertLint;
23+
use crate::errors::{AssertLint, AssertLintKind};
2424
use crate::MirLint;
2525

2626
pub struct ConstPropLint;
@@ -300,9 +300,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
300300
}
301301
}
302302

303-
fn report_assert_as_lint(&self, source_info: &SourceInfo, lint: AssertLint<impl Debug>) {
303+
fn report_assert_as_lint(
304+
&self,
305+
location: Location,
306+
lint_kind: AssertLintKind,
307+
assert_kind: AssertKind<impl Debug>,
308+
) {
309+
let source_info = self.body.source_info(location);
304310
if let Some(lint_root) = self.lint_root(*source_info) {
305-
self.tcx.emit_spanned_lint(lint.lint(), lint_root, source_info.span, lint);
311+
let span = source_info.span;
312+
self.tcx.emit_spanned_lint(
313+
lint_kind.lint(),
314+
lint_root,
315+
span,
316+
AssertLint { span, assert_kind, lint_kind },
317+
);
306318
}
307319
}
308320

@@ -316,13 +328,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
316328
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
317329
// appropriate to use.
318330
assert_eq!(op, UnOp::Neg, "Neg is the only UnOp that can overflow");
319-
let source_info = self.body.source_info(location);
320331
self.report_assert_as_lint(
321-
source_info,
322-
AssertLint::ArithmeticOverflow(
323-
source_info.span,
324-
AssertKind::OverflowNeg(val.to_const_int()),
325-
),
332+
location,
333+
AssertLintKind::ArithmeticOverflow,
334+
AssertKind::OverflowNeg(val.to_const_int()),
326335
);
327336
return None;
328337
}
@@ -354,7 +363,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
354363
let r_bits = r.to_scalar().to_bits(right_size).ok();
355364
if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
356365
debug!("check_binary_op: reporting assert for {:?}", location);
357-
let source_info = self.body.source_info(location);
358366
let panic = AssertKind::Overflow(
359367
op,
360368
match l {
@@ -368,10 +376,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
368376
},
369377
r.to_const_int(),
370378
);
371-
self.report_assert_as_lint(
372-
source_info,
373-
AssertLint::ArithmeticOverflow(source_info.span, panic),
374-
);
379+
self.report_assert_as_lint(location, AssertLintKind::ArithmeticOverflow, panic);
375380
return None;
376381
}
377382
}
@@ -382,13 +387,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
382387
let (_res, overflow) = this.ecx.overflowing_binary_op(op, &l, &r)?;
383388
Ok(overflow)
384389
})? {
385-
let source_info = self.body.source_info(location);
386390
self.report_assert_as_lint(
387-
source_info,
388-
AssertLint::ArithmeticOverflow(
389-
source_info.span,
390-
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
391-
),
391+
location,
392+
AssertLintKind::ArithmeticOverflow,
393+
AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
392394
);
393395
return None;
394396
}
@@ -529,11 +531,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
529531
// Need proper const propagator for these.
530532
_ => return None,
531533
};
532-
let source_info = self.body.source_info(location);
533-
self.report_assert_as_lint(
534-
source_info,
535-
AssertLint::UnconditionalPanic(source_info.span, msg),
536-
);
534+
self.report_assert_as_lint(location, AssertLintKind::UnconditionalPanic, msg);
537535
}
538536

539537
None

compiler/rustc_mir_transform/src/errors.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -201,45 +201,39 @@ impl<'a> DecorateLint<'a, ()> for UnsafeOpInUnsafeFn {
201201
}
202202
}
203203

204-
pub(crate) enum AssertLint<P> {
205-
ArithmeticOverflow(Span, AssertKind<P>),
206-
UnconditionalPanic(Span, AssertKind<P>),
204+
pub(crate) struct AssertLint<P> {
205+
pub span: Span,
206+
pub assert_kind: AssertKind<P>,
207+
pub lint_kind: AssertLintKind,
208+
}
209+
210+
pub(crate) enum AssertLintKind {
211+
ArithmeticOverflow,
212+
UnconditionalPanic,
207213
}
208214

209215
impl<'a, P: std::fmt::Debug> DecorateLint<'a, ()> for AssertLint<P> {
210216
fn decorate_lint<'b>(self, diag: &'b mut DiagnosticBuilder<'a, ()>) {
211-
let span = self.span();
212-
let assert_kind = self.panic();
213-
let message = assert_kind.diagnostic_message();
214-
assert_kind.add_args(&mut |name, value| {
217+
let message = self.assert_kind.diagnostic_message();
218+
self.assert_kind.add_args(&mut |name, value| {
215219
diag.arg(name, value);
216220
});
217-
diag.span_label(span, message);
221+
diag.span_label(self.span, message);
218222
}
219223

220224
fn msg(&self) -> DiagnosticMessage {
221-
match self {
222-
AssertLint::ArithmeticOverflow(..) => fluent::mir_transform_arithmetic_overflow,
223-
AssertLint::UnconditionalPanic(..) => fluent::mir_transform_operation_will_panic,
225+
match self.lint_kind {
226+
AssertLintKind::ArithmeticOverflow => fluent::mir_transform_arithmetic_overflow,
227+
AssertLintKind::UnconditionalPanic => fluent::mir_transform_operation_will_panic,
224228
}
225229
}
226230
}
227231

228-
impl<P> AssertLint<P> {
232+
impl AssertLintKind {
229233
pub fn lint(&self) -> &'static Lint {
230234
match self {
231-
AssertLint::ArithmeticOverflow(..) => lint::builtin::ARITHMETIC_OVERFLOW,
232-
AssertLint::UnconditionalPanic(..) => lint::builtin::UNCONDITIONAL_PANIC,
233-
}
234-
}
235-
pub fn span(&self) -> Span {
236-
match self {
237-
AssertLint::ArithmeticOverflow(sp, _) | AssertLint::UnconditionalPanic(sp, _) => *sp,
238-
}
239-
}
240-
pub fn panic(self) -> AssertKind<P> {
241-
match self {
242-
AssertLint::ArithmeticOverflow(_, p) | AssertLint::UnconditionalPanic(_, p) => p,
235+
AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,
236+
AssertLintKind::UnconditionalPanic => lint::builtin::UNCONDITIONAL_PANIC,
243237
}
244238
}
245239
}

0 commit comments

Comments
 (0)