rbs 3.9.5 → 3.10.0.pre.1
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 +4 -4
- data/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +54 -0
- data/.github/workflows/comments.yml +3 -3
- data/.github/workflows/ruby.yml +34 -19
- data/.github/workflows/typecheck.yml +1 -1
- data/.github/workflows/windows.yml +1 -1
- data/.gitignore +4 -0
- data/README.md +38 -1
- data/Rakefile +152 -23
- data/config.yml +190 -62
- data/core/array.rbs +44 -43
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +3 -2
- data/core/enumerable.rbs +89 -2
- data/core/errno.rbs +8 -0
- data/core/errors.rbs +28 -1
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +3 -3
- data/core/file.rbs +26 -11
- data/core/float.rbs +1 -1
- data/core/gc.rbs +422 -281
- data/core/hash.rbs +1024 -727
- data/core/io/wait.rbs +11 -33
- data/core/io.rbs +6 -4
- data/core/kernel.rbs +49 -43
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +42 -3
- data/core/method.rbs +14 -6
- data/core/module.rbs +71 -11
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +8 -8
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +13 -0
- data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
- data/core/proc.rbs +15 -8
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +278 -437
- data/core/range.rbs +6 -7
- data/core/rbs/unnamed/argf.rbs +1 -1
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +4 -2
- data/core/regexp.rbs +22 -17
- data/core/ruby_vm.rbs +6 -4
- data/core/rubygems/errors.rbs +3 -70
- data/core/rubygems/rubygems.rbs +11 -79
- data/core/set.rbs +439 -332
- data/core/string.rbs +2897 -1117
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +83 -20
- data/core/time.rbs +35 -9
- data/core/unbound_method.rbs +14 -6
- data/docs/aliases.md +79 -0
- data/docs/collection.md +2 -2
- data/docs/gem.md +0 -1
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1016 -0
- data/ext/rbs_extension/ast_translation.h +37 -0
- data/ext/rbs_extension/class_constants.c +157 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +25 -1
- data/ext/rbs_extension/legacy_location.c +317 -0
- data/ext/rbs_extension/legacy_location.h +45 -0
- data/ext/rbs_extension/main.c +365 -14
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +687 -0
- data/include/rbs/defines.h +86 -0
- data/include/rbs/lexer.h +199 -0
- data/include/rbs/location.h +59 -0
- data/include/rbs/parser.h +135 -0
- data/include/rbs/string.h +49 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -67
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +23 -0
- data/include/rbs.h +1 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/cli/validate.rb +2 -2
- data/lib/rbs/cli.rb +1 -1
- data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
- data/lib/rbs/environment.rb +64 -59
- data/lib/rbs/environment_loader.rb +1 -1
- data/lib/rbs/errors.rb +1 -1
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +124 -38
- data/lib/rbs/test/type_check.rb +13 -0
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +1 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +3 -3
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/environment.rbs +57 -6
- data/sig/manifest.yaml +0 -1
- data/sig/parser.rbs +20 -0
- data/sig/resolver/type_name_resolver.rbs +38 -7
- data/sig/types.rbs +4 -1
- data/src/ast.c +1256 -0
- data/src/lexer.c +2956 -0
- data/src/lexer.re +147 -0
- data/src/lexstate.c +205 -0
- data/src/location.c +71 -0
- data/src/parser.c +3495 -0
- data/src/string.c +90 -0
- data/src/util/rbs_allocator.c +152 -0
- data/src/util/rbs_assert.c +21 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +16 -86
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +131 -0
- data/stdlib/cgi/0/core.rbs +2 -396
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +153 -0
- data/stdlib/coverage/0/coverage.rbs +3 -1
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/erb/0/erb.rbs +737 -347
- data/stdlib/fileutils/0/fileutils.rbs +18 -13
- data/stdlib/forwardable/0/forwardable.rbs +3 -0
- data/stdlib/json/0/json.rbs +67 -48
- data/stdlib/net-http/0/net-http.rbs +3 -0
- data/stdlib/objspace/0/objspace.rbs +8 -3
- data/stdlib/open-uri/0/open-uri.rbs +40 -0
- data/stdlib/openssl/0/openssl.rbs +182 -149
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/stdlib/rdoc/0/code_object.rbs +2 -2
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +25 -68
- data/stdlib/ripper/0/ripper.rbs +5 -2
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/socket.rbs +13 -1
- data/stdlib/socket/0/tcp_socket.rbs +10 -2
- data/stdlib/stringio/0/stringio.rbs +412 -80
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +1 -1
- data/stdlib/tsort/0/cyclic.rbs +3 -0
- data/stdlib/uri/0/common.rbs +11 -2
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +16 -15
- data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
- data/stdlib/zlib/0/zstream.rbs +1 -0
- metadata +41 -18
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/lib/rbs/environment.rb
CHANGED
|
@@ -223,21 +223,17 @@ module RBS
|
|
|
223
223
|
end
|
|
224
224
|
end
|
|
225
225
|
|
|
226
|
-
def class_entry(type_name)
|
|
227
|
-
case
|
|
228
|
-
when
|
|
229
|
-
|
|
230
|
-
when (class_alias = class_alias_decls[type_name]).is_a?(ClassAliasEntry)
|
|
231
|
-
class_alias
|
|
226
|
+
def class_entry(type_name, normalized: false)
|
|
227
|
+
case entry = constant_entry(type_name, normalized: normalized || false)
|
|
228
|
+
when ClassEntry, ClassAliasEntry
|
|
229
|
+
entry
|
|
232
230
|
end
|
|
233
231
|
end
|
|
234
232
|
|
|
235
|
-
def module_entry(type_name)
|
|
236
|
-
case
|
|
237
|
-
when
|
|
238
|
-
|
|
239
|
-
when (module_alias = class_alias_decls[type_name]).is_a?(ModuleAliasEntry)
|
|
240
|
-
module_alias
|
|
233
|
+
def module_entry(type_name, normalized: false)
|
|
234
|
+
case entry = constant_entry(type_name, normalized: normalized || false)
|
|
235
|
+
when ModuleEntry, ModuleAliasEntry
|
|
236
|
+
entry
|
|
241
237
|
end
|
|
242
238
|
end
|
|
243
239
|
|
|
@@ -253,26 +249,40 @@ module RBS
|
|
|
253
249
|
end
|
|
254
250
|
|
|
255
251
|
def normalized_module_entry(type_name)
|
|
256
|
-
|
|
257
|
-
case entry = module_entry(name)
|
|
258
|
-
when ModuleEntry, nil
|
|
259
|
-
entry
|
|
260
|
-
when ModuleAliasEntry
|
|
261
|
-
raise
|
|
262
|
-
end
|
|
263
|
-
end
|
|
252
|
+
module_entry(type_name, normalized: true)
|
|
264
253
|
end
|
|
265
254
|
|
|
266
|
-
def module_class_entry(type_name)
|
|
267
|
-
|
|
255
|
+
def module_class_entry(type_name, normalized: false)
|
|
256
|
+
entry = constant_entry(type_name, normalized: normalized || false)
|
|
257
|
+
if entry.is_a?(ConstantEntry)
|
|
258
|
+
nil
|
|
259
|
+
else
|
|
260
|
+
entry
|
|
261
|
+
end
|
|
268
262
|
end
|
|
269
263
|
|
|
270
264
|
def normalized_module_class_entry(type_name)
|
|
271
|
-
|
|
265
|
+
module_class_entry(type_name, normalized: true)
|
|
272
266
|
end
|
|
273
267
|
|
|
274
|
-
def constant_entry(type_name)
|
|
275
|
-
|
|
268
|
+
def constant_entry(type_name, normalized: false)
|
|
269
|
+
if normalized
|
|
270
|
+
if normalized_name = normalize_module_name?(type_name)
|
|
271
|
+
class_decls.fetch(normalized_name, nil)
|
|
272
|
+
else
|
|
273
|
+
# The type_name may be declared with constant declaration
|
|
274
|
+
unless type_name.namespace.empty?
|
|
275
|
+
parent = type_name.namespace.to_type_name
|
|
276
|
+
normalized_parent = normalize_module_name?(parent) or return
|
|
277
|
+
constant_name = TypeName.new(name: type_name.name, namespace: normalized_parent.to_namespace)
|
|
278
|
+
constant_decls.fetch(constant_name, nil)
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
else
|
|
282
|
+
class_decls.fetch(type_name, nil) ||
|
|
283
|
+
class_alias_decls.fetch(type_name, nil) ||
|
|
284
|
+
constant_decls.fetch(type_name, nil)
|
|
285
|
+
end
|
|
276
286
|
end
|
|
277
287
|
|
|
278
288
|
def normalize_type_name?(name)
|
|
@@ -307,6 +317,10 @@ module RBS
|
|
|
307
317
|
end
|
|
308
318
|
end
|
|
309
319
|
|
|
320
|
+
def normalize_type_name(name)
|
|
321
|
+
normalize_type_name?(name) || name
|
|
322
|
+
end
|
|
323
|
+
|
|
310
324
|
def normalized_type_name?(type_name)
|
|
311
325
|
case
|
|
312
326
|
when type_name.interface?
|
|
@@ -321,53 +335,44 @@ module RBS
|
|
|
321
335
|
end
|
|
322
336
|
|
|
323
337
|
def normalized_type_name!(name)
|
|
324
|
-
normalized_type_name?(name) or raise "Normalized type name is expected but given `#{name}
|
|
338
|
+
normalized_type_name?(name) or raise "Normalized type name is expected but given `#{name}`"
|
|
325
339
|
name
|
|
326
340
|
end
|
|
327
341
|
|
|
328
|
-
def normalize_type_name(name)
|
|
329
|
-
normalize_type_name?(name) || name
|
|
330
|
-
end
|
|
331
|
-
|
|
332
|
-
def normalize_module_name(name)
|
|
333
|
-
normalize_module_name?(name) or name
|
|
334
|
-
end
|
|
335
|
-
|
|
336
342
|
def normalize_module_name?(name)
|
|
337
343
|
raise "Class/module name is expected: #{name}" unless name.class?
|
|
338
344
|
name = name.absolute! unless name.absolute?
|
|
339
345
|
|
|
340
|
-
|
|
341
|
-
|
|
346
|
+
original_name = name
|
|
347
|
+
|
|
348
|
+
if @normalize_module_name_cache.key?(original_name)
|
|
349
|
+
return @normalize_module_name_cache[original_name]
|
|
342
350
|
end
|
|
343
351
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
352
|
+
if alias_entry = class_alias_decls.fetch(name, nil)
|
|
353
|
+
unless alias_entry.decl.old_name.absolute?
|
|
354
|
+
# Having relative old_name means the type name resolution was failed.
|
|
355
|
+
# Run TypeNameResolver for failure reason
|
|
356
|
+
resolver = Resolver::TypeNameResolver.build(self)
|
|
357
|
+
name = resolver.resolve_namespace(name, context: nil)
|
|
358
|
+
@normalize_module_name_cache[original_name] = name
|
|
359
|
+
return name
|
|
351
360
|
end
|
|
352
|
-
else
|
|
353
|
-
type_name = name
|
|
354
|
-
end
|
|
355
361
|
|
|
356
|
-
|
|
362
|
+
name = alias_entry.decl.old_name
|
|
363
|
+
end
|
|
357
364
|
|
|
358
|
-
|
|
365
|
+
if class_decls.key?(name)
|
|
366
|
+
@normalize_module_name_cache[original_name] = name
|
|
367
|
+
end
|
|
368
|
+
end
|
|
359
369
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
type_name
|
|
364
|
-
when ClassAliasEntry, ModuleAliasEntry
|
|
365
|
-
normalize_module_name?(entry.decl.old_name)
|
|
366
|
-
else
|
|
367
|
-
nil
|
|
368
|
-
end
|
|
370
|
+
def normalize_module_name(name)
|
|
371
|
+
normalize_module_name?(name) || name
|
|
372
|
+
end
|
|
369
373
|
|
|
370
|
-
|
|
374
|
+
def normalize_module_name!(name)
|
|
375
|
+
normalize_module_name?(name) or raise "Module name `#{name}` cannot be normalized"
|
|
371
376
|
end
|
|
372
377
|
|
|
373
378
|
def insert_decl(decl, outer:, namespace:)
|
|
@@ -509,7 +514,7 @@ module RBS
|
|
|
509
514
|
end
|
|
510
515
|
|
|
511
516
|
def resolve_type_names(only: nil)
|
|
512
|
-
resolver = Resolver::TypeNameResolver.
|
|
517
|
+
resolver = Resolver::TypeNameResolver.build(self)
|
|
513
518
|
env = Environment.new
|
|
514
519
|
|
|
515
520
|
table = UseMap::Table.new()
|
data/lib/rbs/errors.rb
CHANGED
|
@@ -93,7 +93,7 @@ module RBS
|
|
|
93
93
|
params =
|
|
94
94
|
case
|
|
95
95
|
when type_name.class?
|
|
96
|
-
decl = env.
|
|
96
|
+
decl = env.module_class_entry(type_name, normalized: true) or raise
|
|
97
97
|
decl.type_params
|
|
98
98
|
when type_name.interface?
|
|
99
99
|
env.interface_decls.fetch(type_name).decl.type_params
|
data/lib/rbs/parser_aux.rb
CHANGED
|
@@ -35,6 +35,11 @@ module RBS
|
|
|
35
35
|
[buf, dirs, decls]
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
def self.parse_type_params(source, module_type_params: true)
|
|
39
|
+
buf = buffer(source)
|
|
40
|
+
_parse_type_params(buf, 0, buf.last_position, module_type_params)
|
|
41
|
+
end
|
|
42
|
+
|
|
38
43
|
def self.magic_comment(buf)
|
|
39
44
|
start_pos = 0
|
|
40
45
|
|
|
@@ -32,7 +32,7 @@ module RBS
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
environment.class_alias_decls.each do |name, entry|
|
|
35
|
-
normalized_entry = environment.
|
|
35
|
+
normalized_entry = environment.module_class_entry(name, normalized: true) or next
|
|
36
36
|
constant = constant_of_module(name, normalized_entry)
|
|
37
37
|
|
|
38
38
|
# Insert class/module aliases into `children_table` and `toplevel` table
|
|
@@ -176,7 +176,7 @@ module RBS
|
|
|
176
176
|
end
|
|
177
177
|
|
|
178
178
|
def constants_from_ancestors(module_name, constants:)
|
|
179
|
-
entry = builder.env.
|
|
179
|
+
entry = builder.env.module_class_entry(module_name, normalized: true) or raise
|
|
180
180
|
|
|
181
181
|
if entry.is_a?(Environment::ClassEntry) || entry.is_a?(Environment::ModuleEntry)
|
|
182
182
|
constants.merge!(table.children(BuiltinNames::Object.name) || raise)
|
|
@@ -5,17 +5,36 @@ module RBS
|
|
|
5
5
|
class TypeNameResolver
|
|
6
6
|
attr_reader :all_names
|
|
7
7
|
attr_reader :cache
|
|
8
|
-
attr_reader :
|
|
8
|
+
attr_reader :aliases
|
|
9
9
|
|
|
10
|
-
def initialize(
|
|
11
|
-
@all_names =
|
|
10
|
+
def initialize(all_names, aliases)
|
|
11
|
+
@all_names = all_names
|
|
12
|
+
@aliases = aliases
|
|
12
13
|
@cache = {}
|
|
13
|
-
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.new(*args)
|
|
17
|
+
if args.size == 1 && args[0].is_a?(Environment)
|
|
18
|
+
build(args[0])
|
|
19
|
+
else
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.build(env)
|
|
25
|
+
all_names = Set.new #: Set[TypeName]
|
|
14
26
|
|
|
15
27
|
all_names.merge(env.class_decls.keys)
|
|
16
28
|
all_names.merge(env.interface_decls.keys)
|
|
17
29
|
all_names.merge(env.type_alias_decls.keys)
|
|
18
|
-
|
|
30
|
+
|
|
31
|
+
aliases = {} #: Hash[TypeName, [TypeName, context]]
|
|
32
|
+
|
|
33
|
+
env.class_alias_decls.each do |name, entry|
|
|
34
|
+
aliases[name] = [entry.decl.old_name, entry.context]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
new(all_names, aliases)
|
|
19
38
|
end
|
|
20
39
|
|
|
21
40
|
def try_cache(query)
|
|
@@ -26,64 +45,131 @@ module RBS
|
|
|
26
45
|
end
|
|
27
46
|
|
|
28
47
|
def resolve(type_name, context:)
|
|
29
|
-
if type_name.absolute?
|
|
48
|
+
if type_name.absolute? && has_type_name?(type_name)
|
|
30
49
|
return type_name
|
|
31
50
|
end
|
|
32
51
|
|
|
33
52
|
try_cache([type_name, context]) do
|
|
34
|
-
|
|
53
|
+
if type_name.class?
|
|
54
|
+
resolve_namespace0(type_name, context, Set.new) || nil
|
|
55
|
+
else
|
|
56
|
+
namespace = type_name.namespace
|
|
35
57
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if head
|
|
39
|
-
if tail
|
|
40
|
-
absolute_name = tail.with_prefix(head.to_namespace)
|
|
41
|
-
if env.normalize_type_name?(absolute_name)
|
|
42
|
-
absolute_name
|
|
43
|
-
end
|
|
58
|
+
if namespace.empty?
|
|
59
|
+
resolve_type_name(type_name.name, context)
|
|
44
60
|
else
|
|
45
|
-
|
|
61
|
+
if namespace = resolve_namespace0(namespace.to_type_name, context, Set.new)
|
|
62
|
+
type_name = TypeName.new(name: type_name.name, namespace: namespace.to_namespace)
|
|
63
|
+
has_type_name?(type_name)
|
|
64
|
+
end
|
|
46
65
|
end
|
|
47
66
|
end
|
|
48
67
|
end
|
|
49
68
|
end
|
|
50
69
|
|
|
51
|
-
def
|
|
52
|
-
if type_name.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
else
|
|
56
|
-
head, *tail = type_name.namespace.path
|
|
70
|
+
def resolve_namespace(type_name, context:)
|
|
71
|
+
if type_name.absolute? && has_type_name?(type_name)
|
|
72
|
+
return type_name
|
|
73
|
+
end
|
|
57
74
|
|
|
58
|
-
|
|
75
|
+
unless type_name.class?
|
|
76
|
+
raise "Type name must be a class name: #{type_name}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
try_cache([type_name, context]) do
|
|
80
|
+
ns = resolve_namespace0(type_name, context, Set.new) or return ns
|
|
81
|
+
end
|
|
82
|
+
end
|
|
59
83
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
)
|
|
84
|
+
def has_type_name?(full_name)
|
|
85
|
+
if all_names.include?(full_name)
|
|
86
|
+
full_name
|
|
64
87
|
end
|
|
88
|
+
end
|
|
65
89
|
|
|
66
|
-
|
|
90
|
+
def aliased_name?(type_name)
|
|
91
|
+
if aliases.key?(type_name)
|
|
92
|
+
type_name
|
|
93
|
+
end
|
|
67
94
|
end
|
|
68
95
|
|
|
69
|
-
def
|
|
96
|
+
def resolve_type_name(type_name, context)
|
|
70
97
|
if context
|
|
71
|
-
|
|
72
|
-
case
|
|
98
|
+
outer, inner = context
|
|
99
|
+
case inner
|
|
73
100
|
when false
|
|
74
|
-
|
|
101
|
+
resolve_type_name(type_name, outer)
|
|
102
|
+
else
|
|
103
|
+
has_type_name?(inner) or raise "Context must be normalized: #{inner.inspect}"
|
|
104
|
+
has_type_name?(TypeName.new(name: type_name, namespace: inner.to_namespace)) || resolve_type_name(type_name, outer)
|
|
105
|
+
end
|
|
106
|
+
else
|
|
107
|
+
type_name = TypeName.new(name: type_name, namespace: Namespace.root)
|
|
108
|
+
has_type_name?(type_name)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def resolve_head_namespace(head, context)
|
|
113
|
+
if context
|
|
114
|
+
outer, inner = context
|
|
115
|
+
case inner
|
|
116
|
+
when false
|
|
117
|
+
resolve_head_namespace(head, outer)
|
|
75
118
|
when TypeName
|
|
76
|
-
|
|
77
|
-
|
|
119
|
+
has_type_name?(inner) or raise "Context must be normalized: #{inner.inspect}"
|
|
120
|
+
type_name = TypeName.new(name: head, namespace: inner.to_namespace)
|
|
121
|
+
has_type_name?(type_name) || aliased_name?(type_name) || resolve_head_namespace(head, outer)
|
|
78
122
|
end
|
|
79
123
|
else
|
|
80
|
-
|
|
124
|
+
type_name = TypeName.new(name: head, namespace: Namespace.root)
|
|
125
|
+
has_type_name?(type_name) || aliased_name?(type_name)
|
|
81
126
|
end
|
|
82
127
|
end
|
|
83
128
|
|
|
84
|
-
def
|
|
85
|
-
if
|
|
86
|
-
|
|
129
|
+
def normalize_namespace(type_name, rhs, context, visited)
|
|
130
|
+
if visited.include?(type_name)
|
|
131
|
+
# Cycle detected
|
|
132
|
+
return false
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
visited << type_name
|
|
136
|
+
|
|
137
|
+
begin
|
|
138
|
+
resolve_namespace0(rhs, context, visited)
|
|
139
|
+
ensure
|
|
140
|
+
visited.delete(type_name)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def resolve_namespace0(type_name, context, visited)
|
|
145
|
+
head, *tail = [*type_name.namespace.path, type_name.name]
|
|
146
|
+
|
|
147
|
+
head = head #: Symbol
|
|
148
|
+
|
|
149
|
+
head =
|
|
150
|
+
if type_name.absolute?
|
|
151
|
+
root_name = TypeName.new(name: head, namespace: Namespace.root)
|
|
152
|
+
has_type_name?(root_name) || aliased_name?(root_name)
|
|
153
|
+
else
|
|
154
|
+
resolve_head_namespace(head, context)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
if head
|
|
158
|
+
if (rhs, context = aliases.fetch(head, nil))
|
|
159
|
+
head = normalize_namespace(head, rhs, context, visited) or return head
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
tail.inject(head) do |namespace, name|
|
|
163
|
+
type_name = TypeName.new(name: name, namespace: namespace.to_namespace)
|
|
164
|
+
case
|
|
165
|
+
when has_type_name?(type_name)
|
|
166
|
+
type_name
|
|
167
|
+
when (rhs, context = aliases.fetch(type_name, nil))
|
|
168
|
+
m = normalize_namespace(type_name, rhs, context, visited) or return m
|
|
169
|
+
else
|
|
170
|
+
return nil
|
|
171
|
+
end
|
|
172
|
+
end
|
|
87
173
|
end
|
|
88
174
|
end
|
|
89
175
|
end
|
data/lib/rbs/test/type_check.rb
CHANGED
|
@@ -288,6 +288,19 @@ module RBS
|
|
|
288
288
|
values << args
|
|
289
289
|
nil
|
|
290
290
|
end
|
|
291
|
+
when nil
|
|
292
|
+
values = []
|
|
293
|
+
count = 10_000
|
|
294
|
+
|
|
295
|
+
ret = val.each do |*args|
|
|
296
|
+
count -= 1
|
|
297
|
+
values << args
|
|
298
|
+
break if count <= 0
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
if count == 0
|
|
302
|
+
ret = self
|
|
303
|
+
end
|
|
291
304
|
else
|
|
292
305
|
values = []
|
|
293
306
|
ret = val.each do |*args|
|
data/lib/rbs/types.rb
CHANGED
|
@@ -1339,8 +1339,10 @@ module RBS
|
|
|
1339
1339
|
attr_reader :type
|
|
1340
1340
|
attr_reader :required
|
|
1341
1341
|
attr_reader :self_type
|
|
1342
|
+
attr_reader :location
|
|
1342
1343
|
|
|
1343
|
-
def initialize(type:, required:, self_type: nil)
|
|
1344
|
+
def initialize(location: nil, type:, required:, self_type: nil)
|
|
1345
|
+
@location = location
|
|
1344
1346
|
@type = type
|
|
1345
1347
|
@required = required ? true : false
|
|
1346
1348
|
@self_type = self_type
|
data/lib/rbs/version.rb
CHANGED
data/lib/rbs.rb
CHANGED
data/lib/rdoc/discover.rb
CHANGED
data/lib/rdoc_plugin/parser.rb
CHANGED
|
@@ -76,7 +76,7 @@ module RBS
|
|
|
76
76
|
method.visibility = decl.visibility
|
|
77
77
|
method.call_seq = decl.overloads.map {|overload| "#{decl.name.to_s}#{overload.method_type.to_s}" }.join("\n")
|
|
78
78
|
if loc = decl.location
|
|
79
|
-
method.start_collecting_tokens
|
|
79
|
+
method.start_collecting_tokens(:ruby)
|
|
80
80
|
method.add_token({ line_no: 1, char_no: 1, kind: :on_comment, text: "# File #{@top_level.relative_name}, line(s) #{loc.start_line}:#{loc.end_line}\n" })
|
|
81
81
|
method.add_token({ line_no: 1, char_no: 1, text: loc.source })
|
|
82
82
|
method.line = loc.start_line
|
|
@@ -86,7 +86,7 @@ module RBS
|
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
def parse_method_alias_decl(decl:, context:, outer_name: nil)
|
|
89
|
-
alias_def = RDoc::Alias.new(nil, decl.old_name.to_s, decl.new_name.to_s, nil, decl.kind == :singleton)
|
|
89
|
+
alias_def = RDoc::Alias.new(nil, decl.old_name.to_s, decl.new_name.to_s, nil, singleton: decl.kind == :singleton)
|
|
90
90
|
alias_def.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
|
|
91
91
|
context.add_alias(alias_def)
|
|
92
92
|
end
|
|
@@ -100,7 +100,7 @@ module RBS
|
|
|
100
100
|
when ::RBS::AST::Members::AttrAccessor
|
|
101
101
|
'RW'
|
|
102
102
|
end
|
|
103
|
-
attribute = RDoc::Attr.new(nil, decl.name.to_s, rw, nil, decl.kind == :singleton)
|
|
103
|
+
attribute = RDoc::Attr.new(nil, decl.name.to_s, rw, nil, singleton: decl.kind == :singleton)
|
|
104
104
|
attribute.visibility = decl.visibility
|
|
105
105
|
attribute.comment = construct_comment(context: context, comment: comment_string(decl)) if decl.comment
|
|
106
106
|
context.add_attribute(attribute)
|
data/sig/annotate/formatter.rbs
CHANGED
|
@@ -17,8 +17,8 @@ module RBS
|
|
|
17
17
|
|
|
18
18
|
def self.translate: (RDoc::Markup::Document) -> String?
|
|
19
19
|
|
|
20
|
-
def self.each_part: (RDoc::Markup::Document
|
|
21
|
-
| (RDoc::Markup::Document
|
|
20
|
+
def self.each_part: (RDoc::Markup::Document) { (RDoc::Markup::Document) -> void } -> void
|
|
21
|
+
| (RDoc::Markup::Document) -> Enumerator[RDoc::Markup::Document, void]
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
end
|
|
@@ -17,7 +17,7 @@ module RBS
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
interface _WithRDocComment
|
|
20
|
-
def comment: () -> (RDoc::
|
|
20
|
+
def comment: () -> (RDoc::Comment | String)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def each_part: (Array[Object & _WithRDocComment], tester: _PathTester) { ([RDoc::Markup::Document, Object & _WithRDocComment]) -> void } -> void
|
data/sig/environment.rbs
CHANGED
|
@@ -190,22 +190,58 @@ module RBS
|
|
|
190
190
|
# Returns true if the type name is a class alias
|
|
191
191
|
def class_alias?: (TypeName) -> bool
|
|
192
192
|
|
|
193
|
-
|
|
193
|
+
# Returns the entry for the class name
|
|
194
|
+
#
|
|
195
|
+
# Returns `nil` when:
|
|
196
|
+
#
|
|
197
|
+
# * The type name is not found
|
|
198
|
+
# * The type name is not a class
|
|
199
|
+
#
|
|
200
|
+
# It normalizes when `normalized: true` is given -- returns `ClassEntry` or `nil`.
|
|
201
|
+
#
|
|
202
|
+
def class_entry: (TypeName, normalized: true) -> ClassEntry?
|
|
203
|
+
| (TypeName, ?normalized: bool) -> (ClassEntry | ClassAliasEntry | nil)
|
|
194
204
|
|
|
195
205
|
# Returns ClassEntry if the class definition is found, normalized in case of alias
|
|
196
206
|
#
|
|
207
|
+
%a{deprecated: Use `class_entry(type_name, normalized: true)` instead. }
|
|
197
208
|
def normalized_class_entry: (TypeName) -> ClassEntry?
|
|
198
209
|
|
|
199
|
-
|
|
210
|
+
# Returns the entry for the module name
|
|
211
|
+
#
|
|
212
|
+
# Returns `nil` when:
|
|
213
|
+
#
|
|
214
|
+
# * The type name is not found
|
|
215
|
+
# * The type name is not a module
|
|
216
|
+
#
|
|
217
|
+
# It normalizes when `normalized: true` is given -- returns `ModuleEntry` or `nil`.
|
|
218
|
+
#
|
|
219
|
+
def module_entry: (TypeName, normalized: true) -> ModuleEntry?
|
|
220
|
+
| (TypeName, ?normalized: bool) -> (ModuleEntry | ModuleAliasEntry | nil)
|
|
200
221
|
|
|
201
222
|
# Returns ModuleEntry if the module definition is found, normalized in case of alias
|
|
202
223
|
#
|
|
224
|
+
%a{deprecated: Use `module_entry(type_name, normalized: true)` instead. }
|
|
203
225
|
def normalized_module_entry: (TypeName) -> ModuleEntry?
|
|
204
226
|
|
|
205
|
-
|
|
227
|
+
# Returns the entry for the type name
|
|
228
|
+
#
|
|
229
|
+
# The entry is one of ClassEntry, ModuleEntry, ConstantEntry, ClassAliasEntry, or ModuleAliasEntry.
|
|
230
|
+
# When `normalized: true` is given, it returns normalized entry -- one of ClassEntry, ModuleEntry, or ConstantEntry.
|
|
231
|
+
#
|
|
232
|
+
# Returns `nil` if not found.
|
|
233
|
+
#
|
|
234
|
+
def constant_entry: (TypeName, normalized: true) -> (ClassEntry | ModuleEntry | ConstantEntry | nil)
|
|
235
|
+
| (TypeName, ?normalized: bool) -> (ClassEntry | ClassAliasEntry | ModuleEntry | ModuleAliasEntry | ConstantEntry | nil)
|
|
206
236
|
|
|
207
|
-
|
|
237
|
+
# Returns ClassEntry or ModuleEntry if the class/module definition is found
|
|
238
|
+
#
|
|
239
|
+
# Normalizes when `normalized: true` is given.
|
|
240
|
+
#
|
|
241
|
+
def module_class_entry: (TypeName, normalized: true) -> (ClassEntry | ModuleEntry | nil)
|
|
242
|
+
| (TypeName, ?normalized: bool) -> (ClassEntry | ClassAliasEntry | ModuleEntry | ModuleAliasEntry | nil)
|
|
208
243
|
|
|
244
|
+
%a{deprecated: Use `module_class_entry(type_name, normalized: true)` instead. }
|
|
209
245
|
def normalized_module_class_entry: (TypeName) -> (ClassEntry | ModuleEntry | nil)
|
|
210
246
|
|
|
211
247
|
@normalize_module_name_cache: Hash[TypeName, TypeName | false | nil]
|
|
@@ -216,26 +252,39 @@ module RBS
|
|
|
216
252
|
# * Returns `nil` if the rhs name cannot be found
|
|
217
253
|
# * Returns `false` if the name is cyclic
|
|
218
254
|
#
|
|
255
|
+
# Note that this method assumes the declarations have resolved type names.
|
|
256
|
+
#
|
|
219
257
|
def normalize_module_name?: (TypeName) -> (TypeName | nil | false)
|
|
220
258
|
|
|
221
|
-
# Returns the original module name that is defined with `module` declaration
|
|
259
|
+
# Returns the original module name that is defined with `module`/`class` declaration
|
|
222
260
|
#
|
|
223
|
-
# * Raises an error if given type name is not module
|
|
261
|
+
# * Raises an error if given type name is not module/class
|
|
224
262
|
# * Calls `#absolute!` for relative module names
|
|
225
263
|
# * Returns the name itself if the name cannot be normalized to a class/module
|
|
226
264
|
#
|
|
265
|
+
# Note that this method assumes the declarations have resolved type names.
|
|
266
|
+
#
|
|
227
267
|
def normalize_module_name: (TypeName) -> TypeName
|
|
228
268
|
|
|
269
|
+
# Returns the original module name that is defined with `module`/`class` declaration
|
|
270
|
+
#
|
|
271
|
+
# * Raises an error if given type name is not a module/class
|
|
272
|
+
def normalize_module_name!: (TypeName) -> TypeName
|
|
273
|
+
|
|
229
274
|
# Returns a normalized module/class name or a type name with a normalized namespace
|
|
230
275
|
#
|
|
231
276
|
# * Calls `#absolute!` for relative module names
|
|
232
277
|
# * Returns `nil` if the typename cannot be found
|
|
233
278
|
# * Returns `false` if the name is cyclic
|
|
234
279
|
#
|
|
280
|
+
# Note that this method assumes the declarations have resolved type names.
|
|
281
|
+
#
|
|
235
282
|
def normalize_type_name?: (TypeName) -> (TypeName | nil | false)
|
|
236
283
|
|
|
237
284
|
# Normalize the type name or raises an error
|
|
238
285
|
#
|
|
286
|
+
# Note that this method assumes the declarations have resolved type names.
|
|
287
|
+
#
|
|
239
288
|
def normalize_type_name!: (TypeName) -> TypeName
|
|
240
289
|
|
|
241
290
|
# Returns a normalized module/class name or a type name with a normalized namespace
|
|
@@ -243,6 +292,8 @@ module RBS
|
|
|
243
292
|
# * Calls `#absolute!` for relative module names
|
|
244
293
|
# * Returns the typename itself if the name cannot be normalized
|
|
245
294
|
#
|
|
295
|
+
# Note that this method assumes the declarations have resolved type names.
|
|
296
|
+
#
|
|
246
297
|
def normalize_type_name: (TypeName) -> TypeName
|
|
247
298
|
|
|
248
299
|
# Returns `true` if given type name is normalized
|