rbs 1.0.0.pre → 1.0.0.pre2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd21602800550be064e741db770aa752eaab018ca894238f88d75d1d1e950edd
4
- data.tar.gz: 346c39ff339931cf9866e29cd096a0877c620feff86044a2bbb007826276904b
3
+ metadata.gz: 876e6bbb99faa21b1446731aaa99e9e184952dfff4a74c40278a8e4473ab0153
4
+ data.tar.gz: dbe783feb6db3cef15000018b0d9120b5761775fa9e635355b75f23783d558c1
5
5
  SHA512:
6
- metadata.gz: 94651715eeed037f3d899fdae1cc49dc5134f5d3df7da03d06b7ebc7a053d43139adf29680de4a0036446afb16ef824862fae4ad5a6870861ae48bd589799ac6
7
- data.tar.gz: ec967eef1ab035d4da2fe66f8a4661523c672eb1bc13f7033ac73ac8593810fd73e52d2d52173f8284abc47273b4442f9a95f4a5325c0e265cdb6a6d6b0321e1
6
+ metadata.gz: 9bdc0e3cca9cff43bfd69dba8f8635745a5e164c8590c627434075f839fee353173a9f8e829ee5bbcdd3114fe0035fbd5fef54c56d17382135563c3d2e6d8b4b
7
+ data.tar.gz: ae9c5ca0eae79d4261263235817d031e1541e82e07d70826e6d7cd69fda211cf6fd4c933706b839a39ea221b45ca044cd3381b04ff480df1c3ba06a9ee9c528b
@@ -4,14 +4,18 @@
4
4
 
5
5
  ## 1.0.0 (Pre released)
6
6
 
