Skip to content

kernel.c: give BasicObject the helpers defined? compiles to - #7838

Merged
matz merged 1 commit into
mruby:masterfrom
amatsuda:defined-basic-object
Nov 17, 2026
Merged

matz merged 1 commit into
mruby:masterfrom
amatsuda:defined-basic-object

Conversation

@amatsuda

@amatsuda amatsuda commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

In a class that does not inherit from Object, every form of defined? calls method_missing, or raises NoMethodError:

class Foo < BasicObject
  def ivar? = defined?(@x)
end
Foo.new.ivar?
# CRuby: nil
# mruby: undefined method '__defined_ivar?' (NoMethodError)

defined?(@x), defined?(X), defined?(A::B), defined?(@@x), defined?($x), defined?(yield), defined?(super) and defined?(meth) compile to a call on self to a private helper (__defined_ivar? and the like). The helpers are Kernel's, and a BasicObject subclass has no Kernel.

A class with a method_missing that forwards gets that call instead. CRuby's delegate.rb then recurses until the stack is exhausted: SimpleDelegator#__getobj__ asks defined?(@delegate_sd_obj), which goes to Delegator#method_missing, which calls __getobj__.

Fix

The helpers move from Kernel to BasicObject, in a ROM table of their own. They stay private. Objects that inherit from Object find them as before, one step further up.

Test

test/t/basicobject.rb asks each form of defined? in a BasicObject subclass whose method_missing raises. The answers are the same as CRuby 4.1's.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • defined? now correctly reports definitions and missing names for objects that inherit directly from BasicObject.
    • Checks cover instance, constant, class-variable, and global-variable references, as well as methods, blocks, and super.
    • Existing private visibility is preserved.

`defined?(@x)`, `defined?(X)`, `defined?(yield)` and the other forms
compile to a call on self to a private helper such as __defined_ivar?.
The helpers were Kernel's, so in a class that does not inherit Object
(a BasicObject subclass) every `defined?` called method_missing, or
raised NoMethodError, where CRuby answers. CRuby's delegate.rb recursed
without end on it: SimpleDelegator#__getobj__ asks defined?(@obj), and its
method_missing calls __getobj__. The helpers are now BasicObject's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X4cowTc1oGMNQfUma7zNdD
@amatsuda
amatsuda requested a review from matz as a code owner November 17, 2026 15:50
@github-actions github-actions Bot added the core label Nov 17, 2026
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The private defined? helpers are registered in a separate method table installed on BasicObject. Tests add a BasicObject subclass that checks results for defined and missing values and expressions.

Changes: BasicObject defined? helpers

Layer / File(s) Summary
Register and test defined? helpers
src/kernel.c, test/t/basicobject.rb
The private helpers move to a separate method table installed on BasicObject, while Kernel methods remain in their own table. Tests check results for defined and missing variables, constants, methods, blocks, and super.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: core

Merge Risk: Minimal · up to e23a9

This change makes defined? work in classes that inherit directly from BasicObject. No actionable merge-blocking risk was found, and the new test covers the main forms of defined?.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the helpers that compiled defined? expressions use are moved to BasicObject so defined? works in BasicObject subclasses. The title also names the file kernel.c. The ra…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the helpers that compiled defined? expressions use are moved to BasicObject so defined? works in BasicObject subclasses. The title also names the file kernel.c. The raw summary and objectives both describe this relocation from Kernel to BasicObject. The title is short and specific, and it does not describe anything absent from the changeset. Title style is a minor point: the title is a single sentence and is concise.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 823bdc2d-f270-489b-8aa1-38f3a74e912a
Commits

Reviewing files that changed from the base of the PR and between 961fb61 and e23a9ff.

Files selected for processing: 2

  • src/kernel.c
  • test/t/basicobject.rb

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@matz
matz merged commit 8ffaa46 into mruby:master Nov 17, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants