Skip to content

Add specs for Ruby 4.0 features and changes - #1401

Open
SergiuLupaiescu wants to merge 8 commits into
ruby:masterfrom
SergiuLupaiescu:issue-1351
Open

SergiuLupaiescu wants to merge 8 commits into
ruby:masterfrom
SergiuLupaiescu:issue-1351

Conversation

@SergiuLupaiescu

@SergiuLupaiescu SergiuLupaiescu commented Oct 13, 2026 •

Copy link
Copy Markdown

Part of #1351

  • Adds specs for the Ruby 4.0 changes in the NEWS file that had no coverage yet. All new specs are behind ruby_version_is "4.0" guards.

Covers:

  • Logical operators at the start of a line (&&, ||, and, or)
  • Numbered parameters in Binding#local_variable*: local_variables no longer includes them, and local_variable_get, local_variable_set and local_variable_defined? reject them. This is marked done in Write specs for new Ruby 4.0 features and changes #1351 but had no spec.
  • Set#inspect, Pathname as a core class, CGI reduced to cgi/escape, ErrorHighlight caller/callee snippets for ArgumentError, and the "wrong number of arguments" backtrace label.
  • Unicode 17: case mapping for the new cased letters, the Sidetic script and Emoji 17 in regexps.
  • Fiber::Scheduler hooks: fiber_interrupt, yield, io_close and io_write.
  • C API: rb_thread_call_with_gvl (with and without the GVL) and rb_thread_fd_close (deprecated no-op).

@SergiuLupaiescu
SergiuLupaiescu marked this pull request as draft October 13, 2026 11:57
…uler

CRuby treats a truthy return value from Fiber::Scheduler#io_close as 'the scheduler already closed the file descriptor' and does not close it. The test scheduler returned the events Array, so descriptors were never closed and the CHECK_LEAKS check failed on CI.
@SergiuLupaiescu
SergiuLupaiescu marked this pull request as ready for review October 14, 2026 08:31
it "returns false for the \"it\" parameter" do
eval("proc { it; binding.local_variable_defined?(:it) }").call(1).should == false
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar specs were added in language/numbered_parameters_spec.rb (see truffleruby/truffleruby#4313). I'd say it's OK to have them duplicated.

Comment thread core/set/inspect_spec.rb
it "returns a String representation in the #<Set: {...}> format" do
Set[1, 2, 3].inspect.should == "#<Set: {1, 2, 3}>"
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The specs for #inspect were moved to specs for #to_s in 3e241de (#inspect is just an alias) and the exact format is tested indirectly.

out.should.include?("add(1)")
out.should.include?("def add(x, y)")
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be error_highlight gem functionality, so it would be better to test it in a dedicated directory under library/ (e.g., library/error_highlight/).

e.backtrace_locations.first.label.should == "ExceptionSpecs::Arity#bar"
}
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems the original description is a bit incorrect:

A backtrace for ArgumentError of "wrong number of arguments" now
include the receiver's class or module name (e.g., in Foo#bar
instead of in bar). [[Bug #21698](https://bugs.ruby-lang.org/issues/21698)]

Actually what was discussed in the #21698 issue is a message of ArgumentError exception, not a backtrace.

The change in Ruby 4.0 in a message is from

`add': wrong number of arguments (given 1, expected 2) (ArgumentError)

to

'FooBar#add': wrong number of arguments (given 0, expected 2) (ArgumentError)

So a case in message_spec.rb or a dedicated file core/exception/argument_error_spec.rb can be added instead.

JFYI A module/class name was added in a backtrace in Ruby 3.4 and is tested in core/thread/backtrace/location/label_spec.rb

describe "Fiber::Scheduler hooks" do
ruby_version_is "4.0" do
describe "#fiber_interrupt" do
it "is called with the blocked fiber and an IOError when its IO is closed by another fiber" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are already some specs for Fiber hooks and they are grouped around blocking operations (like IO reading and writing). A couple of examples - eecf44f, 0da5077. It makes sense to follow this approach.

The Fiber::Scheduler class is public so if you want to cover its methods with specs in isolation - it's convenient to have a separate directory for a class and test each its method in a dedicated file.


static VALUE thread_spec_rb_thread_call_with_gvl_from_without_gvl(VALUE self) {
return (VALUE)rb_thread_call_without_gvl(without_gvl_calling_with_gvl_func, NULL, RUBY_UBF_IO, NULL);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure if it's a proper way to test rb_thread_call_with_gvl in cases with and without GVL being held:

  • I suppose that by default GVL is already held for a user provided code (so nothing special should be done for "rb_thread_call_with_gvl")
  • and GVL should be released explicitly (e.g. with gvl_release) and then acquired back for "rb_thread_call_with_gvl_from_without_gvl"

return Qnil;
}
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it mean that the spec will actually test rb_thread_fd_close only on GCC? In this case I wouldn't bother with adding such tests at all. Testing deprecations usually has very little value.

@andrykonchin

Copy link
Copy Markdown
Member

Sorry for such delay of the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants