Skip to content

Commit 66d2eac

Browse files
authored
Merge pull request #2685 from sampersand/sampersand/2025-10-17/update-querying
[Kernel] Updated querying methods
2 parents 5ddaf8f + c30022c commit 66d2eac

File tree

4 files changed

+270
-33
lines changed

4 files changed

+270
-33
lines changed

core/kernel.rbs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ module Kernel : BasicObject
182182
# c(4) #=> []
183183
# c(5) #=> nil
184184
#
185-
def self?.caller: (Integer start_or_range, ?Integer length) -> ::Array[String]?
186-
| (::Range[Integer] start_or_range) -> ::Array[String]?
187-
| () -> ::Array[String]
185+
def self?.caller: () -> Array[String]
186+
| (int start, ?int? length) -> Array[String]?
187+
| (range[int] range) -> Array[String]?
188188

189189
# <!--
190190
# rdoc-file=vm_backtrace.c
@@ -207,9 +207,9 @@ module Kernel : BasicObject
207207
# Optionally you can pass a range, which will return an array containing the
208208
# entries within the specified range.
209209
#
210-
def self?.caller_locations: (Integer start_or_range, ?Integer length) -> ::Array[Thread::Backtrace::Location]?
211-
| (::Range[Integer] start_or_range) -> ::Array[Thread::Backtrace::Location]?
212-
| () -> ::Array[Thread::Backtrace::Location]
210+
def self?.caller_locations: () -> Array[Thread::Backtrace::Location]
211+
| (int start, ?int? length) -> Array[Thread::Backtrace::Location]?
212+
| (range[int] range) -> Array[Thread::Backtrace::Location]?
213213

214214
# <!--
215215
# rdoc-file=vm_eval.c
@@ -314,6 +314,16 @@ module Kernel : BasicObject
314314
#
315315
def self?.block_given?: () -> bool
316316

317+
alias self.iterator? self.block_given?
318+
319+
# <!--
320+
# rdoc-file=vm_eval.c
321+
# - iterator? -> true or false
322+
# -->
323+
# Deprecated. Use block_given? instead.
324+
#
325+
alias iterator? block_given?
326+
317327
# <!--
318328
# rdoc-file=vm_eval.c
319329
# - local_variables -> array
@@ -326,7 +336,7 @@ module Kernel : BasicObject
326336
# end
327337
# local_variables #=> [:fred, :i]
328338
#
329-
def self?.local_variables: () -> ::Array[Symbol]
339+
def self?.local_variables: () -> Array[Symbol]
330340

331341
# <!--
332342
# rdoc-file=random.c
@@ -788,7 +798,7 @@ module Kernel : BasicObject
788798
#
789799
# Files that are currently being loaded must not be registered for autoload.
790800
#
791-
def self?.autoload: (interned _module, String filename) -> NilClass
801+
def self?.autoload: (interned const, path filename) -> nil
792802

793803
# <!--
794804
# rdoc-file=load.c
@@ -812,7 +822,7 @@ module Kernel : BasicObject
812822
# autoload?(:B) #=> "b"
813823
# end
814824
#
815-
def self?.autoload?: (interned name) -> String?
825+
def self?.autoload?: (interned name, ?boolish inherit) -> String?
816826

817827
# <!--
818828
# rdoc-file=proc.c
@@ -1193,7 +1203,7 @@ module Kernel : BasicObject
11931203
#
11941204
# global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
11951205
#
1196-
def self?.global_variables: () -> ::Array[Symbol]
1206+
def self?.global_variables: () -> Array[Symbol]
11971207

