rigortype 0.4.8 → 0.4.9

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 (349) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/docs/handbook/01-getting-started.md +1 -1
  4. data/docs/handbook/04-tuples-and-shapes.md +6 -1
  5. data/docs/handbook/12-lightweight-hkt.md +1 -1
  6. data/docs/handbook/appendix-typescript.md +28 -17
  7. data/docs/manual/02-cli-reference.md +10 -0
  8. data/docs/manual/03-configuration.md +1 -1
  9. data/docs/manual/04-diagnostics.md +1 -1
  10. data/docs/manual/10-mcp-server.md +122 -10
  11. data/docs/manual/plugins/rigor-activerecord.md +7 -0
  12. data/lib/rigor/analysis/check_rules/always_raises.rb +95 -0
  13. data/lib/rigor/analysis/check_rules/always_truthy_condition_collector.rb +32 -2
  14. data/lib/rigor/analysis/check_rules/argument_type.rb +802 -0
  15. data/lib/rigor/analysis/check_rules/call_facts.rb +2 -2
  16. data/lib/rigor/analysis/check_rules/dead_assignment_collector.rb +30 -1
  17. data/lib/rigor/analysis/check_rules/declaration_sourced_guard.rb +4 -5
  18. data/lib/rigor/analysis/check_rules/duplicate_hash_key_collector.rb +34 -0
  19. data/lib/rigor/analysis/check_rules/ivar_write_collector.rb +131 -4
  20. data/lib/rigor/analysis/check_rules/nil_receiver.rb +195 -0
  21. data/lib/rigor/analysis/check_rules/override_param_narrowed.rb +111 -0
  22. data/lib/rigor/analysis/check_rules/override_return_widened.rb +88 -0
  23. data/lib/rigor/analysis/check_rules/override_support.rb +246 -0
  24. data/lib/rigor/analysis/check_rules/override_visibility.rb +135 -0
  25. data/lib/rigor/analysis/check_rules/published_constant_guard.rb +75 -31
  26. data/lib/rigor/analysis/check_rules/raise_non_exception.rb +198 -0
  27. data/lib/rigor/analysis/check_rules/receiver_predicates.rb +576 -0
  28. data/lib/rigor/analysis/check_rules/return_in_ensure_collector.rb +52 -4
  29. data/lib/rigor/analysis/check_rules/return_type.rb +225 -0
  30. data/lib/rigor/analysis/check_rules/rigor_testing.rb +119 -0
  31. data/lib/rigor/analysis/check_rules/rule_ids.rb +7 -7
  32. data/lib/rigor/analysis/check_rules/rule_walk.rb +4 -3
  33. data/lib/rigor/analysis/check_rules/self_closedness_scanner.rb +2 -0
  34. data/lib/rigor/analysis/check_rules/self_undefined_method.rb +165 -0
  35. data/lib/rigor/analysis/check_rules/shadowed_rescue_collector.rb +34 -0
  36. data/lib/rigor/analysis/check_rules/source_arity.rb +2 -2
  37. data/lib/rigor/analysis/check_rules/special_global_write.rb +256 -0
  38. data/lib/rigor/analysis/check_rules/suppression.rb +275 -0
  39. data/lib/rigor/analysis/check_rules/undefined_method.rb +882 -0
  40. data/lib/rigor/analysis/check_rules/unreachable_branch.rb +97 -0
  41. data/lib/rigor/analysis/check_rules/unreachable_clause_collector.rb +42 -0
  42. data/lib/rigor/analysis/check_rules/unresolved_toplevel.rb +140 -0
  43. data/lib/rigor/analysis/check_rules/visibility_mismatch.rb +252 -0
  44. data/lib/rigor/analysis/check_rules/void_value_use_collector.rb +27 -0
  45. data/lib/rigor/analysis/check_rules/wrong_arity.rb +303 -0
  46. data/lib/rigor/analysis/check_rules.rb +66 -4931
  47. data/lib/rigor/analysis/dependency_recorder.rb +1 -1
  48. data/lib/rigor/analysis/dependency_source_inference/builder.rb +2 -4
  49. data/lib/rigor/analysis/dependency_source_inference/gem_resolver.rb +1 -2
  50. data/lib/rigor/analysis/dependency_source_inference/walker.rb +15 -15
  51. data/lib/rigor/analysis/incremental_session.rb +4 -4
  52. data/lib/rigor/analysis/path_expansion.rb +1 -1
  53. data/lib/rigor/analysis/plugin_fact_fingerprint.rb +5 -5
  54. data/lib/rigor/analysis/reachability/graph.rb +2 -3
  55. data/lib/rigor/analysis/reachability/plugin_roots.rb +2 -2
  56. data/lib/rigor/analysis/reachability/scan.rb +2 -2
  57. data/lib/rigor/analysis/rule_catalog.rb +2 -1
  58. data/lib/rigor/analysis/run_cache_key.rb +9 -8
  59. data/lib/rigor/analysis/runner/buffer_pool_dispatcher.rb +2 -2
  60. data/lib/rigor/analysis/runner/dependency_rows.rb +206 -0
  61. data/lib/rigor/analysis/runner/diagnostic_aggregator.rb +24 -22
  62. data/lib/rigor/analysis/runner/effect_cache_entries.rb +69 -0
  63. data/lib/rigor/analysis/runner/pool_coordinator.rb +6 -8
  64. data/lib/rigor/analysis/runner/project_pre_passes.rb +3 -5
  65. data/lib/rigor/analysis/runner/scope_seed.rb +144 -0
  66. data/lib/rigor/analysis/runner.rb +49 -296
  67. data/lib/rigor/analysis/template_unit_collector.rb +1 -1
  68. data/lib/rigor/analysis/template_units.rb +10 -10
  69. data/lib/rigor/analysis/worker_session.rb +7 -7
  70. data/lib/rigor/bleeding_edge.rb +3 -2
  71. data/lib/rigor/builtins/hkt_builtins.rb +17 -17
  72. data/lib/rigor/builtins/imported_refinements.rb +8 -16
  73. data/lib/rigor/builtins/regex_refinement.rb +3 -3
  74. data/lib/rigor/builtins/static_return_refinements.rb +143 -19
  75. data/lib/rigor/cache/incremental_snapshot.rb +21 -8
  76. data/lib/rigor/cache/rbs_descriptor.rb +18 -1
  77. data/lib/rigor/cli/coverage_renderer.rb +15 -1
  78. data/lib/rigor/cli/coverage_report.rb +19 -1
  79. data/lib/rigor/cli/diagnostic_formats.rb +3 -2
  80. data/lib/rigor/cli/docs_command.rb +5 -5
  81. data/lib/rigor/cli/effects_command.rb +7 -6
  82. data/lib/rigor/cli/fused_protection_report.rb +7 -7
  83. data/lib/rigor/cli/lsp_command.rb +1 -1
  84. data/lib/rigor/cli/mcp_command.rb +5 -2
  85. data/lib/rigor/cli/options.rb +4 -0
  86. data/lib/rigor/cli/plugins_command.rb +1 -1
  87. data/lib/rigor/cli/sig_gen_command.rb +1 -1
  88. data/lib/rigor/cli/skill_command.rb +5 -4
  89. data/lib/rigor/cli/triage_renderer.rb +3 -2
  90. data/lib/rigor/cli/unused_command.rb +4 -4
  91. data/lib/rigor/cli.rb +14 -0
  92. data/lib/rigor/configuration/coercion.rb +418 -0
  93. data/lib/rigor/configuration/defaults.rb +248 -0
  94. data/lib/rigor/configuration/dependencies.rb +1 -1
  95. data/lib/rigor/configuration/loader.rb +354 -0
  96. data/lib/rigor/configuration/severity_profile.rb +4 -4
  97. data/lib/rigor/configuration.rb +51 -860
  98. data/lib/rigor/effects/attribution.rb +1 -1
  99. data/lib/rigor/effects/effect_table.rb +1 -1
  100. data/lib/rigor/effects/identity.rb +3 -3
  101. data/lib/rigor/effects/local_ownership.rb +2 -2
  102. data/lib/rigor/effects/snapshot.rb +1 -1
  103. data/lib/rigor/effects/summary.rb +5 -4
  104. data/lib/rigor/environment/member_consistency.rb +10 -0
  105. data/lib/rigor/environment/rbs_loader.rb +19 -22
  106. data/lib/rigor/environment/reflection.rb +1 -1
  107. data/lib/rigor/environment/unreadable_signature.rb +21 -0
  108. data/lib/rigor/environment.rb +8 -8
  109. data/lib/rigor/flow_contribution/merger.rb +1 -0
  110. data/lib/rigor/hashing/xxh3.rb +1 -1
  111. data/lib/rigor/inference/acceptance.rb +23 -2
  112. data/lib/rigor/inference/alias_mutation_floor/site_collector.rb +5 -0
  113. data/lib/rigor/inference/alias_mutation_floor.rb +0 -5
  114. data/lib/rigor/inference/anonymous_meta_class.rb +2 -2
  115. data/lib/rigor/inference/block_call_timing.rb +6 -0
  116. data/lib/rigor/inference/budget_trace.rb +3 -2
  117. data/lib/rigor/inference/builtins/date_catalog.rb +3 -3
  118. data/lib/rigor/inference/builtins/method_catalog.rb +3 -3
  119. data/lib/rigor/inference/builtins/time_catalog.rb +3 -3
  120. data/lib/rigor/inference/call_ladder/literal_send.rb +98 -0
  121. data/lib/rigor/inference/call_ladder/overriding_def.rb +98 -0
  122. data/lib/rigor/inference/call_ladder/project_singleton.rb +362 -0
  123. data/lib/rigor/inference/call_ladder/refined.rb +212 -0
  124. data/lib/rigor/inference/call_ladder/top_level_def.rb +222 -0
  125. data/lib/rigor/inference/call_ladder/user_method.rb +247 -0
  126. data/lib/rigor/inference/call_ladder.rb +219 -0
  127. data/lib/rigor/inference/callee_write_census.rb +2 -2
  128. data/lib/rigor/inference/captured_locals.rb +2 -2
  129. data/lib/rigor/inference/clause_widening.rb +63 -1
  130. data/lib/rigor/inference/declaration_walk/context.rb +12 -12
  131. data/lib/rigor/inference/declaration_walk/rules.rb +750 -0
  132. data/lib/rigor/inference/declaration_walk/traversal.rb +9 -8
  133. data/lib/rigor/inference/declaration_walk.rb +4 -3
  134. data/lib/rigor/inference/def_node_resolver.rb +2 -2
  135. data/lib/rigor/inference/def_return_typer.rb +14 -6
  136. data/lib/rigor/inference/deferred_mutation.rb +120 -0
  137. data/lib/rigor/inference/define_method_block_self.rb +1 -1
  138. data/lib/rigor/inference/definer_resolution.rb +4 -4
  139. data/lib/rigor/inference/error_info.rb +8 -8
  140. data/lib/rigor/inference/expression_typer/composites.rb +348 -0
  141. data/lib/rigor/inference/expression_typer/per_element_assemblers.rb +233 -0
  142. data/lib/rigor/inference/expression_typer/writes.rb +218 -0
  143. data/lib/rigor/inference/expression_typer.rb +275 -1825
  144. data/lib/rigor/inference/external_ancestor_resolution.rb +2 -2
  145. data/lib/rigor/inference/fallback.rb +1 -0
  146. data/lib/rigor/inference/for_yield_slots.rb +126 -0
  147. data/lib/rigor/inference/genuine_send.rb +92 -0
  148. data/lib/rigor/inference/global_write_census.rb +2 -2
  149. data/lib/rigor/inference/hkt_body.rb +2 -0
  150. data/lib/rigor/inference/in_effect_refinements/walk.rb +2 -2
  151. data/lib/rigor/inference/index_write_widening.rb +4 -4
  152. data/lib/rigor/inference/indexed_narrowing.rb +1 -1
  153. data/lib/rigor/inference/intervening_ivar_reset.rb +46 -9
  154. data/lib/rigor/inference/ivar_writer_census.rb +1 -1
  155. data/lib/rigor/inference/loop_store_sites.rb +239 -0
  156. data/lib/rigor/inference/macro_block_self_type.rb +4 -3
  157. data/lib/rigor/inference/method_dispatcher/block_folding.rb +3 -2
  158. data/lib/rigor/inference/method_dispatcher/call_context.rb +84 -14
  159. data/lib/rigor/inference/method_dispatcher/composite_receiver.rb +144 -0
  160. data/lib/rigor/inference/method_dispatcher/constant_folding.rb +7 -5
  161. data/lib/rigor/inference/method_dispatcher/declared_constructor.rb +1 -1
  162. data/lib/rigor/inference/method_dispatcher/dependency_source.rb +90 -0
  163. data/lib/rigor/inference/method_dispatcher/discovered_method.rb +78 -0
  164. data/lib/rigor/inference/method_dispatcher/file_folding.rb +2 -2
  165. data/lib/rigor/inference/method_dispatcher/hkt_builtin_return.rb +40 -0
  166. data/lib/rigor/inference/method_dispatcher/iterator_dispatch.rb +1 -6
  167. data/lib/rigor/inference/method_dispatcher/literal_string_folding.rb +4 -2
  168. data/lib/rigor/inference/method_dispatcher/match_data_folding.rb +1 -1
  169. data/lib/rigor/inference/method_dispatcher/meta_introspection.rb +506 -0
  170. data/lib/rigor/inference/method_dispatcher/overload_selector.rb +2 -0
  171. data/lib/rigor/inference/method_dispatcher/plugin_contribution.rb +134 -0
  172. data/lib/rigor/inference/method_dispatcher/project_patched.rb +134 -0
  173. data/lib/rigor/inference/method_dispatcher/rbs_dispatch.rb +125 -43
  174. data/lib/rigor/inference/method_dispatcher/reduce_folding.rb +3 -4
  175. data/lib/rigor/inference/method_dispatcher/regexp_folding.rb +1 -1
  176. data/lib/rigor/inference/method_dispatcher/set_folding.rb +1 -1
  177. data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +19 -6
  178. data/lib/rigor/inference/method_dispatcher/singleton_folding.rb +3 -3
  179. data/lib/rigor/inference/method_dispatcher/singleton_mixin_dispatch.rb +7 -4
  180. data/lib/rigor/inference/method_dispatcher/static_refinement.rb +106 -0
  181. data/lib/rigor/inference/method_dispatcher/synthesized_stub.rb +41 -0
  182. data/lib/rigor/inference/method_dispatcher/synthetic_method_dispatch.rb +133 -0
  183. data/lib/rigor/inference/method_dispatcher/universal_object_dispatch.rb +2 -1
  184. data/lib/rigor/inference/method_dispatcher/user_class_fallback.rb +216 -0
  185. data/lib/rigor/inference/method_dispatcher.rb +175 -1361
  186. data/lib/rigor/inference/method_parameter_binder.rb +18 -18
  187. data/lib/rigor/inference/module_function_state.rb +5 -4
  188. data/lib/rigor/inference/mutation_widening.rb +20 -7
  189. data/lib/rigor/inference/narrowing/case_literals.rb +58 -0
  190. data/lib/rigor/inference/narrowing/class_dispatch.rb +302 -0
  191. data/lib/rigor/inference/narrowing/complements.rb +322 -0
  192. data/lib/rigor/inference/narrowing/presence_predicates.rb +205 -0
  193. data/lib/rigor/inference/narrowing/regex_match.rb +499 -0
  194. data/lib/rigor/inference/narrowing.rb +86 -1269
  195. data/lib/rigor/inference/object_mixins.rb +2 -2
  196. data/lib/rigor/inference/opaque_mutation.rb +214 -0
  197. data/lib/rigor/inference/optimistic_origin.rb +52 -14
  198. data/lib/rigor/inference/parameter_inference_collector.rb +1 -1
  199. data/lib/rigor/inference/pre_eval_constants.rb +11 -9
  200. data/lib/rigor/inference/precision_scanner.rb +27 -2
  201. data/lib/rigor/inference/project_patched_methods.rb +3 -3
  202. data/lib/rigor/inference/range_constant.rb +3 -3
  203. data/lib/rigor/inference/rbs_type_translator.rb +2 -2
  204. data/lib/rigor/inference/refine_self.rb +8 -7
  205. data/lib/rigor/inference/refined_dispatch.rb +2 -2
  206. data/lib/rigor/inference/return_barrier.rb +24 -9
  207. data/lib/rigor/inference/scope_indexer/alias_def_nodes.rb +311 -0
  208. data/lib/rigor/inference/scope_indexer/census_scope.rb +47 -0
  209. data/lib/rigor/inference/scope_indexer/certainty.rb +2 -2
  210. data/lib/rigor/inference/scope_indexer/class_cvar_census.rb +282 -0
  211. data/lib/rigor/inference/scope_indexer/class_cvars_collector.rb +12 -5
  212. data/lib/rigor/inference/scope_indexer/class_declarations.rb +228 -0
  213. data/lib/rigor/inference/scope_indexer/class_ivar_index.rb +2028 -0
  214. data/lib/rigor/inference/scope_indexer/constant_ivar_value.rb +4 -3
  215. data/lib/rigor/inference/scope_indexer/constant_tables.rb +844 -0
  216. data/lib/rigor/inference/scope_indexer/cvar_hierarchy.rb +159 -0
  217. data/lib/rigor/inference/scope_indexer/declaration_artifacts.rb +219 -0
  218. data/lib/rigor/inference/scope_indexer/declaration_signature.rb +212 -0
  219. data/lib/rigor/inference/scope_indexer/def_nestings_collector.rb +2 -2
  220. data/lib/rigor/inference/scope_indexer/factory_call_resolver.rb +53 -0
  221. data/lib/rigor/inference/scope_indexer/literal_receiver_mutations.rb +266 -0
  222. data/lib/rigor/inference/scope_indexer/member_layouts_collector.rb +8 -7
  223. data/lib/rigor/inference/scope_indexer/mixin_accumulator.rb +1 -1
  224. data/lib/rigor/inference/scope_indexer/mixin_effects.rb +470 -0
  225. data/lib/rigor/inference/scope_indexer/mixin_tables.rb +388 -0
  226. data/lib/rigor/inference/scope_indexer/mutated_carrier_widening.rb +163 -0
  227. data/lib/rigor/inference/scope_indexer/mutated_element_census.rb +211 -0
  228. data/lib/rigor/inference/scope_indexer/node_propagation.rb +520 -0
  229. data/lib/rigor/inference/scope_indexer/program_globals.rb +154 -0
  230. data/lib/rigor/inference/scope_indexer/published_constant_cvars.rb +146 -0
  231. data/lib/rigor/inference/scope_indexer/published_constant_ivars.rb +181 -0
  232. data/lib/rigor/inference/scope_indexer/shared_walk_tables.rb +823 -0
  233. data/lib/rigor/inference/scope_indexer/singleton_class_sources.rb +85 -0
  234. data/lib/rigor/inference/scope_indexer/superclasses_collector.rb +5 -5
  235. data/lib/rigor/inference/scope_indexer.rb +2657 -9558
  236. data/lib/rigor/inference/self_call_ivar_writes.rb +22 -3
  237. data/lib/rigor/inference/set_mutation.rb +2 -2
  238. data/lib/rigor/inference/singleton_hook_definers.rb +185 -0
  239. data/lib/rigor/inference/statement_evaluator/def_arguments_and_assertions.rb +365 -0
  240. data/lib/rigor/inference/statement_evaluator/pattern_bindings.rb +581 -0
  241. data/lib/rigor/inference/statement_evaluator.rb +201 -1008
  242. data/lib/rigor/inference/synthetic_method_scanner.rb +0 -3
  243. data/lib/rigor/language_server/completion_provider.rb +8 -8
  244. data/lib/rigor/language_server/diagnostic_publisher.rb +6 -7
  245. data/lib/rigor/language_server/document_symbol_provider.rb +4 -6
  246. data/lib/rigor/language_server/folding_range_provider.rb +3 -3
  247. data/lib/rigor/language_server/hover_provider.rb +2 -2
  248. data/lib/rigor/language_server/loop.rb +2 -2
  249. data/lib/rigor/language_server/server.rb +3 -2
  250. data/lib/rigor/language_server/signature_help_provider.rb +7 -9
  251. data/lib/rigor/lens/document.rb +10 -1
  252. data/lib/rigor/lens/file_reader.rb +2 -1
  253. data/lib/rigor/mcp/loop.rb +48 -6
  254. data/lib/rigor/mcp/server.rb +248 -23
  255. data/lib/rigor/mcp/skill_catalog.rb +157 -0
  256. data/lib/rigor/plugin/base.rb +22 -4
  257. data/lib/rigor/plugin/effect_attribution.rb +1 -1
  258. data/lib/rigor/plugin/inflector.rb +2 -2
  259. data/lib/rigor/plugin/io_boundary.rb +1 -1
  260. data/lib/rigor/plugin/loader.rb +4 -4
  261. data/lib/rigor/plugin/macro/heredoc_template.rb +4 -3
  262. data/lib/rigor/plugin/optimistic_return.rb +20 -0
  263. data/lib/rigor/plugin/registry.rb +2 -2
  264. data/lib/rigor/plugin/template_unit.rb +4 -2
  265. data/lib/rigor/plugin/trust_policy.rb +2 -1
  266. data/lib/rigor/plugin.rb +1 -1
  267. data/lib/rigor/protection/mutator.rb +8 -9
  268. data/lib/rigor/rbs_extended/reporter.rb +2 -2
  269. data/lib/rigor/rbs_extended.rb +2 -2
  270. data/lib/rigor/reflection/constant_ancestors.rb +1 -1
  271. data/lib/rigor/reflection/project_method.rb +2 -2
  272. data/lib/rigor/reflection.rb +4 -4
  273. data/lib/rigor/scope/discovery_index.rb +15 -10
  274. data/lib/rigor/scope/resolution_chain.rb +29 -6
  275. data/lib/rigor/scope.rb +89 -32
  276. data/lib/rigor/sig_gen/generator.rb +5 -4
  277. data/lib/rigor/sig_gen/renderer.rb +3 -3
  278. data/lib/rigor/sig_gen/writer.rb +8 -6
  279. data/lib/rigor/source/parameter_envelope.rb +1 -1
  280. data/lib/rigor/triage/catalogue.rb +4 -4
  281. data/lib/rigor/type/combinator.rb +2 -2
  282. data/lib/rigor/type/data_instance.rb +2 -2
  283. data/lib/rigor/type/struct_instance.rb +3 -3
  284. data/lib/rigor/type_node/generic.rb +2 -1
  285. data/lib/rigor/type_of_memo.rb +2 -1
  286. data/lib/rigor/typing_state.rb +3 -2
  287. data/lib/rigor/version.rb +1 -1
  288. data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/analyzer.rb +2 -2
  289. data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/channel_discoverer.rb +1 -1
  290. data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer/mailer_discoverer.rb +4 -4
  291. data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer.rb +1 -1
  292. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/controller_discoverer.rb +4 -3
  293. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/effects.rb +7 -5
  294. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/erb_compiler.rb +4 -2
  295. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/render_locals.rb +1 -1
  296. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/view_assigns.rb +3 -2
  297. data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack.rb +7 -7
  298. data/plugins/rigor-activejob/lib/rigor/plugin/activejob/job_discoverer.rb +1 -1
  299. data/plugins/rigor-activejob/lib/rigor/plugin/activejob.rb +2 -2
  300. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_discoverer.rb +9 -8
  301. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_index.rb +3 -2
  302. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_parser.rb +15 -7
  303. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_table.rb +10 -3
  304. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/structure_sql_parser.rb +12 -1
  305. data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord.rb +20 -5
  306. data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage/attachment_discoverer.rb +1 -1
  307. data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage.rb +3 -2
  308. data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext/effects.rb +1 -1
  309. data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext.rb +8 -7
  310. data/plugins/rigor-dry-schema/lib/rigor/plugin/dry_schema.rb +1 -1
  311. data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types/alias_scanner.rb +1 -1
  312. data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types.rb +6 -7
  313. data/plugins/rigor-factorybot/lib/rigor/plugin/factorybot/factory_discoverer.rb +3 -2
  314. data/plugins/rigor-graphql/lib/rigor/plugin/graphql/type_scanner.rb +4 -3
  315. data/plugins/rigor-hanami/lib/rigor/plugin/hanami.rb +4 -2
  316. data/plugins/rigor-minitest/lib/rigor/plugin/minitest.rb +1 -1
  317. data/plugins/rigor-pundit/lib/rigor/plugin/pundit.rb +1 -1
  318. data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n/analyzer.rb +5 -3
  319. data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n.rb +1 -1
  320. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/devise_routes.rb +4 -5
  321. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/doorkeeper_routes.rb +2 -2
  322. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_discoverer.rb +5 -4
  323. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_table.rb +3 -2
  324. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/routes_parser.rb +30 -31
  325. data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes.rb +13 -15
  326. data/plugins/rigor-rbnacl/lib/rigor/plugin/rbnacl.rb +1 -1
  327. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/let_scope_index.rb +4 -3
  328. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/matcher_analyzer.rb +16 -13
  329. data/plugins/rigor-rspec/lib/rigor/plugin/rspec/scope_walker.rb +3 -2
  330. data/plugins/rigor-shoulda-matchers/lib/rigor/plugin/shoulda_matchers/analyzer.rb +6 -6
  331. data/plugins/rigor-sidekiq/lib/rigor/plugin/sidekiq/analyzer.rb +3 -3
  332. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/assertion_recognizer.rb +2 -2
  333. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog.rb +3 -2
  334. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog_walker.rb +6 -5
  335. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sig_parser.rb +2 -2
  336. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sigil_detector.rb +4 -5
  337. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/type_translator.rb +1 -1
  338. data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet.rb +3 -4
  339. data/sig/rigor/analysis/check_rules/suppression.rbs +9 -0
  340. data/sig/rigor/analysis/dependency_recorder.rbs +9 -0
  341. data/sig/rigor/inference.rbs +275 -11
  342. data/sig/rigor/plugin/base.rbs +14 -5
  343. data/sig/rigor/plugin/optimistic_return.rbs +16 -0
  344. data/sig/rigor/scope.rbs +7 -1
  345. data/sig/rigor/source.rbs +4 -0
  346. data/skills/rigor-mcp-setup/SKILL.md +20 -6
  347. data/skills/rigor-plugin-review/SKILL.md +3 -3
  348. data/skills/rigor-type-oracle/references/01-oracle-commands.md +1 -1
  349. metadata +90 -3
