Skip to content

Commit 54c3b89

Browse files
authored
Merge pull request #199 from Jarcho/error_above
Allow error comments above the error site.
2 parents 00d902c + 839c662 commit 54c3b89

File tree

7 files changed

+117
-4
lines changed

7 files changed

+117
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
### Fixedw
12+
* Add `//~v` comments to put an error matcher above the error site.
13+
14+
### Fixed
1315

1416
### Changed
1517

src/parser.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ impl Comments {
280280
let defaults = std::mem::take(parser.comments.revisioned.get_mut(&[][..]).unwrap());
281281

282282
let mut fallthrough_to = None; // The line that a `|` will refer to.
283+
let mut last_line = 0;
283284
for (l, line) in content.as_ref().lines().enumerate() {
285+
last_line = l + 1;
284286
let l = NonZeroUsize::new(l + 1).unwrap(); // enumerate starts at 0, but line numbers start at 1
285287
let span = Span {
286288
file: file.to_path_buf(),
@@ -316,6 +318,25 @@ impl Comments {
316318
}
317319
}
318320

321+
for revisioned in parser.comments.revisioned.values() {
322+
for m in &revisioned.error_matches {
323+
if m.line.get() > last_line {
324+
let span = match &m.kind {
325+
ErrorMatchKind::Pattern { pattern, .. } => pattern.span(),
326+
ErrorMatchKind::Code(code) => code.span(),
327+
};
328+
parser.errors.push(Error::InvalidComment {
329+
msg: format!(
330+
"//~v pattern is trying to refer to line {}, but the file only has {} lines",
331+
m.line.get(),
332+
last_line,
333+
),
334+
span,
335+
});
336+
}
337+
}
338+
}
339+
319340
let Revisioned {
320341
span,
321342
ignore,
@@ -852,6 +873,30 @@ impl CommentParser<&mut Revisioned> {
852873
}
853874
}
854875
}
876+
Some(Spanned {
877+
content: 'v',
878+
span: _,
879+
}) => {
880+
let offset = pattern.chars().take_while(|c| c.content == 'v').count();
881+
match pattern
882+
.span()
883+
.line_start
884+
.get()
885+
.checked_add(offset)
886+
.and_then(NonZeroUsize::new)
887+
{
888+
Some(match_line) => (match_line, pattern.split_at(offset).1),
889+
_ => {
890+
// The line count of the file is not yet known so we can only check
891+
// if the resulting line is in the range of a usize.
892+
self.error(pattern.span(), format!(
893+
"//~v pattern is trying to refer to {} lines below, which is more than ui_test can count",
894+
offset,
895+
));
896+
return;
897+
}
898+
}
899+
}
855900
Some(_) => (pattern.span().line_start, pattern),
856901
None => {
857902
self.error(pattern.span(), "no pattern specified");

tests/integrations/basic-fail/Cargo.stdout

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ tests/actual_tests/filters.rs ... FAILED
1212
tests/actual_tests/foomp.rs ... FAILED
1313
tests/actual_tests/foomp2.rs ... FAILED
1414
tests/actual_tests/pattern_too_many_arrow.rs ... FAILED
15+
tests/actual_tests/pattern_too_many_arrow_above.rs ... FAILED
1516
tests/actual_tests/rustc_ice.rs ... FAILED
1617

1718
FAILED TEST: tests/actual_tests/bad_pattern.rs
@@ -268,6 +269,22 @@ full stdout:
268269

269270

270271

272+
FAILED TEST: tests/actual_tests/pattern_too_many_arrow_above.rs
273+
command: "parse comments"
274+
275+
error: //~v pattern is trying to refer to line 8, but the file only has 6 lines
276+
--> tests/actual_tests/pattern_too_many_arrow_above.rs:2:22
277+
|
278+
2 | //~vvvvvv ERROR: mismatched types
279+
| ^^^^^^^^^^^^^^^^
280+
|
281+
282+
full stderr:
283+
284+
full stdout:
285+
286+
287+
271288
FAILED TEST: tests/actual_tests/rustc_ice.rs
272289
command: "rustc" "--error-format=json" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail.rlib" "--extern" "basic_fail=$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug/libbasic_fail-$HASH.rmeta" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "-L" "$DIR/tests/integrations/basic-fail/../../../target/$TMP/$TRIPLE/debug" "--out-dir" "$TMP "tests/actual_tests/rustc_ice.rs" "-Ztreat-err-as-bug" "--edition" "2021"
273290

@@ -323,9 +340,10 @@ FAILURES:
323340
tests/actual_tests/foomp.rs
324341
tests/actual_tests/foomp2.rs
325342
tests/actual_tests/pattern_too_many_arrow.rs
343+
tests/actual_tests/pattern_too_many_arrow_above.rs
326344
tests/actual_tests/rustc_ice.rs
327345

328-
test result: FAIL. 9 failed;
346+
test result: FAIL. 10 failed;
329347

330348
Building dependencies ... ok
331349
tests/actual_tests_bless/aux_build_not_found.rs ... FAILED
@@ -914,6 +932,7 @@ tests/actual_tests/filters.rs ... FAILED
914932
tests/actual_tests/foomp.rs ... FAILED
915933
tests/actual_tests/foomp2.rs ... FAILED
916934
tests/actual_tests/pattern_too_many_arrow.rs ... FAILED
935+
tests/actual_tests/pattern_too_many_arrow_above.rs ... FAILED
917936
tests/actual_tests/rustc_ice.rs ... FAILED
918937

919938
FAILED TEST: tests/actual_tests/bad_pattern.rs
@@ -1002,6 +1021,22 @@ full stdout:
10021021

10031022

10041023

1024+
FAILED TEST: tests/actual_tests/pattern_too_many_arrow_above.rs
1025+
command: "parse comments"
1026+
1027+
error: //~v pattern is trying to refer to line 8, but the file only has 6 lines
1028+
--> tests/actual_tests/pattern_too_many_arrow_above.rs:2:22
1029+
|
1030+
2 | //~vvvvvv ERROR: mismatched types
1031+
| ^^^^^^^^^^^^^^^^
1032+
|
1033+
1034+
full stderr:
1035+
1036+
full stdout:
1037+
1038+
1039+
10051040
FAILED TEST: tests/actual_tests/rustc_ice.rs
10061041
command: "$CMD" "tests/actual_tests/rustc_ice.rs" "-Ztreat-err-as-bug" "--edition" "2021"
10071042

@@ -1019,9 +1054,10 @@ FAILURES:
10191054
tests/actual_tests/foomp.rs
10201055
tests/actual_tests/foomp2.rs
10211056
tests/actual_tests/pattern_too_many_arrow.rs
1057+
tests/actual_tests/pattern_too_many_arrow_above.rs
10221058
tests/actual_tests/rustc_ice.rs
10231059

1024-
test result: FAIL. 9 failed;
1060+
test result: FAIL. 10 failed;
10251061

10261062

10271063
running 0 tests
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
//~vvvvvv ERROR: mismatched types
3+
use_unit(1_u32);
4+
}
5+
6+
fn use_unit(_: ()) {}

tests/integrations/basic/Cargo.stdout

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Building aux file tests/actual_tests/auxiliary/derive_proc_macro.rs ... ok
1313
tests/actual_tests/aux_derive.rs ... ok
1414
Building aux file tests/actual_tests/auxiliary/the_proc_macro.rs ... ok
1515
tests/actual_tests/aux_proc_macro.rs ... ok
16+
tests/actual_tests/error_above.rs ... ok
1617
tests/actual_tests/executable.rs ... ok
1718
tests/actual_tests/foomp-rustfix.rs ... ok
1819
tests/actual_tests/foomp.rs ... ok
@@ -23,7 +24,7 @@ tests/actual_tests/unicode.rs ... ok
2324
tests/actual_tests/windows_paths.rs ... ok
2425
tests/actual_tests/subdir/aux_proc_macro.rs ... ok
2526

26-
test result: ok. 11 passed;
27+
test result: ok. 12 passed;
2728

2829

2930
running 0 tests
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use basic::add;
2+
3+
fn main() {
4+
//~v ERROR: mismatched types
5+
add("42", 3);
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> tests/actual_tests/error_above.rs:5:9
3+
|
4+
5 | add("42", 3);
5+
| --- ^^^^ expected `usize`, found `&str`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
note: function defined here
10+
--> $DIR/tests/integrations/basic/src/lib.rs:1:8
11+
|
12+
1 | pub fn add(left: usize, right: usize) -> usize {
13+
| ^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)