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,322 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../type"
4
+
5
+ module Rigor
6
+ module Inference
7
+ module Narrowing
8
+ # ADR-116 C1 — the complements, finite domains and integer-range algebra that `Narrowing`'s type-level
9
+ # entries sit on: the complement of a `Difference`, `IntegerRange`, `Intersection` or `Refined` within a
10
+ # current type ({.complement_difference}, {.complement_integer_range}, {.complement_intersection},
11
+ # {.complement_refined}), the falsey and nil fragments of the carriers the fast paths leave alone, and the
12
+ # `==` / `!=` narrowing over a finite trusted-literal domain or a singleton.
13
+ #
14
+ # Plain functions on `Rigor::Type` values: no scope, no environment. The one call back is
15
+ # `Narrowing.narrow_not_refinement`, which {.complement_intersection} applies to each member of an
16
+ # intersection. Everything here is reached through `Narrowing`'s own entries (`narrow_truthy`,
17
+ # `narrow_falsey`, `narrow_nil`, `narrow_equal`, `narrow_not_equal`, `narrow_not_refinement`) and the
18
+ # equality predicate analyser.
19
+ # rubocop:disable-next Metrics/ModuleLength
20
+ module Complements
21
+ module_function
22
+
23
+ TRUSTED_EQUALITY_LITERAL_CLASSES = [String, Symbol, Integer, TrueClass, FalseClass, NilClass].freeze
24
+ SINGLETON_LITERAL_CLASSES = [TrueClass, FalseClass, NilClass].freeze
25
+ private_constant :TRUSTED_EQUALITY_LITERAL_CLASSES, :SINGLETON_LITERAL_CLASSES
26
+
27
+ # Complement of `Difference[base, Constant[v]]` within `current_type`. Walks the
28
+ # current type's union members, keeps each member disjoint from `base` (those values
29
+ # were never in the refinement to begin with), and adds the removed-value `Constant[v]`
30
+ # exactly once when any current member covers it. Members that are fully contained in
31
+ # the refinement (i.e. inside `base` and NOT equal to the removed value) are dropped —
32
+ # they are exactly the values the negation excludes.
33
+ def complement_difference(current_type, difference)
34
+ base = difference.base
35
+ removed = difference.removed
36
+ parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
37
+
38
+ survivors = []
39
+ add_removed = false
40
+ parts.each do |part|
41
+ if base_disjoint?(base, part)
42
+ survivors << part
43
+ elsif part_covers_constant?(part, removed)
44
+ add_removed = true
45
+ end
46
+ end
47
+ survivors << removed if add_removed
48
+
49
+ return current_type if survivors.empty?
50
+
51
+ Type::Combinator.union(*survivors)
52
+ end
53
+
54
+ def base_disjoint?(base, part)
55
+ base.accepts(part, mode: :gradual).no?
56
+ end
57
+
58
+ def part_covers_constant?(part, constant)
59
+ result = part.accepts(constant, mode: :gradual)
60
+ result.yes? || result.maybe?
61
+ end
62
+
63
+ # Complement of an `IntegerRange[a, b]` within `current_type`. Splits the range
64
+ # complement into the two open halves `Integer[..a-1]` and `Integer[b+1..]` (skipping a
65
+ # half when its bound is infinity), then intersects each half with the integer-domain
66
+ # parts of `current_type`. Non-integer parts of a Union receiver (nil, String, …)
67
+ # survive unchanged.
68
+ def complement_integer_range(current_type, range)
69
+ halves = integer_range_complement_halves(range)
70
+ parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
71
+
72
+ survivors = []
73
+ parts.each do |part|
74
+ if integer_member?(part)
75
+ halves.each do |half|
76
+ meet = intersect_integer_part(part, half)
77
+ survivors << meet unless meet.nil? || meet.is_a?(Type::Bot)
78
+ end
79
+ else
80
+ survivors << part
81
+ end
82
+ end
83
+
84
+ return current_type if survivors.empty?
85
+
86
+ Type::Combinator.union(*survivors)
87
+ end
88
+
89
+ # Returns the two open halves of an IntegerRange's complement: the left half
90
+ # `Integer[..a-1]` (when `a` is finite) and the right half `Integer[b+1..]` (when `b` is finite).
91
+ # Universal ranges (both bounds infinite) yield an empty array — the complement is
92
+ # empty.
93
+ def integer_range_complement_halves(range)
94
+ halves = []
95
+ left_max = range.min
96
+ right_min = range.max
97
+
98
+ if left_max.is_a?(Integer)
99
+ halves << Type::Combinator.integer_range(Type::IntegerRange::NEG_INFINITY, left_max - 1)
100
+ end
101
+ if right_min.is_a?(Integer)
102
+ halves << Type::Combinator.integer_range(right_min + 1, Type::IntegerRange::POS_INFINITY)
103
+ end
104
+ halves
105
+ end
106
+
107
+ def integer_member?(part)
108
+ case part
109
+ when Type::Constant then part.value.is_a?(Integer)
110
+ when Type::IntegerRange then true
111
+ when Type::Nominal then part.class_name == "Integer"
112
+ else false
113
+ end
114
+ end
115
+
116
+ # Intersect an integer-domain part with a complement half-range. For a
117
+ # Nominal[Integer] receiver the meet is the half itself; for an existing IntegerRange
118
+ # the meet narrows both bounds; for a Constant[Integer] the meet is the constant when
119
+ # the half covers it, otherwise nil.
120
+ def intersect_integer_part(part, half)
121
+ case part
122
+ when Type::Nominal
123
+ half
124
+ when Type::IntegerRange
125
+ integer_range_meet(part, half)
126
+ when Type::Constant
127
+ half.covers?(part.value) ? part : nil
128
+ end
129
+ end
130
+
131
+ def integer_range_meet(left, right)
132
+ low = numeric_to_bound([integer_bound_value(left.min), integer_bound_value(right.min)].max)
133
+ high = numeric_to_bound([integer_bound_value(left.max), integer_bound_value(right.max)].min)
134
+ return nil if integer_range_disjoint?(low, high)
135
+
136
+ Type::Combinator.integer_range(low, high)
137
+ end
138
+
139
+ def integer_bound_value(bound)
140
+ return -Float::INFINITY if bound == Type::IntegerRange::NEG_INFINITY
141
+ return Float::INFINITY if bound == Type::IntegerRange::POS_INFINITY
142
+
143
+ bound
144
+ end
145
+
146
+ def numeric_to_bound(value)
147
+ return Type::IntegerRange::NEG_INFINITY if value == -Float::INFINITY
148
+ return Type::IntegerRange::POS_INFINITY if value == Float::INFINITY
149
+
150
+ value.to_i
151
+ end
152
+
153
+ def integer_range_disjoint?(low, high)
154
+ return false if low == Type::IntegerRange::NEG_INFINITY
155
+ return false if high == Type::IntegerRange::POS_INFINITY
156
+
157
+ low > high
158
+ end
159
+
160
+ # De Morgan: `D \ (M1 ∩ M2 ∩ …) = (D \ M1) ∪ (D \ M2) ∪ …`. Each member's complement is
161
+ # computed independently within `current_type` and the results are unioned. Members the
162
+ # algebra cannot complement contribute `current_type` itself, so the union widens to
163
+ # `current_type` overall — sound but imprecise.
164
+ def complement_intersection(current_type, intersection)
165
+ per_member = intersection.members.map do |member|
166
+ Narrowing.narrow_not_refinement(current_type, member)
167
+ end
168
+ Type::Combinator.union(*per_member)
169
+ end
170
+
171
+ # v0.0.7 — complement of a `Refined[base, predicate]` refinement within `current_type`.
172
+ # Walks the current type's union members; parts that are disjoint from the refinement's
173
+ # base survive unchanged; parts equal to the refinement itself drop entirely (they were
174
+ # exactly the negated subset); parts that overlap the base contribute `Difference[part,
175
+ # refined]` so downstream narrowing knows the refinement subset is excluded.
176
+ #
177
+ # v0.0.9 — when the predicate has a registered complement (see
178
+ # {Type::Refined::COMPLEMENT_PAIRS}) and the part is exactly the refinement's base, the
179
+ # narrowing returns `Refined[base, complement_predicate]` instead of
180
+ # `Difference[base, refined]`. This is the `~T` symmetry the spec promises:
181
+ # `~lowercase-string` narrows `String` to `non-lowercase-string` rather than
182
+ # `Difference[String, lowercase-string]`.
183
+ #
184
+ # Predicates without a registered complement still fall back to the imprecise but sound
185
+ # `Difference[part, refined]` carrier so behaviour is unchanged for untouched call
186
+ # sites.
187
+ def complement_refined(current_type, refined)
188
+ complement = registered_complement_for(refined)
189
+ parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
190
+ survivors = parts.filter_map { |part| complement_refined_part(part, refined, complement) }
191
+ return current_type if survivors.empty?
192
+
193
+ Type::Combinator.union(*survivors)
194
+ end
195
+
196
+ def complement_refined_part(part, refined, complement)
197
+ return nil if part == refined
198
+ return part if base_disjoint?(refined.base, part)
199
+ return complement if complement && part == refined.base
200
+
201
+ Type::Combinator.difference(part, refined)
202
+ end
203
+
204
+ def registered_complement_for(refined)
205
+ complement_id = refined.complement_predicate_id
206
+ return nil if complement_id.nil?
207
+
208
+ Type::Combinator.refined(refined.base, complement_id)
209
+ end
210
+
211
+ def falsey_value?(value)
212
+ value.nil? || value == false
213
+ end
214
+
215
+ def falsey_nominal?(nominal)
216
+ %w[NilClass FalseClass].include?(nominal.class_name)
217
+ end
218
+
219
+ # Carriers that the {.narrow_falsey} fast path does not handle by structural
220
+ # inspection. Singleton/Tuple/HashShape inhabit truthy values, so their falsey fragment
221
+ # is empty; everything else (Top, Dynamic, Bot, and any future carrier) stays
222
+ # conservative and is returned unchanged.
223
+ def narrow_falsey_other(type)
224
+ case type
225
+ when Type::Singleton, Type::Tuple, Type::HashShape then Type::Combinator.bot
226
+ else type
227
+ end
228
+ end
229
+
230
+ # Carriers that the {.narrow_nil} fast path does not handle by structural inspection.
231
+ # Top/Dynamic narrow to `Constant[nil]` so dispatch resolves through `NilClass`; Bot is
232
+ # its own nil fragment; the remaining carriers (Singleton, Tuple, HashShape, and any
233
+ # future carrier whose inhabitants exclude nil) collapse to `Bot`.
234
+ def narrow_nil_other(type)
235
+ case type
236
+ when Type::Dynamic, Type::Top then Type::Combinator.constant_of(nil)
237
+ when Type::Bot then type
238
+ else Type::Combinator.bot
239
+ end
240
+ end
241
+
242
+ def trusted_equality_literal?(literal)
243
+ literal.is_a?(Type::Constant) &&
244
+ TRUSTED_EQUALITY_LITERAL_CLASSES.include?(literal.value.class)
245
+ end
246
+
247
+ def singleton_literal?(literal)
248
+ SINGLETON_LITERAL_CLASSES.include?(literal.value.class)
249
+ end
250
+
251
+ def finite_trusted_literal_domain?(type)
252
+ case type
253
+ when Type::Bot then true
254
+ when Type::Constant then trusted_equality_literal?(type)
255
+ when Type::Union then type.members.all? { |member| finite_trusted_literal_domain?(member) }
256
+ else false
257
+ end
258
+ end
259
+
260
+ def narrow_finite_equal(type, literal)
261
+ case type
262
+ when Type::Bot then type
263
+ when Type::Constant then type == literal ? type : Type::Combinator.bot
264
+ when Type::Union
265
+ Type::Combinator.union(*type.members.map { |member| narrow_finite_equal(member, literal) })
266
+ else Type::Combinator.bot
267
+ end
268
+ end
269
+
270
+ def narrow_finite_not_equal(type, literal)
271
+ case type
272
+ when Type::Constant then type == literal ? Type::Combinator.bot : type
273
+ when Type::Union
274
+ Type::Combinator.union(*type.members.map { |member| narrow_finite_not_equal(member, literal) })
275
+ else type
276
+ end
277
+ end
278
+
279
+ def narrow_singleton_equal(type, literal)
280
+ case type
281
+ when Type::Constant then type == literal ? type : Type::Combinator.bot
282
+ when Type::Nominal then singleton_nominal_matches?(type, literal) ? type : Type::Combinator.bot
283
+ when Type::Union
284
+ Type::Combinator.union(*type.members.map { |member| narrow_singleton_equal(member, literal) })
285
+ else narrow_singleton_equal_other(type)
286
+ end
287
+ end
288
+
289
+ def narrow_singleton_equal_other(type)
290
+ case type
291
+ when Type::Singleton, Type::Tuple, Type::HashShape then Type::Combinator.bot
292
+ else type
293
+ end
294
+ end
295
+
296
+ def narrow_singleton_not_equal(type, literal)
297
+ case type
298
+ when Type::Constant then type == literal ? Type::Combinator.bot : type
299
+ when Type::Nominal then singleton_nominal_matches?(type, literal) ? Type::Combinator.bot : type
300
+ when Type::Union
301
+ Type::Combinator.union(*type.members.map { |member| narrow_singleton_not_equal(member, literal) })
302
+ else type
303
+ end
304
+ end
305
+
306
+ def singleton_nominal_matches?(nominal, literal)
307
+ case literal.value
308
+ when nil then nominal.class_name == "NilClass"
309
+ when true then nominal.class_name == "TrueClass"
310
+ when false then nominal.class_name == "FalseClass"
311
+ else false
312
+ end
313
+ end
314
+
315
+ private_class_method :base_disjoint?, :part_covers_constant?, :integer_range_complement_halves,
316
+ :integer_member?, :intersect_integer_part, :integer_range_meet, :integer_bound_value,
317
+ :numeric_to_bound, :integer_range_disjoint?, :complement_refined_part,
318
+ :registered_complement_for, :narrow_singleton_equal_other, :singleton_nominal_matches?
319
+ end
320
+ end
321
+ end
322
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../../type"
5
+ require_relative "../deferred_mutation"
6
+ require_relative "../key_presence_guard"
7
+
8
+ module Rigor
9
+ module Inference
10
+ module Narrowing
11
+ # ADR-116 C1 — the emptiness and key-presence predicate analysers: `empty?` / `any?` / `none?` over an
12
+ # array receiver ({.analyse_array_emptiness_predicate}), and `key?` / `has_key?` / `include?` / `member?` /
13
+ # `fetch` over a hash receiver ({.analyse_key_presence_predicate}), with the `HashShape` key promotion and
14
+ # removal they apply. {.static_hash_key} is the literal-key reader `Narrowing`'s `respond_to?` predicate
15
+ # shares.
16
+ #
17
+ # Calls nothing in `Narrowing`; `Narrowing.dispatch_call_simple` and `Narrowing.analyse_respond_to_predicate`
18
+ # are its callers.
19
+ module PresencePredicates
20
+ module_function
21
+
22
+ # ADR-47 §4-4 (Elixir `tuple_size`/non-empty analogue) — a bare `arr.empty?` /
23
+ # `arr.any?` / `arr.none?` (no block, no args) narrows an Array-typed receiver to
24
+ # `non-empty-array[T]` on the edge that implies "at least one element":
25
+ #
26
+ # - `empty?` → false edge (the array is NOT empty)
27
+ # - `any?` → true edge (a truthy element exists ⇒ non-empty)
28
+ # - `none?` → false edge (a truthy element exists ⇒ non-empty)
29
+ #
30
+ # The opposite edge is the conservative no-op (`any?`/`none?` falseness does not imply
31
+ # emptiness — the array may hold only falsey elements; `empty?` truth could narrow to an
32
+ # empty array but that carrier move is deferred). Only `Nominal[Array, [T]]` receivers
33
+ # narrow — `Tuple` is already known-length, `Dynamic` is left alone (gradual
34
+ # guarantee), and a non-Array receiver (`String#empty?`, `Range#any?`, …) bails so the
35
+ # existing string / predicate paths still run.
36
+ def analyse_array_emptiness_predicate(node, scope, name)
37
+ return nil if node.block
38
+ return nil unless node.arguments.nil? || node.arguments.arguments.empty?
39
+
40
+ reader, writer = emptiness_receiver_accessors(node.receiver)
41
+ return nil if reader.nil?
42
+
43
+ current = scope.public_send(reader, node.receiver.name)
44
+ return nil if current.nil?
45
+
46
+ non_empty = narrow_to_non_empty_array(current)
47
+ return nil if non_empty.equal?(current) # receiver is not an Array shape → opaque
48
+ # Issue #3340 — a closure that may run later can empty the local after the guard, so the guard proves
49
+ # nothing that lasts ({DeferredMutation}).
50
+ return nil if reader == :local && DeferredMutation.empties?(scope, node.receiver.name, node)
51
+
52
+ non_empty_scope = scope.public_send(writer, node.receiver.name, non_empty)
53
+ name == :any? ? [non_empty_scope, scope] : [scope, non_empty_scope]
54
+ end
55
+
56
+ def emptiness_receiver_accessors(receiver)
57
+ case receiver
58
+ when Prism::LocalVariableReadNode then %i[local with_local]
59
+ when Prism::InstanceVariableReadNode then %i[ivar with_ivar]
60
+ else [nil, nil]
61
+ end
62
+ end
63
+
64
+ # Refines `Array[T]` (and every Array member of a Union) to `non-empty-array[T]`;
65
+ # returns the input unchanged when nothing applies, so the caller can detect "no
66
+ # narrowing".
67
+ def narrow_to_non_empty_array(type)
68
+ case type
69
+ when Type::Nominal
70
+ return type unless type.class_name == "Array"
71
+
72
+ Type::Combinator.non_empty_array(type.type_args.first || Type::Combinator.top)
73
+ when Type::Union
74
+ Type::Combinator.union(*type.members.map { |member| narrow_to_non_empty_array(member) })
75
+ else
76
+ type
77
+ end
78
+ end
79
+
80
+ # ADR-47 §4-3 (Elixir `is_map_key/2` analogue) — `h.key?(:foo)` narrows the receiver on
81
+ # the truthy edge: a `HashShape` whose `:foo` is an OPTIONAL key has it promoted to
82
+ # REQUIRED, so a subsequent `h[:foo]` reads the declared value type instead of `value |
83
+ # nil` (the optionality nil is gone). Sound because key presence removes only the
84
+ # optionality-injected nil, never the value's own intrinsic nil (`h = {foo: nil}` keeps
85
+ # `h[:foo]` nil-typed). The falsey edge is left unchanged — "key absent" is the
86
+ # conservative no-op. Only literal `Symbol`/`String` arguments and
87
+ # `LocalVariableReadNode`/`InstanceVariableReadNode` receivers narrow; everything else
88
+ # (Dynamic, `Nominal[Hash]`, method-chain receivers) bails to no narrowing. A non-literal
89
+ # key goes to {#analyse_computed_key_presence_predicate} (issue #1703).
90
+ def analyse_key_presence_predicate(node, scope)
91
+ return nil if node.arguments.nil?
92
+ return nil unless node.arguments.arguments.size == 1
93
+
94
+ # A literal key promotes a shape's optional key only for `key?` / `has_key?`; `include?` / `member?` reach
95
+ # the computed-key guard alone, so a String or Array receiver's `include?` keeps its own narrowings.
96
+ key = static_hash_key(node.arguments.arguments.first) if %i[key? has_key?].include?(node.name)
97
+ return analyse_computed_key_presence_predicate(node, scope) if key.nil?
98
+
99
+ case node.receiver
100
+ when Prism::LocalVariableReadNode
101
+ key_presence_scopes(node.receiver.name, key, scope, reader: :local, writer: :with_local)
102
+ when Prism::InstanceVariableReadNode
103
+ key_presence_scopes(node.receiver.name, key, scope, reader: :ivar, writer: :with_ivar)
104
+ end
105
+ end
106
+
107
+ # Issue #1703 — `H.key?(k)` / `has_key?` / `include?` / `member?` with a non-literal key (a local, an ivar,
108
+ # or a reader chain such as `prop.column_type`) on a closed hash shape held in a local, an ivar or a
109
+ # constant. The truthy edge records the guard, and a later `H[k]` of the same receiver and structurally the
110
+ # same key drops the miss `nil` ({KeyPresenceGuard}). The falsey edge is the no-op: an
111
+ # absent computed key says nothing a shape can record. Nil when the guard does not apply, so a collection's
112
+ # `include?` still reaches the membership and String predicates.
113
+ def analyse_computed_key_presence_predicate(node, scope)
114
+ truthy = KeyPresenceGuard.record(node, scope)
115
+ truthy && [truthy, scope]
116
+ end
117
+
118
+ def key_presence_scopes(name, key, scope, reader:, writer:)
119
+ current = scope.public_send(reader, name)
120
+ return nil if current.nil?
121
+
122
+ truthy = narrow_hash_key_present(current, key)
123
+ # §4-3 false edge — `h.key?(:foo)` being false proves `:foo` is absent, so on that
124
+ # edge `h[:foo]` reads `nil`. Remove the (optional) key from the shape; a required
125
+ # key makes the false edge dead and an unknown key is already nil, both no-ops.
126
+ falsey = narrow_hash_key_absent(current, key)
127
+ return nil if truthy.equal?(current) && falsey.equal?(current) # predicate is opaque
128
+
129
+ truthy_scope = truthy.equal?(current) ? scope : scope.public_send(writer, name, truthy)
130
+ falsey_scope = falsey.equal?(current) ? scope : scope.public_send(writer, name, falsey)
131
+ [truthy_scope, falsey_scope]
132
+ end
133
+
134
+ def static_hash_key(node)
135
+ case node
136
+ when Prism::SymbolNode then node.unescaped.to_sym
137
+ when Prism::StringNode then node.unescaped
138
+ end
139
+ end
140
+
141
+ # Promotes `key` from optional to required across a HashShape (or every HashShape
142
+ # member of a Union). Returns the input unchanged when nothing applies, so the caller
143
+ # can detect "no narrowing".
144
+ def narrow_hash_key_present(type, key)
145
+ case type
146
+ when Type::HashShape
147
+ promote_hash_key(type, key)
148
+ when Type::Union
149
+ Type::Combinator.union(*type.members.map { |member| narrow_hash_key_present(member, key) })
150
+ else
151
+ type
152
+ end
153
+ end
154
+
155
+ def promote_hash_key(shape, key)
156
+ return shape unless shape.pairs.key?(key) # unknown key → no value type to assert
157
+ return shape unless shape.optional_key?(key) # already required → nothing to promote
158
+
159
+ Type::HashShape.new(
160
+ shape.pairs,
161
+ required_keys: shape.required_keys + [key],
162
+ optional_keys: shape.optional_keys - [key],
163
+ read_only_keys: shape.read_only_keys,
164
+ extra_keys: shape.extra_keys
165
+ )
166
+ end
167
+
168
+ # §4-3 false edge — drops `key` from a HashShape (or every HashShape member of a Union)
169
+ # so the proven-absent key reads `nil`. Returns the input unchanged when nothing
170
+ # applies (caller detects "no narrowing"). Only an *optional* present key is removed: a
171
+ # required key makes `key?` always true (the false edge is dead, leave the shape opaque)
172
+ # and a key absent from `pairs` already reads `nil` on a closed shape. On an open shape an
173
+ # undeclared key keeps reading `untyped` on this edge — sound but imprecise, since the shape
174
+ # carries no per-key exclusion to record what the guard just proved absent.
175
+ def narrow_hash_key_absent(type, key)
176
+ case type
177
+ when Type::HashShape
178
+ remove_hash_key(type, key)
179
+ when Type::Union
180
+ Type::Combinator.union(*type.members.map { |member| narrow_hash_key_absent(member, key) })
181
+ else
182
+ type
183
+ end
184
+ end
185
+
186
+ def remove_hash_key(shape, key)
187
+ return shape unless shape.pairs.key?(key)
188
+ return shape unless shape.optional_key?(key)
189
+
190
+ Type::HashShape.new(
191
+ shape.pairs.except(key),
192
+ required_keys: shape.required_keys,
193
+ optional_keys: shape.optional_keys - [key],
194
+ read_only_keys: shape.read_only_keys - [key],
195
+ extra_keys: shape.extra_keys
196
+ )
197
+ end
198
+
199
+ private_class_method :emptiness_receiver_accessors, :narrow_to_non_empty_array,
200
+ :analyse_computed_key_presence_predicate, :key_presence_scopes, :narrow_hash_key_present,
201
+ :promote_hash_key, :narrow_hash_key_absent, :remove_hash_key
202
+ end
203
+ end
204
+ end
205
+ end