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
@@ -15,8 +15,8 @@ module Rigor
15
15
  # becomes a collector that receives events and never tracks context itself; the {Context} it is handed is
16
16
  # the walk's.
17
17
  #
18
- # Require `rigor/inference/declaration_walk`, not this file: the rules the walk applies are `ScopeIndexer`
19
- # functions, and that entry point loads them.
18
+ # Require `rigor/inference/declaration_walk`, not this file: the rules the walk applies are {Rules}' functions,
19
+ # which this file does not load ({Rules} says why), and that entry point loads them.
20
20
  #
21
21
  # The arms, in the order the walk tries them:
22
22
  #
@@ -71,11 +71,12 @@ module Rigor
71
71
  #
72
72
  # - `factory_block` — a bare `Class.new { … }` / `Module.new` / `Struct.new` / `Data.define` block.
73
73
  # `:unnamed_self` walks the factory's receiver and arguments, then the body with an unnamed `self`,
74
- # and skips the block's parameters (`walk_class_cvars`' rule). `:ordinary_call` walks the call's
75
- # children like any call's — receiver, arguments, the block's parameters and body — under the
76
- # enclosing context, so a `self::` header in the body anchors on the enclosing self
77
- # (`walk_class_superclasses`' rule; `class C; Class.new { class self::E < S; end }; end` files
78
- # `C::E`, a class Ruby never creates; #1521 item 8).
74
+ # and skips the block's parameters (`walk_class_cvars`' rule, in `ScopeIndexer::ClassCvarCensus`).
75
+ # `:ordinary_call` walks the call's children like any call's — receiver, arguments, the block's parameters
76
+ # and body — under the enclosing context, so a `self::` header in the body anchors on the enclosing self
77
+ # (`walk_class_superclasses`' rule, in `ScopeIndexer::SharedWalkTables` with the walkers named below;
78
+ # `class C; Class.new { class self::E < S; end }; end` files `C::E`, a class Ruby never creates; #1521
79
+ # item 8).
79
80
  # - `anonymous_class_path` — the file path an anonymous class's synthetic name carries; answered by
80
81
  # {Context#anonymous_class_path}, which documents `:whole_file` and `:outside_class_bodies` (#1521
81
82
  # item 11).
@@ -396,7 +397,7 @@ module Rigor
396
397
  # `self`, the `:ordinary_call` ones the whole block, parameters included, under the enclosing context.
397
398
  # Each collector sees the same events in the same order as in a run of its own.
398
399
  def walk_factory_call?(node, collectors, context)
399
- return false unless @unnamed_factories && ScopeIndexer.meta_new_constant_rvalue?(node)
400
+ return false unless @unnamed_factories && Rules.meta_new_constant_rvalue?(node)
400
401
 
401
402
  unnamed = unnamed_factory_group(collectors)
402
403
  return false if unnamed.empty?
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # ADR-116 WD5 — the entry point for {Rigor::Inference::DeclarationWalk}. The walk lives in
4
- # `declaration_walk/traversal`, and the context rules it applies are `ScopeIndexer` functions, so this file loads
5
- # the indexer, which loads the walk. `ScopeIndexer` itself requires the traversal file rather than this one, which
6
- # keeps the two free of a require cycle.
4
+ # `declaration_walk/traversal` and the context rules it applies in `declaration_walk/rules` (ADR-116 WD9). The
5
+ # indexer loads both, the rules last of the files its head requires so that their slot keeps its place in
6
+ # `TypingState.slots`, so this file loads the indexer. `ScopeIndexer` itself requires those two files rather than
7
+ # this one, which keeps them free of a require cycle.
7
8
  require_relative "scope_indexer"
8
9
 
9
10
  # {Rigor::Reflection.class_declarations_in}'s collectors: the plugin-facing walk lives with `Reflection` but needs the
@@ -95,8 +95,8 @@ module Rigor
95
95
  # 2. AGREEMENT — where both tables answer for the same DEF (through different objects), they answer the
96
96
  # same chain. A bundle is only reused when the file's content SHA-256 matches
97
97
  # ({Cache::FileDigest.hexdigest}, the digest tier, not the stat one), and the def-nesting table
98
- # (`ScopeIndexer.declaration_walk_tables`) is a pure function of that file's AST, so identical bytes
99
- # yield an identical chain.
98
+ # (`ScopeIndexer::SharedWalkTables.declaration_walk_tables`) is a pure function of that file's AST, so
99
+ # identical bytes yield an identical chain.
100
100
  #
101
101
  # What is NOT true — and was asserted here before it was measured — is that a file takes exactly ONE of
102
102
  # the two branches per run. It does so in the cross-file pre-pass only. An UNCHANGED file re-analysed as
@@ -117,16 +117,23 @@ module Rigor
117
117
  scope = context&.scope_index&.[](node) || scope
118
118
  case node
