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,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../reflection"
4
+ require_relative "../../type"
5
+ require_relative "../external_ancestor_resolution"
6
+ require_relative "user_method"
7
+
8
+ module Rigor
9
+ module Inference
10
+ module CallLadder
11
+ # The `overriding_def` rung of the call ladder (ADR-116 WD12): a call on an instance whose class has its own
12
+ # source `def` of the method, no declaration of its own, and an ancestor's declaration a project signature wrote,
13
+ # answered from the own `def`'s body. `ExpressionTyper` asks it ahead of the dispatcher, in each entry
14
+ # `CallLadder::RUNGS` gives it. {ExpressionTyper} is not required here: it requires this file, and `Scope` is
15
+ # loaded before either.
16
+ #
17
+ # Two entries, and the one other function is private to the module:
18
+ #
19
+ # - {.try_call} is the rung, asked with the site and the walking typer.
20
+ # - {.overriding_own_def?} is the rung's gate, which the read under a splat index (`c[*keys] += v`) asks in
21
+ # place of the rung: a splat leaves no positional argument types to bind to the own `def`'s body.
22
+ #
23
+ # The module holds no state. It reads the scope as `typer.scope` and calls back no method of the typer:
24
+ # `CallLadder::PORT`'s `scope` is all it reads there. It hands the typer on to its gate and to the user-method
25
+ # rung's positional entry, `CallLadder::UserMethod.infer`, which types the own `def`'s body.
26
+ module OverridingDef
27
+ module_function
28
+
29
+ # ADR-110 WD1 / #856 — the receiver's own `def` outranks a declaration that only an ANCESTOR carries.
30
+ #
31
+ # RBS resolves through the fully built method table, so `RbsDispatch` answers a subclass call with a
32
+ # signature written about the base. redmine's `FieldFormat::Base#target_class` honestly declares
33
+ # `-> nil`; `RecordList` overrides it with a real lookup, and every call on a `RecordList` receiver
34
+ # typed as `nil` and fired `undefined method ... for nil` on the subclass's own working code (#744).
35
+ # A signature is authoritative for a receiver only where someone wrote it about that receiver;
36
+ # reaching it through an ancestor is a lookup convenience, not an act of authorship.
37
+ #
38
+ # This is {CallLadder::TopLevelDef.try_call}'s shape one level up — there the enclosing class shadows a
39
+ # top-level `def`, here the receiver's class shadows an ancestor's declaration — and it answers the
40
+ # same way: re-type the override's body, and when that is not possible answer `Dynamic[Top]` rather
41
+ # than let the inherited declaration speak for a method it does not describe.
42
+ #
43
+ # Three conditions, and the third is the blast radius. Clause A of the ADR (an own source `def`, no
44
+ # own declaration) does NOT by itself stay clear of [ADR-43]: `class Foo; def each; end` inheriting
45
+ # `Enumerable#each` satisfies it, and disqualifying bundled declarations that way is the blanket fix
46
+ # ADR-43 rejected — the same reason {CallLadder::TopLevelDef}'s singleton RBS arm is own-class only. So the
47
+ # ancestor whose declaration is being disqualified must itself be PROJECT-declared: a project sidecar
48
+ # describes the source under analysis, while a bundled signature describes a class the project does
49
+ # not own, where a project `def` is a monkey-patch and [ADR-17] owns the question.
50
+ # `project_declared_class?` fail-softs to false, so an unattributable environment changes nothing.
51
+ #
52
+ # The rung, asked with the call `site` describes. The method name is the site's, not the call node's: the read
53
+ # of `c[k] += v` is a site whose node is the write and whose name is `[]`. The body is typed with no block
54
+ # type, as it was in the typer.
55
+ def try_call(site, typer)
56
+ node = site.call_node
57
+ receiver = site.receiver
58
+ arg_types = site.args
59
+ method_name = site.method_name
60
+ return nil unless overriding_own_def?(receiver, method_name, typer)
61
+
62
+ CallLadder::UserMethod.infer(receiver, node, arg_types, method_name, nil, typer) || Type::Combinator.untyped
63
+ end
64
+
65
+ # The three conditions above: an own source `def` of `method_name` on the receiver's class, which has no
66
+ # declaration of its own but inherits one a project-declared ancestor wrote.
67
+ def overriding_own_def?(receiver, method_name, typer)
68
+ return false unless CallLadder::UserMethod.user_inference_receiver?(receiver)
69
+
70
+ class_name = receiver.class_name
71
+ return false if class_name.nil?
72
+ # `Scope#user_def_for`, not `discovered_method?`: the cross-file table deliberately withholds a
73
+ # plain instance `def` under the ADR-17 monkey-patch contract, and this gate must see one.
74
+ return false if typer.scope.user_def_for(class_name, method_name).nil?
75
+
76
+ definition = ExternalAncestorResolution.method_definition(class_name, method_name, :instance,
77
+ scope: typer.scope)
78
+ return false if definition.nil?
79
+ return false if ExternalAncestorResolution.declared_on_class?(definition, class_name)
80
+
81
+ project_declared_owner?(definition, typer.scope)
82
+ end
83
+
84
+ # Whether the class an inherited declaration was written about is one the project declares itself.
85
+ def project_declared_owner?(definition, scope)
86
+ defined_in = definition.defined_in
87
+ return false if defined_in.nil?
88
+
89
+ Rigor::Reflection.project_declared_class?(defined_in.to_s, scope: scope)
90
+ rescue StandardError
91
+ false
92
+ end
93
+
94
+ private_class_method :project_declared_owner?
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,362 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../memo_audit"
4
+ require_relative "../../type"
5
+ require_relative "../singleton_external_ahead"
6
+ require_relative "../singleton_hook_decline"
7
+ require_relative "../singleton_hook_definers"
8
+ require_relative "../singleton_object_constant"
9
+
10
+ module Rigor
11
+ module Inference
12
+ module CallLadder
13
+ # The `project_singleton` rung of the call ladder (ADR-116 WD12): a call on a class object, or on a constant that
14
+ # holds an ordinary object with a `class << Const` body, answered by re-typing the singleton `def` the project
15
+ # wrote, with the call's argument types bound. `ExpressionTyper` asks it behind the user-method rung, in each
16
+ # entry `CallLadder::RUNGS` gives it. {ExpressionTyper} is not required here: it requires this file, and `Scope`
17
+ # is loaded before either.
18
+ #
19
+ # {.try_call} is the rung, asked with the site and the walking typer, and the module's one entry: only the
20
+ # ladder's entries ask it, so it has no second entry. Every other function is private to the module.
21
+ #
22
+ # The module holds no state. It reads the scope as `typer.scope` (never the site's, which `sig/` leaves
23
+ # untyped) and calls back four methods of the typer, the four the user-method rung calls back:
24
+ # `class_graph_buckets`, `positional_correspondence`, `infer_user_method_return` and `degrade_if_overridable`.
25
+ # `CallLadder::PORT` lists them, and `spec/rigor/inference/call_ladder/port_spec.rb` fails a call on the typer
26
+ # that is not one of them. A function that only reads the scope takes it as a parameter and never sees the typer.
27
+ module ProjectSingleton
28
+ module_function
29
+
30
+ # The project-side singleton-method band: a `Foo.bar` call resolved against a `class << …` / `def self.…`
31
+ # body the project itself wrote. Which of the two tiers applies is decided by the receiver carrier —
32
+ # `Singleton[Foo]` when the constant names a class or module, `Nominal[…]` when it holds an ordinary
33
+ # object (#320) — so the two are mutually exclusive and consulting both is one resolution attempt.
34
+ #
35
+ # The rung, asked with the call `site` describes. The method name is the site's, not the call node's: the read
36
+ # of `c[k] += v` is a site whose node is the write and whose name is `[]`.
37
+ def try_call(site, typer)
38
+ receiver = site.receiver
39
+ call_node = site.call_node
40
+ arg_types = site.args
41
+ method_name = site.method_name
42
+ block_type = site.block_type
43
+ try_singleton_method_inference(receiver, call_node, arg_types, method_name, block_type, typer) ||
44
+ try_singleton_object_constant_inference(receiver, call_node, arg_types, method_name, block_type, typer)
45
+ end
46
+
47
+ # Module-singleton call resolution (ADR-57 follow-up) — resolves `Foo.<name>` on a `Singleton[Foo]`
48
+ # receiver against `Foo`'s user-side singleton defs (`def self.x`, `def Foo.x`, a `class << self`
49
+ # body, or a `module_function` method) and re-types the body with the call's argument types bound.
50
+ # The body scope's `self_type` is the SAME `Singleton[Foo]` carrier, so an implicit-self call inside
51
+ # (`def self.via; helper(x); end`) re-enters this tier and resolves against the same singleton table
52
+ # — the symmetric counterpart of the instance-side ancestor walk.
53
+ #
54
+ # Issue #731 — resolution walks the SUPERCLASS chain, so a class method a base class defines answers
55
+ # on its subclasses (`class Plain < Base` with `def self.build` on `Base`: `Plain.build` was
56
+ # `Dynamic[top]` while `Plain.new.inst` resolved on the same fixture). Superclasses only, which is the
57
+ # whole shape of class-method inheritance — an `include`d module's `def self.x` is not callable on the
58
+ # includer, and `extend M` is already folded into the extender's own singleton entries by
59
+ # `ScopeIndexer`. A miss still degrades to `Dynamic[top]`, never a false resolution.
60
+ #
61
+ # The OWNER — not the receiver class — is what the overridable gate keys on, exactly as the instance
62
+ # side does: adopting `Base`'s literal return is unsound when a subclass redefines the method, and
63
+ # after this change the resolved body is routinely not the receiver's own.
64
+ def try_singleton_method_inference(receiver, call_node, arg_types, method_name, block_type, typer)
65
+ return nil unless receiver.is_a?(Type::Singleton)
66
+
67
+ def_node, owner = resolve_singleton_def_with_owner(receiver.class_name, method_name, typer)
68
+ return nil if def_node.nil?
69
+ # Issue #1936 — a contested slot holds a `def` a later definer may have replaced
70
+ # (`singleton_class.alias_method :x, :y` after `def self.x`), or one that may not have run: its body is not
71
+ # proven to be the method Ruby calls, so the call falls through to `Dynamic`.
72
+ return nil if typer.scope.discovery.contested?(:discovered_singleton_def_nodes, [owner, method_name.to_sym])
73
+
74
+ arg_types, positionals_unknown = typer.positional_correspondence(call_node, arg_types)
75
+ result = typer.infer_user_method_return(def_node, receiver, arg_types,
76
+ yield_type: block_type, positionals_unknown: positionals_unknown)
77
+ return result if result.nil?
78
+
79
+ typer.degrade_if_overridable(result, owner, method_name, :singleton)
80
+ rescue StandardError
81
+ nil
82
+ end
83
+
84
+ # The singleton twin of {ExpressionTyper#resolve_user_def_with_owner}. Memoised in a bucket nested under the
85
+ # SINGLETON def table's identity as well as the class-graph trio: `class_graph_buckets` keys on
86
+ # `discovered_def_nodes` / `discovered_superclasses` / `discovered_includes`, and this walk reads a
87
+ # fourth table those three do not imply. Keying on what the answer actually depends on is the #682
88
+ # lesson — a memo that outlives its inputs serves one scope's answer to another.
89
+ #
90
+ # It is the TYPING read only, so it also withholds (`[nil, nil]`, the call types `Dynamic`) where an external
91
+ # ancestor may answer ahead of the definer found (#1572: `extend M; extend Enumerable` makes `C.to_a`
92
+ # `Enumerable#to_a`, which the reader passes over). The existence reads keep the reader's own answer.
93
+ #
94
+ # The memo is the typer's class-graph slot ({ExpressionTyper#class_graph_buckets}), under its `:singleton_def`
95
+ # bucket.
96
+ def resolve_singleton_def_with_owner(class_name, method_name, typer)
97
+ by_singleton = (typer.class_graph_buckets[:singleton_def] ||= {}.compare_by_identity)
98
+ cache = (by_singleton[typer.scope.discovered_singleton_def_nodes] ||= {})
99
+ table = (cache[class_name.to_s] ||= {})
100
+ key = method_name.to_sym
101
+ if table.key?(key)
102
+ return MemoAudit.check("class graph singleton def", table[key], class_name, key) do
103
+ compute_singleton_def_with_owner(class_name, method_name, typer.scope)
104
+ end
105
+ end
106
+
107
+ table[key] = MemoAudit.key_audit("class graph singleton def") do
108
+ compute_singleton_def_with_owner(class_name, method_name, typer.scope)
109
+ end
110
+ end
111
+
112
+ def compute_singleton_def_with_owner(class_name, method_name, scope)
113
+ found = scope.singleton_def_through_ancestors(class_name, method_name)
114
+ owner = found.last
115
+ found = [nil, nil] if owner && SingletonExternalAhead.withhold?(scope, class_name, method_name, owner)
116
+ if found.last && dynamic_singleton_definer_ahead?(class_name, method_name, found.last, scope)
117
+ found = [nil, nil]
118
+ end
119
+ found = [nil, nil] if found.last && singleton_hook_order_split?(class_name, method_name, scope)
120
+ found = [nil, nil] if found.last && singleton_mixin_split?(class_name, method_name, found, scope)
121
+ found = [nil, nil] if found.last && unseen_definer_split?(class_name, method_name, found, scope)
122
+ found
123
+ end
124
+
125
+ # A definer no def table holds that Ruby may call instead of `found`: a hook-placed one, or a foreign `def C.x`.
126
+ def unseen_definer_split?(class_name, method_name, found, scope)
127
+ hidden_hook_split?(class_name, method_name, found.last, scope) ||
128
+ foreign_definer_behind_copy?(method_name, found, scope)
129
+ end
130
+
131
+ # Issue #3155 — a `def C.x` outside C's body (in the extended module, another class or the file) defines C's
132
+ # own `x`, which no def table holds, only its object-singleton mark. A fold copy found on C may then be a
133
+ # body Ruby does not call, so the typing read withholds.
134
+ def foreign_definer_behind_copy?(method_name, found, scope)
135
+ node, owner = found
136
+ return false unless singleton_copy_head(owner, node, scope) != owner
137
+
138
+ SingletonHookDecline.foreign_singleton_definer?(scope, owner, method_name)
139
+ end
140
+
141
+ # The hook-placed definers a found singleton `def` may not be the one Ruby calls through.
142
+ def hidden_hook_split?(class_name, method_name, owner, scope)
143
+ hidden_hook_definer?(class_name, method_name, owner, scope) ||
144
+ hook_block_rival?(class_name, method_name, owner, scope)
145
+ end
146
+
147
+ # Issue #3005 — a class object's own `def self.x` and a `def self.x` a concern's `included` block defines on it
148
+ # replace each other in the order the `def` and the `include` run, which the tables do not hold, so the typing
149
+ # read withholds when the answer is a class object's own `def` and its level's hook-block module defines the
150
+ # name too.
151
+ def hook_block_rival?(class_name, method_name, owner, scope)
152
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
153
+ index = chain.entries.index { |entry| !entry.external? && entry.side == :singleton && entry.name == owner }
154
+ !index.nil? && chain.hook_block_rival?(scope, index, method_name)
155
+ end
156
+
157
+ # Issue #1592 — a module a hook extends the class with may define the name private (`class_methods do private;
158
+ # def x`), which Ruby refuses on an explicit receiver. The visibility rule does not reach that module, so the
159
+ # typing read withholds rather than answer a call Ruby raises on. A module the class `extend`s itself is left to
160
+ # that rule.
161
+ def hidden_hook_definer?(class_name, method_name, owner, scope)
162
+ visibility = scope.discovered_method_visibility(owner, method_name)
163
+ return false if visibility.nil? || visibility == :public
164
+
165
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
166
+ return false unless chain.entries.any? { |entry| entry.name == owner && entry.side == :instance }
167
+
168
+ scope.singleton_extends_of(class_name).none? do |raw|
169
+ written = raw.delete_prefix("::")
170
+ owner == written || owner.end_with?("::#{written}")
171
+ end
172
+ end
173
+
174
+ # Issue #1592 — whether to withhold `method_name`'s class-side answer: a class object on the chain has
175
+ # hook-extended modules whose order against its own `extend`s is not a fact, and the orders give different
176
+ # definers (the retro world's against the chain's, or, with no retro world, the class objects' alone against
177
+ # the chain's). A `def` the extends fold copied onto a class object is read where Ruby finds it, at the extended
178
+ # module's own entry, since the copy sits ahead of every module in both orders.
179
+ def singleton_hook_order_split?(class_name, method_name, scope)
180
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
181
+ return false unless chain.hook_order_marked?
182
+
183
+ answer = positioned_singleton_definer(chain, method_name, scope)
184
+ retro = chain.retro_world
185
+ return answer != positioned_singleton_definer(retro, method_name, scope) if retro
186
+
187
+ answer != positioned_singleton_definer(chain, method_name, scope, copies: true)
188
+ end
189
+
190
+ # Issues #2998 and #3020 — whether a singleton-side mixin of a class object on the chain may answer ahead of
191
+ # `found`: a named prepend master's order does not place, or a module no table names.
192
+ def singleton_mixin_split?(class_name, method_name, found, scope)
193
+ singleton_prepend_split?(class_name, method_name, found, scope) ||
194
+ unnamed_singleton_mixin_split?(class_name, method_name, scope)
195
+ end
196
+
197
+ # Issue #2998 — whether to withhold the class-side answer `found` on a chain whose class objects have
198
+ # singleton-side prepends of their own: master's order, which a chain that does not stand answers, places no
199
+ # such prepend, so its definer may not be the one Ruby's order reaches first. A `def` the extends fold copied
200
+ # onto a class object is compared as its module's. A definer from a prepend region whose order is not a fact
201
+ # is withheld whichever order answers.
202
+ def singleton_prepend_split?(class_name, method_name, found, scope)
203
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
204
+ return false unless chain.singleton_prepend_marked?
205
+
206
+ answer = positioned_singleton_definer(chain, method_name, scope)
207
+ return true if unordered_prepend_definer?(chain, answer)
208
+
209
+ node, owner = found
210
+ head = if scope.singleton_def_for(owner, method_name).equal?(node)
211
+ singleton_copy_head(owner, node, scope)
212
+ else
213
+ owner
214
+ end
215
+ head != answer
216
+ end
217
+
218
+ # Whether `answer` is a module in the prepend region of a class object on the chain whose prepends have no known
219
+ # order: the entries between the start of that class's level and its own entry.
220
+ def unordered_prepend_definer?(chain, answer)
221
+ chain.prepend_order_nodes.any? do |node|
222
+ level = chain.level_classes.index(node)
223
+ next true if level.nil?
224
+
225
+ start = chain.level_starts[level]
226
+ own = chain.entries.index { |entry| entry.side == :singleton && entry.name == node }
227
+ next true if own.nil?
228
+
229
+ chain.entries[start...own].any? { |entry| entry.name == answer }
230
+ end
231
+ end
232
+
233
+ # Issue #3020 — whether to withhold the class-side answer on a chain whose class objects mix a module no table
234
+ # names into their singleton side, which may define the name. A prepend (`class << self; prepend(*MODS)`) sits
235
+ # ahead of the class object's own `def self.`, so a definer at or behind that class object's level is withheld;
236
+ # an `extend(*MODS)` or a `class << self` `include(*MODS)` sits right behind it, so a definer behind the class
237
+ # object's own entry is withheld and its own `def self.` still answers.
238
+ def unnamed_singleton_mixin_split?(class_name, method_name, scope)
239
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
240
+ prepends = chain.unnamed_prepend_nodes
241
+ extends = chain.unnamed_extend_nodes
242
+ return false if prepends.empty? && extends.empty?
243
+
244
+ answer = positioned_singleton_definer(chain, method_name, scope)
245
+ prepends.any? { |node| !answered_ahead?(chain, answer, level_start_of(chain, node)) } ||
246
+ extends.any? { |node| !answered_ahead?(chain, answer, own_singleton_index(chain, node)&.succ) }
247
+ end
248
+
249
+ # Whether `answer` owns an entry before `stop` (nil when the position is unknown: never). A nil `answer` (no
250
+ # class object's own def) owns none.
251
+ def answered_ahead?(chain, answer, stop)
252
+ !stop.nil? && chain.entries[0...stop].any? { |entry| entry.name == answer }
253
+ end
254
+
255
+ def level_start_of(chain, node)
256
+ level = chain.level_classes.index(node)
257
+ level && chain.level_starts[level]
258
+ end
259
+
260
+ def own_singleton_index(chain, node)
261
+ chain.entries.index { |entry| entry.side == :singleton && entry.name == node }
262
+ end
263
+
264
+ # The owner of the first `def` of the name along the chain: a fold copy is skipped, or with `copies:` read as
265
+ # its module's, on the class objects alone (the answer master gives).
266
+ def positioned_singleton_definer(chain, method_name, scope, copies: false)
267
+ chain.search(scope, side: copies ? :singleton : nil) do |entry|
268
+ next if entry.external?
269
+
270
+ name = entry.name
271
+ if entry.side == :singleton
272
+ node = scope.singleton_def_for(name, method_name)
273
+ next nil if node.nil?
274
+
275
+ head = singleton_copy_head(name, node, scope)
276
+ copies ? head : (name if head == name)
277
+ elsif scope.user_def_for(name, method_name)
278
+ name
279
+ end
280
+ end
281
+ end
282
+
283
+ # The module a receiverless singleton `def` was written in when the extends fold copied it onto `owner`, else
284
+ # `owner`.
285
+ def singleton_copy_head(owner, node, scope)
286
+ head = node.receiver.nil? ? scope.discovery.recorded_def_nesting(node)&.first : nil
287
+ head.nil? ? owner : head
288
+ end
289
+
290
+ SINGLETON_WILDCARD_SLOT = %i[singleton *].freeze
291
+ private_constant :SINGLETON_WILDCARD_SLOT
292
+
293
+ # Issue #2284 — a project entry nearer than the `def`'s owner on the singleton chain may define the name
294
+ # without a `def`, which the def tables do not hold: `class S < B; define_singleton_method(:x) { "s" }; end`
295
+ # calls S's block, not `B.x`. The farther `def` is then withheld and the call types `Dynamic`. A class's own
296
+ # singleton side is read from the visibility-contest census: `define_singleton_method(:x)`,
297
+ # `singleton_class.define_method(:x)` / `attr_*` and a computed name contest the entry's slot whether or not a
298
+ # `def` is recorded (#1984, #2013). An `extend`ed module's side is read from `discovered_methods`, which holds
299
+ # its `attr_*` and `define_method` rows, and the module's `def self.extended` hook, or one written outside its
300
+ # body (`def Ex.extended`, #3450), may define any name on the extender, so it withholds too.
301
+ #
302
+ # A hook that runs as the receiver's class is created or includes a module, an `inherited` or `included` hook,
303
+ # may define the name too, so a `def` the receiver only inherits is withheld then as well
304
+ # ({SingletonHookDefiners}).
305
+ def dynamic_singleton_definer_ahead?(class_name, method_name, owner, scope)
306
+ name = method_name.to_sym
307
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
308
+ return true if SingletonHookDefiners.ahead?(chain, class_name.to_s, owner.to_s, scope)
309
+
310
+ chain.entries.each do |entry|
311
+ next if entry.external?
312
+ return false if entry.name == owner.to_s
313
+ return true if entry_defines_without_def?(entry, name, scope)
314
+ # Issue #1592 — a hook of a module the class includes may have extended it with a module no table names.
315
+ return true if entry.side == :singleton && chain.hook_open_nodes.include?(entry.name)
316
+ end
317
+ false
318
+ end
319
+
320
+ def entry_defines_without_def?(entry, name, scope)
321
+ unless entry.side == :singleton
322
+ return scope.discovered_method?(entry.name, name, :instance) ||
323
+ !scope.singleton_def_for(entry.name, :extended).nil? ||
324
+ SingletonHookDefiners.foreign_hook?(scope, entry.name, :extended)
325
+ end
326
+
327
+ discovery = scope.discovery
328
+ # No edge of its own: `singleton_def_through_ancestors`' chain search filed every entry ahead of the owner.
329
+ discovery.contested?(:discovered_method_visibilities, [entry.name, [:singleton, name]]) ||
330
+ discovery.contested?(:discovered_method_visibilities, [entry.name, SINGLETON_WILDCARD_SLOT])
331
+ end
332
+
333
+ # #320 — resolves a call whose receiver is a constant holding an ordinary object with a `class << Const`
334
+ # singleton body, re-typing that body with the call's argument types bound. `self` inside the body IS
335
+ # that object, so the receiver carrier is passed through unchanged. Own-constant only, and only for a
336
+ # name the project actually recorded — a miss degrades to today's `Dynamic[top]`, never a false
337
+ # resolution. `Singleton` receivers never reach here: {.try_singleton_method_inference} owns them.
338
+ def try_singleton_object_constant_inference(receiver, call_node, arg_types, method_name, block_type, typer)
339
+ return nil unless receiver.is_a?(Type::Nominal)
340
+
341
+ def_node = SingletonObjectConstant.def_node_for(call_node, receiver, method_name, typer.scope)
342
+ return nil if def_node.nil?
343
+
344
+ arg_types, positionals_unknown = typer.positional_correspondence(call_node, arg_types)
345
+ typer.infer_user_method_return(def_node, receiver, arg_types,
346
+ yield_type: block_type, positionals_unknown: positionals_unknown)
347
+ rescue StandardError
348
+ nil
349
+ end
350
+
351
+ private_class_method :try_singleton_method_inference, :resolve_singleton_def_with_owner,
352
+ :compute_singleton_def_with_owner, :unseen_definer_split?, :foreign_definer_behind_copy?,
353
+ :hidden_hook_split?, :hook_block_rival?, :hidden_hook_definer?,
354
+ :singleton_hook_order_split?, :singleton_mixin_split?, :singleton_prepend_split?,
355
+ :unordered_prepend_definer?, :unnamed_singleton_mixin_split?, :answered_ahead?,
356
+ :level_start_of, :own_singleton_index, :positioned_singleton_definer,
357
+ :singleton_copy_head, :dynamic_singleton_definer_ahead?,
358
+ :entry_defines_without_def?, :try_singleton_object_constant_inference
359
+ end
360
+ end
361
+ end
362
+ end