Skip to content

A magic comment in ruby's two forms, and only those - #266

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

284km merged 1 commit into
mainfrom
stage10-magic-semicolon

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

A follow-up to #257, from its review. The record is loop/2026-11-25-stage10-magic-comment-forms.md; what follows is it.

A follow-up to #257, from its review. #257 made the evaluator read shareable_constant_value only as a comment's key, so that a comment mentioning the name (one of round 63's, in the Ruby compiler) was no magic comment. It took a key after a ; anywhere, and the review found the case that gets wrong:

# frozen_string_literal: false; shareable_constant_value: literal
A = [1]
A.frozen?   # false in ruby; true on the evaluator; false on the VM

ruby's rule

Read off ruby 4.0.10 for 25 comments, each before a constant written after a statement:

  • the plain form is one pair and nothing else: # key: value, blanks allowed around the colon and after the value. # shareable_constant_value: literal; foo: bar, # ... literal x and # frozen_string_literal: false; shareable_constant_value: literal set nothing;
  • the emacs form is the text between two -*-: pairs separated by ; or by blanks, a word with no : passed over (# -*- foo shareable_constant_value: literal -*- is read);
  • the key is a word of its own: # a-shareable_constant_value: literal and # foo shareable_constant_value: literal are none.

The change

scv_value reads the two forms so. All 25 comments give ruby's answer on both executions (the VM's main script is read by the evaluator's lexer too).

And the warning of a late frozen_string_literal. "'frozen_string_literal' is ignored after any tokens" was said for a plain comment that only started with the pair (# frozen_string_literal: true; shareable_constant_value: none), which ruby does not take for a magic comment and says nothing of; and it was not said for the emacs form (# -*- frozen_string_literal: true -*-), which ruby warns of. fsl_comment_text reads the same two forms. This was on main, on both executions.

Corpus 823's file of magic comments has the forms.

Checked

Against ruby 4.0.10 (tools/ref_ruby.sh), built with Mere 9dec2ff6, on main 8b1b952 (and spot-checked on ec98015: corpus 823, the 25 comments, vm_check):

  • corpus 894/894 (one program, 293, differed in the first sweep at a load of 30, and matches in a sweep again). Under MERE_RUBY_VM=1: 894 on the VM, 0 fell back, 0 different.
  • Corpus 823 differs on main's build on both executions.
  • vm_check 245/245.
  • The gates: dead defs, dup defs, gc roots, bracket depth.
  • These give the same counts as main's build, on both executions: test_parse, test_syntax, test_rubyoptions and test_literal.

🤖 Generated with Claude Code

The plain form is one pair and nothing else; the emacs form is the pairs
between two -*-, separated by ; or blanks. The evaluator took a key after a
; anywhere: # frozen_string_literal: false; shareable_constant_value:
literal made constants shareable. The warning of a late
frozen_string_literal reads the same two forms.

@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 41b55e5. No findings.

  • The evaluator now reads magic comments by ruby's rules.

    • A plain # comment counts only when the whole comment is exactly one key: value pair; a second pair after ; and trailing text are ignored.
    • The emacs -*- … -*- form reads pairs separated by ; or spaces, and skips words without :.
    • The late-frozen_string_literal warning follows the same rule.
  • Ran 12 comment forms on the author's build at this head (clean tree, binary newer than the sources). Each form was followed by A = [1] and S = "s", with -W1, against ruby 4.0.10, the evaluator and MERE_RUBY_VM=1. The forms:

    • a plain pair, and a plain pair followed by ; second;
    • -*- with one pair, two pairs, and coding plus a pair;
    • no space; dashes in the key; an upper-case key;
    • a trailing # …; a vim: line; extra spaces.

    Output and warnings are identical on all three for every form.

  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments. Local results in the description: corpus 894/894, VM 894 / 0, vm_check, gates, 4 unit files equal to main. These meet the merge criteria. Merging.

@284km
284km merged commit f52b0f5 into main Nov 25, 2026
5 of 19 checks passed
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