Skip to content

Conversation

@SupulHeshan
Copy link
Collaborator

@SupulHeshan SupulHeshan commented Jan 27, 2026

Description

This PR improves the parser’s handling of bare except blocks by detecting a common misparse scenario where except { is interpreted as a dictionary context.

When semicolons appear inside what the parser assumes is a dict, we now:

  • Correctly identify it as a bare except usage
  • Point the error to the actual except line
  • Emit a clear, user-friendly message guiding proper usage

This prevents confusing syntax errors and provides a graceful, actionable failure:

“Bare except is not supported. Use except Exception or except Exception as e”

Overall, this makes error reporting more precise, easier to understand, and safer for developers writing exception handling code.

examples:
image

image

Additionally, new test cases have been added.

Copilot AI review requested due to automatic review settings January 27, 2026 10:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves Jac parser diagnostics for the common misparse of bare except { ... } by attempting to detect the scenario and emit a clearer, actionable error message, with accompanying regression tests.

Changes:

  • Add a parser heuristic to detect bare except { when the parser misinterprets { as a dict and then encounters semicolons.
  • Emit a dedicated error message for bare except usage.
  • Add a new fixture and unit test covering the new diagnostic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
jac/jaclang/pycore/jac_parser.py Adds bare-except misparse detection and emits a specialized error message.
jac/tests/compiler/test_parser.py Adds a test asserting the bare-except diagnostic message.
jac/tests/compiler/fixtures/bare_except_error.jac Adds a fixture that triggers the bare-except misparse path.

"Bare except is not supported. Use `except Exception` or `except Exception as e`",
error_tok,
)
return False # Stop parsing gracefully
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning False from on_error causes Lark to re-raise the parse error, and JacParser.transform() will then log an additional generic "Unexpected token ..." error. That undermines the goal of a single clear bare-except diagnostic. Consider returning True with recovery (or setting a flag so the outer except jl.UnexpectedInput path skips logging) so only the bare-except message is reported.

Suggested change
return False # Stop parsing gracefully
return True # Error handled; allow Lark to recover without re-raising

Copilot uses AI. Check for mistakes.
Comment on lines +293 to +298
assert len(prog.errors_had) >= 1
# Check that the first error mentions bare except
error_msg = prog.errors_had[0].pretty_print()
assert "Bare except is not supported" in error_msg
assert "except Exception" in error_msg
assert "except Exception as e" in error_msg
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test validates the new error message text, but it doesn't validate the key behavioral improvement described in the PR (pointing the error at the except line rather than the misparsed semicolon/dict context). Consider asserting the reported location (e.g., prog.errors_had[0].loc.first_line or a line <n> substring in pretty_print()) so regressions in the line/column mapping are caught.

Copilot uses AI. Check for mistakes.
@kugesan1105 kugesan1105 linked an issue Jan 27, 2026 that may be closed by this pull request
@SupulHeshan SupulHeshan changed the title Improve parser error for bare except blocks with clear Improve parser error for bare except blocks with clear Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Misleading syntax error for bare except

2 participants