From e983eb3a255f137c9bd9de44c5d2953d78a8ea34 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 18 Nov 2026 12:50:01 -0500 Subject: [PATCH] Introduce flag for BEGIN in statements You can avoid a separate walk if you know that the program statements do not have a BEGIN in them. --- config.yml | 6 ++++++ rbi/generated/prism/dot_visitor.rbi | 5 +++++ rbi/generated/prism/dsl.rbi | 4 ++++ rbi/generated/prism/node.rbi | 10 ++++++++++ rust/ruby-prism-sys/src/bindings.rs | 19 +++++++++++++++++++ sig/generated/prism/dot_visitor.rbs | 6 ++++++ sig/generated/prism/dsl.rbs | 5 +++++ sig/generated/prism/node.rbs | 12 ++++++++++++ snapshots/nils.txt | 2 +- snapshots/seattlerb/BEGIN.txt | 2 +- snapshots/seattlerb/parse_line_preexe.txt | 2 +- snapshots/unparser/corpus/literal/hookexe.txt | 2 +- snapshots/whitequark/preexe.txt | 2 +- src/prism.c | 9 +++++++-- 14 files changed, 79 insertions(+), 7 deletions(-) diff --git a/config.yml b/config.yml index b0ad271a11..d9f4d33106 100644 --- a/config.yml +++ b/config.yml @@ -786,6 +786,11 @@ flags: - name: EXPERIMENTAL_COPY comment: "constant writes that should be modified with shareable constant value experimental copy" comment: Flags for shareable constant nodes. + - name: StatementsFlags + values: + - name: CONTAINS_PRE_EXECUTION + comment: "the statement list directly contains a `BEGIN` block" + comment: Flags for statement lists. - name: StringFlags values: - name: FORCED_UTF8_ENCODING @@ -4491,6 +4496,7 @@ nodes: [*a] ^^ - name: StatementsNode + flags: StatementsFlags fields: - name: body type: node[] diff --git a/rbi/generated/prism/dot_visitor.rbi b/rbi/generated/prism/dot_visitor.rbi index 3de4ec0f50..b369b190dd 100644 --- a/rbi/generated/prism/dot_visitor.rbi +++ b/rbi/generated/prism/dot_visitor.rbi @@ -605,6 +605,11 @@ module Prism sig { params(node: ShareableConstantNode).returns(String) } private def shareable_constant_node_flags_inspect(node); end + # Inspect a node that has statements_flags flags to display the flags as a + # comma-separated list. + sig { params(node: StatementsNode).returns(String) } + private def statements_flags_inspect(node); end + # Inspect a node that has string_flags flags to display the flags as a # comma-separated list. sig { params(node: ::T.any(SourceFileNode, StringNode)).returns(String) } diff --git a/rbi/generated/prism/dsl.rbi b/rbi/generated/prism/dsl.rbi index b7ccbb365b..b8f76d08a7 100644 --- a/rbi/generated/prism/dsl.rbi +++ b/rbi/generated/prism/dsl.rbi @@ -721,6 +721,10 @@ module Prism sig { params(name: Symbol).returns(Integer) } def shareable_constant_node_flag(name); end + # Retrieve the value of one of the StatementsFlags flags. + sig { params(name: Symbol).returns(Integer) } + def statements_flag(name); end + # Retrieve the value of one of the StringFlags flags. sig { params(name: Symbol).returns(Integer) } def string_flag(name); end diff --git a/rbi/generated/prism/node.rbi b/rbi/generated/prism/node.rbi index 0e6a1547a5..bc14b8f2a0 100644 --- a/rbi/generated/prism/node.rbi +++ b/rbi/generated/prism/node.rbi @@ -13808,6 +13808,10 @@ module Prism sig { override.returns(String) } def inspect; end + # the statement list directly contains a `BEGIN` block + sig { returns(T::Boolean) } + def contains_pre_execution?; end + # Returns the `body` attribute. sig { returns(T::Array[Node]) } def body; end @@ -15103,6 +15107,12 @@ module Prism EXPERIMENTAL_COPY = T.let(nil, ::T.untyped) end + # Flags for statement lists. + module StatementsFlags + # the statement list directly contains a `BEGIN` block + CONTAINS_PRE_EXECUTION = T.let(nil, ::T.untyped) + end + # Flags for string nodes. module StringFlags # internal bytes forced the encoding to UTF-8 diff --git a/rust/ruby-prism-sys/src/bindings.rs b/rust/ruby-prism-sys/src/bindings.rs index 5bf9a50bf7..1aa7b1415c 100644 --- a/rust/ruby-prism-sys/src/bindings.rs +++ b/rust/ruby-prism-sys/src/bindings.rs @@ -2366,6 +2366,9 @@ pub type pm_splat_node_t = pm_splat_node; Type: ::PM_STATEMENTS_NODE + Flags (#pm_statements_flags): + * ::PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION + @extends pm_node_t */ pub type pm_statements_node_t = pm_statements_node; @@ -9108,6 +9111,9 @@ pub struct pm_splat_node { Type: ::PM_STATEMENTS_NODE + Flags (#pm_statements_flags): + * ::PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION + @extends pm_node_t */ #[repr(C)] @@ -10415,6 +10421,19 @@ pub enum pm_shareable_constant_node_flags { } #[repr(u32)] #[non_exhaustive] +/** Flags for statement lists. +*/ +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum pm_statements_flags { + /** the statement list directly contains a `BEGIN` block +*/ + PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION = 4, + /** the statement list directly contains a `BEGIN` block +*/ + PM_STATEMENTS_FLAGS_LAST = 5, +} +#[repr(u32)] +#[non_exhaustive] /** Flags for string nodes. */ #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/sig/generated/prism/dot_visitor.rbs b/sig/generated/prism/dot_visitor.rbs index 2f8c683c8e..d2f4b2f456 100644 --- a/sig/generated/prism/dot_visitor.rbs +++ b/sig/generated/prism/dot_visitor.rbs @@ -620,6 +620,12 @@ module Prism # : (ShareableConstantNode node) -> String def shareable_constant_node_flags_inspect: (ShareableConstantNode node) -> String + # Inspect a node that has statements_flags flags to display the flags as a + # comma-separated list. + # + # : (StatementsNode node) -> String + def statements_flags_inspect: (StatementsNode node) -> String + # Inspect a node that has string_flags flags to display the flags as a # comma-separated list. # diff --git a/sig/generated/prism/dsl.rbs b/sig/generated/prism/dsl.rbs index 8afc9399df..d8f1e10800 100644 --- a/sig/generated/prism/dsl.rbs +++ b/sig/generated/prism/dsl.rbs @@ -888,6 +888,11 @@ module Prism # : (Symbol name) -> Integer def shareable_constant_node_flag: (Symbol name) -> Integer + # Retrieve the value of one of the StatementsFlags flags. + # + # : (Symbol name) -> Integer + def statements_flag: (Symbol name) -> Integer + # Retrieve the value of one of the StringFlags flags. # # : (Symbol name) -> Integer diff --git a/sig/generated/prism/node.rbs b/sig/generated/prism/node.rbs index 3c750a7ef9..50eaf287dc 100644 --- a/sig/generated/prism/node.rbs +++ b/sig/generated/prism/node.rbs @@ -19702,6 +19702,12 @@ module Prism # : () -> String def inspect: () -> String + # :category: Flags + # the statement list directly contains a `BEGIN` block + # + # : () -> bool + def contains_pre_execution?: () -> bool + # :call-seq: # body -> Array[Node] # @@ -21592,6 +21598,12 @@ module Prism EXPERIMENTAL_COPY: untyped end + # Flags for statement lists. + module StatementsFlags + # the statement list directly contains a `BEGIN` block + CONTAINS_PRE_EXECUTION: untyped + end + # Flags for string nodes. module StringFlags # internal bytes forced the encoding to UTF-8 diff --git a/snapshots/nils.txt b/snapshots/nils.txt index 9228d478e0..37beb6fbb6 100644 --- a/snapshots/nils.txt +++ b/snapshots/nils.txt @@ -3,7 +3,7 @@ ├── locals: [] └── statements: @ StatementsNode (location: (1,0)-(12,11)) - ├── flags: ∅ + ├── flags: contains_pre_execution └── body: (length: 5) ├── @ NilNode (location: (1,0)-(1,3)) │ └── flags: newline, static_literal diff --git a/snapshots/seattlerb/BEGIN.txt b/snapshots/seattlerb/BEGIN.txt index a0d73a8039..6ef3f12be6 100644 --- a/snapshots/seattlerb/BEGIN.txt +++ b/snapshots/seattlerb/BEGIN.txt @@ -3,7 +3,7 @@ ├── locals: [] └── statements: @ StatementsNode (location: (1,0)-(1,12)) - ├── flags: ∅ + ├── flags: contains_pre_execution └── body: (length: 1) └── @ PreExecutionNode (location: (1,0)-(1,12)) ├── flags: newline diff --git a/snapshots/seattlerb/parse_line_preexe.txt b/snapshots/seattlerb/parse_line_preexe.txt index a106d56fc2..f230e4ee2c 100644 --- a/snapshots/seattlerb/parse_line_preexe.txt +++ b/snapshots/seattlerb/parse_line_preexe.txt @@ -3,7 +3,7 @@ ├── locals: [] └── statements: @ StatementsNode (location: (1,0)-(3,1)) - ├── flags: ∅ + ├── flags: contains_pre_execution └── body: (length: 1) └── @ PreExecutionNode (location: (1,0)-(3,1)) ├── flags: newline diff --git a/snapshots/unparser/corpus/literal/hookexe.txt b/snapshots/unparser/corpus/literal/hookexe.txt index 95f55b8cfe..edd43b956e 100644 --- a/snapshots/unparser/corpus/literal/hookexe.txt +++ b/snapshots/unparser/corpus/literal/hookexe.txt @@ -3,7 +3,7 @@ ├── locals: [] └── statements: @ StatementsNode (location: (1,0)-(7,1)) - ├── flags: ∅ + ├── flags: contains_pre_execution └── body: (length: 3) ├── @ PreExecutionNode (location: (1,0)-(3,1)) │ ├── flags: newline diff --git a/snapshots/whitequark/preexe.txt b/snapshots/whitequark/preexe.txt index f9fe60d07a..cb1ac430dd 100644 --- a/snapshots/whitequark/preexe.txt +++ b/snapshots/whitequark/preexe.txt @@ -3,7 +3,7 @@ ├── locals: [] └── statements: @ StatementsNode (location: (1,0)-(1,11)) - ├── flags: ∅ + ├── flags: contains_pre_execution └── body: (length: 1) └── @ PreExecutionNode (location: (1,0)-(1,11)) ├── flags: newline diff --git a/src/prism.c b/src/prism.c index 472c99bd0a..0c712cf68d 100644 --- a/src/prism.c +++ b/src/prism.c @@ -6311,6 +6311,9 @@ typedef struct { * earlier statement, so this takes the earliest start and the latest end. */ pm_location_t location; + + /** Flags accumulated as statements are appended. */ + pm_node_flags_t flags; } pm_statements_t; /** @@ -6325,6 +6328,7 @@ pm_statements_body_append(pm_parser_t *parser, pm_statements_t *body, pm_node_t if (body->nodes.size > 0) pm_statements_unreachable_check(parser, pm_node_list_open_last(&body->nodes), statement); pm_node_list_open_append(&parser->node_chunks, &body->nodes, statement); + if (PM_NODE_TYPE_P(statement, PM_PRE_EXECUTION_NODE)) body->flags |= PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION; if (newline) pm_node_flag_set(statement, PM_NODE_FLAG_NEWLINE); } @@ -6338,7 +6342,7 @@ pm_statements_node_create(pm_parser_t *parser, pm_statements_t *body) { return pm_statements_node_new( parser->arena, ++parser->node_id, - 0, + body->flags, body->location, body->nodes ); @@ -6354,7 +6358,7 @@ pm_statements_node_single_create(pm_parser_t *parser, pm_node_t *statement, bool return pm_statements_node_new( parser->arena, ++parser->node_id, - 0, + PM_NODE_TYPE_P(statement, PM_PRE_EXECUTION_NODE) ? PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION : 0, PM_LOCATION_INIT_NODE(statement), pm_node_list_single(parser->arena, statement) ); @@ -6377,6 +6381,7 @@ pm_statements_node_body_update(pm_statements_node_t *node, pm_node_t *statement) node->base.location = pm_statements_node_body_length(node) == 0 ? statement->location : pm_location_join(node->base.location, statement->location); + if (PM_NODE_TYPE_P(statement, PM_PRE_EXECUTION_NODE)) pm_node_flag_set(UP(node), PM_STATEMENTS_FLAGS_CONTAINS_PRE_EXECUTION); } /**