Skip to content

A top-level return with a value, and ruby's warning of it (round 65) - #257

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

284km merged 1 commit into
mainfrom
stage10-r65

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Round 65 of the VM stream (note 558, item 1). The native compiler compiles a top-level return with a value, and ruby's warning of it is said as ruby says it on both executions. Two things found on the way are in it too: the evaluator read a comment that only mentions shareable_constant_value as the magic comment, and round 63 left mere-ruby's embedded copy of the Ruby compiler stale.

The CI checks of #232 follow this PR: one of them checks that the embedded copy is current, and fails on main until this is merged.

The record is loop/2026-11-25-stage10-r65.md; what follows is it.

After round 64 the native compiler declined nothing in the corpus's main scripts. Among the files those programs require, it declined two reasons: a BEGIN block, the evaluator's by design, and a top-level return with a value. This round compiles the second.

What ruby does

A file's own return ends the file, and a value given to it goes nowhere. ruby says so as it compiles the file, before any of it runs:

D/lib.rb: warning: argument of top-level return is ignored
  • once for each such return of the top level, a rescue or an ensure clause's included (one in a method or a block is another scope);
  • with the file and no line;
  • for the main script, a required file and a loaded one alike;
  • not under -W0.

The compiled sequence is the plain return's: the value is pushed and the frame left.

What it was here

ruby the evaluator the VM
when at the compile, before the file runs after the main script had run, the last thing printed at the compile
how often once a return once, for the main script's one that ran once a return
where the file the file the file and the line
a required file's said not said said
a loaded file's said not said not said (it runs on the evaluator)

The change

  • The native compiler compiles the return and counts it (meta 11). ic_vm_native says the warning that many times once the file is loaded.
  • The Ruby compiler says it with the file and no line, and of a rescue or an ensure clause's return too.
  • The evaluator counts a file's top-level returns of its statements (top_ret_args, which now looks through a rescue clause, and / or and a statement's wrapping) and says the warning before the file runs: the main script, a required file the VM does not take, and a loaded file. A file the VM takes is told of by its compiler.
  • The parse's own warnings are said before the compile's, as ruby's are: "statement not reached" comes first.

Found on the way: a magic comment that was only a mention

With the Ruby compiler in use (MERE_RUBY_IC=ruby), every run said

.../__iseq_compiler.rb:3747: warning: invalid value for shareable_constant_value: a

That line is a comment of round 63's: # pm_compile_shareable_constant_value: a literal is pushed.... The evaluator's lexer took the name anywhere in a comment for the magic comment. ruby takes it only as the comment's key: right after the #, or after a ; or the -*- of the emacs form. So # a-shareable_constant_value: literal and # foo shareable_constant_value: literal are no magic comment, and here they were. The lexer reads the key's place now.

