Skip to content

Commit 4bb4367

Browse files
authored
Merge pull request #2939 from ksss/auto-generate-ruby-annotations-union
Auto-generate rbs_ast_ruby_annotations_t union from config.yml
2 parents ea2ed8c + 093110e commit 4bb4367

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

include/rbs/ast.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,12 +941,20 @@ typedef struct rbs_types_variable {
941941

942942
typedef union rbs_ast_ruby_annotations {
943943
rbs_node_t base;
944+
rbs_ast_ruby_annotations_block_param_type_annotation_t block_param_type_annotation;
945+
rbs_ast_ruby_annotations_class_alias_annotation_t class_alias_annotation;
944946
rbs_ast_ruby_annotations_colon_method_type_annotation_t colon_method_type_annotation;
947+
rbs_ast_ruby_annotations_double_splat_param_type_annotation_t double_splat_param_type_annotation;
948+
rbs_ast_ruby_annotations_instance_variable_annotation_t instance_variable_annotation;
945949
rbs_ast_ruby_annotations_method_types_annotation_t method_types_annotation;
950+
rbs_ast_ruby_annotations_module_alias_annotation_t module_alias_annotation;
951+
rbs_ast_ruby_annotations_module_self_annotation_t module_self_annotation;
946952
rbs_ast_ruby_annotations_node_type_assertion_t node_type_assertion;
953+
rbs_ast_ruby_annotations_param_type_annotation_t param_type_annotation;
947954
rbs_ast_ruby_annotations_return_type_annotation_t return_type_annotation;
948955
rbs_ast_ruby_annotations_skip_annotation_t skip_annotation;
949-
rbs_ast_ruby_annotations_param_type_annotation_t param_type_annotation;
956+
rbs_ast_ruby_annotations_splat_param_type_annotation_t splat_param_type_annotation;
957+
rbs_ast_ruby_annotations_type_application_annotation_t type_application_annotation;
950958
} rbs_ast_ruby_annotations_t;
951959

952960
/// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc.

rust/ruby-rbs-sys/build.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,10 @@ fn generate_bindings(include_path: &Path) -> Result<bindgen::Bindings, Box<dyn E
103103
.allowlist_type("rbs_ast_members_prepend_t")
104104
.allowlist_type("rbs_ast_members_private_t")
105105
.allowlist_type("rbs_ast_members_public_t")
106-
.allowlist_type("rbs_ast_ruby_annotations_class_alias_annotation_t")
107-
.allowlist_type("rbs_ast_ruby_annotations_colon_method_type_annotation_t")
108-
.allowlist_type("rbs_ast_ruby_annotations_instance_variable_annotation_t")
109-
.allowlist_type("rbs_ast_ruby_annotations_method_types_annotation_t")
110-
.allowlist_type("rbs_ast_ruby_annotations_module_alias_annotation_t")
111-
.allowlist_type("rbs_ast_ruby_annotations_node_type_assertion_t")
112-
.allowlist_type("rbs_ast_ruby_annotations_return_type_annotation_t")
113-
.allowlist_type("rbs_ast_ruby_annotations_skip_annotation_t")
114-
.allowlist_type("rbs_ast_ruby_annotations_type_application_annotation_t")
115-
.allowlist_type("rbs_ast_ruby_annotations_block_param_type_annotation_t")
116-
.allowlist_type("rbs_ast_ruby_annotations_param_type_annotation_t")
117-
.allowlist_type("rbs_ast_ruby_annotations_splat_param_type_annotation_t")
118-
.allowlist_type("rbs_ast_ruby_annotations_double_splat_param_type_annotation_t")
106+
// Match every `rbs_ast_ruby_annotations_*` struct (and the union itself)
107+
// so newly added annotation kinds do not require edits here. This keeps the
108+
// Rust bindings in sync with `config.yml` automatically.
109+
.allowlist_type("rbs_ast_ruby_annotations_.*")
119110
.allowlist_type("rbs_ast_string_t")
120111
.allowlist_type("rbs_ast_symbol_t")
121112
.allowlist_type("rbs_ast_type_param_t")

templates/include/rbs/ast.h.erb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,9 @@ typedef struct <%= node.c_name %> {
108108
<%- end -%>
109109
typedef union rbs_ast_ruby_annotations {
110110
rbs_node_t base;
111-
rbs_ast_ruby_annotations_colon_method_type_annotation_t colon_method_type_annotation;
112-
rbs_ast_ruby_annotations_method_types_annotation_t method_types_annotation;
113-
rbs_ast_ruby_annotations_node_type_assertion_t node_type_assertion;
114-
rbs_ast_ruby_annotations_return_type_annotation_t return_type_annotation;
115-
rbs_ast_ruby_annotations_skip_annotation_t skip_annotation;
116-
rbs_ast_ruby_annotations_param_type_annotation_t param_type_annotation;
111+
<%- annotation_nodes.each do |node| -%>
112+
<%= node.c_type_name %> <%= node.c_name.delete_prefix("rbs_ast_ruby_annotations_") %>;
113+
<%- end -%>
117114
} rbs_ast_ruby_annotations_t;
118115

119116
/// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc.

templates/template.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,16 @@ def locals
385385
Node.new(node, fields, locations, constructor_params)
386386
end
387387

388+
sorted_nodes = nodes.sort_by { _1.descr.ruby_full_name }
389+
390+
annotation_nodes = sorted_nodes
391+
.select { _1.descr.ruby_full_name.start_with?("RBS::AST::Ruby::Annotations::") }
392+
.sort_by(&:c_name)
393+
388394
{
389-
nodes: nodes.sort_by { _1.descr.ruby_full_name },
390-
enums: enum_desc
395+
nodes: sorted_nodes,
396+
enums: enum_desc,
397+
annotation_nodes: annotation_nodes,
391398
}
392399
end
393400
end

0 commit comments

Comments
 (0)