119
119
  when Prism::CallNode then optimistic_call?(node, scope, context)
120
- when Prism::LocalVariableReadNode then !scope.optimistic_local(node.name).nil?
121
- when Prism::InstanceVariableReadNode then !scope.optimistic_ivar(node.name).nil?
120
+ when Prism::LocalVariableReadNode then lookup_mark?(scope.optimistic_local(node.name))
121
+ when Prism::InstanceVariableReadNode then lookup_mark?(scope.optimistic_ivar(node.name))
122
122
  when Prism::AndNode then [node.left, node.right].any? { optimistic_value?(it, scope, context) }
123
123
  when Prism::OrNode then optimistic_value?(node.right, scope, context)
124
124
  else optimistic_write?(node, scope, context) || optimistic_branch?(node, scope, context)
125
125
  end
126
126
  end
127
127
 
128
+ # #3322 — a mark whose miss a signature should keep. A plugin's optimistic answer
129
+ # (`OptimisticOrigin::PLUGIN_NIL_FREE`, rigor-activerecord's nullable column readers) is not one: the plugin
130
+ # omits the `nil` precisely so that callers' ordinary calls do not report `possible-nil-receiver`, and a
131
+ # `-> T?` written into `sig/` would put that cost back one call away. It still keeps the in-scope certainty
132
+ # consumers from folding a condition over the value.
133
+ def lookup_mark?(cause) = !cause.nil? && cause != OptimisticOrigin::PLUGIN_NIL_FREE
134
+
128
135
  def optimistic_call?(node, scope, context)
129
- if scope.optimistic_origin(node)
136
+ if lookup_mark?(scope.optimistic_origin(node))
130
137
  return false if guarded_lookup?(node, context) || non_empty_end?(node, scope)
131
138
 
132
139
  return true
@@ -175,9 +182,9 @@ module Rigor
175
182
  Prism::LocalVariableOrWriteNode, Prism::InstanceVariableOrWriteNode
176
183
  optimistic_value?(node.value, scope, context)
177
184
  when Prism::LocalVariableAndWriteNode
178
- !scope.optimistic_local(node.name).nil? || optimistic_value?(node.value, scope, context)
185
+ lookup_mark?(scope.optimistic_local(node.name)) || optimistic_value?(node.value, scope, context)
179
186
  when Prism::InstanceVariableAndWriteNode
180
- !scope.optimistic_ivar(node.name).nil? || optimistic_value?(node.value, scope, context)
187
+ lookup_mark?(scope.optimistic_ivar(node.name)) || optimistic_value?(node.value, scope, context)
181
188
  else false
182
189
  end
183
190
  end
@@ -255,7 +262,8 @@ module Rigor
255
262
 
256
263
  type_return_node(node, scope_index, out, lookup_context: lookup_context) if node.is_a?(Prism::ReturnNode)
257
264
  # A barrier call's receiver and arguments still run in the method; only its block is cut off.
258
- barrier_block = node.block if node.is_a?(Prism::CallNode) && ReturnBarrier.block_call?(node)
265
+ barrier = node.is_a?(Prism::CallNode) && ReturnBarrier.block_call?(node, scope: scope_index[node])
266
+ barrier_block = node.block if barrier
259
267
  node.rigor_each_child do |c|
260
268
  collect_return_types(c, scope_index, out, lookup_context: lookup_context) unless c.equal?(barrier_block)
261
269
  end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "alias_mutation_floor"
