Skip to content

Commit 50372fe

Browse files
committed
Freeze internal parts, again
1 parent dc6e7f5 commit 50372fe

File tree

82 files changed

+584
-356
lines changed

Some content is hidden

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

82 files changed

+584
-356
lines changed

config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,13 @@ flags:
626626
- name: HEXADECIMAL
627627
comment: "0x prefix"
628628
comment: Flags for integer nodes that correspond to the base of the integer.
629+
- name: InterpolatedStringNodeFlags
630+
values:
631+
- name: FROZEN
632+
comment: "frozen by virtue of a `frozen_string_literal: true` comment or `--enable-frozen-string-literal`"
633+
- name: MUTABLE
634+
comment: "mutable by virtue of a `frozen_string_literal: false` comment or `--disable-frozen-string-literal`"
635+
comment: Flags for interpolated string nodes that indicated mutability if they are also marked as literals.
629636
- name: KeywordHashNodeFlags
630637
values:
631638
- name: SYMBOL_KEYS
@@ -2260,6 +2267,9 @@ nodes:
22602267
^^^^^^^^^^^^^^^^
22612268
- name: InterpolatedStringNode
22622269
fields:
2270+
- name: flags
2271+
type: flags
2272+
kind: InterpolatedStringNodeFlags
22632273
- name: opening_loc
22642274
type: location?
22652275
- name: parts

