Skip to content

IO's and File's methods as reflection names them (TestIO#test_marshal_closed_io on the VM) - #294

Merged
284km merged 1 commit into
mainfrom
stage10-io-reflection
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
stage10-io-reflection

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

test/ruby whole on both executions (#287) left one test failing on the VM alone, TestIO#test_marshal_closed_io: undef_method :closed? in an IO's singleton class raised NameError. The evaluator took the statement inside class << r and refused the call form (r.singleton_class.send(:undef_method, :closed?)) the same way. The cause was not the VM's: reflection did not know IO's methods.

What reflection saw

IO's public methods are the dispatcher's: no method row names them. The other core classes' are named by a list generated from the reference (m_builtin_lists.mere, tools/gen_builtin_lists.rb), which IO and File were not in. What the method table had instead was the prelude's: its helpers on IO (__fd_read and 125 more), its dup and to_s on IO, and, on File, its versions of IO's methods (read, close, ...). So, on main:

ruby main
IO.instance_methods(false) 77 59 of them missing, 128 more (126 of them internal)
File.instance_methods(false) 10 51 more, 42 of them IO's in ruby
r.singleton_class.method_defined?(:closed?) true false
undef_method :closed? in class << r undefines it NameError on the VM

The change

  • The list. tools/gen_builtin_lists.rb takes IO and File too; the lists are generated again (the others are unchanged, the header now names 4.0.10). The probe asks File's instance methods of a File, not of the class. IO's are asked of $stdout, which does not answer nine of them (close_read, close_write, fdatasync, fsync, ioctl, pread, pwrite, set_encoding_by_bom, to_io), so the list leaves those out: a pipe answers them through File's rows, $stdout does not, and the list is a claim respond_to? reads.
  • What the prelude wrote. A row the prelude wrote on IO or File (io_row_hidden) is left out of a class's own list unless ruby gives the class that name, and out of a list down the chain when it is a helper (__...). File's close_write is still File's to answer down the chain. Every one of them can still be called. Three lists read the rule: instance_methods(false), the public, private and protected instance_methods of either form, and the walk under methods and its kin (collect_vis_go).
  • What the list does not speak for. respond_to? reads the lists too, and IO's names what an IO or a File answers. An object of another IO subclass, a socket, answers the stream methods through its own route (fdio_route_call) and not, for one, to_path: with IO's list behind it, IPSocket.allocate.respond_to?(:to_path) was true, the socket library's #inspect called it, and corpus 476 failed. For such an object respond_to? reads the lists without IO's and File's (listed_builtin_root_skip_io), as before.
  • The walker's message. The class body's walker refused an unknown name with the class's key, (sng:281474976710835); it names it as the call does now, #<Class:#<IO:0x...>>, and a module as a module.

Now

ruby here
IO.instance_methods(false) 77 the same less the nine $stdout does not answer
File.instance_methods(false), public_instance_methods(false) 10 the same
File.instance_methods the same less ioctl
internal names in any public list of IO, File, a pipe, a File none none

TestIO#test_marshal_closed_io passes on both executions.

Corpus 826 has the lists (the names both have, File's whole own list, no internal name in seven lists), method_defined?, undef_method of an IO method in a pipe's and a File's singleton class, Marshal of a closed IO before and after, and an unknown name refused.

Recorded

  • $stdout (and IO.new(fd)) does not answer the nine names above; a pipe does.
  • IO.private_instance_methods(false) lacks initialize and initialize_copy.
  • Other classes' prelude helpers show the same way: Dir#__adopt, Thread#__tf_install. The rule is IO's and File's only.
  • File.instance_method(:read).owner is File (ruby: IO): the owner is the row's.
  • A socket does not answer to_path, path or lineno, which ruby's do (IO's); its respond_to? says so.

Checked

Against ruby 4.0.10, built with Mere 9dec2ff6, on main ccb6895 (and corpus 476, 824, 825, 826 and the test again after the rebase onto b70ce5c):

  • run_corpus.sh --both and MERE_RUBY_VM=1 run_corpus.sh --both: 952/952 each, with the hash index on and off. (Before the respond_to? rule, corpus 476 failed on both: the socket's #inspect called to_path.)
  • tools/iseq/vm_check.sh: 245/245.
  • unittest/vm_compare.sh over test_io, test_file, test_method, test_module and test_object: 615 tests, VM_ONLY 0, EVAL_ONLY 0, VM_FILE 0. Against Record test/ruby whole on both executions (VM_ONLY 2, VM_FILE 0) #287's whole run (main d586947) the five files' failing tests are the same on both executions, less TestIO#test_marshal_closed_io on the VM.
  • TestModule#test_complemented_method_entry_memory_leak differed once and not the second time: at a load of 17 it reaches its 10 s limit on main ccb6895 and here alike (three runs each, alternating), and the instructions are the same (28.64 G and 28.70 G for its loop); VM: a declaration block's def goes the evaluator's way (test_module's leak test inside its limit) #289's 8.1 s was at a load of 5.
  • The source gates (dead_defs, dup_defs, gc_roots, meth_writes, name_len, ns_names, bracket_depth, record_hygiene) each exit 0.

🤖 Generated with Claude Code

The builtin lists take IO and File (the probe asks File's of a File). A row
the prelude wrote on IO or File is left out of the class's own list unless
ruby gives it that name, and out of a list down the chain when it is a
helper (__fd_read and 125 more): IO.instance_methods(false) is ruby's less
nine names $stdout does not answer, File's is ruby's. undef_method of an IO
method in an IO's singleton class finds it, so TestIO#test_marshal_closed_io
passes on the VM (and the evaluator's call form). respond_to? does not read
IO's list for a socket. The class body walker's NameError names the class as
it prints. Corpus 826.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed 9dec672. No Critical / Major / Minor findings.

  • IO and File now have generated builtin name lists. Their own-method lists hide the prelude's internal definitions, so IO.instance_methods(false) and File.instance_methods(false) give ruby's names, and the __ internals are gone from every public list. respond_to? on other IO subclasses (sockets) no longer reads the IO/File lists, which kept corpus 476 (a socket must not answer to_path) right.
  • Ran on the author's build at this head (clean tree, binary newer than every .mere file) on the evaluator and MERE_RUBY_VM=1, against ruby 4.0.10 and a main build:
    • __ names in IO.instance_methods, File.instance_methods, IO.public_instance_methods, IO.methods, File.methods and File.singleton_methods: exactly ruby's (__id__, __send__). On main, the IO lists carried 120+ internal names.
    • Owners match ruby: File#path → IO, IO#read → IO, File#flock → File, TCPServer#accept → TCPServer. method_defined? for read/to_path, respond_to?(:to_path) on $stdout, a File, a UNIXSocket and a StringIO, and undef_method on a dup'd $stdout's singleton class: all match.
  • Remaining differences, all the same on main (follow-ups, not findings):
    • IO.instance_methods(false) is 68 vs ruby's 77: the 9 the description names ($stdout does not answer them).
    • IO.private_instance_methods still lists 7 internal Kernel-level names (__arity!, __cycle_size, __each_all, __enum_size, __gather, __int!, __isq_eval); main lists these and more.
    • IO.public_method_defined?(:sysread) is false (ruby true).
  • The description has the local results our merge policy asks for: corpus --both 952/952 plain and VM, vm_check 245/245, the 5-file unit comparison with VM_ONLY 0 (TestIO#test_marshal_closed_io now passes on the VM, the last VM-only failure from the full test/ruby run), and the 8 gates, re-checked after the rebase.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit 4b31ea2 into main Nov 25, 2026
17 of 19 checks passed

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed exact head 9dec67210e6e3261ff94b21891ff6890e60969b5 against its fetched pre-merge main b70ce5c04a269db2f39246dce5d2821faca831e9. Read the prior review, description, discussion, complete diff, surrounding reflection/alias/Method-copy paths and callers. There are no existing inline threads; neither reviewed tree has tracked AGENTS.md/CLAUDE.md instructions.

One new Major / P2 regression: the prelude filter hides aliases explicitly installed by user code on IO/File because aliases copy their source method's location. The inline comment gives a minimal reproduction and suggested correction. A separate plain Method-copy listing discrepancy also occurs on the pre-merge baseline and is excluded from this finding.

Independent Darwin arm64 evidence:

  • Corpus 476, 824, 825 and 826 match clean Ruby 4.0.10 in stdout, stderr and exit status on evaluator/VM with hash indexing on/off: 16 successful comparisons. These controls confirm the intended IO/File inventories, singleton undefinition and socket handling.
  • The alias-only probe fully matches Ruby on the freshly built pre-merge main in all four modes. On the PR head, all four modes lose the eight tested listing results, while respond_to? and the actual read still work. A second probe confirms the same inherited-list/instance-list loss for a __ alias; an ordinary user-written def __user_defined stays visible.
  • Fresh ordinary O2 build of pre-merge main with clean pinned compiler 9dec2ff687a08433247dbdb7729bc19c8522e538 and mgz 661a01d8bb68f5acd68e3192bd47f081d179c436 succeeds. Baseline binary SHA-256: f23480ad105ce1808bc1f33880f47aea96d962c54821203481dcea595d41e9e4.
  • Source checks dup_defs, dead_defs, meth_writes, name_len and ns_names pass; diff whitespace passes.

Head binary reuse: all 2,608 tracked regular files match its build-source checkout byte for byte; its mgz submodule is clean at the pin above and build metadata says O2. Reused binary SHA-256: c1cf1f044777197296667548da8dd9cb709adbeb8966ecce414d424bf3a024f9. No fresh head compilation, complete corpus sweep, full upstream-file comparison, Linux execution or memory/platform acceptance is claimed. Broader description results remain author-reported evidence.

The PR was merged externally at 2026-11-25T16:00:59Z while the independent baseline build was finishing; this session did not initiate that merge. Fetched current main 4b31ea2c33665b62c16a18541b08100ec70f8a7a has exactly the reviewed head's tree 46fb390ccf22454359ce343e87a4a6cde02307be, so the verified regression is present in that main tree as well. The finding still needs a follow-up fix.

Hosted acceptance remains incomplete: PR build-linux is still executing; macOS/reference coverage is queued, two macOS gem runs are cancelled, and downstream coverage is incomplete. The new main CI run 45650399966 is pending with zero jobs exposed at inspection; no passing acceptance or cleared failure is claimed. Recorded hold_all_merges / new_failure_review_required conditions remain active, and no further merges are performed. Already-reviewed ready PRs #254, #272 and #290 retain failed checks and/or unresolved findings; merelang/mere has no open PRs. COMMENT because the authenticated account owns this PR.

Comment thread main.mere
(str_eq cls "IO" || str_eq cls "File")
&& (if own then (if str_contains (" " ++ builtin_own_names cls ++ " ") (" " ++ nm ++ " ") then false else true)
else str_len nm > 2 && str_eq (substring nm 0 2) "__")
&& str_eq (method_text (meth_source_get (cls ++ "#" ++ nm) 1)) "<internal:prelude>"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

[Major / P2] Keep user-created aliases visible in IO/File reflection

Aliases retain the original method's source filename, so <internal:prelude> does not prove that the entry being filtered was installed by the prelude. For example, class File; alias saved_read read; end; p File.instance_methods(false).include?(:saved_read) prints true on Ruby 4.0.10 and the freshly built pre-merge main b70ce5c0, but false here on both evaluator and VM (hash indexing on/off). alias_method :saved_close, :close on File and alias saved_to_s to_s on IO likewise disappear from their own-method lists. An alias named __user_read additionally disappears from inherited instance_methods / public_instance_methods and a File instance's methods / public_methods, while respond_to? and the call still work. Reflection used to discover or wrap application/library-installed aliases therefore loses methods that were previously listed. Restrict hiding to original prelude-installed entries and preserve subsequently created aliases regardless of their copied source location; cover both alias forms and a user alias beginning with __.

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.

1 participant