Module: Rigor::RbsExtended

Defined in:
lib/rigor/rbs_extended.rb,
lib/rigor/rbs_extended/reporter.rb,
lib/rigor/rbs_extended/hkt_directives.rb

Overview

Slice 7 phase 15 — first-preview reader for the ‘RBS::Extended` annotation surface described in `docs/type-specification/rbs-extended.md`.

This module reads ‘%a<payload>` annotations off RBS method definitions and returns well-typed effect objects the inference engine can consume. v0.0.2 recognises:

  • ‘rigor:v1:predicate-if-true <target> is <ClassName>`

  • ‘rigor:v1:predicate-if-false <target> is <ClassName>`

  • ‘rigor:v1:assert <target> is <ClassName>`

  • ‘rigor:v1:assert-if-true <target> is <ClassName>`

  • ‘rigor:v1:assert-if-false <target> is <ClassName>`

‘predicate-if-*` fires when the call is used as an `if` / `unless` condition; `assert` fires unconditionally at the call’s post-scope; ‘assert-if-true` / `assert-if-false` fire at the post-scope only when the call’s return value can be observed as truthy / falsey (currently: when the call is the predicate of a subsequent ‘if` / `unless`). Other directives in the spec (`param`, `return`, `conforms-to`, negation `~T`, `target: self` narrowing, …) remain on the v0.0.x roadmap. Annotations whose key is in the `rigor:v1:` namespace but whose directive is unrecognised are silently ignored at first-preview quality (a future slice MAY surface them as diagnostics-on-Rigor-itself per the spec’s “unsupported metadata” guidance).

The parser is minimal: it accepts a strict shape ‘<target> is <ClassName>` where `<target>` is a Ruby identifier (parameter name) or `self`, and `<ClassName>` is a single non-namespaced class identifier or a `::Foo::Bar` style constant path. Negative refinements (`~T`), intersections, and unions are deferred to the next iteration.

Defined Under Namespace

Modules: HktDirectives Classes: AssertEffect, ParamOverride, PredicateEffect, Reporter

Constant Summary collapse

DIRECTIVE_PREFIX =

rubocop:disable Metrics/ModuleLength

"rigor:v1:"
RBS_EXTENDED_PROVENANCE =

The shared FlowContribution::Provenance for every bundle this module produces. ‘source_family: :rbs_extended` so consumers (today the documentation surface; v0.1.0 the plugin contribution merger) can attribute facts back to the RBS::Extended layer.

FlowContribution::Provenance.new(
  source_family: :rbs_extended,
  plugin_id: nil,
  node: nil,
  descriptor: nil
).freeze

Class Method Summary collapse

Class Method Details

.build_flow_contribution(predicate_effects, assert_effects, return_override) ⇒ Object



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/rigor/rbs_extended.rb', line 658

def build_flow_contribution(predicate_effects, assert_effects, return_override)
  truthy = predicate_effects.select(&:truthy_only?).map(&:to_fact)
  falsey = predicate_effects.select(&:falsey_only?).map(&:to_fact)
  post_return = []

  assert_effects.each do |effect|
    case effect.condition
    when :if_truthy_return then truthy << effect.to_fact
    when :if_falsey_return then falsey << effect.to_fact
    else post_return << effect.to_fact
    end
  end

  FlowContribution.new(
    return_type: return_override,
    truthy_facts: nilable_slot(truthy),
    falsey_facts: nilable_slot(falsey),
    post_return_facts: nilable_slot(post_return),
    provenance: RBS_EXTENDED_PROVENANCE
  )
end

.nilable_slot(facts) ⇒ Object



680
681
682
# File 'lib/rigor/rbs_extended.rb', line 680

def nilable_slot(facts)
  facts.empty? ? nil : facts
end

.param_type_override_map(method_def, environment: nil) ⇒ Object

Convenience reader for call sites that want to look up a single override by parameter name. Returns a frozen Hash<Symbol, Rigor::Type>; missing keys mean “use the RBS-declared type”. Callers MUST treat the hash as read-only.



568
569
570
571
572
# File 'lib/rigor/rbs_extended.rb', line 568

def param_type_override_map(method_def, environment: nil)
  read_param_type_overrides(method_def, environment: environment)
    .to_h { |o| [o.param_name, o.type] }
    .freeze
end

.parse_app_payload(payload, name_scope: nil, reporter: nil, source_location: nil, hkt_registry: nil) ⇒ Object

