rigortype 0.0.5 → 0.0.7

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.
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "prism"
4
4
 
5
+ require_relative "../reflection"
5
6
  require_relative "../source/node_walker"
6
7
  require_relative "../type"
7
8
  require_relative "diagnostic"
@@ -172,9 +173,7 @@ module Rigor
172
173
  kind = receiver_type.is_a?(Type::Singleton) ? :singleton : :instance
173
174
  return nil if scope.discovered_method?(class_name, call_node.name, kind)
174
175
 
175
- loader = scope.environment.rbs_loader
176
- return nil if loader.nil?
177
- return nil unless loader.class_known?(class_name)
176
+ return nil unless Rigor::Reflection.rbs_class_known?(class_name, scope: scope)
178
177
 
179
178
  # When the loader cannot build a class definition for a
180
179
  # name it nominally knows (constant-decl aliases such
@@ -182,9 +181,9 @@ module Rigor
182
181
  # malformed signatures), we cannot enumerate methods
183
182
  # so we MUST NOT emit a false positive. Skip the rule
184
183
  # in that case.
185
- return nil unless definition_available?(loader, receiver_type, class_name)
184
+ return nil unless definition_available?(receiver_type, class_name, scope)
186
185
 
187
- method_def = lookup_method(loader, receiver_type, class_name, call_node.name)
186
+ method_def = lookup_method(receiver_type, class_name, call_node.name, scope)
188
187
  return nil if method_def
189
188
 
190
189
  build_undefined_method_diagnostic(path, call_node, receiver_type)
@@ -219,27 +218,29 @@ module Rigor
219
218
  nil
220
219
  end
221
220
 
222
- def definition_available?(loader, receiver_type, class_name)
221
+ def definition_available?(receiver_type, class_name, scope)
223
222
  if receiver_type.is_a?(Type::Singleton)
224
- !loader.singleton_definition(class_name).nil?
223
+ !Rigor::Reflection.singleton_definition(class_name, scope: scope).nil?
225
224
  else
226
- !loader.instance_definition(class_name).nil?
225
+ !Rigor::Reflection.instance_definition(class_name, scope: scope).nil?
227
226
  end
228
- rescue StandardError
229
- false
230
227
  end
231
228
 
232
- def lookup_method(loader, receiver_type, class_name, method_name)
229
+ def lookup_method(receiver_type, class_name, method_name, scope)
233
230
  if receiver_type.is_a?(Type::Singleton)
234
- loader.singleton_method(class_name: class_name, method_name: method_name)
231
+ Rigor::Reflection.singleton_method_definition(class_name, method_name, scope: scope)
235
232
  else
236
- loader.instance_method(class_name: class_name, method_name: method_name)
233
+ Rigor::Reflection.instance_method_definition(class_name, method_name, scope: scope)
237
234
  end
238
235
  rescue StandardError
239
- # The loader is best-effort and may raise on malformed
240
- # RBS. Treat any failure as "method exists" so we do
241
- # NOT emit a false positive when our knowledge of the
242
- # receiver class is structurally incomplete.
236
+ # The Reflection facade catches loader exceptions and
237
+ # returns nil. The wrapper here treats failures as
238
+ # "method exists" so we do NOT emit a false positive
239
+ # when our knowledge of the receiver class is
240
+ # structurally incomplete (Reflection's own rescue
241
+ # already returns nil; this catch is a defensive
242
+ # double-net for any future call shape that might
243
+ # raise).
243
244
  true
244
245
  end
245
246
 
@@ -271,12 +272,10 @@ module Rigor
271
272
  kind = receiver_type.is_a?(Type::Singleton) ? :singleton : :instance
272
273
  return nil if scope.discovered_method?(class_name, call_node.name, kind)
273
274
 
274
- loader = scope.environment.rbs_loader
275
- return nil if loader.nil?
276
- return nil unless loader.class_known?(class_name)
277
- return nil unless definition_available?(loader, receiver_type, class_name)
275
+ return nil unless Rigor::Reflection.rbs_class_known?(class_name, scope: scope)
276
+ return nil unless definition_available?(receiver_type, class_name, scope)
278
277
 
279
- method_def = lookup_method(loader, receiver_type, class_name, call_node.name)
278
+ method_def = lookup_method(receiver_type, class_name, call_node.name, scope)
280
279
  return nil if method_def.nil? || method_def == true
281
280
 
282
281
  arity_envelope = compute_arity_envelope(method_def)
@@ -383,12 +382,14 @@ module Rigor
383
382
  receiver_type = scope.type_of(call_node.receiver)
384
383
  return nil unless receiver_type.is_a?(Type::Union)
385
384
 
386
- loader = scope.environment.rbs_loader
387
- return nil if loader.nil?
385
+ # The rule only fires when the analyzer has access to
386
+ # an RBS loader; without it, the per-member method-
387
+ # presence checks below cannot rule out a sound call.
388
+ return nil unless Rigor::Reflection.rbs_class_known?("NilClass", scope: scope)
388
389
 
389
390
  return nil unless union_contains_nil?(receiver_type)
390
- return nil unless union_method_present_on_non_nil?(receiver_type, call_node.name, loader, scope)
391
- return nil if nil_class_has_method?(call_node.name, loader)
391
+ return nil unless union_method_present_on_non_nil?(receiver_type, call_node.name, scope)
392
+ return nil if nil_class_has_method?(call_node.name, scope)
392
393
 
393
394
  build_nil_receiver_diagnostic(path, call_node)
394
395
  end
@@ -409,27 +410,25 @@ module Rigor
409
410
  # that are unsound on the non-nil branch — that is the
410
411
  # `undefined_method_diagnostic` rule's job, and we want
411
412
  # exactly one diagnostic per offending call site.
412
- def union_method_present_on_non_nil?(union, method_name, loader, scope)
413
+ def union_method_present_on_non_nil?(union, method_name, scope)
413
414
  non_nil_members = union.members.reject { |m| nil_member?(m) }
414
415
  return false if non_nil_members.empty?
415
416
 
416
- non_nil_members.all? { |m| method_present_anywhere?(m, method_name, loader, scope) }
417
+ non_nil_members.all? { |m| method_present_anywhere?(m, method_name, scope) }
417
418
  end
418
419
 
419
- def method_present_anywhere?(member, method_name, loader, scope)
420
+ def method_present_anywhere?(member, method_name, scope)
420
421
  class_name = concrete_class_name(member)
421
422
  return true if class_name.nil? # Dynamic / Top / Bot — be permissive.
422
423
  return true if scope.discovered_method?(class_name, method_name, :instance)
423
- return true unless loader.class_known?(class_name)
424
- return true unless definition_available?(loader, member, class_name)
424
+ return true unless Rigor::Reflection.rbs_class_known?(class_name, scope: scope)
425
+ return true unless definition_available?(member, class_name, scope)
425
426
 
426
- !lookup_method(loader, member, class_name, method_name).nil?
427
+ !lookup_method(member, class_name, method_name, scope).nil?
427
428
  end
428
429
 
429
- def nil_class_has_method?(method_name, loader)
430
- return false unless loader.class_known?("NilClass")
431
-
432
- definition = loader.instance_definition("NilClass")
430
+ def nil_class_has_method?(method_name, scope)
431
+ definition = Rigor::Reflection.instance_definition("NilClass", scope: scope)
433
432
  return false if definition.nil?
434
433
 
435
434
  !definition.methods[method_name.to_sym].nil?
@@ -684,12 +683,10 @@ module Rigor
684
683
  # supplies BOTH a `def` and an RBS sig, the sig is
685
684
  # the authoritative parameter contract and we
686
685
  # should validate calls against it.
687
- loader = scope.environment.rbs_loader
688
- return nil if loader.nil?
689
- return nil unless loader.class_known?(class_name)
690
- return nil unless definition_available?(loader, receiver_type, class_name)
686
+ return nil unless Rigor::Reflection.rbs_class_known?(class_name, scope: scope)
687
+ return nil unless definition_available?(receiver_type, class_name, scope)
691
688
 
692
- method_def = lookup_method(loader, receiver_type, class_name, call_node.name)
689
+ method_def = lookup_method(receiver_type, class_name, call_node.name, scope)
693
690
  return nil if method_def.nil? || method_def == true
694
691
  return nil unless method_def.method_types.size == 1
695
692
 
@@ -78,6 +78,41 @@ module Rigor
78
78
  return nil unless args.size == 2
79
79
 
80
80
  Type::Combinator.non_empty_hash(args[0], args[1])
81
+ },
82
+ # v0.0.7 — `key_of[T]` and `value_of[T]` type functions.
83
+ # Each takes a single type argument and projects the
84
+ # known-keys (resp. known-values) union out of `T`. See
85
+ # `Type::Combinator.key_of` for the per-shape projection
86
+ # rules. Use `lower_snake` per the
87
+ # imported-built-in-types.md type-function naming rule.
88
+ "key_of" => lambda { |args|
89
+ return nil unless args.size == 1
90
+
91
+ Type::Combinator.key_of(args.first)
92
+ },
93
+ "value_of" => lambda { |args|
94
+ return nil unless args.size == 1
95
+
96
+ Type::Combinator.value_of(args.first)
97
+ },
98
+ # `int_mask[1, 2, 4]` — every integer representable by
99
+ # a bitwise OR over the listed flags. Each arg must be a
100
+ # `Constant<Integer>`; the parser wraps integer literals
101
+ # for this purpose. Builder declines on any non-integer
102
+ # arg.
103
+ "int_mask" => lambda { |args|
104
+ flags = args.map { |arg| arg.is_a?(Type::Constant) && arg.value.is_a?(Integer) ? arg.value : nil }
105
+ return nil if flags.any?(&:nil?)
106
+
107
+ Type::Combinator.int_mask(flags)
108
+ },
109
+ # `int_mask_of[T]` — derives the closure from a finite
110
+ # integer literal type (single Constant<Integer> or a
111
+ # Union of them).
112
+ "int_mask_of" => lambda { |args|
113
+ return nil unless args.size == 1
114
+
115
+ Type::Combinator.int_mask_of(args.first)
81
116
  }
82
117
  }.freeze
83
118
  private_constant :PARAMETERISED_TYPE_BUILDERS
@@ -145,7 +180,7 @@ module Rigor
145
180
  # soft (returns `nil` from `parse`) on any deviation so the
146
181
  # `RBS::Extended` directive site can fall back to the
147
182
  # RBS-declared type rather than crash on a typo.
148
- class Parser
183
+ class Parser # rubocop:disable Metrics/ClassLength
149
184
  def initialize(input)
150
185
  @scanner = StringScanner.new(input.strip)
151
186
  end
@@ -153,6 +188,12 @@ module Rigor
153
188
  def parse
154
189
  type = parse_type
155
190
  return nil if type.nil?
191
+
192
+ # v0.0.7 — trailing `[K]` indexed-access projects
193
+ # into the parsed type. Multiple `[K]` segments
194
+ # chain (`Tuple[A, B, C][1][0]`).
195
+ type = parse_indexed_access_chain(type)
196
+ return nil if type.nil?
156
197
  return nil unless @scanner.eos?
157
198
 
158
199
  type
@@ -160,12 +201,21 @@ module Rigor
160
201
 
161
202
  private
162
203
 
163
- SIMPLE_NAME = /[a-z][a-z0-9-]*/
204
+ # Refinement names use kebab-case (`non-empty-string`),
205
+ # type-function names use lower_snake (`key_of`,
206
+ # `value_of`, `int_mask`). The regex accepts both shapes;
207
+ # the registry lookup decides which family the name
208
+ # belongs to.
209
+ SIMPLE_NAME = /[a-z][a-z0-9_-]*/
164
210
  CLASS_NAME = /[A-Z][A-Za-z0-9_]*(?:::[A-Z][A-Za-z0-9_]*)*/
165
211
  SIGNED_INT = /-?\d+/
166
212
  private_constant :SIMPLE_NAME, :CLASS_NAME, :SIGNED_INT
167
213
 
168
214
  def parse_type
215
+ if (class_name = @scanner.scan(CLASS_NAME))
216
+ return parse_class_arg_tail(class_name)
217
+ end
218
+
169
219
  name = @scanner.scan(SIMPLE_NAME)
170
220
  return nil if name.nil?
171
221
 
@@ -176,6 +226,25 @@ module Rigor
176
226
  end
177
227
  end
178
228
 
229
+ # `T[K]` — keep applying `[K]` indexes until no more
230
+ # opening brackets are present. Each index consumes one
231
+ # type argument; multi-arg `[K1, K2]` fails (the spec
232
+ # specifies a single key).
233
+ def parse_indexed_access_chain(type)
234
+ loop do
235
+ skip_ws
236
+ break unless @scanner.peek(1) == "["
237
+
238
+ @scanner.getch
239
+ args = parse_type_arg_list
240
+ return nil if args.nil? || args.size != 1
241
+ return nil unless @scanner.getch == "]"
242
+
243
+ type = Type::Combinator.indexed_access(type, args.first)
244
+ end
245
+ type
246
+ end
247
+
179
248
  def parse_parametric_type_args(name)
180
249
  builder = PARAMETERISED_TYPE_BUILDERS[name]
181
250
  return nil if builder.nil?
@@ -227,12 +296,33 @@ module Rigor
227
296
  def parse_type_arg
228
297
  skip_ws
229
298
  if (class_name = @scanner.scan(CLASS_NAME))
230
- Type::Combinator.nominal_of(class_name)
299
+ parse_class_arg_tail(class_name)
300
+ elsif (literal = @scanner.scan(SIGNED_INT))
301
+ # Integer-literal arg, used by `int_mask[1, 2, 4]`.
302
+ # Wrapped as `Constant<Integer>` so type-arg builders
303
+ # see a uniform `Array<Type::t>`.
304
+ Type::Combinator.constant_of(Integer(literal))
231
305
  else
232
306
  parse_type
233
307
  end
234
308
  end
235
309
 
310
+ # Class-name-headed type argument with optional `[T_1,
311
+ # …]` type-args tail. Used so `key_of[Hash[Symbol,
312
+ # Integer]]` parses as the projection of a parameterised
313
+ # nominal carrier rather than rejecting the inner
314
+ # brackets.
315
+ def parse_class_arg_tail(class_name)
316
+ return Type::Combinator.nominal_of(class_name) unless @scanner.peek(1) == "["
317
+
318
+ @scanner.getch # consume '['
319
+ args = parse_type_arg_list
320
+ return nil if args.nil?
321
+ return nil unless @scanner.getch == "]"
322
+
323
+ Type::Combinator.nominal_of(class_name, type_args: args)
324
+ end
325
+
236
326
  def parse_int_bound
237
327
  skip_ws
238
328
  literal = @scanner.scan(SIGNED_INT)
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "method_catalog"
4
+
5
+ module Rigor
6
+ module Inference
7
+ module Builtins
8
+ # `Pathname` catalog. Singleton — load once, consult during
9
+ # dispatch.
10
+ #
11
+ # TODO(blocklist curation): read
12
+ # `data/builtins/ruby_core/pathname.yml` and add per-method
13
+ # blocklist entries for any `:leaf` classifications that are
14
+ # actually mutators or otherwise unsafe to fold. Each entry
15
+ # SHOULD carry a one-line comment naming the indirect mutator
16
+ # helper that triggered the false positive (see
17
+ # `string_catalog.rb`, `array_catalog.rb`, `time_catalog.rb`
18
+ # for the canonical shape).
19
+ PATHNAME_CATALOG = MethodCatalog.new(
20
+ path: File.expand_path(
21
+ "../../../../data/builtins/ruby_core/pathname.yml",
22
+ __dir__
23
+ ),
24
+ mutating_selectors: {
25
+ "Pathname" => Set[
26
+ # initialize_copy is blocklisted by convention so a
27
+ # hypothetical future `Constant<Pathname>` carrier
28
+ # cannot fold an aliasing copy through the catalog.
29
+ :initialize_copy
30
+ ]
31
+ }
32
+ )
33
+ end
34
+ end
35
+ end