And the embedded copy was stale. mere-ruby carries the Ruby compiler as a string, generated from lib/iseq_compiler_ext.rb into m_pathname_lib.mere by tools/gen_pathname_lib.rb. Round 63 (#233) changed the lib and did not regenerate the copy. So on main the Ruby compiler that mere-ruby runs still declined the shareable constant writes that round 63 taught it, and compare_files.rb, which runs the lib under ruby, could not see it. The copy is regenerated here, and the CI checks #232 proposed, in a PR that follows this one, check that it is current.

Numbers

The native compiler now declines, among every file the corpus's programs require or load, only the BEGIN blocks: three files.

Corpus 823 runs children and reads their two streams as one: a main script, two required files with returns in an if, a begin with rescue, else and ensure, a block, a method and an and, a loaded file, a bare return; under no flag, -W0 and -w. And the magic comment's forms. It matches ruby on both executions, and on the VM through the Ruby compiler too; on main's build it differs on both.

Recorded, as on main: the VM through the Ruby compiler

The Ruby compiler is the VM's when the native one declines a file, which in the corpus is nowhere now, and under Coverage's branch mode, where it is the one that compiles the branches in. With it forced for every file (MERE_RUBY_IC=ruby), three corpus programs differ from ruby: 675, 725 and 819. They differ the same way on main's build. 819 is round 62's literal Hash key, which the native compiler's values intern and the Ruby compiler's do not. Four more fall back there, the sources in other encodings, which the Ruby compiler does not take.

Checked

Against ruby 4.0.10 (tools/ref_ruby.sh), built with Mere 9dec2ff6, on main 66a7afd (#235; main has moved since by #232 and #236, which change no code this touches):

  • corpus 878/878. Under MERE_RUBY_VM=1: 878 on the VM, 0 fell back, 0 different.
  • Corpus 823 differs on main's build, on both executions.
  • Every corpus program as a required file (round 60's check), on both executions: 872 the same as ruby on both, 5 different on both, 1 on the evaluator only, 0 on the VM only. The six are round 60's six.
  • vm_check 245/245.
  • The gates: dead defs, dup defs, gc roots, bracket depth.
  • tools/iseq/native/check.sh over the corpus and four files of returns: 882 the same as the Ruby compiler, 0 refused, 0 different. compare_files.rb over the same: 882 the same as ruby. compare_with_ruby.rb: 460 the same.
  • tools/gen_pathname_lib.rb gives m_pathname_lib.mere as committed.
  • The CI corpus job's OpenSSL checks: asn1, provider, formats, reinitialize, memory, wasm.
  • These give the same counts as main 66a7afd's build, on both executions: test_require, test_syntax, test_parse, test_iseq, test_eval, test_rubyoptions and test_compile_prism.
  • On main 323706a with this change, the tree that was pushed: corpus 893/893; under MERE_RUBY_VM=1, 893 on the VM, 0 fell back, 0 different; vm_check 245/245; the gates; m_pathname_lib.mere as generated.

🤖 Generated with Claude Code

The native compiler compiles a file's own return with a value, and counts
it. ruby says "argument of top-level return is ignored" as it compiles a
file, before it runs, once a return, with the file and no line: said so now
on both executions, for the main script, a required file and a loaded one.

The evaluator read a comment that only mentions shareable_constant_value as
the magic comment; it reads the comment's key now. One of round 63's
comments in the Ruby compiler warned each time that compiler was loaded.

m_pathname_lib.mere, the embedded copy of the Ruby compiler, is generated
again: round 63 changed the lib and not the copy.

Corpus 823.

@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 699f0ae. No Critical / Major / Minor findings that block.

  • What changed:
    • The native compiler compiles a top-level return with a value.
    • "argument of top-level return is ignored" is reported as ruby does: at compile time, once per such return, and with the file name only. This covers main scripts, require and load, on both modes.
    • The evaluator reads a magic comment only at a key position, so a comment line that merely mentions shareable_constant_value: no longer sets it.
    • The embedded copy of the Ruby compiler (m_pathname_lib.mere) is regenerated. It had missed round 63's shareable compile.
  • Probe on the author's build at this head (clean tree, binary newer than the sources), against ruby 4.0.10, the evaluator, MERE_RUBY_VM=1, and main 323706a + #253 on both modes. The script requires a file containing return 1 if true and another with a bare return, then loads the first. It also requires a file whose first line is # foo shareable_constant_value: literal under -*- frozen_string_literal: true -*-, and ends with return 2.
    • The warnings (which, how many, in what order, file names only), the outputs and the frozen states match ruby on both modes.
    • main's evaluator printed one warning at the end and treated the commented line as a magic comment.
    • main's VM added line numbers.
  • Follow-up, the same on main's evaluator: # frozen_string_literal: false; shareable_constant_value: literal on a plain # line (no -*- … -*-) makes Y = [1] frozen on the evaluator. ruby honours only the first key there, and the VM agrees with ruby (false). ruby reads ;-separated keys only inside the emacs-style -*- … -*- form.
  • git merge-tree against current main (44aa511): clean. No mere-pr-review-watch inline comments.
  • Local results in the description meet the merge criteria:
    • on 66a7afd: the full set;
    • on 323706a: corpus 893/893, VM 893 / 0 / 0, vm_check and gates;
    • the MERE_RUBY_IC=ruby differences (675, 725, 819) are recorded as the same as main.

Merging.

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