diff --git a/tests/ui/backtrace/auxiliary/line-tables-only-helper.rs b/tests/ui/backtrace/auxiliary/line-tables-only-helper.rs index 65da2c3f5c761..a9d555feb86b3 100644 --- a/tests/ui/backtrace/auxiliary/line-tables-only-helper.rs +++ b/tests/ui/backtrace/auxiliary/line-tables-only-helper.rs @@ -1,22 +1,22 @@ //@ compile-flags: -Cstrip=none -Cdebuginfo=line-tables-only #[no_mangle] -pub fn baz(mut cb: F, data: u32) where F: FnMut(u32) { +pub fn backtrace_with_baz_in_it(mut cb: F, data: u32) where F: FnMut(u32) { cb(data); } #[no_mangle] -pub fn bar(cb: F, data: u32) where F: FnMut(u32) { - baz(cb, data); +pub fn backtrace_with_bar_in_it(cb: F, data: u32) where F: FnMut(u32) { + backtrace_with_baz_in_it(cb, data); } #[no_mangle] -pub fn foo(cb: F, data: u32) where F: FnMut(u32) { - bar(cb, data); +pub fn backtrace_with_foo_in_it(cb: F, data: u32) where F: FnMut(u32) { + backtrace_with_bar_in_it(cb, data); } pub fn capture_backtrace() -> std::backtrace::Backtrace { let mut bt = None; - foo(|_| bt = Some(std::backtrace::Backtrace::capture()), 42); + backtrace_with_foo_in_it(|_| bt = Some(std::backtrace::Backtrace::capture()), 42); bt.unwrap() } diff --git a/tests/ui/backtrace/line-tables-only.rs b/tests/ui/backtrace/line-tables-only.rs index b1d1fb670a6d6..320ad93393c71 100644 --- a/tests/ui/backtrace/line-tables-only.rs +++ b/tests/ui/backtrace/line-tables-only.rs @@ -18,7 +18,8 @@ //@ ignore-visionos needs the `.dSYM` files to be moved to the device //@ needs-unwind //@ aux-build: line-tables-only-helper.rs - +//@ edition: 2021 +#![feature(backtrace_frames)] extern crate line_tables_only_helper; @@ -30,13 +31,27 @@ fn assert_contains( expected_file: &str, expected_line: u32, ) { - // FIXME(jieyouxu): fix this ugly fragile test when `BacktraceFrame` has accessors like... - // `symbols()`. - let backtrace = format!("{:#?}", backtrace); - eprintln!("{}", backtrace); - assert!(backtrace.contains(expected_name), "backtrace does not contain expected name {}", expected_name); - assert!(backtrace.contains(expected_file), "backtrace does not contain expected file {}", expected_file); - assert!(backtrace.contains(&expected_line.to_string()), "backtrace does not contain expected line {}", expected_line); + // The formatted frames look like this: + // `{ fn: "backtrace_with_baz_in_it", file: ".../tests/ui/backtrace/auxiliary/line-tables-only-helper.rs", line: 5 }` + // or this: + // `{ fn: "line_tables_only_helper::backtrace_with_baz_in_it", file: "...\tests\ui\backtrace\auxiliary\line-tables-only-helper.rs", line: 5 },` + // Make sure we match the right part when searching for the function name and line number. + let expected_line_str = format!("line: {expected_line} "); + eprintln!("{:#?}", backtrace); + for frame in backtrace.frames() { + // FIXME: we use string matching. Replace this by getting the actual data out of the frame, + // once that is possible. + let frame = format!("{:#?}", frame); + if frame.contains(expected_name) + && frame.contains(expected_file) + && frame.contains(&expected_line_str) + { + return; + } + } + panic!( + "backtrace does not contain expected frame with name={expected_name}, file={expected_file}, line={expected_line}" + ); } fn main() { @@ -48,8 +63,8 @@ fn main() { // And with #143208 we also lost `bar` in the line tables. #[cfg(not(all(target_pointer_width = "32", target_env = "msvc")))] { - assert_contains(&backtrace, "foo", "line-tables-only-helper.rs", 5); - assert_contains(&backtrace, "bar", "line-tables-only-helper.rs", 10); + assert_contains(&backtrace, "backtrace_with_foo_in_it", "line-tables-only-helper.rs", 15); + assert_contains(&backtrace, "backtrace_with_bar_in_it", "line-tables-only-helper.rs", 10); } - assert_contains(&backtrace, "baz", "line-tables-only-helper.rs", 5); + assert_contains(&backtrace, "backtrace_with_baz_in_it", "line-tables-only-helper.rs", 5); }