Skip to content

Commit fcc2615

Browse files
committed
[GR-18163] Simplify String#-@
PullRequest: truffleruby/3403
2 parents 0b8869e + c75f800 commit fcc2615

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

spec/ruby/core/string/shared/dedup.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
dynamic.send(@method).should equal("this string is frozen".send(@method).freeze)
3939
end
4040

41+
it "does not deduplicate a frozen string when it has instance variables" do
42+
dynamic = %w(this string is frozen).join(' ')
43+
dynamic.instance_variable_set(:@a, 1)
44+
dynamic.freeze
45+
46+
dynamic.send(@method).should_not equal("this string is frozen".freeze)
47+
dynamic.send(@method).should_not equal("this string is frozen".send(@method).freeze)
48+
dynamic.send(@method).should equal(dynamic)
49+
end
50+
4151
ruby_version_is "3.0" do
4252
it "interns the provided string if it is frozen" do
4353
dynamic = "this string is unique and frozen #{rand}".freeze

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,29 @@ protected RubyArray instanceVariables(Object self) {
11501150

11511151
}
11521152

1153+
@Primitive(name = "any_instance_variable?")
1154+
public abstract static class AnyInstanceVariableNode extends PrimitiveArrayArgumentsNode {
1155+
1156+
@Specialization(limit = "getDynamicObjectCacheLimit()")
1157+
protected boolean any(RubyDynamicObject self,
1158+
@CachedLibrary("self") DynamicObjectLibrary objectLibrary) {
1159+
Object[] keys = objectLibrary.getKeyArray(self);
1160+
1161+
for (Object key : keys) {
1162+
if (key instanceof String) {
1163+
return true;
1164+
}
1165+
}
1166+
1167+
return false;
1168+
}
1169+
1170+
@Specialization(guards = "!isRubyDynamicObject(self)")
1171+
protected boolean noVariablesInImmutableObject(Object self) {
1172+
return false;
1173+
}
1174+
}
1175+
11531176
@CoreMethod(names = { "is_a?", "kind_of?" }, required = 1)
11541177
public abstract static class KernelIsANode extends CoreMethodArrayArgumentsNode {
11551178

src/main/ruby/truffleruby/core/string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ def -@
12471247
str = frozen? ? self : dup.freeze
12481248
if Primitive.string_interned?(self)
12491249
self
1250-
elsif !(str.instance_variables).empty?
1250+
elsif Primitive.any_instance_variable?(str)
12511251
str
12521252
else
12531253
Primitive.string_intern(str)

0 commit comments

Comments
 (0)