diff --git a/lib/typeprof/core/ast.rb b/lib/typeprof/core/ast.rb index d1ec4ef8..2f03228a 100644 --- a/lib/typeprof/core/ast.rb +++ b/lib/typeprof/core/ast.rb @@ -280,6 +280,10 @@ def self.create_node(raw_node, lenv, use_result = true, allow_meta = false) return AttrReaderMetaNode.new(raw_node, lenv) when :attr_accessor return AttrAccessorMetaNode.new(raw_node, lenv) + when :module_function + if raw_node.arguments.nil? + return ModuleFunctionMetaNode.new(raw_node, lenv) + end end end CallNode.new(raw_node, lenv) diff --git a/lib/typeprof/core/ast/meta.rb b/lib/typeprof/core/ast/meta.rb index 2da05462..6864e31a 100644 --- a/lib/typeprof/core/ast/meta.rb +++ b/lib/typeprof/core/ast/meta.rb @@ -146,5 +146,11 @@ def install0(genv) Source.new end end + class ModuleFunctionMetaNode < Node + def install0(genv) + @lenv.module_function = true + Source.new + end + end end end diff --git a/lib/typeprof/core/ast/method.rb b/lib/typeprof/core/ast/method.rb index 843dcca8..443be522 100644 --- a/lib/typeprof/core/ast/method.rb +++ b/lib/typeprof/core/ast/method.rb @@ -289,6 +289,9 @@ def install0(genv) ) @changes.add_method_def_box(genv, @lenv.cref.cpath, @singleton, @mid, f_args, @body.lenv.return_boxes) + if @lenv.module_function && !@singleton + @changes.add_method_def_box(genv, @lenv.cref.cpath, true, @mid, f_args, @body.lenv.return_boxes) + end Source.new(Type::Symbol.new(genv, @mid)) end diff --git a/lib/typeprof/core/env.rb b/lib/typeprof/core/env.rb index 91117468..6ef3510b 100644 --- a/lib/typeprof/core/env.rb +++ b/lib/typeprof/core/env.rb @@ -331,6 +331,7 @@ def initialize(file_context, cref, locals, return_boxes) end attr_reader :file_context, :cref, :locals, :return_boxes, :break_vtx, :next_boxes, :strict_const_scope + attr_accessor :module_function def path = @file_context&.path def code_range_from_node(node) diff --git a/scenario/misc/module_function.rb b/scenario/misc/module_function.rb new file mode 100644 index 00000000..bd20c8df --- /dev/null +++ b/scenario/misc/module_function.rb @@ -0,0 +1,16 @@ +## update +module Foo + module_function + + def bar + 42 + end +end + +Foo.bar + +## assert +module Foo + def bar: -> Integer + def self.bar: -> Integer +end