Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions bootstraptest/test_yjit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ def to_s
inval_method
}

# test that overriding to_s on a String subclass isn't over-optimised
# test that overriding to_s on a String subclass works consistently
assert_equal 'meh', %q{
class MyString < String
def to_s
Expand All @@ -1368,7 +1368,7 @@ def test_to_s(obj)

OBJ = MyString.new

# Should return 'meh' both times
# Should return '' both times
test_to_s("")
test_to_s("")

Expand Down Expand Up @@ -1421,6 +1421,81 @@ def make_str(foo)
uplus_str.frozen?
}

# String-subclass objects should behave as expected inside string-interpolation via concatstrings
assert_equal 'monkeys, yo!', %q{
class MyString < String
# This is a terrible idea in production code, but we'd like YJIT to match CRuby
def to_s
super + ", yo!"
end
end

m = MyString.new('monkeys')
"#{m.to_s}"

raise "String-subclass to_s should not be called for interpolation" if "#{m}" != 'monkeys'
raise "String-subclass to_s should be called explicitly during interpolation" if "#{m.to_s}" != 'monkeys, yo!'

m.to_s
}

# String-subclass objects should behave as expected for string equality
assert_equal 'a', %q{
class MyString < String
# This is a terrible idea in production code, but we'd like YJIT to match CRuby
def ==(b)
"#{self}_" == b
end
end

ma = MyString.new("a")

raise "Not dispatching to string-subclass equality!" if ma == "a" || ma != "a_"
raise "Incorrectly dispatching for String equality!" if "a_" == ma || "a" != ma
raise "Error in equality between string subclasses!" if ma != MyString.new("a_")
# opt_equality has an explicit "string always equals itself" test, but should never be used when == is redefined
raise "Error in reflexive equality!" if ma == ma
Comment on lines +1453 to +1457
Copy link
Contributor

@maximecb maximecb Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we haven't been using raise in any of these tests. We also try to keep them short, not testing a lot of things at once, because if YJIT side-exits, it won't be testing what we need.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I found some other raises in the file, but I could probably find a way to remove these. This would be a lot more lines if I need to re-declare the assert block and string class for every comparison, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Will accept these as-is :)


ma.to_s
}

assert_equal '', %q{
class MyString < String; end

a = "a"
ma = MyString.new("a")
fma = MyString.new("a").freeze

# Test to_s on string subclass
raise "to_s should not duplicate a String!" if a.object_id != a.to_s.object_id
raise "to_s should duplicate a String subclass!" if ma.object_id == ma.to_s.object_id

# Test freeze, uminus and uplus on string subclass
raise "Freezing a string subclass should not duplicate it!" if fma.object_id != fma.freeze.object_id
raise "Unary minus on frozen string subclass should not duplicate it!" if fma.object_id != (-fma).object_id
raise "Unary minus on unfrozen string subclass should duplicate it!" if ma.object_id == (-ma).object_id
raise "Unary plus on unfrozen string subclass should not duplicate it!" if ma.object_id != (+ma).object_id
raise "Unary plus on frozen string subclass should duplicate it!" if fma.object_id == (+fma).object_id

''
}

# Test << operator on string subclass
assert_equal 'abab', %q{
class MyString < String; end

a = -"a"
mb = MyString.new("b")

buf = String.new
mbuf = MyString.new

buf << a << mb
mbuf << a << mb

buf + mbuf
}

# test invokebuiltin as used in struct assignment
assert_equal '123', %q{
def foo(obj)
Expand Down
5 changes: 0 additions & 5 deletions yjit/src/cruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,6 @@ impl VALUE {
self == Qnil
}

/// Returns true or false depending whether the value is a string
pub fn string_p(self) -> bool {
unsafe { CLASS_OF(self) == rb_cString }
}

/// Read the flags bits from the RBasic object, then return a Ruby type enum (e.g. RUBY_T_ARRAY)
pub fn builtin_type(self) -> ruby_value_type {
assert!(!self.special_const_p());
Expand Down