Skip to content

Commit

Permalink
Auto merge of rust-lang#112346 - saethlin:no-comment, r=oli-obk
Browse files Browse the repository at this point in the history
Remove comments from mir-opt MIR dumps

See https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Line.20numbers.20in.20mir-opt.20tests/near/363849874

In rust-lang#99780 there is mention that "there has been a zulip conversation about disabling line numbers with mixed opinions" which to me means that some people opposed this. I can't find the referenced conversation so... here we go.

The current situation is quite chaotic. It's not hard to find MIR diffs which contain

* Absolute line numbers
* Relative line numbers
* Substituted line numbers (LL)
For example: https://github.com/rust-lang/rust/blob/408bbd040613f6776e0a7d05d582c81f4ddc189a/tests/mir-opt/inline/inline_shims.drop.Inline.diff#L10-L17

And sometimes adding a comment at the top of a mir-opt test generates a diff in the test because a line number changed: https://github.com/rust-lang/rust/pull/98112/files#diff-b8cf4bcce95078e6a3faf075e9abf6864872fb28a64d95c04f04513b9e3bbd81

And irrelevant changes to the standard library can generate diffs in mir-opt tests: https://github.com/rust-lang/rust/pull/110694/files#diff-bf96b0e7c67b8b272814536888fd9428c314991e155beae1f0a2a67f0ac47b2c
rust-lang@769886c

I think we should, specifically in mir-opt tests, completely remove the comments, or insert placeholders for all line and column numbers.
  • Loading branch information
