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,882 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../../reflection"
5
+ require_relative "../../scope"
6
+ require_relative "../../type"
7
+ require_relative "../../inference/object_mixins"
8
+ require_relative "../diagnostic"
9
+ require_relative "receiver_predicates"
10
+
11
+ module Rigor
12
+ module Analysis
13
+ module CheckRules
14
+ # ADR-116 WD1 — the `call.undefined-method` rule: an explicit-receiver call whose receiver statically resolves
15
+ # to a class the RBS environment declares and whose method name that class's method table lacks, with every
16
+ # exemption the rule has accumulated (open receivers, project patches, refinements, hook-defined methods,
17
+ # plugin-typed receivers, union and safe-navigation shapes).
18
+ #
19
+ # The one entry point is {.diagnostic}, called by `CheckRules.call_node_diagnostics`. It reads the shared
20
+ # predicates through {ReceiverPredicates} and calls nothing else in `CheckRules`.
21
+ #
22
+ # The primary rule (`call.undefined-method`) flags an
23
+ # explicit-receiver `Prism::CallNode` whose receiver statically
24
+ # resolves to a class known to the RBS environment and whose
25
+ # method name does not appear on that class's method table.
26
+ # It does NOT fire for:
27
+ #
28
+ # - implicit-self calls (no `node.receiver`) — too noisy
29
+ # without per-method RBS for every helper in the class.
30
+ # - dynamic / unknown receivers (`Dynamic[T]`, `Top`)
31
+ # — by definition we cannot enumerate the method set.
32
+ # - shape carriers: `Tuple` → "Array", `HashShape` → "Hash",
33
+ # `Constant` → the constant's class — `concrete_class_name`
34
+ # resolves these to their runtime class for dispatch.
35
+ # - receivers whose class name is NOT registered in the
36
+ # loader (RBS-blind environments, unknown stdlib).
37
+ # rubocop:disable-next Metrics/ModuleLength
38
+ module UndefinedMethod
39
+ module_function
40
+
41
+ def diagnostic(path, call_node, facts, lexical_sites = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
42
+ return nil if call_node.receiver.nil?
43
+
44
+ scope = facts.scope
45
+ return nil if scope.nil?
46
+
47
+ # The exemptions that hold whatever shape the receiver has. Ahead of the receiver-shape
48
+ # branch below because a plugin is consulted ONCE for the whole receiver, union included, so the
49
+ # gate belongs where that one consult is rather than duplicated into each shape's path.
50
+ #
51
+ # The union side of that placement is reachable. The
52
+ # shape needs a non-nil union of at least two RBS-DECLARED classes — an undeclared arm is an
53
+ # ADR-26 open receiver and `union_arm_blocks_undefined_fire?` bails before the method lookup,
54
+ # which is why the earlier probes read silent and were misread as the gate being unreachable.
55
+ # `spec/integration/composite_receiver_plugin_typed_suppression_spec.rb` builds it. What keeps
56
+ # the gate from firing there today is that the dispatcher no longer records a plugin-typed call
57
+ # it declined (#1101), not that no such union can be built.
58
+ #
59
+ # Note what the gate would do if it were reached: the scalar `Ledger#settle` a plugin declares
60
+ # is exempt here, while `(Ledger | Invoice)#settle` is not, because the union rule's own
61
+ # `method_present_anywhere?` reads RBS and project source and never asks the registry. Closing
62
+ # that asymmetry is #653's union coverage, not this rule's placement.
63
+ # See {#call_site_exempt?}.
64
+ return nil if call_site_exempt?(call_node, scope)
65
+
66
+ # N3 — a safe-navigation call (`recv&.m`) never dispatches on the
67
+ # nil edge of its receiver: at runtime it short-circuits to nil.
68
+ # A receiver that types as exactly `nil` yields nil with no call at
69
+ # all, so it is silent (no dead-code diagnostic — `&.` is the
70
+ # nil-skip operator by design, and frightening working
71
+ # `@x = nil; @x&.m` code would breach FP discipline). A nil-bearing
72
+ # *union* receiver is left to flow through unchanged: a `T | nil`
73
+ # union has no single concrete class, so the rule already bails
74
+ # below — preserving that keeps `&.` from newly firing on the
75
+ # non-nil constituent (which, for a cross-file project def, would
76
+ # be a working-code false positive).
77
+ receiver_type = safe_navigation_receiver(call_node, facts.receiver_type)
78
+
79
+ # #320 — the private-singleton-object idiom (`class << Merger = Object.new`). The body's methods
80
+ # are recorded on the constant's own name, but the receiver reads back as `Object`, so the
81
+ # class-keyed probes below cannot see them. Recover the name from the receiver syntax. Scoped to
82
+ # the recorded name only: `Merger.nope` still fires.
83
+ return nil if Inference::SingletonObjectConstant.recorded?(call_node, receiver_type, call_node.name, scope)
84
+
85
+ # Issue #1699 — judged as its one class; the message still names the union. See
86
+ # {#single_class_literal_union}.
87
+ rendered_receiver = receiver_type
88
+ receiver_type = single_class_literal_union(receiver_type)
89
+ class_name = ReceiverPredicates.concrete_class_name(receiver_type)
90
+ # A union receiver has no single concrete class. The scalar path
91
+ # below cannot reason about it, but the call is still definitely
92
+ # undefined when EVERY arm lacks the method — see
93
+ # `union_undefined_method_diagnostic`.
94
+ if class_name.nil?
95
+ return union_undefined_method_diagnostic(path, call_node, receiver_type, scope, lexical_sites)
96
+ end
97
+
98
+ # An unbounded receiver surface: either a plugin-declared
99
+ # open receiver (ADR-26 — e.g. `ActiveRecord::Relation`), or
100
+ # a type Rigor synthesized (a missing-namespace module / a
101
+ # stub for a referenced-but-undeclared type) to keep a
102
+ # malformed project signature buildable. A synthesized stub's
103
+ # method table is empty only because Rigor invented it, not
104
+ # because the real type is empty (the real `DRb` has
105
+ # `start_service`), so enumerating it to prove a call
106
+ # "undefined" would be a false positive.
107
+ return nil if unenumerable_receiver?(receiver_type, class_name, scope)
108
+
109
+ # Slice 7 phase 12 — suppress when the user has
110
+ # declared the method in source (`def` /
111
+ # `define_method`) OR in a `pre_eval:` monkey-patch
112
+ # file (ADR-17). Both paths are project-side method
113
+ # contributions the dispatcher already resolved; the
114
+ # rule must not surface a false `undefined-method`
115
+ # for them.
116
+ kind = receiver_type.is_a?(Type::Singleton) ? :singleton : :instance
117
+ return nil if ReceiverPredicates.source_declared_method?(scope, class_name, call_node.name, kind,
118
+ call_node: call_node)
119
+
120
+ return nil unless Rigor::Reflection.rbs_class_known?(class_name, scope: scope)
121
+
122
+ # When the loader cannot build a class definition for a
123
+ # name it nominally knows (constant-decl aliases such
124
+ # as `YAML` → `Psych`, or RBS-build failures for
125
+ # malformed signatures), we cannot enumerate methods
126
+ # so we MUST NOT emit a false positive. Skip the rule
127
+ # in that case.
128
+ return nil unless ReceiverPredicates.definition_available?(receiver_type, class_name, scope)
129
+
130
+ method_def = ReceiverPredicates.lookup_method(receiver_type, class_name, call_node.name, scope)
131
+ return nil if method_def
132
+
133
+ return nil if last_resort_surface_answers?(receiver_type, class_name, call_node, scope, kind, lexical_sites)
134
+ return nil if open_project_singleton?(receiver_type, class_name, call_node, scope)
135
+ return nil if subclass_instance_may_answer?(rendered_receiver, class_name, call_node.name, scope,
136
+ call_node.receiver)
137
+
138
+ definition_site = project_definition_site(scope, class_name, call_node.name, kind)
139
+ # Issue #735 — a project `def` on a class the project's own sidecar `sig/` declares is not an ADR-17
140
+ # monkey-patch; see {#project_sidecar_owns_method?}.
141
+ return nil if project_sidecar_owns_method?(scope, class_name, call_node.name, kind)
142
+
143
+ build_undefined_method_diagnostic(path, call_node, rendered_receiver, definition_site, class_name)
144
+ end
145
+
146
+ # Issue #1717 — an explicit `self.m` / `self.m = v` inside a block whose `self` the engine does not know
147
+ # (`Scope#block_self_unknown?`, set where issue #316's opaque mark is). The receiver still types as the
148
+ # ENCLOSING `self`, but the yielding method may `instance_exec` the block on another object
149
+ # (`ActionController::Renderers.add` runs its block on the controller), so a method missing on the
150
+ # enclosing `self` is no evidence. The implicit `m` in the same block is already silent; the explicit
151
+ # spelling must not be stricter. A block
152
+ # whose `self` IS narrowed (`block_as_methods:`, `define_method`, a `Class.new` body) has cleared the
153
+ # mark and keeps being checked, unless the narrowing was read off an unknown enclosing `self`.
154
+ def unknown_block_self_receiver?(call_node, scope)
155
+ call_node.receiver.is_a?(Prism::SelfNode) && scope.block_self_unknown?
156
+ end
157
+
158
+ # Issue #1120 — the method exists at THIS call site but not everywhere: a refinement whose `using` is in
159
+ # effect here, or a `def o.m` on the receiver local in this scope. Asked from {#last_resort_surface_answers?},
160
+ # once every project-wide table has come back "absent", so the file walk behind {LexicalMethodSites} runs
161
+ # only for a call about to fire.
162
+ def lexically_defined_method?(class_name, call_node, scope, kind, lexical_sites)
163
+ return false if lexical_sites.nil?
164
+ return true if lexical_sites.singleton_local_def?(call_node)
165
+
166
+ ReceiverPredicates.refined_method_in_effect?(class_name, call_node, scope, kind, lexical_sites)
167
+ end
168
+
169
+ # The probes that run only once every cheaper answer has come back "absent", kept together
170
+ # because they share that position and nothing else.
171
+ #
172
+ # - Issue #1120 — a method that exists at this call site only: a refinement in effect here, or a
173
+ # singleton `def` on the receiver local ({#lexically_defined_method?}).
174
+ # - Issue #739 — an instance-side MODULE receiver. A value typed as a mixin module is an instance of
175
+ # some class that includes it, and that class contributes an arbitrary surface: inside
176
+ # `module Attachable::InstanceMethods`, `self.project` is defined by the ActiveRecord model that
177
+ # includes the module, and in RBS a parameter typed `Taggable` means "something whose class
178
+ # includes Taggable", not "something whose methods are Taggable's". The surface is not
179
+ # enumerable, so nothing here can prove a method absent. This probe used to RETRY against Object
180
+ # — enough for `self.inspect` / `self.class`, and nothing else — which left 45 firings on
181
+ # redmine's `acts_as_*` mixins the moment a `sig/` declared them.
182
+ #
183
+ # The union twin already declines these arms outright (`union_arm_blocks_undefined_fire?`, "a
184
+ # module mixin whose Object-inherited methods the per-arm lookup would miss"). The two rules
185
+ # answering the same question differently about the same receiver is the fork this closes; the
186
+ # union's reading is the correct one.
187
+ #
188
+ # Singleton-side module receivers are NOT covered and must not be: `M.typo` on a namespace module
189
+ # with `module_function` / `def self.` has a real, enumerable surface and keeps firing.
190
+ # - Issue #723 — the project's own ancestry. Everything above answered "the RBS does not have
191
+ # it"; a project ancestor still might, and the typer has already resolved the site through
192
+ # exactly this walk. It sits late because it walks the class graph and so records an ADR-46 ancestry
193
+ # edge (see {#ancestry_declares_method?} for why that placement is load-bearing, not tidiness).
194
+ # - The other probes are described at their own definitions.
195
+ def last_resort_surface_answers?(receiver_type, class_name, call_node, scope, kind, lexical_sites = nil)
196
+ return true if lexically_defined_method?(class_name, call_node, scope, kind, lexical_sites)
197
+ return true if ReceiverPredicates.project_patched_through_ancestors?(scope, class_name, call_node.name, kind,
198
+ call_node: call_node)
199
+ return true if ReceiverPredicates.module_mixin_receiver?(receiver_type, scope)
200
+ return true if mixin_self_class_receiver?(call_node, scope)
201
+ return true if unknown_mixin_includer?(class_name, scope, kind)
202
+ return true if ReceiverPredicates.object_mixin_may_define?(receiver_type, class_name, call_node.name, scope)
203
+ return true if ReceiverPredicates.ancestry_declares_method?(scope, class_name, call_node.name, kind)
204
+ return true if open_names_through_ancestors?(scope, class_name, kind)
205
+ return true if foreign_singleton_through_ancestors?(scope, class_name, call_node.name, kind)
206
+
207
+ rbs_ancestor_may_answer?(class_name, call_node.name, scope, kind)
208
+ end
209
+
210
+ # Issue #2793 — the class, or a project class or module on its chain, carries
211
+ # `Scope::DiscoveryIndex::ENVELOPE_OPEN_NAMES_MARK`: a macro there defines methods under names its syntax
212
+ # does not spell (`def_delegators :@x, *NAMES`), so no name is provably missing. A chain cut at its budget
213
+ # never reaches here: {#ancestry_declares_method?} reads the same chain first and declines on the cut.
214
+ # A non-literal `define_method` records its own side's twin of the mark instead, read beside it here.
215
+ OPEN_NAMES_MARKS = {
216
+ instance: [Scope::DiscoveryIndex::ENVELOPE_OPEN_NAMES_MARK,
217
+ Scope::DiscoveryIndex::ENVELOPE_OPEN_INSTANCE_NAMES_MARK].freeze,
218
+ singleton: [Scope::DiscoveryIndex::ENVELOPE_OPEN_NAMES_MARK,
219
+ Scope::DiscoveryIndex::ENVELOPE_OPEN_SINGLETON_NAMES_MARK].freeze
220
+ }.freeze
221
+ private_constant :OPEN_NAMES_MARKS
222
+
223
+ def open_names_through_ancestors?(scope, class_name, kind)
224
+ side = kind == :singleton ? :singleton : :instance
225
+ marks = OPEN_NAMES_MARKS.fetch(side)
226
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, side, :methods)
227
+ # A project class with no `def` of its own is held as an external entry (#2094), so an external's
228
+ # candidate names are asked too.
229
+ found = chain.search(scope) do |entry|
230
+ names = entry.external? ? entry.candidates : [entry.name]
231
+ names.any? do |name|
232
+ bucket = scope.parameter_envelopes_of(name)
233
+ marks.any? { |mark| bucket.key?(mark) } ||
234
+ (side == :instance && (computed_definer_called?(scope, name, bucket) ||
235
+ hook_block_macro_defines?(scope, name)))
236
+ end
237
+ end
238
+ !found.nil?
239
+ end
240
+
241
+ # Issue #3018 — a class object's singleton method that `def Foo.x`, `Foo.define_singleton_method(:x)` or a
242
+ # `Foo.singleton_class.class_eval` def defines outside `Foo`'s body records no `:singleton` row, only an
243
+ # envelope entry. The class-object path reads the definer's own entry
244
+ # (`Scope::DiscoveryIndex::ENVELOPE_OBJECT_SINGLETON_KIND`) on the chain's class-object entries, the class and
245
+ # its superclasses, so a subclass inherits it. An extended module's entry is instance-side: `extend M` adds
246
+ # `M`'s instance methods, not a `def M.x`. It does not read the opaque `[:singleton, x]` entry beside it, which
247
+ # a name-rewriting class-body macro (`my_attr :x`) files too without defining `x` on the class object.
248
+ def foreign_singleton_through_ancestors?(scope, class_name, method_name, kind)
249
+ return false unless kind == :singleton
250
+
251
+ key = [Scope::DiscoveryIndex::ENVELOPE_OBJECT_SINGLETON_KIND, method_name.to_sym]
252
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
253
+ found = chain.search(scope, side: :singleton) do |entry|
254
+ names = entry.external? ? entry.candidates : [entry.name]
255
+ names.any? { |name| scope.parameter_envelopes_of(name).key?(key) }
256
+ end
257
+ !found.nil?
258
+ end
259
+
260
+ # Issue #2901 — the body of `name` calls, on `self`, a method its singleton chain resolves to a computed
261
+ # definer (`Scope::DiscoveryIndex::ENVELOPE_COMPUTED_DEFINER_KIND`): a class macro of its own, a
262
+ # superclass's or an extended module's, which runs `define_method` under a name no table holds. The first
263
+ # entry on that chain carrying the macro's definer entry answers; a closer override that defines nothing is
264
+ # not looked for, so it leaves the class open.
265
+ #
266
+ # Issue #2975 — the definer entry carries its side: the class's own singleton entry answers with a singleton
267
+ # method (`def self.my_attr`), an extended module's entry with an instance method, so an instance `def my_attr`
268
+ # of the class itself opens nothing. A macro patched onto `Module` (or `Class`, `Object`, `Kernel`,
269
+ # `BasicObject`) as an instance method is on every class object's singleton chain past its own classes.
270
+ def computed_definer_called?(scope, name, bucket)
271
+ macros = bucket.each_key.filter_map do |key|
272
+ key.last if key.is_a?(Array) && key.first == Scope::DiscoveryIndex::ENVELOPE_BODY_CALL_KIND
273
+ end
274
+ return false if macros.empty?
275
+
276
+ chain = Scope::ResolutionChain.for(scope, name.to_s, :singleton, :methods)
277
+ found = macros.any? do |macro|
278
+ !chain.search(scope) { |entry| macro_entry_defines?(scope, entry, macro) }.nil? ||
279
+ universal_macro_defines?(scope, macro)
280
+ end
281
+ found || hook_extended_macro_defines?(scope, name, macros)
282
+ end
283
+
284
+ # Issue #2975 — a module that a class on `name`'s superclass chain includes or prepends directly, and whose
285
+ # `included` / `prepended` hook extends its includer with a module (`def self.included(base) =
286
+ # base.extend(ClassMethods)`), puts that module's instance methods on `name`'s singleton chain, which records no
287
+ # edge for it. Ruby runs the hook for the direct includer only, so a module included through another module does
288
+ # not count; the chain's levels flatten a class's transitive mixins, so each level class's own mixin list is
289
+ # read, one hop, never followed further. Each mixin and each extended constant resolves to the first candidate
290
+ # the project declares, the innermost cref first, whether or not RBS declares the modules; a concern's
291
+ # `ClassMethods` resolves as `const_get` does instead (#2986).
292
+ def hook_extended_macro_defines?(scope, name, macros)
293
+ definers = macro_definers(macros)
294
+ direct_hook_mixins(scope, name).any? { |mixin, _edges| hook_extension_defines?(scope, mixin, definers) }
295
+ end
296
+
297
+ # Issue #2986 — an ActiveSupport::Concern's `included do … end` block runs in the class body of a class that
298
+ # includes the concern directly, and its `prepended do … end` block in one that prepends it, so a macro call
299
+ # in the block is read as that class's own, against the module the concern extends it with. A call in the
300
+ # concern's own module body is no includer's: Ruby raises there as the module loads.
301
+ #
302
+ # Issue #2992 — Ruby runs the block in every direct includer, a module included, so the read is made for each
303
+ # entry of the receiver's chain (`owner`) against that entry's own mixins, one hop. A concern included into a
304
+ # concern after its `extend ActiveSupport::Concern` runs no block there: `Concern#append_features` records it as
305
+ # a dependency, and the outer concern's final includer includes it, on that includer's own edge
306
+ # ({#with_concern_dependencies}). Only a mixin whose one `include` / `prepend` statement is such a dependency
307
+ # is left out of `owner`'s own read: one written ahead of the `extend`, or again in a reopening that may load
308
+ # first, is an ordinary include there.
309
+ def hook_block_macro_defines?(scope, owner)
310
+ own_mixin_edges(scope, owner, deferred: true).any? do |mixin, edges|
311
+ macros = scope.parameter_envelopes_of(mixin).each_key.filter_map do |key|
312
+ key[2] if key.is_a?(Array) && key.first == Scope::DiscoveryIndex::ENVELOPE_HOOK_BODY_CALL_KIND &&
313
+ edges.include?(Scope::ResolutionChain.effective_hook(scope.discovery, mixin, key[1]))
314
+ end
315
+ !macros.empty? && hook_extension_defines?(scope, mixin, macro_definers(macros))
316
+ end
317
+ end
318
+
319
+ def macro_definers(macros)
320
+ macros.map { |macro| [Scope::DiscoveryIndex::ENVELOPE_COMPUTED_DEFINER_KIND, :instance, macro] }
321
+ end
322
+
323
+ # Each module a class on `name`'s superclass chain includes or prepends directly, resolved, with the hook
324
+ # blocks its edges run: `:included` for an `include`, `:prepended` for a `prepend`; a concern's dependencies
325
+ # follow it on the same edges.
326
+ def direct_hook_mixins(scope, name)
327
+ chain = Scope::ResolutionChain.for(scope, name.to_s, :instance, :methods)
328
+ chain.record(scope) if Analysis::DependencyRecorder.active?
329
+ chain.level_classes.compact.flat_map { |klass| own_mixin_edges(scope, klass) }
330
+ end
331
+
332
+ # The modules `owner` itself includes or prepends, as {#direct_hook_mixins} gives them. With `deferred:`, a
333
+ # provable dependency of a concern `owner` is left out ({#hook_block_macro_defines?}).
334
+ def own_mixin_edges(scope, owner, deferred: false)
335
+ mixins = scope.includes_of(owner)
336
+ prepends = scope.prepends_of(owner)
337
+ deferred_mixins = deferred ? deferred_dependencies(scope, owner, mixins) : NO_DEFERRED
338
+ mixins.uniq.flat_map do |raw|
339
+ mixin = declared_constant(scope, owner, raw)
340
+ next [] unless mixin
341
+
342
+ edges = []
343
+ edges << :prepended if prepends.include?(raw)
344
+ edges << :included if mixins.count(raw) > prepends.count(raw)
345
+ next [] if deferred_mixins.include?(raw)
346
+
347
+ with_concern_dependencies(scope, mixin).map { |member| [member, edges] }
348
+ end
349
+ end
350
+
351
+ NO_DEFERRED = [].freeze
352
+ private_constant :NO_DEFERRED
353
+
354
+ # The spellings among `owner`'s mixins that name a concern dependency of `owner`, when `owner` is declared in
355
+ # one file: a reopening may write the same `include` without the `extend`, and load first.
356
+ def deferred_dependencies(scope, owner, mixins)
357
+ sites = scope.discovery.discovered_class_sources[owner.to_s]
358
+ return NO_DEFERRED if sites.nil? || sites.size > 1
359
+
360
+ resolver = Scope::ResolutionChain.resolver_for(scope, :methods)
361
+ dependencies = Scope::ResolutionChain.concern_dependencies(scope.discovery, resolver, owner)
362
+ return NO_DEFERRED if dependencies.empty?
363
+
364
+ Scope::ResolutionChain.dependency_spellings(scope.discovery, owner).select do |raw|
365
+ mixins.include?(raw) && dependencies.include?(resolver.resolve_declared(owner, raw))
366
+ end
367
+ end
368
+
369
+ # Issue #2992 — `mixin` and, when it is a concern, the concerns it includes or prepends, at any depth through
370
+ # concerns ({Scope::ResolutionChain.concern_dependencies}): `Concern#append_features` (or `prepend_features`)
371
+ # includes (or prepends) each of them into the final includer ahead of `mixin` itself, on the includer's own
372
+ # edge to `mixin`, whatever edge each was given. A reader files each one's sites as it reads its envelopes.
373
+ def with_concern_dependencies(scope, mixin)
374
+ resolver = Scope::ResolutionChain.resolver_for(scope, :methods)
375
+ [mixin, *Scope::ResolutionChain.concern_dependencies(scope.discovery, resolver, mixin)]
376
+ end
377
+
378
+ def hook_extension_defines?(scope, mixin, definers)
379
+ hook_extends(scope, mixin).any? do |written, lookup|
380
+ target = if lookup == :const_get then const_get_constant(scope, mixin, written)
381
+ else declared_constant(scope, mixin, written)
382
+ end
383
+ record_hook_target_edges(scope, mixin, written, lookup) if Analysis::DependencyRecorder.active?
384
+ target && definers.any? { |definer| scope.parameter_envelopes_of(target).key?(definer) }
385
+ end
386
+ end
387
+
388
+ # Issue #2992 — the read depends on the extended module's files whether or not it resolved: an empty
389
+ # `module ClassMethods; end` is no project class yet, and the edit that gives it the definer makes it one. So
390
+ # every name the lookup tried files its declaring sites, and the constant's last segment its negative class
391
+ # edge, which a NEW file declaring it fires.
392
+ def record_hook_target_edges(scope, mixin, written, lookup)
393
+ candidates = if lookup == :const_get
394
+ chain = Scope::ResolutionChain.for(scope, mixin, :instance, :methods)
395
+ chain.entries.flat_map { |entry| entry.external? ? entry.candidates : [entry.name] }
396
+ .map { |name| "#{name}::#{written}" } << written
397
+ else
398
+ own = written.start_with?("::") ? [] : ["#{mixin}::#{written}"]
399
+ own + scope.ancestor_name_candidates(mixin, written)
400
+ end
401
+ candidates.each { |candidate| Scope::ResolutionChain.record_class(scope, candidate.delete_prefix("::")) }
402
+ Analysis::DependencyRecorder.read_last_segment(:class, written)
403
+ end
404
+
405
+ # The constant `raw`, written in `owner`'s body, as the first candidate the project declares: `Owner::Raw`
406
+ # first, since `owner` is the innermost cref there, then the enclosing namespaces. Nil when none is declared.
407
+ def declared_constant(scope, owner, raw)
408
+ candidates = scope.ancestor_name_candidates(owner, raw)
409
+ candidates = ["#{owner}::#{raw}", *candidates] unless raw.start_with?("::")
410
+ candidates.find { |candidate| scope.known_user_class?(candidate) }
411
+ end
412
+
413
+ # Issue #2986 — `owner.const_get(raw)`, as `Concern#append_features` looks `ClassMethods` up: `owner` and its
414
+ # ancestors, then `Object`, never the namespaces enclosing `owner`. Issue #2997 — a `class_methods` block's
415
+ # `Name::ClassMethods` counts only for a concern `Name` ({Scope::ResolutionChain.const_get_candidate?}).
416
+ def const_get_constant(scope, owner, raw)
417
+ chain = Scope::ResolutionChain.for(scope, owner, :instance, :methods)
418
+ found = chain.search(scope) do |entry|
419
+ names = entry.external? ? entry.candidates : [entry.name]
420
+ names.map { |name| [name, "#{name}::#{raw}"] }.find do |name, candidate|
421
+ scope.known_user_class?(candidate) &&
422
+ Scope::ResolutionChain.const_get_candidate?(scope.discovery, name, candidate)
423
+ end&.last
424
+ end
425
+ found || (raw if scope.known_user_class?(raw))
426
+ end
427
+
428
+ # The `[written, lookup]` pairs of a module's hook-extend entries; `lookup` is `:const_get` for a concern's.
429
+ # Issue #3005 — a gated concern-block entry counts only for a concern `owner`.
430
+ def hook_extends(scope, owner)
431
+ scope.parameter_envelopes_of(owner).each_key.filter_map do |key|
432
+ next unless key.is_a?(Array) && key.first == Scope::DiscoveryIndex::ENVELOPE_HOOK_EXTEND_KIND
433
+ next unless Scope::ResolutionChain.effective_hook(scope.discovery, owner, key[2])
434
+
435
+ key[1, 2]
436
+ end
437
+ end
438
+
439
+ # A macro's owner holds the macro's `def`, so it is never an external entry.
440
+ def macro_entry_defines?(scope, entry, macro)
441
+ !entry.external? &&
442
+ scope.parameter_envelopes_of(entry.name).key?([Scope::DiscoveryIndex::ENVELOPE_COMPUTED_DEFINER_KIND,
443
+ entry.side, macro])
444
+ end
445
+
446
+ # The classes past a class object's own singleton chain, whose instance methods it answers. A module's chain
447
+ # does not reach `Class`, but a module body calling a `Class` macro raises as it loads, so it is not told apart.
448
+ UNIVERSAL_MACRO_OWNERS = %w[Class Module Object Kernel BasicObject].freeze
449
+ private_constant :UNIVERSAL_MACRO_OWNERS
450
+
451
+ def universal_macro_defines?(scope, macro)
452
+ definer = [Scope::DiscoveryIndex::ENVELOPE_COMPUTED_DEFINER_KIND, :instance, macro]
453
+ UNIVERSAL_MACRO_OWNERS.any? { |owner| scope.parameter_envelopes_of(owner).key?(definer) }
454
+ end
455
+
456
+ # Issue #1722 — a `method_missing`, or a project mixin, on one of the class's RBS ancestors
457
+ # ({RbsReceiverSurface}). Last for {#ancestry_declares_method?}'s reason: it walks the class graph.
458
+ # Issue #2792 — a `method_missing` RBS declares on the class or an ancestor, other than the universal one.
459
+ def rbs_ancestor_may_answer?(class_name, method_name, scope, kind)
460
+ side = kind == :singleton ? :singleton : :instance
461
+ return true if RbsReceiverSurface.rbs_missing_hook?(scope, class_name, side)
462
+
463
+ if kind == :singleton
464
+ RbsReceiverSurface.singleton_hook?(scope, class_name) ||
465
+ RbsReceiverSurface.singleton_mixin_may_answer?(scope, class_name, method_name)
466
+ else
467
+ RbsReceiverSurface.ancestor_may_answer?(scope, class_name, method_name)
468
+ end
469
+ end
470
+
471
+ # Issue #1722 — a receiver typed by its class's name, rather than by a literal, may hold an instance of a
472
+ # subclass (a `#: () -> String` signature admits a `MyStr < String`), so a method a project subclass
473
+ # defines, or a subclass's `method_missing`, may be the one that runs. A value the engine built from a
474
+ # literal is exactly its class and keeps firing: a literal type (`"a"`, `1`, a union of them), a shape
475
+ # carrier (`{a: 1}`, `[1, 2]`, `{}`, `[]`, and what folds to one), or a receiver written as a literal
476
+ # (`"a#{x}b"`, which types as plain `String`). A local holding an interpolated String types as plain
477
+ # `String` with no origin, so it still declines. Instance side only.
478
+ def subclass_instance_may_answer?(receiver_type, class_name, method_name, scope, receiver_node = nil)
479
+ return false if receiver_type.is_a?(Type::Singleton)
480
+ return false if exact_literal_receiver?(receiver_type) || LITERAL_RECEIVER_NODES.include?(receiver_node.class)
481
+
482
+ RbsReceiverSurface.subclass_may_define?(scope, class_name, method_name)
483
+ end
484
+
485
+ EXACT_TYPES = [Type::Constant, Type::Tuple, Type::HashShape].freeze
486
+ LITERAL_RECEIVER_NODES = [
487
+ Prism::StringNode, Prism::InterpolatedStringNode, Prism::XStringNode, Prism::InterpolatedXStringNode,
488
+ Prism::ArrayNode, Prism::HashNode, Prism::RegularExpressionNode, Prism::InterpolatedRegularExpressionNode
489
+ ].to_set.freeze
490
+ private_constant :EXACT_TYPES, :LITERAL_RECEIVER_NODES
491
+
492
+ def exact_literal_receiver?(receiver_type)
493
+ members = receiver_type.is_a?(Type::Union) ? receiver_type.members : [receiver_type]
494
+ members.all? { |member| EXACT_TYPES.any? { |kind| member.is_a?(kind) } }
495
+ end
496
+
497
+ # Issue #739 — the singleton twin of the module-mixin receiver, and the only shape that reaches the
498
+ # singleton side legitimately. `self.class` inside a mixin's instance method is the INCLUDER's class
499
+ # at runtime; the engine types it `Singleton[<the module>]`, one hop along from the same wrong
500
+ # `self`. redmine's `acts_as_*` mixins reach their configuration that way
501
+ # (`self.class.attachable_options`), 17 sites of it.
502
+ #
503
+ # Keyed on the call-site SYNTAX rather than on the receiver type, because `Singleton[M]` is also
504
+ # what a namespace module's own `M.helper` produces, and that surface is real and enumerable — a
505
+ # type-only test would silence every `M.typo` in the project. Here the question is narrower: was
506
+ # this singleton produced by `.class` on a value whose type is a mixin module?
507
+ def mixin_self_class_receiver?(call_node, scope)
508
+ receiver = call_node.receiver
509
+ return false unless receiver.is_a?(Prism::CallNode) && receiver.name == :class
510
+ return false unless receiver.arguments.nil?
511
+
512
+ inner = receiver.receiver
513
+ return false if inner.nil?
514
+
515
+ ReceiverPredicates.module_mixin_receiver?(scope.type_of(inner), scope)
516
+ end
517
+
518
+ # ADR-17 — when the project itself defines this method on the
519
+ # receiver class somewhere in the analyzed file set (a reopened
520
+ # core/stdlib/gem class the dispatcher does not apply cross-
521
+ # file), return that `"path:line"` site so the diagnostic points
522
+ # at `pre_eval:` instead of reading as a bare unresolved call.
523
+ # Instance-side only (the cross-file def-source index tracks
524
+ # `def` instance methods); the diagnostic still fires — Rigor
525
+ # does not auto-apply project monkey-patches (the full-project
526
+ # pre-pass is deferred per ADR-17 slice 5) — but it is now
527
+ # actionable rather than mistakable for a typo.
528
+ def project_definition_site(scope, class_name, method_name, kind)
529
+ return nil unless kind == :instance
530
+
531
+ scope.user_def_site_for(class_name, method_name)
532
+ end
533
+
534
+ # Issue #735 — the site is evidence, and whose RBS the class has decides what it is evidence OF. On
535
+ # a BUNDLED declaration (core / stdlib / a gem) the class is not the project's, so a project `def`
536
+ # on it is the ADR-17 monkey-patch this rule surfaces with the `pre_eval:` nudge. On the project's
537
+ # OWN sidecar `sig/` the declaration describes the source being analysed, the "patch" is just
538
+ # another of the project's files, and the nudge is not something a user can act on: it asks them to
539
+ # `pre_eval:` their own application. `rigor sig-gen --write` on redmine turned 29
540
+ # `call.undefined-method` into 171 through exactly this reading, 49 of them here.
541
+ def project_sidecar_owns_method?(scope, class_name, method_name, kind)
542
+ Rigor::Reflection.project_declared_class?(class_name, scope: scope) &&
543
+ ReceiverPredicates.project_defines_method?(scope, class_name, method_name, kind)
544
+ end
545
+
546
+ # Issue #742 — the receivers whose method surface this rule cannot enumerate, in one question.
547
+ #
548
+ # {#unbounded_receiver_surface?} covers the ADR-26 open receivers and Rigor's own synthesized stubs.
549
+ # The generic metaclasses join it here: a value typed `Class` or `Module` is SOME class or module
550
+ # object, and its singleton methods cannot be read off the metaclass — `def self.included(base)`
551
+ # receives the includer, so `base.class_attribute :main_menu` and `base.main_menu = true` are calls
552
+ # on whatever included the module. The union twin has declined these arms since it was written
553
+ # (`METACLASS_ARMS`, "a `plugin_class : Class` really holds a `Plugin` subclass with `.manifest`");
554
+ # the scalar rule enumerated `Module`'s own RBS instead and reported them.
555
+ #
556
+ # Kept beside `unbounded_receiver_surface?` rather than folded into it: that predicate has other
557
+ # callers, and #742 had no evidence that any of them spoke on these receivers.
558
+ #
559
+ # Issue #879 supplied it for exactly one — `call.wrong-arity`, which decided `v.hexdigest(1, 2, 3)`
560
+ # against `Digest::Instance`'s declaration on the same run where `v.no_such_method_zzz` was declined
561
+ # as unenumerable. One receiver, one run, two answers about whether that surface can be read. The
562
+ # reporter could make no other caller speak: `raise` on a module- or `Class`-typed operand is silent,
563
+ # and `Class`-typed `.new` with a wrong arity is silent too, so the other callers stay on
564
+ # `unbounded_receiver_surface?` until something demonstrates otherwise.
565
+ #
566
+ # Arity takes {#unenumerable_receiver_shape?}, not this whole predicate: ADR-26's open receivers are
567
+ # handled for the signature-reading rules by {#unauthoritative_inherited_signature?}, which is
568
+ # deliberately narrower — a method the open class DECLARES itself keeps its arity check.
569
+ def unenumerable_receiver?(receiver_type, class_name, scope)
570
+ ReceiverPredicates.unenumerable_receiver_shape?(receiver_type, class_name, scope) ||
571
+ ReceiverPredicates.unbounded_receiver_surface?(class_name, scope)
572
+ end
573
+
574
+ # Issue #746 — a class that includes a module the environment does not know. `SudoMode::Form`
575
+ # includes `ActiveModel::Validations`, which no RBS in redmine's environment declares, and `valid?`
576
+ # comes from it: the ancestors are not fully known, so the method table is not enumerable and a
577
+ # verdict read off the part we can see is unsound. Same reasoning `unbounded_receiver_surface?`
578
+ # already applies to an ADR-26 open receiver and to Rigor's own synthesized stubs.
579
+ #
580
+ # Asked LATE, with {#ancestry_declares_method?} and for the same reason: `Scope#includes_of` records
581
+ # an ADR-46 file-level ancestry edge, so running it on the hot path made `Widget.new` — a call RBS
582
+ # answers, that never reaches a verdict — depend on `Widget`'s declaring file.
583
+ # `dependency_recorder_spec` caught it here exactly as it caught #723's walk.
584
+ #
585
+ # A constant the environment does not know is read off the receiver's OWN includes only. Issue #2991 — a
586
+ # mixin no name spells (`include(*MODS)`, `include helper`) opens the receiver when it sits anywhere on its
587
+ # instance-side chain, its own level or an ancestor's: the chain marks it (`ResolutionChain#unnamed_mixin?`),
588
+ # and any name may come from the unlisted module (a hook's `base.include M` is read so too). `self.include M`
589
+ # and a receiverless `send(:include, M)` are recorded as `include M` is (#2995), so the ordinary lookup reads
590
+ # `M`. A mixin whose ORDER alone is unknown (`include M; prepend M`) has its module recorded, so it opens
591
+ # nothing. A class-object receiver reads its singleton chain instead ({#singleton_mixin_may_answer?}).
592
+ def unknown_mixin_includer?(class_name, scope, kind)
593
+ includes = scope.includes_of(class_name)
594
+ return true if includes.any? { |raw| !known_mixin?(raw, class_name, scope) }
595
+ return false if kind == :singleton
596
+
597
+ Scope::ResolutionChain.for(scope, class_name.to_s, :instance, :methods).unnamed_mixin?
598
+ end
599
+
600
+ def known_mixin?(raw_name, class_name, scope)
601
+ scope.ancestor_name_candidates(class_name, raw_name).any? do |candidate|
602
+ next true if scope.discovered_classes.key?(candidate)
603
+
604
+ Rigor::Reflection.rbs_class_known?(candidate, scope: scope) &&
605
+ !ReceiverPredicates.synthesized_stub_receiver?(candidate, scope)
606
+ end
607
+ end
608
+
609
+ # Issue #2438 — a class-method call on a class the project's own signatures name but whose source leaves
610
+ # the singleton surface open. Before `rigor sig-gen --write` such a class was source-only and an
611
+ # unresolved call on it was silent; the sidecar must not turn the same call into a finding. Two shapes:
612
+ #
613
+ # - `C.instance` where `C`, or a project class up its superclass chain, `include`s the stdlib `Singleton`:
614
+ # the answer is `SingletonMixinDispatch.answers?`, the predicate the typer itself uses, so the rule never
615
+ # reports a call the engine typed. The class's other members are irrelevant: the mixin's hook defines
616
+ # `instance`.
617
+ # - every project declaration of `C` is member-less (`RbsLoader#memberless_project_class?`) AND the source
618
+ # leaves `C` open ({#open_source_class?}). A member-less declaration does not describe the class's
619
+ # surface, but a class whose whole definition Rigor can see in `paths:` is still closed, so a typo on it
620
+ # keeps firing. Instance-side calls keep being checked; declaring any member re-closes the class.
621
+ def open_project_singleton?(receiver_type, class_name, call_node, scope)
622
+ return false unless receiver_type.is_a?(Type::Singleton)
623
+ return true if Inference::MethodDispatcher::SingletonMixinDispatch.answers?(
624
+ scope, receiver_type, call_node.name, call_node.arguments.nil?
625
+ )
626
+
627
+ loader = scope.environment&.rbs_loader
628
+ return false if loader.nil? || !loader.respond_to?(:memberless_project_class?)
629
+
630
+ loader.memberless_project_class?(class_name) && open_source_class?(class_name, scope, loader)
631
+ end
632
+
633
+ OPENING_HOOKS = %i[inherited included extended prepended method_missing respond_to_missing?].freeze
634
+ private_constant :OPENING_HOOKS
635
+
636
+ CLOSED_ROOTS = Set["Object", "BasicObject"].freeze
637
+ private_constant :CLOSED_ROOTS
638
+
639
+ # Issue #2438 — whether a class's singleton surface may hold methods its source in `paths:` does not show,
640
+ # read off `Scope::ResolutionChain` (`:constants` flavor, so a project module that holds only constants is
641
+ # still a project entry). Open when the class, or a project class up its superclass chain:
642
+ #
643
+ # 1. `extend`s a module (any singleton-side entry beyond the classes' own singletons), or records an
644
+ # `inherited` / `included` / `extended` / `prepended` hook or `method_missing` / `respond_to_missing?`.
645
+ # A plain `include` or `prepend` adds instance methods only, so it leaves the singleton side closed;
646
+ # 2. has no `class` / `module` body of its own inside `paths:` (`class Ext::Foo` names `Ext` without one);
647
+ # 3. or the chain reaches a class the project does not define, other than `Object` / `BasicObject`: an
648
+ # external superclass (`ApplicationRecord < ActiveRecord::Base`, `MyError < StandardError`), a
649
+ # superclass written as an expression, or — where the source writes no superclass for the last class —
650
+ # the superclass its member-less declaration writes, unless that is a project class.
651
+ #
652
+ # A definition or reopening in a file outside `paths:` cannot be seen, so a class whose body is inside
653
+ # `paths:` and whose methods live only outside it counts as closed.
654
+ def open_source_class?(class_name, scope, loader)
655
+ name = class_name.to_s.delete_prefix("::")
656
+ instance = Scope::ResolutionChain.for(scope, name, :instance, :constants)
657
+ singleton = Scope::ResolutionChain.for(scope, name, :singleton, :constants)
658
+ [instance, singleton].each { |chain| chain.record(scope) }
659
+ classes = instance.level_classes.compact
660
+ return true if classes.empty? || instance.truncated?
661
+
662
+ sources = scope.discovered_class_sources
663
+ return true if classes.any? { |klass| (sources[klass] || []).empty? || opening_hook?(klass, scope) }
664
+ return true if opening_entry?(instance, classes, mixins: false) ||
665
+ opening_entry?(singleton, classes, mixins: true)
666
+
667
+ open_tail?(instance, classes.last, scope, loader)
668
+ end
669
+
670
+ # An external superclass other than `Object` / `BasicObject` opens either side; a module entry opens the
671
+ # chain only where `mixins` is set (the singleton side, where it is an `extend`).
672
+ def opening_entry?(chain, classes, mixins:)
673
+ chain.entries.any? do |entry|
674
+ if entry.external? && entry.superclass_edge
675
+ !CLOSED_ROOTS.include?(entry.raw.to_s.delete_prefix("::"))
676
+ else
677
+ mixins && !classes.include?(entry.name)
678
+ end
679
+ end
680
+ end
681
+
682
+ def opening_hook?(klass, scope)
683
+ OPENING_HOOKS.any? do |hook|
684
+ scope.discovered_method?(klass, hook, :singleton) || scope.discovered_method?(klass, hook, :instance)
685
+ end
686
+ end
687
+
688
+ # The last class's own superclass, which the chain does not name: an expression the tables cannot resolve,
689
+ # or one only the member-less declaration writes (the chain ends at the implicit `Object` exactly when
690
+ # the source writes none).
691
+ def open_tail?(chain, last, scope, loader)
692
+ return true if chain.opaque_tail?(scope)
693
+
694
+ raw = loader.memberless_project_superclass(last)
695
+ return false if raw.nil? || CLOSED_ROOTS.include?(raw.delete_prefix("::"))
696
+
697
+ !scope.discovered_classes.key?(raw.delete_prefix("::"))
698
+ end
699
+
700
+ # The `call.undefined-method` exemptions that depend only on the CALL SITE, never on the receiver's
701
+ # type or class — so they are decided before the rule looks at the receiver at all:
702
+ #
703
+ # - ADR-67 WD6b — an inferred-parameter receiver's type is an open-call-site lower bound, so firing
704
+ # undefined-method against it is a false positive by construction.
705
+ # - Issue #653 — a plugin answered this call site ({#plugin_typed_call?}).
706
+ # - Issue #1717 — an explicit `self` receiver in a block whose `self` is unmodelled
707
+ # ({#unknown_block_self_receiver?}).
708
+ def call_site_exempt?(call_node, scope)
709
+ ReceiverPredicates.inferred_param_receiver?(call_node, scope) ||
710
+ ReceiverPredicates.plugin_typed_call?(call_node, scope) ||
711
+ unknown_block_self_receiver?(call_node, scope)
712
+ end
713
+
714
+ # The receiver type the `call.undefined-method` existence check
715
+ # should reason about. For a safe-navigation call whose receiver
716
+ # types as exactly `nil`, this is `Type::Bot` — the call is
717
+ # statically skipped at runtime, and `concrete_class_name(Bot)` is
718
+ # nil so the rule bails (silent). Every other receiver (including a
719
+ # `T | nil` union, which already has no single concrete class) flows
720
+ # through unchanged.
721
+ def safe_navigation_receiver(call_node, receiver_type)
722
+ return receiver_type unless call_node.safe_navigation?
723
+ return receiver_type unless ReceiverPredicates.nil_member?(receiver_type)
724
+
725
+ Type::Combinator.bot
726
+ end
727
+
728
+ # Teeth on a *union* receiver. The scalar `undefined_method_diagnostic`
729
+ # bails when the receiver has no single concrete class; here we fire
730
+ # when EVERY non-nil arm is a fully-known, bounded, instance class on
731
+ # which the method is absent — the call is then undefined regardless of
732
+ # which arm the value takes at runtime (`A | B` responds to `m` only if
733
+ # both `A` and `B` do). FP-safe by construction: `method_present_anywhere?`
734
+ # returns "present" for any Dynamic / unknown / unbuildable /
735
+ # source-declared arm, so the `any?` short-circuits and we never fire on
736
+ # uncertainty; `union_arm_blocks_undefined_fire?` additionally bails on
737
+ # any open (ADR-26) / synthesized / singleton / module-mixin arm. This is
738
+ # no more aggressive than the scalar rule — it just applies the same
739
+ # certainty test to each arm.
740
+ #
741
+ # Nil-bearing unions are deferred: their nil arm interacts with the
742
+ # `possible-nil-receiver` rule, safe-navigation, and ADR-58
743
+ # declaration-sourced nil. Slice 1 handles pure non-nil unions
744
+ # (e.g. `String | Symbol`).
745
+ def union_undefined_method_diagnostic(path, call_node, receiver_type, scope, lexical_sites = nil)
746
+ return nil unless receiver_type.is_a?(Type::Union)
747
+ return nil if call_node.safe_navigation?
748
+
749
+ members = receiver_type.members
750
+ # Nil-bearing unions (`T | nil`) stay silent — the deliberate N3
751
+ # decision (`safe_navigation_undefined_method_spec.rb`). A corpus FP
752
+ # study of a bundled-arm-narrowed candidate (12 projects incl.
753
+ # ActiveSupport-heavy) found ~zero real firings yet a demonstrated
754
+ # loss-of-specificity false positive, so the silence is kept. See
755
+ # ADR-62 and `docs/notes/20260613-mutation-teeth-harness.md`.
756
+ return nil if members.any? { |member| ReceiverPredicates.nil_member?(member) }
757
+ return nil if members.any? { |member| union_arm_blocks_undefined_fire?(member, scope) }
758
+ # Only a genuinely multi-class union ("the value is an A or a B") gains
759
+ # from this rule. A union whose arms all resolve to ONE class
760
+ # (`Hash[K1, V1] | Hash[K2, V2]`) is a shape-join artifact — checking
761
+ # method existence there is the scalar rule's job, and when the join is
762
+ # a misinference it is a false positive (a corpus probe caught mail's
763
+ # `compose_codepoints` typed `Hash | Hash` for an `Array`, flagging
764
+ # `.pack`). Require at least two distinct arm classes.
765
+ return nil if members.map { |member| ReceiverPredicates.concrete_class_name(member) }.uniq.size < 2
766
+ return nil if union_may_answer?(members, call_node, scope, lexical_sites)
767
+
768
+ build_undefined_method_diagnostic(path, call_node, receiver_type)
769
+ end
770
+
771
+ # Whether some arm of a non-nil union of classes may answer the call, which keeps the union rule silent.
772
+ def union_may_answer?(members, call_node, scope, lexical_sites)
773
+ return true if members.any? do |member|
774
+ ReceiverPredicates.method_present_anywhere?(member, call_node.name, scope)
775
+ end
776
+ return true if members.any? { |member| arm_surface_may_answer?(member, call_node.name, scope) }
777
+ # Issue #1722 — `def o.m`, `o.extend(M)` and their kin give the one object the method, whatever its class.
778
+ return true if lexical_sites&.singleton_local_def?(call_node)
779
+
780
+ refined_on_some_arm?(members, call_node, scope, lexical_sites)
781
+ end
782
+
783
+ # Issue #1722 — the scalar rule's RBS-ancestor and subclass declines, asked of one arm. Kept out of
784
+ # {#method_present_anywhere?}, which `call.possible-nil-receiver` also asks: there a "present" answer is
785
+ # what lets the rule fire, and a `method_missing` on `Object` answers on the `nil` arm too.
786
+ def arm_surface_may_answer?(member, method_name, scope)
787
+ class_name = ReceiverPredicates.concrete_class_name(member)
788
+ rbs_ancestor_may_answer?(class_name, method_name, scope, :instance) ||
789
+ subclass_instance_may_answer?(member, class_name, method_name, scope)
790
+ end
791
+
792
+ # ADR-121 WD7 — a refinement in effect (or one Rigor cannot read) may define the name on one arm's class.
793
+ def refined_on_some_arm?(members, call_node, scope, lexical_sites)
794
+ members.any? do |member|
795
+ kind = member.is_a?(Type::Singleton) ? :singleton : :instance
796
+ ReceiverPredicates.refined_method_in_effect?(ReceiverPredicates.concrete_class_name(member), call_node,
797
+ scope, kind, lexical_sites)
798
+ end
799
+ end
800
+
801
+ # Issue #1699 — a union of one class's literals, alone or beside that class's plain nominal
802
+ # (`0 | Integer`, which `gets.to_i` types; `"a" | "b"`; `1 | 2`), holds nothing but instances of
803
+ # that class, so it answers a method-existence question exactly as the class does. The union rule
804
+ # above declines it through its distinct-class guard, which exists for SHAPE joins
805
+ # (`Hash[K1, V1] | Hash[K2, V2]`, a corpus misinference on mail), and the scalar rule declined it
806
+ # for having no single concrete class, so neither judged it. Returns that class's nominal, or `type`
807
+ # itself. Literal-and-plain-nominal arms only: a generic, shape or refinement arm keeps the union
808
+ # declined, and so does a union spanning two classes.
809
+ def single_class_literal_union(type)
810
+ return type unless type.is_a?(Type::Union)
811
+
812
+ members = type.members
813
+ return type unless members.any?(Type::Constant)
814
+ return type unless members.all? do |member|
815
+ member.is_a?(Type::Constant) || (member.is_a?(Type::Nominal) && member.type_args.empty?)
816
+ end
817
+
818
+ class_names = members.map { |member| ReceiverPredicates.concrete_class_name(member) }.uniq
819
+ return type unless class_names.size == 1
820
+
821
+ class_name = class_names.first
822
+ return type if class_name.nil? || class_name == "NilClass"
823
+
824
+ Type::Combinator.nominal_of(class_name)
825
+ end
826
+
827
+ def union_arm_blocks_undefined_fire?(member, scope)
828
+ class_name = ReceiverPredicates.concrete_class_name(member)
829
+ return true if class_name.nil?
830
+ return true if member.is_a?(Type::Singleton)
831
+ return true if ReceiverPredicates.metaclass_arm?(class_name)
832
+ return true if ReceiverPredicates.unbounded_receiver_surface?(class_name, scope)
833
+
834
+ ReceiverPredicates.module_mixin_receiver?(member, scope)
835
+ end
836
+
837
+ def build_undefined_method_diagnostic(path, call_node, receiver_type, definition_site = nil, class_name = nil)
838
+ rendered_receiver = receiver_type.describe
839
+ message = "undefined method `#{call_node.name}' for #{rendered_receiver}"
840
+ # ADR-17 — when the project itself defines this method on the
841
+ # receiver class somewhere in the file set, name the site and
842
+ # point at `pre_eval:`. Rigor does not apply project monkey-
843
+ # patches cross-file automatically, so the diagnostic still
844
+ # fires, but the enriched message makes it actionable (and
845
+ # `rigor triage` keys on the structured `project_definition_site`
846
+ # field to recommend `pre_eval:` with high confidence).
847
+ if definition_site
848
+ def_owner = class_name || rendered_receiver
849
+ message += "; the project defines `#{def_owner}##{call_node.name}' at " \
850
+ "#{definition_site} — Rigor does not apply project monkey-patches " \
851
+ "cross-file; list that file in `.rigor.yml`'s `pre_eval:` (ADR-17)"
852
+ end
853
+ Diagnostic.from_message_loc(
854
+ call_node,
855
+ rule: RULE_UNDEFINED_METHOD,
856
+ path: path,
857
+ message: message,
858
+ severity: :error,
859
+ receiver_type: rendered_receiver,
860
+ method_name: call_node.name.to_s,
861
+ project_definition_site: definition_site
862
+ )
863
+ end
864
+
865
+ private_class_method :unknown_block_self_receiver?, :lexically_defined_method?, :last_resort_surface_answers?,
866
+ :open_names_through_ancestors?, :foreign_singleton_through_ancestors?,
867
+ :computed_definer_called?, :hook_extended_macro_defines?, :hook_block_macro_defines?,
868
+ :macro_definers, :direct_hook_mixins, :own_mixin_edges, :deferred_dependencies,
869
+ :with_concern_dependencies, :hook_extension_defines?, :record_hook_target_edges,
870
+ :declared_constant, :const_get_constant, :hook_extends, :macro_entry_defines?,
871
+ :universal_macro_defines?, :rbs_ancestor_may_answer?, :subclass_instance_may_answer?,
872
+ :exact_literal_receiver?, :mixin_self_class_receiver?, :project_definition_site,
873
+ :project_sidecar_owns_method?, :unenumerable_receiver?, :unknown_mixin_includer?,
874
+ :known_mixin?, :open_project_singleton?, :open_source_class?, :opening_entry?,
875
+ :opening_hook?, :open_tail?, :call_site_exempt?, :safe_navigation_receiver,
876
+ :union_undefined_method_diagnostic, :union_may_answer?, :arm_surface_may_answer?,
877
+ :refined_on_some_arm?, :single_class_literal_union, :union_arm_blocks_undefined_fire?,
878
+ :build_undefined_method_diagnostic
879
+ end
880
+ end
881
+ end
882
+ end