ADR-20 slice 2d. Parses ‘App[<uri>, <ClassName>, …]` syntax into a `Rigor::Type::App`. When `hkt_registry` is supplied and the URI is registered with a body_tree, the `App` is reduced eagerly via Inference::HktRegistry#reduce so call sites observe the unfolded form (e.g. `Union[nil, true, false, …, Array[App[json::value, String]], Hash[String, App[json::value, String]]]`) rather than the opaque carrier. When the registry is absent or the URI is unregistered, the carrier with its registry-supplied bound (or `untyped` as a last-resort fallback) is returned as-is.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/rigor/rbs_extended.rb', line 478

def parse_app_payload(payload, name_scope: nil, reporter: nil, source_location: nil, hkt_registry: nil)
  match = APP_PAYLOAD_PATTERN.match(payload)
  return nil if match.nil?

  uri = match[:uri].to_sym
  arg_classes = match[:args].split(",").map(&:strip)
  args = arg_classes.map { |name| resolve_app_arg(name, name_scope: name_scope) }

  if args.any?(&:nil?)
    record_unresolved(reporter, "App payload `#{payload}`: unresolved arg class name", source_location)
    return nil
  end

  registration = hkt_registry&.registration(uri)
  bound = registration&.bound || Type::Combinator.untyped
  app = Type::App.new(uri, args, bound: bound)

  return app if hkt_registry.nil? || !hkt_registry.defined?(uri)

  reduced = hkt_registry.reduce(app)
  reduced || app
end

.parse_assert_annotation(string, name_scope: nil, reporter: nil, source_location: nil) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/rigor/rbs_extended.rb', line 271

def parse_assert_annotation(string, name_scope: nil, reporter: nil, source_location: nil)
  match = ASSERT_DIRECTIVE_PATTERN.match(string)
  return nil if match.nil?

  directive = match[:directive].to_s
  condition = ASSERT_CONDITIONS[directive]
  return nil if condition.nil?

  target = match[:target].to_s
  target_kind, target_name = target_fields(target)
  class_name, refinement_type, negative = resolve_directive_rhs(
    match,
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location
  )
  if class_name.nil? && refinement_type.nil?
    record_unresolved(reporter, string, source_location)
    return nil
  end

  AssertEffect.new(
    condition: condition,
    target_kind: target_kind,
    target_name: target_name,
    class_name: class_name,
    negative: negative,
    refinement_type: refinement_type
  )
end

.parse_param_annotation(string, name_scope: nil, reporter: nil, source_location: nil) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/rigor/rbs_extended.rb', line 593

def parse_param_annotation(string, name_scope: nil, reporter: nil, source_location: nil)
  match = PARAM_DIRECTIVE_PATTERN.match(string)
  return nil if match.nil?

  type = Builtins::ImportedRefinements.parse(
    match[:payload],
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location
  )
  if type.nil?
    record_unresolved(reporter, string, source_location)
    return nil
  end

  ParamOverride.new(param_name: match[:param].to_sym, type: type)
end

.parse_predicate_annotation(string, name_scope: nil, reporter: nil, source_location: nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rigor/rbs_extended.rb', line 188

def parse_predicate_annotation(string, name_scope: nil, reporter: nil, source_location: nil)
  match = PREDICATE_DIRECTIVE_PATTERN.match(string)
  return nil if match.nil?

  directive = match[:directive].to_s
  target = match[:target].to_s
  edge = directive == "predicate-if-true" ? :truthy_only : :falsey_only
  target_kind, target_name = target_fields(target)
  class_name, refinement_type, negative = resolve_directive_rhs(
    match,
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location
  )
  if class_name.nil? && refinement_type.nil?
    record_unresolved(reporter, string, source_location)
    return nil
  end

  PredicateEffect.new(
    edge: edge,
    target_kind: target_kind,
    target_name: target_name,
    class_name: class_name,
    negative: negative,
    refinement_type: refinement_type
  )
end

.parse_return_type_override(string, name_scope: nil, reporter: nil, source_location: nil, hkt_registry: nil) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/rigor/rbs_extended.rb', line 444

def parse_return_type_override(string, name_scope: nil, reporter: nil, source_location: nil, hkt_registry: nil)
  match = RETURN_DIRECTIVE_PATTERN.match(string)
  return nil if match.nil?

  app_type = parse_app_payload(
    match[:payload],
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location,
    hkt_registry: hkt_registry
  )
  return app_type if app_type

  type = Builtins::ImportedRefinements.parse(
    match[:payload],
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location
  )
  record_unresolved(reporter, string, source_location) if type.nil?
  type
end

.read_assert_effects(method_def, environment: nil) ⇒ Object

Reads RBS::Extended assertion effects (‘assert`, `assert-if-true`, `assert-if-false`) off `RBS::Definition::Method#annotations`. Returns an empty array when no recognised assertion directives are attached to the method.

See read_predicate_effects for the ‘environment:` keyword contract.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rigor/rbs_extended.rb', line 225

def read_assert_effects(method_def, environment: nil)
  return [] if method_def.nil?

  annotations = method_def.annotations
  return [] if annotations.nil? || annotations.empty?

  name_scope = environment&.name_scope
  reporter = environment&.rbs_extended_reporter

  effects = []
  annotations.each do |annotation|
    effect = parse_assert_annotation(
      annotation.string,
      name_scope: name_scope,
      reporter: reporter,
      source_location: annotation.location
    )
    effects << effect if effect
  end
  effects.uniq
end

.read_flow_contribution(method_def, environment: nil) ⇒ Object

Rolls up every recognised RBS::Extended directive on ‘method_def` into a single FlowContribution with the canonical FlowContribution::Fact payload (see ADR-7 § “Slice 4-A”):

  • ‘predicate-if-true` → `truthy_facts`

  • ‘predicate-if-false` → `falsey_facts`

  • ‘assert` → `post_return_facts`

  • ‘assert-if-true` → `truthy_facts`

  • ‘assert-if-false` → `falsey_facts`

  • ‘return:` override → `return_type` (`Rigor::Type`)

Param overrides are intentionally NOT included — they refine the call’s signature contract rather than its flow facts and do not fit ADR-2 § “Flow Contribution Bundle” slot semantics. Callers that care about parameter contracts keep using read_param_type_overrides / param_type_override_map.

Returns ‘nil` when the method carries no recognised contribution directives (callers can skip the merge step without iterating an empty bundle).

See read_predicate_effects for the ‘environment:` keyword contract.



647
648
649
650
651
652
653
654
655
656
# File 'lib/rigor/rbs_extended.rb', line 647

def read_flow_contribution(method_def, environment: nil)
  return nil if method_def.nil?

  predicate_effects = read_predicate_effects(method_def, environment: environment)
  assert_effects = read_assert_effects(method_def, environment: environment)
  return_override = read_return_type_override(method_def, environment: environment)
  return nil if predicate_effects.empty? && assert_effects.empty? && return_override.nil?

  build_flow_contribution(predicate_effects, assert_effects, return_override)
end

.read_param_type_overrides(method_def, environment: nil) ⇒ Object

Reads every ‘rigor:v1:param: <name> <refinement>` directive off `RBS::Definition::Method#annotations` and returns the resolved `ParamOverride` list. Annotations the parser cannot resolve (typo, unknown refinement, no `param:` directive at all) are silently dropped — the call site keeps the RBS-declared parameter type for those parameters. The reader accepts a nil method definition so call sites can pass through optional method lookups without a guard.

Example annotation in an RBS file:

class Slug
  %a{rigor:v1:param: id is non-empty-string}
  def normalise: (::String id) -> String
end

The RBS-declared type of ‘id` is `String`. The override tightens it to `non-empty-string` for argument-check purposes; passing a too-wide `Nominal` argument is flagged as an argument-type mismatch at the call site.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/rigor/rbs_extended.rb', line 544

def read_param_type_overrides(method_def, environment: nil)
  return [] if method_def.nil?

  annotations = method_def.annotations
  return [] if annotations.nil? || annotations.empty?

  name_scope = environment&.name_scope
  reporter = environment&.rbs_extended_reporter

  annotations.filter_map do |annotation|
    parse_param_annotation(
      annotation.string,
      name_scope: name_scope,
      reporter: reporter,
      source_location: annotation.location
    )
  end
end

.read_predicate_effects(method_def, environment: nil) ⇒ Object

Reads RBS::Extended predicate effects off ‘RBS::Definition::Method#annotations`. Returns the effects in source order; duplicates and unrecognised `rigor:v1:` directives are dropped. Returns an empty array (NEVER `nil`) for a method with no recognised annotations so callers can iterate unconditionally.

Parameters:

  • environment (Rigor::Environment, nil) (defaults to: nil)

    ADR-13 slice 3b. When provided, threads the plugin-supplied ‘name_scope:` and the per-run reporter through the annotation-parse path. `nil` (default) preserves the pre-slice-3b behaviour — no plugin resolvers consulted and no diagnostics accumulated.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rigor/rbs_extended.rb', line 141

def read_predicate_effects(method_def, environment: nil)
  return [] if method_def.nil?

  annotations = method_def.annotations
  return [] if annotations.nil? || annotations.empty?

  name_scope = environment&.name_scope
  reporter = environment&.rbs_extended_reporter

  effects = []
  annotations.each do |annotation|
    effect = parse_predicate_annotation(
      annotation.string,
      name_scope: name_scope,
      reporter: reporter,
      source_location: annotation.location
    )
    effects << effect if effect
  end
  effects.uniq
end

.read_return_type_override(method_def, environment: nil) ⇒ Object

Reads the ‘rigor:v1:return: <kebab-name>` directive off `RBS::Definition::Method#annotations`. The directive overrides a method’s RBS-declared return type with one of the imported-built-in refinements registered in ‘Rigor::Builtins::ImportedRefinements`. The override is the primary integration path for refinement carriers (`non-empty-string`, `positive-int`, `non-empty-array`, …) in v0.0 — annotation-driven, opt-in per method, and never silently rewrites a hand-authored RBS signature outside the annotation.

Example annotation in an RBS file:

class User
  %a{rigor:v1:return: non-empty-string}
  def name: () -> String
end

The RBS-declared return is ‘String`. The override tightens it to `non-empty-string` (i.e. `Difference[String, “”]`) for callers; RBS erasure of the tightened return goes back to `String` so the round-trip to ordinary RBS is unaffected.

Returns the resolved ‘Rigor::Type` value, or `nil` when:

  • the method has no annotations,

  • none of the annotations match the ‘rigor:v1:return:` directive,

  • the directive’s payload names a refinement not registered in ‘Rigor::Builtins::ImportedRefinements` (the analyzer prefers a silent miss over crashing on a typo; ADR-13 slice 3b surfaces the miss as a `dynamic.rbs-extended.unresolved` `:info` diagnostic when an `environment:` is supplied).



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/rigor/rbs_extended.rb', line 381

def read_return_type_override(method_def, environment: nil)
  return nil if method_def.nil?

  annotations = method_def.annotations
  return nil if annotations.nil? || annotations.empty?

  name_scope = environment&.name_scope
  reporter = environment&.rbs_extended_reporter
  hkt_registry = environment&.hkt_registry

  annotations.each do |annotation|
    type = parse_return_type_override(
      annotation.string,
      name_scope: name_scope,
      reporter: reporter,
      source_location: annotation.location,
      hkt_registry: hkt_registry
    )
    return type if type
  end
  nil
end

.record_unresolved(reporter, payload, source_location) ⇒ Object

ADR-13 slice 3b — guards every reporter call so the in-RbsExtended-module call sites can record events uniformly without nil-checking each time. When the reporter is nil (the v0.1.0 → v0.1.3 default for call sites that do not yet thread ‘environment:`), the call is a no-op and the parser stays fail-soft.



690
691
692
693
694
# File 'lib/rigor/rbs_extended.rb', line 690

def record_unresolved(reporter, payload, source_location)
  return if reporter.nil?

  reporter.record_unresolved(payload: payload, source_location: source_location)
end

.resolve_app_arg(class_name, name_scope: nil) ⇒ Object



501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/rigor/rbs_extended.rb', line 501

def resolve_app_arg(class_name, name_scope: nil)
  return nil unless /\A(?:::)?(?:[A-Z]\w*)(?:::[A-Z]\w*)*\z/.match?(class_name)

  normalized = class_name.sub(/\A::/, "")
  return Type::Nominal.new(normalized) if name_scope.nil?

  if name_scope.respond_to?(:nominal_for_name)
    resolved = name_scope.nominal_for_name(normalized)
    return resolved if resolved
  end
  Type::Nominal.new(normalized)
end

.resolve_directive_rhs(match, name_scope: nil, reporter: nil, source_location: nil) ⇒ Object

Resolves the ‘class_name` / `refinement` alternation in the assert / predicate directive patterns. Returns `[class_name, refinement_type, negative]`:

  • Class-name arm matched: ‘class_name` is the resolved string (leading `::` stripped), `refinement_type` is nil, `negative` reflects the optional `~` prefix.

  • Refinement arm matched: ‘class_name` is nil, `refinement_type` is the resolved `Rigor::Type`, `negative` reflects the `~` prefix. v0.0.5 supports refinement-form negation for the `Difference[base, Constant]` shape (the narrowing tier computes the complement decomposition); other refinement carriers under negation fall back to the conservative “current_type unchanged” answer.

  • Refinement payload unparseable: returns ‘[nil, nil, false]` so callers can drop the directive silently (fail-soft policy).



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/rigor/rbs_extended.rb', line 320

def resolve_directive_rhs(match, name_scope: nil, reporter: nil, source_location: nil)
  negative = match[:negation].to_s == "~"
  class_capture = match[:class_name]
  return [class_capture.to_s.sub(/\A::/, ""), nil, negative] if class_capture

  refinement_capture = match[:refinement]
  return [nil, nil, false] if refinement_capture.nil?

  type = Builtins::ImportedRefinements.parse(
    refinement_capture,
    name_scope: name_scope,
    reporter: reporter,
    source_location: source_location
  )
  return [nil, nil, false] if type.nil?

  [nil, type, negative]
end

.target_fields(target) ⇒ Object



339
340
341
342
343
344
345
# File 'lib/rigor/rbs_extended.rb', line 339

def target_fields(target)
  if target == "self"
    %i[self self]
  else
    [:parameter, target.to_sym]
  end
end