Skip to content

Kernel#eval: a bare eval(str) sees the caller's local variables - #441

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster13
Aug 24, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster13

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

eval(str) with no explicit binding ran against a fresh local scope, so
it could not read or assign the caller's local variables — a reference
to a caller local raised NameError, and eval("x = 1") did not write
back to the caller's x. MRI evaluates a bare eval against the
caller's binding.

The compiler now rewrites a syntactic eval(str) (no receiver, no
block, exactly one argument) to eval(str, binding), reusing the
binding intrinsic that already captures the current frame, so the
evaluated code resolves and mutates the caller's locals through the
existing eval(str, binding) path. bindingEval is made transparent to
__method__ / __callee__ (it now inherits the caller's method context,
as the no-binding path did), so eval("__method__") inside a method
still reports that method. An explicit-receiver form (Kernel.eval(str))
keeps the plain fresh-scope behaviour.

Verified against ruby 4.0.6; a full language+core sweep shows +39
(core/kernel/eval_spec 20 → 36, language/def_spec 33 → 47,
core/module/{private,protected,public}_spec +6, and more) with no
regressions, and the whole-suite coverage gate stays at 100.0%.

🤖 Generated with Claude Code

eval(str) with no explicit binding ran against a fresh local scope, so
it could not read or assign the caller's local variables — a reference
to a caller local raised NameError, and eval("x = 1") did not write back
to the caller's x. MRI evaluates a bare eval against the caller's
binding.

The compiler now rewrites a syntactic eval(str) (no receiver, no block,
exactly one argument) to eval(str, binding), reusing the binding
intrinsic that already captures the current frame, so the evaluated code
resolves and mutates the caller's locals through the existing
eval(str, binding) path. bindingEval is made transparent to
__method__ / __callee__ (it now inherits the caller's method context, as
the no-binding path did), so eval("__method__") inside a method still
reports that method.

Verified against ruby 4.0.6; core/kernel/eval_spec 20 -> 36. Whole-suite
coverage gate stays at 100.0% and a full language+core sweep shows no
regressions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit beeb486 into main Aug 24, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster13 branch August 24, 2026 15:16
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