summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2024-03-15 13:24:23 +0100
committergit <svn-admin@ruby-lang.org>2024-03-15 13:31:02 +0000
commitf2e96d4bd90595b0818f02559a53905174496e34 (patch)
tree1f732d276d95dc0c671e97dd710c1a5f62e24c37
parentec4333c9703939109588af87936ee43ad48f9dcc (diff)
[ruby/prism] 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. https://github.com/ruby/prism/commit/7e33c92afd
-rw-r--r--prism/config.yml3
-rw-r--r--prism/prism.c13
-rw-r--r--test/prism/snapshots/keyword_method_names.txt1
-rw-r--r--test/prism/snapshots/keywords.txt1
-rw-r--r--test/prism/snapshots/patterns.txt6
-rw-r--r--test/prism/snapshots/unparser/corpus/literal/pragma.txt1
-rw-r--r--test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt2
-rw-r--r--test/prism/snapshots/whitequark/string___FILE__.txt1
-rw-r--r--test/prism/static_inspect_test.rb2
-rw-r--r--test/prism/static_literals_test.rb1
10 files changed, 28 insertions, 3 deletions
diff --git a/prism/config.yml b/prism/config.yml
index ab4aeae7d7..934574b3ea 100644
--- a/prism/config.yml
+++ b/prism/config.yml
@@ -3123,6 +3123,9 @@ nodes:
^^^^^^^^^^^^
- name: SourceFileNode
fields:
+ - name: flags
+ type: flags
+ kind: StringFlags
- name: filepath
type: string
comment: |
diff --git a/prism/prism.c b/prism/prism.c
index 96435a38b1..38390b5a11 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -5801,10 +5801,21 @@ pm_source_file_node_create(pm_parser_t *parser, const pm_token_t *file_keyword)
pm_source_file_node_t *node = PM_ALLOC_NODE(parser, pm_source_file_node_t);
assert(file_keyword->type == PM_TOKEN_KEYWORD___FILE__);
+ pm_node_flags_t flags = 0;
+
+ switch (parser->frozen_string_literal) {
+ case PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED:
+ flags |= PM_STRING_FLAGS_MUTABLE;
+ break;
+ case PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED:
+ flags |= PM_NODE_FLAG_STATIC_LITERAL | PM_STRING_FLAGS_FROZEN;
+ break;
+ }
+
*node = (pm_source_file_node_t) {
{
.type = PM_SOURCE_FILE_NODE,
- .flags = PM_NODE_FLAG_STATIC_LITERAL,
+ .flags = flags,
.location = PM_LOCATION_TOKEN_VALUE(file_keyword),
},
.filepath = parser->filepath
diff --git a/test/prism/snapshots/keyword_method_names.txt b/test/prism/snapshots/keyword_method_names.txt
index 9eb0428084..6d59790b07 100644
--- a/test/prism/snapshots/keyword_method_names.txt
+++ b/test/prism/snapshots/keyword_method_names.txt
@@ -132,6 +132,7 @@
│ ├── name_loc: (22,13)-(22,14) = "a"
│ ├── receiver:
│ │ @ SourceFileNode (location: (22,4)-(22,12))
+ │ │ ├── flags: ∅
│ │ └── filepath: "keyword_method_names.txt"
│ ├── parameters: ∅
│ ├── body: ∅
diff --git a/test/prism/snapshots/keywords.txt b/test/prism/snapshots/keywords.txt
index 6df11487d7..8a93c38bfd 100644
--- a/test/prism/snapshots/keywords.txt
+++ b/test/prism/snapshots/keywords.txt
@@ -8,5 +8,6 @@
├── @ SelfNode (location: (5,0)-(5,4))
├── @ SourceEncodingNode (location: (7,0)-(7,12))
├── @ SourceFileNode (location: (9,0)-(9,8))
+ │ ├── flags: ∅
│ └── filepath: "keywords.txt"
└── @ SourceLineNode (location: (11,0)-(11,8))
diff --git a/test/prism/snapshots/patterns.txt b/test/prism/snapshots/patterns.txt
index 4efd3159fc..5662129dae 100644
--- a/test/prism/snapshots/patterns.txt
+++ b/test/prism/snapshots/patterns.txt
@@ -445,6 +445,7 @@
│ │ └── block: ∅
│ ├── pattern:
│ │ @ SourceFileNode (location: (23,7)-(23,15))
+ │ │ ├── flags: ∅
│ │ └── filepath: "patterns.txt"
│ └── operator_loc: (23,4)-(23,6) = "=>"
├── @ MatchRequiredNode (location: (24,0)-(24,15))
@@ -1153,9 +1154,11 @@
│ │ ├── flags: ∅
│ │ ├── left:
│ │ │ @ SourceFileNode (location: (49,7)-(49,15))
+ │ │ │ ├── flags: ∅
│ │ │ └── filepath: "patterns.txt"
│ │ ├── right:
│ │ │ @ SourceFileNode (location: (49,19)-(49,27))
+ │ │ │ ├── flags: ∅
│ │ │ └── filepath: "patterns.txt"
│ │ └── operator_loc: (49,16)-(49,18) = ".."
│ └── operator_loc: (49,4)-(49,6) = "=>"
@@ -2823,6 +2826,7 @@
│ │ └── block: ∅
│ ├── pattern:
│ │ @ SourceFileNode (location: (126,7)-(126,15))
+ │ │ ├── flags: ∅
│ │ └── filepath: "patterns.txt"
│ └── operator_loc: (126,4)-(126,6) = "in"
├── @ MatchPredicateNode (location: (127,0)-(127,15))
@@ -3504,6 +3508,7 @@
│ │ └── @ InNode (location: (157,10)-(157,26))
│ │ ├── pattern:
│ │ │ @ SourceFileNode (location: (157,13)-(157,21))
+ │ │ │ ├── flags: ∅
│ │ │ └── filepath: "patterns.txt"
│ │ ├── statements: ∅
│ │ ├── in_loc: (157,10)-(157,12) = "in"
@@ -4460,6 +4465,7 @@
│ │ │ │ @ StatementsNode (location: (184,13)-(184,21))
│ │ │ │ └── body: (length: 1)
│ │ │ │ └── @ SourceFileNode (location: (184,13)-(184,21))
+ │ │ │ │ ├── flags: ∅
│ │ │ │ └── filepath: "patterns.txt"
│ │ │ ├── consequent: ∅
│ │ │ └── end_keyword_loc: ∅
diff --git a/test/prism/snapshots/unparser/corpus/literal/pragma.txt b/test/prism/snapshots/unparser/corpus/literal/pragma.txt
index ade81bc8ce..08e386b872 100644
--- a/test/prism/snapshots/unparser/corpus/literal/pragma.txt
+++ b/test/prism/snapshots/unparser/corpus/literal/pragma.txt
@@ -5,6 +5,7 @@
└── body: (length: 4)
├── @ SourceEncodingNode (location: (1,0)-(1,12))
├── @ SourceFileNode (location: (2,0)-(2,8))
+ │ ├── flags: ∅
│ └── filepath: "unparser/corpus/literal/pragma.txt"
├── @ SourceLineNode (location: (3,0)-(3,8))
└── @ CallNode (location: (4,0)-(4,7))
diff --git a/test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt b/test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt
index 48f377cc66..6f315780ad 100644
--- a/test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt
+++ b/test/prism/snapshots/whitequark/pattern_matching__FILE__LINE_literals.txt
@@ -9,6 +9,7 @@
│ ├── flags: ∅
│ ├── elements: (length: 3)
│ │ ├── @ SourceFileNode (location: (1,14)-(1,22))
+ │ │ │ ├── flags: ∅
│ │ │ └── filepath: "whitequark/pattern_matching__FILE__LINE_literals.txt"
│ │ ├── @ CallNode (location: (1,24)-(1,36))
│ │ │ ├── flags: ∅
@@ -37,6 +38,7 @@
│ │ ├── constant: ∅
│ │ ├── requireds: (length: 3)
│ │ │ ├── @ SourceFileNode (location: (2,14)-(2,22))
+ │ │ │ │ ├── flags: ∅
│ │ │ │ └── filepath: "whitequark/pattern_matching__FILE__LINE_literals.txt"
│ │ │ ├── @ SourceLineNode (location: (2,24)-(2,32))
│ │ │ └── @ SourceEncodingNode (location: (2,34)-(2,46))
diff --git a/test/prism/snapshots/whitequark/string___FILE__.txt b/test/prism/snapshots/whitequark/string___FILE__.txt
index c6ce21be5f..a12499a631 100644
--- a/test/prism/snapshots/whitequark/string___FILE__.txt
+++ b/test/prism/snapshots/whitequark/string___FILE__.txt
@@ -4,4 +4,5 @@
@ StatementsNode (location: (1,0)-(1,8))
└── body: (length: 1)
└── @ SourceFileNode (location: (1,0)-(1,8))
+ ├── flags: ∅
└── filepath: "whitequark/string___FILE__.txt"
diff --git a/test/prism/static_inspect_test.rb b/test/prism/static_inspect_test.rb
index 07b7ac3244..41301693e3 100644
--- a/test/prism/static_inspect_test.rb
+++ b/test/prism/static_inspect_test.rb
@@ -58,7 +58,7 @@ module Prism
end
def test_source_file
- assert_equal __FILE__.inspect, static_inspect("__FILE__", filepath: __FILE__)
+ assert_equal __FILE__.inspect, static_inspect("__FILE__", filepath: __FILE__, frozen_string_literal: true)
end
def test_source_line
diff --git a/test/prism/static_literals_test.rb b/test/prism/static_literals_test.rb
index 4e98c15701..2669931974 100644
--- a/test/prism/static_literals_test.rb
+++ b/test/prism/static_literals_test.rb
@@ -34,7 +34,6 @@ module Prism
assert_warning("\"#{__FILE__}\"")
assert_warning("\"foo\"")
- assert_warning("\"#{__FILE__}\"", "__FILE__")
assert_warning("/foo/")