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
@@ -0,0 +1,581 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../reflection"
6
+ require_relative "../../type"
7
+ require_relative "../definer_resolution"
8
+ require_relative "../error_info"
9
+ require_relative "../method_dispatcher"
10
+ require_relative "../multi_target_binder"
11
+ require_relative "../narrowing"
12
+
13
+ module Rigor
14
+ module Inference
15
+ class StatementEvaluator
16
+ module PatternBindings # rubocop:disable Metrics/ModuleLength
17
+ module_function
18
+
19
+ # --------------------------------------------------------------- rescue variable binding helpers
20
+ # ---------------------------------------------------------------
21
+
22
+ # Returns `scope` extended with the rescue reference variable bound to the exception instance type. Leaves scope
23
+ # unchanged when the node carries no reference (bare `rescue` without `=> var`). An index-target reference
24
+ # (`rescue => h[:e]`) stores the exception through `[]=` instead, so its receiver widens with the exception
25
+ # instance type as the stored value, exactly as `rescue => e; h[:e] = e` widens it.
26
+ #
27
+ # Issue #1360 — with or without a reference, the clause runs with `$!` bound to the exception it rescued, `$@`
28
+ # to its backtrace and `$?` unbound ({ErrorInfo.rescue_entry}); {#eval_begin_node} restores `$!` and `$@` once
29
+ # the `begin` exits.
30
+ def bind_rescue_reference(rescue_node, scope, evaluator)
31
+ exception_type = rescue_exception_type(rescue_node, scope, evaluator)
32
+ ref = rescue_node.reference
33
+ reference = ref.name if ref.is_a?(Prism::LocalVariableTargetNode)
34
+ scope = ErrorInfo.rescue_entry(scope, exception_type, rescue_node.statements, reference)
35
+ case ref
36
+ when Prism::LocalVariableTargetNode
37
+ scope.with_local(ref.name, exception_type)
38
+ when Prism::InstanceVariableTargetNode
39
+ # Issue #1446 — `rescue => @io` writes the ivar, which ends a class guard's narrowing of it, and `rescue =>
40
+ # $out` the global, which ends any guard's narrowing of it.
41
+ scope.with_ivar(ref.name, exception_type).without_ivar_guard(ref.name)
42
+ when Prism::GlobalVariableTargetNode
43
+ scope.with_global(ref.name, exception_type)
44
+ when Prism::ClassVariableTargetNode
45
+ scope.with_cvar(ref.name, exception_type)
46
+ when Prism::IndexTargetNode
47
+ evaluator.widen_index_target(ref, exception_type, scope, type_scope: scope)
48
+ when Prism::CallTargetNode
49
+ # Issue #3205 — `rescue => x.r` calls `r=` on `x`, as `for x.r in` does (issue #2693).
50
+ evaluator.widen_attribute_write(ref.receiver, ref.name, scope)
51
+ else
52
+ scope
53
+ end
54
+ end
55
+
56
+ # Derives the exception instance type for a `RescueNode` ({ErrorInfo.rescue_exception_type}): `StandardError`
57
+ # for a bare `rescue`, else the union of the named classes' instances, with `Dynamic[top]` for one that does not
58
+ # resolve to a `Singleton`.
59
+ def rescue_exception_type(rescue_node, scope, evaluator)
60
+ ErrorInfo.rescue_exception_type(rescue_node) do |exception_node|
61
+ evaluator.sub_eval(exception_node, scope).first
62
+ end
63
+ end
64
+
65
+ # --------------------------------------------------------------- `case/in` pattern variable binding helpers
66
+ # ---------------------------------------------------------------
67
+
68
+ # Builds the entry scope for an `in` branch by injecting every variable captured by the pattern as a local
69
+ # binding. `subject_type` is the type of the `case` subject — nil when the `case` carries no predicate, which
70
+ # leaves every binding at the `Dynamic[top]` floor — and `subject_node` the subject expression, which the
71
+ # `deconstruct` / `deconstruct_keys` dispatch reads for its freshness gate.
72
+ def apply_in_pattern_bindings(subject_type, subject_node, pattern, scope, evaluator)
73
+ bindings = collect_in_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node: subject_node)
74
+ bound = bindings.reduce(scope) { |s, (name, type)| s.with_local(name, type) }
75
+ marked = subject_node.is_a?(Prism::LocalVariableReadNode) && scope.gradual_narrowing?(subject_node.name)
76
+ mark_gradual_class_capture(subject_type, pattern, bound, marked: marked)
77
+ end
78
+
79
+ # Issue #2656 — `in C => y` over a `Dynamic` subject binds `y` to `C`'s `Nominal`, as a class guard's edge binds
80
+ # the subject, and marks it as that edge marks a local ({Narrowing.mark_gradual_local}). Only a capture of
81
+ # the whole subject by a class or an alternation of classes is marked.
82
+ def mark_gradual_class_capture(subject_type, pattern, scope, marked: false)
83
+ return scope unless pattern.is_a?(Prism::CapturePatternNode) && class_capture_value?(pattern.value)
84
+
85
+ name = pattern.target.name
86
+ Narrowing.mark_gradual_local(scope, name, subject_type, scope.local(name), marked: marked)
87
+ end
88
+
89
+ def class_capture_value?(value)
90
+ case value
91
+ when Prism::ConstantReadNode, Prism::ConstantPathNode then true
92
+ when Prism::AlternationPatternNode then class_capture_value?(value.left) && class_capture_value?(value.right)
93
+ else false
94
+ end
95
+ end
96
+
97
+ # Returns an array of `[Symbol, Rigor::Type]` pairs for every variable captured by `pattern`, typed against the
98
+ # subject. Unrecognised pattern nodes contribute no bindings (fail-soft).
99
+ #
100
+ # A union subject distributes first (see {#collect_union_pattern_bindings}); every other subject walks the
101
+ # pattern once, and each node kind decides how much of the subject it can name:
102
+ #
103
+ # - a bare target (`in [i, s]`, `in x`) binds the slot the enclosing pattern hands it,
104
+ # - a capture (`Integer => i`, `[a, b] => whole`) binds its own constraint, and recurses into the captured
105
+ # pattern,
106
+ # - an array / find / hash pattern decomposes the subject (see the three collectors below).
107
+ def collect_in_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node: nil)
108
+ if subject_type.is_a?(Type::Union)
109
+ return collect_union_pattern_bindings(subject_type.members, pattern, scope, evaluator,
110
+ subject_node: subject_node)
111
+ end
112
+
113
+ case pattern
114
+ when Prism::CapturePatternNode
115
+ collect_capture_pattern_bindings(subject_type, pattern, scope, evaluator)
116
+ when Prism::LocalVariableTargetNode
117
+ [[pattern.name, subject_type || Type::Combinator.untyped]]
118
+ when Prism::ImplicitNode
119
+ collect_in_pattern_bindings(subject_type, pattern.value, scope, evaluator)
120
+ when Prism::ArrayPatternNode
121
+ collect_array_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node: subject_node)
122
+ when Prism::FindPatternNode
123
+ collect_find_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node: subject_node)
124
+ when Prism::HashPatternNode
125
+ collect_hash_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node: subject_node)
126
+ when Prism::AlternationPatternNode
127
+ collect_alternation_pattern_bindings(subject_type, pattern, scope, evaluator)
128
+ else
129
+ []
130
+ end
131
+ end
132
+
133
+ # `pattern => target` binds `target` to what the pattern matched AND every name the pattern itself binds:
134
+ # `in [a, b] => whole` binds `a`, `b` and `whole`. The target's own type is the pattern's constraint when it
135
+ # names a class (`Integer => i`), and the subject otherwise — a capture over any other pattern IS the subject
136
+ # the pattern matched.
137
+ def collect_capture_pattern_bindings(subject_type, pattern, scope, evaluator)
138
+ target = pattern.target
139
+ inner = collect_in_pattern_bindings(subject_type, pattern.value, scope, evaluator)
140
+ return inner unless target.is_a?(Prism::LocalVariableTargetNode)
141
+
142
+ [[target.name, capture_pattern_type(subject_type, pattern.value, scope, evaluator)]] + inner
143
+ end
144
+
145
+ # `in [i, s]` / `in [a, *rest, z]` / `in Foo[a, b]`.
146
+ def collect_array_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node:)
147
+ subject_type = pattern_class_constraint(subject_type, pattern.constant, scope, evaluator)
148
+ fronts, rest_type, backs = pattern_slot_types(subject_type, pattern, scope, subject_node)
149
+ bindings = pattern.requireds.each_with_index.flat_map do |elem, i|
150
+ collect_in_pattern_bindings(fronts[i], elem, scope, evaluator)
151
+ end
152
+ bindings += pattern.posts.each_with_index.flat_map do |elem, i|
153
+ collect_in_pattern_bindings(backs[i], elem, scope, evaluator)
154
+ end
155
+ append_array_splat_binding(bindings, pattern.rest, rest_type)
156
+ bindings
157
+ end
158
+
159
+ # The per-slot types a positional pattern reads: the subject's own decomposition when it is a carrier
160
+ # {MultiTargetBinder.decompose_slots} accepts (`Tuple`, `Array[T]`), else the `deconstruct` projection of a
161
+ # subject that defines one (`Struct#deconstruct`, a `Data` instance, a class whose `deconstruct` names the
162
+ # parts), else the `Dynamic[top]` floor per slot.
163
+ def pattern_slot_types(subject_type, pattern, scope, subject_node)
164
+ view = positional_pattern_view(subject_type, scope, subject_node)
165
+ return floor_pattern_slots(pattern.requireds.size, pattern.posts.size, !pattern.rest.nil?) if view.nil?
166
+
167
+ MultiTargetBinder.decompose_slots(
168
+ view, front_count: pattern.requireds.size, back_count: pattern.posts.size,
169
+ rest_present: !pattern.rest.nil?, scope: scope
170
+ )
171
+ end
172
+
173
+ # The carrier a positional pattern decomposes: the subject itself when {MultiTargetBinder} already accepts it,
174
+ # else what the subject's `deconstruct` answers with (a `Tuple` / `Array[T]` carrier), else nil.
175
+ #
176
+ # `subject_node` rides along as the dispatch's call node so the `Struct` fold's freshness gate can see the
177
+ # receiver: `case Point.new(1, 2); in [x, y]` is a freshly materialised instance and folds, while a stored
178
+ # binding that may have been mutated since does not (ADR-48).
179
+ def positional_pattern_view(subject_type, scope, subject_node)
180
+ return subject_type if subject_type.is_a?(Type::Tuple)
181
+ return subject_type if MultiTargetBinder.array_element_type(subject_type)
182
+
183
+ deconstruct_projection(subject_type, scope, subject_node)
184
+ end
185
+
186
+ # What `subject.deconstruct` answers with, when that is a carrier this binder decomposes; nil otherwise —
187
+ # an absent method, or `Struct#deconstruct`'s RBS `Array[untyped]` for a class whose members are not known.
188
+ def deconstruct_projection(subject_type, scope, subject_node)
189
+ result = struct_instance_projection(subject_type, :deconstruct, scope, subject_node) ||
190
+ pattern_decomposition_dispatch(subject_type, :deconstruct, [], scope) ||
191
+ source_decomposition_projection(subject_type, :deconstruct, [], scope)
192
+ return nil if result.nil?
193
+ return result if result.is_a?(Type::Tuple) || MultiTargetBinder.array_element_type(result)
194
+
195
+ nil
196
+ end
197
+
198
+ # The inferred return type of a project-defined `deconstruct` / `deconstruct_keys` (issue #1122). The
199
+ # dispatcher answers nil for a class no RBS describes: the body-inference tier that would type
200
+ # `subject.deconstruct` lives on `ExpressionTyper` and needs the call NODE a pattern does not have, so
201
+ # this asks the scope's own entry point for the same answer a resolved call site gets (ADR-84 memo
202
+ # included). nil when the project defines no such method, or when the body's answer is the gradual floor.
203
+ def source_decomposition_projection(subject_type, method_name, arg_types, scope)
204
+ return nil unless subject_type.is_a?(Type::Nominal)
205
+
206
+ def_node = own_definer_node(scope, subject_type.class_name, method_name)
207
+ return nil if def_node.nil?
208
+
209
+ result = scope.user_method_return(def_node, subject_type, arg_types)
210
+ return nil if result.nil? || result.is_a?(Type::Dynamic) || result.is_a?(Type::Top)
211
+
212
+ result
213
+ end
214
+
215
+ # The `def` node the receiver's OWN class defines `method_name` with, or nil. ADR-119 C2-e (#1615): asked of
216
+ # `DefinerResolution`, so a slot rebuilt from an ADR-85 seed bundle is resolved to a node and the read files
217
+ # its ADR-46 edge. UNKNOWN answers nil: an unsettled or forked chain, or a contested slot once ADR-119 C1d
218
+ # fills the siblings. Conditional or reopened defs are not contested until C1d. A definer further up the
219
+ # chain (a prepended or included module, a superclass) is not the own class's, so the owner check declines it.
220
+ def own_definer_node(scope, class_name, method_name)
221
+ case DefinerResolution.resolve(scope, class_name, method_name, :instance, question: :definer)
222
+ in DefinerResolution::Known(answer: [node, owner])
223
+ owner == class_name ? node : nil
224
+ in DefinerResolution::UNKNOWN # the case/in contract needs one arm per answer
225
+ nil
226
+ in DefinerResolution::ABSENT # rubocop:disable Lint/DuplicateBranch -- see above
227
+ nil
228
+ end
229
+ end
230
+
231
+ # A `StructInstance`'s own projection — `Tuple` of its member values for `deconstruct`, `HashShape` of
232
+ # its members for `deconstruct_keys` — or nil when the subject is another carrier or the projection would
233
+ # be unsound.
234
+ #
235
+ # The `Struct` fold's freshness gate cannot answer for a pattern through the dispatcher: it asks whether
236
+ # the CALL's receiver was freshly materialised (`Point.new(1, 2).x`), while a pattern's subject node IS
237
+ # that materialisation (`case Point.new(1, 2); in [x, y]`). The gate still applies — a `Struct` is
238
+ # mutable, so a stored binding's member map may be stale (ADR-48) — so this asks it the pattern's own
239
+ # question, with the subject expression as the materialisation. A `Data` instance needs none of this:
240
+ # it is frozen, and the dispatcher already projects it (see `DataFolding`).
241
+ def struct_instance_projection(subject_type, method_name, scope, subject_node)
242
+ return nil unless subject_type.is_a?(Type::StructInstance)
243
+ return nil unless MethodDispatcher::StructMaterialization.materialization_call?(subject_node, scope)
244
+
245
+ case method_name
246
+ when :deconstruct then Type::Combinator.tuple_of(*subject_type.members.values)
247
+ when :deconstruct_keys then Type::Combinator.hash_shape_of(subject_type.members.dup)
248
+ end
249
+ end
250
+
251
+ # `deconstruct` / `deconstruct_keys` on a subject carrier. A `Dynamic` / `Top` answer is the gradual floor
252
+ # rather than a method's result, so it reads as "cannot ask" to every caller here.
253
+ #
254
+ # The subject node is deliberately NOT passed as the dispatch's `call_node`: the tiers read a call node's
255
+ # receiver and arguments, and a pattern's subject is any expression at all — `StructFolding`'s freshness
256
+ # gate dereferences `call_node.receiver`, which a local read or a literal does not answer. The one
257
+ # freshness question a pattern needs (`case Point.new(1, 2)`) is asked by
258
+ # {#struct_instance_projection} instead.
259
+ def pattern_decomposition_dispatch(subject_type, method_name, args, scope)
260
+ return nil if subject_type.nil?
261
+
262
+ result = MethodDispatcher.dispatch(
263
+ receiver_type: subject_type, method_name: method_name, arg_types: args,
264
+ environment: scope.environment, scope: scope
265
+ )
266
+ return nil if result.is_a?(Type::Dynamic) || result.is_a?(Type::Top)
267
+
268
+ result
269
+ end
270
+
271
+ # `in [*pre, m, *post]`. Ruby matches a find pattern's required elements at the EARLIEST position the
272
+ # surrounding splats allow (the pre-splat is non-greedy: `[1, 2, 3] in [*pre, x, *post]` binds `pre = []`, `x =
273
+ # 1`), but a required that does not match there slides right, so each required binds the union of every
274
+ # position it could occupy and the surrounding splats bind an `Array` of the subject's element type.
275
+ def collect_find_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node:)
276
+ subject_type = pattern_class_constraint(subject_type, pattern.constant, scope, evaluator)
277
+ view = positional_pattern_view(subject_type, scope, subject_node)
278
+ slots = find_pattern_slots(view, pattern.requireds.size)
279
+ bindings = pattern.requireds.each_with_index.flat_map do |elem, i|
280
+ collect_in_pattern_bindings(slots ? slots[i] : Type::Combinator.untyped, elem, scope, evaluator)
281
+ end
282
+ surround = find_pattern_surround_type(view)
283
+ [pattern.left, pattern.right].each { |splat| append_array_splat_binding(bindings, splat, surround) }
284
+ bindings
285
+ end
286
+
287
+ # The type each of a find pattern's `count` requireds can see, or nil when the subject cannot supply that many
288
+ # elements (the pattern cannot match).
289
+ def find_pattern_slots(view, count)
290
+ if view.is_a?(Type::Tuple)
291
+ elements = view.elements
292
+ return nil if elements.size < count
293
+
294
+ return Array.new(count) { |i| Type::Combinator.union(*elements[i..(elements.size - count + i)]) }
295
+ end
296
+
297
+ element = view && MultiTargetBinder.array_element_type(view)
298
+ element && Array.new(count) { element }
299
+ end
300
+
301
+ # `*pre` / `*post` capture the elements the requireds did not: `Array[T]` for an `Array[T]` subject, an `Array`
302
+ # of the element union for a `Tuple`, `Array[untyped]` when the subject could not be decomposed.
303
+ def find_pattern_surround_type(view)
304
+ element = case view
305
+ when Type::Tuple then union_of_types(view.elements)
306
+ else view && MultiTargetBinder.array_element_type(view)
307
+ end
308
+ Type::Combinator.nominal_of("Array", type_args: [element || Type::Combinator.untyped])
309
+ end
310
+
311
+ # `in {name: String => n}` / `in {name:, **rest}`. Each element reads the subject's value at its key through
312
+ # the subject's `deconstruct_keys` projection; `**rest` binds the remaining entries.
313
+ def collect_hash_pattern_bindings(subject_type, pattern, scope, evaluator, subject_node:)
314
+ subject_type = pattern_class_constraint(subject_type, pattern.constant, scope, evaluator)
315
+ view = hash_pattern_view(subject_type, scope, subject_node)
316
+ bindings = pattern.elements.flat_map do |assoc|
317
+ next [] unless assoc.is_a?(Prism::AssocNode) && assoc.value
318
+
319
+ collect_in_pattern_bindings(hash_pattern_value_type(view, assoc.key), assoc.value, scope, evaluator)
320
+ end
321
+ rest = pattern.rest
322
+ if rest.is_a?(Prism::AssocSplatNode) && rest.value.is_a?(Prism::LocalVariableTargetNode)
323
+ bindings << [rest.value.name, hash_pattern_rest_type(view)]
324
+ end
325
+ bindings
326
+ end
327
+
328
+ # The `Hash`-shaped view a hash pattern reads: the subject's `deconstruct_keys` answer, which is a `HashShape`
329
+ # for a shape carrier or a `deconstruct_keys` body and `Hash[K, V]` for the RBS answer of a plain `Hash` — or
330
+ # nil when the subject cannot be asked.
331
+ def hash_pattern_view(subject_type, scope, subject_node)
332
+ args = [Type::Combinator.constant_of(nil)]
333
+ struct_instance_projection(subject_type, :deconstruct_keys, scope, subject_node) ||
334
+ pattern_decomposition_dispatch(subject_type, :deconstruct_keys, args, scope) ||
335
+ source_decomposition_projection(subject_type, :deconstruct_keys, args, scope)
336
+ end
337
+
338
+ # The value type the pattern's key reads out of the view: a `HashShape` answers per key, a `Hash[K, V]`
339
+ # answers `V` for every key, and anything else — a key the AST does not pin (`in {"#{k}": v}`), a shape that
340
+ # does not carry the key — answers the floor.
341
+ def hash_pattern_value_type(view, key_node)
342
+ key = hash_pattern_key(key_node)
343
+ return Type::Combinator.untyped if key.nil?
344
+ return view.pairs[key] || Type::Combinator.untyped if view.is_a?(Type::HashShape)
345
+
346
+ hash_value_type(view) || Type::Combinator.untyped
347
+ end
348
+
349
+ # The key a hash pattern element names, or nil when the AST does not pin one (an interpolated or computed key).
350
+ def hash_pattern_key(key_node)
351
+ case key_node
352
+ when Prism::SymbolNode then key_node.unescaped.to_sym
353
+ when Prism::StringNode then key_node.unescaped
354
+ end
355
+ end
356
+
357
+ # `{ key:, **rest }` binds `rest` to a Hash whose keys are Symbols (the only legal key shape for a hash pattern)
358
+ # and whose values are the view's own value type — the entries the pattern named are a subset of it.
359
+ def hash_pattern_rest_type(view)
360
+ value = view.is_a?(Type::HashShape) ? union_of_types(view.pairs.values) : hash_value_type(view)
361
+ Type::Combinator.nominal_of(
362
+ "Hash",
363
+ type_args: [Type::Combinator.nominal_of("Symbol"), value || Type::Combinator.untyped]
364
+ )
365
+ end
366
+
367
+ # The `V` of a `Hash[K, V]`, or nil for a raw `Hash`, a non-`Hash` nominal, or a `Dynamic` / `Top` value.
368
+ def hash_value_type(type)
369
+ return nil unless type.is_a?(Type::Nominal) && type.class_name == "Hash" && type.type_args.size == 2
370
+
371
+ value = type.type_args.last
372
+ return nil if value.is_a?(Type::Dynamic) || value.is_a?(Type::Top)
373
+
374
+ value
375
+ end
376
+
377
+ # The union of a list of types, ignoring the gradual floor (`Dynamic[top]` / `Top` carries nothing to union)
378
+ # and answering nil when nothing is left.
379
+ def union_of_types(types)
380
+ known = types.reject { |type| type.is_a?(Type::Dynamic) || type.is_a?(Type::Top) }
381
+ known.empty? ? nil : Type::Combinator.union(*known)
382
+ end
383
+
384
+ # `[..., *rest, ...]` / `[*pre, x, *post]` capture an Array of the unmatched elements. `rest_type` is the
385
+ # enclosing decomposition's own rest (a `Tuple` of the middle elements for a tuple subject, `Array[T]` for an
386
+ # `Array[T]`); without one — a subject that did not decompose — the rest is `Array[untyped]`.
387
+ def append_array_splat_binding(bindings, splat, rest_type)
388
+ return unless splat.is_a?(Prism::SplatNode)
389
+
390
+ target = splat.expression
391
+ return unless target.is_a?(Prism::LocalVariableTargetNode)
392
+
393
+ bindings << [target.name, rest_type || Type::Combinator.nominal_of("Array", type_args: [Type::Combinator.untyped])]
394
+ end
395
+
396
+ # The `Dynamic[top]` floor per slot, for a subject no rule decomposes.
397
+ def floor_pattern_slots(front_count, back_count, rest_present)
398
+ [
399
+ Array.new(front_count) { Type::Combinator.untyped },
400
+ rest_present ? Type::Combinator.nominal_of("Array", type_args: [Type::Combinator.untyped]) : nil,
401
+ Array.new(back_count) { Type::Combinator.untyped }
402
+ ]
403
+ end
404
+
405
+ # The class a pattern's own constant asserts (`in Point[x, y]`, `in Foo{...}`), applied to the subject. An
406
+ # opaque subject — `Dynamic` / `Top`, which no `is_a?`-style narrowing can refine — becomes `Nominal[C]`: the
407
+ # pattern's `C === subject` test has just established the class, which is what lets a constrained pattern bind
408
+ # off a subject whose own type named nothing. A subject whose class is already known keeps it.
409
+ def pattern_class_constraint(subject_type, constant_node, scope, evaluator)
410
+ return subject_type if constant_node.nil?
411
+
412
+ nominal = singleton_to_nominal(evaluator.sub_eval(constant_node, scope).first)
413
+ return subject_type unless opaque_pattern_subject?(subject_type)
414
+ return subject_type if nominal.is_a?(Type::Dynamic) || nominal.is_a?(Type::Top)
415
+
416
+ nominal
417
+ end
418
+
419
+ def opaque_pattern_subject?(subject_type)
420
+ subject_type.nil? || subject_type.is_a?(Type::Dynamic) || subject_type.is_a?(Type::Top)
421
+ end
422
+
423
+ # Distributes a union subject over the pattern, the rule {MultiTargetBinder} applies to `a, b = union`: every
424
+ # member walks the same pattern and each name binds the join of its per-member types, while a member that binds
425
+ # `Dynamic[top]` floors the name for the whole union — a precise member must not stand for one nothing is known
426
+ # about. A member that PROVABLY cannot match the pattern contributes nothing at all instead of flooring:
427
+ # `case maybe; in [a, b]` over `Tuple[1, "a"] | nil` binds `1` / `"a"`, because a `nil` subject raises
428
+ # `NoMatchingPatternError` rather than reaching the body.
429
+ def collect_union_pattern_bindings(members, pattern, scope, evaluator, subject_node: nil)
430
+ reachable = members.reject { |member| pattern_match_impossible?(member, pattern, scope) }
431
+ walks = (reachable.empty? ? [Type::Combinator.untyped] : reachable).map do |member|
432
+ collect_in_pattern_bindings(member, pattern, scope, evaluator, subject_node: subject_node)
433
+ end
434
+ merge_pattern_bindings(walks)
435
+ end
436
+
437
+ # Whether `type` provably cannot match `pattern`, so its arm contributes no binding. Only the two
438
+ # decompositions a pattern asks for are checked — `deconstruct` (array / find patterns) and `deconstruct_keys`
439
+ # (hash patterns) — and both only for a class the RBS environment knows, whose method set is closed, the same
440
+ # rule `array_conversion_free?` applies to the multi-assign `to_ary` question (issue #1094). A carrier the
441
+ # check cannot prove negative about (Dynamic, a source class, an unresolved constant) answers false, which
442
+ # keeps the union at the conservative floor.
443
+ def pattern_match_impossible?(type, pattern, scope)
444
+ case pattern
445
+ when Prism::ArrayPatternNode, Prism::FindPatternNode then !decomposable_as_array?(type, scope)
446
+ when Prism::HashPatternNode then !decomposable_as_hash?(type, scope)
447
+ when Prism::CapturePatternNode then pattern_match_impossible?(type, pattern.value, scope)
448
+ else false
449
+ end
450
+ end
451
+
452
+ def decomposable_as_array?(type, scope)
453
+ return true unless pattern_decomposition_dispatch(type, :deconstruct, [], scope).nil?
454
+
455
+ class_name = MultiTargetBinder.conversion_class_name(type)
456
+ class_name.nil? || !MethodDispatcher::RbsDispatch.array_conversion_free?(class_name, scope)
457
+ end
458
+
459
+ def decomposable_as_hash?(type, scope)
460
+ args = [Type::Combinator.constant_of(nil)]
461
+ return true unless pattern_decomposition_dispatch(type, :deconstruct_keys, args, scope).nil?
462
+
463
+ class_name = MultiTargetBinder.conversion_class_name(type)
464
+ return true if class_name.nil? || scope.environment.nil?
465
+
466
+ !Reflection.rbs_class_known?(class_name, environment: scope.environment)
467
+ end
468
+
469
+ # Joins per-member binding lists by name: `Dynamic[top]` from any walk — or a name a walk does not bind —
470
+ # floors the name, otherwise the members union. The first walk's order is the declaration order.
471
+ def merge_pattern_bindings(walks)
472
+ floor = Type::Combinator.untyped
473
+ walks.first.map do |name, _type|
474
+ types = walks.map { |walk| walk.assoc(name)&.last }
475
+ floored = types.any? { |type| type.nil? || Type::Combinator.untyped?(type) }
476
+ [name, floored ? floor : Type::Combinator.union(*types)]
477
+ end
478
+ end
479
+
480
+ # `expr in pattern` (a `MatchPredicateNode`, evaluating to a boolean) and `expr => pattern` (a
481
+ # `MatchRequiredNode`, evaluating to `nil` and raising `NoMatchingPatternError` on a mismatch) — the one-line
482
+ # pattern matches. Both bind every name the pattern captures into the post-scope, decomposed exactly as an `in`
483
+ # branch of a `case` is, and WITHOUT the nil-injection a surrounding join would add: a name is read on the
484
+ # truthy side only after the pattern matched it, which is the shape `if config in {timeout: Integer => t}`
485
+ # depends on.
486
+ def eval_match_pattern(node, scope, tracer, evaluator)
487
+ subject_type, post_value = evaluator.sub_eval(node.value, scope)
488
+ bound = apply_in_pattern_bindings(subject_type, node.value, node.pattern, post_value, evaluator)
489
+ [scope.type_of(node, tracer: tracer), bound]
490
+ end
491
+
492
+ # --------------------------------------------------------------- named-capture regex binding (`MatchWriteNode`)
493
+ # ---------------------------------------------------------------
494
+
495
+ # `/(?<year>\d+)/ =~ str` — Prism emits a `MatchWriteNode` that wraps the `=~` call and lists the named-capture
496
+ # targets. Each target is bound to `String | nil` (the capture is absent as nil when the pattern doesn't match
497
+ # or the group didn't participate).
498
+ def eval_match_write(node, scope, evaluator)
499
+ match_type, post_scope = evaluator.sub_eval(node.call, scope)
500
+ string_or_nil = Type::Combinator.union(
501
+ Type::Combinator.nominal_of("String"),
502
+ Type::Combinator.constant_of(nil)
503
+ )
504
+ bound_scope = node.targets.reduce(post_scope) do |s, target|
505
+ next s unless target.is_a?(Prism::LocalVariableTargetNode)
506
+
507
+ s.with_local(target.name, string_or_nil)
508
+ end
509
+ [match_type, bound_scope]
510
+ end
511
+
512
+ # --------------------------------------------------------------- shared type conversion helper
513
+ # ---------------------------------------------------------------
514
+
515
+ # Converts a `Singleton[ClassName]` (the class object) to the corresponding `Nominal[ClassName]` (an instance).
516
+ # Falls back to `untyped` for carriers that are not Singleton (e.g. Dynamic[Top] when the class could not be
517
+ # resolved).
518
+ def singleton_to_nominal(type)
519
+ type.is_a?(Type::Singleton) ? Type::Combinator.nominal_of(type.class_name) : Type::Combinator.untyped
520
+ end
521
+
522
+ # Returns the type to bind for a `CapturePatternNode`'s target. A class reference (`Integer => x`, and every
523
+ # alternate of `Integer | String => x`) answers the constraint's `Nominal[T]` — the pattern's own `T ===
524
+ # subject` test is what licenses the binding even when the subject's type is opaque. A value pattern (`1 => x`)
525
+ # answers the literal's own type. A capture over any other pattern (`[a, b] => whole`, `^(x) => y`) answers the
526
+ # subject, which is what that pattern matched.
527
+ def capture_pattern_type(subject_type, value_node, scope, evaluator)
528
+ case value_node
529
+ when Prism::ConstantReadNode, Prism::ConstantPathNode
530
+ singleton_to_nominal(evaluator.sub_eval(value_node, scope).first)
531
+ when Prism::AlternationPatternNode
532
+ Type::Combinator.union(
533
+ capture_pattern_type(subject_type, value_node.left, scope, evaluator),
534
+ capture_pattern_type(subject_type, value_node.right, scope, evaluator)
535
+ )
536
+ when Prism::ArrayPatternNode, Prism::FindPatternNode, Prism::HashPatternNode,
537
+ Prism::PinnedVariableNode, Prism::PinnedExpressionNode, Prism::ImplicitNode
538
+ subject_type || Type::Combinator.untyped
539
+ else
540
+ literal_pattern_type(value_node, scope, evaluator)
541
+ end
542
+ end
543
+
544
+ # The type a non-class pattern node evaluates to: `Constant[1]` for `1 => x`, the nominal for `/re/ => x`,
545
+ # the range for `1..5 => x`. A class reference is the one carrier that must convert (`Singleton[C]` is the
546
+ # class object; the binding holds an instance), which the caller's constant arm does.
547
+ def literal_pattern_type(value_node, scope, evaluator)
548
+ type = evaluator.sub_eval(value_node, scope).first
549
+ type.is_a?(Type::Singleton) ? singleton_to_nominal(type) : type
550
+ end
551
+
552
+ # `in PatternA | PatternB` — Ruby rejects a named capture inside an alternative pattern (only `_`-prefixed names
553
+ # compile), so the binder, which runs against the AST, simply collects bindings from each side and merges by
554
+ # name, unioning types when both alternates contribute. A name only one alternate contributes still surfaces.
555
+ def collect_alternation_pattern_bindings(subject_type, pattern, scope, evaluator)
556
+ left = collect_in_pattern_bindings(subject_type, pattern.left, scope, evaluator)
557
+ right = collect_in_pattern_bindings(subject_type, pattern.right, scope, evaluator)
558
+ merged = {}
559
+ (left + right).each do |name, type|
560
+ merged[name] = merged.key?(name) ? Type::Combinator.union(merged[name], type) : type
561
+ end
562
+ merged.to_a
563
+ end
564
+
565
+ private_class_method :rescue_exception_type, :mark_gradual_class_capture, :class_capture_value?,
566
+ :collect_in_pattern_bindings, :collect_capture_pattern_bindings,
567
+ :collect_array_pattern_bindings, :pattern_slot_types, :positional_pattern_view,
568
+ :deconstruct_projection, :source_decomposition_projection, :own_definer_node,
569
+ :struct_instance_projection, :pattern_decomposition_dispatch,
570
+ :collect_find_pattern_bindings, :find_pattern_slots, :find_pattern_surround_type,
571
+ :collect_hash_pattern_bindings, :hash_pattern_view, :hash_pattern_value_type,
572
+ :hash_pattern_key, :hash_pattern_rest_type, :hash_value_type, :union_of_types,
573
+ :append_array_splat_binding, :floor_pattern_slots, :pattern_class_constraint,
574
+ :opaque_pattern_subject?, :collect_union_pattern_bindings, :pattern_match_impossible?,
575
+ :decomposable_as_array?, :decomposable_as_hash?, :merge_pattern_bindings,
576
+ :singleton_to_nominal, :capture_pattern_type, :literal_pattern_type,
577
+ :collect_alternation_pattern_bindings
578
+ end
579
+ end
580
+ end
581
+ end