lib/prism/node_ext.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class StringNode < Node
5555
def to_interpolated
5656
InterpolatedStringNode.new(
5757
source,
58+
frozen? ? InterpolatedStringNodeFlags::FROZEN : 0,
5859
opening_loc,
5960
[copy(opening_loc: nil, closing_loc: nil, location: content_loc)],
6061
closing_loc,

src/prism.c

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,7 @@ pm_interpolated_regular_expression_node_create(pm_parser_t *parser, const pm_tok
42814281
*node = (pm_interpolated_regular_expression_node_t) {
42824282
{
42834283
.type = PM_INTERPOLATED_REGULAR_EXPRESSION_NODE,
4284+
.flags = PM_NODE_FLAG_STATIC_LITERAL,
42844285
.location = {
42854286
.start = opening->start,
42864287
.end = NULL,
@@ -4302,6 +4303,15 @@ pm_interpolated_regular_expression_node_append(pm_interpolated_regular_expressio
43024303
if (node->base.location.end < part->location.end) {
43034304
node->base.location.end = part->location.end;
43044305
}
4306+
4307+
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
4308+
pm_node_flag_set(part, PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN);
4309+
}
4310+
4311+
if (!PM_NODE_FLAG_P(part, PM_NODE_FLAG_STATIC_LITERAL)) {
4312+
pm_node_flag_unset((pm_node_t *) node, PM_NODE_FLAG_STATIC_LITERAL);
4313+
}
4314+
43054315
pm_node_list_append(&node->parts, part);
43064316
}
43074317

@@ -4312,6 +4322,38 @@ pm_interpolated_regular_expression_node_closing_set(pm_parser_t *parser, pm_inte
43124322
pm_node_flag_set((pm_node_t *)node, pm_regular_expression_flags_create(parser, closing));
43134323
}
43144324

4325+
/**
4326+
* Append a part to an InterpolatedStringNode node.
4327+
*/
4328+
static inline void
4329+
pm_interpolated_string_node_append(pm_parser_t *parser, pm_interpolated_string_node_t *node, pm_node_t *part) {
4330+
if (node->parts.size == 0 && node->opening_loc.start == NULL) {
4331+
node->base.location.start = part->location.start;
4332+
}
4333+
4334+
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
4335+
pm_node_flag_set(part, PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN);
4336+
}
4337+
4338+
if (!PM_NODE_FLAG_P(part, PM_NODE_FLAG_STATIC_LITERAL)) {
4339+
pm_node_flag_unset((pm_node_t *) node, PM_NODE_FLAG_STATIC_LITERAL | PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN | PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE);
4340+
}
4341+
4342+
pm_node_list_append(&node->parts, part);
4343+
node->base.location.end = MAX(node->base.location.end, part->location.end);
4344+
4345+
if (PM_NODE_FLAG_P(node, PM_NODE_FLAG_STATIC_LITERAL)) {
4346+
switch (parser->frozen_string_literal) {
4347+
case PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED:
4348+
pm_node_flag_set((pm_node_t *) node, PM_INTERPOLATED_STRING_NODE_FLAGS_FROZEN);
4349+
break;
4350+
case PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED:
4351+
pm_node_flag_set((pm_node_t *) node, PM_INTERPOLATED_STRING_NODE_FLAGS_MUTABLE);
4352+
break;
4353+
}
4354+
}
4355+
}
4356+
43154357
/**
43164358
* Allocate and initialize a new InterpolatedStringNode node.
43174359
*/
@@ -4322,6 +4364,7 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin
43224364
*node = (pm_interpolated_string_node_t) {
43234365
{
43244366
.type = PM_INTERPOLATED_STRING_NODE,
4367+
.flags = PM_NODE_FLAG_STATIC_LITERAL,
43254368
.location = {
43264369
.start = opening->start,
43274370
.end = closing->end,
@@ -4333,25 +4376,14 @@ pm_interpolated_string_node_create(pm_parser_t *parser, const pm_token_t *openin
43334376
};
43344377

43354378
if (parts != NULL) {
4336-
node->parts = *parts;
4379+
for (size_t index = 0; index < parts->size; index++) {
4380+
pm_interpolated_string_node_append(parser, node, parts->nodes[index]);
4381+
}
43374382
}
43384383

43394384
return node;
43404385
}
43414386

4342-
/**
4343-
* Append a part to an InterpolatedStringNode node.
4344-
*/
4345-
static inline void
4346-
pm_interpolated_string_node_append(pm_interpolated_string_node_t *node, pm_node_t *part) {
4347-
if (node->parts.size == 0 && node->opening_loc.start == NULL) {
4348-
node->base.location.start = part->location.start;
4349-
}
4350-
4351-
pm_node_list_append(&node->parts, part);
4352-
node->base.location.end = part->location.end;
4353-
}
4354-
43554387
/**
43564388
* Set the closing token of the given InterpolatedStringNode node.
43574389
*/
@@ -4361,6 +4393,24 @@ pm_interpolated_string_node_closing_set(pm_interpolated_string_node_t *node, con
43614393
node->base.location.end = closing->end;
43624394
}
43634395

4396+
static void
4397+
pm_interpolated_symbol_node_append(pm_interpolated_symbol_node_t *node, pm_node_t *part) {
4398+
if (node->parts.size == 0 && node->opening_loc.start == NULL) {
4399+
node->base.location.start = part->location.start;
4400+
}
4401+
4402+
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
4403+
pm_node_flag_set(part, PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN);
4404+
}
4405+
4406+
if (!PM_NODE_FLAG_P(part, PM_NODE_FLAG_STATIC_LITERAL)) {
4407+
pm_node_flag_unset((pm_node_t *) node, PM_NODE_FLAG_STATIC_LITERAL);
4408+
}
4409+
4410+
pm_node_list_append(&node->parts, part);
4411+
node->base.location.end = MAX(node->base.location.end, part->location.end);
4412+
}
4413+
43644414
/**
43654415
* Allocate and initialize a new InterpolatedSymbolNode node.
43664416
*/
@@ -4371,6 +4421,7 @@ pm_interpolated_symbol_node_create(pm_parser_t *parser, const pm_token_t *openin
43714421
*node = (pm_interpolated_symbol_node_t) {
43724422
{
43734423
.type = PM_INTERPOLATED_SYMBOL_NODE,
4424+
.flags = PM_NODE_FLAG_STATIC_LITERAL,
43744425
.location = {
43754426
.start = opening->start,
43764427
.end = closing->end,
@@ -4382,22 +4433,14 @@ pm_interpolated_symbol_node_create(pm_parser_t *parser, const pm_token_t *openin
43824433
};
43834434

43844435
if (parts != NULL) {
4385-
node->parts = *parts;
4436+
for (size_t index = 0; index < parts->size; index++) {
4437+
pm_interpolated_symbol_node_append(node, parts->nodes[index]);
4438+
}
43864439
}
43874440

43884441
return node;
43894442
}
43904443

4391-
static inline void
4392-
pm_interpolated_symbol_node_append(pm_interpolated_symbol_node_t *node, pm_node_t *part) {
4393-
if (node->parts.size == 0 && node->opening_loc.start == NULL) {
4394-
node->base.location.start = part->location.start;
4395-
}
4396-
4397-
pm_node_list_append(&node->parts, part);
4398-
node->base.location.end = part->location.end;
4399-
}
4400-
44014444
/**
44024445
* Allocate a new InterpolatedXStringNode node.
44034446
*/
@@ -4423,6 +4466,10 @@ pm_interpolated_xstring_node_create(pm_parser_t *parser, const pm_token_t *openi
44234466

44244467
static inline void
44254468
pm_interpolated_xstring_node_append(pm_interpolated_x_string_node_t *node, pm_node_t *part) {
4469+
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
4470+
pm_node_flag_set(part, PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN);
4471+
}
4472+
44264473
pm_node_list_append(&node->parts, part);
44274474
node->base.location.end = part->location.end;
44284475
}
@@ -15427,11 +15474,11 @@ parse_strings(pm_parser_t *parser, pm_node_t *current) {
1542715474
pm_token_t bounds = not_provided(parser);
1542815475

1542915476
pm_interpolated_string_node_t *container = pm_interpolated_string_node_create(parser, &bounds, NULL, &bounds);
15430-
pm_interpolated_string_node_append(container, current);
15477+
pm_interpolated_string_node_append(parser, container, current);
1543115478
current = (pm_node_t *) container;
1543215479
}
1543315480

15434-
pm_interpolated_string_node_append((pm_interpolated_string_node_t *) current, node);
15481+
pm_interpolated_string_node_append(parser, (pm_interpolated_string_node_t *) current, node);
1543515482
}
1543615483
}
1543715484

@@ -17365,15 +17412,15 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1736517412
// If we hit string content and the current node is
1736617413
// an interpolated string, then we need to append
1736717414
// the string content to the list of child nodes.
17368-
pm_interpolated_string_node_append((pm_interpolated_string_node_t *) current, string);
17415+
pm_interpolated_string_node_append(parser, (pm_interpolated_string_node_t *) current, string);
1736917416
} else if (PM_NODE_TYPE_P(current, PM_STRING_NODE)) {
1737017417
// If we hit string content and the current node is
1737117418
// a string node, then we need to convert the
1737217419
// current node into an interpolated string and add
1737317420
// the string content to the list of child nodes.
1737417421
pm_interpolated_string_node_t *interpolated = pm_interpolated_string_node_create(parser, &opening, NULL, &closing);
17375-
pm_interpolated_string_node_append(interpolated, current);
17376-
pm_interpolated_string_node_append(interpolated, string);
17422+
pm_interpolated_string_node_append(parser, interpolated, current);
17423+
pm_interpolated_string_node_append(parser, interpolated, string);
1737717424
current = (pm_node_t *) interpolated;
1737817425
} else {
1737917426
assert(false && "unreachable");
@@ -17398,7 +17445,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1739817445
pm_token_t opening = not_provided(parser);
1739917446
pm_token_t closing = not_provided(parser);
1740017447
pm_interpolated_string_node_t *interpolated = pm_interpolated_string_node_create(parser, &opening, NULL, &closing);
17401-
pm_interpolated_string_node_append(interpolated, current);
17448+
pm_interpolated_string_node_append(parser, interpolated, current);
1740217449
current = (pm_node_t *) interpolated;
1740317450
} else {
1740417451
// If we hit an embedded variable and the current
@@ -17407,7 +17454,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1740717454
}
1740817455

1740917456
pm_node_t *part = parse_string_part(parser);
17410-
pm_interpolated_string_node_append((pm_interpolated_string_node_t *) current, part);
17457+
pm_interpolated_string_node_append(parser, (pm_interpolated_string_node_t *) current, part);
1741117458
break;
1741217459
}
1741317460
case PM_TOKEN_EMBEXPR_BEGIN: {
@@ -17427,7 +17474,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1742717474
pm_token_t opening = not_provided(parser);
1742817475
pm_token_t closing = not_provided(parser);
1742917476
pm_interpolated_string_node_t *interpolated = pm_interpolated_string_node_create(parser, &opening, NULL, &closing);
17430-
pm_interpolated_string_node_append(interpolated, current);
17477+
pm_interpolated_string_node_append(parser, interpolated, current);
1743117478
current = (pm_node_t *) interpolated;
1743217479
} else if (PM_NODE_TYPE_P(current, PM_INTERPOLATED_STRING_NODE)) {
1743317480
// If we hit an embedded expression and the current
@@ -17438,7 +17485,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1743817485
}
1743917486

1744017487
pm_node_t *part = parse_string_part(parser);
17441-
pm_interpolated_string_node_append((pm_interpolated_string_node_t *) current, part);
17488+
pm_interpolated_string_node_append(parser, (pm_interpolated_string_node_t *) current, part);
1744217489
break;
1744317490
}
1744417491
default:

test/prism/snapshots/alias.txt

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

test/prism/snapshots/dash_heredocs.txt

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

0 commit comments

Comments
 (0)