Repository navigation
Report locals set by an abbreviated tokenize or an unloaded program, accept g as generate, and check abbreviated variables in linear time - #415
Merged
Conversation
…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]>
Contributor
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. Next included review available in 44 minutes. View limit details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An audit of
v0.15.1..mainbefore 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.
tokenhid an undefined caller local.program define p token alpha beta c_local `1' 1 end p result display "[`result'] [`alpha']"StataMP 18 prints
[] [1]becausetoken(short fortokenize) resets`1'toalpha. Sight only recognized the full wordtokenize, so it treatedresultas defined. It now acceptstoken,tokeni,tokenizandtokenize. 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.dodefinesreview_setter(args name, thenc_local `name' 1) andmain.docallsreview_setter resultwithout runninglibrary.do. Main reported 0 issues, while v0.15.1 warned. The positionalc_localsupport 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_localnames only if Stata could reach the program at that call:_getrhs.ado), in the workspace or on the adopath. This is the case Stop reporting a local as undefined after a call like _getrhs rhs, where the program sets it with c_local from an argument #381 tested, and it still works.run library.do). The match is by file name.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
.adosetters, and no test pins an unrun workspace.do. I read the sentence as covering files the caller can reach and narrowed it.configuration.mdandcross-file.mdnow say so.Decisions and limits:
main.dorunssetup.doandsetup.dorunslibrary.do, a positionalc_localfromlibrary.dowarns inmain.do. The analyzer can't see the calls insidesetup.do, and the provider fallback reads onlyresolved_scope.symbols, not the symbols of forward call sites. That fix belongs insrc/providers/diagnostics.ts, which this PR doesn't touch. It would make a good follow-up.c_local nameandlocal()-option lookups still search the whole workspace. That is unchanged from v0.15.1, where an unrunlibrary.docontainingc_local result 1is also silent.3. Opening a file with many abbreviated variable references was quadratic.
is_variable_definedscanned 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, sosight: variablesdeclarations still apply only from their line forward. Here is the medianDocumentStore.opentime for N=8000 (gen vI_long = 1for each I, thensum vI_for each I, either grouped or interleaved):4.
g year = 1did not defineyear. Every spelling ofgeneratedown tognow defines the variable.input(inp,inpu),rename(rena,renam) andconfirm(confthroughconfir) had the same gap and are fixed too. The minimums come from the command database, and a test checks them.egenhas no shorter form, andreplacedoesn't create variables.Tests
tokeandTokendon't..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 therun, and the provider's ordering. Threesight checkCLI tests cover an unrun file,run library.do, and a later definition.startsWithscan, 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.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 checkon each repro, before and after:alphaand now also reportsresult.result. With the program defined later in the same file, it also went from 0 issues toresult. Withrun library.dofirst, it stays at 0 issues.year may not be definedand 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), andlint. 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).