@@ -33,6 +33,35 @@ mod needs;
3333#[ cfg( test) ]
3434mod tests;
3535
36+ /// A line number in a file. Internally the first line has index 1.
37+ /// If it is 0 it means "no specific line" (used e.g. for implied directives).
38+ /// When displayed, the first line is 1.
39+ pub ( crate ) struct LineNumber ( usize ) ;
40+
41+ impl LineNumber {
42+ /// Create a LineNumber from a zero-based line index. I.e. if `zero_based`
43+ /// is `0` it means "the first line".
44+ pub ( crate ) fn from_zero_based ( zero_based : usize ) -> Self {
45+ // Ensure to panic on overflow.
46+ LineNumber ( zero_based. strict_add ( 1 ) )
47+ }
48+
49+ pub ( crate ) fn from_one_based ( one_based : usize ) -> LineNumber {
50+ assert ! ( one_based > 0 ) ;
51+ LineNumber ( one_based)
52+ }
53+
54+ pub ( crate ) fn none ( ) -> LineNumber {
55+ LineNumber ( 0 )
56+ }
57+ }
58+
59+ impl std:: fmt:: Display for LineNumber {
60+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
61+ write ! ( f, "{}" , self . 0 )
62+ }
63+ }
64+
3665pub struct DirectivesCache {
3766 /// "Conditions" used by `ignore-*` and `only-*` directives, prepared in
3867 /// advance so that they don't have to be evaluated repeatedly.
@@ -591,7 +620,7 @@ fn iter_directives(
591620 ] ;
592621 // Process the extra implied directives, with a dummy line number of 0.
593622 for directive_str in extra_directives {
594- let directive_line = line_directive ( testfile, 0 , directive_str)
623+ let directive_line = line_directive ( testfile, LineNumber :: none ( ) , directive_str)
595624 . unwrap_or_else ( || panic ! ( "bad extra-directive line: {directive_str:?}" ) ) ;
596625 it ( & directive_line) ;
597626 }
@@ -703,7 +732,7 @@ impl Config {
703732 line : & DirectiveLine < ' _ > ,
704733 directive : & str ,
705734 ) -> Option < String > {
706- let & DirectiveLine { file_path, line_number, .. } = line;
735+ let & DirectiveLine { file_path, ref line_number, .. } = line;
707736
708737 if line. name != directive {
709738 return None ;
@@ -938,7 +967,7 @@ pub(crate) fn make_test_description(
938967 iter_directives (
939968 config. mode ,
940969 file_directives,
941- & mut |ln @ & DirectiveLine { line_number, .. } | {
970+ & mut |ln @ & DirectiveLine { ref line_number, .. } | {
942971 if !ln. applies_to_test_revision ( test_revision) {
943972 return ;
944973 }
@@ -1262,7 +1291,7 @@ enum IgnoreDecision {
12621291
12631292fn parse_edition_range ( config : & Config , line : & DirectiveLine < ' _ > ) -> Option < EditionRange > {
12641293 let raw = config. parse_name_value_directive ( line, "edition" ) ?;
1265- let & DirectiveLine { file_path : testfile, line_number, .. } = line;
1294+ let & DirectiveLine { file_path : testfile, ref line_number, .. } = line;
12661295
12671296 // Edition range is half-open: `[lower_bound, upper_bound)`
12681297 if let Some ( ( lower_bound, upper_bound) ) = raw. split_once ( ".." ) {
0 commit comments