Skip to content

prelude: Enumerable#to_set warns on arguments, in the six places MRI does and no others - #945

Merged
tannevaled merged 1 commit into
mainfrom
enumerable-to-set-deprecation
Nov 15, 2026
Merged

tannevaled merged 1 commit into
mainfrom
enumerable-to-set-deprecation

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

prelude: Enumerable#to_set warns on arguments, in the six places MRI does and no others

#909: MRI 4.0 warns when Enumerable#to_set is passed arguments and rbgo was
silent -- 206 bytes on stderr against 0. Measured through cmd/oracle against
MRI 4.0.7, and THREE of the six cases are not what "with arguments warns" would
lead anyone to write:

[1,2].to_set silent
[1,2].to_set(Set) warns <- even though Set IS the default value
[1,2].to_set(MySet) warns
[1,2].to_set { … } SILENT <- a block alone is not "arguments"
(1..3).to_set silent
(1..3).to_set(Set) warns
Set[1,2].to_set(Set) SILENT <- Set#to_set does not warn at all
under Warning[:deprecated] = false STILL WARNS

Set is a C core class in 4.0, so there is no set.rb to read: the behaviour is
the spec, and the oracle is how it was obtained.

Arity, not value

def to_set(klass = Set, *args) cannot express rows 1 and 2: inside the method
to_set and to_set(Set) are identical -- klass == Set, args empty, both
times. So the signature is *args and the test is args.empty?.

And the warning is a plain warn, NOT the :deprecated category, because MRI
still emits it under Warning[:deprecated] = false. Routing it through the
category would silence something MRI does not silence.

Range#to_set needed two changes, neither of which alters what it returns

  1. *args instead of klass = Set. The C it quotes in its own comment forwards
    argc/argv VERBATIM (rb_call_super(argc, argv)) and a default parameter does
    not: a bare range.to_set was reaching Enumerable#to_set carrying an explicit
    Set. Harmless while that method ignored the difference, a false warning the
    moment it stopped.
  2. It warns itself and calls a warning-free core instead of super, so there is
    exactly one warning and its uplevel is computed from a known depth.

⚠ So the call shape no longer mirrors rb_call_super while the RESULT still does.
The C can forward blindly because both to_set are C and add no Ruby frame; a
prelude written in Ruby cannot.

⛔ The line number, and a test that could not see it

With the warning at uplevel: 1 the prefix mixed the CALLER'S PATH with the
PRELUDE'S LINE:

MRI probe.rb:15 probe.rb:16
rbgo probe.rb:214 probe.rb:1208 <- in a 16-line file

A real file at an impossible line. Same shape as a line number that belongs to
the parser rather than the document.

⛔ And the first version of the test asserted msg.include?("prelude") == false
and PASSED under that mutation -- because the message never says "prelude", it
says the user's filename with the prelude's line. A test written for a property
it cannot see is worse than no test. The assertion is now on the NUMBER, taken
from __LINE__ at the call site, and the mutation turns it red on BOTH paths.

Checks

  • cmd/oracle with MRI 4.0.7 and this build: 2 engines AGREED, byte for byte,
    stdout AND stderr (1230 bytes, all six warnings on the caller's lines).
  • gotest over the new test plus TestEmbeddedPreludeMatchesSource (the prelude
    drift gate), TestASetSubclassReportsItsOwnClass and the three Set suites: all
    green, 2.685s.
  • gofmt -l . silent; the frozen prelude regenerated with cmd/freeze-prelude.

⚠ Not verified locally: whether the new warning changes any ruby/spec file's
count. core/enumerable/to_set_spec.rb should gain the example it was failing,
but a spec that calls to_set with a class and asserts silence would lose one.
The rubyspec-ratchet lane is the judge of that and I have not pre-empted it.

Refs #909

Co-Authored-By: Claude Opus 5 noreply@anthropic.com

…does and no others

#909: MRI 4.0 warns when Enumerable#to_set is passed arguments and rbgo was
silent -- 206 bytes on stderr against 0. Measured through cmd/oracle against
MRI 4.0.7, and THREE of the six cases are not what "with arguments warns" would
lead anyone to write:

  [1,2].to_set          silent
  [1,2].to_set(Set)     warns     <- even though Set IS the default value
  [1,2].to_set(MySet)   warns
  [1,2].to_set { … }    SILENT    <- a block alone is not "arguments"
  (1..3).to_set         silent
  (1..3).to_set(Set)    warns
  Set[1,2].to_set(Set)  SILENT    <- Set#to_set does not warn at all
  under Warning[:deprecated] = false   STILL WARNS

Set is a C core class in 4.0, so there is no set.rb to read: the behaviour is
the spec, and the oracle is how it was obtained.

## Arity, not value

`def to_set(klass = Set, *args)` cannot express rows 1 and 2: inside the method
`to_set` and `to_set(Set)` are identical -- klass == Set, args empty, both
times. So the signature is `*args` and the test is `args.empty?`.

And the warning is a plain `warn`, NOT the :deprecated category, because MRI
still emits it under `Warning[:deprecated] = false`. Routing it through the
category would silence something MRI does not silence.

## Range#to_set needed two changes, neither of which alters what it returns

1. `*args` instead of `klass = Set`. The C it quotes in its own comment forwards
   argc/argv VERBATIM (`rb_call_super(argc, argv)`) and a default parameter does
   not: a bare `range.to_set` was reaching Enumerable#to_set carrying an explicit
   Set. Harmless while that method ignored the difference, a false warning the
   moment it stopped.
2. It warns itself and calls a warning-free core instead of `super`, so there is
   exactly one warning and its uplevel is computed from a known depth.

⚠ So the call shape no longer mirrors rb_call_super while the RESULT still does.
The C can forward blindly because both to_set are C and add no Ruby frame; a
prelude written in Ruby cannot.

## ⛔ The line number, and a test that could not see it

With the warning at `uplevel: 1` the prefix mixed the CALLER'S PATH with the
PRELUDE'S LINE:

  MRI   probe.rb:15    probe.rb:16
  rbgo  probe.rb:214   probe.rb:1208      <- in a 16-line file

A real file at an impossible line. Same shape as a line number that belongs to
the parser rather than the document.

⛔ And the first version of the test asserted `msg.include?("prelude") == false`
and PASSED under that mutation -- because the message never says "prelude", it
says the user's filename with the prelude's line. A test written for a property
it cannot see is worse than no test. The assertion is now on the NUMBER, taken
from `__LINE__` at the call site, and the mutation turns it red on BOTH paths.

## Checks

- `cmd/oracle` with MRI 4.0.7 and this build: 2 engines AGREED, byte for byte,
  stdout AND stderr (1230 bytes, all six warnings on the caller's lines).
- `gotest` over the new test plus TestEmbeddedPreludeMatchesSource (the prelude
  drift gate), TestASetSubclassReportsItsOwnClass and the three Set suites: all
  green, 2.685s.
- `gofmt -l .` silent; the frozen prelude regenerated with cmd/freeze-prelude.

⚠ Not verified locally: whether the new warning changes any ruby/spec file's
count. core/enumerable/to_set_spec.rb should gain the example it was failing,
but a spec that calls to_set with a class and asserts silence would lose one.
The rubyspec-ratchet lane is the judge of that and I have not pre-empted it.

Refs #909

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 38b3ff1 into main Nov 15, 2026
26 checks passed
@tannevaled
tannevaled deleted the enumerable-to-set-deprecation branch November 15, 2026 14:20
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