Skip to content

Credit c_local arguments from programs that files loaded at any depth define, keep them after a later tokenize, and stop input types from defining variables - #423

Merged
jbearak merged 1 commit into
mainfrom
t3/fix-analyzer-regressions
Nov 19, 2026

Conversation

@jbearak

@jbearak jbearak commented Nov 19, 2026

Copy link
Copy Markdown
Owner

#415 merged without independent review, and a cold review then found problems in it. The biggest one: valid projects got a false `result' is not defined whenever the program that c_locals an argument came from a file loaded through another file or a sight: directive. Its reachability check compared file names against the caller's own do/run lines. This PR fixes that and the other findings.

Positional c_local credit now uses the resolved execution scope. I chose the scope-based design over the fallback. The diagnostics provider's positional fallback now reads the forward call sites the scope resolver already builds. Those sites are transitive and resolved by path, and they include sight: directive sites. It walks statements in execution order, and each per-name lookup is built once. The analyzer keeps only what one file shows on its own: same-file definitions, program drop, and an ado named after the program. Its file-name matching is removed. The workspace index now keeps <name>.ado when several files define a program, because that is the file Stata loads.

These now report 0 issues (they reported 1 on main):

  • nestedrun, nesteddo, nestedinclude: main loads setup.do, which loads library.do
  • directive20, directive21 (and directive): // sight: do|include|run library.do
  • collideado: setter.ado beside alib.do (always 1 on main in 3 runs)
  • collideboth, selectcollision: the file actually run wins
  • bodylaterload: a call in a program body, with the library loaded before the program is called
  • adolater: an ado named after the program, with a later same-file definition (main gave 0 or 1 depending on index order)

These now warn (main reported 0):

  • basename: run a/lib.do with the setter only in b/lib.do
  • dropped: call after program drop setter
  • directnestedif, nesteddefinition: call before a definition inside if 1 { }

These are unchanged: a3 (never loaded) and a later top-level definition still warn. statementorder (run library.do; setter result; on one #delimit ; line) is still 0. capture program drop setter followed by a definition still credits.

A reset after the c_local no longer erases it (laterToken: 1 → 0). The check is now order-aware. A tokenize, macro shift, local 1 or gettoken into 1 blocks credit only for a c_local that runs after it on the first pass. Each if/else branch starts from the state before the chain.

input no longer defines its storage types as variables (inptyped, inputtype: 0 → 1 with undefinedVariable = "warning"). The full spelling input had the same bug back to v0.15.1, so the fix covers every spelling. A table test checks that every abbreviation of generate, input, rename and confirm gives the same result as the full word.

Quadratic work removed

  • Setter calls no longer rescan earlier forward calls: path splits for 100/200/400 loads and calls went from 10,000/40,000/160,000 to 0. The new provider walk reads each loaded file's program map at most twice, which a count test checks.
  • The rename wildcard check reads only the tokens on the name's line, found by binary search: token visits for 100/200/400 renames went from 82,012/324,012/1,288,012 to 812/1,612/3,212. This covers both rena and rename.
  • The variable prefix index now lasts for one analysis instead of sitting in a module cache keyed by map size. The prefix.expr probe's second read used to return a stale answer and now returns the right one.

Trade-offs and limits

  • When a program is reached only through another loaded file, the warning is suppressed but no symbol is registered. Go-to-definition, references, and the loop expander's poisoning no longer see that argument word. Same-file and ado programs still register it. docs/configuration.md says this.
  • input byte x:lab y still drops y. That is a parser limitation that predates this PR.
  • In the LSP, editing only a callee's c_local does not revalidate the caller. The resolver's interface hash keys on program names; this is also true on main.
  • The walk-count and prefix-index tests use the new APIs, so on main they fail at import. The "before" numbers above come from the review's probes.

Proof: each fixture above was checked 3× before and after. Regression tests fail on main (18 in the new unit and CLI tests, 2 in the input tests). StataMP 18 confirmed laterToken and its loop variant (result set), inp double wage str10 city (only wage and city), and program drop (rc 199, local unset). check:line-endings, typecheck, bun test ./tests (10157 pass), lint and test:ci all pass. One earlier full run hit a random-seed parser property failure (di _ = 1). The same input fails on main, and that test file passed on 3 reruns.

Created with Claude Opus 5.5 in Claude Code.

… define, keep them after a later tokenize, and stop input types from defining variables

- A call credits a program's positional c_local names when the
  resolved execution scope defines the program at the call: files run,
  done or included at any depth, sight: do/run/include directives, the
  file actually run (resolved by path, not file name), an ado named
  after the program, and, from a program body, a file loaded anywhere
  in the file. A top-level program drop, a later or nested later
  definition, and a same-named file in another directory no longer
  credit it.
- A tokenize, macro shift or local 1 after `c_local `1'' ran no longer
  erases the credit; if/else branches start from the state before them.
- input (every spelling) no longer registers storage types such as
  double or str10 as variables.
- Rename wildcard checks visit only the name's line; the variable
  prefix index lives for one analysis, so a changed declared-variable
  map is never answered from a stale index.

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 59 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: 53584709-d617-4d51-82d4-c5efc58c6b97
Commits

Reviewing files that changed from the base of the PR and between 1c22bfb and f207e3b.

Files selected for processing: 14

  • 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
  • src/indexer/index.ts
  • src/providers/diagnostics.ts
  • tests/integration/c-local-arg-reachability-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

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