Skip to content

Commit a3590c8

Browse files
Copilotminestarks
andauthored
Fix off-by-one error in stack traces by making line and column numbers 1-based for user display (#2628)
Stack traces were displaying both line and column numbers using 0-based indexing instead of the standard 1-based indexing expected by users and editors. ## Problem When a runtime error occurred, the stack trace would report incorrect line and column numbers that were off by one: ```qsharp function Main() : Unit { fail "line 2"; // This is line 2, column 5 (1-based counting) } ``` **Before fix:** ``` Error: program failed: line 2 Call stack: at Main in test.qs:1:4 // Wrong! Shows line 1, column 4 ``` **After fix:** ``` Error: program failed: line 2 Call stack: at Main in test.qs:2:5 // Correct! Shows line 2, column 5 ``` ## Root Cause The `format_call_stack` function in `source/compiler/qsc/src/interpret/debug.rs` was displaying `pos.line` and `pos.column` (which are 0-based for internal calculations) directly to users. Both line and column numbers should be 1-based for user-facing display as this is the standard convention used by editors and IDEs. ## Solution Updated both line and column number display to add 1 to convert from internal 0-based indexing to user-expected 1-based indexing: ```rust // Before pos.line, pos.column, // After pos.line + 1, pos.column + 1, ``` This change ensures that both line and column numbers are displayed correctly to users while preserving the internal 0-based indexing used throughout the codebase. ## Testing - Updated existing stack trace test expectations to reflect correct 1-based line and column numbers - All 158 qsc package tests pass - All debug-specific tests pass Fixes #2627. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: minestarks <[email protected]>
1 parent 03bc8e5 commit a3590c8

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

source/compiler/qsc/src/interpret/debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub(crate) fn format_call_stack(
5454
trace,
5555
" in {}:{}:{}",
5656
name.unwrap_or("<expression>".to_string()),
57-
pos.line,
58-
pos.column,
57+
pos.line + 1,
58+
pos.column + 1,
5959
)
6060
.expect("writing to string should succeed");
6161

source/compiler/qsc/src/interpret/debug/tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ fn stack_traces_can_cross_eval_session_and_file_boundaries() {
9494
expect![[r#"
9595
Error: division by zero
9696
Call stack:
97-
at Adjoint Test.C in 1.qs:10:12
98-
at Adjoint Test.B in 1.qs:3:12
99-
at Adjoint Test2.A in 2.qs:4:12
100-
at Z in line_0:0:34
97+
at Adjoint Test.C in 1.qs:11:13
98+
at Adjoint Test.B in 1.qs:4:13
99+
at Adjoint Test2.A in 2.qs:5:13
100+
at Z in line_0:1:35
101101
"#]]
102102
.assert_eq(stack_trace);
103103
}
@@ -165,9 +165,9 @@ fn stack_traces_can_cross_file_and_entry_boundaries() {
165165
expect![[r#"
166166
Error: division by zero
167167
Call stack:
168-
at Adjoint Test.C in 1.qs:10:12
169-
at Adjoint Test.B in 1.qs:3:12
170-
at Adjoint Test2.A in 2.qs:4:12
168+
at Adjoint Test.C in 1.qs:11:13
169+
at Adjoint Test.B in 1.qs:4:13
170+
at Adjoint Test2.A in 2.qs:5:13
171171
"#]]
172172
.assert_eq(stack_trace);
173173
}

0 commit comments

Comments
 (0)