11981208
# <!--
11991209
# rdoc-file=load.c
@@ -1843,7 +1853,12 @@ module Kernel : BasicObject
18431853
# ----------------|---------------------------------------------
18441854
# <code>'-'</code>|Whether the entities exist and are identical.
18451855
#
1846-
def self?.test: (String | Integer cmd, String | IO file1, ?String | IO file2) -> (TrueClass | FalseClass | Time | nil | Integer)
1856+
def self?.test: ('b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'G' | 'k' | 'l' | 'o' | 'O' | 'p' | 'r' | 'R' | 'S' | 'u' | 'w' | 'W' | 'x' | 'X' | 'z' |
1857+
98 | 99 | 100 | 101 | 102 | 103 | 71 | 107 | 108 | 111 | 79 | 112 | 114 | 82 | 83 | 117 | 119 | 87 | 120 | 88 | 122, path filepath) -> bool
1858+
| ('s' | 115, path filepath) -> Integer?
1859+
| ('A' | 'M' | 'C' | 65 | 77 | 67, path filepath) -> Time
1860+
| ('<' | '=' | '>' | '-' | 60 | 61 | 62 | 45, path filepath1, path filepath2) -> bool
1861+
| (String | int cmd, path filepath1, ?path filepath2) -> (bool | Time | Integer | nil)
18471862

18481863
# <!--
18491864
# rdoc-file=vm_eval.c

lib/rbs/test/type_check.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,12 @@ def value(val, type)
356356
when Types::Variable
357357
true
358358
when Types::Literal
359-
type.literal == val
359+
begin
360+
type.literal == val
361+
rescue NoMethodError
362+
raise if defined?(val.==)
363+
false
364+
end
360365
when Types::Union
361366
type.types.any? {|type| value(val, type) }
362367
when Types::Intersection

test/stdlib/Kernel_test.rb

Lines changed: 230 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ class KernelSingletonTest < Test::Unit::TestCase
77

88
testing "singleton(::Kernel)"
99

10-
def test_caller_locations
11-
assert_send_type "() -> Array[Thread::Backtrace::Location]",
12-
Kernel, :caller_locations
13-
14-
assert_send_type "(Integer) -> Array[Thread::Backtrace::Location]?",
15-
Kernel, :caller_locations, 1
16-
17-
assert_send_type "(Integer, Integer) -> Array[Thread::Backtrace::Location]?",
18-
Kernel, :caller_locations, 1, 2
19-
20-
assert_send_type "(::Range[Integer]) -> Array[Thread::Backtrace::Location]?",
21-
Kernel, :caller_locations, (1..3)
22-
end
23-
2410
def test_Array
2511
assert_send_type "(nil) -> []",
2612
Kernel, :Array, nil
@@ -180,6 +166,38 @@ def test_throw
180166
end
181167
end
182168

169+
TOPLEVEL___callee__ = __callee__ # outside of a method
170+
def test___callee__
171+
assert_send_type '() -> Symbol',
172+
Kernel, :__callee__
173+
assert_type 'nil', TOPLEVEL___callee__
174+
end
175+
176+
TOPLEVEL___method__ = __method__ # outside of a method
177+
def test___method__
178+
assert_send_type '() -> Symbol',
179+
Kernel, :__method__
180+
assert_type 'nil', TOPLEVEL___method__
181+
end
182+
183+
def test___dir__
184+
assert_send_type '() -> String',
185+
Kernel, :__dir__
186+
187+
# Make sure it can return `nil`; this can't go through `assert_send_type`,
188+
# as it's only `nil` thru `eval`s
189+
assert_equal nil, eval('__dir__')
190+
end
191+
192+
def test_autoload
193+
with_interned :TestModuleForAutoload do |const|
194+
with_path '/does/not/exist' do |path|
195+
assert_send_type '(interned, path) -> nil',
196+
Kernel, :autoload, const, path
197+
end
198+
end
199+
end
200+
183201
TestException = Class.new(Exception)
184202

185203
def test_raise(method: :raise)
@@ -239,17 +257,208 @@ def test_fail
239257
test_raise method: :fail
240258
end
241259

