Skip to content

Commit 3c88b28

Browse files
authored
Merge pull request jruby#8547 from enebo/chilled_symbols
Fix failing specs for chilled symbols
2 parents 148fc42 + f156dc8 commit 3c88b28

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

core/src/main/java/org/jruby/ObjectFlags.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public interface ObjectFlags {
2424
int FSTRING = registry.newFlag(RubyString.class);
2525
int CR_7BIT_F = registry.newFlag(RubyString.class);
2626
int CR_VALID_F = registry.newFlag(RubyString.class);
27-
int CHILLED_F = registry.newFlag(RubyString.class);
27+
int CHILLED_LITERAL_F = registry.newFlag(RubyString.class);
28+
int CHILLED_SYMBOL_TO_S_F = registry.newFlag(RubyString.class);
2829

2930
int MATCH_BUSY = registry.newFlag(RubyMatchData.class);
3031

core/src/main/java/org/jruby/RubyBasicObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,11 @@ protected RubyBasicObject cloneSetup(ThreadContext context, RubyBasicObject clon
945945
if (freeze == context.nil) {
946946
sites(context).initialize_clone.call(context, clone, clone, this);
947947
if (this instanceof RubyString str && str.isChilled()) {
948-
((RubyString) clone).chill();
948+
if (str.isChilledLiteral()) {
949+
((RubyString) clone).chill();
950+
} else {
951+
((RubyString) clone).chill_symbol_string();
952+
}
949953
} else if (isFrozen()) {
950954
clone.setFrozen(true);
951955
}

core/src/main/java/org/jruby/RubyString.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import org.jruby.api.Create;
6363
import org.jruby.api.JRubyAPI;
6464
import org.jruby.ast.util.ArgsUtil;
65-
import org.jruby.common.IRubyWarnings.ID;
6665
import org.jruby.exceptions.JumpException;
6766
import org.jruby.platform.Platform;
6867
import org.jruby.runtime.Arity;
@@ -89,7 +88,8 @@
8988
import java.util.Locale;
9089
import java.util.function.Function;
9190

92-
import static org.jruby.ObjectFlags.CHILLED_F;
91+
import static org.jruby.ObjectFlags.CHILLED_LITERAL_F;
92+
import static org.jruby.ObjectFlags.CHILLED_SYMBOL_TO_S_F;
9393
import static org.jruby.RubyComparable.invcmp;
9494
import static org.jruby.RubyEnumerator.SizeFn;
9595
import static org.jruby.RubyEnumerator.enumeratorize;
@@ -984,12 +984,20 @@ public final void ensureInstanceVariablesSettable() {
984984
}
985985

986986
private void mutateChilledString() {
987-
getRuntime().getWarnings().warn("literal string will be frozen in the future");
988-
flags &= ~CHILLED_F;
987+
if ((flags & CHILLED_LITERAL_F) != 0) {
988+
getRuntime().getWarnings().warn("literal string will be frozen in the future");
989+
} else if ((flags & CHILLED_SYMBOL_TO_S_F) != 0) {
990+
getRuntime().getWarnings().warn("string returned by :" + value + ".to_s will be frozen in the future");
991+
}
992+
flags &= ~(CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F);
989993
}
990994

991995
protected boolean isChilled() {
992-
return (flags & CHILLED_F) != 0;
996+
return (flags & (CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F)) != 0;
997+
}
998+
999+
protected boolean isChilledLiteral() {
1000+
return (flags & CHILLED_LITERAL_F) != 0;
9931001
}
9941002

9951003
/** rb_str_modify
@@ -6803,14 +6811,19 @@ public IRubyObject scrub_bang(ThreadContext context, IRubyObject repl, Block blo
68036811

68046812
@JRubyMethod @JRubyAPI
68056813
public IRubyObject freeze(ThreadContext context) {
6806-
if (isChilled()) flags &= ~CHILLED_F;
6814+
if (isChilled()) flags &= ~(CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F);
68076815
if (isFrozen()) return this;
68086816
resize(size());
68096817
return super.freeze(context);
68106818
}
68116819

68126820
public RubyString chill() {
6813-
flags |= CHILLED_F;
6821+
flags |= CHILLED_LITERAL_F;
6822+
return this;
6823+
}
6824+
6825+
public RubyString chill_symbol_string() {
6826+
flags |= CHILLED_SYMBOL_TO_S_F;
68146827
return this;
68156828
}
68166829

core/src/main/java/org/jruby/RubySymbol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ public IRubyObject inspect(ThreadContext context) {
478478
@Override
479479
@JRubyMethod(name = "to_s")
480480
public IRubyObject to_s(ThreadContext context) {
481-
return RubyString.newStringShared(context.runtime, getBytes());
481+
return RubyString.newStringShared(context.runtime, getBytes()).chill_symbol_string();
482482
}
483483

484484
final RubyString to_s(Ruby runtime) {
485-
return RubyString.newStringShared(runtime, getBytes());
485+
return (RubyString) to_s(runtime.getCurrentContext());
486486
}
487487

488488
@JRubyMethod(name = "name")

0 commit comments

Comments
 (0)