rbs 1.7.0.beta.3 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/core/false_class.rbs CHANGED
@@ -37,4 +37,6 @@ class FalseClass
37
37
  def |: (nil) -> false
38
38
  | (false) -> false
39
39
  | (untyped obj) -> true
40
+
41
+ def clone: (?freeze: true?) -> self
40
42
  end
data/core/float.rbs CHANGED
@@ -171,8 +171,6 @@ class Float < Numeric
171
171
  def ceil: () -> Integer
172
172
  | (int digits) -> (Integer | Float)
173
173
 
174
- def clone: (?freeze: bool) -> self
175
-
176
174
  # Returns an array with both `numeric` and `float` represented as Float objects.
177
175
  #
178
176
  # This is achieved by converting `numeric` to a Float.
data/core/integer.rbs CHANGED
@@ -272,8 +272,6 @@ class Integer < Numeric
272
272
  #
273
273
  def chr: (?encoding) -> String
274
274
 
275
- def clone: (?freeze: bool) -> self
276
-
277
275
  # Returns an array with both a `numeric` and a `big` represented as Bignum
278
276
  # objects.
279
277
  #
data/core/nil_class.rbs CHANGED
@@ -79,4 +79,6 @@ class NilClass
79
79
  def |: (nil) -> false
80
80
  | (false) -> false
81
81
  | (untyped obj) -> bool
82
+
83
+ def clone: (?freeze: true?) -> self
82
84
  end
data/core/numeric.rbs CHANGED
@@ -416,4 +416,11 @@ class Numeric
416
416
  # Returns `true` if `num` has a zero value.
417
417
  #
418
418
  def zero?: () -> bool
419
+
420
+ # Returns +self+.
421
+ #
422
+ # Raises an exception if the value for +freeze+ is neither +true+ nor +nil+.
423
+ #
424
+ # Related: Numeric#dup.
425
+ def clone: (?freeze: true?) -> self
419
426
  end
data/core/object.rbs CHANGED
@@ -76,7 +76,7 @@ class Object < BasicObject
76
76
  # This method may have class-specific behavior. If so, that behavior will be
77
77
  # documented under the #`initialize_copy` method of the class.
78
78
  #
79
- def clone: (?freeze: bool) -> self
79
+ def clone: (?freeze: bool?) -> self
80
80
 
81
81
  # Defines a singleton method in the receiver. The *method* parameter can be a
82
82
  # `Proc`, a `Method` or an `UnboundMethod` object. If a block is specified, it
data/core/proc.rbs CHANGED
@@ -210,6 +210,8 @@
210
210
  # {test: 1}.to_proc.call(:test) #=> 1
211
211
  # %i[test many keys].map(&{test: 1}) #=> [1, nil, nil]
212
212
  class Proc < Object
213
+ def clone: () -> self
214
+
213
215
  # Returns the number of mandatory arguments. If the block is declared to
214
216
  # take no arguments, returns 0. If the block is known to take exactly n
215
217
  # arguments, returns n. If the block has optional arguments, returns -n-1,
data/core/rational.rbs CHANGED
@@ -175,8 +175,6 @@ class Rational < Numeric
175
175
  def ceil: () -> Integer
176
176
  | (Integer digits) -> (Integer | Rational)
177
177
 
178
- def clone: (?freeze: bool) -> self
179
-
180
178
  def coerce: (Numeric) -> [Numeric, Numeric]
181
179
 
182
180
  def conj: () -> Rational
data/core/symbol.rbs CHANGED
@@ -226,4 +226,6 @@ class Symbol
226
226
  | (:ascii | :lithuanian | :turkic) -> Symbol
227
227
  | (:lithuanian, :turkic) -> Symbol
228
228
  | (:turkic, :lithuanian) -> Symbol
229
+
230
+ def clone: (?freeze: true?) -> self
229
231
  end
data/core/thread.rbs CHANGED
@@ -554,7 +554,7 @@ class Thread < Object
554
554
  # b = Thread.new { raise 'something went wrong' }
555
555
  # b.value #=> RuntimeError: something went wrong
556
556
  # ```
557
- def value: () -> Object
557
+ def value: () -> untyped
558
558
 
559
559
  # Marks a given thread as eligible for scheduling, however it may still
560
560
  # remain blocked on I/O.
data/core/true_class.rbs CHANGED
@@ -43,4 +43,6 @@ class TrueClass
43
43
  # or
44
44
  #
45
45
  def |: (untyped obj) -> true
46
+
47
+ def clone: (?freeze: true?) -> self
46
48
  end
@@ -45,6 +45,19 @@
45
45
  # um.bind(t).call #=> :original
46
46
  #
47
47
  class UnboundMethod
48
+ # Returns a clone of this method.
49
+ #
50
+ # class A
51
+ # def foo
52
+ # return "bar"
53
+ # end
54
+ # end
55
+ #
56
+ # m = A.new.method(:foo)
57
+ # m.call # => "bar"
58
+ # n = m.clone.call # => "bar"
59
+ def clone: () -> self
60
+
48
61
  # Returns an indication of the number of arguments accepted by a method. Returns
49
62
  # a nonnegative integer for methods that take a fixed number of arguments. For
50
63
  # Ruby methods that take a variable number of arguments, returns -n-1, where n
@@ -229,9 +229,9 @@ end
229
229
 
230
230
  ```ruby
231
231
  # .rb
232
- [1,2,3,4,5].select {|num| num.even? }
232
+ [1,2,3,4,5].filter {|num| num.even? }
233
233
  # => [2, 4]
234
- %w[ a b c d e f ].select {|v| v =~ /[aeiou]/ }
234
+ %w[ a b c d e f ].filter {|v| v =~ /[aeiou]/ }
235
235
  # => ["a", "e"]
236
236
  [1,2,3,4,5].filter
237
237
  ```
data/docs/syntax.md CHANGED
@@ -110,10 +110,10 @@ Array[Integer | String] # Array of Integer or String
110
110
  Intersection type denotes _a type of all of the given types_.
111
111
 
112
112
  ```
113
- Integer & String # Integer and String
113
+ _Reader & _Writer # _Reader and _Writer
114
114
  ```
115
115
 
116
- Note that `&` has higher precedence than `|` that `Integer & String | Symbol` is `(Integer & String) | Symbol`.
116
+ Note that `&` has higher precedence than `|` that `A & B | C` is `(A & B) | C`.
117
117
 
118
118
  ### Optional type
119
119
 
@@ -289,7 +289,6 @@ _method-member_ ::= `def` _method-name_ `:` _method-types_ # Instance
289
289
  | `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
290
290
 
291
291
  _method-types_ ::= # Empty
292
- | `super` # `super` overloading
293
292
  | _type-parameters_ _method-type_ `|` _method-types_ # Overloading types
294
293
  | `...` # Overloading for duplicate definitions
295
294
 
@@ -273,7 +273,7 @@ static VALUE parse_function_param(parserstate *state) {
273
273
  param_range.start = type_range.start;
274
274
  param_range.end = name_range.end;
275
275
 
276
- VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token));
276
+ VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0));
277
277
  VALUE location = rbs_new_location(state->buffer, param_range);
278
278
  rbs_loc *loc = rbs_check_location(location);
279
279
  rbs_loc_add_optional_child(loc, rb_intern("name"), name_range);
@@ -1487,6 +1487,8 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1487
1487
  case INSTANCE_SINGLETON_KIND:
1488
1488
  k = ID2SYM(rb_intern("singleton_instance"));
1489
1489
  break;
1490
+ default:
1491
+ rbs_abort();
1490
1492
  }
1491
1493
 
1492
1494
  VALUE location = rbs_new_location(state->buffer, member_range);
@@ -87,7 +87,6 @@ bool parser_typevar_member(parserstate *state, ID id) {
87
87
  }
88
88
 
89
89
  void print_parser(parserstate *state) {
90
- pp(state->buffer);
91
90
  printf(" current_token = %s (%d...%d)\n", token_type_str(state->current_token.type), state->current_token.range.start.char_pos, state->current_token.range.end.char_pos);
92
91
  printf(" next_token = %s (%d...%d)\n", token_type_str(state->next_token.type), state->next_token.range.start.char_pos, state->next_token.range.end.char_pos);
93
92
  printf(" next_token2 = %s (%d...%d)\n", token_type_str(state->next_token2.type), state->next_token2.range.start.char_pos, state->next_token2.range.end.char_pos);
@@ -37,4 +37,4 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes);
37
37
  * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type})
38
38
  * ```
39
39
  * */
40
- NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...);
40
+ PRINTF_ARGS(NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4);
@@ -41,6 +41,4 @@ VALUE rbs_type_name(VALUE namespace, VALUE name);
41
41
  VALUE rbs_union(VALUE types, VALUE location);
42
42
  VALUE rbs_variable(VALUE name, VALUE location);
43
43
 
44
- void pp(VALUE object);
45
-
46
44
  #endif
@@ -102,7 +102,12 @@ module RBS
102
102
  git 'fetch', 'origin'
103
103
  end
104
104
  else
105
- git 'clone', remote, git_dir.to_s
105
+ begin
106
+ # git v2.27.0 or greater
107
+ git 'clone', '--filter=blob:none', remote, git_dir.to_s
108
+ rescue CommandError
109
+ git 'clone', remote, git_dir.to_s
110
+ end
106
111
  end
107
112
 
108
113
  begin
@@ -26,38 +26,38 @@ module RBS
26
26
  autoload :LexerError, "rbs/parser_compat/lexer_error"
27
27
  autoload :LocatedValue, "rbs/parser_compat/located_value"
28
28
 
29
- KEYWORDS = Set.new(
30
- %w(
31
- bool
32
- bot
33
- class
34
- instance
35
- interface
36
- nil
37
- self
38
- singleton
39
- top
40
- void
41
- type
42
- unchecked
43
- in
44
- out
45
- end
46
- def
47
- include
48
- extend
49
- prepend
50
- alias
51
- module
52
- attr_reader
53
- attr_writer
54
- attr_accessor
55
- public
56
- private
57
- untyped
58
- true
59
- false
60
- )
61
- )
29
+ KEYWORDS = %w(
30
+ bool
31
+ bot
32
+ class
33
+ instance
34
+ interface
35
+ nil
36
+ self
37
+ singleton
38
+ top
39
+ void
40
+ type
41
+ unchecked
42
+ in
43
+ out
44
+ end
45
+ def
46
+ include
47
+ extend
48
+ prepend
49
+ alias
50
+ module
51
+ attr_reader
52
+ attr_writer
53
+ attr_accessor
54
+ public
55
+ private
56
+ untyped
57
+ true
58
+ false
59
+ ).each_with_object({}) do |keyword, hash|
60
+ hash[keyword] = nil
61
+ end
62
62
  end
63
63
  end
@@ -0,0 +1,113 @@
1
+ module RBS
2
+ module Prototype
3
+ module Helpers
4
+ private
5
+
6
+ def block_from_body(node)
7
+ _, args_node, body_node = node.children
8
+
9
+ _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block = args_from_node(args_node)
10
+
11
+ method_block = nil
12
+
13
+ if block
14
+ method_block = Types::Block.new(
15
+ # HACK: The `block` is :& on `def m(...)` syntax.
16
+ # In this case the block looks optional in most cases, so it marks optional.
17
+ # In other cases, we can't determine which is required or optional, so it marks required.
18
+ required: block != :&,
19
+ type: Types::Function.empty(untyped)
20
+ )
21
+ end
22
+
23
+ if body_node
24
+ if (yields = any_node?(body_node) {|n| n.type == :YIELD })
25
+ method_block = Types::Block.new(
26
+ required: true,
27
+ type: Types::Function.empty(untyped)
28
+ )
29
+
30
+ yields.each do |yield_node|
31
+ array_content = yield_node.children[0]&.children&.compact || []
32
+
33
+ positionals, keywords = if keyword_hash?(array_content.last)
34
+ [array_content.take(array_content.size - 1), array_content.last]
35
+ else
36
+ [array_content, nil]
37
+ end
38
+
39
+ if (diff = positionals.size - method_block.type.required_positionals.size) > 0
40
+ diff.times do
41
+ method_block.type.required_positionals << Types::Function::Param.new(
42
+ type: untyped,
43
+ name: nil
44
+ )
45
+ end
46
+ end
47
+
48
+ if keywords
49
+ keywords.children[0].children.each_slice(2) do |key_node, value_node|
50
+ if key_node
51
+ key = key_node.children[0]
52
+ method_block.type.required_keywords[key] ||=
53
+ Types::Function::Param.new(
54
+ type: untyped,
55
+ name: nil
56
+ )
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ method_block
65
+ end
66
+
67
+ def each_child(node, &block)
68
+ each_node node.children, &block
69
+ end
70
+
71
+ def each_node(nodes)
72
+ nodes.each do |child|
73
+ if child.is_a?(RubyVM::AbstractSyntaxTree::Node)
74
+ yield child
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ def any_node?(node, nodes: [], &block)
81
+ if yield(node)
82
+ nodes << node
83
+ end
84
+
85
+ each_child node do |child|
86
+ any_node? child, nodes: nodes, &block
87
+ end
88
+
89
+ nodes.empty? ? nil : nodes
90
+ end
91
+
92
+ def keyword_hash?(node)
93
+ if node
94
+ if node.type == :HASH
95
+ node.children[0].children.compact.each_slice(2).all? {|key, _|
96
+ key.type == :LIT && key.children[0].is_a?(Symbol)
97
+ }
98
+ end
99
+ end
100
+ end
101
+
102
+ # NOTE: args_node may be a nil by a bug
103
+ # https://bugs.ruby-lang.org/issues/17495
104
+ def args_from_node(args_node)
105
+ args_node&.children || [0, nil, nil, nil, 0, nil, nil, nil, nil, nil]
106
+ end
107
+
108
+ def untyped
109
+ @untyped ||= Types::Bases::Any.new(location: nil)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -1,6 +1,8 @@
1
1
  module RBS
2
2
  module Prototype
3
3
  class RB
4
+ include Helpers
5
+
4
6
  Context = Struct.new(:module_function, :singleton, :namespace, keyword_init: true) do
5
7
  def self.initial(namespace: Namespace.root)
6
8
  self.new(module_function: false, singleton: false, namespace: namespace)
@@ -372,18 +374,6 @@ module RBS
372
374
  end
373
375
  end
374
376
 
375
- def each_node(nodes)
376
- nodes.each do |child|
377
- if child.is_a?(RubyVM::AbstractSyntaxTree::Node)
378
- yield child
379
- end
380
- end
381
- end
382
-
383
- def each_child(node, &block)
384
- each_node node.children, &block
385
- end
386
-
387
377
  def function_type_from_body(node, def_name)
388
378
  table_node, args_node, *_ = node.children
389
379
 
@@ -568,95 +558,6 @@ module RBS
568
558
  end
569
559
  end
570
560
 
571
- def block_from_body(node)
572
- _, args_node, body_node = node.children
573
-
574
- _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block = args_from_node(args_node)
575
-
576
- method_block = nil
577
-
578
- if block
579
- method_block = Types::Block.new(
580
- # HACK: The `block` is :& on `def m(...)` syntax.
581
- # In this case the block looks optional in most cases, so it marks optional.
582
- # In other cases, we can't determine which is required or optional, so it marks required.
583
- required: block != :&,
584
- type: Types::Function.empty(untyped)
585
- )
586
- end
587
-
588
- if body_node
589
- if (yields = any_node?(body_node) {|n| n.type == :YIELD })
590
- method_block = Types::Block.new(
591
- required: true,
592
- type: Types::Function.empty(untyped)
593
- )
594
-
595
- yields.each do |yield_node|
596
- array_content = yield_node.children[0]&.children&.compact || []
597
-
598
- positionals, keywords = if keyword_hash?(array_content.last)
599
- [array_content.take(array_content.size - 1), array_content.last]
600
- else
601
- [array_content, nil]
602
- end
603
-
604
- if (diff = positionals.size - method_block.type.required_positionals.size) > 0
605
- diff.times do
606
- method_block.type.required_positionals << Types::Function::Param.new(
607
- type: untyped,
608
- name: nil
609
- )
610
- end
611
- end
612
-
613
- if keywords
614
- keywords.children[0].children.each_slice(2) do |key_node, value_node|
615
- if key_node
616
- key = key_node.children[0]
617
- method_block.type.required_keywords[key] ||=
618
- Types::Function::Param.new(
619
- type: untyped,
620
- name: nil
621
- )
622
- end
623
- end
624
- end
625
- end
626
- end
627
- end
628
-
629
- method_block
630
- end
631
-
632
- # NOTE: args_node may be a nil by a bug
633
- # https://bugs.ruby-lang.org/issues/17495
634
- def args_from_node(args_node)
635
- args_node&.children || [0, nil, nil, nil, 0, nil, nil, nil, nil, nil]
636
- end
637
-
638
- def keyword_hash?(node)
639
- if node
640
- if node.type == :HASH
641
- node.children[0].children.compact.each_slice(2).all? {|key, _|
642
- key.type == :LIT && key.children[0].is_a?(Symbol)
643
- }
644
- end
645
- end
646
- end
647
-
648
- def any_node?(node, nodes: [], &block)
649
- if yield(node)
650
- nodes << node
651
- end
652
-
653
- each_child node do |child|
654
- any_node? child, nodes: nodes, &block
655
- end
656
-
657
- nodes.empty? ? nil : nodes
658
- end
659
-
660
561
  def node_type(node, default: Types::Bases::Any.new(location: nil))
661
562
  case node.type
662
563
  when :LIT
@@ -689,10 +590,6 @@ module RBS
689
590
  end
690
591
  end
691
592
 
692
- def untyped
693
- @untyped ||= Types::Bases::Any.new(location: nil)
694
- end
695
-
696
593
  def private
697
594
  @private ||= AST::Members::Private.new(location: nil)
698
595
  end
@@ -1,6 +1,8 @@
1
1
  module RBS
2
2
  module Prototype
3
3
  class Runtime
4
+ include Helpers
5
+
4
6
  attr_reader :patterns
5
7
  attr_reader :env
6
8
  attr_reader :merge
@@ -138,6 +140,8 @@ module RBS
138
140
  end
139
141
  end
140
142
 
143
+ block ||= block_from_ast_of(method)
144
+
141
145
  return_type = if method.name == :initialize
142
146
  Types::Bases::Void.new(location: nil)
143
147
  else
@@ -522,6 +526,18 @@ module RBS
522
526
  []
523
527
  end
524
528
  end
529
+
530
+ def block_from_ast_of(method)
531
+ return nil if RUBY_VERSION < '3.1'
532
+
533
+ begin
534
+ ast = RubyVM::AbstractSyntaxTree.of(method)
535
+ rescue ArgumentError
536
+ return # When the method is defined in eval
537
+ end
538
+
539
+ block_from_body(ast) if ast&.type == :SCOPE
540
+ end
525
541
  end
526
542
  end
527
543
  end
@@ -13,6 +13,7 @@ begin
13
13
  filter = ENV.fetch('RBS_TEST_TARGET', "").split(',').map! { |e| e.strip }
14
14
  skips = (ENV['RBS_TEST_SKIP'] || '').split(',').map! { |e| e.strip }
15
15
  RBS.logger_level = (ENV["RBS_TEST_LOGLEVEL"] || "info")
16
+ logger.level = RBS.logger_level
16
17
  sample_size = get_sample_size(ENV['RBS_TEST_SAMPLE_SIZE'] || '')
17
18
  double_class = to_double_class(ENV['RBS_TEST_DOUBLE_SUITE'])
18
19
  unchecked_classes = (ENV['RBS_TEST_UNCHECKED_CLASSES'] || '').split(',').map! { |unchecked_class| unchecked_class.strip }.push(*double_class)
data/lib/rbs/types.rb CHANGED
@@ -690,7 +690,7 @@ module RBS
690
690
 
691
691
  def to_s
692
692
  if name
693
- if Parser::KEYWORDS.include?(name)
693
+ if Parser::KEYWORDS.include?(name.to_s)
694
694
  "#{type} `#{name}`"
695
695
  else
696
696
  "#{type} #{name}"
data/lib/rbs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RBS
2
- VERSION = "1.7.0.beta.3"
2
+ VERSION = "1.7.1"
3
3
  end
data/lib/rbs.rb CHANGED
@@ -32,6 +32,7 @@ require "rbs/constant"
32
32
  require "rbs/constant_table"
33
33
  require "rbs/ast/comment"
34
34
  require "rbs/writer"
35
+ require "rbs/prototype/helpers"
35
36
  require "rbs/prototype/rbi"
36
37
  require "rbs/prototype/rb"
37
38
  require "rbs/prototype/runtime"
@@ -46,6 +46,8 @@ module RBS
46
46
 
47
47
  def _install: (dest: Pathname , config_entry: Config::gem_entry) -> void
48
48
 
49
+ def cp_r: (Pathname, Pathname) -> void
50
+
49
51
  def setup!: (revision: String) -> void
50
52
 
51
53
  def need_to_fetch?: (String revision ) -> bool
data/sig/parser.rbs CHANGED
@@ -6,7 +6,7 @@ module RBS
6
6
 
7
7
  def self.parse_signature: (Buffer | String, ?line: Integer, ?column: Integer) -> Array[AST::Declarations::t]
8
8
 
9
- KEYWORDS: Set[String]
9
+ KEYWORDS: Hash[String, bot]
10
10
 
11
11
  private
12
12