Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0882e5

Browse files
committedMar 17, 2025
Report line number of test when should_panic test failed
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 66678e6 commit f0882e5

File tree

8 files changed

+68
-14
lines changed

8 files changed

+68
-14
lines changed
 

‎compiler/rustc_builtin_macros/src/test.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ pub(crate) fn expand_test_or_bench(
267267

268268
let location_info = get_location_info(cx, &item);
269269

270+
let span_sym = Symbol::intern(&format!("{:?}", sp).split(' ').next().unwrap_or_default());
271+
270272
let mut test_const =
271273
cx.item(
272274
sp,
@@ -336,14 +338,16 @@ pub(crate) fn expand_test_or_bench(
336338
cx.expr_path(should_panic_path("No"))
337339
}
338340
// test::ShouldPanic::Yes
339-
ShouldPanic::Yes(None) => {
340-
cx.expr_path(should_panic_path("Yes"))
341-
}
341+
ShouldPanic::Yes(None) => cx.expr_call(
342+
sp,
343+
cx.expr_path(should_panic_path("Yes")),
344+
thin_vec![cx.expr_str(sp, span_sym)],
345+
),
342346
// test::ShouldPanic::YesWithMessage("...")
343347
ShouldPanic::Yes(Some(sym)) => cx.expr_call(
344348
sp,
345349
cx.expr_path(should_panic_path("YesWithMessage")),
346-
thin_vec![cx.expr_str(sp, sym)],
350+
thin_vec![cx.expr_str(sp, sym), cx.expr_str(sp, span_sym)],
347351
),
348352
},),
349353
// test_type: ...

‎library/test/src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub(crate) enum BenchMode {
1111
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1212
pub enum ShouldPanic {
1313
No,
14-
Yes,
15-
YesWithMessage(&'static str),
14+
Yes(&'static str),
15+
YesWithMessage(&'static str, &'static str),
1616
}
1717

1818
/// Whether should console output be colored or not

‎library/test/src/test_result.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub(crate) fn calc_result<'a>(
4646
exec_time: Option<&time::TestExecTime>,
4747
) -> TestResult {
4848
let result = match (&desc.should_panic, task_result) {
49-
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TestResult::TrOk,
50-
(&ShouldPanic::YesWithMessage(msg), Err(err)) => {
49+
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes(_), Err(_)) => TestResult::TrOk,
50+
(&ShouldPanic::YesWithMessage(msg, _), Err(err)) => {
5151
let maybe_panic_str = err
5252
.downcast_ref::<String>()
5353
.map(|e| &**e)
@@ -71,8 +71,8 @@ pub(crate) fn calc_result<'a>(
7171
))
7272
}
7373
}
74-
(&ShouldPanic::Yes, Ok(())) | (&ShouldPanic::YesWithMessage(_), Ok(())) => {
75-
TestResult::TrFailedMsg("test did not panic as expected".to_string())
74+
(&ShouldPanic::Yes(span), Ok(())) | (&ShouldPanic::YesWithMessage(_, span), Ok(())) => {
75+
TestResult::TrFailedMsg(format!("test did not panic as expected at {}", span))
7676
}
7777
_ => TestResult::TrFailed,
7878
};

‎library/test/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn test_should_panic() {
149149
start_col: 0,
150150
end_line: 0,
151151
end_col: 0,
152-
should_panic: ShouldPanic::Yes,
152+
should_panic: ShouldPanic::Yes(""),
153153
compile_fail: false,
154154
no_run: false,
155155
test_type: TestType::Unknown,

‎library/test/src/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl TestDesc {
230230
return None;
231231
}
232232
match self.should_panic {
233-
options::ShouldPanic::Yes | options::ShouldPanic::YesWithMessage(_) => {
233+
options::ShouldPanic::Yes(_) | options::ShouldPanic::YesWithMessage(_, _) => {
234234
return Some("should panic");
235235
}
236236
options::ShouldPanic::No => {}
@@ -274,7 +274,7 @@ impl TestDescAndFn {
274274
compile_fail: false,
275275
no_run,
276276
should_panic: if should_panic {
277-
options::ShouldPanic::Yes
277+
options::ShouldPanic::Yes("")
278278
} else {
279279
options::ShouldPanic::No
280280
},

‎src/tools/compiletest/src/header.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ pub fn make_test_description<R: Read>(
14121412
// If desired, we could add a `should-fail-pretty` annotation.
14131413
let should_panic = match config.mode {
14141414
crate::common::Pretty => test::ShouldPanic::No,
1415-
_ if should_fail => test::ShouldPanic::Yes,
1415+
_ if should_fail => test::ShouldPanic::Yes(""),
14161416
_ => test::ShouldPanic::No,
14171417
};
14181418

‎tests/ui/test-should-panic.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ run-fail
2+
//@ check-run-results
3+
//@ compile-flags: --test
4+
5+
6+
#[test]
7+
#[should_panic]
8+
fn do_panic1() {
9+
panic!("Panic!");
10+
}
11+
12+
#[test]
13+
#[should_panic = "SubString"]
14+
fn do_panic2() {
15+
// DON'T PANIC
16+
panic!("SubString");
17+
}
18+
19+
#[test]
20+
#[should_panic]
21+
fn dont_panic1() {
22+
// DON'T PANIC
23+
}
24+
25+
26+
#[test]
27+
#[should_panic = "SubString"]
28+
fn dont_panic2() {
29+
// DON'T PANIC
30+
}

‎tests/ui/test-should-panic.run.stdout

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
running 4 tests
3+
test dont_panic2 - should panic ... FAILED
4+
test dont_panic1 - should panic ... FAILED
5+
test do_panic2 - should panic ... ok
6+
test do_panic1 - should panic ... ok
7+
8+
failures:
9+
10+
---- dont_panic2 stdout ----
11+
note: test did not panic as expected at $DIR/test-should-panic.rs:28:1:
12+
---- dont_panic1 stdout ----
13+
note: test did not panic as expected at $DIR/test-should-panic.rs:21:1:
14+
15+
failures:
16+
dont_panic1
17+
dont_panic2
18+
19+
test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
20+

0 commit comments

Comments
 (0)
Failed to load comments.