Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

redsun82
Copy link
Contributor

@redsun82 redsun82 commented Jun 23, 2025

This refactors the macro-based magic of pre_emit and post_emit to use a trait on Translator, with the corresponding ast node as generic parameter. This needs to be explicitly requested for an AST node in the ast-generator code (changing the has_special_emission function).

This is safer, less error-prone, and nicer to the IDE.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 23, 2025
Base automatically changed from redsun82/cargo-upgrade-3 to main June 23, 2025 15:43
@redsun82 redsun82 force-pushed the redsun82/rust-emission-trait branch from e15e81e to 8443aaa Compare June 24, 2025 07:19
@redsun82 redsun82 force-pushed the redsun82/rust-emission-trait branch from 8443aaa to d0c7550 Compare June 24, 2025 08:40
@redsun82 redsun82 marked this pull request as ready for review June 24, 2025 08:55
@Copilot Copilot AI review requested due to automatic review settings June 24, 2025 08:55
@redsun82 redsun82 requested a review from a team as a code owner June 24, 2025 08:55
@redsun82 redsun82 requested a review from aibaars June 24, 2025 08:55
Copy link
Contributor

@Copilot Copilot AI left a 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 HasTrapClass and Emission<T> traits in mappings.rs and implement them for all AST nodes in base.rs.
  • Remove pre_emit!/post_emit! macros and update the ast-generator templates to emit self.pre_emit(node) and self.post_emit(node, label) calls.
  • Extend the AST generator to emit HasTrapClass impls and guard template sections with a new has_special_emission flag.

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 HasTrapClass trait and its associated TrapClass type for future maintainers.
pub(crate) trait HasTrapClass: AstNode {

@@ -1,7 +1,20 @@
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};
Copy link
Preview

Copilot AI Jun 24, 2025

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.

Suggested change
use ra_ap_syntax::{AstNode, ast, ast::RangeItem};
use ra_ap_syntax::{AstNode, ast};

Copilot uses AI. Check for mistakes.

@@ -52,6 +52,32 @@ fn property_name(type_name: &str, field_name: &str) -> String {
name.to_owned()
}

fn has_special_emission(type_name: &str) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could also move this list into base.rs as a call to a macro that generates empty impl Emissions for the types that do not require special emission. That way you can keep the configuration out of the generator. Something like

no_special_emission!(AssocItem, ExternItem,...);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the absence of special emission is on the complementary set of that list, so it would need to explicitly list all nodes/enums except the ones listed here, which seems impractical.

We could move this entirely in base.rs if we had impl specialization and we could add a blanket implementation with special cases. I wouldn't hold my breath waiting for that to become stable anytime soon though (it's not seeing a lot of activity). We could use that with a nightly toolchain, but I'd avoid that.

@redsun82 redsun82 requested a review from aibaars June 24, 2025 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants