Skip to content

conformance shim: _guard_run returns nil where mspec returns the block's value, so a guard used as an expression contributes nothing #962

Closed

Description

@tannevaled

_guard_run returns nil where mspec returns the block's value, so a guard used as an expression contributes nothing.

Found while measuring #837 on the ubuntu lane. It is not caused by that change and it is not fixed by it.

The divergence

scripts/conformance/rubyspec/spec_helper.rb:855-859:

def _guard_run(match)
  return match unless block_given?
  yield if match
  nil
end

ruby/mspec lib/mspec/guards/guard.rb:77-86, read at the tip 2026-11-20:

def run_if(name, &block)
  @name = name
  if block
    yield if yield?(false)
  else
    yield?(false)
  end
ensure
  unregister
end

yield if yield?(false) returns the block's value. (ensure does not change a
method's return value, so unregister is not in the way.) Our shim returns a
hardcoded nil instead.

For the overwhelmingly common use — a guard wrapping a describe or a set of
its, whose value nobody reads — the two are indistinguishable. They part
company the moment a guard is used as an expression.

Where it costs us, measured

core/file/stat/birthtime_spec.rb:3-5 of the pinned corpus:

platform_is(:windows, :darwin, :freebsd, :netbsd,
            *ruby_version_is("4.0") { :linux },
           ) do

The corpus is saying "and, from Ruby 4.0, linux too". At CUR_VERSION = 4.0.x:

ruby_version_is("4.0") { :linux } the splat adds
MRI / mspec :linux :linux
this shim nil nothing

So the whole file is guarded for a platform list that never contains
:linux
, and on the gate's own ubuntu runner it is skipped — at a version
where upstream runs it.

It was invisible until now for a reason worth recording: the shim's old
hardcoded PLATFORMS = [:darwin, :bsd, :unix] matched :darwin on every
machine, so the file ran on ubuntu by accident, through a second defect.
Removing that literal (#961) makes the first one visible as a -1:

core/file/stat/birthtime_spec.rb: 0 passing, baseline 1 (-1)

⭐ Two defects were cancelling, and the measurement only became possible once
one of them was gone. That is the same shape as the two-errors-cancel
lesson
the parity plan
opens with, and it is the argument for fixing one thing at a time.

The fix, and why it is not in #961

def _guard_run(match)
  return match unless block_given?
  yield if match
end

One line. It is deliberately not in #961, because both changes select
examples and #837's own text asks that a population move be measured before
UPDATE_BASELINE=1 records it. Two moves in one commit cannot be attributed to
either. This one lands next, measured on its own.

What has to be checked with it

The census owes an answer to "how many sites use a guard as an expression", not
just this one. A static reader has to look for a guard appearing after *, =,
?, and/or, or inside an argument list — cmd/platguard already classifies
such sites as used as a VALUE for platform_is (5 on linux), and
cmd/guards has the same notion for ruby_version_is (inline sites). Neither
counts the splat form yet.

⛔ And the gate for the fix is not "birthtime_spec goes green". It is a test
that a guard used as an expression yields the block's value and a guard whose
condition is false yields nil — two sides, because a _guard_run that
returned the block's value unconditionally would also turn this file green.

Prior art: priorart --repo go-embedded-ruby/ruby "_guard_run returns nil where mspec run_if returns the block value, splat of a guard result" — 1210 tracker
entries matched on broad terms, none on this subject; 8 branches, none of them
about the guards' return value.

Refs #837, #961, #941

🤖 Generated with Claude Code

Prior art: priorart "_guard_run returns nil where mspec run_if returns the block value, splat of a guard result" at 2026-11-20 16:24 CET reported prior art; checked: /Users/david_delavennat/Documents/VCS/GIT/localhost/_conform/pa-guardrun.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions