github / stack-graphs

Rust implementation of stack graphs
https://docs.rs/stack-graphs/*/stack_graphs/
Apache License 2.0
717 stars 122 forks source link

Python TSG: Failing to parse function in function #404

Closed SubhamPramanik closed 1 month ago

SubhamPramanik commented 4 months ago

Python Code:

def something():
    def something_else():
       print('Hello World!')
something_else()

Failing Stanza:

(function_definition
  parameters: (parameters
    . (identifier) @param)
  body: (block) @body)
{
  edge @param.def -> @param.class_self_scope
  edge @param.class_member_attr_scope -> @param.output
  edge @param.output -> @body.after_scope
  attr (@param.output) push_node = @param
}
nohehf commented 1 month ago

I was not able to reproduce the above, however I ran into a similar issue, when the inner function has parameters: This is ok (indexing works):

# OK
def outer():
    def inner():
       pass

But this isn't:

# KO
def outer():
    def inner(x):
       pass

(notice the added x parameter to inner)

stacktrace:


[...]/repro.py: failed to build stack graph
    0: Error executing statement edge @param.def -> @param.class_self_scope at (799, 3)
       src/stack-graphs.tsg:799:3:
       799 |   edge @param.def -> @param.class_self_scope
           |   ^
       in stanza
       src/stack-graphs.tsg:794:1:
       794 | (function_definition
           | ^
       matching (function_definition) node
       [...]/repro.py:2:5:
       2 |     def inner(x):
         |     ^
    1: Evaluating edge sink
    2: Expected a graph node reference got #null

Error: failed to build stack graph

Failing stanza:

(function_definition
  parameters: (parameters
    . (identifier) @param)
  body: (block) @body)
{
  edge @param.def -> @param.class_self_scope
  edge @param.class_member_attr_scope -> @param.output
  edge @param.output -> @body.after_scope
  attr (@param.output) push_node = @param
}
nohehf commented 1 month ago

I believe @param.class_self_scope is unset in this situation

Edit: fyi I'm working on a fix, I will open a PR soon