rbs 4.0.3 → 4.1.0.pre.2

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.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/c-check.yml +3 -0
  3. data/.github/workflows/dependabot.yml +1 -1
  4. data/.github/workflows/jruby.yml +67 -0
  5. data/.github/workflows/milestone.yml +4 -1
  6. data/.github/workflows/ruby.yml +40 -0
  7. data/.github/workflows/rust.yml +3 -2
  8. data/.github/workflows/truffleruby.yml +54 -0
  9. data/.github/workflows/typecheck.yml +3 -0
  10. data/.github/workflows/wasm.yml +53 -0
  11. data/.github/workflows/windows.yml +3 -0
  12. data/.gitignore +7 -0
  13. data/CHANGELOG.md +0 -8
  14. data/README.md +3 -3
  15. data/Rakefile +118 -3
  16. data/Steepfile +7 -0
  17. data/config.yml +24 -0
  18. data/core/array.rbs +144 -144
  19. data/core/builtin.rbs +7 -6
  20. data/core/class.rbs +5 -3
  21. data/core/enumerable.rbs +109 -109
  22. data/core/enumerator/product.rbs +5 -5
  23. data/core/enumerator.rbs +28 -28
  24. data/core/file.rbs +24 -1018
  25. data/core/file_constants.rbs +463 -0
  26. data/core/file_stat.rbs +534 -0
  27. data/core/hash.rbs +117 -101
  28. data/core/integer.rbs +21 -58
  29. data/core/io.rbs +25 -7
  30. data/core/kernel.rbs +26 -11
  31. data/core/module.rbs +88 -74
  32. data/core/numeric.rbs +3 -0
  33. data/core/object_space/weak_key_map.rbs +7 -7
  34. data/core/range.rbs +23 -23
  35. data/core/rbs/ops.rbs +154 -0
  36. data/core/rbs/unnamed/argf.rbs +3 -3
  37. data/core/ruby_vm.rbs +40 -0
  38. data/core/set.rbs +3 -3
  39. data/core/struct.rbs +16 -16
  40. data/core/thread.rbs +6 -6
  41. data/docs/CONTRIBUTING.md +2 -1
  42. data/docs/inline.md +65 -7
  43. data/docs/rbs_by_example.md +20 -20
  44. data/docs/syntax.md +2 -2
  45. data/docs/wasm_serialization.md +80 -0
  46. data/ext/rbs_extension/ast_translation.c +1298 -956
  47. data/ext/rbs_extension/ast_translation.h +4 -0
  48. data/ext/rbs_extension/class_constants.c +2 -0
  49. data/ext/rbs_extension/class_constants.h +1 -0
  50. data/ext/rbs_extension/extconf.rb +1 -0
  51. data/ext/rbs_extension/main.c +139 -4
  52. data/include/rbs/ast.h +323 -298
  53. data/include/rbs/defines.h +13 -0
  54. data/include/rbs/lexer.h +1 -0
  55. data/include/rbs/serialize.h +39 -0
  56. data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
  57. data/lib/rbs/ast/ruby/annotations.rb +42 -0
  58. data/lib/rbs/ast/ruby/comment_block.rb +6 -4
  59. data/lib/rbs/ast/ruby/declarations.rb +11 -1
  60. data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
  61. data/lib/rbs/ast/ruby/members.rb +40 -1
  62. data/lib/rbs/buffer.rb +48 -11
  63. data/lib/rbs/cli.rb +3 -5
  64. data/lib/rbs/collection/config/lockfile_generator.rb +14 -1
  65. data/lib/rbs/collection/sources/git.rb +6 -0
  66. data/lib/rbs/definition_builder/method_builder.rb +12 -6
  67. data/lib/rbs/environment.rb +10 -3
  68. data/lib/rbs/inline_parser.rb +54 -28
  69. data/lib/rbs/namespace.rb +47 -11
  70. data/lib/rbs/prototype/runtime.rb +2 -0
  71. data/lib/rbs/resolver/type_name_resolver.rb +12 -14
  72. data/lib/rbs/rewriter.rb +70 -0
  73. data/lib/rbs/test/type_check.rb +6 -1
  74. data/lib/rbs/type_name.rb +33 -13
  75. data/lib/rbs/unit_test/type_assertions.rb +9 -0
  76. data/lib/rbs/version.rb +1 -1
  77. data/lib/rbs/wasm/deserializer.rb +213 -0
  78. data/lib/rbs/wasm/location.rb +61 -0
  79. data/lib/rbs/wasm/parser.rb +137 -0
  80. data/lib/rbs/wasm/runtime.rb +217 -0
  81. data/lib/rbs/wasm/serialization_schema.rb +110 -0
  82. data/lib/rbs.rb +14 -2
  83. data/rbs.gemspec +19 -2
  84. data/sig/annotate/rdoc_annotater.rbs +12 -9
  85. data/sig/ast/ruby/annotations.rbs +49 -0
  86. data/sig/ast/ruby/members.rbs +21 -1
  87. data/sig/buffer.rbs +19 -1
  88. data/sig/collection/config/lockfile_generator.rbs +2 -0
  89. data/sig/inline_parser.rbs +2 -0
  90. data/sig/manifest.yaml +0 -1
  91. data/sig/namespace.rbs +20 -0
  92. data/sig/parser.rbs +10 -0
  93. data/sig/resolver/type_name_resolver.rbs +2 -4
  94. data/sig/rewriter.rbs +45 -0
  95. data/sig/typename.rbs +15 -0
  96. data/sig/unit_test/type_assertions.rbs +4 -0
  97. data/sig/wasm/deserializer.rbs +66 -0
  98. data/sig/wasm/serialization_schema.rbs +13 -0
  99. data/src/ast.c +186 -162
  100. data/src/lexer.c +137 -114
  101. data/src/lexer.re +1 -0
  102. data/src/lexstate.c +6 -1
  103. data/src/parser.c +55 -5
  104. data/src/serialize.c +958 -0
  105. data/src/util/rbs_allocator.c +1 -4
  106. data/stdlib/abbrev/0/array.rbs +1 -1
  107. data/stdlib/csv/0/csv.rbs +5 -5
  108. data/stdlib/digest/0/digest.rbs +1 -1
  109. data/stdlib/etc/0/etc.rbs +18 -4
  110. data/stdlib/fileutils/0/fileutils.rbs +21 -21
  111. data/stdlib/json/0/json.rbs +6 -6
  112. data/stdlib/openssl/0/openssl.rbs +7 -7
  113. data/stdlib/resolv/0/resolv.rbs +1 -1
  114. data/stdlib/shellwords/0/shellwords.rbs +1 -1
  115. data/stdlib/stringio/0/stringio.rbs +32 -10
  116. data/stdlib/strscan/0/string_scanner.rbs +74 -55
  117. data/stdlib/tsort/0/cyclic.rbs +1 -1
  118. data/stdlib/tsort/0/interfaces.rbs +8 -8
  119. data/stdlib/tsort/0/tsort.rbs +9 -9
  120. data/stdlib/zlib/0/gzip_reader.rbs +2 -2
  121. data/wasm/README.md +59 -0
  122. data/wasm/rbs_wasm.c +411 -0
  123. metadata +23 -5
  124. data/.vscode/extensions.json +0 -5
  125. data/.vscode/settings.json +0 -19
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # This file is generated by the templates/template.rb script and should not be
4
+ # modified manually. To change the template see
5
+ # templates/lib/rbs/wasm/serialization_schema.rb.erb
6
+
7
+ module RBS
8
+ module WASM
9
+ # Describes how to decode the binary buffer produced by `rbs_serialize_node`
10
+ # (src/serialize.c) back into RBS::AST objects. RBS::WASM::Deserializer walks
11
+ # this table; the matching encoder is generated from the same config.yml, so
12
+ # the two stay in sync.
13
+ #
14
+ # SCHEMA is indexed by node tag: tag 0 is NULL and SYMBOL_TAG is the
15
+ # interned-symbol tag. Each remaining entry is one of:
16
+ #
17
+ # [:node, class_name, expose_location, loc_children, fields, resolve_type_params]
18
+ # [:bool] / [:integer] / [:string_value] / [:record_field]
19
+ # [:signature] / [:namespace] / [:type_name]
20
+ #
21
+ # where loc_children is [[name, required?], ...] and fields is
22
+ # [[name, reader], ...] with reader one of :node, :node_list, :hash, :string,
23
+ # :bool, :location_range, :location_range_list, :attr_ivar_name, or
24
+ # [:enum, [value_or_nil, ...]].
25
+ module SerializationSchema
26
+ SYMBOL_TAG = 78
27
+
28
+ SCHEMA = [
29
+ nil, # tag 0 is reserved for NULL
30
+ [:node, "RBS::AST::Annotation", true, nil, [[:string, :string]], false],
31
+ [:bool],
32
+ [:node, "RBS::AST::Comment", true, nil, [[:string, :string]], false],
33
+ [:node, "RBS::AST::Declarations::Class", true, [[:keyword, true], [:name, true], [:end, true], [:type_params, false], [:lt, false]], [[:name, :node], [:type_params, :node_list], [:super_class, :node], [:members, :node_list], [:annotations, :node_list], [:comment, :node]], true],
34
+ [:node, "RBS::AST::Declarations::Class::Super", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
35
+ [:node, "RBS::AST::Declarations::ClassAlias", true, [[:keyword, true], [:new_name, true], [:eq, true], [:old_name, true]], [[:new_name, :node], [:old_name, :node], [:comment, :node], [:annotations, :node_list]], false],
36
+ [:node, "RBS::AST::Declarations::Constant", true, [[:name, true], [:colon, true]], [[:name, :node], [:type, :node], [:comment, :node], [:annotations, :node_list]], false],
37
+ [:node, "RBS::AST::Declarations::Global", true, [[:name, true], [:colon, true]], [[:name, :node], [:type, :node], [:comment, :node], [:annotations, :node_list]], false],
38
+ [:node, "RBS::AST::Declarations::Interface", true, [[:keyword, true], [:name, true], [:end, true], [:type_params, false]], [[:name, :node], [:type_params, :node_list], [:members, :node_list], [:annotations, :node_list], [:comment, :node]], true],
39
+ [:node, "RBS::AST::Declarations::Module", true, [[:keyword, true], [:name, true], [:end, true], [:type_params, false], [:colon, false], [:self_types, false]], [[:name, :node], [:type_params, :node_list], [:self_types, :node_list], [:members, :node_list], [:annotations, :node_list], [:comment, :node]], true],
40
+ [:node, "RBS::AST::Declarations::Module::Self", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
41
+ [:node, "RBS::AST::Declarations::ModuleAlias", true, [[:keyword, true], [:new_name, true], [:eq, true], [:old_name, true]], [[:new_name, :node], [:old_name, :node], [:comment, :node], [:annotations, :node_list]], false],
42
+ [:node, "RBS::AST::Declarations::TypeAlias", true, [[:keyword, true], [:name, true], [:eq, true], [:type_params, false]], [[:name, :node], [:type_params, :node_list], [:type, :node], [:annotations, :node_list], [:comment, :node]], true],
43
+ [:node, "RBS::AST::Directives::Use", true, [[:keyword, true]], [[:clauses, :node_list]], false],
44
+ [:node, "RBS::AST::Directives::Use::SingleClause", true, [[:type_name, true], [:keyword, false], [:new_name, false]], [[:type_name, :node], [:new_name, :node]], false],
45
+ [:node, "RBS::AST::Directives::Use::WildcardClause", true, [[:namespace, true], [:star, true]], [[:namespace, :node]], false],
46
+ [:integer],
47
+ [:node, "RBS::AST::Members::Alias", true, [[:keyword, true], [:new_name, true], [:old_name, true], [:new_kind, false], [:old_kind, false]], [[:new_name, :node], [:old_name, :node], [:kind, [:enum, [:instance, :singleton]]], [:annotations, :node_list], [:comment, :node]], false],
48
+ [:node, "RBS::AST::Members::AttrAccessor", true, [[:keyword, true], [:name, true], [:colon, true], [:kind, false], [:ivar, false], [:ivar_name, false], [:visibility, false]], [[:name, :node], [:type, :node], [:ivar_name, :attr_ivar_name], [:kind, [:enum, [:instance, :singleton]]], [:annotations, :node_list], [:comment, :node], [:visibility, [:enum, [nil, :public, :private]]]], false],
49
+ [:node, "RBS::AST::Members::AttrReader", true, [[:keyword, true], [:name, true], [:colon, true], [:kind, false], [:ivar, false], [:ivar_name, false], [:visibility, false]], [[:name, :node], [:type, :node], [:ivar_name, :attr_ivar_name], [:kind, [:enum, [:instance, :singleton]]], [:annotations, :node_list], [:comment, :node], [:visibility, [:enum, [nil, :public, :private]]]], false],
50
+ [:node, "RBS::AST::Members::AttrWriter", true, [[:keyword, true], [:name, true], [:colon, true], [:kind, false], [:ivar, false], [:ivar_name, false], [:visibility, false]], [[:name, :node], [:type, :node], [:ivar_name, :attr_ivar_name], [:kind, [:enum, [:instance, :singleton]]], [:annotations, :node_list], [:comment, :node], [:visibility, [:enum, [nil, :public, :private]]]], false],
51
+ [:node, "RBS::AST::Members::ClassInstanceVariable", true, [[:name, true], [:colon, true], [:kind, false]], [[:name, :node], [:type, :node], [:comment, :node]], false],
52
+ [:node, "RBS::AST::Members::ClassVariable", true, [[:name, true], [:colon, true], [:kind, false]], [[:name, :node], [:type, :node], [:comment, :node]], false],
53
+ [:node, "RBS::AST::Members::Extend", true, [[:name, true], [:keyword, true], [:args, false]], [[:name, :node], [:args, :node_list], [:annotations, :node_list], [:comment, :node]], false],
54
+ [:node, "RBS::AST::Members::Include", true, [[:name, true], [:keyword, true], [:args, false]], [[:name, :node], [:args, :node_list], [:annotations, :node_list], [:comment, :node]], false],
55
+ [:node, "RBS::AST::Members::InstanceVariable", true, [[:name, true], [:colon, true], [:kind, false]], [[:name, :node], [:type, :node], [:comment, :node]], false],
56
+ [:node, "RBS::AST::Members::MethodDefinition", true, [[:keyword, true], [:name, true], [:kind, false], [:overloading, false], [:visibility, false]], [[:name, :node], [:kind, [:enum, [:instance, :singleton, :singleton_instance]]], [:overloads, :node_list], [:annotations, :node_list], [:comment, :node], [:overloading, :bool], [:visibility, [:enum, [nil, :public, :private]]]], false],
57
+ [:node, "RBS::AST::Members::MethodDefinition::Overload", false, nil, [[:annotations, :node_list], [:method_type, :node]], false],
58
+ [:node, "RBS::AST::Members::Prepend", true, [[:name, true], [:keyword, true], [:args, false]], [[:name, :node], [:args, :node_list], [:annotations, :node_list], [:comment, :node]], false],
59
+ [:node, "RBS::AST::Members::Private", true, nil, nil, false],
60
+ [:node, "RBS::AST::Members::Public", true, nil, nil, false],
61
+ [:node, "RBS::AST::Ruby::Annotations::BlockParamTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:ampersand_location, :location_range], [:name_location, :location_range], [:colon_location, :location_range], [:question_location, :location_range], [:type_location, :location_range], [:type, :node], [:comment_location, :location_range]], false],
62
+ [:node, "RBS::AST::Ruby::Annotations::ClassAliasAnnotation", true, nil, [[:prefix_location, :location_range], [:keyword_location, :location_range], [:type_name, :node], [:type_name_location, :location_range]], false],
63
+ [:node, "RBS::AST::Ruby::Annotations::ColonMethodTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:annotations, :node_list], [:method_type, :node]], false],
64
+ [:node, "RBS::AST::Ruby::Annotations::DoubleSplatParamTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:star2_location, :location_range], [:name_location, :location_range], [:colon_location, :location_range], [:param_type, :node], [:comment_location, :location_range]], false],
65
+ [:node, "RBS::AST::Ruby::Annotations::InstanceVariableAnnotation", true, nil, [[:prefix_location, :location_range], [:ivar_name, :node], [:ivar_name_location, :location_range], [:colon_location, :location_range], [:type, :node], [:comment_location, :location_range]], false],
66
+ [:node, "RBS::AST::Ruby::Annotations::MethodTypesAnnotation", true, nil, [[:prefix_location, :location_range], [:overloads, :node_list], [:vertical_bar_locations, :location_range_list], [:dot3_location, :location_range]], false],
67
+ [:node, "RBS::AST::Ruby::Annotations::ModuleAliasAnnotation", true, nil, [[:prefix_location, :location_range], [:keyword_location, :location_range], [:type_name, :node], [:type_name_location, :location_range]], false],
68
+ [:node, "RBS::AST::Ruby::Annotations::ModuleSelfAnnotation", true, nil, [[:prefix_location, :location_range], [:keyword_location, :location_range], [:colon_location, :location_range], [:name, :node], [:args, :node_list], [:open_bracket_location, :location_range], [:close_bracket_location, :location_range], [:args_comma_locations, :location_range_list], [:comment_location, :location_range]], false],
69
+ [:node, "RBS::AST::Ruby::Annotations::NodeTypeAssertion", true, nil, [[:prefix_location, :location_range], [:type, :node]], false],
70
+ [:node, "RBS::AST::Ruby::Annotations::ParamTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:name_location, :location_range], [:colon_location, :location_range], [:param_type, :node], [:comment_location, :location_range]], false],
71
+ [:node, "RBS::AST::Ruby::Annotations::ReturnTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:return_location, :location_range], [:colon_location, :location_range], [:return_type, :node], [:comment_location, :location_range]], false],
72
+ [:node, "RBS::AST::Ruby::Annotations::SkipAnnotation", true, nil, [[:prefix_location, :location_range], [:skip_location, :location_range], [:comment_location, :location_range]], false],
73
+ [:node, "RBS::AST::Ruby::Annotations::SplatParamTypeAnnotation", true, nil, [[:prefix_location, :location_range], [:star_location, :location_range], [:name_location, :location_range], [:colon_location, :location_range], [:param_type, :node], [:comment_location, :location_range]], false],
74
+ [:node, "RBS::AST::Ruby::Annotations::TypeApplicationAnnotation", true, nil, [[:prefix_location, :location_range], [:type_args, :node_list], [:close_bracket_location, :location_range], [:comma_locations, :location_range_list]], false],
75
+ [:string_value],
76
+ [:node, "RBS::AST::TypeParam", true, [[:name, true], [:variance, false], [:unchecked, false], [:upper_bound, false], [:lower_bound, false], [:default, false]], [[:name, :node], [:variance, [:enum, [:invariant, :covariant, :contravariant]]], [:upper_bound, :node], [:lower_bound, :node], [:default_type, :node], [:unchecked, :bool]], false],
77
+ [:node, "RBS::MethodType", true, [[:type, true], [:type_params, false]], [[:type_params, :node_list], [:type, :node], [:block, :node]], true],
78
+ [:namespace],
79
+ [:signature],
80
+ [:type_name],
81
+ [:node, "RBS::Types::Alias", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
82
+ [:node, "RBS::Types::Bases::Any", true, nil, [[:todo, :bool]], false],
83
+ [:node, "RBS::Types::Bases::Bool", true, nil, nil, false],
84
+ [:node, "RBS::Types::Bases::Bottom", true, nil, nil, false],
85
+ [:node, "RBS::Types::Bases::Class", true, nil, nil, false],
86
+ [:node, "RBS::Types::Bases::Instance", true, nil, nil, false],
87
+ [:node, "RBS::Types::Bases::Nil", true, nil, nil, false],
88
+ [:node, "RBS::Types::Bases::Self", true, nil, nil, false],
89
+ [:node, "RBS::Types::Bases::Top", true, nil, nil, false],
90
+ [:node, "RBS::Types::Bases::Void", true, nil, nil, false],
91
+ [:node, "RBS::Types::Block", true, nil, [[:type, :node], [:required, :bool], [:self_type, :node]], false],
92
+ [:node, "RBS::Types::ClassInstance", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
93
+ [:node, "RBS::Types::ClassSingleton", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
94
+ [:node, "RBS::Types::Function", false, nil, [[:required_positionals, :node_list], [:optional_positionals, :node_list], [:rest_positionals, :node], [:trailing_positionals, :node_list], [:required_keywords, :hash], [:optional_keywords, :hash], [:rest_keywords, :node], [:return_type, :node]], false],
95
+ [:node, "RBS::Types::Function::Param", true, [[:name, false]], [[:type, :node], [:name, :node]], false],
96
+ [:node, "RBS::Types::Interface", true, [[:name, true], [:args, false]], [[:name, :node], [:args, :node_list]], false],
97
+ [:node, "RBS::Types::Intersection", true, nil, [[:types, :node_list]], false],
98
+ [:node, "RBS::Types::Literal", true, nil, [[:literal, :node]], false],
99
+ [:node, "RBS::Types::Optional", true, nil, [[:type, :node]], false],
100
+ [:node, "RBS::Types::Proc", true, nil, [[:type, :node], [:block, :node], [:self_type, :node]], false],
101
+ [:node, "RBS::Types::Record", true, nil, [[:all_fields, :hash]], false],
102
+ [:record_field],
103
+ [:node, "RBS::Types::Tuple", true, nil, [[:types, :node_list]], false],
104
+ [:node, "RBS::Types::Union", true, nil, [[:types, :node_list]], false],
105
+ [:node, "RBS::Types::UntypedFunction", false, nil, [[:return_type, :node]], false],
106
+ [:node, "RBS::Types::Variable", true, nil, [[:name, :node]], false],
107
+ ].freeze
108
+ end
109
+ end
110
+ end
data/lib/rbs.rb CHANGED
@@ -4,7 +4,11 @@ require "rbs/version"
4
4
 
5
5
  require "set"
6
6
  require "json"
7
- require "pathname" unless defined?(Pathname)
7
+ # Always require pathname: `Pathname()` (Kernel#Pathname) is only defined once
8
+ # pathname is loaded. Guarding on `defined?(Pathname)` is wrong because another
9
+ # library (e.g. Bundler) can define the Pathname constant without that method,
10
+ # which left RBS::EnvironmentLoader's `Pathname(...)` undefined on JRuby.
11
+ require "pathname"
8
12
  require "pp"
9
13
  require "logger"
10
14
  require "tsort"
@@ -51,6 +55,7 @@ require "rbs/resolver/constant_resolver"
51
55
  require "rbs/resolver/type_name_resolver"
52
56
  require "rbs/ast/comment"
53
57
  require "rbs/writer"
58
+ require "rbs/rewriter"
54
59
  require "rbs/prototype/helpers"
55
60
  require "rbs/prototype/rbi"
56
61
  require "rbs/prototype/rb"
@@ -68,7 +73,14 @@ require "rbs/type_alias_dependency"
68
73
  require "rbs/type_alias_regularity"
69
74
  require "rbs/collection"
70
75
 
71
- require "rbs_extension"
76
+ if RUBY_ENGINE == "jruby"
77
+ # JRuby cannot load the MRI C extension. Run the parser in WebAssembly and
78
+ # provide pure-Ruby implementations of RBS::Location and RBS::Parser instead.
79
+ require "rbs/wasm/location"
80
+ require "rbs/wasm/parser"
81
+ else
82
+ require "rbs_extension"
83
+ end
72
84
  require "rbs/parser_aux"
73
85
  require "rbs/location_aux"
74
86
 
data/rbs.gemspec CHANGED
@@ -30,12 +30,29 @@ Gem::Specification.new do |spec|
30
30
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
31
  `git ls-files -z`.split("\x0").reject do |f|
32
32
  [
33
- %r{^(test|spec|features|bin|steep|benchmark|templates|rust)/},
33
+ %r{^(test|spec|features|bin|steep|benchmark|templates|rust|\.vscode)/},
34
34
  /Gemfile/,
35
35
  ].any? {|r| f.match(r) }
36
36
  end
37
37
  end
38
- spec.extensions = %w{ext/rbs_extension/extconf.rb}
38
+
39
+ # JRuby cannot load the MRI C extension. On JRuby (and in the `java` platform
40
+ # gem, built with RBS_PLATFORM=java) RBS runs the WebAssembly-backed parser, so
41
+ # ship the prebuilt parser and the Chicory jars (assembled by
42
+ # `rake wasm:jruby_setup`) and skip the C extension.
43
+ building_java_gem = ENV["RBS_PLATFORM"] == "java"
44
+ on_jruby = defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
45
+
46
+ if building_java_gem || on_jruby
47
+ # Only stamp the platform when building the release gem; leave it unset for
48
+ # local development on JRuby so it still matches a `ruby` platform lockfile.
49
+ spec.platform = "java" if building_java_gem
50
+ spec.files += Dir.chdir(File.expand_path('..', __FILE__)) do
51
+ Dir.glob("lib/rbs/wasm/rbs_parser.wasm") + Dir.glob("lib/rbs/wasm/jars/*.jar")
52
+ end
53
+ else
54
+ spec.extensions = %w{ext/rbs_extension/extconf.rb}
55
+ end
39
56
 
40
57
  if false
41
58
  spec.required_ruby_version = ">= 3.4"
@@ -8,9 +8,10 @@ module RBS
8
8
 
9
9
  def initialize: (source: RDocSource) -> void
10
10
 
11
- def annotate_file: (Pathname, preserve: bool) -> void
11
+ def annotate_file: (Pathname) -> void
12
+ | %a{deprecated: it preserves the format by default} (Pathname, preserve: bool) -> void
12
13
 
13
- def annotate_decls: (Array[AST::Declarations::t], ?outer: Array[Namespace]) -> void
14
+ def annotate_decls: (Array[AST::Declarations::t], Rewriter, ?outer: Array[Namespace]) -> void
14
15
 
15
16
  interface _PathTester
16
17
  def test_path: (String) -> bool
@@ -50,15 +51,15 @@ module RBS
50
51
 
51
52
  def resolve_doc_source: (Annotations::Copy?, tester: _PathTester) { () -> String? } -> String?
52
53
 
53
- def annotate_class: (AST::Declarations::Class | AST::Declarations::Module, outer: Array[Namespace]) -> void
54
+ def annotate_class: (AST::Declarations::Class | AST::Declarations::Module, Rewriter, outer: Array[Namespace]) -> void
54
55
 
55
- def annotate_constant: (AST::Declarations::Constant, outer: Array[Namespace]) -> void
56
+ def annotate_constant: (AST::Declarations::Constant, Rewriter, outer: Array[Namespace]) -> void
56
57
 
57
- def annotate_method: (TypeName, AST::Members::MethodDefinition) -> void
58
+ def annotate_method: (TypeName, AST::Members::MethodDefinition, Rewriter) -> void
58
59
 
59
- def annotate_alias: (TypeName, AST::Members::Alias) -> void
60
+ def annotate_alias: (TypeName, AST::Members::Alias, Rewriter) -> void
60
61
 
61
- def annotate_attribute: (TypeName, AST::Members::AttrReader | AST::Members::AttrWriter | AST::Members::AttrAccessor) -> void
62
+ def annotate_attribute: (TypeName, AST::Members::AttrReader | AST::Members::AttrWriter | AST::Members::AttrAccessor, Rewriter) -> void
62
63
 
63
64
  def annotations: (_Annotated) -> Annotations
64
65
 
@@ -70,10 +71,12 @@ module RBS
70
71
  # - If empty string is given as `comment`, it deletes the original comment.
71
72
  # - If `nil` is given as `comment`, it keeps the original comment.
72
73
  #
73
- def replace_comment: (Object & _Commented, String? comment) -> void
74
+ def replace_comment: (_Commented, String?, Rewriter) -> void
74
75
 
75
76
  interface _Commented
76
- def comment: () -> AST::Comment?
77
+ %a{pure} def comment: () -> AST::Comment?
78
+ %a{pure} def location: () -> Location[untyped, untyped]?
79
+ def annotations: () -> Array[AST::Annotation]
77
80
  end
78
81
 
79
82
  def resolve_name: (TypeName, outer: Array[Namespace]) -> TypeName
@@ -7,6 +7,7 @@ module RBS
7
7
  | SkipAnnotation
8
8
  | ReturnTypeAnnotation
9
9
  | InstanceVariableAnnotation
10
+ | ModuleSelfAnnotation
10
11
  | ParamTypeAnnotation
11
12
  | SplatParamTypeAnnotation
12
13
  | DoubleSplatParamTypeAnnotation
@@ -237,6 +238,54 @@ module RBS
237
238
  def type_fingerprint: () -> untyped
238
239
  end
239
240
 
241
+ # `@rbs module-self: NAME[ARGS] -- comment` annotation in leading comments
242
+ #
243
+ # ```
244
+ # @rbs module-self: _Each[String, Integer] -- comment
245
+ # ^^^^ -- prefix_location
246
+ # ^^^^^^^^^^^ -- keyword_location
247
+ # ^ -- colon_location
248
+ # ^ -- open_bracket_location
249
+ # ^ -- args_comma_locations[0]
250
+ # ^ -- close_bracket_location
251
+ # ^^^^^^^^^^ -- comment_location
252
+ # ```
253
+ #
254
+ class ModuleSelfAnnotation < Base
255
+ attr_reader keyword_location: Location
256
+
257
+ attr_reader colon_location: Location
258
+
259
+ attr_reader name: TypeName
260
+
261
+ attr_reader args: Array[Types::t]
262
+
263
+ attr_reader open_bracket_location: Location?
264
+
265
+ attr_reader close_bracket_location: Location?
266
+
267
+ attr_reader args_comma_locations: Array[Location]
268
+
269
+ attr_reader comment_location: Location?
270
+
271
+ def initialize: (
272
+ location: Location,
273
+ prefix_location: Location,
274
+ keyword_location: Location,
275
+ colon_location: Location,
276
+ name: TypeName,
277
+ args: Array[Types::t],
278
+ open_bracket_location: Location?,
279
+ close_bracket_location: Location?,
280
+ args_comma_locations: Array[Location],
281
+ comment_location: Location?,
282
+ ) -> void
283
+
284
+ def map_type_name: () { (TypeName) -> TypeName } -> self
285
+
286
+ def type_fingerprint: () -> untyped
287
+ end
288
+
240
289
  class ParamTypeAnnotation < Base
241
290
  attr_reader name_location: Location
242
291
 
@@ -16,6 +16,7 @@ module RBS
16
16
  | IncludeMember | ExtendMember | PrependMember
17
17
  | AttrReaderMember | AttrWriterMember | AttrAccessorMember
18
18
  | InstanceVariableMember
19
+ | ModuleSelfMember
19
20
 
20
21
  class MethodTypeAnnotation
21
22
  class DocStyle
@@ -83,10 +84,15 @@ module RBS
83
84
 
84
85
  attr_reader name: Symbol
85
86
  attr_reader node: Prism::DefNode
87
+ attr_reader kind: :instance | :singleton
86
88
  attr_reader method_type: MethodTypeAnnotation
87
89
  attr_reader leading_comment: CommentBlock?
88
90
 
89
- def initialize: (Buffer, Symbol name, Prism::DefNode node, MethodTypeAnnotation, CommentBlock? leading_comment) -> void
91
+ def initialize: (Buffer, Symbol name, Prism::DefNode node, MethodTypeAnnotation, CommentBlock? leading_comment, ?kind: :instance | :singleton) -> void
92
+
93
+ def singleton?: () -> bool
94
+
95
+ def instance?: () -> bool
90
96
 
91
97
  def location: () -> Location
92
98
 
@@ -172,6 +178,20 @@ module RBS
172
178
 
173
179
  def type_fingerprint: () -> untyped
174
180
  end
181
+
182
+ class ModuleSelfMember < Base
183
+ attr_reader annotation: Annotations::ModuleSelfAnnotation
184
+
185
+ def initialize: (Buffer, Annotations::ModuleSelfAnnotation) -> void
186
+
187
+ def name: () -> TypeName
188
+
189
+ def args: () -> Array[Types::t]
190
+
191
+ def location: () -> Location
192
+
193
+ def type_fingerprint: () -> untyped
194
+ end
175
195
  end
176
196
  end
177
197
  end
data/sig/buffer.rbs CHANGED
@@ -60,6 +60,19 @@ module RBS
60
60
  def rbs_location: (Prism::Location) -> Location
61
61
  | (Prism::Location, Prism::Location) -> Location
62
62
 
63
+ # Translate a byte offset (into the top buffer's source) to a character offset.
64
+ #
65
+ # Resolution is delegated to the top buffer, which keeps a sparse cache of
66
+ # resolved (byte, char) pairs so successive calls scan only the delta from the
67
+ # nearest cached pair. Amortizes to O(content_size) across all calls; a single
68
+ # call is one byteslice + length.
69
+ #
70
+ def character_offset: (Integer byte_offset) -> Integer
71
+
72
+ # Sparse cache backing `#character_offset`: a pair `[byte_keys, char_values]`
73
+ # kept in ascending byte order to support binary search.
74
+ @character_offset_cache: [Array[Integer], Array[Integer]]?
75
+
63
76
  # Construct a buffer from substrings of this buffer.
64
77
  #
65
78
  # The returned buffer contains lines from given ranges.
@@ -75,7 +88,12 @@ module RBS
75
88
  # buffer.sub_buffer(lines: [5..7]) # => Raises an error because the range contains newline
76
89
  # ```
77
90
  #
78
- %a{pure} def sub_buffer: (lines: Array[Range[Integer]]) -> Buffer
91
+ # `byte_lines_hint:` is an optional performance hint: byte ranges corresponding
92
+ # to `lines:`. When provided, slicing uses `byteslice` (O(slice_size) per line)
93
+ # instead of `content[char_range]`, which on a multi-byte string is O(content_size)
94
+ # per call. Result is identical either way.
95
+ #
96
+ %a{pure} def sub_buffer: (lines: Array[Range[Integer]], ?byte_lines_hint: Array[Range[Integer]]?) -> Buffer
79
97
 
80
98
  %a{pure} def parent_buffer: () -> Buffer?
81
99
 
@@ -6,6 +6,8 @@ module RBS
6
6
  #
7
7
  ALUMNI_STDLIBS: Hash[String, String?]
8
8
 
9
+ NONGEM_STDLIBS: Set[String]
10
+
9
11
  class GemfileLockMismatchError < StandardError
10
12
  @expected: Pathname
11
13
 
@@ -110,6 +110,8 @@ module RBS
110
110
 
111
111
  def report_unused_block: (AST::Ruby::CommentBlock) -> void
112
112
 
113
+ def visit_class_or_module_body: (module_context, Prism::ClassNode | Prism::ModuleNode) -> void
114
+
113
115
  private
114
116
 
115
117
  def parse_mixin_call: (Prism::CallNode) -> void
data/sig/manifest.yaml CHANGED
@@ -1,5 +1,4 @@
1
1
  dependencies:
2
- - name: logger
3
2
  - name: json
4
3
  - name: optparse
5
4
  - name: tsort
data/sig/namespace.rbs CHANGED
@@ -40,10 +40,30 @@ module RBS
40
40
 
41
41
  @parent: Namespace?
42
42
 
43
+ @hash: Integer?
44
+
43
45
  self.@root: Namespace?
44
46
 
45
47
  self.@empty: Namespace?
46
48
 
49
+ self.@intern_mutex: Mutex
50
+
51
+ # Per-`absolute` flag tries of nested Hashes keyed on path Symbols,
52
+ # with the cached Namespace stored under the `INTERN_LEAF` sentinel
53
+ # at each path's terminal node.
54
+ self.@intern_trie_absolute: Hash[Symbol | Module, untyped]
55
+
56
+ self.@intern_trie_relative: Hash[Symbol | Module, untyped]
57
+
58
+ INTERN_LEAF: Module
59
+
60
+ # Returns an interned `Namespace` instance.
61
+ #
62
+ # Subsequent calls with structurally equal `path` and `absolute` arguments
63
+ # return the same object. The internal `path` array is duplicated and
64
+ # frozen before being cached.
65
+ def self.[]: (Array[Symbol] path, boolish absolute) -> Namespace
66
+
47
67
  # Returns new _empty_ namespace.
48
68
  def self.empty: () -> Namespace
49
69
 
data/sig/parser.rbs CHANGED
@@ -140,6 +140,16 @@ module RBS
140
140
 
141
141
  def self._parse_signature: (Buffer, Integer start_pos, Integer end_pos) -> [Array[AST::Directives::t], Array[AST::Declarations::t]]
142
142
 
143
+ # Parse and serialize the result to the binary format consumed by
144
+ # RBS::WASM::Deserializer (see ext/rbs_extension/main.c and
145
+ # docs/wasm_serialization.md). The `_to_bytes` variants exist so the
146
+ # round-trip can be exercised on CRuby.
147
+ def self._parse_type_to_bytes: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof, bool void_allowed, bool self_allowed, bool classish_allowed) -> String?
148
+
149
+ def self._parse_method_type_to_bytes: (Buffer, Integer start_pos, Integer end_pos, Array[Symbol] variables, bool require_eof) -> String?
150
+
151
+ def self._parse_signature_to_bytes: (Buffer, Integer start_pos, Integer end_pos) -> String
152
+
143
153
  def self._parse_type_params: (Buffer, Integer start_pos, Integer end_pos, bool module_type_params) -> Array[AST::TypeParam]
144
154
 
145
155
  def self._lex: (Buffer, Integer end_pos) -> Array[[Symbol, Location[untyped, untyped]]]
@@ -6,8 +6,6 @@ module RBS
6
6
  # It just ignores included modules and super classes.
7
7
  #
8
8
  class TypeNameResolver
9
- type query = [TypeName, context]
10
-
11
9
  def initialize: (Set[TypeName] all_names, Hash[TypeName, [TypeName, context]] aliases) -> void
12
10
 
13
11
  def self.build: (Environment) -> instance
@@ -32,7 +30,7 @@ module RBS
32
30
 
33
31
  attr_reader aliases: Hash[TypeName, [TypeName, context]]
34
32
 
35
- attr_reader cache: Hash[query, TypeName?]
33
+ attr_reader cache: Hash[context, Hash[TypeName, TypeName?]]
36
34
 
37
35
  # Returns the type name if it exists in `all_names` (normalized)
38
36
  #
@@ -42,7 +40,7 @@ module RBS
42
40
  #
43
41
  def aliased_name?: (TypeName) -> TypeName?
44
42
 
45
- def try_cache: (query) { () -> TypeName? } -> TypeName?
43
+ def try_cache: (TypeName, context) { () -> TypeName? } -> TypeName?
46
44
 
47
45
  # Translates the head module name in the context and returns an absolute type name
48
46
  #
data/sig/rewriter.rbs ADDED
@@ -0,0 +1,45 @@
1
+ module RBS
2
+ # Rewriter performs targeted character-range replacements on Buffer content.
3
+ #
4
+ # Unlike Writer which regenerates entire source from AST, Rewriter preserves
5
+ # everything outside the rewritten ranges, including non-documentation comments.
6
+ #
7
+ # Rewrite requests are buffered and applied all at once when `#string` is called.
8
+ #
9
+ class Rewriter
10
+ attr_reader buffer: Buffer
11
+
12
+ # Initialize with a toplevel buffer.
13
+ # Raises if the buffer has a parent (non-toplevel).
14
+ #
15
+ def initialize: (Buffer buffer) -> void
16
+
17
+ # Register a rewrite request for the given location.
18
+ #
19
+ def rewrite: (Location[untyped, untyped] location, String string) -> self
20
+
21
+ # Add a new comment before the earliest of the given locations.
22
+ #
23
+ def add_comment: (*Location[untyped, untyped] locations, content: String) -> self
24
+
25
+ # Replace an existing comment's content.
26
+ #
27
+ def replace_comment: (AST::Comment comment, content: String) -> self
28
+
29
+ # Delete an existing comment.
30
+ #
31
+ def delete_comment: (AST::Comment comment) -> self
32
+
33
+ # Apply all buffered rewrites and return the resulting string.
34
+ #
35
+ # Raises if any rewrite ranges overlap.
36
+ #
37
+ def string: () -> String
38
+
39
+ private
40
+
41
+ def format_comment: (String content, String indent) -> String
42
+
43
+ @rewrites: Array[[Location[untyped, untyped], String]]
44
+ end
45
+ end
data/sig/typename.rbs CHANGED
@@ -28,6 +28,21 @@ module RBS
28
28
  #
29
29
  def initialize: (namespace: Namespace, name: Symbol) -> void
30
30
 
31
+ @hash: Integer?
32
+
33
+ self.@intern_mutex: Mutex
34
+
35
+ # Two-level Hash keyed by canonical Namespace identity (outer uses
36
+ # `compare_by_identity`) and name Symbol.
37
+ self.@intern_cache: Hash[Namespace, Hash[Symbol, TypeName]]
38
+
39
+ # Returns an interned `TypeName` instance.
40
+ #
41
+ # The given `namespace` is canonicalized so cached instances always hold an
42
+ # interned namespace. Subsequent calls with structurally equal arguments
43
+ # return the same object.
44
+ def self.[]: (Namespace namespace, Symbol name) -> TypeName
45
+
31
46
  def ==: (untyped other) -> bool
32
47
 
33
48
  def hash: () -> Integer
@@ -179,6 +179,10 @@ module RBS
179
179
  #
180
180
  def assert_type: (String | Types::t value_type, untyped value) -> void
181
181
 
182
+ # Asserts if RBS definition declares given `method_name` with `visibility`
183
+ #
184
+ def assert_visibility: (:private | :public visibility, Symbol method_name) -> void
185
+
182
186
  # Allow non _simple-type_ method types given to `assert_send_type` and `refute_send_type`
183
187
  #
184
188
  # ```ruby
@@ -0,0 +1,66 @@
1
+ module RBS
2
+ module WASM
3
+ # Rebuilds RBS::AST objects from the binary buffer produced by
4
+ # `rbs_serialize_node` (src/serialize.c), driven by SerializationSchema.
5
+ class Deserializer
6
+ @bytes: String
7
+
8
+ @buffer: Buffer
9
+
10
+ @encoding: Encoding
11
+
12
+ @pos: Integer
13
+
14
+ @class_cache: Hash[String, untyped]
15
+
16
+ # Deserialize a buffer produced for a whole signature, returning
17
+ # `[directives, declarations]` to match RBS::Parser._parse_signature.
18
+ def self.deserialize: (String bytes, Buffer buffer) -> untyped
19
+
20
+ # Deserialize a bare node list (RBS::Parser._parse_type_params).
21
+ def self.deserialize_node_list: (String bytes, Buffer buffer) -> Array[untyped]
22
+
23
+ # Deserialize the token stream from rbs_wasm_lex (RBS::Parser._lex).
24
+ def self.deserialize_tokens: (String bytes, Buffer buffer) -> Array[[ Symbol, Location ]]
25
+
26
+ def initialize: (String bytes, Buffer buffer) -> void
27
+
28
+ # Reads the next node and returns the reconstructed Ruby value.
29
+ def read_node: () -> untyped
30
+
31
+ def read_node_list: () -> Array[untyped]
32
+
33
+ def read_tokens: () -> Array[[ Symbol, Location ]]
34
+
35
+ private
36
+
37
+ def read_struct: (Array[untyped] entry) -> untyped
38
+
39
+ def read_field: (untyped reader) -> untyped
40
+
41
+ def read_hash: () -> Hash[untyped, untyped]
42
+
43
+ def read_count: () -> Integer
44
+
45
+ def read_location: (Array[untyped]? loc_children) -> Location?
46
+
47
+ def read_location_value: () -> Location?
48
+
49
+ def read_location_value_list: () -> Array[Location?]
50
+
51
+ def read_attr_ivar_name: () -> (Symbol | false | nil)
52
+
53
+ def read_range: () -> [ Integer, Integer ]?
54
+
55
+ def read_u8: () -> Integer
56
+
57
+ def read_u32: () -> Integer
58
+
59
+ def read_i32: () -> Integer
60
+
61
+ def read_string: (Encoding encoding) -> String
62
+
63
+ def class_for: (String name) -> untyped
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,13 @@
1
+ module RBS
2
+ module WASM
3
+ # Generated from config.yml. See `templates/lib/rbs/wasm/serialization_schema.rb.erb`.
4
+ module SerializationSchema
5
+ # The node tag used for interned symbols (`rbs_ast_symbol`).
6
+ SYMBOL_TAG: Integer
7
+
8
+ # Indexed by node tag. Each entry describes how to decode one node; see the
9
+ # generated file for the exact shape.
10
+ SCHEMA: Array[Array[untyped]?]
11
+ end
12
+ end
13
+ end