Skip to content

Commit

Permalink
Unrolled build for rust-lang#121286
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#121286 - gurry:constprop-lint-rename, r=oli-obk

Rename `ConstPropLint` to `KnownPanicsLint`

`OverflowLint` is a clearer name because it communicates what the lint does instead of the underlying mechanism it uses (const propagation) which should be of secondary concern.

`OverflowLint` isn't the most accurate name because the lint looks for other errors as well such as division by zero not just overflows, but I couldn't think of another equally succinct name.

As a part of this change. I've also added/updated some of the comments.

cc ```@RalfJung``` ```@oli-obk``` for visibility in case you go looking for the lint using the old name.

Edit:

Changed the name from `OverflowLint` to `KnownPanicsLint`
  • Loading branch information
rust-timer committed Feb 20, 2024
2 parents 2b43e75 + 42c4df0 commit 52959b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Propagates constants for early reporting of statically known
//! assertion failures
//! A lint that checks for known panics like
//! overflows, division by zero,
//! out-of-bound access etc.
//! Uses const propagation to determine the
//! values of operands during checks.

use std::fmt::Debug;

Expand All @@ -21,9 +24,9 @@ use crate::dataflow_const_prop::DummyMachine;
use crate::errors::{AssertLint, AssertLintKind};
use crate::MirLint;

pub struct ConstPropLint;
pub struct KnownPanicsLint;

impl<'tcx> MirLint<'tcx> for ConstPropLint {
impl<'tcx> MirLint<'tcx> for KnownPanicsLint {
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
if body.tainted_by_errors.is_some() {
return;
Expand All @@ -37,31 +40,28 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
// Only run const prop on functions, methods, closures and associated constants
if !is_fn_like && !is_assoc_const {
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
trace!("ConstPropLint skipped for {:?}", def_id);
trace!("KnownPanicsLint skipped for {:?}", def_id);
return;
}

// FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
// computing their layout.
if tcx.is_coroutine(def_id.to_def_id()) {
trace!("ConstPropLint skipped for coroutine {:?}", def_id);
trace!("KnownPanicsLint skipped for coroutine {:?}", def_id);
return;
}

trace!("ConstPropLint starting for {:?}", def_id);
trace!("KnownPanicsLint starting for {:?}", def_id);

// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
// constants, instead of just checking for const-folding succeeding.
// That would require a uniform one-def no-mutation analysis
// and RPO (or recursing when needing the value of a local).
let mut linter = ConstPropagator::new(body, tcx);
linter.visit_body(body);

trace!("ConstPropLint done for {:?}", def_id);
trace!("KnownPanicsLint done for {:?}", def_id);
}
}

/// Finds optimization opportunities on the MIR.
/// Visits MIR nodes, performs const propagation
/// and runs lint checks as it goes
struct ConstPropagator<'mir, 'tcx> {
ecx: InterpCx<'mir, 'tcx, DummyMachine>,
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
// dedicated error variants should be introduced instead.
assert!(
!error.kind().formatted_string(),
"const-prop encountered formatting error: {}",
"known panics lint encountered formatting error: {}",
format_interp_error(self.ecx.tcx.dcx(), error),
);
None
Expand All @@ -253,7 +253,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
return None;
}

// Normalization needed b/c const prop lint runs in
// Normalization needed b/c known panics lint runs in
// `mir_drops_elaborated_and_const_checked`, which happens before
// optimized MIR. Only after optimizing the MIR can we guarantee
// that the `RevealAll` pass has happened and that the body's consts
Expand Down Expand Up @@ -864,6 +864,8 @@ pub enum ConstPropMode {
NoPropagation,
}

/// A visitor that determines locals in a MIR body
/// that can be const propagated
pub struct CanConstProp {
can_const_prop: IndexVec<Local, ConstPropMode>,
// False at the beginning. Once set, no more assignments are allowed to that local.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ mod remove_place_mention;
mod add_subtyping_projections;
pub mod cleanup_post_borrowck;
mod const_debuginfo;
mod const_prop_lint;
mod copy_prop;
mod coroutine;
mod cost_checker;
Expand All @@ -83,6 +82,7 @@ mod gvn;
pub mod inline;
mod instsimplify;
mod jump_threading;
mod known_panics_lint;
mod large_enums;
mod lint;
mod lower_intrinsics;
Expand Down Expand Up @@ -533,7 +533,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&elaborate_box_derefs::ElaborateBoxDerefs,
&coroutine::StateTransform,
&add_retag::AddRetag,
&Lint(const_prop_lint::ConstPropLint),
&Lint(known_panics_lint::KnownPanicsLint),
];
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
}
Expand Down

0 comments on commit 52959b1

Please sign in to comment.