Fix edge case in 'findBindingAtPosition' when looking up global binding at start of file #1254
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.
The 'findBindingAtPosition' AstQuery function can be used to lookup a local or global binding.
Inside of this function is a check to "Ignore this binding if we're inside its definition. e.g. local abc = abc -- Will take the definition of abc from outer scope".
However, this check is incorrect when we are looking up a global binding at the start of a file.
Consider a complete file with the contents:
and we pass the location of the marker
|
as the position to the find binding position. We will pick up the global binding of the definitionstring
coming from a builtin source (either defined via C++ code or a definitions file and loaded into the global scope).The global binding
string
will have a zero position:0,0,0,0
. However, thefindBindingLocalStatement
check works by looking up the AstAncestry at the binding's defined begin position in the current source module. This will then incorrectly return the local statement forlocal x
, as that is at the start of the source code. Then in turn, we assume we are in thelocal abc = abc
case, and end up skipping over the correct binding.We fix this by checking if the binding is at the global position. If so, we early exit because it is impossible for a global binding to be defined in a local statement.