@@ -7,6 +7,7 @@ require_relative "../source/node_children"
7
7
  require_relative "../source/parameter_envelope"
8
8
  require_relative "../type"
9
9
  require_relative "guard_rebinding"
10
+ require_relative "mutation_widening"
10
11
  require_relative "project_method_ownership"
11
12
  require_relative "refined_dispatch/own_method"
12
13
  require_relative "../typing_state"
@@ -100,7 +101,21 @@ module Rigor
100
101
 
101
102
  # The names of the instance variables `call_node`, an implicit-self or `self.` call, may write, as a frozen
102
103
  # `Set`, or nil when the analysis cannot prove the set. `scope` is the scope the call's operands were typed in.
103
- def of(call_node, scope)
104
+ def of(call_node, scope) = effects(call_node, scope)&.written
105
+
106
+ # Issue #3340 — what a proven call does to `self`'s instance variables: `written`, the names {.of} answers, and
107
+ # `mutated`, `{ name => [call, ...] }` for each instance variable the read code calls an in-place mutator on
108
+ # ({MUTATORS}: `@xs.clear`, `@h[k] = v`). A mutation rebinds nothing, so it is no write, but it falsifies what
109
+ # the caller's binding says about the object's contents (`return if @xs.empty?; reset!; @xs.size`).
110
+ Effects = Data.define(:written, :mutated)
111
+
112
+ # The names an in-place mutation of an instance variable is read by: the ones the straight-line widening
113
+ # responds to, on any carrier.
114
+ MUTATORS = (MutationWidening::SHAPE_MUTATORS | SetMutation::MUTATORS).freeze
115
+
116
+ # The {Effects} of `call_node`, an implicit-self or `self.` call, or nil when the analysis cannot prove them.
117
+ # `scope` is the scope the call's operands were typed in.
118
+ def effects(call_node, scope)
104
119
  return nil unless call_node.is_a?(Prism::CallNode)
105
120
  return nil unless call_node.receiver.nil? || call_node.receiver.is_a?(Prism::SelfNode)
106
121
 
@@ -112,7 +127,7 @@ module Rigor
112
127
  proof.no_protocol_override
113
128
  proof.self_call(call_node)
114
129
  proof.read(call_node.block)
115
- proof.written.freeze
130
+ Effects.new(written: proof.written.freeze, mutated: proof.mutated.each_value(&:freeze).freeze)
116
131
  rescue StandardError # an `Unprovable`, or a lookup that raised: either proves nothing
117
132
  nil
118
133
  end
@@ -163,13 +178,14 @@ module Rigor
163
178
 
164
179
  # One proof's state: the instance variables read code writes, and the `def` bodies already read.
165
180
  class Proof
166
- attr_reader :written
181
+ attr_reader :written, :mutated
167
182
 
168
183
  def initialize(self_type, scope)
169
184
  @self_type = self_type
170
185
  @class_name = self_type.class_name
171
186
  @scope = scope
172
187
  @written = Set.new
188
+ @mutated = {}
173
189
  @visited = {}.compare_by_identity
174
190
  end
175
191
 
@@ -312,6 +328,9 @@ module Rigor
312
328
  unprovable! if GuardRebinding.code_running_name?(node.name) || refined?(node.name)
313
329
  type = receiver_type(receiver)
314
330
  unprovable! if type.nil? || GuardRebinding.type_method_foreign?(type, node.name, @scope)
331
+ return unless receiver.is_a?(Prism::InstanceVariableReadNode) && MUTATORS.include?(node.name)
332
+
333
+ (@mutated[receiver.name] ||= []) << node
315
334
  end
316
335
 
317
336
  def read_def(def_node)
@@ -18,7 +18,7 @@ module Rigor
18
18
  # this module answers end at the typed Set #2290 builds for a non-constant literal — `Set[E | Dynamic[top]]`:
19
19
  #
20
20
  # - a constant-folded `Constant[Set]` widens to that carrier, its members' value pins dropped
21
- # ({MethodDispatcher.array_new_element}) and joined with the `Dynamic[top]` floor;
21
+ # ({MethodDispatcher::MetaIntrospection.array_new_element}) and joined with the `Dynamic[top]` floor;
22
22
  # - a typed `Set` whose element already carries a gradual arm re-grows the way
23
23
  # {MutationRejoin.regrowable_carrier?} lets an `Array` / `Hash` re-grow;
24
24
  # - a PRECISE `Set[String]` (an RBS return such as `Array#to_set`) re-grows too (issue #2361), as a precise
@@ -182,7 +182,7 @@ module Rigor
182
182
  # A stored member's value pin and literal shape are dropped, as an `Array.new` fill's are: the set holds the
183
183
  # object, and the program keeps a reference to it.
184
184
  def widened_member(type)
185
- MethodDispatcher.array_new_element(type)
185
+ MethodDispatcher::MetaIntrospection.array_new_element(type)
186
186
  end
187
187
  end
188
188
  end
@@ -0,0 +1,185 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rigor
4
+ module Inference
5
+ # The Ruby hooks that may define a singleton method on a class ahead of a `def self.x` the class only inherits,
6
+ # read by `CallLadder::ProjectSingleton`'s withholding: when one may run, the inherited `def` is withheld and the
7
+ # call types `Dynamic`. Neither reads which name the hook defines, as the `extended` arm does not.
8
+ #
9
+ # - An `inherited` hook anywhere on the chain past the receiver's own entry runs when the receiver's class is
10
+ # created (`def self.inherited(s) = s.define_singleton_method(:x)`): a class's `def self.inherited`, or an
11
+ # `inherited` an extended module defines.
12
+ # - Issue #3373 — a module's `def self.included(base)` hook runs when a class nearer than the `def`'s owner
13
+ # includes it (`base.define_singleton_method(:x) { … }`), read off the chain
14
+ # ({Scope::ResolutionChain#included_hook_before?}) for a hook whose body may define one
15
+ # ({.may_define_singleton?}).
16
+ module SingletonHookDefiners
17
+ module_function
18
+
19
+ def ahead?(chain, receiver, owner, scope)
20
+ (receiver != owner && inherited_hook?(chain, receiver, scope)) || included_hook?(chain, owner, scope)
21
+ end
22
+
23
+ def inherited_hook?(chain, receiver, scope)
24
+ chain.entries.any? do |entry|
25
+ next false if entry.external? || entry.name == receiver
26
+
27
+ if entry.side == :singleton
28
+ !scope.singleton_def_for(entry.name, :inherited).nil? || foreign_hook?(scope, entry.name, :inherited)
29
+ else
30
+ scope.discovered_method?(entry.name, :inherited, :instance)
31
+ end
32
+ end
33
+ end
34
+
35
+ def included_hook?(chain, owner, scope)
36
+ chain.included_hook_before?(owner, scope) do |mixin, hook|
37
+ foreign_hook?(scope, mixin, :included) || (!hook.nil? && may_define_singleton?(hook))
38
+ end
39
+ end
40
+
41
+ # Issue #3450 — a hook written outside its module's or class's body (`def Hk.included(base)`, `def
42
+ # Tt.inherited(s)`, `Hk.define_singleton_method(:included) { … }`) is in no def table, only in the
43
+ # object-singleton mark ({SingletonHookDecline.foreign_singleton_definer?}). Its body is not held, so it may
44
+ # define.
45
+ def foreign_hook?(scope, owner, hook_name)
46
+ SingletonHookDecline.foreign_singleton_definer?(scope, owner, hook_name)
47
+ end
48
+
49
+ # The calls an `included` hook may make on its includer without defining a singleton method on it: they add a
50
+ # module, which the chain reads as a hook-extended entry (#1592).
51
+ MIXIN_CALLS = %i[extend include prepend].to_set.freeze
52
+ # The calls that define, or may define, a singleton method on an object that need not be written as their
53
+ # receiver: the definers themselves, and the eval and send families, which can reach the includer by name.
54
+ OPAQUE_CALLS = %i[
55
+ define_singleton_method singleton_class eval instance_eval class_eval module_eval instance_exec class_exec
56
+ module_exec binding send public_send __send__ local_variable_get method instance_variable_get
57
+ ].to_set.freeze
58
+ OPAQUE_NODES = [
59
+ Prism::SingletonClassNode, Prism::SuperNode, Prism::ForwardingSuperNode, Prism::YieldNode,
60
+ Prism::ForwardingArgumentsNode, Prism::BlockArgumentNode
61
+ ].freeze
62
+ private_constant :MIXIN_CALLS, :OPAQUE_CALLS, :OPAQUE_NODES
63
+
64
+ # Whether an `included` hook may define a singleton method on its includer. The hook that only mixes constant
65
+ # modules into it (`base.extend(ClassMethods)`, past guards such as `raise "no" unless base`) cannot, and
66
+ # withholding for it would leave every inherited `def self.x` and every hook-extended method untyped. Any
67
+ # other hook may, however it reaches the includer, so the read is an allowlist: the includer, bound by the
68
+ # hook's one positional parameter ({.includer_names}), may only be read as the receiver of a mixin call whose
69
+ # arguments are constants, that takes no block and whose value nothing in the hook reads, or as an
70
+ # `if`/`unless` predicate; and nothing in the body or a parameter default may be an opaque call or node
71
+ # ({OPAQUE_CALLS}, {OPAQUE_NODES}) or a `def` with a receiver. A hook read from neither a `def` nor a
72
+ # `define_singleton_method` block may define, and so may one whose includer no single name binds (#3451).
73
+ def may_define_singleton?(hook)
74
+ parameters, body = hook_shape(hook)
75
+ return true if parameters == :unknown
76
+
77
+ includer = includer_names(parameters)
78
+ return true if includer.nil?
79
+
80
+ defaults = parameters ? [*parameters.optionals.map(&:value), *keyword_defaults(parameters)] : []
81
+ defaults.any? { |node| opaque?(node, includer) } || opaque?(body, includer, used: false)
82
+ end
83
+
84
+ def hook_shape(hook)
85
+ case hook
86
+ when Prism::DefNode then [hook.parameters, hook.body]
87
+ when Prism::CallNode
88
+ block = hook.block
89
+ return :unknown unless hook.name == :define_singleton_method && block.is_a?(Prism::BlockNode)
90
+
91
+ [block_parameters(block), block.body]
92
+ when Prism::BlockNode, Prism::LambdaNode then [block_parameters(hook), hook.body]
93
+ else :unknown
94
+ end
95
+ end
96
+
97
+ # A block's parameter list, nil for a block with none, or `:unknown` for `it` and `_1`
98
+ # (`ItParametersNode`, `NumberedParametersNode`), which bind the includer under no parameter name (#3451).
99
+ def block_parameters(block)
100
+ case block.parameters
101
+ when nil then nil
102
+ when Prism::BlockParametersNode then block.parameters.parameters
103
+ else :unknown
104
+ end
105
+ end
106
+
107
+ POSITIONAL_INCLUDERS = [
108
+ Prism::RequiredParameterNode, Prism::OptionalParameterNode, Prism::RestParameterNode
109
+ ].freeze
110
+ private_constant :POSITIONAL_INCLUDERS
111
+
112
+ # The names the includer is bound to: none for a hook without positional parameters, else the name of its one
113
+ # positional parameter, which must be a required, optional or named rest one. Any other positional list (a
114
+ # post, an anonymous `*`, a destructure, a second positional) binds it where the walk does not look, and
115
+ # answers nil (#3451). Keywords, a keyword rest and a block parameter never receive it.
116
+ def includer_names(parameters)
117
+ return [] if parameters.nil?
118
+
119
+ positional = [*parameters.requireds, *parameters.optionals, parameters.rest, *parameters.posts].compact
120
+ return [] if positional.empty?
121
+ return nil unless positional.one?
122
+
123
+ sole = positional.first
124
+ POSITIONAL_INCLUDERS.any? { |kind| sole.is_a?(kind) } && sole.name ? [sole.name] : nil
125
+ end
126
+
127
+ def keyword_defaults(parameters)
128
+ parameters.keywords.filter_map { |keyword| keyword.value if keyword.is_a?(Prism::OptionalKeywordParameterNode) }
129
+ end
130
+
131
+ # `used:` says whether the hook may read `node`'s value. `extend`, `include` and `prepend` return their
132
+ # receiver, so a mixin call whose value is read hands the includer on (`base.extend(M).cattr(:x)`, `k =
133
+ # base.extend(M)`) and is opaque (#3451). Only a statement's value, passed up through the statement lists and
134
+ # branches that end the hook, is known to be dropped; every other position counts as read.
135
+ def opaque?(node, includer, used: true)
136
+ return false unless node.is_a?(Prism::Node)
137
+
138
+ case node
139
+ when *OPAQUE_NODES then true
140
+ when Prism::DefNode then !node.receiver.nil? || opaque?(node.body, includer)
141
+ when Prism::LocalVariableReadNode then includer.include?(node.name)
142
+ when Prism::StatementsNode then statements_opaque?(node, includer, used)
143
+ when Prism::ParenthesesNode then opaque?(node.body, includer, used: used)
144
+ when Prism::ElseNode then opaque?(node.statements, includer, used: used)
145
+ when Prism::IfNode, Prism::UnlessNode then conditional_opaque?(node, includer, used)
146
+ when Prism::CallNode then call_opaque?(node, includer, used)
147
+ else node.compact_child_nodes.any? { |child| opaque?(child, includer) }
148
+ end
149
+ end
150
+
151
+ def statements_opaque?(node, includer, used)
152
+ last = node.body.last
153
+ node.body.any? { |child| opaque?(child, includer, used: used && child.equal?(last)) }
154
+ end
155
+
156
+ def conditional_opaque?(node, includer, used)
157
+ predicate = node.predicate
158
+ bare = predicate.is_a?(Prism::LocalVariableReadNode) && includer.include?(predicate.name)
159
+ node.compact_child_nodes.any? do |child|
160
+ next false if bare && child.equal?(predicate)
161
+
162
+ opaque?(child, includer, used: child.equal?(predicate) || used)
163
+ end
164
+ end
165
+
166
+ def call_opaque?(call, includer, used)
167
+ return true if OPAQUE_CALLS.include?(call.name)
168
+ return used || mixin_opaque?(call) if includer_receiver?(call, includer) && MIXIN_CALLS.include?(call.name)
169
+
170
+ call.compact_child_nodes.any? { |child| opaque?(child, includer) }
171
+ end
172
+
173
+ def includer_receiver?(call, includer)
174
+ call.receiver.is_a?(Prism::LocalVariableReadNode) && includer.include?(call.receiver.name)
175
+ end
176
+
177
+ def mixin_opaque?(call)
178
+ return true if call.block
179
+
180
+ arguments = call.arguments&.arguments || []
181
+ arguments.empty? || !arguments.all? { |arg| arg.is_a?(Prism::ConstantReadNode) || arg.is_a?(Prism::ConstantPathNode) }
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,365 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../flow_contribution"
6
+ require_relative "../../rbs_extended"
7
+ require_relative "../../reflection"
8
+ require_relative "../../source/constant_path"
9
+ require_relative "../../type"
10
+ require_relative "../narrowing"
11
+
12
+ module Rigor
13
+ module Inference
14
+ class StatementEvaluator
15
+ module DefArgumentsAndAssertions # rubocop:disable Metrics/ModuleLength
16
+ module_function
17
+
18
+ def evaluate_def_arguments(call_node, evaluator)
19
+ args = call_node.arguments
20
+ return unless args.respond_to?(:arguments)
21
+
22
+ args.arguments.each do |arg|
23
+ evaluator.eval_def(arg) if arg.is_a?(Prism::DefNode)
24
+ end
25
+ end
26
+
27
+ # Recognises a small catalogue of RSpec matcher patterns as assert-shaped narrows on the local passed to
28
+ # `expect(...)`. The pattern is matched purely on AST shape; no RBS for RSpec is required (and none is shipped
29
+ # today).
30
+ #
31
+ # Recognised today:
32
+ #
33
+ # expect(x).not_to(be_nil)
34
+ # expect(x).to_not(be_nil)
35
+ # → narrow `x` AWAY from `NilClass`.
36
+ #
37
+ # expect(x).to(be_a(C))
38
+ # expect(x).to(be_kind_of(C))
39
+ # expect(x).to(be_an_instance_of(C))
40
+ # expect(x).to(be_instance_of(C))
41
+ # → narrow `x` to `C` (exact for the two
42
+ # `*instance_of` matchers, subtype-permitting
43
+ # otherwise).
44
+ #
45
+ # Anything else is silently passed through. Symmetric negative class assertions (`not_to be_a(C)`) and narrowing
46
+ # TO `NilClass` are intentionally NOT modelled: they are rarely useful in practice and risk masking bugs if the
47
+ # assertion later fails.
48
+ def apply_rspec_matcher_narrowing(call_node, current_scope)
49
+ narrow = rspec_matcher_narrowing_request(call_node)
50
+ return current_scope if narrow.nil?
51
+
52
+ local_name = narrow.fetch(:local)
53
+ current_type = current_scope.local(local_name)
54
+ return current_scope if current_type.nil?
55
+
56
+ narrowed = apply_rspec_narrow(current_type, narrow, current_scope.environment)
57
+ current_scope.with_local(local_name, narrowed)
58
+ end
59
+
60
+ # Decodes an `expect(x).<chain>` outer call into a narrowing request hash, or `nil` when the shape is not
61
+ # recognised. The hash carries `:local` (the local name being narrowed) plus the narrowing parameters.
62
+ def rspec_matcher_narrowing_request(call_node)
63
+ local_name = rspec_expectation_target(call_node)
64
+ return nil if local_name.nil?
65
+
66
+ case call_node.name
67
+ when :not_to, :to_not
68
+ rspec_negative_narrow(call_node, local_name)
69
+ when :to
70
+ rspec_positive_narrow(call_node, local_name)
71
+ end
72
+ end
73
+
74
+ def rspec_negative_narrow(call_node, local_name)
75
+ return nil unless rspec_matcher_argument?(call_node, :be_nil)
76
+
77
+ { local: local_name, kind: :not_class, class_name: "NilClass", exact: false }
78
+ end
79
+
80
+ def rspec_positive_narrow(call_node, local_name)
81
+ matcher = rspec_matcher_node(call_node)
82
+ return nil if matcher.nil?
83
+
84
+ case matcher.name
85
+ when :be_a, :be_kind_of
86
+ rspec_be_a_narrow(matcher, local_name, exact: false)
87
+ when :be_an_instance_of, :be_instance_of
88
+ rspec_be_a_narrow(matcher, local_name, exact: true)
89
+ end
90
+ end
91
+
92
+ # `be_a` / `be_kind_of` / `be_an_instance_of` accept a single class argument — either a `ConstantReadNode`
93
+ # (`Integer`) or a `ConstantPathNode` (`Rigor::Type::Nominal`).
94
+ def rspec_be_a_narrow(matcher, local_name, exact:)
95
+ args = matcher.arguments&.arguments || []
96
+ return nil unless args.size == 1
97
+
98
+ class_name = Source::ConstantPath.qualified_name_or_nil(args.first)
99
+ return nil if class_name.nil?
100
+
101
+ { local: local_name, kind: :class, class_name: class_name, exact: exact }
102
+ end
103
+
104
+ def apply_rspec_narrow(current_type, narrow, environment)
105
+ case narrow.fetch(:kind)
106
+ when :not_class
107
+ Narrowing.narrow_not_class(current_type, narrow.fetch(:class_name),
108
+ exact: narrow.fetch(:exact), environment: environment)
109
+ when :class
110
+ Narrowing.narrow_class(current_type, narrow.fetch(:class_name),
111
+ exact: narrow.fetch(:exact), environment: environment)
112
+ end
113
+ end
114
+
115
+ # Returns the local name passed to `expect(...)` when the receiver chain matches `expect(<local>)` exactly, or
116
+ # nil otherwise. Centralised so each per-matcher decoder can short-circuit on a non-matching outer call.
117
+ def rspec_expectation_target(call_node)
118
+ receiver = call_node.receiver
119
+ return nil unless receiver.is_a?(Prism::CallNode) && receiver.name == :expect
120
+ return nil unless receiver.receiver.nil?
121
+
122
+ args = receiver.arguments&.arguments || []
123
+ return nil unless args.size == 1
124
+
125
+ target = args.first
126
+ target.is_a?(Prism::LocalVariableReadNode) ? target.name : nil
127
+ end
128
+
129
+ def rspec_matcher_node(call_node)
130
+ args = call_node.arguments&.arguments || []
131
+ return nil unless args.size == 1
132
+
133
+ matcher = args.first
134
+ return nil unless matcher.is_a?(Prism::CallNode) && matcher.receiver.nil? && matcher.block.nil?
135
+
136
+ matcher
137
+ end
138
+
139
+ # True when `call_node`'s sole argument is an implicit-self matcher call with the given name and no positional
140
+ # arguments — used by the no-arg matchers (`be_nil`).
141
+ def rspec_matcher_argument?(call_node, matcher_name)
142
+ matcher = rspec_matcher_node(call_node)
143
+ return false if matcher.nil?
144
+ return false unless matcher.name == matcher_name
145
+
146
+ matcher.arguments.nil? || matcher.arguments.arguments.empty?
147
+ end
148
+
149
+ # Slice 4b-2 (ADR-7 § "Slice 4-A/4-B") — applies the post-return facts the merger produces for an
150
+ # `RBS::Extended`-annotated call. Reads through `RbsExtended.read_flow_contribution` so the bundle carries the
151
+ # canonical `Rigor::FlowContribution::Fact` rows for `:always` assert directives (the slice-4a routing places
152
+ # conditional asserts on `truthy_facts` / `falsey_facts`, which `Narrowing.predicate_scopes` consumes). Plugin
153
+ # `:always` assertions are handled by the sibling `apply_plugin_assertions`, not this path.
154
+ def apply_rbs_extended_assertions(call_node, current_scope, tracer)
155
+ method_def = resolve_call_method(call_node, current_scope, tracer)
156
+ return current_scope if method_def.nil?
157
+
158
+ contribution = RbsExtended.read_flow_contribution(method_def, environment: current_scope.environment)
159
+ return current_scope if contribution.nil?
160
+
161
+ result = Rigor::FlowContribution::Merger.merge([contribution])
162
+ post_return = result.post_return_facts
163
+ return current_scope if post_return.empty?
164
+
165
+ post_return.reduce(current_scope) do |scope_acc, fact|
166
+ apply_post_return_fact(fact, call_node, scope_acc, method_def)
167
+ end
168
+ end
169
+
170
+ # ADR-7 § "Slice 4-A" / T.bind priority slice 2 — applies the post-return facts plugin contributions produce.
171
+ # This is the sibling of {apply_rbs_extended_assertions}: the carrier (`Rigor::FlowContribution::Fact`) and the
172
+ # downstream narrowing path (`apply_post_return_fact` → `apply_self_post_return_fact`) are the same; only the
173
+ # *source* of the bundle changes (RBS::Extended vs the registered plugins' `flow_contribution_for`).
174
+ #
175
+ # `:self`-targeted facts narrow `scope.self_type` for the surrounding scope. In a block body, the surrounding
176
+ # scope is the block's own scope, so the narrowing applies to the rest of the block — exactly the contract
177
+ # Sorbet's `T.bind(self, T)` commits to.
178
+ #
179
+ # `:parameter`-targeted facts only land when the called method has an authoritative RBS sig (via
180
+ # `resolve_call_method`); plugins recognising their own synthetic call shapes (e.g. `T.assert_type!`) have no
181
+ # method_def and the parameter facts silently skip — the plugin's own diagnostics_for_file path covers those
182
+ # cases. The full plugin-side parameter-targeting story (PHPStan-style Type-Specifying Extensions on
183
+ # plugin-recognised calls) lives behind a follow-up slice that introduces an `:argument_at` target kind.
184
+ def apply_plugin_assertions(call_node, current_scope, tracer)
185
+ registry = current_scope.environment&.plugin_registry
186
+ return current_scope if registry.nil? || registry.empty?
187
+
188
+ contributions = collect_plugin_contributions(registry, call_node, current_scope)
189
+ return current_scope if contributions.empty?
190
+
191
+ result = Rigor::FlowContribution::Merger.merge(contributions)
192
+ post_return = result.post_return_facts
193
+ return current_scope if post_return.empty?
194
+
195
+ method_def = resolve_call_method(call_node, current_scope, tracer)
196
+ post_return.reduce(current_scope) do |scope_acc, fact|
197
+ apply_post_return_fact(fact, call_node, scope_acc, method_def)
198
+ end
199
+ end
200
+
201
+ EMPTY_CONTRIBUTIONS = [].freeze
202
+ private_constant :EMPTY_CONTRIBUTIONS
203
+
204
+ # ADR-37 slice 2 / ADR-52 WD3 — gathers each plugin's post-return narrowing from the method-gated
205
+ # `narrowing_facts` DSL, wrapped as a facts-only `FlowContribution`, swallowing per-plugin exceptions so a buggy
206
+ # plugin can't abort the assertion path.
207
+ #
208
+ # Fast-exit guard: skip if no plugin declares a `narrowing_facts` rule, or if no registered method-name gate
209
+ # matches the call. See `collect_gated_statement_contributions` for the full consultation.
210
+ def collect_plugin_contributions(registry, call_node, current_scope)
211
+ index = registry.contribution_index
212
+ relevant = index.for_statement
213
+ return EMPTY_CONTRIBUTIONS if relevant.empty?
214
+
215
+ name = call_node.respond_to?(:name) ? call_node.name : nil
216
+ return EMPTY_CONTRIBUTIONS unless index.statement_candidate?(name)
217
+
218
+ collect_gated_statement_contributions(index, relevant, name, call_node, current_scope)
219
+ end
220
+
221
+ # ADR-37 slice 2 / ADR-52 WD1 — post-gate walk in registry order. Visits only plugins in `for_statement`
222
+ # (declare a `narrowing_facts` rule), further gated by the method-name Set probe so the common no-candidate case
223
+ # is a single lookup. Accumulates lazily; caller is read-only.
224
+ def collect_gated_statement_contributions(index, relevant, name, call_node, current_scope)
225
+ result = nil
226
+ relevant.each do |plugin|
227
+ next unless index.narrowing_facts_candidate_for?(plugin, name)
228
+
229
+ facts = plugin.narrowing_facts_for(call_node: call_node, scope: current_scope)
230
+ (result ||= []) << Rigor::FlowContribution.new(post_return_facts: facts) if facts && !facts.empty?
231
+ rescue StandardError
232
+ next
233
+ end
234
+ result || EMPTY_CONTRIBUTIONS
235
+ end
236
+
237
+ def resolve_call_method(call_node, current_scope, tracer)
238
+ receiver_node = call_node.receiver
239
+ receiver_type =
240
+ if receiver_node
241
+ current_scope.type_of(receiver_node, tracer: tracer)
242
+ else
243
+ current_scope.self_type
244
+ end
245
+ return nil if receiver_type.nil?
246
+
247
+ class_name = assertion_class_name(receiver_type)
248
+ return nil if class_name.nil?
249
+ return nil unless Rigor::Reflection.rbs_class_known?(class_name, scope: current_scope)
250
+
251
+ if receiver_type.is_a?(Type::Singleton)
252
+ Rigor::Reflection.singleton_method_definition(class_name, call_node.name, scope: current_scope)
253
+ else
254
+ Rigor::Reflection.instance_method_definition(class_name, call_node.name, scope: current_scope)
255
+ end
256
+ rescue StandardError
257
+ nil
258
+ end
259
+
260
+ def assertion_class_name(receiver_type)
261
+ case receiver_type
262
+ when Type::Nominal, Type::Singleton then receiver_type.class_name
263
+ end
264
+ end
265
+
266
+ # Slice 4b-2 — applies a single post-return Fact to the scope. Mirrors `Narrowing#apply_fact_to_scope`;
267
+ # shares the narrowing logic via `Narrowing.narrow_for_fact` so the
268
+ # predicate / assert / plugin paths all converge on the same hierarchy-aware narrowing rules.
269
+ #
270
+ # v0.1.8 Pillar 2 Slice 1 added the `:local` target_kind branch so plugins recognising bespoke call shapes
271
+ # (`expect(x).to be_a(T)`) can directly narrow a named local in the surrounding scope, bypassing the
272
+ # parameter-name lookup that requires an authoritative RBS sig on the called method (which RSpec matchers lack).
273
+ def apply_post_return_fact(fact, call_node, current_scope, method_def)
274
+ return apply_local_post_return_fact(fact, current_scope) if fact.target_kind == :local
275
+
276
+ target_node = fact_target_node(fact, call_node, method_def)
277
+ return apply_self_post_return_fact(fact, target_node, current_scope) if fact.target_kind == :self
278
+ return current_scope unless target_node.is_a?(Prism::LocalVariableReadNode)
279
+
280
+ local_name = target_node.name
281
+ current_type = current_scope.local(local_name)
282
+ return current_scope if current_type.nil?
283
+
284
+ narrowed = Narrowing.narrow_for_fact(current_type, fact, current_scope.environment)
285
+ current_scope.with_local(local_name, narrowed)
286
+ end
287
+
288
+ # v0.1.8 Pillar 2 Slice 1 — narrows the named local directly without consulting the call node's argument list.
289
+ # The fact's `target_name` is the local-variable name as written in source. Silently no-ops when the local is
290
+ # unbound in the current scope (the plugin's named local may have already gone out of scope when the
291
+ # contribution fires).
292
+ def apply_local_post_return_fact(fact, current_scope)
293
+ local_name = fact.target_name
294
+ current_type = current_scope.local(local_name)
295
+ return current_scope if current_type.nil?
296
+
297
+ narrowed = Narrowing.narrow_for_fact(current_type, fact, current_scope.environment)
298
+ current_scope.with_local(local_name, narrowed)
299
+ end
300
+
301
+ # v0.1.1 Track 1 slice 3 — `assert self is T` post-return narrowing for the four supported receiver shapes
302
+ # (mirrors `Narrowing#apply_self_fact`).
303
+ def apply_self_post_return_fact(fact, receiver_node, current_scope)
304
+ case receiver_node
305
+ when nil, Prism::SelfNode
306
+ current = current_scope.self_type
307
+ return current_scope if current.nil?
308
+
309
+ narrowed = Narrowing.narrow_for_fact(current, fact, current_scope.environment)
310
+ current_scope.with_self_type(narrowed)
311
+ when Prism::LocalVariableReadNode
312
+ current = current_scope.local(receiver_node.name)
313
+ return current_scope if current.nil?
314
+
315
+ narrowed = Narrowing.narrow_for_fact(current, fact, current_scope.environment)
316
+ current_scope.with_local(receiver_node.name, narrowed)
317
+ when Prism::InstanceVariableReadNode
318
+ current = current_scope.ivar(receiver_node.name)
319
+ return current_scope if current.nil?
320
+
321
+ narrowed = Narrowing.narrow_for_fact(current, fact, current_scope.environment)
322
+ current_scope.with_ivar(receiver_node.name, narrowed)
323
+ else
324
+ current_scope
325
+ end
326
+ end
327
+
328
+ # `:self` routes to the call receiver; otherwise we look up the matching positional argument by parameter name.
329
+ def fact_target_node(fact, call_node, method_def)
330
+ if fact.target_kind == :self
331
+ call_node.receiver
332
+ else
333
+ lookup_post_return_arg(call_node, method_def, fact.target_name)
334
+ end
335
+ end
336
+
337
+ def lookup_post_return_arg(call_node, method_def, target_name)
338
+ # Plugin-source contributions arrive without an authoritative method_def (the plugin recognised the call shape
339
+ # directly). Parameter-targeting falls back to "no narrow" in that case — the wider plugin-side parameter
340
+ # mapping (`:argument_at`) is a follow-up slice.
341
+ return nil if method_def.nil?
342
+
343
+ arguments = call_node.arguments&.arguments || []
344
+ # A `(?)` overload (`RBS::Types::UntypedFunction`, #1430) names no parameter and accepts any argument list,
345
+ # so a call may reach it with the target's position holding anything: no overload proves the target.
346
+ return nil unless method_def.method_types.all? { |mt| mt.type.respond_to?(:required_positionals) }
347
+
348
+ method_def.method_types.each do |mt|
349
+ params = mt.type.required_positionals + mt.type.optional_positionals
350
+ index = params.find_index { |param| param.name == target_name }
351
+ return arguments[index] if index && arguments[index]
352
+ end
353
+ nil
354
+ end
355
+
356
+ private_class_method :rspec_matcher_narrowing_request, :rspec_negative_narrow, :rspec_positive_narrow,
357
+ :rspec_be_a_narrow, :apply_rspec_narrow, :rspec_expectation_target, :rspec_matcher_node,
358
+ :rspec_matcher_argument?, :collect_plugin_contributions,
359
+ :collect_gated_statement_contributions, :resolve_call_method, :assertion_class_name,
360
+ :apply_post_return_fact, :apply_local_post_return_fact, :apply_self_post_return_fact,
361
+ :fact_target_node, :lookup_post_return_arg
362
+ end
363
+ end
364
+ end
365
+ end