Skip to content

Commit

Permalink
Merge pull request jruby#8547 from enebo/chilled_symbols
Browse files Browse the repository at this point in the history
Fix failing specs for chilled symbols
  • Loading branch information
enebo authored Dec 26, 2024
2 parents 148fc42 + f156dc8 commit 3c88b28
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/ObjectFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public interface ObjectFlags {
int FSTRING = registry.newFlag(RubyString.class);
int CR_7BIT_F = registry.newFlag(RubyString.class);
int CR_VALID_F = registry.newFlag(RubyString.class);
int CHILLED_F = registry.newFlag(RubyString.class);
int CHILLED_LITERAL_F = registry.newFlag(RubyString.class);
int CHILLED_SYMBOL_TO_S_F = registry.newFlag(RubyString.class);

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

Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,11 @@ protected RubyBasicObject cloneSetup(ThreadContext context, RubyBasicObject clon
if (freeze == context.nil) {
sites(context).initialize_clone.call(context, clone, clone, this);
if (this instanceof RubyString str && str.isChilled()) {
((RubyString) clone).chill();
if (str.isChilledLiteral()) {
((RubyString) clone).chill();
} else {
((RubyString) clone).chill_symbol_string();
}
} else if (isFrozen()) {
clone.setFrozen(true);
}
Expand Down
27 changes: 20 additions & 7 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.jruby.api.Create;
import org.jruby.api.JRubyAPI;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.exceptions.JumpException;
import org.jruby.platform.Platform;
import org.jruby.runtime.Arity;
Expand All @@ -89,7 +88,8 @@
import java.util.Locale;
import java.util.function.Function;

import static org.jruby.ObjectFlags.CHILLED_F;
import static org.jruby.ObjectFlags.CHILLED_LITERAL_F;
import static org.jruby.ObjectFlags.CHILLED_SYMBOL_TO_S_F;
import static org.jruby.RubyComparable.invcmp;
import static org.jruby.RubyEnumerator.SizeFn;
import static org.jruby.RubyEnumerator.enumeratorize;
Expand Down Expand Up @@ -984,12 +984,20 @@ public final void ensureInstanceVariablesSettable() {
}

private void mutateChilledString() {
getRuntime().getWarnings().warn("literal string will be frozen in the future");
flags &= ~CHILLED_F;
if ((flags & CHILLED_LITERAL_F) != 0) {
getRuntime().getWarnings().warn("literal string will be frozen in the future");
} else if ((flags & CHILLED_SYMBOL_TO_S_F) != 0) {
getRuntime().getWarnings().warn("string returned by :" + value + ".to_s will be frozen in the future");
}
flags &= ~(CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F);
}

protected boolean isChilled() {
return (flags & CHILLED_F) != 0;
return (flags & (CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F)) != 0;
}

protected boolean isChilledLiteral() {
return (flags & CHILLED_LITERAL_F) != 0;
}

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

@JRubyMethod @JRubyAPI
public IRubyObject freeze(ThreadContext context) {
if (isChilled()) flags &= ~CHILLED_F;
if (isChilled()) flags &= ~(CHILLED_LITERAL_F|CHILLED_SYMBOL_TO_S_F);
if (isFrozen()) return this;
resize(size());
return super.freeze(context);
}

public RubyString chill() {
flags |= CHILLED_F;
flags |= CHILLED_LITERAL_F;
return this;
}

public RubyString chill_symbol_string() {
flags |= CHILLED_SYMBOL_TO_S_F;
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubySymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ public IRubyObject inspect(ThreadContext context) {
@Override
@JRubyMethod(name = "to_s")
public IRubyObject to_s(ThreadContext context) {
return RubyString.newStringShared(context.runtime, getBytes());
return RubyString.newStringShared(context.runtime, getBytes()).chill_symbol_string();
}

final RubyString to_s(Ruby runtime) {
return RubyString.newStringShared(runtime, getBytes());
return (RubyString) to_s(runtime.getCurrentContext());
}

@JRubyMethod(name = "name")
Expand Down

0 comments on commit 3c88b28

Please sign in to comment.