Skip to content

Commit 2c00db8

Browse files
committed
compiletest: Add LineNo newtype to avoid +1 magic here and there
Start small and only use it in `debugger.rs` for now. If it works well we can incrementally increase usage as time passes.
1 parent b53da99 commit 2c00db8

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/tools/compiletest/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod edition;
1212
mod errors;
1313
mod executor;
1414
mod json;
15+
mod line_no;
1516
mod output_capture;
1617
mod panic_hook;
1718
mod raise_fd_limit;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// A line number in a file. Internally the first line has index 0.
2+
/// But when displayed, the first line is 1.
3+
pub struct LineNo(usize);
4+
5+
impl LineNo {
6+
pub fn from_zero_based(zero_based: usize) -> Self {
7+
LineNo(zero_based)
8+
}
9+
}
10+
11+
impl std::fmt::Display for LineNo {
12+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13+
write!(f, "{}", self.0 + 1)
14+
}
15+
}

src/tools/compiletest/src/runtest/debugger.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use std::io::{BufRead, BufReader};
44

55
use camino::{Utf8Path, Utf8PathBuf};
66

7+
use crate::line_no::LineNo;
78
use crate::runtest::ProcRes;
89

910
/// Representation of information to invoke a debugger and check its output
1011
pub(super) struct DebuggerCommands {
1112
/// Commands for the debuuger
1213
pub commands: Vec<String>,
1314
/// Lines to insert breakpoints at
14-
pub breakpoint_lines: Vec<usize>,
15+
pub breakpoint_lines: Vec<LineNo>,
1516
/// Contains the source line number to check and the line itself
16-
check_lines: Vec<(usize, String)>,
17+
check_lines: Vec<(LineNo, String)>,
1718
/// Source file name
1819
file: Utf8PathBuf,
1920
}
@@ -26,15 +27,13 @@ impl DebuggerCommands {
2627
let mut breakpoint_lines = vec![];
2728
let mut commands = vec![];
2829
let mut check_lines = vec![];
29-
let mut counter = 0;
3030
let reader = BufReader::new(File::open(file.as_std_path()).unwrap());
3131
for (line_no, line) in reader.lines().enumerate() {
32-
counter += 1;
3332
let line = line.map_err(|e| format!("Error while parsing debugger commands: {}", e))?;
3433

3534
// Breakpoints appear on lines with actual code, typically at the end of the line.
3635
if line.contains("#break") {
37-
breakpoint_lines.push(counter);
36+
breakpoint_lines.push(LineNo::from_zero_based(line_no));
3837
continue;
3938
}
4039

@@ -46,7 +45,7 @@ impl DebuggerCommands {
4645
commands.push(command);
4746
}
4847
if let Some(pattern) = parse_name_value(&line, &check_directive) {
49-
check_lines.push((line_no, pattern));
48+
check_lines.push((LineNo::from_zero_based(line_no), pattern));
5049
}
5150
}
5251

@@ -88,15 +87,14 @@ impl DebuggerCommands {
8887
);
8988

9089
for (src_lineno, err_line) in missing {
91-
write!(msg, "\n ({fname}:{num}) `{err_line}`", num = src_lineno + 1).unwrap();
90+
write!(msg, "\n ({fname}:{src_lineno}) `{err_line}`").unwrap();
9291
}
9392

9493
if !found.is_empty() {
9594
let init = "\nthe following subset of check directive(s) was found successfully:";
9695
msg.push_str(init);
9796
for (src_lineno, found_line) in found {
98-
write!(msg, "\n ({fname}:{num}) `{found_line}`", num = src_lineno + 1)
99-
.unwrap();
97+
write!(msg, "\n ({fname}:{src_lineno}) `{found_line}`").unwrap();
10098
}
10199
}
102100

0 commit comments

Comments
 (0)