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,506 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../type"
4
+ require_relative "../anonymous_meta_class"
5
+ require_relative "../mutation_widening"
6
+ require_relative "argument_nodes"
7
+ require_relative "declared_constructor"
8
+ require_relative "set_folding"
9
+
10
+ module Rigor
11
+ module Inference
12
+ module MethodDispatcher
13
+ # The meta-introspection tier: `x.class` on a receiver of known identity, and `Singleton[X].new` with its
14
+ # curated constructor lifts (`Array`, `Range`, `Set`, `Hash`, `Regexp`, `Date`, `Struct`, `Class.new`, the
15
+ # scalar-constant classes). It is the first of `MethodDispatcher`'s `PRECISE_TIERS_HEAD`, behind the `Data` and
16
+ # `Struct` value tiers, whose receivers `meta_new` would otherwise answer as a bare `Nominal`.
17
+ module MetaIntrospection # rubocop:disable Metrics/ModuleLength
18
+ module_function
19
+
20
+ # Slice 7 phase 8 — meta-introspection shortcuts. The default `Object#class` RBS return
21
+ # type is `Class`, but for a receiver of known nominal identity we can do better:
22
+ # `instance_of(Foo).class` is `Singleton[Foo]` (the class object itself), which downstream
23
+ # dispatch uses to resolve `self.class.some_class_method`. The same logic answers
24
+ # `Foo.class` as `Singleton[Class]` (deliberate; calling `.class` on a class object yields
25
+ # `Class`, the metaclass). We also special-case `is_a?`-adjacent calls and the trivial
26
+ # `instance_of?(self)` later as the rule catalogue grows; today `class` and `new` are handled.
27
+ def try_dispatch(context)
28
+ receiver_type = context.receiver
29
+ method_name = context.method_name
30
+ arg_types = context.args
31
+ case method_name
32
+ when :class then meta_class(receiver_type, context.environment)
33
+ when :new then meta_new(receiver_type, arg_types, context)
34
+ end
35
+ end
36
+
37
+ def meta_class(receiver_type, environment = nil)
38
+ case receiver_type
39
+ when Type::Nominal
40
+ # An instance of module `M` is an instance of some class that includes `M`, never of `M`
41
+ # itself: `self.class` in `M`'s instance method is that includer, which no RBS type names.
42
+ # `Dynamic[Class]` keeps the surface every class shares (`name`, `ancestors`) and answers
43
+ # nothing for the includer's own singleton methods.
44
+ if environment&.rbs_module?(receiver_type.class_name)
45
+ return Type::Combinator.dynamic(Type::Combinator.nominal_of("Class"))
46
+ end
47
+
48
+ Type::Combinator.singleton_of(receiver_type.class_name)
49
+ when Type::Constant then constant_metaclass(receiver_type.value)
50
+ end
51
+ end
52
+
53
+ # `Singleton[Foo].new` returns `Nominal[Foo]` (a fresh instance), regardless of whether Foo
54
+ # is in RBS. This short-circuits the Class.new generic-`instance` plumbing for user
55
+ # classes, so a discovered-class `ScanAccumulator.new` types as
56
+ # `Nominal[ScanAccumulator]` rather than `Class`.
57
+ #
58
+ # v0.0.7 — for the curated set of immutable scalar-shaped classes that
59
+ # `Type::Constant::SCALAR_CLASSES` accepts (today: `Pathname`), `.new(Constant<…>)` lifts
60
+ # to a `Constant<…>` carrier so downstream method calls fold through the standard catalog
61
+ # tier.
62
+ def meta_new(receiver_type, arg_types = [], context = nil)
63
+ return nil unless receiver_type.is_a?(Type::Singleton)
64
+
65
+ constant_lift = constant_constructor_lift(receiver_type.class_name, arg_types)
66
+ return constant_lift if constant_lift
67
+
68
+ array_lift = array_new_lift(receiver_type.class_name, arg_types, context&.block_type)
69
+ return array_lift if array_lift
70
+
71
+ range_lift = range_new_lift(receiver_type.class_name, arg_types)
72
+ return range_lift if range_lift
73
+
74
+ set_lift = set_new_lift(receiver_type.class_name, arg_types, context)
75
+ return set_lift if set_lift
76
+
77
+ hash_lift = hash_new_lift(receiver_type.class_name, arg_types, context&.call_node)
78
+ return hash_lift if hash_lift
79
+
80
+ regexp_lift = regexp_new_lift(receiver_type.class_name, arg_types)
81
+ return regexp_lift if regexp_lift
82
+
83
+ date_lift = date_new_lift(receiver_type.class_name, arg_types)
84
+ return date_lift if date_lift
85
+
86
+ struct_new_lift = struct_new_lift(receiver_type.class_name, arg_types)
87
+ return struct_new_lift if struct_new_lift
88
+
89
+ class_new_lift = class_new_lift(receiver_type.class_name, arg_types, context)
90
+ return class_new_lift if class_new_lift
91
+
92
+ declared_or_nominal(receiver_type.class_name, context)
93
+ end
94
+
95
+ # Issue #2884 — the `meta_new` tail: a declared `def self.new` ({DeclaredConstructor}), else `Nominal[X]`.
96
+ def declared_or_nominal(class_name, context)
97
+ declared = context && DeclaredConstructor.try_dispatch(class_name, context)
98
+ declared || Type::Combinator.nominal_of(class_name)
99
+ end
100
+
101
+ # `Struct.new(:a, :b)` synthesises an anonymous Struct *subclass* (a class object), not a
102
+ # Struct *instance* — so the chained idiom `Struct.new(:a, :b).new(1, 2)` must resolve
103
+ # `.new` again on a class-like carrier. The constant-bound form (`S = Struct.new(:a);
104
+ # S.new(1)`) already records `Singleton[S]` via `ScopeIndexer::DeclarationArtifacts.record_meta_new_constant?`;
105
+ # this lift gives the *chained* (anonymous) position the same class-like carrier so the
106
+ # trailing `.new` dispatches instead of firing a spurious `undefined method 'new' for
107
+ # Struct`.
108
+ #
109
+ # The disambiguation mirrors `DeclarationWalk::Rules.struct_new_call?`: a call whose positionals are
110
+ # all `Constant<Symbol>` literals is a member-list class definition → `Singleton[Struct]`.
111
+ # The following `AnonStruct.new(1, 2)` carries non-symbol args, so it falls through this
112
+ # gate to `Nominal[Struct]` (a fresh instance) via the `meta_new` tail. ADR-48 deferred full
113
+ # Struct *value* folding (member-reader precision) on mutability grounds; this is the
114
+ # narrower `.new`-dispatch-only fix and contributes no member layout, so `instance.a` stays
115
+ # at its RBS/Dynamic type.
116
+ def struct_new_lift(class_name, arg_types)
117
+ return nil unless class_name == "Struct"
118
+
119
+ positional = arg_types.grep_v(Type::HashShape)
120
+ return nil if positional.empty?
121
+ return nil unless positional.all? { |t| t.is_a?(Type::Constant) && t.value.is_a?(Symbol) }
122
+
123
+ Type::Combinator.singleton_of("Struct")
124
+ end
125
+
126
+ # `Class.new` and `Class.new(Parent)` create a brand-new anonymous class. Statically that
127
+ # class is representable as the parent's singleton type — its singleton-method surface is
128
+ # the parent's (plus whatever the block defines, which we do not statically track here),
129
+ # so `Singleton[Parent]` lets downstream `klass.some_class_method` resolve. No parent →
130
+ # `singleton(Object)` (or the block's anonymous class, #319 below). Anything else (dynamic parent, more
131
+ # than one positional, …) falls back to `Nominal[Class]` via the surrounding `meta_new` tail.
132
+ # #319 — a `Class.new do ... end` with no parent is NOT `Object`: the block body is a class body, so the
133
+ # class owns whatever that body defines. Answering `Singleton[Object]` handed the call site `Object`'s
134
+ # zero-arity `new` and `Object`'s method surface, so an `initialize(bucket)` written right there produced
135
+ # `wrong number of arguments ... (given 1, expected 0)` on code Ruby runs happily. `ScopeIndexer` registers
136
+ # the body's methods under the call site's anonymous name; return that name so the lookups reach them.
137
+ #
138
+ # Scoped to the parentless form on purpose. `Class.new(Parent) { ... }` keeps `Singleton[Parent]`: the
139
+ # anonymous name would have to carry the full ancestor chain for `Parent`'s own surface to stay reachable,
140
+ # and the parentless case is where the false positive lives.
141
+ def class_new_lift(class_name, arg_types, context = nil)
142
+ return nil unless class_name == "Class"
143
+
144
+ if arg_types.empty?
145
+ anonymous = anonymous_class_new_name(context)
146
+ return Type::Combinator.singleton_of(anonymous || "Object")
147
+ end
148
+ return nil unless arg_types.size == 1
149
+
150
+ parent = arg_types.first
151
+ factory_parent = parent.is_a?(Type::StructClass) || parent.is_a?(Type::DataClass)
152
+ return nil if factory_parent && context&.call_node&.block
153
+ return parent if parent.is_a?(Type::Singleton) || factory_parent
154
+
155
+ nil
156
+ end
157
+
158
+ # The synthetic name `ScopeIndexer` keyed this call site's block body by, or nil when the context carries no
159
+ # call node (internal dispatcher callers) or the call has no block. The source path is passed through
160
+ # verbatim — including when it is nil, which is exactly what `ScopeIndexer` saw for the same file — so the
161
+ # two passes derive the same name whether or not the caller supplied a path.
162
+ def anonymous_class_new_name(context)
163
+ return nil if context.nil?
164
+
165
+ AnonymousMetaClass.name_for(context.call_node, context.scope&.source_path)
166
+ end
167
+
168
+ # ADR-15 Phase 4b.x — `Ractor.make_shareable` on both the outer Hash and each lambda value.
169
+ # A plain `.freeze` leaves the Procs unshareable; reading `CONSTANT_CONSTRUCTORS[class]`
170
+ # from a worker Ractor would raise `Ractor::IsolationError`, which the `rescue
171
+ # StandardError` in `constant_constructor_lift` silently swallows — `meta_new` then falls
172
+ # back to `Nominal[Pathname]` in pool mode while sequential builds the `Constant<Pathname>`
173
+ # lift. The divergence surfaces downstream as a spurious `call.argument-type-mismatch`
174
+ # (sequential's `CheckRules::ArgumentType.diagnostic` short-circuits on Constant<Pathname> because
175
+ # Pathname is not in its CONSTANT_CLASSES table; pool's Nominal[Pathname] doesn't
176
+ # short-circuit). Surfaced on GitLab FOSS via `lib/gitlab/mail_room.rb:17`.
177
+ CONSTANT_CONSTRUCTORS = Ractor.make_shareable({
178
+ "Pathname" => Ractor.make_shareable(lambda { |arg|
179
+ Pathname.new(arg)
180
+ })
181
+ })
182
+ private_constant :CONSTANT_CONSTRUCTORS
183
+
184
+ def constant_constructor_lift(class_name, arg_types)
185
+ builder = CONSTANT_CONSTRUCTORS[class_name]
186
+ return nil if builder.nil?
187
+ return nil unless arg_types.size == 1
188
+
189
+ arg = arg_types.first
190
+ return nil unless arg.is_a?(Type::Constant) && arg.value.is_a?(String)
191
+
192
+ result = builder.call(arg.value)
193
+ Type::Combinator.constant_of(result)
194
+ rescue StandardError
195
+ nil
196
+ end
197
+
198
+ # `Array.new(n, value)` and `Array.new(n)` (no value, default `nil`) lift to a per-position
199
+ # `Tuple[…]` when `n` is a small `Constant<Integer>`. Cap at `ARRAY_NEW_TUPLE_LIMIT` (16)
200
+ # so a `Array.new(1_000_000)` does not balloon the carrier; oversize calls fall through to
201
+ # the element-parameter seed below.
202
+ #
203
+ # #317 — `Array.new(n) { |i| ... }` fills every slot from the BLOCK's return type instead
204
+ # of the two-arg `(n, default_value)` overload's `nil` fill. The block and the trailing
205
+ # `default_value` positional are mutually exclusive per `Array#initialize`'s RBS overload
206
+ # set (`(int size, ?E default_value) -> void` vs `(int size) { (Integer index) -> E } ->
207
+ # void`), so a block present at the call site always wins over any (illegal, but tolerated
208
+ # rather than rejected here) second positional.
209
+ ARRAY_NEW_TUPLE_LIMIT = 16
210
+ private_constant :ARRAY_NEW_TUPLE_LIMIT
211
+
212
+ def array_new_lift(class_name, arg_types, block_type = nil)
213
+ return nil unless class_name == "Array"
214
+ return nil if arg_types.empty? || arg_types.size > 2
215
+
216
+ size = array_new_size(arg_types.first)
217
+ if size && !size.negative? && size <= ARRAY_NEW_TUPLE_LIMIT
218
+ return Type::Combinator.tuple_of(*Array.new(size, block_type || array_new_fill(arg_types[1])))
219
+ end
220
+
221
+ array_new_seed(arg_types, block_type)
222
+ end
223
+
224
+ # The carrier for a constructor whose size is not a small literal — `Array.new(n)`,
225
+ # `Array.new(n, v)`, `Array.new(n) { … }`.
226
+ #
227
+ # This must never be a BARE `Array`. A nominal with no type argument carries no element
228
+ # arm, and the ADR-56 block/loop seams read exactly that as an ELEMENTLESS seed — a fresh
229
+ # accumulator whose every store the seam saw — so they CLOSE the carrier over the block's
230
+ # own stores. `acc = Array.new(n); [1].each { acc.push(1) }` then read `Array[1]` for an
231
+ # array whose other `n` slots hold `nil`, and `acc = Array.new(n, "x")` read `Array[1]` and
232
+ # drew `undefined method 'upcase'` on the correct `acc.first.upcase` (issue #615). Handing
233
+ # the seams a real element arm is what puts the constructor under the #586 rule, which
234
+ # keeps every seed arm through the join.
235
+ #
236
+ # The element itself is the fill value's / block result's type, value-pin widened for the
237
+ # reason `hash_new_lift` widens its default: the slots are rewritten over the array's
238
+ # lifetime, so pinning the parameter to the literal would let a later `a[i] == "x"`
239
+ # constant-fold.
240
+ #
241
+ # **The no-fill form seeds the GRADUAL element, not `nil`.** `Array.new(n)` really does put
242
+ # a `nil` in every slot, but a `Nominal` seed gets DECLARED-carrier semantics downstream:
243
+ # the #586 join keeps a seed arm forever, and `MutationWidening#widen_for_mutator` declines
244
+ # a `Nominal` outright, so no whole-array rewrite — `[]=`, `fill`, `map!`, `replace`,
245
+ # `concat` — can ever retract it. The placeholder would become a permanent nil possibility
246
+ # over the allocate-then-fill idiom that is the whole point of the constructor:
247
+ #
248
+ # dp = Array.new(xs.size); dp[0] = 1
249
+ # (1...n).each { |i| dp[i] = dp[i - 1] + xs[i] } # `undefined method '+' for nil`
250
+ #
251
+ # buf = Array.new(256); 256.times { |i| buf[i] = i.to_s }
252
+ # buf.each { |c| c.upcase } # possible nil receiver
253
+ #
254
+ # Both are correct Ruby, and both went from quiet to an ERROR on the `nil` seed. The true
255
+ # positive the `nil` would buy — `Array.new(n).first.upcase` on an array nothing ever wrote
256
+ # — is not worth the dominant idiom, so the no-fill form stays gradual whatever the size
257
+ # argument reads as. (That also happens to absorb Ruby's array-convertible COPY overload:
258
+ # `Array.new([1, 2])` is `[1, 2]`, not two nils.)
259
+ def array_new_seed(arg_types, block_type)
260
+ fill = block_type || arg_types[1]
261
+ return array_seed_of(Type::Combinator.untyped) if fill.nil?
262
+
263
+ element = array_new_element(fill)
264
+ # `Array.new(n, nil)` is the same allocate-then-fill idiom as `Array.new(n)`, spelled with the
265
+ # placeholder made explicit — concurrent-ruby writes `@Resolutions = ::Array.new(count, nil)` and
266
+ # fills it by index. A nil-only element would be a PERMANENT nil arm (a Nominal carrier's element is
267
+ # never widened by a later `[]=` / `fill` / `replace`), so every read of a filled slot would draw
268
+ # `undefined method '…' for nil` on correct code. Same trade as the no-fill form above: the seam
269
+ # needs an arm, not a precise one.
270
+ element = Type::Combinator.untyped if nil_only_element?(element)
271
+ array_seed_of(element)
272
+ end
273
+
274
+ # Whether the widened fill leaves nothing but `nil` behind — see {#array_new_seed}.
275
+ def nil_only_element?(element)
276
+ members = element.is_a?(Type::Union) ? element.members : [element]
277
+ members.all? do |member|
278
+ (member.is_a?(Type::Constant) && member.value.nil?) ||
279
+ (member.is_a?(Type::Nominal) && member.class_name == "NilClass")
280
+ end
281
+ end
282
+
283
+ def array_seed_of(element)
284
+ Type::Combinator.nominal_of("Array", type_args: [element])
285
+ end
286
+
287
+ # A fill / block result as an element PARAMETER. Value pinning is dropped, and a literal
288
+ # container widens to its nominal: `Array.new(n) { [] }` builds n INDEPENDENT arrays that
289
+ # the program then appends to, and `Array[Tuple[]]` claims every one of them stays empty —
290
+ # the adjacency-list idiom would read `adj[i].first` as `nil`. Both conversions go through
291
+ # {MutationWidening}'s own helpers, the single owner of "literal container → parameterised
292
+ # nominal" that `MemberShapeProjection` already borrows.
293
+ #
294
+ # The walk is **recursive**, through `Union` members and through a container's own elements /
295
+ # values alike. Every one of those positions is a fresh object per constructed slot that the
296
+ # program goes on to mutate, so stopping at the outermost level only moved the wrong-precise
297
+ # answer one level in: `a = Array.new(n) { [[1]] }; a[0][0] << 5` left `Array[Array[[1]]]`
298
+ # and folded `a[0][0].last == 5` always-falsey on correct code. Judging a `Union` wholesale
299
+ # was the same defect one axis over — `Array.new(n) { flag ? [1] : [2] }` kept both arms as
300
+ # fixed-arity tuples.
301
+ #
302
+ # `depth` is defensive only. A `Type` carrier is an immutable value object assembled
303
+ # bottom-up (`Tuple.new(elements)`, `HashShape.new(pairs)`), so it cannot contain itself and
304
+ # this walk is bounded by the source literal's own nesting; the cap costs nothing on any
305
+ # literal a person writes and stops a malformed carrier from recursing forever.
306
+ ARRAY_NEW_FILL_DEPTH_LIMIT = 8
307
+ private_constant :ARRAY_NEW_FILL_DEPTH_LIMIT
308
+
309
+ def array_new_element(type, depth = 0)
310
+ return Type::Combinator.untyped if depth > ARRAY_NEW_FILL_DEPTH_LIMIT
311
+
312
+ case type
313
+ when Type::Tuple then MutationWidening.widen_tuple(recursed_tuple(type, depth))
314
+ when Type::HashShape then MutationWidening.widen_hash_shape(recursed_hash_shape(type, depth))
315
+ when Type::Union then Type::Combinator.union(*type.members.map { |m| array_new_element(m, depth + 1) })
316
+ else Type::Combinator.widen_value_pinned(type)
317
+ end
318
+ end
319
+
320
+ # The same tuple / hash shape with every element / value already widened, so the
321
+ # {MutationWidening} helper that consumes it sees nominals where the literal had pins. The
322
+ # rebuilt carrier is a throwaway argument — never surfaced — and the helpers read only
323
+ # `elements` / `pairs`, so no policy field rides on it.
324
+ def recursed_tuple(type, depth)
325
+ Type::Combinator.tuple_of(*type.elements.map { |e| array_new_element(e, depth + 1) })
326
+ end
327
+
328
+ def recursed_hash_shape(type, depth)
329
+ Type::Combinator.hash_shape_of(
330
+ type.pairs.transform_values { |v| array_new_element(v, depth + 1) }
331
+ )
332
+ end
333
+
334
+ def array_new_size(type)
335
+ return nil unless type.is_a?(Type::Constant) && type.value.is_a?(Integer)
336
+
337
+ type.value
338
+ end
339
+
340
+ def array_new_fill(type)
341
+ return Type::Combinator.constant_of(nil) if type.nil?
342
+
343
+ type
344
+ end
345
+
346
+ # `Hash.new(default)` — lifts the default value's type into the Hash's value parameter so
347
+ # a subsequent `h[k]` read surfaces the default type rather than `Dynamic[top]`. The
348
+ # common counter idiom `h = Hash.new(0); h[k] += 1` then types the read as `Integer`. The
349
+ # key parameter is left `untyped` (the default carrier imposes no key constraint), so
350
+ # reads of any key resolve through the value parameter. A value-pinned `Constant` default
351
+ # (`0`) is widened to its nominal (`Integer`): the hash's values mutate over its lifetime,
352
+ # so pinning the parameter to the literal would be unsound for the aggregate.
353
+ #
354
+ # Only the single-argument default form folds. The zero-arg (`Hash.new`) and the block
355
+ # form (`Hash.new { |h, k| … }`) keep the bare `Nominal[Hash]` answer — the block's value
356
+ # type is not available at this `:new` dispatch site, and conservatively leaving the read
357
+ # as today's behaviour is precision-additive.
358
+ #
359
+ # Issue #1812 — a brace-less keyword hash is `Hash#initialize`'s `capacity:` keyword, not the
360
+ # default (`Hash.new(capacity: 10).default` is `nil`), so it is dropped before counting the
361
+ # positionals. A braced hash (`Hash.new({ capacity: 10 })`) is a positional and stays the default.
362
+ def hash_new_lift(class_name, arg_types, call_node = nil)
363
+ return nil unless class_name == "Hash"
364
+
365
+ arg_types = hash_new_positionals(arg_types, call_node)
366
+ return nil unless arg_types&.size == 1
367
+
368
+ default = arg_types.first
369
+ return nil if default.nil?
370
+
371
+ value = Type::Combinator.widen_value_pinned(default)
372
+ Type::Combinator.nominal_of("Hash", type_args: [Type::Combinator.untyped, value])
373
+ end
374
+
375
+ # `Hash.new`'s positional argument types: `arg_types` without a trailing brace-less keyword hash. A literal
376
+ # send (`Hash.send(:new, capacity: 1)`) dispatches with the outer call node, whose arguments are the
377
+ # selector symbol followed by `arg_types`' own, and is read through that offset. When the node's arguments
378
+ # correspond otherwise, a sole hash argument may be the keyword hash, so the answer is nil (decline) and the
379
+ # bare `Hash` stands; any other argument cannot be a keyword hash and is kept.
380
+ def hash_new_positionals(arg_types, call_node)
381
+ return arg_types[0...-1] if ArgumentNodes.keyword_hash_last?(call_node, arg_types)
382
+
383
+ arguments = call_node.respond_to?(:arguments) ? call_node.arguments&.arguments : nil
384
+ return arg_types if arguments.nil? || arguments.size == arg_types.size
385
+ if arguments.size == arg_types.size + 1 && arguments.first.is_a?(Prism::SymbolNode)
386
+ return arguments.last.is_a?(Prism::KeywordHashNode) ? arg_types[0...-1] : arg_types
387
+ end
388
+
389
+ arg_types.last.is_a?(Type::HashShape) ? nil : arg_types
390
+ end
391
+
392
+ # `Range.new(b, e)` / `Range.new(b, e, excl)` — folds to `Constant[Range]` when both
393
+ # endpoints are `Constant[Integer]` or both are `Constant[String]`, and the optional third
394
+ # argument is a `Constant[true/false]`. Nil endpoints (beginless / endless ranges) are not
395
+ # folded because the useful instance methods (`to_a`, `first`, `last`) are not defined for
396
+ # them; the RBS tier answers `Nominal[Range]` for those forms.
397
+ def range_new_lift(class_name, arg_types)
398
+ return nil unless class_name == "Range"
399
+ return nil if arg_types.size < 2 || arg_types.size > 3
400
+
401
+ b_type = arg_types[0]
402
+ e_type = arg_types[1]
403
+
404
+ return nil unless b_type.is_a?(Type::Constant) && e_type.is_a?(Type::Constant)
405
+
406
+ b_val = b_type.value
407
+ e_val = e_type.value
408
+
409
+ # Only fold homogeneous Integer or String endpoint pairs.
410
+ return nil unless b_val.instance_of?(e_val.class)
411
+ return nil unless b_val.is_a?(Integer) || b_val.is_a?(String)
412
+
413
+ excl = range_new_excl(arg_types[2])
414
+ return nil if excl.nil?
415
+
416
+ Type::Combinator.constant_of(Range.new(b_val, e_val, excl))
417
+ rescue StandardError
418
+ nil
419
+ end
420
+
421
+ # Resolves the optional `exclude_end` argument for `range_new_lift`. Returns `false` (no
422
+ # arg), `true`/`false` (Constant[bool] arg), or `nil` to signal "decline" (wrong type /
423
+ # wrong value class).
424
+ def range_new_excl(excl_type)
425
+ case excl_type
426
+ when nil then false
427
+ when Type::Constant
428
+ excl_type.value if [true, false].include?(excl_type.value)
429
+ end
430
+ end
431
+
432
+ # `Set.new` / `Set.new(tuple_of_constants)` — folds to `Constant[Set]` when zero arguments
433
+ # are given or the single argument is a `Tuple` whose every element is a `Constant[T]`.
434
+ # Mirrors `SetFolding#fold_new` but lives here so the `:new` path in
435
+ # `try_dispatch` can reach it before the RBS tier answers `Nominal[Set]`.
436
+ def set_new_lift(class_name, arg_types, context = nil)
437
+ return nil unless class_name == "Set"
438
+
439
+ SetFolding.fold_new(arg_types, context&.call_node, context&.block_type)
440
+ end
441
+
442
+ # `Regexp.new(pattern)` / `Regexp.new(pattern, opts)` — folds to `Constant[Regexp]` when
443
+ # the pattern is a `Constant[String]`. Mirrors `RegexpFolding#fold_new` but lives here so
444
+ # the `:new` path in `try_dispatch` can reach it.
445
+ def regexp_new_lift(class_name, arg_types)
446
+ return nil unless class_name == "Regexp"
447
+ return nil if arg_types.empty? || arg_types.size > 2
448
+
449
+ pattern_arg = arg_types.first
450
+ return nil unless pattern_arg.is_a?(Type::Constant) && pattern_arg.value.is_a?(String)
451
+
452
+ opts = if arg_types.size == 2
453
+ opt_type = arg_types[1]
454
+ return nil unless opt_type.is_a?(Type::Constant)
455
+
456
+ opt_type.value
457
+ else
458
+ 0
459
+ end
460
+
461
+ Type::Combinator.constant_of(Regexp.new(pattern_arg.value, opts))
462
+ rescue StandardError
463
+ nil
464
+ end
465
+
466
+ # `Date.new(y, m, d)` / `DateTime.new(y, m, d, h, min, s, off)` — folds to `Constant[Date]`
467
+ # / `Constant[DateTime]` when every argument is a `Constant` carrying an Integer (or, for
468
+ # the `DateTime` offset / `start` slots, a Rational or String). `Date.new` carries no
469
+ # timezone, and `DateTime.new`'s offset defaults to UTC, so the literal is
470
+ # machine-independent. `Date.today` / `Date.parse` are not constructors here — they are
471
+ # non-deterministic / string-dependent and stay `Nominal[Date]`.
472
+ DATE_NEW_CLASSES = %w[Date DateTime].freeze
473
+ private_constant :DATE_NEW_CLASSES
474
+
475
+ def date_new_lift(class_name, arg_types)
476
+ return nil unless DATE_NEW_CLASSES.include?(class_name)
477
+ return nil unless arg_types.size.between?(1, 8)
478
+ return nil unless arg_types.all?(Type::Constant)
479
+
480
+ values = arg_types.map(&:value)
481
+ return nil unless values.all? { |v| v.is_a?(Integer) || v.is_a?(Rational) || v.is_a?(String) }
482
+
483
+ klass = class_name == "Date" ? Date : DateTime
484
+ Type::Combinator.constant_of(klass.new(*values))
485
+ rescue StandardError
486
+ nil
487
+ end
488
+
489
+ CONSTANT_METACLASSES = {
490
+ Integer => "Integer", Float => "Float", String => "String",
491
+ Symbol => "Symbol", Range => "Range",
492
+ TrueClass => "TrueClass", FalseClass => "FalseClass",
493
+ NilClass => "NilClass"
494
+ }.freeze
495
+ private_constant :CONSTANT_METACLASSES
496
+
497
+ def constant_metaclass(value)
498
+ CONSTANT_METACLASSES.each do |klass, name|
499
+ return Type::Combinator.singleton_of(name) if value.is_a?(klass)
500
+ end
501
+ nil
502
+ end
503
+ end
504
+ end
505
+ end
506
+ end
@@ -37,6 +37,8 @@ module Rigor
37
37
  # those to `Dynamic[Top]`, which gradually accepts any argument — so without this preference, an
