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,233 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../type"
4
+
5
+ module Rigor
6
+ module Inference
7
+ class ExpressionTyper
8
+ # The per-element assemblers `Array#map` / `select` / `reject` / `partition` / `find` / … reach when a block is
9
+ # folded once per Tuple position: pure functions from the per-position block results and the receiver's element
10
+ # types to the call's type, or nil to decline. Reads no state and calls back nothing. The entry is
11
+ # `assemble_per_element_result`; the dispatch table moved with it. `NO_MATCH` stays in {ExpressionTyper}, read
12
+ # lexically.
13
+ module PerElementAssemblers
14
+ module_function
15
+
16
+ PER_ELEMENT_ASSEMBLERS = {
17
+ map: :assemble_map_result,
18
+ collect: :assemble_map_result,
19
+ filter_map: :assemble_filter_map_result,
20
+ flat_map: :assemble_flat_map_result,
21
+ collect_concat: :assemble_flat_map_result,
22
+ select: :assemble_select_result,
23
+ filter: :assemble_select_result,
24
+ find_all: :assemble_select_result,
25
+ reject: :assemble_reject_result,
26
+ take_while: :assemble_take_while_result,
27
+ drop_while: :assemble_drop_while_result,
28
+ partition: :assemble_partition_result,
29
+ count: :assemble_count_result,
30
+ find: :assemble_find_result,
31
+ detect: :assemble_find_result,
32
+ rfind: :assemble_rfind_result,
33
+ find_index: :assemble_find_index_result,
34
+ index: :assemble_find_index_result,
35
+ rindex: :assemble_rindex_result
36
+ }.freeze
37
+ private_constant :PER_ELEMENT_ASSEMBLERS
38
+
39
+ def assemble_per_element_result(method_name, per_position, element_types)
40
+ handler = PER_ELEMENT_ASSEMBLERS[method_name]
41
+ send(handler, per_position, element_types) if handler
42
+ end
43
+
44
+ def assemble_map_result(per_position, _element_types)
45
+ Type::Combinator.tuple_of(*per_position)
46
+ end
47
+
48
+ def assemble_select_result(per_position, element_types)
49
+ assemble_filter_result(per_position, element_types, keep_on_truthy: true)
50
+ end
51
+
52
+ def assemble_reject_result(per_position, element_types)
53
+ assemble_filter_result(per_position, element_types, keep_on_truthy: false)
54
+ end
55
+
56
+ # `select` / `filter` / `reject`: keeps each receiver element whose per-position predicate result
57
+ # folds to a decisive `Constant` — Ruby-truthy for `select` / `filter`, Ruby-falsey for `reject`. The
58
+ # surviving elements assemble into a `Tuple`, strictly tighter than the RBS-projected `Array[Elem]`.
59
+ #
60
+ # Folds tightly only when EVERY position is a `Constant`: a single non-`Constant` position leaves the
61
+ # result cardinality unknown (the element might or might not survive), so this assembler declines and
62
+ # {ExpressionTyper#undecided_fold_floor} answers `Array[Elem]`. `[].select` style empty results are sound — an
63
+ # empty `Tuple` is the empty-array carrier.
64
+ def assemble_filter_result(per_position, element_types, keep_on_truthy:)
65
+ return nil unless per_position.all?(Type::Constant)
66
+
67
+ kept = element_types.each_index.filter_map do |index|
68
+ element_types[index] if truthy_constant?(per_position[index]) == keep_on_truthy
69
+ end
70
+ Type::Combinator.tuple_of(*kept)
71
+ end
72
+
73
+ # `take_while` / `drop_while`: stops at the first falsey `Constant` position when every preceding
74
+ # position is a truthy `Constant` (or takes/drops all elements when every position is a truthy
75
+ # `Constant`). Declines when an undecided position precedes the stopping point.
76
+ def assemble_take_while_result(per_position, element_types)
77
+ cutoff = leading_truthy_cutoff(per_position)
78
+ return nil if cutoff.nil?
79
+
80
+ Type::Combinator.tuple_of(*element_types[0...cutoff])
81
+ end
82
+
83
+ def assemble_drop_while_result(per_position, element_types)
84
+ cutoff = leading_truthy_cutoff(per_position)
85
+ return nil if cutoff.nil?
86
+
87
+ Type::Combinator.tuple_of(*element_types[cutoff..])
88
+ end
89
+
90
+ def leading_truthy_cutoff(per_position)
91
+ first_falsey_index = per_position.index { |type| falsey_constant?(type) }
92
+ return (per_position.all?(Type::Constant) ? per_position.size : nil) if first_falsey_index.nil?
93
+ return nil unless per_position[0...first_falsey_index].all?(Type::Constant)
94
+
95
+ first_falsey_index
96
+ end
97
+
98
+ # `partition`: returns `Tuple[truthy_tuple, falsey_tuple]` when every position folds to a `Constant`.
99
+ def assemble_partition_result(per_position, element_types)
100
+ return nil unless per_position.all?(Type::Constant)
101
+
102
+ truthy = assemble_filter_result(per_position, element_types, keep_on_truthy: true)
103
+ falsey = assemble_filter_result(per_position, element_types, keep_on_truthy: false)
104
+ Type::Combinator.tuple_of(truthy, falsey)
105
+ end
106
+
107
+ # `count`: returns `Constant[Integer]` of the truthy positions when every position folds to a `Constant`.
108
+ def assemble_count_result(per_position, _element_types)
109
+ return nil unless per_position.all?(Type::Constant)
110
+
111
+ Type::Combinator.constant_of(per_position.count { |type| truthy_constant?(type) })
112
+ end
113
+
114
+ # `filter_map` folds tightly only when every per-position result is a `Constant`: positions whose
115
+ # value is `nil` or `false` drop, the rest survive in declaration order. When any position is
116
+ # non-Constant the dispatcher declines (returns nil) so the RBS tier widens to `Array[U]`.
117
+ def assemble_filter_map_result(per_position, _element_types = nil)
118
+ return nil unless per_position.all?(Type::Constant)
119
+
120
+ kept = per_position.reject { |type| type.value.nil? || type.value == false }
121
+ Type::Combinator.tuple_of(*kept)
122
+ end
123
+
124
+ # `flat_map` flattens a single level: if the per-position result is a `Tuple`, its elements are
125
+ # concatenated; if it's a non-Array scalar carrier (`Constant<…>` over a non-Array literal) it
126
+ # contributes one element. We fold tightly only when every per-position result is one of those two
127
+ # recognisable shapes — `Nominal[Array[T]]`, `Union[…]`, and other opaque carriers decline so the RBS
128
+ # tier widens to `Array[U]`.
129
+ #
130
+ # `Type::Constant` only ever holds non-Array scalars (the carrier rejects Array literals), so a single
131
+ # `Constant` safely contributes itself as a single Tuple element.
132
+ def assemble_flat_map_result(per_position, _element_types = nil)
133
+ flattened = per_position.flat_map { |type| flat_map_contribution(type) }
134
+ return nil if flattened.nil? || flattened.any?(&:nil?)
135
+
136
+ Type::Combinator.tuple_of(*flattened)
137
+ end
138
+
139
+ def flat_map_contribution(type)
140
+ case type
141
+ when Type::Tuple then type.elements
142
+ when Type::Constant then [type]
143
+ else [nil]
144
+ end
145
+ end
146
+
147
+ # `find` / `detect`: returns the first receiver element whose block result is Ruby-truthy, or `nil`
148
+ # when no position folds to truthy.
149
+ #
150
+ # When the first decisive truthy position is found, the answer is the corresponding receiver element,
151
+ # joined with the element of every earlier position whose result is not a `Type::Constant`: such a
152
+ # position may match first, but the search ends at the truthy one whatever it answers, so the call
153
+ # never returns `nil`. When every position folds to falsey, the answer is `Constant[nil]`. With no
154
+ # decisive truthy position and some undecided one, it declines ({ExpressionTyper#undecided_fold_floor}).
155
+ def assemble_find_result(per_position, element_types)
156
+ candidates = first_match_candidates(per_position)
157
+ return candidates if candidates.nil? || candidates.equal?(NO_MATCH)
158
+
159
+ Type::Combinator.union(*candidates.map { |index| element_types[index] })
160
+ end
161
+
162
+ # `rfind`: reverse of `find` — returns the last receiver element whose block result is Ruby-truthy,
163
+ # joined with any undecided positions to its right.
164
+ def assemble_rfind_result(per_position, element_types)
165
+ candidates = last_match_candidates(per_position)
166
+ return candidates if candidates.nil? || candidates.equal?(NO_MATCH)
167
+
168
+ Type::Combinator.union(*candidates.map { |index| element_types[index] })
169
+ end
170
+
171
+ # `find_index` / `index`: returns the index of the first truthy position, or `Constant[nil]` when
172
+ # nothing matches, on {.assemble_find_result}'s terms.
173
+ def assemble_find_index_result(per_position, _element_types = nil)
174
+ candidates = first_match_candidates(per_position)
175
+ return candidates if candidates.nil? || candidates.equal?(NO_MATCH)
176
+
177
+ Type::Combinator.union(*candidates.map { |index| Type::Combinator.constant_of(index) })
178
+ end
179
+
180
+ # `rindex`: returns the index of the last truthy position, or `Constant[nil]` when nothing matches.
181
+ def assemble_rindex_result(per_position, _element_types = nil)
182
+ candidates = last_match_candidates(per_position)
183
+ return candidates if candidates.nil? || candidates.equal?(NO_MATCH)
184
+
185
+ Type::Combinator.union(*candidates.map { |index| Type::Combinator.constant_of(index) })
186
+ end
187
+
188
+ # The positions `find` may stop at — the first decisive truthy one and every undecided one before it —
189
+ # `NO_MATCH` when every position folds falsey, or nil when an undecided position has no truthy one after it.
190
+ def first_match_candidates(per_position)
191
+ first_truthy_index = per_position.index { |type| truthy_constant?(type) }
192
+ if first_truthy_index.nil?
193
+ return per_position.all?(Type::Constant) ? NO_MATCH : nil
194
+ end
195
+
196
+ undecided = (0...first_truthy_index).reject { |index| per_position[index].is_a?(Type::Constant) }
197
+ undecided + [first_truthy_index]
198
+ end
199
+
200
+ # The positions `rfind` / `rindex` may stop at — the last decisive truthy one and every undecided one
201
+ # after it — `NO_MATCH` when every position folds falsey, or nil when an undecided position has no
202
+ # truthy one before it.
203
+ def last_match_candidates(per_position)
204
+ last_truthy_index = per_position.rindex { |type| truthy_constant?(type) }
205
+ if last_truthy_index.nil?
206
+ return per_position.all?(Type::Constant) ? NO_MATCH : nil
207
+ end
208
+
209
+ undecided = ((last_truthy_index + 1)...per_position.size).reject do |index|
210
+ per_position[index].is_a?(Type::Constant)
211
+ end
212
+ [last_truthy_index] + undecided
213
+ end
214
+
215
+ def truthy_constant?(type)
216
+ type.is_a?(Type::Constant) && !type.value.nil? && type.value != false
217
+ end
218
+
219
+ def falsey_constant?(type)
220
+ type.is_a?(Type::Constant) && (type.value.nil? || type.value == false)
221
+ end
222
+
223
+ private_class_method :assemble_map_result, :assemble_select_result, :assemble_reject_result,
224
+ :assemble_filter_result, :assemble_take_while_result, :assemble_drop_while_result,
225
+ :leading_truthy_cutoff, :assemble_partition_result, :assemble_count_result,
226
+ :assemble_filter_map_result, :assemble_flat_map_result, :flat_map_contribution,
227
+ :assemble_find_result, :assemble_rfind_result, :assemble_find_index_result,
228
+ :assemble_rindex_result, :first_match_candidates, :last_match_candidates,
229
+ :truthy_constant?, :falsey_constant?
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,218 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../analysis/dependency_recorder"
6
+ require_relative "../../reflection"
7
+ require_relative "../../source/constant_path"
8
+ require_relative "../../type"
9
+ require_relative "../method_dispatcher"
10
+ require_relative "../narrowing"
11
+
12
+ module Rigor
13
+ module Inference
14
+ class ExpressionTyper
15
+ # Typers of an assignment or compound write in value position: the rvalue for a plain write, and the value a `+=`
16
+ # / `||=` / `&&=` stores for a variable, constant or index target. Reads `scope` and `tracer`; calls back
17
+ # {ExpressionTyper#type_of}, {ExpressionTyper#dynamic_top} and {ExpressionTyper#resolve_constant_name}. The
18
+ # entries are `type_of_assignment_write`, `type_of_compound_variable_write`, `type_of_compound_constant_write`,
19
+ # `type_of_index_compound_write`; every other method is private to the module (ADR-116 C3). State is passed, not
20
+ # shared: each entry takes the values it reads from the typer as trailing positional parameters (`scope`,
21
+ # `tracer`) and the typer itself (`typer`) when it calls back. `ScopeIndexer` (for `DYNAMIC_TARGET_PREFIX`) is not
22
+ # required here: it requires this class, and a direct require makes a cycle (`ruby -w`). `StatementEvaluator` is
23
+ # not required either: requiring it from here changes the load order of the TypingState slot declarations. Both
24
+ # are loaded by `expression_typer.rb`'s own requires before any call.
25
+ module Writes
26
+ module_function
27
+
28
+ # All `*WriteNode` flavours expose a `.value` rvalue child. Their type is the type of that rvalue.
29
+ # Binding the result back into the scope is the responsibility of the statement-level evaluator
30
+ # (Slice 3), never of `type_of` itself.
31
+ def type_of_assignment_write(node, typer)
32
+ typer.type_of(node.value)
33
+ end
34
+
35
+ # `v += 1` / `@v ||= x` / `$v &&= x` as an EXPRESSION. The value of a compound write is the value it
36
+ # stores, which is a function of the variable's CURRENT binding — not of the rvalue alone. Typing it as
37
+ # `type_of(node.value)` answered `1` for `total += 1` whatever `total` held.
38
+ #
39
+ # `StatementEvaluator#compound_eval` already computes the right answer on the straight-line path, so the
40
+ # gap only showed where a body is typed WITHOUT that evaluator: the block-return pass types a
41
+ # single-statement body by expression alone, and issue #617 residue (3) is the live consequence —
42
+ # `total = 0; r = [1, 2].map { total += 1 }` folded to `[1, 1]`, and `r.last == 1` fired a false
43
+ # `flow.always-truthy-condition` on a program where it is `2`. Under the per-element fold the captured
44
+ # local is already bound to its converged `Integer` (issue #587 (b)); reading that binding here is what
45
+ # turns the stale pin into the honest `Integer`.
46
+ #
47
+ # The algebra is `StatementEvaluator#compound_result_type`'s, deliberately: the two must not disagree
48
+ # about what a compound write evaluates to. An unbound target is `Dynamic[top]` — the same fallback the
49
+ # evaluator takes — and an operator the receiver does not answer widens to `Dynamic[top]` rather than
50
+ # inventing the rvalue.
51
+ #
52
+ # Constant targets have {.type_of_compound_constant_write}, over the same algebra. Index targets have their
53
+ # own handler, {.type_of_index_compound_write}.
54
+ def type_of_compound_variable_write(node, scope, typer)
55
+ compound_write_value(node, compound_write_current_binding(node, scope), typer.type_of(node.value), scope,
56
+ typer)
57
+ end
58
+
59
+ # `H ||= 0` / `Conf::LIMIT += 1` as an EXPRESSION: the value it stores, by
60
+ # {.type_of_compound_variable_write}'s algebra. Typed as the rvalue alone, `H = { x: 1 }; v = (H ||= 0);
61
+ # v[:x]` reported `Integer#[]` on a program whose `v` is `H`, and `F.transform_values { |e| F ||= 0 }`
62
+ # folded every value to `0`.
63
+ #
64
+ # A constant has no scope binding, so its current binding is what a plain read of the same spelling
65
+ # resolves to at the write site — the lexical ladder, the in-source table of plain writes, then RBS —
66
+ # less the caller-derived rungs a top-level body keeps for reads Ruby raises on. The memoization idiom
67
+ # (`def registry = REGISTRY ||= {}`, legal where a plain `REGISTRY = {}` is a dynamic constant
68
+ # assignment) writes a constant nothing else binds, so an unresolved target keeps the rvalue reading
69
+ # exactly as an unbound variable does — unless a write other than a memo binds it
70
+ # ({.written_constant_binding}). A path whose base renders no static name (`klass::X`, `self::X`) names
71
+ # no binding the resolver can look up, and reads as unbound too, on the same condition.
72
+ def type_of_compound_constant_write(node, scope, typer)
73
+ rhs = typer.type_of(node.value)
74
+ compound_write_value(node, compound_write_constant_binding(node, scope, typer), rhs, scope, typer)
75
+ end
76
+
77
+ def compound_write_value(node, current, rhs, scope, typer)
78
+ case node
79
+ when Prism::LocalVariableOrWriteNode, Prism::InstanceVariableOrWriteNode,
80
+ Prism::ClassVariableOrWriteNode, Prism::GlobalVariableOrWriteNode,
81
+ Prism::ConstantOrWriteNode, Prism::ConstantPathOrWriteNode
82
+ # An UNBOUND target is the memoization idiom (`def self.default = @default ||= new`): nothing
83
+ # has written the variable on any path the analyzer saw, so the stored value is the rvalue.
84
+ # Reading it as `Dynamic[top] | rhs` would skip every memoized singleton in `sig-gen`
85
+ # (ADR-5 optimism; three `.default` readers went `sig.skipped.untyped-return` without this).
86
+ #
87
+ # The index rule's exception applies: an rvalue with no truthy part makes the write a guard, not a
88
+ # memo. `@settings ||= raise "boot first"` returns only when something the analyzer did not see set
89
+ # `@settings`, and `@verbose ||= false` answers `true` once one did, so the value is that unseen
90
+ # binding — `Dynamic[top]`, the statement evaluator's reading of an unbound target — never the
91
+ # rvalue's `bot` or `false` alone. A local is no exception: the scope a loop body is typed from does
92
+ # not carry what the previous iteration stored.
93
+ if current.nil?
94
+ return rhs unless Narrowing.narrow_truthy(rhs).is_a?(Type::Bot)
95
+
96
+ current = typer.dynamic_top
97
+ end
98
+
99
+ Type::Combinator.union(Narrowing.narrow_truthy(current), rhs)
100
+ when Prism::LocalVariableAndWriteNode, Prism::InstanceVariableAndWriteNode,
101
+ Prism::ClassVariableAndWriteNode, Prism::GlobalVariableAndWriteNode,
102
+ Prism::ConstantAndWriteNode, Prism::ConstantPathAndWriteNode
103
+ # `&&=` is no memo: an unset target returns its own `nil` without evaluating the rvalue (a constant or
104
+ # class variable raises instead). Beside the rvalue, an UNBOUND target's value is its own falsey value —
105
+ # `nil` when unset, or a falsey value a write the analyzer did not see stored — which
106
+ # `narrow_falsey(Dynamic[top])` carries as `Dynamic[top]`, the statement evaluator's reading. The index
107
+ # rule says the same of `h[k] &&= v`. Read as the rvalue, `if (@x &&= 1)` folded always-truthy on a
108
+ # program that takes the else arm.
109
+ Type::Combinator.union(Narrowing.narrow_falsey(current || typer.dynamic_top), rhs)
110
+ else
111
+ compound_operator_result(current || typer.dynamic_top, rhs, node.binary_operator, scope, typer)
112
+ end
113
+ end
114
+
115
+ # `h[k] += v` / `h[k] ||= v` / `h[k] &&= v` as an EXPRESSION. Like a variable compound write, its value is
116
+ # what it stores through `[]=` — the dispatched `h[k] + v`, `truthy(h[k]) | v`, `falsey(h[k]) | v` — which
117
+ # reads the slot's current type, recorded indexed narrowing included. Typed as the rvalue alone it answered
118
+ # `1` for `counts[w] += 1`, so `r = words.map { |w| counts[w] += 1 }` pinned every position to `1` and
119
+ # `r.last == 1` drew a false `flow.always-truthy-condition`.
120
+ #
121
+ # The statement evaluator already owned that algebra for the straight-line write and the `[]=` widening
122
+ # join, so this reads its answer rather than keeping a second copy. It asks for the value alone, not a
123
+ # whole `evaluate`: the widening and the narrowing record are scope effects a value position discards,
124
+ # and the memoizing `@cache[k] ||= build(k)` tail is common enough not to pay for them.
125
+ #
126
+ # One exception carries over, narrowed, from {.type_of_compound_variable_write}: a memoizing `||=` whose
127
+ # slot the analyzer has no evidence about reads as the rvalue. The evaluator's value method owns it,
128
+ # because it is decided on the `[]` read the evaluator performs.
129
+ def type_of_index_compound_write(node, scope, tracer)
130
+ StatementEvaluator.new(scope: scope, tracer: tracer).index_compound_write_value(node)
131
+ end
132
+
133
+ def compound_write_current_binding(node, scope)
134
+ case node
135
+ when Prism::LocalVariableOperatorWriteNode, Prism::LocalVariableOrWriteNode,
136
+ Prism::LocalVariableAndWriteNode then scope.local(node.name)
137
+ when Prism::InstanceVariableOperatorWriteNode, Prism::InstanceVariableOrWriteNode,
138
+ Prism::InstanceVariableAndWriteNode then scope.ivar(node.name)
139
+ when Prism::ClassVariableOperatorWriteNode, Prism::ClassVariableOrWriteNode,
140
+ Prism::ClassVariableAndWriteNode then scope.cvar(node.name)
141
+ else scope.global(node.name)
142
+ end
143
+ end
144
+
145
+ def compound_write_constant_binding(node, scope, typer)
146
+ case node
147
+ when Prism::ConstantOperatorWriteNode, Prism::ConstantOrWriteNode, Prism::ConstantAndWriteNode
148
+ full_name = node.name.to_s
149
+ rooted = false
150
+ else
151
+ full_name = Source::ConstantPath.qualified_name_or_nil(node.target)
152
+ return unnamed_path_binding(node.target, scope, typer) if full_name.nil?
153
+
154
+ rooted = Source::ConstantPath.rooted?(node.target)
155
+ end
156
+ typer.resolve_constant_name(full_name, rooted: rooted, caller_derived: false) ||
157
+ written_constant_binding(full_name, rooted, scope, typer)
158
+ end
159
+
160
+ # A constant the plain read cannot resolve may still be bound: another file writes a value that never
161
+ # published (`H = { x: 1 }`), the compound write withdrew one by being the second writer the census
162
+ # counts, or this file writes it in a form its own table does not carry (`A, B = …`, `A = B = …`). The
163
+ # value is one the analyzer does not have, so the binding is `Dynamic[top]` rather than the memo's
164
+ # unbound reading. The one write that does not bind is a memo `||=` no other file shares, so the
165
+ # memoization idiom keeps that reading however often its file repeats it; memos of the name in two files
166
+ # bind, since either may load first and set what the other reads.
167
+ #
168
+ # Which name the write reads is the ladder's to decide, not the spelling's: the same resolution runs
169
+ # again with only the census's binding names in the in-source table, so `Other::REGISTRY` elsewhere
170
+ # leaves a top-level `REGISTRY ||= {}` its memo reading. Two census spellings the ladder cannot reach
171
+ # still bind: a write through a base nothing names (`k::X = 1`), which may have written any of them,
172
+ # and a path the census keeps as written ([#690](https://github.com/rigortype/rigor/issues/690)), so
173
+ # `Foo::BAR = …` inside `module M` binds `M::Foo::BAR ||= 0`.
174
+ def written_constant_binding(full_name, rooted, scope, typer)
175
+ names = scope.bound_constant_names(full_name)
176
+ return nil if names.empty?
177
+ return typer.dynamic_top if names.any? { |name| unladdered_census_name?(name, full_name) }
178
+
179
+ written = names.to_h { |name| [name, typer.dynamic_top] }
180
+ probe = scope.with_discovery(scope.discovery.with(in_source_constants: written))
181
+ Reflection.resolve_constant_type(full_name, scope: probe, rooted: rooted, caller_derived: false)
182
+ end
183
+
184
+ def unladdered_census_name?(name, full_name)
185
+ return true if name.start_with?(ScopeIndexer::DYNAMIC_TARGET_PREFIX)
186
+
187
+ name.include?("::") && full_name.end_with?("::#{name}")
188
+ end
189
+
190
+ # `self::X ||= v` / `klass::X ||= v` name no constant the resolver can look up, so any binding write
191
+ # that shares the last segment may be the one the base reaches. The answer now depends on other files'
192
+ # writes, and this path never reaches `Reflection.resolve_constant_type`, which records the
193
+ # `constant:<segment>` edge for every other form — so it records the edge itself, or an incremental
194
+ # run keeps serving the reading from before another file's write of the segment appeared.
195
+ def unnamed_path_binding(target, scope, typer)
196
+ segment = target.name&.to_s
197
+ return nil if segment.nil?
198
+
199
+ Analysis::DependencyRecorder.read_name(:constant, segment) if Analysis::DependencyRecorder.active?
200
+ typer.dynamic_top unless scope.bound_constant_names(segment).empty?
201
+ end
202
+
203
+ def compound_operator_result(current, rhs, operator, scope, typer)
204
+ MethodDispatcher.dispatch(
205
+ receiver_type: current,
206
+ method_name: operator.to_sym,
207
+ arg_types: [rhs],
208
+ environment: scope.environment
209
+ ) || typer.dynamic_top
210
+ end
211
+
212
+ private_class_method :compound_write_value, :compound_write_current_binding, :compound_write_constant_binding,
213
+ :written_constant_binding, :unladdered_census_name?, :unnamed_path_binding,
214
+ :compound_operator_result
215
+ end
216
+ end
217
+ end
218
+ end