Skip to content

Commit 0ac598a

Browse files
zzarbttooclaude
andcommitted
Clarify that the except error range stops before the closing colon
`invalid_except_stmt_end` returns the exclusive end of the range, so the column it yields is the one the `:` sits on even though the `:` is not part of the range. The previous "ending at the `:`" wording read as if the colon were included, which invites an off-by-one "fix". Comments only; no behavior change. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 7c39f0e commit 0ac598a

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

‎crates/vm/src/vm/vm_new.rs‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -935,9 +935,10 @@ impl VirtualMachine {
935935
}
936936
_ => false,
937937
};
938-
// CPython reports the unparenthesized exception types error with a range ending
939-
// at the `:` that closes the `except` clause, which covers the `as NAME` part,
940-
// while the parser reports the exception types alone. See `invalid_except_stmt_end`.
938+
// CPython reports the unparenthesized exception types error with a range stopping
939+
// just before the `:` that closes the `except` clause, which covers the `as NAME`
940+
// part, while the parser reports the exception types alone. The exclusive end that
941+
// yields is the column of the `:`. See `invalid_except_stmt_end`.
941942
let except_as_end = cfg_select! {
942943
feature = "parser" => {
943944
if msg == "multiple exception types must be parenthesized when using 'as'"
@@ -1152,13 +1153,18 @@ impl VirtualMachine {
11521153
define_exception_fn!(fn new_unbound_local_error, unbound_local_error, UnboundLocalError);
11531154
}
11541155

1155-
/// Returns the end of the range CPython reports for its `invalid_except_stmt` rule, as a
1156-
/// 1-based `(line, column)` pair: the location of the `:` that closes the `except` clause
1157-
/// whose exception types were reported as needing parentheses.
1156+
/// Returns the exclusive end of the range CPython reports for its `invalid_except_stmt`
1157+
/// rule, as a 1-based `(line, column)` pair. Being exclusive, that column is the one the
1158+
/// `:` closing the `except` clause sits on: the `:` itself is not part of the range.
11581159
///
11591160
/// CPython raises that error only once the whole clause has matched, and reports a range
1160-
/// starting at the first exception type and ending at the `:`, so the range covers the
1161-
/// `as NAME` part as well:
1161+
/// starting at the first exception type and stopping just before the `:`, so the range
1162+
/// covers the `as NAME` part as well:
1163+
///
1164+
/// ```text
1165+
/// except A, B as e:
1166+
/// ^^^^^^^^^ offset = 8, end_offset = 17
1167+
/// ```
11621168
///
11631169
/// ```text
11641170
/// invalid_except_stmt:

0 commit comments

Comments
 (0)