Skip to content

Commit 808b88d

Browse files
committed
Dynamically obtain line number of panic! expression
1 parent 2ac8d82 commit 808b88d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

godot-core/src/private.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ where
390390
let msg = extract_panic_message(err);
391391
let mut msg = format_panic_message(msg);
392392

393-
// try to add location information
393+
// Try to add location information.
394394
if let Ok(guard) = info.lock() {
395395
if let Some(info) = guard.as_ref() {
396396
msg = format!("{}\n at {}:{}", msg, info.file, info.line);

itest/rust/src/object_tests/dynamic_call_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,14 @@ fn dynamic_call_with_panic() {
154154
#[cfg(not(target_os = "windows"))]
155155
let path = "itest/rust/src/object_tests/object_test.rs";
156156

157+
// Obtain line number dynamically, avoids tedious maintenance on code reorganization.
158+
let line = ObjPayload::get_panic_line();
159+
157160
let expected_error_message = format!(
158161
"godot-rust function call failed: Object::call(&\"do_panic\")\
159162
\n Source: ObjPayload::do_panic()\
160163
\n Reason: [panic] do_panic exploded\
161-
\n at {path}:893"
164+
\n at {path}:{line}"
162165
);
163166

164167
assert_eq!(call_error.to_string(), expected_error_message);

itest/rust/src/object_tests/object_test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,11 @@ impl ObjPayload {
887887
fn do_panic(&self) {
888888
panic!("do_panic exploded");
889889
}
890+
891+
// Obtain the line number of the panic!() call above; keep equidistant to do_panic() method.
892+
pub fn get_panic_line() -> u32 {
893+
line!() - 5
894+
}
890895
}
891896

892897
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)