Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] fn $FUNC($$$ARGS) { $$$ } does not match the function item of Rust #1056

Open
kdy1 opened this issue Apr 16, 2024 · 1 comment
Open
Labels
question Further information is requested 👑Sponsor👑 Request from Sponsors!

Comments

@kdy1
Copy link

kdy1 commented Apr 16, 2024

⏯ Playground Link

Playground link with relevant code

💻 Code

Code:

#[turbo_tasks::function]
async fn read(name: String) -> Result<Vc<Option<String>>> {
    
}

Pattern:

id: update-function-parameters
language: rust
rule:
  pattern: 'fn $FUNC($$$ARGS) { $$$ }'

or pattern = fn $FUNC($$$ARGS) { $$$ } in the playground

🙁 Actual behavior

It didn't match any function.

🙂 Expected behavior

It should match all functions

@kdy1 kdy1 added the bug Something isn't working label Apr 16, 2024
@HerringtonDarkholme HerringtonDarkholme added the 👑Sponsor👑 Request from Sponsors! label Apr 16, 2024
@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Apr 16, 2024

Thanks for the report.

There are two things in the repro:

  1. fn $FUNC($$$ARGS) { $$$ } does not have a return type. So it does not match the fn read(name: String) -> Result<Vc<Option<String>>> because the latter has a return type. This is expected.

I recommend using any to combine these two cases.

id: update-function-parameters
language: rust
rule:
  any:
  - pattern: 'fn $FUNC($$$ARGS) { $$$ }'
  - pattern: 'fn $FUNC($$$ARGS) -> $RET { $$$ }'
  1. However, the above YAML still does not match async function. This is also expected. Because async is an AST node function_modifiers, the pattern query does not specify the keyword.
    See the AST dump
image

This is the work around to avoid matching return type or async modifiers

id: update-function-parameters
language: rust
rule:
  kind: function_item
  has:
    kind: parameters
    pattern: $PARAMS
  

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InJ1c3QiLCJxdWVyeSI6ImZuICRGVU5DKCQkJEFSR1MpIC0+ICRSRVQgeyAgJCQkIH0iLCJyZXdyaXRlIjoiIiwiY29uZmlnIjoiaWQ6IHVwZGF0ZS1mdW5jdGlvbi1wYXJhbWV0ZXJzXG5sYW5ndWFnZTogcnVzdFxucnVsZTpcbiAga2luZDogZnVuY3Rpb25faXRlbVxuICBoYXM6XG4gICAga2luZDogcGFyYW1ldGVyc1xuICAgIHBhdHRlcm46ICRQQVJBTVNcbiAgIiwic291cmNlIjoiI1t0dXJib190YXNrczo6ZnVuY3Rpb25dXG5hc3luYyBmbiByZWFkKG5hbWU6IFN0cmluZykgLT4gUmVzdWx0PFZjPE9wdGlvbjxTdHJpbmc+Pj4ge1xuICAgIFxufVxuIn0=

@HerringtonDarkholme HerringtonDarkholme added question Further information is requested and removed bug Something isn't working labels Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested 👑Sponsor👑 Request from Sponsors!
Projects
None yet
Development

No branches or pull requests

2 participants