38
38
  # alias-typed overload like `Array#[](::int) -> Elem` would beat the strict
39
39
  # `Array#[](Range) -> Array[Elem]?` overload for a Range argument.
40
+ # 2a. **Pass 1.5 — alias-resolved.** When pass 1 finds nothing, consult each core alias's strict arm
41
+ # (`alias_strict_nominals.rb`), so `Array#*(int)` beats `Array#*(string)` for an Integer argument.
40
42
  # 3. **Pass 2 — gradual fall-back.** If no fully strict overload matches, accept the first
41
43
  # arity-and-gradual-accept match (the v0.1.1 behaviour). Alias / Interface / Intersection params
42
44
  # still reach this pass, so call sites whose only candidate IS an alias-typed overload keep
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../flow_contribution"
4
+ require_relative "../../flow_contribution/merger"
5
+ require_relative "../../plugin/optimistic_return"
6
+ require_relative "../optimistic_origin"
7
+
8
+ module Rigor
9
+ module Inference
10
+ module MethodDispatcher
11
+ # The plugin contribution tier (ADR-2 § "Flow Contribution Bundle", v0.1.1 Track 2 slice 7): the return type
12
+ # the loaded plugins' gated `dynamic_return` rules contribute for one call, merged.
13
+ # `MethodDispatcher.resolve_site` asks it ahead of `RbsDispatch` and records its answer on the scope, so it is
14
+ # a call there and a member of no tier array.
15
+ module PluginContribution
16
+ module_function
17
+
18
+ # The tier, as `resolve_site` asks it: the merged contribution for the site's call node, scope and receiver.
19
+ # It declines a site without a call node or a scope, which is every dispatch the engine makes for itself (a
20
+ # per-element block fold, an alias target).
21
+ #
22
+ # Issue #3322 — when a contributing plugin answered through `Plugin::Base#optimistic_nil_free`, the call node
23
+ # is recorded {OptimisticOrigin::PLUGIN_NIL_FREE} so a condition over the value does not fold to a constant.
24
+ def try_dispatch(context)
25
+ call_node = context.call_node
26
+ scope = context.scope
27
+ type, optimistic = merged_answer(call_node, scope, context.receiver)
28
+ if type && optimistic && call_node.is_a?(Prism::CallNode)
29
+ scope.record_optimistic_origin(call_node, OptimisticOrigin::PLUGIN_NIL_FREE)
30
+ end
31
+ type
32
+ end
33
+
34
+ # ADR-2 § "Flow Contribution Bundle" / v0.1.1 Track 2 slice 7; ADR-52 WD3 — consults each
35
+ # loaded plugin's gated `dynamic_return` rules, wraps the contributed types as
36
+ # `FlowContribution` bundles, merges them through `FlowContribution::Merger`, and returns
37
+ # the merged `return_type` slot (or nil when no plugin contributed a return type).
38
+ #
39
+ # Plugins whose hook raises have their contribution silently dropped for this call so the
40
+ # dispatch chain keeps moving — the run-level diagnostic envelope (per ADR-2 § "Plugin
41
+ # Trust and I/O Policy") is owned by `Analysis::Runner#plugin_emitted_diagnostics`.
42
+ #
43
+ # The tier's second entry: `MethodDispatcher.plugin_member_answers?` asks it with the three values it holds
44
+ # (the veto has no `CallContext`), and {#try_dispatch} with the same three read from the site.
45
+ def merged_return_type(call_node, scope, receiver_type)
46
+ merged_answer(call_node, scope, receiver_type).first
47
+ end
48
+
49
+ NO_ANSWER = [nil, false].freeze
50
+ private_constant :NO_ANSWER
51
+
52
+ # `[merged return type or nil, whether any contribution was an optimistic answer]`.
53
+ def merged_answer(call_node, scope, receiver_type)
54
+ return NO_ANSWER if call_node.nil? || scope.nil?
55
+
56
+ registry = scope.environment&.plugin_registry
57
+ return NO_ANSWER if registry.nil? || registry.empty?
58
+
59
+ contributions, optimistic = collect_plugin_answers(registry, call_node, scope, receiver_type)
60
+ return NO_ANSWER if contributions.empty?
61
+
62
+ [FlowContribution::Merger.merge(contributions).return_type, optimistic]
63
+ end
64
+
65
+ # ADR-37 slice 2 / ADR-52 WD3 — gathers each plugin's return-type contribution from the
66
+ # gated `dynamic_return` DSL, wrapped as a return-only `FlowContribution` for the shared
67
+ # merger. (The legacy ungated `flow_contribution_for` escape valve was deleted once its
68
+ # five users migrated.)
69
+ EMPTY_CONTRIBUTIONS = [[].freeze, false].freeze
70
+ private_constant :EMPTY_CONTRIBUTIONS
71
+
72
+ # Collects every plugin's flow / dynamic-return contribution for one call site. Three
73
+ # prunings keep this off the hot path on plugin-heavy projects (it was the #1 allocation
74
+ # site and a top CPU cost):
75
+ #
76
+ # 1. Only the plugins that *structurally* implement a per-call path are visited —
77
+ # `registry.contribution_index.for_method_dispatch` is the registry-ordered subset
78
+ # declaring a `dynamic_return`. Iterating the subset in registry order, and gating each
79
+ # path by membership, yields the exact same contributions in the same order as visiting
80
+ # every plugin would (a skipped plugin's call returns nil/[] anyway). The
81
+ # receiver-class ancestry match still happens per dispatch inside `dynamic_return_type`.
82
+ # 2. Contributions accumulate lazily — allocate only when one actually appears, and share a
83
+ # frozen empty array otherwise. The caller treats the result as read-only (`.empty?` /
84
+ # `Merger.merge`).
85
+ # 3. ADR-52 WD1 — method-name gates compiled at registry build. The global gate makes the
86
+ # common "no plugin cares about this call" case a single Set probe; the per-plugin gate
87
+ # skips a plugin whose `dynamic_return` rules are all `methods:`-gated on other names. A
88
+ # pruned consultation could only have returned nil, so contribution order and content
89
+ # are unchanged.
90
+ def collect_plugin_contributions(registry, call_node, scope, receiver_type)
91
+ collect_plugin_answers(registry, call_node, scope, receiver_type).first
92
+ end
93
+
94
+ # {.collect_plugin_contributions} paired with whether any plugin answered optimistically (#3322).
95
+ def collect_plugin_answers(registry, call_node, scope, receiver_type)
96
+ index = registry.contribution_index
97
+ relevant = index.for_method_dispatch
98
+ return EMPTY_CONTRIBUTIONS if relevant.empty?
99
+
100
+ # `call_node` is not always a CallNode — the `&:symbol` block path dispatches with the
101
+ # `Prism::BlockArgumentNode` itself (`ExpressionTyper#symbol_block_return_type`). A bare
102
+ # `.name` here raised, and the raise was silently absorbed by `block_return_type_for`'s
103
+ # rescue, nil-ing the block type and flipping `select(&:p)`-style calls onto their
104
+ # no-block Enumerator overloads (caught by the GitLab corpus gate).
105
+ name = call_node.respond_to?(:name) ? call_node.name : nil
106
+ return EMPTY_CONTRIBUTIONS unless index.dispatch_candidate?(name)
107
+
108
+ collect_gated_contributions(index, relevant, name, call_node, scope, receiver_type)
109
+ end
110
+
111
+ # The post-gate walk, in registry order — the same order the ungated walk used.
112
+ # Answers `[contributions, optimistic]`, the second true when any plugin's answer was a
113
+ # `Plugin::OptimisticReturn`.
114
+ def collect_gated_contributions(index, relevant, name, call_node, scope, receiver_type)
115
+ result = nil
116
+ optimistic = false
117
+ relevant.each do |plugin|
118
+ next unless index.dynamic_candidate_for?(plugin, name)
119
+
120
+ answer = plugin.dynamic_return_answer(call_node: call_node, scope: scope, receiver_type: receiver_type)
121
+ dynamic = Plugin::OptimisticReturn.unwrap(answer)
122
+ next unless dynamic
123
+
124
+ optimistic = true if answer.is_a?(Plugin::OptimisticReturn)
125
+ (result ||= []) << FlowContribution.new(return_type: dynamic)
126
+ rescue StandardError
127
+ next
128
+ end
129
+ result ? [result, optimistic] : EMPTY_CONTRIBUTIONS
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end