Skip to content

Commit

Permalink
Allow error comments above the error site.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 13, 2024
1 parent b6d9ea6 commit d07792d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Fixedw
* Add `//~v` comments to put an error matcher above the error site.

### Fixed

### Changed

Expand Down
45 changes: 45 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ impl Comments {
let defaults = std::mem::take(parser.comments.revisioned.get_mut(&[][..]).unwrap());

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

for (_, revisioned) in &parser.comments.revisioned {
for m in &revisioned.error_matches {
if m.line.get() > last_line {
let span = match &m.kind {
ErrorMatchKind::Pattern { pattern, .. } => pattern.span(),
ErrorMatchKind::Code(code) => code.span(),
};
parser.errors.push(Error::InvalidComment {
msg: format!(
"//~v pattern is trying to refer to line {}, but the file only has {} lines",
m.line.get(),
last_line,
),
span,
});
}
}
}

let Revisioned {
span,
ignore,
Expand Down Expand Up @@ -852,6 +873,30 @@ impl CommentParser<&mut Revisioned> {
}
}
}
Some(Spanned {
content: 'v',
span: _,
}) => {
let offset = pattern.chars().take_while(|c| c.content == 'v').count();
match pattern
.span()
.line_start
.get()
.checked_add(offset)
.and_then(NonZeroUsize::new)
{
Some(match_line) => (match_line, pattern.split_at(offset).1),
_ => {
// The line count of the file is not yet known so we can only check
// if the resulting line is in the range of a usize.
self.error(pattern.span(), format!(
"//~v pattern is trying to refer to {} lines below, which is more than ui_test can count",
offset,
));
return;
}
}
}
Some(_) => (pattern.span().line_start, pattern),
None => {
self.error(pattern.span(), "no pattern specified");
Expand Down
40 changes: 38 additions & 2 deletions tests/integrations/basic-fail/Cargo.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tests/actual_tests/filters.rs ... FAILED
tests/actual_tests/foomp.rs ... FAILED
tests/actual_tests/foomp2.rs ... FAILED
tests/actual_tests/pattern_too_many_arrow.rs ... FAILED
tests/actual_tests/pattern_too_many_arrow_above.rs ... FAILED
tests/actual_tests/rustc_ice.rs ... FAILED

FAILED TEST: tests/actual_tests/bad_pattern.rs
Expand Down Expand Up @@ -229,6 +230,22 @@ full stdout:



FAILED TEST: tests/actual_tests/pattern_too_many_arrow_above.rs
command: "parse comments"

error: //~v pattern is trying to refer to line 8, but the file only has 6 lines
--> tests/actual_tests/pattern_too_many_arrow_above.rs:2:22
|
2 | //~vvvvvv ERROR: mismatched types
| ^^^^^^^^^^^^^^^^
|

full stderr:

full stdout:



FAILED TEST: tests/actual_tests/rustc_ice.rs
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"

Expand Down Expand Up @@ -284,9 +301,10 @@ FAILURES:
tests/actual_tests/foomp.rs
tests/actual_tests/foomp2.rs
tests/actual_tests/pattern_too_many_arrow.rs
tests/actual_tests/pattern_too_many_arrow_above.rs
tests/actual_tests/rustc_ice.rs

test result: FAIL. 9 failed;
test result: FAIL. 10 failed;

Building dependencies ... ok
tests/actual_tests_bless/aux_build_not_found.rs ... FAILED
Expand Down Expand Up @@ -875,6 +893,7 @@ tests/actual_tests/filters.rs ... FAILED
tests/actual_tests/foomp.rs ... FAILED
tests/actual_tests/foomp2.rs ... FAILED
tests/actual_tests/pattern_too_many_arrow.rs ... FAILED
tests/actual_tests/pattern_too_many_arrow_above.rs ... FAILED
tests/actual_tests/rustc_ice.rs ... FAILED

FAILED TEST: tests/actual_tests/bad_pattern.rs
Expand Down Expand Up @@ -963,6 +982,22 @@ full stdout:



FAILED TEST: tests/actual_tests/pattern_too_many_arrow_above.rs
command: "parse comments"

error: //~v pattern is trying to refer to line 8, but the file only has 6 lines
--> tests/actual_tests/pattern_too_many_arrow_above.rs:2:22
|
2 | //~vvvvvv ERROR: mismatched types
| ^^^^^^^^^^^^^^^^
|

full stderr:

full stdout:



FAILED TEST: tests/actual_tests/rustc_ice.rs
command: "$CMD" "tests/actual_tests/rustc_ice.rs" "-Ztreat-err-as-bug" "--edition" "2021"

Expand All @@ -980,9 +1015,10 @@ FAILURES:
tests/actual_tests/foomp.rs
tests/actual_tests/foomp2.rs
tests/actual_tests/pattern_too_many_arrow.rs
tests/actual_tests/pattern_too_many_arrow_above.rs
tests/actual_tests/rustc_ice.rs

test result: FAIL. 9 failed;
test result: FAIL. 10 failed;


running 0 tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
//~vvvvvv ERROR: mismatched types
use_unit(1_u32);
}

fn use_unit(_: ()) {}
3 changes: 2 additions & 1 deletion tests/integrations/basic/Cargo.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Building aux file tests/actual_tests/auxiliary/derive_proc_macro.rs ... ok
tests/actual_tests/aux_derive.rs ... ok
Building aux file tests/actual_tests/auxiliary/the_proc_macro.rs ... ok
tests/actual_tests/aux_proc_macro.rs ... ok
tests/actual_tests/error_above.rs ... ok
tests/actual_tests/executable.rs ... ok
tests/actual_tests/foomp-rustfix.rs ... ok
tests/actual_tests/foomp.rs ... ok
Expand All @@ -23,7 +24,7 @@ tests/actual_tests/unicode.rs ... ok
tests/actual_tests/windows_paths.rs ... ok
tests/actual_tests/subdir/aux_proc_macro.rs ... ok

test result: ok. 11 passed;
test result: ok. 12 passed;


running 0 tests
Expand Down
6 changes: 6 additions & 0 deletions tests/integrations/basic/tests/actual_tests/error_above.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use basic::add;

fn main() {
//~v ERROR: mismatched types
add("42", 3);
}
17 changes: 17 additions & 0 deletions tests/integrations/basic/tests/actual_tests/error_above.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> tests/actual_tests/error_above.rs:5:9
|
5 | add("42", 3);
| --- ^^^^ expected `usize`, found `&str`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/tests/integrations/basic/src/lib.rs:1:8
|
1 | pub fn add(left: usize, right: usize) -> usize {
| ^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit d07792d

Please sign in to comment.