7
- * Signature updates for `URI`, `IO`, and `Time` ([#529](https://github.com/ruby/rbs/pull/529), [#521](https://github.com/ruby/rbs/pull/521), [#520](https://github.com/ruby/rbs/pull/520), [#511](https://github.com/ruby/rbs/pull/511), [#517](https://github.com/ruby/rbs/pull/517))
7
+ * Signature updates for `URI`, `IO`, `File`, `Pathname`, `Module`, and `Time` ([#529](https://github.com/ruby/rbs/pull/529), [#521](https://github.com/ruby/rbs/pull/521), [#520](https://github.com/ruby/rbs/pull/520), [#511](https://github.com/ruby/rbs/pull/511), [#517](https://github.com/ruby/rbs/pull/517), [#542](https://github.com/ruby/rbs/pull/542), [#546](https://github.com/ruby/rbs/pull/546), [#540](https://github.com/ruby/rbs/pull/540), [#538](https://github.com/ruby/rbs/pull/538))
8
8
  * `rbs prototype runtime` generates `extend`s ([#535](https://github.com/ruby/rbs/pull/535))
9
9
  * `rbs prototype runtime` stability improvements ([#533](https://github.com/ruby/rbs/pull/533), [#526](https://github.com/ruby/rbs/pull/526))
10
- * Improve runtime type checker compatibility ([#532](https://github.com/ruby/rbs/pull/532), [#528](https://github.com/ruby/rbs/pull/528), )
10
+ * `rbs prototype rb` compatibility improvements ([#545](https://github.com/ruby/rbs/pull/545))
11
+ * Implement method names escape in `RBS::Writer` ([#537](https://github.com/ruby/rbs/pull/537))
12
+ * Improve runtime type checker compatibility ([#532](https://github.com/ruby/rbs/pull/532), [#528](https://github.com/ruby/rbs/pull/528), [#547](https://github.com/ruby/rbs/pull/547))
13
+ * Fix `ruby2_keywords` for `Proc` in Ruby <2.7 ([#513](https://github.com/ruby/rbs/pull/513))
11
14
  * Better compatibility for record type attribute names ([#525](https://github.com/ruby/rbs/pull/525), [#524](https://github.com/ruby/rbs/pull/524))
12
- * Let module-self-types be classes ([#523](https://github.com/ruby/rbs/pull/523))
15
+ * Let module self-types be classes ([#523](https://github.com/ruby/rbs/pull/523))
16
+ * Delete `extension` syntax ([#543](https://github.com/ruby/rbs/pull/543))
13
17
  * Method resolution improvements about `alias` and `.new` ([#522](https://github.com/ruby/rbs/pull/522), [#519](https://github.com/ruby/rbs/pull/519), [#516](https://github.com/ruby/rbs/pull/516))
14
- * Fix `ruby2_keywords` for `Proc` in Ruby <2.7 ([#513](https://github.com/ruby/rbs/pull/513))
18
+ * Better message for `DuplicatedMethodDefinitionError` ([#539](https://github.com/ruby/rbs/pull/539))
15
19
 
16
20
  ## 0.20.1 (2020-12-06)
17
21
 
data/Rakefile CHANGED
@@ -45,6 +45,14 @@ task :validate => :parser do
45
45
  lib << "monitor"
46
46
  end
47
47
 
48
+ if lib == ["csv"]
49
+ lib << "forwardable"
50
+ end
51
+
52
+ if lib == ["prime"]
53
+ lib << "singleton"
54
+ end
55
+
48
56
  sh "#{ruby} #{rbs} #{lib.map {|l| "-r #{l}"}.join(" ")} validate --silent"
49
57
  end
50
58
  end
data/Steepfile CHANGED
@@ -2,8 +2,9 @@ target :lib do
2
2
  signature "sig"
3
3
  check "lib"
4
4
  ignore "lib/rbs/parser.rb"
5
+ ignore "lib/rbs/prototype", "lib/rbs/test", "lib/rbs/test.rb"
5
6
 
6
- library "set", "pathname", "json", "logger"
7
+ library "set", "pathname", "json", "logger", "monitor", "tsort"
7
8
  end
8
9
 
9
10
  # target :lib do
@@ -140,10 +140,6 @@ ARGV.map {|f| Pathname(f) }.each do |path|
140
140
  comment = comment_for_class(decl, stores: stores)
141
141
  decl.instance_variable_set(:@comment, comment)
142
142
 
143
- print_members stores, decl.name.to_s, decl.members
144
- when RBS::AST::Declarations::Extension
145
- puts " Importing documentation for #{decl.name} (#{decl.extension_name})"
146
-
147
143
  print_members stores, decl.name.to_s, decl.members
148
144
  end
149
145
  end
@@ -531,6 +531,9 @@ class File < IO
531
531
  #
532
532
  def self.mtime: (string | _ToPath | IO file_name) -> Time
533
533
 
534
+ # Alias of `File.new`.
535
+ def self.open: (string | _ToPath | int file_name, ?(string | int) mode, ?int perm) -> instance
536
+
534
537
  # Returns `true` if the named file exists and the effective used id of the
535
538
  # calling process is the owner of the file.
536
539
  #
@@ -683,7 +683,12 @@ class IO < Object
683
683
 
684
684
  def self.write: (String name, _ToS arg0, ?Integer offset, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode) -> Integer
685
685
 
686
- def self.for_fd: (Integer fd, ?Integer mode, ?Integer opt) -> self
686
+ def self.for_fd: (int fd, ?(string | int) mode, **untyped opt) -> instance
687
+
688
+ alias self.open self.for_fd
689
+
690
+ def self.open: [A] (int fd, ?(string | int) mode, **untyped opt) { (instance) -> A } -> A
691
+ | ...
687
692
 
688
693
  def bytes: () { (Integer arg0) -> untyped } -> self
689
694
  | () -> ::Enumerator[Integer, self]
@@ -443,6 +443,47 @@ class Module < Object
443
443
  #
444
444
  def const_set: (Symbol | String arg0, untyped arg1) -> untyped
445
445
 
446
+ # Returns the Ruby source filename and line number containing first definition
447
+ # of constant specified. If the named constant is not found, `nil` is returned.
448
+ # If the constant is found, but its source location can not be extracted
449
+ # (constant is defined in C code), empty array is returned.
450
+ #
451
+ # *inherit* specifies whether to lookup in `mod.ancestors` (`true` by default).
452
+ #
453
+ # # test.rb:
454
+ # class A
455
+ # C1 = 1
456
+ # end
457
+ #
458
+ # module M
459
+ # C2 = 2
460
+ # end
461
+ #
462
+ # class B < A
463
+ # include M
464
+ # C3 = 3
465
+ # end
466
+ #
467
+ # class A # continuation of A definition
468
+ # end
469
+ #
470
+ # p B.const_source_location('C3') # => ["test.rb", 11]
471
+ # p B.const_source_location('C2') # => ["test.rb", 6]
472
+ # p B.const_source_location('C1') # => ["test.rb", 2]
473
+ #
474
+ # p B.const_source_location('C2', false) # => nil -- don't lookup in ancestors
475
+ #
476
+ # p Object.const_source_location('B') # => ["test.rb", 9]
477
+ # p Object.const_source_location('A') # => ["test.rb", 1] -- note it is first entry, not "continuation"
478
+ #
479
+ # p B.const_source_location('A') # => ["test.rb", 1] -- because Object is in ancestors
480
+ # p M.const_source_location('A') # => ["test.rb", 1] -- Object is not ancestor, but additionally checked for modules
481
+ #
482
+ # p Object.const_source_location('A::C1') # => ["test.rb", 2] -- nesting is supported
483
+ # p Object.const_source_location('String') # => [] -- constant is defined in C code
484
+ #
485
+ def const_source_location: (Symbol | String name, ?boolish inherit) -> ([String, Integer] | [ ] | nil)
486
+
446
487
  # Returns an array of the names of the constants accessible in *mod*. This
447
488
  # includes the names of constants in any included modules (example at start of
448
489
  # section), unless the *inherit* parameter is set to `false`.
@@ -531,23 +531,6 @@ interface _Foo
531
531
  end
532
532
  ```
533
533
 
534
- ### Extension declaration
535
-
536
- Extension is to model _open class_.
537
-
538
- ```
539
- extension Kernel (Pathname)
540
- def Pathname: (String) -> Pathname
541
- end
542
-
543
- extension Array[A] (ActiveSupport)
544
- def to: (Integer) -> Array[A]
545
- def from: (Integer) -> Array[A]
546
- def second: () -> A?
547
- def third: () -> A?
548
- end
549
- ```
550
-
551
534
  ### Type alias declaration
552
535
 
553
536
  You can declare an alias of types.
@@ -3,7 +3,7 @@ rules:
3
3
  pattern: 💪👽🚨
4
4
  message: Do you forget to delete `arglists` section?
5
5
  glob:
6
- - "stdlib/**/*.rbs"
6
+ - "{core,stdlib}/**/*.rbs"
7
7
  fail:
8
8
  - |
9
9
  # arglists 💪👽🚨 << Delete this section
@@ -22,7 +22,7 @@ rules:
22
22
  justification:
23
23
  - Documents (comments) may contain that pattern.
24
24
  glob:
25
- - "stdlib/**/*.rbs"
25
+ - "{core,stdlib}/**/*.rbs"
26
26
  fail:
27
27
  - "def `send`: (String | Symbol arg0, *untyped arg1) -> untyped"
28
28
  pass:
@@ -300,53 +300,6 @@ module RBS
300
300
  end
301
301
  end
302
302
 
303
- class Extension < Base
304
- attr_reader :name
305
- attr_reader :type_params
306
- attr_reader :extension_name
307
- attr_reader :members
308
- attr_reader :annotations
309
- attr_reader :location
310
- attr_reader :comment
311
-
312
- def initialize(name:, type_params:, extension_name:, members:, annotations:, location:, comment:)
313
- @name = name
314
- @type_params = type_params
315
- @extension_name = extension_name
316
- @members = members
317
- @annotations = annotations
318
- @location = location
319
- @comment = comment
320
- end
321
-
322
- def ==(other)
323
- other.is_a?(Extension) &&
324
- other.name == name &&
325
- other.type_params == type_params &&
326
- other.extension_name == extension_name &&
327
- other.members == members
328
- end
329
-
330
- alias eql? ==
331
-
332
- def hash
333
- self.class.hash ^ name.hash ^ type_params.hash ^ extension_name.hash ^ members.hash
334
- end
335
-
336
- def to_json(*a)
337
- {
338
- declaration: :extension,
339
- name: name,
340
- type_params: type_params,
341
- extension_name: extension_name,
342
- members: members,
343
- annotations: annotations,
344
- location: location,
345
- comment: comment
346
- }.to_json(*a)
347
- end
348
- end
349
-
350
303
  class Interface < Base
351
304
  attr_reader :name
352
305
  attr_reader :type_params
@@ -44,7 +44,8 @@ module RBS
44
44
 
45
45
  ancestors = ancestor_builder.interface_ancestors(type_name)
46
46
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
47
- ancestor_builder.one_interface_ancestors(type_name).included_interfaces.each do |mod|
47
+ included_interfaces = ancestor_builder.one_interface_ancestors(type_name).included_interfaces or raise
48
+ included_interfaces.each do |mod|
48
49
  defn = build_interface(mod.name)
49
50
  subst = Substitution.build(defn.type_params, mod.args)
50
51
 
@@ -54,13 +55,17 @@ module RBS
54
55
  end
55
56
 
56
57
  methods = method_builder.build_interface(type_name)
58
+ one_ancestors = ancestor_builder.one_interface_ancestors(type_name)
59
+
60
+ validate_type_params(definition, methods: methods, ancestors: one_ancestors)
61
+
57
62
  methods.each do |defn|
58
- method = case defn.original
63
+ method = case original = defn.original
59
64
  when AST::Members::MethodDefinition
60
- defs = defn.original.types.map do |method_type|
65
+ defs = original.types.map do |method_type|
61
66
  Definition::Method::TypeDef.new(
62
67
  type: method_type,
63
- member: defn.original,
68
+ member: original,
64
69
  defined_in: type_name,
65
70
  implemented_in: nil
66
71
  )
@@ -73,15 +78,15 @@ module RBS
73
78
  alias_of: nil
74
79
  )
75
80
  when AST::Members::Alias
76
- unless definition.methods.key?(defn.original.old_name)
81
+ unless definition.methods.key?(original.old_name)
77
82
  raise UnknownMethodAliasError.new(
78
- original_name: defn.original.old_name,
79
- aliased_name: defn.original.new_name,
80
- location: defn.original.location
83
+ original_name: original.old_name,
84
+ aliased_name: original.new_name,
85
+ location: original.location
81
86
  )
82
87
  end
83
88
 
84
- original_method = definition.methods[defn.original.old_name]
89
+ original_method = definition.methods[original.old_name]
85
90
  Definition::Method.new(
86
91
  super_method: nil,
87
92
  defs: original_method.defs.map do |defn|
@@ -101,10 +106,14 @@ module RBS
101
106
  end
102
107
 
103
108
  definition.methods[defn.name]
109
+
110
+ when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
111
+ raise
112
+
104
113
  end
105
114
 
106
115
  defn.overloads.each do |overload|
107
- defs = overload.types.map do |method_type|
116
+ overload_defs = overload.types.map do |method_type|
108
117
  Definition::Method::TypeDef.new(
109
118
  type: method_type,
110
119
  member: overload,
@@ -113,7 +122,7 @@ module RBS
113
122
  )
114
123
  end
115
124
 
116
- method.defs.unshift(*defs)
125
+ method.defs.unshift(*overload_defs)
117
126
  end
118
127
 
119
128
  definition.methods[defn.name] = method
@@ -141,15 +150,21 @@ module RBS
141
150
  validate_type_params definition, methods: methods, ancestors: one_ancestors
142
151
 
143
152
  if super_class = one_ancestors.super_class
144
- defn = build_instance(super_class.name)
145
- merge_definition(src: defn,
146
- dest: definition,
147
- subst: Substitution.build(defn.type_params, super_class.args),
148
- keep_super: true)
153
+ case super_class
154
+ when Definition::Ancestor::Instance
155
+ build_instance(super_class.name).yield_self do |defn|
156
+ merge_definition(src: defn,
157
+ dest: definition,
158
+ subst: Substitution.build(defn.type_params, super_class.args),
159
+ keep_super: true)
160
+ end
161
+ else
162
+ raise
163
+ end
149
164
  end
150
165
 
151
- if one_ancestors.self_types
152
- one_ancestors.self_types.each do |ans|
166
+ if self_types = one_ancestors.self_types
167
+ self_types.each do |ans|
153
168
  defn = if ans.name.interface?
154
169
  build_interface(ans.name)
155
170
  else
@@ -164,7 +179,7 @@ module RBS
164
179
  end
165
180
  end
166
181
 
167
- one_ancestors.included_modules.each do |mod|
182
+ one_ancestors.each_included_module do |mod|
168
183
  defn = build_instance(mod.name)
169
184
  merge_definition(src: defn,
170
185
  dest: definition,
@@ -173,16 +188,20 @@ module RBS
173
188
 
174
189
  interface_methods = {}
175
190
 
176
- one_ancestors.included_interfaces.each do |mod|
191
+ one_ancestors.each_included_interface do |mod|
177
192
  defn = build_interface(mod.name)
178
193
  subst = Substitution.build(defn.type_params, mod.args)
179
194
 
180
195
  defn.methods.each do |name, method|
181
196
  if interface_methods.key?(name)
197
+ include_member = mod.source
198
+
199
+ raise unless include_member.is_a?(AST::Members::Include)
200
+
182
201
  raise DuplicatedInterfaceMethodDefinitionError.new(
183
202
  type: self_type,
184
203
  method_name: name,
185
- member: mod.source
204
+ member: include_member
186
205
  )
187
206
  end
188
207
 
@@ -221,7 +240,7 @@ module RBS
221
240
  end
222
241
  end
223
242
 
224
- one_ancestors.prepended_modules.each do |mod|
243
+ one_ancestors.each_prepended_module do |mod|
225
244
  defn = build_instance(mod.name)
226
245
  merge_definition(src: defn,
227
246
  dest: definition,
@@ -261,24 +280,28 @@ module RBS
261
280
  end
262
281
  end
263
282
 
264
- one_ancestors.extended_modules.each do |mod|
265
- defn = build_instance(mod.name)
266
- merge_definition(src: defn,
283
+ one_ancestors.each_extended_module do |mod|
284
+ mod_defn = build_instance(mod.name)
285
+ merge_definition(src: mod_defn,
267
286
  dest: definition,
268
- subst: Substitution.build(defn.type_params, mod.args))
287
+ subst: Substitution.build(mod_defn.type_params, mod.args))
269
288
  end
270
289
 
271
290
  interface_methods = {}
272
- one_ancestors.extended_interfaces.each do |mod|
273
- defn = build_interface(mod.name)
274
- subst = Substitution.build(defn.type_params, mod.args)
291
+ one_ancestors.each_extended_interface do |mod|
292
+ mod_defn = build_interface(mod.name)
293
+ subst = Substitution.build(mod_defn.type_params, mod.args)
275
294
 
276
- defn.methods.each do |name, method|
295
+ mod_defn.methods.each do |name, method|
277
296
  if interface_methods.key?(name)
297
+ src_member = mod.source
298
+
299
+ raise unless src_member.is_a?(AST::Members::Extend)
300
+
278
301
  raise DuplicatedInterfaceMethodDefinitionError.new(
279
302
  type: self_type,
280
303
  method_name: name,
281
- member: mod.source
304
+ member: src_member
282
305
  )
283
306
  end
284
307
 
@@ -418,6 +441,20 @@ module RBS
418
441
  end
419
442
  end
420
443
 
444
+ def source_location(source, decl)
445
+ case source
446
+ when nil
447
+ decl.location
448
+ when :super
449
+ case decl
450
+ when AST::Declarations::Class
451
+ decl.super_class&.location
452
+ end
453
+ else
454
+ source.location
455
+ end
456
+ end
457
+
421
458
  def validate_type_params(definition, ancestors:, methods:)
422
459
  type_params = definition.type_params_decl
423
460
 
@@ -429,19 +466,17 @@ module RBS
429
466
  when Definition::Ancestor::Instance
430
467
  result = calculator.in_inherit(name: ancestor.name, args: ancestor.args, variables: param_names)
431
468
  validate_params_with(type_params, result: result) do |param|
432
- location = case source = ancestor.source
433
- when nil
434
- definition.entry.primary.decl.location
435
- when :super
436
- definition.entry.primary.decl.super_class.location
437
- else
438
- source.location
439
- end
469
+ decl = case entry = definition.entry
470
+ when Environment::ModuleEntry, Environment::ClassEntry
471
+ entry.primary.decl
472
+ when Environment::SingleEntry
473
+ entry.decl
474
+ end
440
475
 
441
476
  raise InvalidVarianceAnnotationError.new(
442
477
  type_name: definition.type_name,
443
478
  param: param,
444
- location: location
479
+ location: source_location(ancestor.source, decl)
445
480
  )
446
481
  end
447
482
  end
@@ -481,8 +516,6 @@ module RBS
481
516
  nil
482
517
  when nil
483
518
  nil
484
- else
485
- raise
486
519
  end
487
520
 
488
521
  if method_types
@@ -537,11 +570,11 @@ module RBS
537
570
  if interface_methods.key?(method_name)
538
571
  interface_method = interface_methods[method_name]
539
572
 
540
- if method_def.original
573
+ if original = method_def.original
541
574
  raise DuplicatedMethodDefinitionError.new(
542
575
  type: definition.self_type,
543
576
  method_name: method_name,
544
- members: [method_def.original]
577
+ members: [original]
545
578
  )
546
579
  end
547
580
 
@@ -561,6 +594,7 @@ module RBS
561
594
  )
562
595
  end
563
596
 
597
+ # @type var accessibility: RBS::Definition::accessibility
564
598
  accessibility = if method_name == :initialize
565
599
  :private
566
600
  else
@@ -640,7 +674,7 @@ module RBS
640
674
  end
641
675
 
642
676
  method_def.overloads.each do |overload|
643
- defs = overload.types.map do |method_type|
677
+ type_defs = overload.types.map do |method_type|
644
678
  Definition::Method::TypeDef.new(
645
679
  type: method_type,
646
680
  member: overload,
@@ -649,7 +683,7 @@ module RBS
649
683
  )
650
684
  end
651
685
 
652
- method.defs.unshift(*defs)
686
+ method.defs.unshift(*type_defs)
653
687
  end
654
688
 
655
689
  definition.methods[method_name] = method