Skip to content

Commit 36f4f4a

Browse files
committed
Auto merge of rust-lang#99780 - Nilstrieb:mir-opt-test-line-no, r=oli-obk
Use line numbers relative to the function in mir-opt tests As shown in rust-lang#99770, the line numbers can be a big source of needless and confusing diffs. This PR adds a new flag `-Zmir-pretty-relative-line-numbers` to make them relative to the function declaration, which avoids most needless diffs from attribute changes. `@JakobDegen` told me that there has been a zulip conversation about disabling line numbers with mixed opinions, so I'd like to get some feedback here, for this hopefully better solution. r? rust-lang/wg-mir-opt
2 parents e568261 + 11c0280 commit 36f4f4a

File tree

337 files changed

+9956
-9918
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

337 files changed

+9956
-9918
lines changed

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);

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

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)"),

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.

src/test/mir-opt/76803_regression.encode.SimplifyBranchSame.diff

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
+ // MIR for `encode` after SimplifyBranchSame
33

44
fn encode(_1: Type) -> Type {
5-
debug v => _1; // in scope 0 at $DIR/76803_regression.rs:10:15: 10:16
6-
let mut _0: Type; // return place in scope 0 at $DIR/76803_regression.rs:10:27: 10:31
7-
let mut _2: isize; // in scope 0 at $DIR/76803_regression.rs:12:9: 12:16
5+
debug v => _1; // in scope 0 at $DIR/76803_regression.rs:+0:15: +0:16
6+
let mut _0: Type; // return place in scope 0 at $DIR/76803_regression.rs:+0:27: +0:31
7+
let mut _2: isize; // in scope 0 at $DIR/76803_regression.rs:+2:9: +2:16
88

99
bb0: {
10-
_2 = discriminant(_1); // scope 0 at $DIR/76803_regression.rs:11:11: 11:12
11-
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:11:5: 11:12
10+
_2 = discriminant(_1); // scope 0 at $DIR/76803_regression.rs:+1:11: +1:12
11+
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:+1:5: +1:12
1212
}
1313

1414
bb1: {
15-
_0 = move _1; // scope 0 at $DIR/76803_regression.rs:13:14: 13:15
16-
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:13:14: 13:15
15+
_0 = move _1; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
16+
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:+3:14: +3:15
1717
}
1818

1919
bb2: {
20-
Deinit(_0); // scope 0 at $DIR/76803_regression.rs:12:20: 12:27
21-
discriminant(_0) = 1; // scope 0 at $DIR/76803_regression.rs:12:20: 12:27
22-
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:12:20: 12:27
20+
Deinit(_0); // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
21+
discriminant(_0) = 1; // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
22+
goto -> bb3; // scope 0 at $DIR/76803_regression.rs:+2:20: +2:27
2323
}
2424

2525
bb3: {
26-
return; // scope 0 at $DIR/76803_regression.rs:15:2: 15:2
26+
return; // scope 0 at $DIR/76803_regression.rs:+5:2: +5:2
2727
}
2828
}
2929

0 commit comments

Comments
 (0)