Support class Foo = Bar / module Foo = Bar declarations in RBS#444
Merged
mame merged 1 commit intoruby:masterfrom Apr 29, 2026
Merged
Support class Foo = Bar / module Foo = Bar declarations in RBS#444mame merged 1 commit intoruby:masterfrom
class Foo = Bar / module Foo = Bar declarations in RBS#444mame merged 1 commit intoruby:masterfrom
Conversation
The empty `when RBS::AST::Declarations::AliasDecl` branch in `AST.create_rbs_decl` was previously a no-op (the prior fix only avoided the crash). This commit implements actual semantic resolution for class and module aliases, so that: - `module YAML = Psych` makes `YAML.load`, `YAML::SyntaxError`, and `include YAML` resolve identically to `Psych.load` etc. - `class HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess` (used in gem_rbs_collection's activesupport RBS) resolves correctly. Implementation (Option A from the design discussion): - `ModuleEntity` gains `@alias_decls` (decl => target_mod) and `@alias_target`, with `add_alias_decl`/`remove_alias_decl` mirroring the existing module decl lifecycle. The alias decl is also registered as a const on the outer module so const lookup of the alias name succeeds. - `Genv#resolve_cpath` follows `alias_target` after resolving each cpath segment, with cycle detection to handle `module A = B; module B = A`. - New AST nodes `SigClassAliasNode` / `SigModuleAliasNode` register the alias via the standard `define`/`undefine`/`install` lifecycle. They access the alias's own `ModuleEntity` via `outer.inner_modules[cname]` directly to avoid being short-circuited by `resolve_cpath`'s alias following. - `AST.create_rbs_decl` replaces the empty `when AliasDecl` branch with explicit `when ClassAlias` / `when ModuleAlias` branches. Tests cover singleton method, nested constant, instance method, mixin, nested-module alias, and the cycle case.
7a9d46e to
ba9a78e
Compare
Member
|
@rhiroe Thanks! The PR looks good to me. Is it still marked as Draft because you're planning to do more work on it? If not, I'll go ahead and merge. |
3 tasks
Contributor
Author
|
@mame I've marked this PR as ready for review. |
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implement RBS class alias and module alias declarations (
class Foo = Bar,module Foo = Bar).Builds on #443, which fixed the
NoMethodErrorcaused by these declarations being silently skipped without.compact. With that crash gone, the alias was still being dropped — everyYAML.load/HashWithIndifferentAccess.fooetc. came back untyped. This PR makes those resolve.After this PR,
module YAML = Psychmakes the following resolve identically toPsych:YAML.load(str)YAML::SyntaxErrorclass A; include YAML endThis also unblocks real-world RBS sources such as
module YAML = Psych(commonly authored by users),module Sidekiq::ClientMiddleware = Sidekiq::ServerMiddleware(gem_rbs_collection sidekiq), andclass HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess(gem_rbs_collection activesupport).Approach
The change centered on
Genv#resolve_cpath:ModuleEntitygains@alias_decls(decl ⇒ target_mod) and@alias_target, withadd_alias_decl/remove_alias_declmirroring the existing module decl lifecycle. The alias decl is also registered as a const on the outer module so const lookup of the alias name succeeds.Genv#resolve_cpathfollowsalias_targetafter resolving each cpath segment, with cycle detection to handlemodule A = B; module B = A.SigClassAliasNode/SigModuleAliasNoderegister the alias via the standarddefine/undefine/installlifecycle. They access the alias's ownModuleEntityviaouter.inner_modules[cname]directly to avoid being short-circuited byresolve_cpath's alias following.when RBS::AST::Declarations::AliasDeclbranch inAST.create_rbs_declis replaced with explicitwhen ClassAlias/when ModuleAliasbranches.Test plan
scenario/rbs/class-module-alias.rb: class alias instance method, module alias singleton method / nested constant / mixinscenario/rbs/class-module-alias-nested.rb: alias declared inside a module (module Outer; module InnerAlias = Inner; end)scenario/rbs/class-module-alias-cycle.rb: cycle (module A = B; module B = A) does not infinite-loopbundle exec rake test— 417 tests, 772 assertions, 0 failuresKnown limitations / follow-ups