Skip to content

Commit 188acee

Browse files
sinsokumame
authored andcommitted
Fix constant resolution to prioritize nesting scope over Object ancestors
In Ruby's constant lookup, lexical scope (nesting) takes priority over ancestors. However, `resolve` searched all ancestors including Object before checking outer nesting scopes. This caused `include Store` inside `ActiveRecord::Base` to resolve to `::Store` (app model, via Object) instead of `ActiveRecord::Store` (via nesting). Stop ancestor search at Object when outer nesting scopes remain to be checked (cref.outer), while preserving existing behavior for ScopedConstRead which already passes break_object=true.
1 parent b93d846 commit 188acee

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/typeprof/core/env/static_read.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def resolve(genv, cref, search_ancestors, break_object)
3737
scope = cref.cpath
3838
mod = genv.resolve_cpath(scope)
3939
genv.each_superclass(mod, false) do |mod, _singleton|
40-
break if mod == genv.mod_object && break_object
40+
break if mod == genv.mod_object && (break_object || cref.outer)
4141

4242
unless @source_modules.include?(mod)
4343
@source_modules << mod
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## update: test.rbs
2+
module Outer
3+
class Base
4+
include Inner
5+
end
6+
7+
module Inner
8+
def foo: -> Integer
9+
end
10+
end
11+
12+
## update: test.rb
13+
class Inner < Outer::Base
14+
def bar
15+
foo
16+
end
17+
end
18+
19+
## assert: test.rb
20+
class Inner < Outer::Base
21+
def bar: -> Integer
22+
end

0 commit comments

Comments
 (0)