6
+ require_relative "deep_erasure"
7
+ require_relative "mutation_widening"
8
+ require_relative "opaque_mutation"
9
+ require_relative "receiver_alias"
10
+ require_relative "refinement_mutation"
11
+ require_relative "remover_holder_scan"
12
+ require_relative "set_mutation"
13
+
14
+ module Rigor
15
+ module Inference
16
+ # Issue #3340 — whether a closure that may run later can empty a local's collection. An emptiness guard's
17
+ # `non-empty-array[T]` is a fact about the object, and a closure made before the guard may run after it
18
+ # (`f = -> { xs.clear }; return if xs.empty?; f.call`): the widening at the closure is undone by the guard, and
19
+ # nothing ties a later call to the closure. So the guard does not narrow such a local at all
20
+ # (`Narrowing::PresencePredicates`), the rule the remover narrowing takes for a local a later-running closure
21
+ # references ({RemoverHolderScan}).
22
+ #
23
+ # A closure is a lambda literal or a block that may run after its call returns
24
+ # ({RemoverHolderScan.deferred_block?}: `proc`, `lambda`, `define_method`, `define_singleton_method`, any block
25
+ # but a catalogued iterator's or `tap` / `then` / `yield_self`'s, so a `loop` block too), anywhere in the frame
26
+ # body the guard runs in, so one made in a scope enclosing the guard counts. A closure the guard itself sits in
27
+ # does not: its own statements run in order, and the straight-line rules read them. That assumes the closure
28
+ # does not call itself, which would re-enter it past the guard.
29
+ #
30
+ # It can empty the local when its body, a nested block included, runs on a read of it that resolves outside the
31
+ # closure: a mutator that can leave a non-empty receiver empty (every {MUTATORS} name but the ones
32
+ # {RefinementMutation.preserves_witness?} keeps, so `<<` and `sort!` do not count), spelled directly, through a
33
+ # literal `send` or a literal-named `Method` object, or by a symbol block over a literal Array holding it; or an
34
+ # opaque hand-off ({OpaqueMutation}: a computed `send`, `instance_eval`, a `yield` passing it). A closure that
35
+ # only reads the local, or only appends to it, leaves the guard's narrowing alone, and so does a block that ran
36
+ # and finished before the guard. Not read: a holder of the object made outside the closure (`ys = xs; f = -> {
37
+ # ys.clear }`), an alias made inside it, a callee the closure passes the local to, and an emptier behind a call
38
+ # that hands the receiver back (`xs.tap(&:clear)`).
39
+ module DeferredMutation
40
+ MUTATORS = (MutationWidening::SHAPE_MUTATORS | SetMutation::MUTATORS).freeze
41
+ SCOPE_NODES = Set[Prism::BlockNode, Prism::LambdaNode].freeze
42
+ private_constant :MUTATORS, :SCOPE_NODES
43
+
44
+ module_function
45
+
46
+ # True when a closure in `scope`'s frame body that may run later, and that does not hold `guard`, can empty
47
+ # the Array the local `name` holds.
48
+ def empties?(scope, name, guard)
49
+ body = scope.match_frame&.body
50
+ return false if body.nil?
51
+
52
+ closure_emptier?(body, name.to_sym, 0, nil, guard.location.start_offset)
53
+ end
54
+
55
+ # `depth` counts the blocks and lambdas entered from the outermost later-running closure on, that one
56
+ # included; zero outside every such closure. A read at `depth` names the frame's local when it resolves that
57
+ # many scopes out, which a read inside a nested `def` or class body never does.
58
+ def closure_emptier?(node, name, depth, parent, guard_offset)
59
+ klass = node.class
60
+ if SCOPE_NODES.include?(klass)
61
+ depth += 1 if depth.positive? || later_closure?(node, parent, guard_offset)
62
+ elsif depth.positive? && emptier?(node, name, depth)
63
+ return true
64
+ end
65
+ found = false
66
+ node.rigor_each_child { |child| found ||= closure_emptier?(child, name, depth, node, guard_offset) }
67
+ found
68
+ end
69
+
70
+ # A lambda literal, or a block that may run after its call returns, that does not hold the guard. A `loop`
71
+ # block counts: the name is no catalogued iterator's, a project may define it and keep the block, and the
72
+ # entry of a `Kernel#loop` body reads only the mutators the body spells on the local.
73
+ def later_closure?(node, parent, guard_offset)
74
+ return false unless node.is_a?(Prism::LambdaNode) || RemoverHolderScan.deferred_block?(parent)
75
+
76
+ location = node.location
77
+ guard_offset < location.start_offset || guard_offset >= location.end_offset
78
+ end
79
+
80
+ def emptier?(node, name, depth)
81
+ return names?(OpaqueMutation.local_arguments(node.arguments&.arguments || []), name, depth) if yield?(node)
82
+ return false unless node.is_a?(Prism::CallNode)
83
+
84
+ receiver = emptied_receiver(node)
85
+ return true if receiver && names?(ReceiverAlias.mutated_reads(receiver), name, depth)
86
+
87
+ symbol_block_emptier?(node, name, depth)
88
+ end
89
+
90
+ def yield?(node) = node.is_a?(Prism::YieldNode)
91
+
92
+ # The receiver `call_node` may empty: its own under an emptying mutator, spelled directly or as the call a
93
+ # literal `send` or a literal-named `Method` object makes, or the one it hands to unread code.
94
+ def emptied_receiver(call_node)
95
+ made = AliasMutationFloor.literal_send_target(call_node) || OpaqueMutation.method_object_target(call_node) ||
96
+ call_node
97
+ return made.receiver if emptying?(made.name)
98
+
99
+ OpaqueMutation.opaque_receiver(call_node)
100
+ end
101
+
102
+ def emptying?(method_name)
103
+ MUTATORS.include?(method_name) && !RefinementMutation.preserves_witness?("Array", method_name)
104
+ end
105
+
106
+ # `[xs, ys].each(&:clear)`: a symbol block naming an emptying mutator over a literal Array holding the local.
107
+ def symbol_block_emptier?(call_node, name, depth)
108
+ receiver = call_node.receiver
109
+ return false unless receiver.is_a?(Prism::ArrayNode) && DeepErasure.symbol_proc_mutation?(call_node.block)
110
+ return false unless emptying?(call_node.block.expression.unescaped.to_sym)
111
+
112
+ receiver.elements.any? { |element| names?(ReceiverAlias.mutated_reads(element), name, depth) }
113
+ end
114
+
115
+ def names?(reads, name, depth)
116
+ reads.any? { |read| read.is_a?(Prism::LocalVariableReadNode) && read.name == name && read.depth >= depth }
117
+ end
118
+ end
119
+ end
120
+ end
@@ -13,7 +13,7 @@ module Rigor
13
13
  # receiving instance. A bare `text` inside the block therefore reaches `C`'s member / attr reader,