bors committed Jun 16, 2023
2 parents 0252b40 + 0a1fa41 commit c84d5e7
Show file tree
Hide file tree
Showing 701 changed files with 21,875 additions and 24,144 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ fn test_unstable_options_tracking_hash() {
untracked!(ls, true);
untracked!(macro_backtrace, true);
untracked!(meta_stats, true);
untracked!(mir_pretty_relative_line_numbers, true);
untracked!(mir_include_spans, true);
untracked!(nll_facts, true);
untracked!(no_analysis, true);
untracked!(no_leak_check, true);
Expand Down
121 changes: 71 additions & 50 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,22 @@ where
for statement in &data.statements {
extra_data(PassWhere::BeforeLocation(current_location), w)?;
let indented_body = format!("{0}{0}{1:?};", INDENT, statement);
writeln!(
w,
"{:A$} // {}{}",
indented_body,
if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() },
comment(tcx, statement.source_info, body.span),
A = ALIGN,
)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
writeln!(
w,
"{:A$} // {}{}",
indented_body,
if tcx.sess.verbose() {
format!("{:?}: ", current_location)
} else {
String::new()
},
comment(tcx, statement.source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{}", indented_body)?;
}

write_extra(tcx, w, |visitor| {
visitor.visit_statement(statement, current_location);
Expand All @@ -374,14 +382,18 @@ where
// Terminator at the bottom.
extra_data(PassWhere::BeforeLocation(current_location), w)?;
let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
writeln!(
w,
"{:A$} // {}{}",
indented_terminator,
if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() },
comment(tcx, data.terminator().source_info, body.span),
A = ALIGN,
)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
writeln!(
w,
"{:A$} // {}{}",
indented_terminator,
if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() },
comment(tcx, data.terminator().source_info),
A = ALIGN,
)?;
} else {
writeln!(w, "{}", indented_terminator)?;
}

write_extra(tcx, w, |visitor| {
visitor.visit_terminator(data.terminator(), current_location);
Expand All @@ -400,10 +412,12 @@ fn write_extra<'tcx, F>(tcx: TyCtxt<'tcx>, write: &mut dyn Write, mut visit_op:
where
F: FnMut(&mut ExtraComments<'tcx>),
{
let mut extra_comments = ExtraComments { tcx, comments: vec![] };
visit_op(&mut extra_comments);
for comment in extra_comments.comments {
writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
let mut extra_comments = ExtraComments { tcx, comments: vec![] };
visit_op(&mut extra_comments);
for comment in extra_comments.comments {
writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -522,13 +536,8 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
}
}

fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo, function_span: Span) -> String {
let location = if tcx.sess.opts.unstable_opts.mir_pretty_relative_line_numbers {
tcx.sess.source_map().span_to_relative_line_string(span, function_span)
} else {
tcx.sess.source_map().span_to_embeddable_string(span)
};

fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String {
let location = tcx.sess.source_map().span_to_embeddable_string(span);
format!("scope {} at {}", scope.index(), location,)
}

Expand Down Expand Up @@ -560,13 +569,17 @@ fn write_scope_tree(
var_debug_info.value,
);

writeln!(
w,
"{0:1$} // in {2}",
indented_debug_info,
ALIGN,
comment(tcx, var_debug_info.source_info, body.span),
)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
writeln!(
w,
"{0:1$} // in {2}",
indented_debug_info,
ALIGN,
comment(tcx, var_debug_info.source_info),
)?;
} else {
writeln!(w, "{}", indented_debug_info)?;
}
}

// Local variable types.
Expand Down Expand Up @@ -594,14 +607,18 @@ fn write_scope_tree(

let local_name = if local == RETURN_PLACE { " return place" } else { "" };

writeln!(
w,
"{0:1$} //{2} in {3}",
indented_decl,
ALIGN,
local_name,
comment(tcx, local_decl.source_info, body.span),
)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
writeln!(
w,
"{0:1$} //{2} in {3}",
indented_decl,
ALIGN,
local_name,
comment(tcx, local_decl.source_info),
)?;
} else {
writeln!(w, "{}", indented_decl,)?;
}
}

let Some(children) = scope_tree.get(&parent) else {
Expand All @@ -627,14 +644,18 @@ fn write_scope_tree(

let indented_header = format!("{0:1$}scope {2}{3} {{", "", indent, child.index(), special);

if let Some(span) = span {
writeln!(
w,
"{0:1$} // at {2}",
indented_header,
ALIGN,
tcx.sess.source_map().span_to_embeddable_string(span),
)?;
if tcx.sess.opts.unstable_opts.mir_include_spans {
if let Some(span) = span {
writeln!(
w,
"{0:1$} // at {2}",
indented_header,
ALIGN,
tcx.sess.source_map().span_to_embeddable_string(span),
)?;
} else {
writeln!(w, "{}", indented_header)?;
}
} else {
writeln!(w, "{}", indented_header)?;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,14 +1558,14 @@ options! {
"use like `-Zmir-enable-passes=+DestinationPropagation,-InstSimplify`. Forces the specified passes to be \
enabled, overriding all other checks. Passes that are not specified are enabled or \
disabled by other flags as usual."),
mir_include_spans: bool = (false, parse_bool, [UNTRACKED],
"use line numbers relative to the function in mir pretty printing"),
mir_keep_place_mention: bool = (false, parse_bool, [TRACKED],
"keep place mention MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \
(default: no)"),
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
mir_pretty_relative_line_numbers: bool = (false, parse_bool, [UNTRACKED],
"use line numbers relative to the function in mir pretty printing"),
move_size_limit: Option<usize> = (None, parse_opt_number, [TRACKED],
"the size at which the `large_assignments` lint starts to be emitted"),
mutable_noalias: bool = (true, parse_bool, [TRACKED],
Expand Down
1 change: 0 additions & 1 deletion src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,6 @@ impl<'test> TestCx<'test> {
&zdump_arg,
"-Zvalidate-mir",
"-Zdump-mir-exclude-pass-number",
"-Zmir-pretty-relative-line-numbers=yes",
]);
if let Some(pass) = &self.props.mir_unit_test {
rustc.args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]);
Expand Down
Loading

0 comments on commit c84d5e7

Please sign in to comment.