260+
242261
def test_autoload?
243-
with_interned :TestModuleForAutoload do |interned|
244-
assert_send_type "(::interned) -> String?",
245-
Kernel, :autoload?, interned
262+
with_interned :TestModuleForAutoloadP do |const|
263+
assert_send_type '(interned) -> nil',
264+
Kernel, :autoload?, const
265+
266+
with_boolish do |inherit|
267+
assert_send_type '(interned, boolish) -> nil',
268+
Kernel, :autoload?, const, inherit
269+
end
270+
end
271+
272+
# Unfortunately, `autoload` doesn't play well with `assert_send_type`
273+
Kernel.autoload :TestModuleForAutoloadP, '/does/not/exist'
274+
275+
with_interned :TestModuleForAutoloadP do |const|
276+
assert_type 'String', Kernel.autoload?(const)
277+
278+
with_boolish do |inherit|
279+
assert_type 'String', Kernel.autoload?(const, inherit)
280+
end
281+
end
282+
end
283+
284+
def test_binding
285+
assert_send_type '() -> Binding',
286+
Kernel, :binding
287+
end
288+
289+
def test_block_given?(method: :block_given?)
290+
assert_send_type '() -> bool',
291+
Kernel, method
292+
end
293+
294+
def test_iterator?
295+
silence_warning :deprecated do
296+
test_block_given?(method: :iterator?)
297+
end
298+
end
299+
300+
def test_caller
301+
assert_send_type '() -> Array[String]',
302+
Kernel, :caller
303+
304+
with_int 1 do |start|
305+
assert_send_type '(int) -> Array[String]',
306+
Kernel, :caller, start
307+
308+
with_int(2).and_nil do |length|
309+
assert_send_type '(int, int?) -> Array[String]',
310+
Kernel, :caller, start, length
311+
end
312+
end
313+
314+
with_int 100000 do |start|
315+
assert_send_type '(int) -> nil',
316+
Kernel, :caller, start
317+
318+
with_int(2).and_nil do |length|
319+
assert_send_type '(int, int?) -> nil',
320+
Kernel, :caller, start, length
321+
end
322+
end
323+
324+
with_range with_int(1), with_int(2) do |range|
325+
assert_send_type '(range[int]) -> Array[String]',
326+
Kernel, :caller, range
246327
end
247328

248-
autoload :TestModuleForAutoload, '/shouldnt/be/executed'
329+
with_range with_int(100000) ,with_int(100001) do |range|
330+
assert_send_type '(range[int]) -> nil',
331+
Kernel, :caller, range
332+
end
333+
end
249334

