Skip to content

Accept a standalone { } block, and report a block header with no { - #375

Merged
jbearak merged 1 commit into
mainfrom
brace-diagnostics
Nov 5, 2026
Merged

jbearak merged 1 commit into
mainfrom
brace-diagnostics

Conversation

@jbearak

@jbearak jbearak commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Sight reported every standalone { … } block as OPEN_BRACE_ALONE at error level, but it's valid Stata. StataMP 18 runs one at the top level, in a program, after another block, nested, in a loop, and under #delimit ;. The parser also skipped the block's body, so the code inside it got spurious UNNECESSARY_INDENTATION hints. Sight also said nothing about if x with no command and no {, which Stata rejects with "{ required" (r(100)).

Now a { that starts a statement is a block with no prefix. The parser reads its body, and both the pretty printer and parse --json already supported a block with no prefix. A block header with no { gets a new error, MISSING_OPEN_BRACE.

Behavior

  • MISSING_OPEN_BRACE (error): reported for if x alone and else if x alone. Also for foreach, forvalues or while with no { at the end of the line (under #delimit ;, the statement), even with a command after it. All of these stop Stata with r(100), including under #delimit ;. After capture they set _rc to 100.
  • OPEN_BRACE_ALONE now fires only for a { that starts the statement right after one that needed it:
    • after a header with no { (if 1, then { on the next line), where Stata stops at the header;
    • after a bare else, or a prefix alone on its line such as capture or quietly, where Stata runs the block on its own: whatever the if found, or without the prefix (an error in it isn't captured);
    • a { after a command on the same line (b {), which Stata passes to the command as an argument. The old recovery stays for this case; only the message changed.
  • A bare else (else, then a command on the next line) is valid Stata and isn't reported.
  • docs/diagnostics.md describes both codes.

Formatting

sight parse --json changes (for do2r)

The golden files don't change, because no fixture has these forms. For other files:

  • A standalone { … } now appears in statements as kind: "block" with prefixes: [] and its body. Before, it wasn't in statements at all, and its body was missing too.
  • New code MISSING_OPEN_BRACE, severity: "error", with a range covering the header from its keyword to its last token.
  • OPEN_BRACE_ALONE goes away for standalone blocks. Where it stays, the message names the keyword: open brace must be on the same line as `if`; Stata stops with "{ required", and `else` or `capture` with "here the block runs…". The b { case reads "open brace does not open a block here: Stata passes it to the command before it". The range is still the { token.
  • { di 1 without a prefix now gives CODE_AFTER_OPEN_BRACE (warning) instead of OPEN_BRACE_ALONE (error), the same as quietly { di 1 already did.

Proof: tests/unit/brace-block-diagnostics.test.ts runs every form below through DiagnosticsProvider (36 of 54 fail on main). The cases mirror StataMP 18 runs on each form. All four gates pass separately, including the parse --json golden test. A sweep of about 1,000 unique real .do/.ado files found two new MISSING_OPEN_BRACE hits, both real (if (cond) with nothing after it), and otherwise the same brace diagnostics as main.

StataMP 18 transcripts (abridged)
. {                                   . if 1
.     di "in block"                   { required
in block                              r(100);
. }
                                      . foreach v in a b di "x"
. program p                           { required
  1.     {                            r(100);
  2.         di "in prog block"
  3.     }                            . #delimit ;
  4.     di "after in prog"           . while 0 ;
  5. end                              { required
. p                                   r(100);
in prog block
after in prog                         . capture if 1
                                      . di "after rc=" _rc
. if 1 {                              after rc=100
.     di "if body"
if body                               . if 1 {
. }                                   .     di "if body"
. {                                   if body
.     di "second block"               . }
second block                          . else
. }                                   . {
                                      .     di "else body ran"
. #delimit ;                          else body ran
. { ;                                 . }
. di "delim block" ;
delim block                           . capture
. } ;                                 . {
                                      .     error 7
. { di "same line"                    r(7);
program error:  code follows on the
same line as open brace               . bprog {
r(198);                               args: {'"
                                      .     di "inside"
. if 1 ///                            inside
> {                                   . }
.     di "cont body"                  } is not a valid command name
cont body                             r(199);
. }

Not changed (reported separately)

  • The analyzer doesn't build symbols from a block's body, so quietly { local x 5 } followed by `x' still warns that x is undefined. That also applies to standalone blocks. The analyzer-command-bodies thread is handling it.
  • CODE_AFTER_OPEN_BRACE is a warning, though Stata stops at { di and quietly { di with r(198). Only after a control-flow header (if 1 { di) does Stata ignore the code.

Reviewed with sol-loop: 1 round with gpt-6.1-sol. Sol verdict: approved

Created with Claude Opus 5.5 in T3 Code (Claude Code harness), reviewed by gpt-6.1-sol in Codex.

@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 3 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available. Your 52 included PR review attempts over the past 7 days set your current allowance at 4 reviews per hour.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Team
  • Run ID: cc5fe061-4c58-4792-9811-610c542845f7
Commits

Reviewing files that changed from the base of the PR and between bd6b72e and bc754da.

Files selected for processing: 6

  • docs/diagnostics.md
  • src/parser/index.ts
  • src/types/index.ts
  • src/utils/syntax-error-severity.ts
  • tests/unit/ast-format-unprintable-code.test.ts
  • tests/unit/brace-block-diagnostics.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

- A `{ ... }` block that starts a statement is a block with no prefix,
  not OPEN_BRACE_ALONE. StataMP 18 runs one at the top level, in a
  program, after another block, nested, in a loop, and under
  `#delimit ;`. Its body is now parsed and printed.
- New MISSING_OPEN_BRACE (error): `if x` alone, `else if x` alone, and
  `foreach`, `forvalues`, or `while` with no `{` at the end of the line
  (or statement, under `#delimit ;`), even with a command after it.
  Stata stops with "{ required" (r(100)).
- OPEN_BRACE_ALONE now means a `{` that starts the statement after a
  header with no `{` (Stata stops at the header), after a bare `else`
  or a prefix like `capture` (Stata runs the block on its own), or a
  `{` after a command on its line (Stata passes it to the command).
- AST formatting leaves `{ di 1` and `quietly { di 1` as written:
  Stata stops there (r(198)), and printing would move `di 1` onto a
  line where it runs.
@jbearak
jbearak merged commit fcf970c into main Nov 5, 2026
16 checks passed
@jbearak
jbearak deleted the brace-diagnostics branch November 5, 2026 04:10
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.

1 participant