14
14
  # while `Singleton[C]` answers only `C`'s class methods.
15
15
  #
16
- # The consequence #963 records is #618's veto going the wrong way: `ExpressionTyper#self_type_answers?`
16
+ # The consequence #963 records is #618's veto going the wrong way: `CallLadder::TopLevelDef.self_type_answers?`
17
17
  # asked the singleton side, found nothing, and let a same-named top-level `def text` bind ahead of the
18
18
  # reader — typing `text.upcase` inside `class Line < Struct.new(:text); define_method(:shout) { text.upcase }`
19
19
  # as the top-level def's `nil`. Narrowing the block body's `self_type` to `Nominal[C]` makes the
@@ -26,9 +26,9 @@ module Rigor
26
26
  # `spec/rigor/inference/definer_resolution_case_in_spec.rb` fails on a call site of any other shape.
27
27
  #
28
28
  # It migrates one question at a time: `SourceArity` (C1b, C2-c) asks for the receiver's own side, instance or
29
- # singleton; the override lints (C1c) and the C2 typing sites (`ExpressionTyper#resolve_typing_def_with_owner`,
30
- # `StatementEvaluator#own_definer_node`) read the instance side; `RefinedDispatch::OwnMethod.certain_owner?` and
31
- # `CheckRules#reached_definer_visibility` pass the side they hold.
29
+ # singleton; the override lints (C1c) and the C2 typing sites (`CallLadder::UserMethod.resolve_typing_def_with_owner`,
30
+ # `StatementEvaluator::PatternBindings.own_definer_node`) read the instance side;
31
+ # `RefinedDispatch::OwnMethod.certain_owner?` and `CheckRules::VisibilityMismatch` pass the side they hold.
32
32
  module DefinerResolution
33
33
  Known = Data.define(:answer, :owner)
34
34
  UnknownResult = Data.define
@@ -230,7 +230,7 @@ module Rigor
230
230
  # this question does read.
231
231
  #
232
232
  # Issue #2134 — the contest is a read of the slot, so a question that reads it files `visibility:<owner>#<name>`:
233
- # an answer block may answer from RBS without reading the slot itself (`nearest_ancestor_method_def`).
233
+ # an answer block may answer from RBS without reading the slot itself (`OverrideSupport`).
234
234
  def possible?(scope, hit, method_name, reads_visibility)
235
235
  discovery = scope.discovery
236
236
  owner = hit.owner
@@ -40,8 +40,8 @@ module Rigor
40
40
  # The exception instance type a `RescueNode` binds its reference to: `StandardError` for a bare `rescue`, else
41
41
  # the union over the named exceptions, each the instance of the class the block types it as (`yield`ed the
42
42
  # exception node) or `Dynamic[top]` when that is not a `Singleton` (a splat list, an unresolved constant).
43
- # Shared by `StatementEvaluator#rescue_exception_type` and the class-ivar census (issue #1217), so the two
44
- # agree.
43
+ # Shared by `StatementEvaluator::PatternBindings.rescue_exception_type` and the class-ivar census (issue #1217),
44
+ # so the two agree.
45
45
  def rescue_exception_type(rescue_node)
46
46
  exceptions = rescue_node.exceptions
47
47
  return Type::Combinator.nominal_of("StandardError") if exceptions.empty?
@@ -55,12 +55,12 @@ module Rigor
55
55
 
56
56
  # The scope a `rescue` clause, or a rescue modifier's fallback, runs from: `$!` bound to the exception it rescued
57
57
  # and `$@` to that exception's backtrace, with `$?` unbound. `exception_type` is what `rescue … => e` binds `e` to
