Open
Conversation
Issue When running tests with Ruby 3.4.0rc1, the following warnings are displayed. ```console $ rake test Run options: --seed 46514 /work/ruby/json-stream/lib/json/stream/parser.rb:549: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information) /work/ruby/json-stream/lib/json/stream/parser.rb:555: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information) ``` Cuases According to https://bugs.ruby-lang.org/issues/20205, string literals may be frozen by default in future Ruby versions (potentially Ruby 4.0+). Starting from Ruby 3.4.0, warnings like `warning: literal string will be frozen in the future` are shown if string literals are mutable. Solution We explicitly make string literals mutable using `+"string literal"` Additionally, we add `# frozen_string_literal: true` at the top of the affected files and handle the warnings to ensure compatibility with Ruby versions before 3.4.0.
kou
reviewed
Dec 25, 2024
lib/json/stream/buffer.rb
Outdated
| # Avoid state machine for complete UTF-8. | ||
| if @buffer.empty? | ||
| data.force_encoding(Encoding::UTF_8) | ||
| (+data).force_encoding(Encoding::UTF_8) |
Contributor
There was a problem hiding this comment.
Does this work?
a = "a"
a.force_encoding("ASCII-8BIT")
a.freeze
(+a).force_encoding(Encoding::UTF_8)
p a.encoding # -> #<Encoding:BINARY (ASCII-8BIT)>
Author
There was a problem hiding this comment.
Sorry it doesn't work. We should assign the result to new variable. I will fix it.
a = "a"
a.force_encoding("ASCII-8BIT")
a.freeze
b = (+a).force_encoding(Encoding::UTF_8)
p a.encoding # -> #<Encoding:BINARY (ASCII-8BIT)>
p b.econding # -> #<Encoding:UTF-8>
Contributor
There was a problem hiding this comment.
dup than + (conditional dup) may be better here because users may not expect that << may change the argument (data).
data = data.dup
data.force_encoding(Encoding::UTF_8)
Author
There was a problem hiding this comment.
fix: d97d4d2 I see. It totally makes sense to me too!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
When running tests with Ruby 3.4.0rc1, the following warnings are displayed.
Causes
According to https://bugs.ruby-lang.org/issues/20205, string literals may be frozen by default in future Ruby versions (potentially Ruby 4.0+).
Starting from Ruby 3.4.0, warnings like
warning: literal string will be frozen in the futureare shown if string literals are mutable.Solution
We explicitly make string literals mutable using
+"string literal"Additionally, we add# frozen_string_literal: trueat the top of the affected files and handle the warnings to ensure compatibility with Ruby versions before 3.4.0.