Skip to content

Rust: expand derive macros #19824

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

Merged
merged 9 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions misc/codegen/lib/schemadefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,16 @@ def modify(self, prop: _schema.Property):
def annotate(
annotated_cls: type,
add_bases: _Iterable[type] | None = None,
replace_bases: _Dict[type, type] | None = None,
replace_bases: _Dict[type, type | None] | None = None,
cfg: bool = False,
) -> _Callable[[type], _PropertyModifierList]:
"""
Add or modify schema annotations after a class has been defined previously.

The name of the class used for annotation must be `_`.

`replace_bases` can be used to replace bases on the annotated class.
`replace_bases` can be used to replace bases on the annotated class. Mapping to
`None` will remove that base class.
"""

def decorator(cls: type) -> _PropertyModifierList:
Expand All @@ -341,7 +342,9 @@ def decorator(cls: type) -> _PropertyModifierList:
_ClassPragma(p, value=v)(annotated_cls)
if replace_bases:
annotated_cls.__bases__ = tuple(
replace_bases.get(b, b) for b in annotated_cls.__bases__
b
for b in (replace_bases.get(b, b) for b in annotated_cls.__bases__)
if b is not None
)
if add_bases:
annotated_cls.__bases__ += tuple(add_bases)
Expand Down
14 changes: 14 additions & 0 deletions misc/codegen/templates/rust_classes.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ impl {{name}} {
pub fn emit_{{singular_field_name}}(id: trap::Label<Self>{{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i: usize{{/is_unordered}}{{/is_repeated}}, value: {{base_type}}{{/is_predicate}}, out: &mut trap::Writer) {
out.add_tuple("{{table_name}}", vec![id.into(){{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i.into(){{/is_unordered}}{{/is_repeated}}, value.into(){{/is_predicate}}]);
}

{{#is_repeated}}
pub fn emit_{{field_name}}(id: trap::Label<Self>, values: impl IntoIterator<Item={{base_type}}>, out: &mut trap::Writer) {
values
.into_iter()
{{^is_unordered}}
.enumerate()
.for_each(|(i, value)| Self::emit_{{singular_field_name}}(id, i, value, out));
{{/is_unordered}}
{{#is_unordered}}
.for_each(|value| Self::emit_{{singular_field_name}}(id, value, out));
{{/is_unordered}}
}
{{/is_repeated}}
{{/detached_fields}}
}
{{/has_detached_fields}}
Expand Down
8 changes: 4 additions & 4 deletions rust/ast-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ struct ExtractorInfo {
}

fn enum_to_extractor_info(node: &AstEnumSrc) -> Option<ExtractorEnumInfo> {
if node.name == "VariantDef" {
// currently defined but unused
if node.name == "Adt" {
// no fields have `Adt` type, so we don't need extraction for it
return None;
}
Some(ExtractorEnumInfo {
Expand Down Expand Up @@ -484,8 +484,8 @@ fn main() -> anyhow::Result<()> {
.parse()
.expect("Failed to parse grammar");
let mut grammar = codegen::grammar::lower(&grammar);

grammar.enums.retain(|x| x.name != "Adt");
// remove the VariantDef enum, there is no use for it at the moment
grammar.enums.retain(|e| e.name != "VariantDef");

let mut super_types: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
for node in &grammar.enums {
Expand Down
Loading
Loading