Skip to content

vm.c: do not cache what const_missing gives for a lexical read - #7835

Merged
matz merged 1 commit into
mruby:masterfrom
amatsuda:const-missing-cache
Nov 17, 2026
Merged

matz merged 1 commit into
mruby:masterfrom
amatsuda:const-missing-cache

Conversation

@amatsuda

@amatsuda amatsuda commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

OP_GETCONST caches the value a lexical constant read gives, and it cached the value of a const_missing hook as if it were the constant. Later reads at the same place then got that value without calling the hook again:

module M
  @n = 0
  def self.const_missing(name) = (@n += 1)
  def self.read = Missing
end

M.read  #=> 1
M.read  #=> 1, 2 in CRuby

The value stays until the entry is dropped, by a collision or by setting any constant, and the hook is skipped for each read it serves. CRuby calls const_missing for each read.

Fix

const_get() clears *cachep when it calls const_missing, the way a deprecated constant already keeps itself out of the cache (3a118cb). A read the hook answers is then not cached. A read that finds the constant is cached as before.

This also closes the other way a stale value could be cached: Ruby code run during the lookup setting the constant before OP_GETCONST stores what it found. Of the code a lookup can run, an autoload reads the constant again after loading, a deprecation warning is not cached, and const_missing is now not cached either.

Tests

test/t/module.rb: a lexical read calls const_missing each time, and reads the constant once it is set.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Unresolved constants that use const_missing are no longer cached, so each read calls const_missing again. Once the constant is defined, reads return its stored value.

OP_GETCONST cached the value of a const_missing hook as if it were the
constant, so later reads at that place gave the same value without
calling the hook, until the entry was dropped. CRuby calls the hook for
each read.

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 16, 2026 23:39
@github-actions github-actions Bot added the core label Nov 16, 2026
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

For an unresolved constant, const_get now clears cachep before calling const_missing. A test checks that repeated reads call the hook again, then verifies that reads return the stored value after the constant is set.

Changes: Constant Lookup Cache

Layer / File(s) Summary
Unresolved constant lookup and cache behavior
src/variable.c, src/vm.c, test/t/module.rb
const_get clears cachep before calling const_missing for an undefined constant. The OP_GETCONST comment documents that values from const_missing are not cached. The test checks repeated hook calls and reads after setting the constant.

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

Change: Bug fix

Possibly related PRs

  • mruby/mruby#7760: Changes Module#const_get lookup behavior and its const_missing path.
  • mruby/mruby#7798: Adds pending autoload sentinels that can load a file and define a constant during lookup.
Merge Risk: Minimal · up to aa52f

This change stops mruby from caching values returned by const_missing, so the hook is called on each lexical constant read as in CRuby. The edit is small and has test coverage, so merge risk is minimal.

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: a lexical constant read no longer caches the value returned by const_missing. The raw summary confirms const_get clears cachep when const_missing is called, and the vm…
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: a lexical constant read no longer caches the value returned by const_missing. The raw summary confirms const_get clears cachep when const_missing is called, and the vm.c comment now says such values are not cached. The title is specific and matches the changeset, so it passes.

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: ea839199-52ca-497b-b2b9-888e28e5bf03
Commits

Reviewing files that changed from the base of the PR and between 81fd905 and aa52f0e.

Files selected for processing: 3

  • src/variable.c
  • src/vm.c
  • test/t/module.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 d442a67 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