Skip to content

Commit

Permalink
Merge pull request #1460 from ksss/runtime-mixin
Browse files Browse the repository at this point in the history
[prototype runtime] Support prepend mixin
  • Loading branch information
soutaro committed Aug 25, 2023
2 parents 0ecafa8 + 279f27b commit 58ea286
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
49 changes: 19 additions & 30 deletions lib/rbs/prototype/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,20 @@ def to_type_name(name, full_name: false)
end
end

def each_included_module(type_name, mod)
private def each_mixined_module(type_name, mod)
each_mixined_module_one(type_name, mod) do |module_name, module_full_name, is_prepend|
yield module_name, module_full_name, is_prepend ? AST::Members::Prepend : AST::Members::Include
end
each_mixined_module_one(type_name, mod.singleton_class) do |module_name, module_full_name, _|
yield module_name, module_full_name, AST::Members::Extend
end
end

private def each_mixined_module_one(type_name, mod)
supers = Set[]

prepends = mod.ancestors.take_while { |m| !mod.equal?(m) }.to_set

mod.included_modules.each do |mix|
supers.merge(mix.included_modules)
end
Expand All @@ -89,8 +100,8 @@ def each_included_module(type_name, mod)
end
end

mod.included_modules.each do |mix|
unless supers.include?(mix)
mod.included_modules.uniq.each do |mix|
if !supers.include?(mix) || prepends.include?(mix)
unless const_name(mix)
RBS.logger.warn("Skipping anonymous module #{mix} included in #{mod}")
else
Expand All @@ -99,7 +110,7 @@ def each_included_module(type_name, mod)
module_name = TypeName.new(name: module_full_name.name, namespace: Namespace.empty)
end

yield module_name, module_full_name, mix
yield module_name, module_full_name, prepends.include?(mix)
end
end
end
Expand Down Expand Up @@ -406,20 +417,9 @@ def generate_class(mod)
outer_decls << decl
end

each_included_module(type_name, mod) do |module_name, module_full_name, _|
args = type_args(module_full_name)
decl.members << AST::Members::Include.new(
name: module_name,
args: args,
location: nil,
comment: nil,
annotations: []
)
end

each_included_module(type_name, mod.singleton_class) do |module_name, module_full_name ,_|
each_mixined_module(type_name, mod) do |module_name, module_full_name, mixin_class|
args = type_args(module_full_name)
decl.members << AST::Members::Extend.new(
decl.members << mixin_class.new(
name: module_name,
args: args,
location: nil,
Expand Down Expand Up @@ -460,20 +460,9 @@ def generate_module(mod)
outer_decls << decl
end

each_included_module(type_name, mod) do |module_name, module_full_name, _|
args = type_args(module_full_name)
decl.members << AST::Members::Include.new(
name: module_name,
args: args,
location: nil,
comment: nil,
annotations: []
)
end

each_included_module(type_name, mod.singleton_class) do |module_name, module_full_name, _|
each_mixined_module(type_name, mod) do |module_name, module_full_name, mixin_class|
args = type_args(module_full_name)
decl.members << AST::Members::Extend.new(
decl.members << mixin_class.new(
name: module_name,
args: args,
location: nil,
Expand Down
14 changes: 14 additions & 0 deletions test/rbs/runtime_prototype_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ module Foo
module Bar
end

module Baz
end

class Test < String
include Foo
extend Bar
prepend Baz

NAME = "Hello"

Expand Down Expand Up @@ -52,13 +56,18 @@ module TestTargets
module Bar
end
module Baz
end
module Foo
include Enumerable[untyped]
extend Comparable
end
class Test < ::String
prepend RBS::RuntimePrototypeTest::TestTargets::Baz
include RBS::RuntimePrototypeTest::TestTargets::Foo
extend RBS::RuntimePrototypeTest::TestTargets::Bar
Expand Down Expand Up @@ -116,13 +125,18 @@ module TestTargets
module Bar
end
module Baz
end
module Foo
include Enumerable[untyped]
extend Comparable
end
class Test < ::String
prepend RBS::RuntimePrototypeTest::TestTargets::Baz
include RBS::RuntimePrototypeTest::TestTargets::Foo
extend RBS::RuntimePrototypeTest::TestTargets::Bar
Expand Down

0 comments on commit 58ea286

Please sign in to comment.