Skip to content

Commit 94371d5

Browse files
committed
Validate and test -Zmir-enable-passes
1 parent 2a9cc8f commit 94371d5

9 files changed

+75
-5
lines changed

compiler/rustc_mir_transform/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ mir_transform_undefined_transmute = pointers cannot be transmuted to integers du
3434
.note = at compile-time, pointers do not have an integer value
3535
.note2 = avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
3636
.help = for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
37+
38+
mir_transform_unknown_pass_name = MIR pass `{$name}` is unknown and will be ignored

compiler/rustc_mir_transform/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ pub(crate) struct UnalignedPackedRef {
3838
pub span: Span,
3939
}
4040

41+
#[derive(Diagnostic)]
42+
#[diag(mir_transform_unknown_pass_name)]
43+
pub(crate) struct UnknownPassName<'a> {
44+
pub(crate) name: &'a str,
45+
}
46+
4147
pub(crate) struct AssertLint<P> {
4248
pub span: Span,
4349
pub assert_kind: AssertKind<P>,

compiler/rustc_mir_transform/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ macro_rules! declare_passes {
9090
)+
9191
)*
9292

93-
#[cfg(debug_assertions)]
9493
static PASS_NAMES: LazyLock<Vec<String>> = LazyLock::new(|| vec![
9594
// Fake marker pass
9695
"PreCodegen".to_string(),

compiler/rustc_mir_transform/src/pass_manager.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_session::Session;
88
use tracing::trace;
99

1010
use crate::lint::lint_body;
11-
use crate::validate;
11+
use crate::{errors, validate};
1212

1313
thread_local! {
1414
static PASS_NAMES: RefCell<FxHashMap<&'static str, &'static str>> = {
@@ -198,13 +198,29 @@ fn run_passes_inner<'tcx>(
198198
let overridden_passes = &tcx.sess.opts.unstable_opts.mir_enable_passes;
199199
trace!(?overridden_passes);
200200

201+
let named_passes: FxIndexSet<_> =
202+
overridden_passes.iter().map(|(name, _)| name.as_str()).collect();
203+
let known_passes: FxIndexSet<_> = crate::PASS_NAMES.iter().map(|p| p.as_str()).collect();
204+
205+
for &name in named_passes.difference(&known_passes) {
206+
tcx.dcx().emit_warn(errors::UnknownPassName { name });
207+
}
208+
209+
// Verify that no passes are missing from the `declare_passes` invocation
201210
#[cfg(debug_assertions)]
211+
#[allow(rustc::diagnostic_outside_of_impl)]
212+
#[allow(rustc::untranslatable_diagnostic)]
202213
{
203214
let used_passes: FxIndexSet<_> = passes.iter().map(|p| p.name()).collect();
204-
let known_passes: FxIndexSet<_> = crate::PASS_NAMES.iter().map(|p| p.as_str()).collect();
205215

206-
for &name in used_passes.difference(&known_passes) {
207-
tcx.dcx().bug(format!("pass `{name}` is not declared in `PASS_NAMES`"));
216+
let undeclared = used_passes.difference(&known_passes).collect::<Vec<_>>();
217+
if let Some((name, rest)) = undeclared.split_first() {
218+
let mut err =
219+
tcx.dcx().struct_bug(format!("pass `{name}` is not declared in `PASS_NAMES`"));
220+
for name in rest {
221+
err.note(format!("pass `{name}` is also not declared in `PASS_NAMES`"));
222+
}
223+
err.emit();
208224
}
209225
}
210226

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: MIR pass `ThisPass` is unknown and will be ignored
2+
3+
warning: MIR pass `DoesNotExist` is unknown and will be ignored
4+
5+
warning: MIR pass `ThisPass` is unknown and will be ignored
6+
|
7+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
8+
9+
warning: MIR pass `DoesNotExist` is unknown and will be ignored
10+
|
11+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
12+
13+
warning: 4 warnings emitted
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: incorrect value `` for unstable option `mir-enable-passes` - a comma-separated list of strings, with elements beginning with + or - was expected
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: MIR pass `ThisPassDoesNotExist` is unknown and will be ignored
2+
3+
warning: MIR pass `ThisPassDoesNotExist` is unknown and will be ignored
4+
|
5+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
6+
7+
warning: 2 warnings emitted
8+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ revisions: empty unprefixed all_unknown all_known mixed
2+
3+
//@[empty] compile-flags: -Zmir-enable-passes=
4+
//@[empty] error-pattern error: incorrect value `` for unstable option `mir-enable-passes` - a comma-separated list of strings, with elements beginning with + or - was expected
5+
6+
//@[unprefixed] compile-flags: -Zmir-enable-passes=CheckAlignment
7+
//@[unprefixed] error-pattern error: incorrect value `CheckAlignment` for unstable option `mir-enable-passes` - a comma-separated list of strings, with elements beginning with + or - was expected
8+
9+
//@[all_unknown] check-pass
10+
//@[all_unknown] compile-flags: -Zmir-enable-passes=+ThisPass,-DoesNotExist
11+
//@[all_unknown] error-pattern: warning: MIR pass `ThisPass` is unknown and will be ignored
12+
//@[all_unknown] error-pattern: warning: MIR pass `DoesNotExist` is unknown and will be ignored
13+
14+
//@[all_known] check-pass
15+
//@[all_known] compile-flags: -Zmir-enable-passes=+CheckAlignment,+LowerIntrinsics
16+
17+
//@[mixed] check-pass
18+
//@[mixed] compile-flags: -Zmir-enable-passes=+ThisPassDoesNotExist,+CheckAlignment
19+
//@[mixed] error-pattern: warning: MIR pass `ThisPassDoesNotExist` is unknown and will be ignored
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: incorrect value `CheckAlignment` for unstable option `mir-enable-passes` - a comma-separated list of strings, with elements beginning with + or - was expected
2+

0 commit comments

Comments
 (0)