Skip to content

Commit 7e33c92

Browse files
committed
Add PM_STRING_FLAGS_FROZEN / PM_STRING_FLAGS_MUTABLE on PM_SOURCE_FILE_NODE
For all intent and purposes, `__FILE__` is a string literal subject to the `# frozen_string_literal: true/false` comment and to the global `--enable-frozen-string-literal / --disable-frozen-string-literal` CLI flags.
1 parent 20e768d commit 7e33c92

File tree

10 files changed

+28
-3
lines changed

10 files changed

+28
-3
lines changed

config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,9 @@ nodes:
30903090
^^^^^^^^^^^^
30913091
- name: SourceFileNode
30923092
fields:
3093+
- name: flags
3094+
type: flags
3095+
kind: StringFlags
30933096
- name: filepath
30943097
type: string
30953098
comment: |

src/prism.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5782,10 +5782,21 @@ pm_source_file_node_create(pm_parser_t *parser, const pm_token_t *file_keyword)
57825782
pm_source_file_node_t *node = PM_ALLOC_NODE(parser, pm_source_file_node_t);
57835783
assert(file_keyword->type == PM_TOKEN_KEYWORD___FILE__);
57845784

5785+
pm_node_flags_t flags = 0;
5786+
5787+
switch (parser->frozen_string_literal) {
5788+
case PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED:
5789+
flags |= PM_STRING_FLAGS_MUTABLE;
5790+
break;
5791+
case PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED:
5792+
flags |= PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN;
5793+
break;
5794+
}
5795+
57855796
*node = (pm_source_file_node_t) {
57865797
{
57875798
.type = PM_SOURCE_FILE_NODE,
5788-
.flags = PM_NODE_FLAG_STATIC_LITERAL,
5799+
.flags = flags,
57895800
.location = PM_LOCATION_TOKEN_VALUE(file_keyword),
57905801
},
57915802
.filepath = parser->filepath

test/prism/snapshots/keyword_method_names.txt

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

test/prism/snapshots/keywords.txt

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

test/prism/snapshots/patterns.txt

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

test/prism/snapshots/unparser/corpus/literal/pragma.txt

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

test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt

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

test/prism/snapshots/whitequark/string___FILE__.txt

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

test/prism/static_inspect_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_source_encoding
5858
end
5959

6060
def test_source_file
61-
assert_equal __FILE__.inspect, static_inspect("__FILE__", filepath: __FILE__)
61+
assert_equal __FILE__.inspect, static_inspect("__FILE__", filepath: __FILE__, frozen_string_literal: true)
6262
end
6363

6464
def test_source_line

test/prism/static_literals_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def test_static_literals
3434

3535
assert_warning("\"#{__FILE__}\"")
3636
assert_warning("\"foo\"")
37-
assert_warning("\"#{__FILE__}\"", "__FILE__")
3837

3938
assert_warning("/foo/")
4039

0 commit comments

Comments
 (0)