From 95de14f3354d793fea1c5b638ddb4f1568977624 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 12 Jan 2024 12:28:51 +0900 Subject: [PATCH 1/4] Update prototype for `SYM` node --- lib/rbs/prototype/helpers.rb | 13 ++++++++++++- lib/rbs/prototype/rb.rb | 11 +++++++++++ lib/rbs/prototype/rbi.rb | 16 +++++++++------- sig/prototype/helpers.rbs | 4 ++++ sig/prototype/rbi.rbs | 2 ++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/rbs/prototype/helpers.rb b/lib/rbs/prototype/helpers.rb index 1c36ff18e..05285926c 100644 --- a/lib/rbs/prototype/helpers.rb +++ b/lib/rbs/prototype/helpers.rb @@ -109,7 +109,7 @@ def any_node?(node, nodes: [], &block) def keyword_hash?(node) if node && node.type == :HASH node.children[0].children.compact.each_slice(2).all? {|key, _| - key.type == :LIT && key.children[0].is_a?(Symbol) + symbol_literal_node?(key) } else false @@ -122,6 +122,17 @@ def args_from_node(args_node) args_node&.children || [0, nil, nil, nil, 0, nil, nil, nil, nil, nil] end + def symbol_literal_node?(node) + case node.type + when :LIT + if node.children[0].is_a?(Symbol) + node.children[0] + end + when :SYM + node.children[0] + end + end + def untyped @untyped ||= Types::Bases::Any.new(location: nil) end diff --git a/lib/rbs/prototype/rb.rb b/lib/rbs/prototype/rb.rb index 186b8e53a..39cfa6d65 100644 --- a/lib/rbs/prototype/rb.rb +++ b/lib/rbs/prototype/rb.rb @@ -461,6 +461,8 @@ def const_to_name(node, context:) def literal_to_symbol(node) case node.type + when :SYM + node.children[0] when :LIT node.children[0] if node.children[0].is_a?(Symbol) when :STR @@ -574,6 +576,13 @@ def literal_to_type(node) end when :DSTR, :XSTR BuiltinNames::String.instance_type + when :SYM + lit = node.children[0] + if lit.to_s.ascii_only? + Types::Literal.new(literal: lit, location: nil) + else + BuiltinNames::Symbol.instance_type + end when :DSYM BuiltinNames::Symbol.instance_type when :DREGX @@ -718,6 +727,8 @@ def param_type(node, default: Types::Bases::Any.new(location: nil)) else default end + when :SYM + BuiltinNames::Symbol.instance_type when :STR, :DSTR BuiltinNames::String.instance_type when :NIL diff --git a/lib/rbs/prototype/rbi.rb b/lib/rbs/prototype/rbi.rb index c0a10ffc1..7c96267d8 100644 --- a/lib/rbs/prototype/rbi.rb +++ b/lib/rbs/prototype/rbi.rb @@ -3,6 +3,8 @@ module RBS module Prototype class RBI + include Helpers + attr_reader :decls attr_reader :modules attr_reader :last_sig @@ -218,11 +220,11 @@ def process(node, outer: [], comments:) if (send = node.children.last) && send.type == :FCALL && send.children[0] == :type_member unless each_arg(send.children[1]).any? {|node| node.type == :HASH && - each_arg(node.children[0]).each_slice(2).any? {|a, _| a.type == :LIT && a.children[0] == :fixed } + each_arg(node.children[0]).each_slice(2).any? {|a, _| symbol_literal_node?(a) == :fixed } } # @type var variance: AST::TypeParam::variance? - if (a0 = each_arg(send.children[1]).to_a[0]) && a0.type == :LIT - variance = case a0.children[0] + if (a0 = each_arg(send.children[1]).to_a[0]) && (v = symbol_literal_node?(a0)) + variance = case v when :out :covariant when :in @@ -321,8 +323,8 @@ def method_type(args_node, type_node, variables:, overloads:) type_params = [] each_arg args do |node| - if node.type == :LIT - type_params << node.children[0] + if name = symbol_literal_node?(node) + type_params << name end end @@ -613,8 +615,8 @@ def node_to_hash(node) each_arg(node.children[0]).each_slice(2) do |var, type| var or raise - if var.type == :LIT && type - hash[var.children[0]] = type + if (name = symbol_literal_node?(var)) && type + hash[name] = type end end diff --git a/sig/prototype/helpers.rbs b/sig/prototype/helpers.rbs index e260d58c7..ab7e51802 100644 --- a/sig/prototype/helpers.rbs +++ b/sig/prototype/helpers.rbs @@ -13,6 +13,10 @@ module RBS def keyword_hash?: (node) -> bool + # Returns a symbol if the node is a symbol literal node + # + def symbol_literal_node?: (node) -> Symbol? + def args_from_node: (node?) -> Array[untyped] def untyped: () -> Types::Bases::Any diff --git a/sig/prototype/rbi.rbs b/sig/prototype/rbi.rbs index 547baa5db..d8b37e9e8 100644 --- a/sig/prototype/rbi.rbs +++ b/sig/prototype/rbi.rbs @@ -1,6 +1,8 @@ module RBS module Prototype class RBI + include Helpers + attr_reader decls: Array[AST::Declarations::t] type module_decl = AST::Declarations::Class | AST::Declarations::Module From d3b16d7018eab1b9ca9757971a355cc031d5dafe Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 12 Jan 2024 12:28:58 +0900 Subject: [PATCH 2/4] Update minitest --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 76b0628ea..2e8f75ec9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,7 +29,7 @@ GEM addressable (>= 2.8) language_server-protocol (3.17.0.3) marcel (1.0.2) - minitest (5.20.0) + minitest (5.21.1) net-protocol (0.2.2) timeout net-smtp (0.4.0.1) From a51170303f175c4e7687e10f17a74b1da4d9a1a9 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 12 Jan 2024 12:29:05 +0900 Subject: [PATCH 3/4] Ignore `mutex_m` test fails --- bin/test_runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/test_runner.rb b/bin/test_runner.rb index 770edb704..57ddef2e8 100755 --- a/bin/test_runner.rb +++ b/bin/test_runner.rb @@ -14,7 +14,7 @@ end end -KNOWN_FAILS = %w(dbm).map do |lib| +KNOWN_FAILS = %w(dbm mutex_m).map do |lib| /cannot load such file -- #{lib}/ end From 3671f215ffa34d8066f86677863b7fa5d8a86670 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 12 Jan 2024 12:29:49 +0900 Subject: [PATCH 4/4] Fix stdlib_test warning message --- bin/test_runner.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/test_runner.rb b/bin/test_runner.rb index 57ddef2e8..05af15b06 100755 --- a/bin/test_runner.rb +++ b/bin/test_runner.rb @@ -5,12 +5,12 @@ require "set" IS_LATEST_RUBY = Gem::Version.new(RUBY_VERSION).yield_self do |ruby_version| - Gem::Version.new('3.2.0') <= ruby_version && ruby_version < Gem::Version.new('3.3.0') + Gem::Version.new('3.3.0') <= ruby_version && ruby_version < Gem::Version.new('3.4.0') end unless IS_LATEST_RUBY unless ENV["CI"] - STDERR.puts "⚠️⚠️⚠️⚠️ stdlib test assumes Ruby 3.2 but RUBY_VERSION==#{RUBY_VERSION} ⚠️⚠️⚠️⚠️" + STDERR.puts "⚠️⚠️⚠️⚠️ stdlib test assumes Ruby 3.3 but RUBY_VERSION==#{RUBY_VERSION} ⚠️⚠️⚠️⚠️" end end