58
- # (`StatementEvaluator#rescue_exception_type`): the union of the named classes' instances, `StandardError` for a
59
- # bare `rescue`. A member that is not a class RBS or the program places below `Exception` reads `Dynamic[top]`
60
- # instead: a rescue list may name a module with its own `===` (`rescue NetworkErrors`), which matches exceptions
61
- # that are not instances of it, and a class the analyzer cannot place may be such an object too. So does a class
62
- # that it or a project ancestor gives its own singleton `===` (`class Matchy < StandardError; def self.===(o) =
63
- # true`).
58
+ # (`StatementEvaluator::PatternBindings.rescue_exception_type`): the union of the named classes' instances,
59
+ # `StandardError` for a bare `rescue`. A member that is not a class RBS or the program places below `Exception`
60
+ # reads `Dynamic[top]` instead: a rescue list may name a module with its own `===` (`rescue NetworkErrors`), which
61
+ # matches exceptions that are not instances of it, and a class the analyzer cannot place may be such an object
62
+ # too. So does a class that it or a project ancestor gives its own singleton `===` (`class Matchy < StandardError;
63
+ # def self.===(o) = true`).
64
64
  #
65
65
  # `body` is the clause's statements or the fallback, and `reference` the name of the local a `rescue … => e`
66
66
  # clause binds. When the body guards `$!`, or that local, which is the same object, by its class ({.guarded?}),
