-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: refactor pre_emit! and post_emit! to a trait
#19851
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
Conversation
e15e81e to
8443aaa
Compare
8443aaa to
d0c7550
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Refactors the existing macro-based pre_emit!/post_emit! logic into a trait-driven approach, improving IDE support and reducing hidden macro magic. Key changes include:
- Introduce
HasTrapClassandEmission<T>traits inmappings.rsand implement them for all AST nodes inbase.rs. - Remove
pre_emit!/post_emit!macros and update theast-generatortemplates to emitself.pre_emit(node)andself.post_emit(node, label)calls. - Extend the AST generator to emit
HasTrapClassimpls and guard template sections with a newhas_special_emissionflag.
Reviewed Changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| rust/extractor/src/translate/mappings.rs | Add HasTrapClass and Emission traits |
| rust/extractor/src/translate/base.rs | Remove macros, implement Emission<T> for AST nodes, add emit_else_branch |
| rust/ast-generator/templates/{pre_emission,post_emission}.mustache | Generate trait-based pre/post emission calls |
| rust/ast-generator/templates/trap_class_mapping.mustache | Emit HasTrapClass impls for nodes with special emission |
| rust/ast-generator/templates/extractor.mustache | Remove macro imports, update imports and include new template partials |
| rust/ast-generator/src/main.rs | Add has_special_emission helper and propagate flag |
Comments suppressed due to low confidence (1)
rust/extractor/src/translate/mappings.rs:6
- [nitpick] Consider adding a doc comment to explain the purpose of the
HasTrapClasstrait and its associatedTrapClasstype for future maintainers.
pub(crate) trait HasTrapClass: AstNode {
| use crate::trap::{Label, TrapClass}; | ||
| use ra_ap_hir::{Enum, Function, HasContainer, Module, Semantics, Struct, Trait, Union}; | ||
| use ra_ap_ide_db::RootDatabase; | ||
| use ra_ap_syntax::{AstNode, ast, ast::RangeItem}; |
Copilot
AI
Jun 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import ast::RangeItem is unused in this file. Consider removing it to clean up unused dependencies.
| use ra_ap_syntax::{AstNode, ast, ast::RangeItem}; | |
| use ra_ap_syntax::{AstNode, ast}; |
This refactors the macro-based magic of
pre_emitandpost_emitto use a trait onTranslator, with the corresponding ast node as generic parameter. This needs to be explicitly requested for an AST node in theast-generatorcode (changing thehas_special_emissionfunction).This is safer, less error-prone, and nicer to the IDE.