Skip to content

Commit 29e34aa

Browse files
hvitvedCopilot
andcommitted
Rust: Remove parenthesized expression and pattern nodes
Extract parenthesized expressions and patterns as their inner entities, and update QL consumers and generated schema. Preserve attributes from parentheses and update query expectations for the resulting behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 915bfa8 commit 29e34aa

43 files changed

Lines changed: 199 additions & 879 deletions

Some content is hidden

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

‎rust/ast-generator/src/main.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ fn should_enum_be_skipped(name: &str) -> bool {
7676
fn should_node_be_skipped(name: &str) -> bool {
7777
name == "TypeAnchor" // we flatten TypeAnchor into PathSegment in the extractor
7878
|| name == "MacroStmts" // we workaround a getter bug in the extractor
79+
|| name == "ParenExpr" // parenthesized expressions are extracted as their inner expression
80+
|| name == "ParenPat" // parenthesized patterns are extracted as their inner pattern
7981
}
8082

8183
fn should_node_be_skipped_in_extractor(name: &str) -> bool {
@@ -349,6 +351,7 @@ struct EnumVariantInfo {
349351
name: String,
350352
snake_case_name: String,
351353
variant_ast_name: String,
354+
emission: String,
352355
}
353356

354357
#[derive(Serialize)]
@@ -365,6 +368,7 @@ struct ExtractorNodeFieldInfo {
365368
name: String,
366369
method: String,
367370
snake_case_ty: String,
371+
attributes: bool,
368372
string: bool,
369373
predicate: bool,
370374
optional: bool,
@@ -399,10 +403,16 @@ fn enum_to_extractor_info(node: &AstEnumSrc) -> ExtractorEnumInfo {
399403
.map(|v| {
400404
let name = class_name(v);
401405
let snake_case_name = to_lower_snake_case(v);
406+
let emission = match v.as_str() {
407+
"ParenExpr" => "self.emit_parenthesized_expr(inner)".to_owned(),
408+
"ParenPat" => "inner.pat().and_then(|x| self.emit_pat(&x))".to_owned(),
409+
_ => format!("self.emit_{snake_case_name}(inner).map(Into::into)"),
410+
};
402411
EnumVariantInfo {
403412
name,
404413
snake_case_name,
405414
variant_ast_name: v.to_string(),
415+
emission,
406416
}
407417
})
408418
.collect(),
@@ -412,6 +422,7 @@ fn enum_to_extractor_info(node: &AstEnumSrc) -> ExtractorEnumInfo {
412422

413423
fn field_info_to_extractor_info(name: &str, field: &FieldInfo) -> ExtractorNodeFieldInfo {
414424
let name = property_name(name, &field.name);
425+
let attributes = name == "attrs";
415426
match &field.ty {
416427
FieldType::String => ExtractorNodeFieldInfo {
417428
name,
@@ -440,6 +451,7 @@ fn field_info_to_extractor_info(name: &str, field: &FieldInfo) -> ExtractorNodeF
440451
},
441452
FieldType::List(ty) => ExtractorNodeFieldInfo {
442453
name,
454+
attributes,
443455
method: if ty == "AnyAttr" {
444456
"attrs_with_doc".to_owned()
445457
} else {

‎rust/ast-generator/templates/extractor.mustache‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Translator<'_> {
1818
{{>pre_emission}}
1919
let label = match node {
2020
{{#variants}}
21-
ast::{{ast_name}}::{{variant_ast_name}}(inner) => self.emit_{{snake_case_name}}(inner).map(Into::into),
21+
ast::{{ast_name}}::{{variant_ast_name}}(inner) => {{{emission}}},
2222
{{/variants}}
2323
}?;
2424
{{>post_emission}}
@@ -51,10 +51,20 @@ impl Translator<'_> {
5151
{{/body}}
5252
;
5353
{{/fields}}
54+
{{#fields}}
55+
{{#attributes}}
56+
let attrs = self.merge_parenthesized_expr_attrs(attrs);
57+
{{/attributes}}
58+
{{/fields}}
5459
let label = self.trap.emit(generated::{{name}} {
5560
id: TrapId::Star,
5661
{{#fields}}
62+
{{#attributes}}
63+
attrs,
64+
{{/attributes}}
65+
{{^attributes}}
5766
{{name}},
67+
{{/attributes}}
5868
{{/fields}}
5969
});
6070
self.emit_location(label, node);

‎rust/extractor/src/generated/.generated.list‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/extractor/src/generated/top.rs‎

Lines changed: 0 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/extractor/src/translate/base.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub struct Translator<'db> {
132132
pub semantics: Option<&'db Semantics<'db, RootDatabase>>,
133133
source_kind: SourceKind,
134134
pub(crate) macro_context_depth: usize,
135+
parenthesized_expr_attrs: Vec<Label<generated::AnyAttr>>,
135136
diagnostic_count: usize,
136137
/// When emitting a reconstructed built-in derive expansion, holds the span map of the
137138
/// synthesized syntax tree. Those nodes are not registered in the semantics cache, so
@@ -162,6 +163,7 @@ impl<'db> Translator<'db> {
162163
semantics: semantic_info.map(|i| i.semantics),
163164
source_kind,
164165
macro_context_depth: 0,
166+
parenthesized_expr_attrs: Vec::new(),
165167
diagnostic_count: 0,
166168
builtin_derive_span_map: None,
167169
}
@@ -523,6 +525,34 @@ impl<'db> Translator<'db> {
523525
})
524526
}
525527

528+
pub(crate) fn emit_parenthesized_expr(
529+
&mut self,
530+
node: &ast::ParenExpr,
531+
) -> Option<Label<generated::Expr>> {
532+
if self.should_be_excluded(node) {
533+
return None;
534+
}
535+
536+
let pending_attrs = self.parenthesized_expr_attrs.len();
537+
let attrs = node
538+
.attrs_with_doc()
539+
.filter_map(|attr| self.emit_any_attr(&attr))
540+
.collect::<Vec<_>>();
541+
self.parenthesized_expr_attrs.extend(attrs);
542+
let result = node.expr().and_then(|expr| self.emit_expr(&expr));
543+
self.parenthesized_expr_attrs.truncate(pending_attrs);
544+
result
545+
}
546+
547+
pub(crate) fn merge_parenthesized_expr_attrs(
548+
&mut self,
549+
mut attrs: Vec<Label<generated::AnyAttr>>,
550+
) -> Vec<Label<generated::AnyAttr>> {
551+
let mut parent_attrs = std::mem::take(&mut self.parenthesized_expr_attrs);
552+
parent_attrs.append(&mut attrs);
553+
parent_attrs
554+
}
555+
526556
pub(crate) fn should_skip_bodies(&self) -> bool {
527557
self.source_kind == SourceKind::Library
528558
}

0 commit comments

Comments
 (0)