@@ -0,0 +1,348 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../type"
6
+
7
+ module Rigor
8
+ module Inference
9
+ class ExpressionTyper
10
+ # Typers of nodes whose answer is built from the types of their children: hash, array, range, interpolation and
11
+ # the pass-through nodes (parentheses, statements, `else`, `ensure`, `when` / `in`, `class` / `module` bodies).
12
+ # Reads no state; calls back {ExpressionTyper#type_of}. The entries are `type_of_statements_node`,
13
+ # `type_of_program`, `type_of_hash`, `type_of_interpolated_string`, `type_of_embedded_statements`,
14
+ # `type_of_class_or_module`, `type_of_else`, `type_of_when_or_in`, `type_of_ensure`, `statements_or_nil`,
15
+ # `type_of_shareable_constant`, `type_of_implicit`, `array_type_for`, `parentheses_type_for`, `type_of_range`;
16
+ # every other method is private to the module (ADR-116 C3). State is passed, not shared: each entry takes the
17
+ # values it reads from the typer as trailing positional parameters (`scope`, `tracer`) and the typer itself
18
+ # (`typer`) when it calls back. `NO_STATIC_HASH_KEY` is the class's private sentinel, read lexically: moving its
19
+ # initialiser would add a row to a gate file.
20
+ module Composites
21
+ module_function
22
+
23
+ def type_of_statements_node(node, typer)
24
+ statements_type_for(node, typer)
25
+ end
26
+
27
+ def type_of_program(node, typer)
28
+ statements_type_for(node.statements, typer)
29
+ end
30
+
31
+ # Slice 5 phase 1 upgrades hash literals to `HashShape{...}` when every entry is a static `AssocNode`
32
+ # whose key is a value-pinned scalar literal — Symbol, plain String, Integer, Float, `true`, `false`,
33
+ # or `nil` (covering `{ a: 1, "b" => 2 }` and `{ 1 => 2, 1.0 => 4 }` alike) — falling back to the
34
+ # generic `Hash[K, V]` form otherwise. Splatted entries (`{ **other }`) and dynamic keys widen to the
35
+ # underlying `Hash[K, V]` form by unioning the types each entry exposes — a splat exposes the `[K, V]`
36
+ # of the hash it copies plus a `Dynamic[top]` arm (see {.hash_splat_pair}), so every entry contributes a
37
+ # pair. A splat never keeps a shape, even over an exact closed one.
38
+ def type_of_hash(node, typer)
39
+ elements = node.respond_to?(:elements) ? node.elements : []
40
+ # v0.0.7 — `{}` resolves to the empty `HashShape{}` carrier rather than `Nominal[Hash]`, mirroring the
41
+ # v0.0.6 empty-array literal change. Both forms erase to plain `Hash`, but `HashShape{}` pins the
42
+ # literal's known size (zero) so HashShape projections (`empty?`, `first`, `count`, …) fold against it.
43
+ return Type::Combinator.hash_shape_of({}) if elements.empty?
44
+
45
+ shape = static_hash_shape_for(elements, typer)
46
+ return shape if shape
47
+
48
+ keys, values = generic_hash_pairs_for(elements, typer)
49
+ Type::Combinator.nominal_of(
50
+ Hash,
51
+ type_args: [Type::Combinator.union(*keys), Type::Combinator.union(*values)]
52
+ )
53
+ end
54
+
55
+ # Builds `HashShape{...}` when every entry is an `AssocNode` whose key is a value-pinned scalar
56
+ # literal (Symbol, plain String, Integer, Float, true, false, nil). Returns nil otherwise so the
57
+ # caller falls back to the generic shape.
58
+ #
59
+ # Duplicate keys are LAST-WINS, matching the runtime (`{ a: 1, a: 2 }` keeps `a: 2`; Ruby itself
60
+ # only warns under `-w`). Key identity is Ruby `Hash` `eql?` identity because `pairs` is a native
61
+ # Hash — `1` and `1.0` stay distinct entries, `1.0` and `1.00` collide. The plain re-assignment
62
+ # also reproduces the runtime's ordering: the key keeps its FIRST insertion position while the
63
+ # value comes from the LAST occurrence.
64
+ def static_hash_shape_for(elements, typer)
65
+ pairs = {}
66
+ elements.each do |entry|
67
+ return nil unless entry.is_a?(Prism::AssocNode)
68
+
69
+ key = static_hash_key(entry.key)
70
+ return nil if key.equal?(NO_STATIC_HASH_KEY)
71
+
72
+ pairs[key] = typer.type_of(entry.value)
73
+ end
74
+ return nil if pairs.empty?
75
+
76
+ Type::Combinator.hash_shape_of(pairs)
77
+ end
78
+
79
+ # Returns the value-pinned scalar literal carried by a hash key node, or {NO_STATIC_HASH_KEY} when
80
+ # the key is dynamic (a computed expression, an interpolated string — SymbolNode#value /
81
+ # StringNode#unescaped are nil under interpolation — a constant, a local, …). `nil` / `false` are
82
+ # real key values here, hence the sentinel rather than a nil return.
83
+ def static_hash_key(node)
84
+ case node
85
+ when Prism::SymbolNode
86
+ raw = node.value
87
+ raw.nil? ? NO_STATIC_HASH_KEY : raw.to_sym
88
+ when Prism::StringNode
89
+ node.unescaped || NO_STATIC_HASH_KEY
90
+ when Prism::IntegerNode, Prism::FloatNode then node.value
91
+ when Prism::TrueNode then true
92
+ when Prism::FalseNode then false
93
+ when Prism::NilNode then nil
94
+ else NO_STATIC_HASH_KEY
95
+ end
96
+ end
97
+
98
+ def generic_hash_pairs_for(elements, typer)
99
+ keys = []
100
+ values = []
101
+ elements.each do |entry|
102
+ if entry.is_a?(Prism::AssocNode)
103
+ keys << typer.type_of(entry.key)
104
+ values << typer.type_of(entry.value)
105
+ else
106
+ key, value = hash_splat_pair(entry, typer)
107
+ keys << key
108
+ values << value
109
+ end
110
+ end
111
+ [keys, values]
112
+ end
113
+
114
+ # The `[K, V]` a `**splat` entry adds to the literal: what the analysis can read of the hash it copies, each
115
+ # side joined with `Dynamic[top]`. Ruby inserts every pair of that hash, so leaving the entry out typed
116
+ # `o = { a: :z }; { **o, b: :y }` as `Hash[:b, :y]` and folded `h[:a] == :z` on correct code.
117
+ #
118
+ # The `Dynamic[top]` arm is there even when the copy is read exactly. The literal builds a new hash that no
119
+ # declaration describes, while `MutationRejoin` regrows a `Hash[K, V]` after `[]=` / `merge!` only when it
120
+ # already carries a gradual arm, because it reads a precise one as a declared claim. Without the arm
121
+ # `h = { **o }; h[:b] = 2; h[:b] == 2` would fold always-falsey and `h[:e] = "s"; h[:e].upcase` would fire
122
+ # `call.undefined-method`, where the `Hash` a splat-only literal used to type as had stayed quiet. The same
123
+ # arm stands in for what the analysis cannot read: an anonymous `**`, an untyped value, a nominal other than
124
+ # `Hash`, an open shape's unlisted entries, and a hash filled through an alias the engine does not track,
125
+ # which still reads `{}`.
126
+ def hash_splat_pair(entry, typer)
127
+ untyped = Type::Combinator.untyped
128
+ copied = entry.value && splatted_hash_pair(typer.type_of(entry.value))
129
+ return [untyped, untyped] if copied.nil?
130
+
131
+ copied.map { |side| Type::Combinator.union(side, untyped) }
132
+ end
133
+
134
+ # The `[K, V]` the analysis can read of a splatted value, or nil when it reads nothing: a shape's keys and
135
+ # values, a `Hash[K, V]`'s type arguments (through a difference over one, `non-empty-hash[K, V]`), and the
136
+ # join over a union's members. `nil` reads as nothing because `**nil` splats nothing (Ruby 3.4+; earlier
137
+ # Rubies raise). A `Hash` subclass is not read: its own type arguments, if any, are not `Hash`'s `K` and `V`.
138
+ def splatted_hash_pair(type)
139
+ case type
140
+ when Type::HashShape then splatted_shape_pair(type)
141
+ when Type::Nominal then type.class_name == "Hash" && type.type_args.size == 2 ? type.type_args : nil
142
+ when Type::Difference then splatted_hash_pair(type.base)
143
+ when Type::Union then joined_hash_pair(type.members.filter_map { |member| splatted_hash_pair(member) })
144
+ end
145
+ end
146
+
147
+ def splatted_shape_pair(shape)
148
+ return nil if shape.pairs.empty?
149
+
150
+ [Type::Combinator.union(*shape.pairs.keys.map { |k| Type::Combinator.constant_of(k) }),
151
+ Type::Combinator.union(*shape.pairs.values)]
152
+ end
153
+
154
+ def joined_hash_pair(pairs)
155
+ return nil if pairs.empty?
156
+
157
+ [Type::Combinator.union(*pairs.map(&:first)), Type::Combinator.union(*pairs.map(&:last))]
158
+ end
159
+
160
+ # An interpolated string `"#{a}b#{c}"` is `literal-string` when every part contributes literal-bearing
161
+ # material: plain text segments are literal by construction, embedded expressions count when their type
162
+ # is itself literal-string-compatible (a `Constant<String>`, the `literal-string` carrier, an
163
+ # `Intersection` containing it, or a `Union` whose members all qualify). Otherwise the result widens to
164
+ # plain `Nominal[String]` as before.
165
+ def type_of_interpolated_string(node, typer)
166
+ return Type::Combinator.literal_string if interpolation_parts_literal?(node.parts, typer)
167
+
168
+ Type::Combinator.nominal_of(String)
169
+ end
170
+
171
+ def interpolation_parts_literal?(parts, typer)
172
+ parts.all? { |part| interpolation_part_literal?(part, typer) }
173
+ end
174
+
175
+ def interpolation_part_literal?(part, typer)
176
+ case part
177
+ when Prism::StringNode
178
+ true
179
+ when Prism::EmbeddedStatementsNode, Prism::EmbeddedVariableNode
180
+ Type::Combinator.literal_string_compatible?(typer.type_of(part))
181
+ else
182
+ false
183
+ end
184
+ end
185
+
186
+ def type_of_embedded_statements(node, typer)
187
+ statements_type_for(node.statements, typer)
188
+ end
189
+
190
+ # `class Foo; body; end`, `module Foo; body; end`, and `class << x; body; end` evaluate to the value of
191
+ # the body's last expression, or `nil` when the body is empty. We do not track class/module scope yet,
192
+ # so the body is typed in the surrounding scope and that result is returned.
193
+ def type_of_class_or_module(node, typer)
194
+ body = node.body
195
+ return Type::Combinator.constant_of(nil) if body.nil?
196
+
197
+ typer.type_of(body)
198
+ end
199
+
200
+ def type_of_else(node, typer)
201
+ statements_or_nil(node.statements, typer)
202
+ end
203
+
204
+ # `when` clauses for `case` and `in` clauses for `case ... in` have the same body shape; we reuse one
205
+ # handler for both Prism node classes.
206
+ def type_of_when_or_in(node, typer)
207
+ statements_or_nil(node.statements, typer)
208
+ end
209
+
210
+ def type_of_ensure(node, typer)
211
+ statements_or_nil(node.statements, typer)
212
+ end
213
+
214
+ def type_of_range(node, typer)
215
+ left_static, left = static_range_endpoint(node.left, typer)
216
+ right_static, right = static_range_endpoint(node.right, typer)
217
+ if left_static && right_static
218
+ range = static_range_value(left, right, node.exclude_end?)
219
+ return Type::Combinator.constant_of(range) if range
220
+ end
221
+
222
+ nominal_range_for_endpoints(node.left, node.right, typer)
223
+ end
224
+
225
+ # The `Range` the static endpoints denote, or nil when Ruby would refuse to build it (`1.."a"`,
226
+ # `"a"..1` raise `ArgumentError: bad value for range`). The literal is a runtime error,
227
+ # not a constant, so the fold declines and the caller types it as a plain Range.
228
+ def static_range_value(left, right, exclude_end)
229
+ Range.new(left, right, exclude_end)
230
+ rescue ArgumentError
231
+ nil
232
+ end
233
+
234
+ # Derives `Nominal[Range, [T]]` from the endpoint expression types when at least one endpoint is
235
+ # statically typeable. The element parameter is the union of the endpoint types (lifted from
236
+ # `Constant<v>` to `Nominal<v.class>` so the carrier matches what `Range#each` would yield). Falls back
237
+ # to bare `Nominal[Range]` when no endpoint contributes a typable shape.
238
+ def nominal_range_for_endpoints(left_node, right_node, typer)
239
+ endpoints = [left_node, right_node].compact.map { |n| range_endpoint_element_type(n, typer) }
240
+ endpoints.reject! { |t| Type::Combinator.untyped?(t) }
241
+ return Type::Combinator.nominal_of("Range") if endpoints.empty?
242
+
243
+ Type::Combinator.nominal_of("Range", type_args: [Type::Combinator.union(*endpoints)])
244
+ end
245
+
246
+ def range_endpoint_element_type(node, typer)
247
+ type = typer.type_of(node)
248
+ case type
249
+ when Type::Constant
250
+ value = type.value
251
+ return Type::Combinator.untyped if value.nil?
252
+
253
+ Type::Combinator.nominal_of(value.class.name)
254
+ when Type::IntegerRange
255
+ Type::Combinator.nominal_of("Integer")
256
+ when Type::FloatRange
257
+ Type::Combinator.nominal_of("Float")
258
+ else
259
+ type
260
+ end
261
+ end
262
+
263
+ # A range endpoint folds to a static value when it is a literal (`IntegerNode` / `StringNode`) or when
264
+ # its *evaluated* type is a `Constant<v>` carrying a range-able value (Integer / Float / String —
265
+ # matching the literal-path value kinds). The evaluated arm lets `(1..n)` fold to `Constant<Range>`
266
+ # when per-call body inference has pinned `n` to a constant (fact2 chain). A `nil` node is a
267
+ # beginless/endless boundary: keep today's static-nil behaviour (which yields `Constant<Range>` only
268
+ # when the *other* end is also static, preserving today's beginless/endless path).
269
+ def static_range_endpoint(node, typer)
270
+ return [true, nil] if node.nil?
271
+ return [true, node.value] if node.is_a?(Prism::IntegerNode)
272
+ return [true, node.unescaped] if node.is_a?(Prism::StringNode) && node.respond_to?(:unescaped)
273
+
274
+ type = typer.type_of(node)
275
+ if type.is_a?(Type::Constant)
276
+ value = type.value
277
+ return [true, value] if value.is_a?(Integer) || value.is_a?(Float) || value.is_a?(String)
278
+ end
279
+
280
+ [false, nil]
281
+ end
282
+
283
+ # Helper for the many control-flow handlers that read a body `Prism::StatementsNode` or treat its
284
+ # absence as nil. Note that Prism uses nil (rather than an empty `StatementsNode`) for missing bodies
285
+ # in many node kinds.
286
+ def statements_or_nil(statements_node, typer)
287
+ return Type::Combinator.constant_of(nil) if statements_node.nil?
288
+
289
+ statements_type_for(statements_node, typer)
290
+ end
291
+
292
+ # `# shareable_constant_value:` magic comment wraps the next constant write. Type is the wrapped
293
+ # write's value.
294
+ def type_of_shareable_constant(node, typer)
295
+ typer.type_of(node.write)
296
+ end
297
+
298
+ # `{ x: }` shorthand hash. The implicit value is the call to `x` (or a local read of `x`). Delegate.
299
+ def type_of_implicit(node, typer)
300
+ typer.type_of(node.value)
301
+ end
302
+
303
+ # Slice 5 phase 1 upgrades array literals to `Tuple[T1..Tn]` when every element is a non-splat value.
304
+ # Splatted entries (`[*xs, 1]`) preserve the Slice 4 phase 2d behavior: we union the contributed
305
+ # element types and emit `Nominal[Array, [union]]`.
306
+ #
307
+ # v0.0.6 — the empty literal `[]` resolves to the empty `Tuple[]` carrier rather than the raw
308
+ # `Nominal[Array]`. Both carriers erase to RBS `Array`, but `Tuple[]` pins the literal's known arity
309
+ # (zero), which lets the per-element block fold concatenate across all-empty positions like `[1,
310
+ # 2].flat_map { |_| [] }`.
311
+ def array_type_for(node, typer)
312
+ elements = node.elements
313
+ return Type::Combinator.tuple_of if elements.empty?
314
+
315
+ if elements.any?(Prism::SplatNode)
316
+ element_types = elements.map { |e| typer.type_of(e) }
317
+ element_union = Type::Combinator.union(*element_types)
318
+ return Type::Combinator.nominal_of(Array, type_args: [element_union])
319
+ end
320
+
321
+ Type::Combinator.tuple_of(*elements.map { |e| typer.type_of(e) })
322
+ end
323
+
324
+ def parentheses_type_for(node, typer)
325
+ body = node.body
326
+ return Type::Combinator.constant_of(nil) if body.nil?
327
+
328
+ typer.type_of(body)
329
+ end
330
+
331
+ def statements_type_for(statements_node, typer)
332
+ return Type::Combinator.constant_of(nil) if statements_node.nil?
333
+
334
+ body = statements_node.body
335
+ return Type::Combinator.constant_of(nil) if body.empty?
336
+
337
+ typer.type_of(body.last)
338
+ end
339
+
340
+ private_class_method :static_hash_shape_for, :static_hash_key, :generic_hash_pairs_for, :hash_splat_pair,
341
+ :splatted_hash_pair, :splatted_shape_pair, :joined_hash_pair,
342
+ :interpolation_parts_literal?, :interpolation_part_literal?, :static_range_value,
343
+ :nominal_range_for_endpoints, :range_endpoint_element_type, :static_range_endpoint,
344
+ :statements_type_for
345
+ end
346
+ end
347
+ end
348
+ end