Skip to content

Commit 66f88d3

Browse files
committed
Rust: fix PathSegment workaround
1 parent 9430c8a commit 66f88d3

File tree

17 files changed

+30
-232
lines changed

17 files changed

+30
-232
lines changed

rust/ast-generator/src/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,9 @@ fn write_schema(
155155
.iter()
156156
.map(|node| enum_src_to_schema_class(node, &super_types)),
157157
);
158-
schema.classes.extend(
159-
grammar
160-
.nodes
161-
.iter()
162-
.map(|node| node_src_to_schema_class(node, &super_types)),
163-
);
158+
schema
159+
.classes
160+
.extend(get_nodes(grammar).map(|node| node_src_to_schema_class(node, &super_types)));
164161
let template = mustache_ctx.compile_path("schema")?;
165162
let res = template.render_to_string(&schema)?;
166163
Ok(fix_blank_lines(&res))
@@ -295,7 +292,7 @@ fn get_fields(node: &AstNodeSrc) -> Vec<FieldInfo> {
295292
let name = field.method_name();
296293
match (node.name.as_str(), name.as_str()) {
297294
("ArrayExpr", "expr") // The ArrayExpr type also has an 'exprs' field
298-
| ("PathSegment", "ty" | "path_type") // these are broken, handling them manually
295+
| ("PathSegment", "type_anchor") // we flatten TypeAnchor into PathSegment in the extractor
299296
| ("Param", "pat") | ("MacroCall", "token_tree") // handled manually to use `body`
300297
=> continue,
301298
_ => {}
@@ -384,6 +381,11 @@ struct ExtractorInfo {
384381
nodes: Vec<ExtractorNodeInfo>,
385382
}
386383

384+
fn get_nodes(grammar: &AstSrc) -> impl Iterator<Item = &AstNodeSrc> {
385+
// we flatten TypeAnchor into PathSegment in the extractor
386+
grammar.nodes.iter().filter(|n| n.name != "TypeAnchor")
387+
}
388+
387389
fn enum_to_extractor_info(node: &AstEnumSrc) -> Option<ExtractorEnumInfo> {
388390
if node.name == "VariantDef" {
389391
// currently defined but unused
@@ -470,7 +472,7 @@ fn write_extractor(grammar: &AstSrc, mustache_ctx: &mustache::Context) -> mustac
470472
.iter()
471473
.filter_map(enum_to_extractor_info)
472474
.collect(),
473-
nodes: grammar.nodes.iter().map(node_to_extractor_info).collect(),
475+
nodes: get_nodes(grammar).map(node_to_extractor_info).collect(),
474476
};
475477
let template = mustache_ctx.compile_path("extractor")?;
476478
let res = template.render_to_string(&extractor_info)?;

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 & 58 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,11 @@ impl<'a> Translator<'a> {
667667
) {
668668
// work around a bug in rust-analyzer AST generation machinery
669669
// this code was inspired by rust-analyzer's own workaround for this:
670-
// https://github.com/rust-lang/rust-analyzer/blob/1f86729f29ea50e8491a1516422df4fd3d1277b0/crates/syntax/src/ast/node_ext.rs#L268-L277
671-
if item.l_angle_token().is_some() {
670+
// https://github.com/rust-lang/rust-analyzer/blob/a642aa8023be11d6bc027fc6a68c71c2f3fc7f72/crates/syntax/src/ast/node_ext.rs#L290-L297
671+
if let Some(anchor) = item.type_anchor() {
672672
// <T> or <T as Trait>
673673
// T is any TypeRef, Trait has to be a PathType
674-
let mut type_refs = item
674+
let mut type_refs = anchor
675675
.syntax()
676676
.children()
677677
.filter(|node| ast::Type::can_cast(node.kind()));

rust/extractor/src/translate/generated.rs

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

0 commit comments

Comments
 (0)