Skip to content

Rust support for ellipses inside struct initialization and use lists#563

Merged
Fidget-Grep merged 2 commits intomainfrom
02-12-rust_support_for_ellipses_inside_structs
Feb 13, 2026
Merged

Rust support for ellipses inside struct initialization and use lists#563
Fidget-Grep merged 2 commits intomainfrom
02-12-rust_support_for_ellipses_inside_structs

Conversation

@Fidget-Grep
Copy link
Contributor

@Fidget-Grep Fidget-Grep commented Feb 12, 2026

Added two new grammar rules. One for field_initializers of struct initialization, giving them an option to include ellipsis. One for _use_clauses of use statements, also giving them the options of being an ellipsis.

Checklist

  • Any new parsing code was already published, integrated, and merged into Semgrep. DO NOT MERGE THIS PR BEFORE THE SEMGREP INTEGRATION WORK WAS COMPLETED.
  • Change has no security implications (otherwise, ping the security team)

NOTES:
For anyone completely new to contributing to the Semgrep Ocaml repo (like me), here's what I learned:

  • Here's how to update a language grammar (high level): https://semgrep.dev/docs/contributing/updating-a-grammar
  • This Ocaml engine (tree-sitter) takes Rust code and/or Semgrep patterns and turns them into CSTs (concrete syntax trees). In the test directory for each language, you can see this happening. In this PR I added import.rs and struct.rs code snippets, and when you run ./test-lang rust from the lang directory, the engine creates CST files import.rs.cst and struct.rs.cst.
  • All the tests for each language are in the test/corpus directory for each grammar (semgrep-rust in our case). The tests are stored in .txt files with titles and headers. Each test contains both an example Semgrep pattern and an AST (abstract syntax tree). When the test is run, the engine will turn the Semgrep pattern into a CST and then an AST, and then will check it against the provided AST for correctness.
    • Generally, when Semgrep runs, it compares the AST of your Semgrep pattern to the AST of your Rust code. So, if your pattern isn't flagging code you expect it to flag, it's probably because the ASTs differ.
  • Finally, the real grammar changes happen in grammar.js. The Semgrep grammar builds on top of an existing grammar (from the tree-sitter project), which is also defined in a grammar.js. The grammar file I'm changing in this PR builds on top of the grammar.js file inside the tree-sitter-rust submodule, and I referred to that as a reference to see how the grammar is defined and overwritten. Here's what my two additions are actually saying:
    • The token field_initializer can be one of two things: the exact same field_initializer defined in tree-sitter-rust/grammar.js, or an ellipsis.
    • The token _use_clause can be any of the original options defined in _use_clause in the original tree-sitter-rust/grammar.js, or an ellipsis.
  • For most small grammar changes, these are the only two files you need to change. grammar.js to change/expand the grammar, and semgrep.txt to add new tests. Refer to these two files in other languages for examples on how to do what you want to do.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@Fidget-Grep Fidget-Grep changed the title Rust support for ellipses inside structs Rust support for ellipses inside struct initialization and use lists Feb 13, 2026
Copy link
Collaborator

@nmote nmote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

@Fidget-Grep Fidget-Grep marked this pull request as ready for review February 13, 2026 21:42
@Fidget-Grep Fidget-Grep requested a review from a team as a code owner February 13, 2026 21:42
Copy link
Contributor Author

Fidget-Grep commented Feb 13, 2026

Merge activity

  • Feb 13, 9:49 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Feb 13, 9:50 PM UTC: Graphite couldn't merge this PR because it failed for an unknown reason (Merge commits are not allowed on this repository).

@Fidget-Grep Fidget-Grep merged commit cb0541f into main Feb 13, 2026
32 of 47 checks passed
@Fidget-Grep Fidget-Grep deleted the 02-12-rust_support_for_ellipses_inside_structs branch February 13, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants