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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -597,6 +597,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 @@ -3933,6 +3933,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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One use case is to use expect. And you cannot expect a forbidden lint.

"broken MIR",
report_in_external_macro: true
}
Comment on lines +3936 to +3943
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

Copy link
Contributor

Choose a reason for hiding this comment

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

In what sense is this MIR broken?

The motivation behind moving those checks out of validator was the fact that there is nothing inherently wrong about violating them. Those reports should NOT be treated as bugs. It doesn't even mean that the user provided program has an undefined behaviour, since we don't know whether those instructions are ever executed.


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
20 changes: 14 additions & 6 deletions compiler/rustc_mir_transform/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use std::borrow::Cow;

use rustc_data_structures::fx::FxHashSet;
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;

pub fn lint_body<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, when: String) {
let always_live_locals = &always_storage_live_locals(body);
Expand All @@ -30,6 +32,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,
maybe_storage_live,
Expand All @@ -45,6 +52,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>,
maybe_storage_live: ResultsCursor<'a, 'tcx, MaybeStorageLive<'a>>,
Expand All @@ -53,19 +61,19 @@ 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(
span,
format!(
self.tcx.node_span_lint(BROKEN_MIR, self.lint_id, span, |lint| {
lint.primary_message(format!(
"broken MIR in {:?} ({}) at {:?}:\n{}",
self.body.source.instance,
self.when,
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
//@ test-mir-pass: ReferencePropagation
//@ needs-unwind

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

#[inline(never)]
Expand Down Expand Up @@ -678,6 +677,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 @@ -704,6 +704,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 @@ -734,6 +735,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 @@ -21,6 +21,7 @@ fn multiple_storage() {
{
StorageLive(a);
StorageLive(a);
//~^ ERROR broken MIR
Return()
}
}
Expand Down
10 changes: 3 additions & 7 deletions tests/ui/mir/lint/storage-live.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ error: internal compiler error: broken MIR in Item(DefId(0:8 ~ storage_live[HASH
LL | StorageLive(a);
| ^^^^^^^^^^^^^^
|
note: delayed at compiler/rustc_mir_transform/src/lint.rs:LL:CC - disabled backtrace
--> $DIR/storage-live.rs:23: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:
#0 [mir_built] building MIR for `multiple_storage`
#1 [has_ffi_unwind_calls] checking if `multiple_storage` contains FFI-unwind calls
end of query stack
Loading