Skip to content

Report locals set by an abbreviated tokenize or an unloaded program, accept g as generate, and check abbreviated variables in linear time - #415

Merged
jbearak merged 2 commits into
mainfrom
t3/fix-analyzer-macro-and-variable-analysis
Nov 19, 2026
Merged

jbearak merged 2 commits into
mainfrom
t3/fix-analyzer-macro-and-variable-analysis

Conversation

@jbearak

@jbearak jbearak commented Nov 19, 2026

Copy link
Copy Markdown
Owner

An audit of v0.15.1..main before the stable release turned up four analyzer bugs. Three are regressions from v0.15.1, and one is a gap in the new opt-in undefined-variable check.

1. token hid an undefined caller local.

program define p
    token alpha beta
    c_local `1' 1
end
p result
display "[`result'] [`alpha']"

StataMP 18 prints [] [1] because token (short for tokenize) resets `1' to alpha. Sight only recognized the full word tokenize, so it treated result as defined. It now accepts token, tokeni, tokeniz and tokenize. The minimum of 5 characters comes from the command database, and a test checks it against the cache.

2. A program the caller never loads still defined the caller's locals. Suppose library.do defines review_setter (args name, then c_local `name' 1) and main.do calls review_setter result without running library.do. Main reported 0 issues, while v0.15.1 warned. The positional c_local support from #381 looked the program up anywhere in the workspace index. That breaks the contract that workspace symbols alone don't suppress undefined-macro diagnostics. The same happened when the program was defined later in the same file than a top-level call.

A call now picks up a program's positional c_local names only if Stata could reach the program at that call:

The provider's resolved-scope fallback (collect_c_local_arg_calls) already handles programs from a parent that ran before this file. It now also skips a top-level call to a program that the same file defines later.

On #381's intent: the docs said this "works for programs in the same file, in other workspace files, and on the ado-path". #381's tests and corpus only use .ado setters, and no test pins an unrun workspace .do. I read the sentence as covering files the caller can reach and narrowed it. configuration.md and cross-file.md now say so.

Decisions and limits:

  • Forward calls are matched by file name only, because the analyzer doesn't resolve paths. A path built from a macro never matches.
  • One case main handled now warns, as it did in v0.15.1. If main.do runs setup.do and setup.do runs library.do, a positional c_local from library.do warns in main.do. The analyzer can't see the calls inside setup.do, and the provider fallback reads only resolved_scope.symbols, not the symbols of forward call sites. That fix belongs in src/providers/diagnostics.ts, which this PR doesn't touch. It would make a good follow-up.
  • Literal c_local name and local()-option lookups still search the whole workspace. That is unchanged from v0.15.1, where an unrun library.do containing c_local result 1 is also silent.

3. Opening a file with many abbreviated variable references was quadratic. is_variable_defined scanned every known variable for each abbreviated reference. A prefix index (src/analyzer/name-prefix-index.ts) now replaces the scan and is updated as variables are added. It maps each prefix to the earliest line where it is visible, so sight: variables declarations still apply only from their line forward. Here is the median DocumentStore.open time for N=8000 (gen vI_long = 1 for each I, then sum vI_ for each I, either grouped or interleaved):

grouped interleaved
main 234 ms 219 ms
this PR 28–32 ms 28 ms
v0.15.1 21 ms 17 ms

4. g year = 1 did not define year. Every spelling of generate down to g now defines the variable. input (inp, inpu), rename (rena, renam) and confirm (conf through confir) had the same gap and are fixed too. The minimums come from the command database, and a test checks them. egen has no shorter form, and replace doesn't create variables.

Tests

  • Item 1: each spelling counts, and toke and Token don't.
  • Item 2: unit tests for an unrun .do, an ado with the wrong name, a later program in the same file, an earlier one, a later one called from a program body, forward calls (run library, do "sub/library.do", include), a call before the run, and the provider's ordering. Three sight check CLI tests cover an unrun file, run library.do, and a later definition.
  • Item 3: a fast-check property checks that the index always agrees with a startsWith scan, including the line rule and changes the index isn't told about. A counting test checks that the map is iterated a constant number of times across 2,000 interleaved adds and lookups. A growth bound checks that 8x the lines takes under 24x the time, using the best of 5 runs per size. The old code measured about 50x. The new code measured at most 11.7x over 60 samples and 11.1x while the full suite ran.
  • Item 4: every spelling and storage types (g byte, ge double) define the variable, and shorter or capitalized forms don't.

Proof: 18 of the new tests fail on origin/main. sight check on each repro, before and after:

  • Item 1 reported only alpha and now also reports result.
  • Item 2 had 0 issues and now reports result. With the program defined later in the same file, it also went from 0 issues to result. With run library.do first, it stays at 0 issues.
  • Item 4 reported year may not be defined and now has 0 issues.

All four gates pass after rebasing on #412, each run on its own: check:line-endings, typecheck, bun test ./tests (9,937 pass, 0 fail), and lint. I did the review pass myself rather than with an outside reviewer, because this task said not to start subagents.

Created with Claude Opus 5.5 in T3 Code (Claude Code harness).

…accept g as generate, and check abbreviated variables in linear time

- A program that runs `token` (or tokeni, tokeniz) before c_local `1'
  no longer credits the caller's argument: `token` resets the numbered
  locals, as tokenize does (min abbreviation 5 in the command database).
- A call credits a program's positional c_local names only when Stata
  could reach the program at the call: defined earlier in the file
  (anywhere in it, from inside a program body), an ado named after the
  program, or a file the caller ran or included earlier. A program in
  an unrelated workspace .do file, or defined later in the same file
  than a top-level call, no longer suppresses the undefined warning.
- generate, input, rename and confirm define variables in every
  spelling Stata accepts (g, inp, ren, conf and up).
- Abbreviated variable references are checked through a prefix index
  instead of a scan of every known variable per reference.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 19, 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 44 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 86 included PR review attempts over the past 7 days set your current allowance at 1 review 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: 92ccb705-74fd-449d-9910-651a88074b6a
Commits

Reviewing files that changed from the base of the PR and between a9fdfbc and 2409947.

Files selected for processing: 12

  • docs/configuration.md
  • docs/cross-file.md
  • docs/diagnostics.md
  • src/analyzer/c-local-arg-positions.ts
  • src/analyzer/command-macro-writes.ts
  • src/analyzer/index.ts
  • src/analyzer/name-prefix-index.ts
  • tests/integration/c-local-arg-positions-cli.test.ts
  • tests/property/name-prefix-index.property.test.ts
  • tests/unit/c-local-arg-positions.test.ts
  • tests/unit/undefined-variable-check.test.ts
  • tests/unit/undefined-variable-scaling.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@jbearak
jbearak merged commit 49213d6 into main Nov 19, 2026
16 checks passed
@jbearak
jbearak deleted the t3/fix-analyzer-macro-and-variable-analysis branch November 19, 2026 14:44
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