Rust: refactor pre_emit! and post_emit! to a trait#19851
Merged
Conversation
e15e81e to
8443aaa
Compare
8443aaa to
d0c7550
Compare
Contributor
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
The import ast::RangeItem is unused in this file. Consider removing it to clean up unused dependencies.
Suggested change
| use ra_ap_syntax::{AstNode, ast, ast::RangeItem}; | |
| use ra_ap_syntax::{AstNode, ast}; |
aibaars
reviewed
Jun 24, 2025
aibaars
approved these changes
Jun 25, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.