Fix type inference for parent scopes and method definitions with receivers#4062
Fix type inference for parent scopes and method definitions with receivers#4062vinistock wants to merge 3 commits intorubydex_adoption_feature_branchfrom
Conversation
93eacb0 to
338eeee
Compare
9bf9d19 to
9ca38db
Compare
338eeee to
4c1f108
Compare
328b3db to
d1435ac
Compare
4c1f108 to
9846ab4
Compare
| assert_equal(6, response[0].range.start.line) | ||
| end | ||
| end | ||
|
|
There was a problem hiding this comment.
Should we add a few tests using extend?
There was a problem hiding this comment.
There's no logic in go to definition that's specific to extend and I've been trying to not mix Rubydex related tests with the request related tests.
For example, we already have tests to assert that we follow ancestors when searching for instance variable definitions. If that wasn't working for extend, that would be a bug in Rubydex and so I believe the test belongs there.
| when Prism::ConstantReadNode, Prism::ConstantPathNode | ||
| MethodDef.new(node.name.to_s, receiver.slice) | ||
| else | ||
| MethodDef.new(node.name.to_s, nil) |
There was a problem hiding this comment.
Does this mean we'll handle cases like def foo.bar; baz; end as if it didn't have a receiver?
There was a problem hiding this comment.
Yup. It's not correct, but I didn't think it was worth the complexity to add a special case to represent an unknown type of self.
Method definition receivers other than self are already not super common and dynamic ones are even more rare.
Motivation
In the original Ruby LSP indexer, we did not handle the resolution of parent scopes used in definitions. For example:
Similarly this example
We also did not handle method definition receivers correctly, which don't advance the lexical scope, but modify the type of
self. For example:Implementation
There are two main parts to fix, which can be found in the first commit:
def self.fooas a singleton entry in the node context nesting. Method receivers don't advance the lexical scope and do not influence constant resolution. They only influence the type ofselfThen I made the necessary adjustments on the type inferrer.
Automated Tests
The last commit includes a bunch of tests verifying that we're correctly handling these cases in definition and hover.