Skip to content

Commit 7cf7ead

Browse files
committed
Use line numbers relative to function in mir opt tests
This adds a new option, `-Zmir-pretty-relative-line-numbers`, that is then used in compiletest for the mir-opt tests.
1 parent 2643b16 commit 7cf7ead

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

Diff for: compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ fn test_unstable_options_tracking_hash() {
670670
untracked!(ls, true);
671671
untracked!(macro_backtrace, true);
672672
untracked!(meta_stats, true);
673+
untracked!(mir_pretty_relative_line_numbers, true);
673674
untracked!(nll_facts, true);
674675
untracked!(no_analysis, true);
675676
untracked!(no_interleave_lints, true);

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ where
360360
"{:A$} // {}{}",
361361
indented_body,
362362
if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() },
363-
comment(tcx, statement.source_info),
363+
comment(tcx, statement.source_info, body.span),
364364
A = ALIGN,
365365
)?;
366366

@@ -381,7 +381,7 @@ where
381381
"{:A$} // {}{}",
382382
indented_terminator,
383383
if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() },
384-
comment(tcx, data.terminator().source_info),
384+
comment(tcx, data.terminator().source_info, body.span),
385385
A = ALIGN,
386386
)?;
387387

@@ -518,8 +518,14 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
518518
}
519519
}
520520

521-
fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String {
522-
format!("scope {} at {}", scope.index(), tcx.sess.source_map().span_to_embeddable_string(span))
521+
fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo, function_span: Span) -> String {
522+
let location = if tcx.sess.opts.unstable_opts.mir_pretty_relative_line_numbers {
523+
tcx.sess.source_map().span_to_relative_line_string(span, function_span)
524+
} else {
525+
tcx.sess.source_map().span_to_embeddable_string(span)
526+
};
527+
528+
format!("scope {} at {}", scope.index(), location,)
523529
}
524530

525531
/// Prints local variables in a scope tree.
@@ -550,7 +556,7 @@ fn write_scope_tree(
550556
"{0:1$} // in {2}",
551557
indented_debug_info,
552558
ALIGN,
553-
comment(tcx, var_debug_info.source_info),
559+
comment(tcx, var_debug_info.source_info, body.span),
554560
)?;
555561
}
556562

@@ -585,7 +591,7 @@ fn write_scope_tree(
585591
indented_decl,
586592
ALIGN,
587593
local_name,
588-
comment(tcx, local_decl.source_info),
594+
comment(tcx, local_decl.source_info, body.span),
589595
)?;
590596
}
591597

Diff for: compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,8 @@ options! {
13791379
"use like `-Zmir-enable-passes=+DestProp,-InstCombine`. Forces the specified passes to be \
13801380
enabled, overriding all other checks. Passes that are not specified are enabled or \
13811381
disabled by other flags as usual."),
1382+
mir_pretty_relative_line_numbers: bool = (false, parse_bool, [UNTRACKED],
1383+
"use line numbers relative to the function in mir pretty printing"),
13821384
#[cfg_attr(not(bootstrap), rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field"))]
13831385
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
13841386
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),

Diff for: compiler/rustc_span/src/source_map.rs

+27
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,33 @@ impl SourceMap {
463463
self.span_to_string(sp, FileNameDisplayPreference::Remapped)
464464
}
465465

466+
/// Format the span location suitable for pretty printing anotations with relative line numbers
467+
pub fn span_to_relative_line_string(&self, sp: Span, relative_to: Span) -> String {
468+
if self.files.borrow().source_files.is_empty() || sp.is_dummy() || relative_to.is_dummy() {
469+
return "no-location".to_string();
470+
}
471+
472+
let lo = self.lookup_char_pos(sp.lo());
473+
let hi = self.lookup_char_pos(sp.hi());
474+
let offset = self.lookup_char_pos(relative_to.lo());
475+
476+
if lo.file.name != offset.file.name {
477+
return self.span_to_embeddable_string(sp);
478+
}
479+
480+
let lo_line = lo.line.saturating_sub(offset.line);
481+
let hi_line = hi.line.saturating_sub(offset.line);
482+
483+
format!(
484+
"{}:+{}:{}: +{}:{}",
485+
lo.file.name.display(FileNameDisplayPreference::Remapped),
486+
lo_line,
487+
lo.col.to_usize() + 1,
488+
hi_line,
489+
hi.col.to_usize() + 1,
490+
)
491+
}
492+
466493
/// Format the span location to be printed in diagnostics. Must not be emitted
467494
/// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
468495
/// for string suitable for embedding.

Diff for: src/test/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
-Z meta-stats=val -- gather metadata statistics (default: no)
7777
-Z mir-emit-retag=val -- emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 (default: no)
7878
-Z mir-enable-passes=val -- use like `-Zmir-enable-passes=+DestProp,-InstCombine`. 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.
79+
-Z mir-pretty-relative-line-numbers=val -- use line numbers relative to the function in mir pretty printing
7980
-Z mir-opt-level=val -- MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)
8081
-Z move-size-limit=val -- the size at which the `large_assignments` lint starts to be emitted
8182
-Z mutable-noalias=val -- emit noalias metadata for mutable references (default: yes)

Diff for: src/tools/compiletest/src/runtest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,7 @@ impl<'test> TestCx<'test> {
19601960
"-Zdump-mir=all",
19611961
"-Zvalidate-mir",
19621962
"-Zdump-mir-exclude-pass-number",
1963+
"-Zmir-pretty-relative-line-numbers=yes",
19631964
]);
19641965
if let Some(pass) = &self.props.mir_unit_test {
19651966
rustc.args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]);

0 commit comments

Comments
 (0)