aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--string.c3
-rw-r--r--test/ruby/test_string.rb16
2 files changed, 19 insertions, 0 deletions
diff --git a/string.c b/string.c
index 6bb8a24313..2bf9586e98 100644
--- a/string.c
+++ b/string.c
@@ -2658,6 +2658,9 @@ str_uplus(VALUE str)
static VALUE
str_uminus(VALUE str)
{
+ if (!BARE_STRING_P(str) && !rb_obj_frozen_p(str)) {
+ str = rb_str_dup(str);
+ }
return rb_fstring(str);
}
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 507e067a0d..26de52dedb 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -3175,6 +3175,22 @@ CODE
assert_same(str, -bar, "uminus deduplicates [Feature #13077]")
end
+ def test_uminus_no_freeze_not_bare
+ str = @cls.new("foo")
+ -str
+ assert_equal(false, str.frozen?)
+
+ str = @cls.new("foo")
+ str.instance_variable_set(:@iv, 1)
+ -str
+ assert_equal(false, str.frozen?)
+
+ str = @cls.new("foo")
+ str.taint
+ -str
+ assert_equal(false, str.frozen?)
+ end
+
def test_ord
assert_equal(97, "a".ord)
assert_equal(97, "abc".ord)