Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow error comments above the error site. #199

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.values() {
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 @@ -268,6 +269,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 @@ -323,9 +340,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 @@ -914,6 +932,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 @@ -1002,6 +1021,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 @@ -1019,9 +1054,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 1 previous error

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