250-
with_interned :TestModuleForAutoload do |interned|
251-
assert_send_type "(::interned) -> String?",
252-
Kernel, :autoload?, interned
335+
def test_caller_locations
336+
assert_send_type '() -> Array[Thread::Backtrace::Location]',
337+
Kernel, :caller_locations
338+
339+
with_int 1 do |start|
340+
assert_send_type '(int) -> Array[Thread::Backtrace::Location]',
341+
Kernel, :caller_locations, start
342+
343+
with_int(2).and_nil do |length|
344+
assert_send_type '(int, int?) -> Array[Thread::Backtrace::Location]',
345+
Kernel, :caller_locations, start, length
346+
end
347+
end
348+
349+
with_int 100000 do |start|
350+
assert_send_type '(int) -> nil',
351+
Kernel, :caller_locations, start
352+
353+
with_int(2).and_nil do |length|
354+
assert_send_type '(int, int?) -> nil',
355+
Kernel, :caller_locations, start, length
356+
end
357+
end
358+
359+
with_range with_int(1), with_int(2) do |range|
360+
assert_send_type '(range[int]) -> Array[Thread::Backtrace::Location]',
361+
Kernel, :caller_locations, range
362+
end
363+
364+
with_range with_int(100000) ,with_int(100001) do |range|
365+
assert_send_type '(range[int]) -> nil',
366+
Kernel, :caller_locations, range
367+
end
368+
end
369+
370+
def test_global_variables
371+
assert_send_type '() -> Array[Symbol]',
372+
Kernel, :global_variables
373+
end
374+
375+
def test_local_variables
376+
assert_send_type '() -> Array[Symbol]',
377+
Kernel, :local_variables
378+
end
379+
380+
def test_test
381+
# true/false tests
382+
with_path do |filepath|
383+
%w[b c d e f g G k l o O p r R S u w W x X z].each do |test_char|
384+
test_ord = test_char.ord
385+
386+
with test_char, test_ord do |test_literal|
387+
assert_send_type "('b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'G' | 'k' | 'l' | 'o' | 'O' | 'p' | 'r' | 'R' | 'S' | 'u' | 'w' | 'W' | 'x' | 'X' | 'z' |
388+
98 | 99 | 100 | 101 | 102 | 103 | 71 | 107 | 108 | 111 | 79 | 112 | 114 | 82 | 83 | 117 | 119 | 87 | 120 | 88 | 122, path) -> bool",
389+
Kernel, :test, test_literal, filepath
390+
end
391+
392+
with_int(test_ord).and test_char do |test_nonliteral|
393+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
394+
Kernel, :test, test_nonliteral, filepath
395+
end
396+
end
397+
end
398+
399+
# Integer? tests
400+
%w[s].each do |test_char|
401+
test_ord = test_char.ord
402+
403+
with_path __FILE__ do |filepath|
404+
with test_char, test_ord do |test_literal|
405+
assert_send_type "('s' | 115, path) -> Integer",
406+
Kernel, :test, test_literal, filepath
407+
end
408+
409+
with_int(test_ord).and test_char do |test_nonliteral|
410+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
411+
Kernel, :test, test_nonliteral, filepath
412+
end
413+
end
414+
415+
with_path '/not/a/file' do |filepath|
416+
with test_char, test_ord do |test_literal|
417+
assert_send_type "('s' | 115, path) -> nil",
418+
Kernel, :test, test_literal, filepath
419+
end
420+
421+
with_int(test_ord).and test_char do |test_nonliteral|
422+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
423+
Kernel, :test, test_nonliteral, filepath
424+
end
425+
end
426+
end
427+
428+
# Time tests
429+
with_path __FILE__ do |filepath|
430+
%w[A M C].each do |test_char|
431+
test_ord = test_char.ord
432+
433+
with test_char, test_ord do |test_literal|
434+
assert_send_type "('A' | 'M' | 'C' | 65 | 77 | 67, path) -> Time",
435+
Kernel, :test, test_literal, filepath
436+
end
437+
438+
with_int(test_ord).and test_char do |test_nonliteral|
439+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
440+
Kernel, :test, test_nonliteral, filepath
441+
end
442+
end
443+
end
444+
445+
# Comparison Tests
446+
with_path __dir__ + '/Integer_test.rb' do |filepath1|
447+
with_path __FILE__ do |filepath2|
448+
%w[< = > -].each do |test_char|
449+
test_ord = test_char.ord
450+
451+
with test_char, test_ord do |test_literal|
452+
assert_send_type "('<' | '=' | '>' | '-' | 60 | 61 | 62 | 45, path, path) -> bool",
453+
Kernel, :test, test_literal, filepath1, filepath2
454+
end
455+
456+
with_int(test_ord).and test_char do |test_nonliteral|
457+
assert_send_type "(String | int, path, ?path) -> (bool | Time | Integer | nil)",
458+
Kernel, :test, test_nonliteral, filepath1, filepath2
459+
end
460+
end
461+
end
253462
end
254463
end
255464

test/stdlib/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def self.included(base)
149149
end
150150

151151
RUBY_EXECUTABLE = ENV["RUBY"] || RbConfig.ruby
152+
153+
def silence_warning(which)
154+
old_warning = Warning[which]
155+
Warning[which] = false
156+
yield
157+
ensure
158+
Warning[which] = old_warning
159+
end
152160
end
153161

154162
class StdlibTest < Test::Unit::TestCase

0 commit comments

Comments
 (0)