Skip to content

Commit 23c449f

Browse files
authored
Merge pull request #19824 from github/redsun82/rust-derive-macro-expansion
Rust: expand derive macros
2 parents 25b337b + f4bdd4d commit 23c449f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+18425
-935
lines changed

misc/codegen/lib/schemadefs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,16 @@ def modify(self, prop: _schema.Property):
321321
def annotate(
322322
annotated_cls: type,
323323
add_bases: _Iterable[type] | None = None,
324-
replace_bases: _Dict[type, type] | None = None,
324+
replace_bases: _Dict[type, type | None] | None = None,
325325
cfg: bool = False,
326326
) -> _Callable[[type], _PropertyModifierList]:
327327
"""
328328
Add or modify schema annotations after a class has been defined previously.
329329
330330
The name of the class used for annotation must be `_`.
331331
332-
`replace_bases` can be used to replace bases on the annotated class.
332+
`replace_bases` can be used to replace bases on the annotated class. Mapping to
333+
`None` will remove that base class.
333334
"""
334335

335336
def decorator(cls: type) -> _PropertyModifierList:
@@ -341,7 +342,9 @@ def decorator(cls: type) -> _PropertyModifierList:
341342
_ClassPragma(p, value=v)(annotated_cls)
342343
if replace_bases:
343344
annotated_cls.__bases__ = tuple(
344-
replace_bases.get(b, b) for b in annotated_cls.__bases__
345+
b
346+
for b in (replace_bases.get(b, b) for b in annotated_cls.__bases__)
347+
if b is not None
345348
)
346349
if add_bases:
347350
annotated_cls.__bases__ += tuple(add_bases)

misc/codegen/templates/rust_classes.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ impl {{name}} {
6666
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) {
6767
out.add_tuple("{{table_name}}", vec![id.into(){{^is_predicate}}{{#is_repeated}}{{^is_unordered}}, i.into(){{/is_unordered}}{{/is_repeated}}, value.into(){{/is_predicate}}]);
6868
}
69+
70+
{{#is_repeated}}
71+
pub fn emit_{{field_name}}(id: trap::Label<Self>, values: impl IntoIterator<Item={{base_type}}>, out: &mut trap::Writer) {
72+
values
73+
.into_iter()
74+
{{^is_unordered}}
75+
.enumerate()
76+
.for_each(|(i, value)| Self::emit_{{singular_field_name}}(id, i, value, out));
77+
{{/is_unordered}}
78+
{{#is_unordered}}
79+
.for_each(|value| Self::emit_{{singular_field_name}}(id, value, out));
80+
{{/is_unordered}}
81+
}
82+
{{/is_repeated}}
6983
{{/detached_fields}}
7084
}
7185
{{/has_detached_fields}}

rust/ast-generator/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ struct ExtractorInfo {
385385
}
386386

387387
fn enum_to_extractor_info(node: &AstEnumSrc) -> Option<ExtractorEnumInfo> {
388-
if node.name == "VariantDef" {
389-
// currently defined but unused
388+
if node.name == "Adt" {
389+
// no fields have `Adt` type, so we don't need extraction for it
390390
return None;
391391
}
392392
Some(ExtractorEnumInfo {
@@ -484,8 +484,8 @@ fn main() -> anyhow::Result<()> {
484484
.parse()
485485
.expect("Failed to parse grammar");
486486
let mut grammar = codegen::grammar::lower(&grammar);
487-
488-
grammar.enums.retain(|x| x.name != "Adt");
487+
// remove the VariantDef enum, there is no use for it at the moment
488+
grammar.enums.retain(|e| e.name != "VariantDef");
489489

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

0 commit comments

Comments
 (0)