Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make broken MIR a proper lint. #119260

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ fn register_internals(store: &mut LintStore) {
"rustc::internal",
None,
vec![
LintId::of(BROKEN_MIR),
LintId::of(DEFAULT_HASH_TYPES),
LintId::of(POTENTIAL_QUERY_INSTABILITY),
LintId::of(USAGE_OF_TY_TYKIND),
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3955,6 +3955,15 @@ declare_lint! {
"`break` expression with label and unlabeled loop as value expression"
}

crate::declare_tool_lint! {
/// The `broken_mir` statically detects undefined behaviour in the MIR optimization pipeline.
/// This is an internal lint, and not intended to be used directly.
pub rustc::BROKEN_MIR,
Deny,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forbid maybe?

"broken MIR",
report_in_external_macro: true
}
Comment on lines +3958 to +3965
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bike-shedding: The term broken MIR is already used when validation fails. It also overstates the severity of what is being reported. Maybe unusual MIR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe broken is the right term here


declare_lint! {
/// The `non_exhaustive_omitted_patterns` lint aims to help consumers of a `#[non_exhaustive]`
/// struct or enum who want to match all of its fields/variants explicitly.
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_mir_transform/src/lint.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! This pass statically detects code which has undefined behaviour or is likely to be erroneous.
//! It can be used to locate problems in MIR building or optimizations. It assumes that all code
//! can be executed, so it has false positives.

use rustc_hir::{HirId, CRATE_HIR_ID};
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_mir_dataflow::impls::{MaybeStorageDead, MaybeStorageLive};
use rustc_mir_dataflow::storage::always_storage_live_locals;
use rustc_mir_dataflow::{Analysis, ResultsCursor};
use rustc_session::lint::builtin::BROKEN_MIR;
use std::borrow::Cow;

pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
Expand All @@ -28,6 +31,11 @@ pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
tcx,
when,
body,
lint_id: body
.source
.def_id()
.as_local()
.map_or(CRATE_HIR_ID, |def_id| tcx.local_def_id_to_hir_id(def_id)),
is_fn_like: tcx.def_kind(body.source.def_id()).is_fn_like(),
always_live_locals,
reachable_blocks,
Expand All @@ -41,6 +49,7 @@ struct Lint<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
when: String,
body: &'a Body<'tcx>,
lint_id: HirId,
is_fn_like: bool,
always_live_locals: &'a BitSet<Local>,
reachable_blocks: BitSet<BasicBlock>,
Expand All @@ -49,10 +58,13 @@ struct Lint<'a, 'tcx> {
}

impl<'a, 'tcx> Lint<'a, 'tcx> {
#[track_caller]
#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
fn fail(&self, location: Location, msg: impl AsRef<str>) {
let span = self.body.source_info(location).span;
self.tcx.sess.dcx().span_delayed_bug(
self.tcx.struct_span_lint_hir(
BROKEN_MIR,
self.lint_id,
span,
format!(
"broken MIR in {:?} ({}) at {:?}:\n{}",
Expand All @@ -61,6 +73,7 @@ impl<'a, 'tcx> Lint<'a, 'tcx> {
location,
msg.as_ref()
),
|_| {},
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/mir-opt/reference_prop.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// compile-flags: -Zlint-mir=no
// unit-test: ReferencePropagation
// needs-unwind

#![feature(raw_ref_op)]
#![feature(raw_ref_op, lint_reasons)]
#![feature(core_intrinsics, custom_mir)]

#[inline(never)]
Expand Down Expand Up @@ -680,6 +679,7 @@ fn read_through_raw(x: &mut usize) -> usize {
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
#[expect(rustc::broken_mir)]
fn multiple_storage() {
// CHECK-LABEL: multiple_storage
// CHECK: _3 = (*_2);
Expand All @@ -706,6 +706,7 @@ fn multiple_storage() {
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
#[expect(rustc::broken_mir)]
fn dominate_storage() {
// CHECK-LABEL: dominate_storage
// CHECK: _5 = (*_2);
Expand Down Expand Up @@ -736,6 +737,7 @@ fn dominate_storage() {
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
#[expect(rustc::broken_mir)]
fn maybe_dead(m: bool) {
// CHECK-LABEL: fn maybe_dead(
// CHECK: (*_5) = const 7_i32;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/mir/lint/storage-live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn multiple_storage() {
{
StorageLive(a);
StorageLive(a);
//~^ ERROR broken MIR
Return()
}
)
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/mir/lint/storage-live.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: internal compiler error: broken MIR in Item(DefId(0:8 ~ storage_live[HASH]::multiple_storage)) (after pass CheckPackedRef) at bb0[1]:
StorageLive(_1) which already has storage here
error: broken MIR in Item(DefId(0:8 ~ storage_live[HASH]::multiple_storage)) (after pass CheckPackedRef) at bb0[1]:
StorageLive(_1) which already has storage here
--> $DIR/storage-live.rs:22:13
|
LL | StorageLive(a);
| ^^^^^^^^^^^^^^

aborting due to `-Z treat-err-as-bug=1`
|
= aborting due to `-Z treat-err-as-bug=1`
error: the compiler unexpectedly panicked. this is a bug.

query stack during panic:
Expand Down