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,750 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../memo_audit"
6
+ require_relative "../../source/constant_path"
7
+ require_relative "../../typing_state"
8
+
9
+ module Rigor
10
+ module Inference
11
+ module DeclarationWalk
12
+ # The declaration-context rules (ADR-116 WD9): what a `class` / `module` header, a `K = Class.new { … }`-family
13
+ # write and an eval-family block make of `self`, the cref and `Module.nesting`, with the recognisers and the
14
+ # per-file oracle of declared names those answers rest on. It is the closure of the calls {Context} and
15
+ # {Traversal} make for a rule, moved out of `scope_indexer.rb` whole. `ScopeIndexer`'s hand-written walkers, the
16
+ # collectors under `scope_indexer/` and the other files that ask a rule call the same functions, as
17
+ # `DeclarationWalk::Rules.x`.
18
+ #
19
+ # Every function is a module function of the positionals it is handed. The module holds no state of its own: its
20
+ # one memo, the set of names a parse's source declares, is in the `TypingState` slot {DECLARED_NAMES} declares.
21
+ # It names nothing of `ScopeIndexer`: a rule that needs a walker's answer takes it as an argument.
22
+ # `spec/rigor/inference/declaration_walk/rules_spec.rb` pins the functions callable from outside and the constants
23
+ # the module exports, as a list that may shrink and does not grow; an entry added here is an amendment to
24
+ # ADR-116's WD9 section.
25
+ #
26
+ # `scope_indexer.rb` requires this file at the end of the `require_relative` block at its head. {DECLARED_NAMES}
27
+ # is declared as the file loads, and that place keeps its index in `TypingState.slots`, which loading it from
28
+ # `traversal.rb` would move. So `context.rb` and `traversal.rb` name {Rules} without requiring this file; they are
29
+ # loaded through `rigor/inference/declaration_walk`.
30
+ #
31
+ # @api private
32
+ module Rules # rubocop:disable Metrics/ModuleLength
33
+ module_function
34
+
35
+ # The scope a census walk threads into the body a `class` / `module` header opens: the scope it already
36
+ # threads, carrying that body's real `Module.nesting`.
37
+ #
38
+ # Issue #708 — the qualified prefix stopped being sufficient to DERIVE that chain. A rooted header
39
+ # RESETS the prefix (`class ::Rooted` inside `module Outer` prefixes `["Rooted"]`), so `Outer` is absent
40
+ # from it entirely and no function of the prefix alone recovers it, while Ruby keeps `Outer` on the
41
+ # ladder as a live rung. `Source::ConstantPath.pushed_nesting` — the one function both declaration walks
42
+ # already push with — has that case right, so the chain travels on the scope these walks thread anyway
43
+ # rather than through every walk's parameter list, and stays a single owner ([#652](https://github.com/rigortype/rigor/issues/652)).
44
+ def scope_entering_declaration(default_scope, constant_path)
45
+ return default_scope if default_scope.nil?
46
+
47
+ chain = Source::ConstantPath.pushed_nesting(default_scope.lexical_nesting || EMPTY_NESTING,
48
+ constant_path)
49
+ chain ? default_scope.with_lexical_nesting(chain) : default_scope
50
+ end
51
+
52
+ # Ruby's `Module.nesting` for a body enclosed by `qualified_prefix`, innermost first: each entry is the
53
+ # prefix truncated at one declaration, joined, so the compact spelling contributes exactly one entry and
54
+ # the nested spelling one per `class` / `module` keyword.
55
+ #
56
+ # **This is an approximation, and only correct when no ROOTED header encloses the body** — a rooted
57
+ # header resets the prefix without resetting Ruby's nesting, so the entries beneath the reset are not
58
+ # in the prefix to be truncated out of it (#708). It remains the answer for a caller that threaded no
59
+ # chain, where it is what the code did before and never a new firing; the walks that reach a body
60
+ # thread the real chain via {#scope_entering_declaration}.
61
+ def lexical_nesting_for_prefix(qualified_prefix)
62
+ (1..qualified_prefix.size).map { |n| qualified_prefix.first(n).join("::") }.reverse.freeze
63
+ end
64
+
65
+ # Issue #705 — the block-taking calls that swap `self` for the RECEIVER: Ruby evaluates the block with
66
+ # the receiver as `self` and leaves `Module.nesting` untouched, so `self::BAR = …` inside one names the
67
+ # receiver's `BAR` while a bare `BAR = …` in the same block still names the enclosing namespace's.
68
+ SELF_REBINDING_EVAL_CALLS = %i[class_eval module_eval class_exec module_exec].freeze
69
+ private_constant :SELF_REBINDING_EVAL_CALLS
70
+
71
+ # `instance_eval` / `instance_exec` rebind `self` to the receiver exactly as `class_eval`
72
+ # does — `X.instance_eval { self::BAR = 1 }` writes `X::BAR` — but the default definee moves
73
+ # to the receiver's SINGLETON instead of the receiver: `X.instance_eval { def m }` installs
74
+ # `X.m`, while `define_method`/`attr_*` inside (method calls on the receiver-as-module)
75
+ # still install `X#m`. The def-owning walks split the two surfaces on this list.
76
+ INSTANCE_EVAL_CALLS = %i[instance_eval instance_exec].freeze
77
+ RECEIVER_EVAL_CALLS = (SELF_REBINDING_EVAL_CALLS + INSTANCE_EVAL_CALLS).freeze
78
+
79
+ # The owner whose constants the census spells BARE — `Object`'s constants are the top-level ones.
80
+ OBJECT_OWNER = "Object"
81
+
82
+ # Whether `self` in this position names nothing a `self::` receiver or write target
83
+ # can resolve against: inside `class <<` (self is the singleton), under a self an
84
+ # eval declined to name (an explicit empty def-owner), or anywhere below an
85
+ # unnameable lexical cref without an eval-derived self — a `class D` nested in
86
+ # `class <<` reopens `#<Class:C>::D`, a real class object nothing can spell. A
87
+ # nameable eval owner still answers: `Foo.class_eval { self::X = }` inside
88
+ # `class <<` resolves `Foo::X`.
89
+ def unnameable_eval_self?(in_singleton_class, def_owner_prefix, qualified_prefix, singleton_cref)
90
+ return true if in_singleton_class
91
+ return true if def_owner_prefix && def_owner_prefix.empty?
92
+
93
+ singleton_cref && (def_owner_prefix || qualified_prefix).empty?
94
+ end
95
+
96
+ # v0.1.2 — when a `Const = Data.define(*sym) do ... end` / `Const = Struct.new(*sym) do ... end` constant write
97
+ # carries a block, the block body holds method overrides whose canonical class is `Const`. Survey item (e)
98
+ # extended the recognition to `Const = Module.new do ... end` and `Const = Class.new(?super) do ... end` — the
99
+ # ADR-16 Tier A "block-as-method" idiom at constant-write position. Returns the block body node (a
100
+ # `Prism::StatementsNode`) when the rvalue matches; nil otherwise. Used by
101
+ # `ScopeIndexer#walk_methods_and_def_nodes` to push `Const` onto the qualified prefix before recursing.
102
+ #
103
+ # Issue [#703](https://github.com/rigortype/rigor/issues/703) — a PATH write is the same idiom.
104
+ # `Holder::Thing = Struct.new(:a) do … end` inside a module is ordinary Ruby, and declining it here lost the
105
+ # factory registration, the member layout and the block's own defs together: the constant answered
106
+ # `singleton(Struct)` and its instance `Struct`, a class RBS knows, so every member read and every override
107
+ # call fired `call.undefined-method` on code Ruby runs. The name the write gives the class is
108
+ # {#meta_new_child_prefix}'s, so every walk that pushes it agrees.
109
+ #
110
+ # Issue [#963](https://github.com/rigortype/rigor/issues/963) — the spelling of the WRITE and the tail of the
111
+ # rvalue are both incidental to the idiom, and both were load-bearing here. `Const = Struct.new(:a) do … end`
112
+ # was recognised while `Const ||= Struct.new(:a) do … end` and `Const = Struct.new(:a) do … end.freeze` were
113
+ # not, so the same body was walked as a class body in one spelling and left in the enclosing scope in the
114
+ # other two. {#meta_new_block_call} owns both unwrappings; every walk asks it, so no spelling can be
115
+ # recognised by one pass and missed by another.
116
+ def meta_new_block_body(node)
117
+ block = meta_new_block_call(node)&.block
118
+ # A `&BODY` / `&:sym` block ARGUMENT is not a literal block: its body is unreadable here (#1816).
119
+ block.body if block.is_a?(Prism::BlockNode)
120
+ end
121
+
122
+ # The class-creating call a constant write's rvalue ultimately is, or nil when the write is not the idiom.
123
+ # The single recognition point for {#meta_new_block_body} and every caller that needs the CALL rather than
124
+ # its body — `StatementEvaluator#eval_constant_write` enters the block through it, so the evaluator and the
125
+ # index cannot disagree about which node opened the class body.
126
+ def meta_new_block_call(node)
127
+ rvalue = meta_new_rvalue(node)
128
+ return nil unless rvalue.is_a?(Prism::CallNode) && meta_new_constant_rvalue?(rvalue)
129
+
130
+ rvalue
131
+ end
132
+
133
+ # The four constant-write spellings that name the class their rvalue creates. `Const ||= …` is the
134
+ # define-once idiom: where the constant is unset — the case the program is written for — Ruby evaluates the
135
+ # rvalue and names the class `Const`, exactly as the plain write does. `&&=` and the operator writes are NOT
136
+ # here: neither names a freshly created class.
137
+ #
138
+ # The six whole-tree walks that recognise the idiom spell these four classes out in their `when` arms rather
139
+ # than splatting this list: `when *ARRAY` copies the array on EVERY evaluation, and the arms are evaluated once
140
+ # per AST node per walk, which cost `rigor check lib` about 2.3M allocations (+10%) when they were splatted.
141
+ # This list is for the `is_a?` scans; keep the arms and the list in step.
142
+ META_CONSTANT_WRITE_NODES = [
143
+ Prism::ConstantWriteNode,
144
+ Prism::ConstantPathWriteNode,
145
+ Prism::ConstantOrWriteNode,
146
+ Prism::ConstantPathOrWriteNode
147
+ ].freeze
148
+ private_constant :META_CONSTANT_WRITE_NODES
149
+
150
+ # The rvalue a recognised constant write assigns, with the two value-preserving wrappers stripped: a
151
+ # `.freeze` tail (`Struct.new(:a) do … end.freeze` — `Module#freeze` returns the receiver, so the constant
152
+ # still holds the class the call created) and a `Const = Const || Struct.new(…)` guard, the long spelling of
153
+ # `||=`. Nil when `node` is not a constant write at all.
154
+ def meta_new_rvalue(node)
155
+ return nil unless META_CONSTANT_WRITE_NODES.any? { |kind| node.is_a?(kind) }
156
+
157
+ unwrap_freeze_tail(unwrap_or_guard(node, node.value))
158
+ end
159
+
160
+ # `Const = Const || <rvalue>` carries the same meaning as `Const ||= <rvalue>`, and only when the guarded
161
+ # name is the constant being written: `A = B || Struct.new(:x)` may hold `B`, whose class `A` does not name.
162
+ def unwrap_or_guard(node, value)
163
+ return value unless value.is_a?(Prism::OrNode)
164
+
165
+ written = meta_constant_write_name(node)
166
+ return value if written.nil?
167
+
168
+ Source::ConstantPath.qualified_name_or_nil(value.left) == written ? value.right : value
169
+ end
170
+
171
+ # A `.freeze` tail, repeated (`freeze.freeze` is legal and idempotent). Only the receiverful, argumentless,
172
+ # blockless call is unwrapped — anything else is a different method that may return a different object.
173
+ # `&.freeze` is declined with them: its value is the receiver OR nil, and a rule that answers "the constant
174
+ # holds the class the factory made" must not be stated over a shape whose value can be nil.
175
+ def unwrap_freeze_tail(value)
176
+ value = value.receiver while freeze_tail?(value)
177
+ value
178
+ end
179
+
180
+ def freeze_tail?(value)
181
+ value.is_a?(Prism::CallNode) && value.name == :freeze && !value.safe_navigation? &&
182
+ value.receiver && value.arguments.nil? && value.block.nil?
183
+ end
184
+
185
+ # The name a recognised constant write assigns, as written (`Const`, `Holder::Thing`). Used only to compare
186
+ # a `Const = Const || …` guard against its own target.
187
+ def meta_constant_write_name(node)
188
+ case node
189
+ when Prism::ConstantWriteNode, Prism::ConstantOrWriteNode then node.name.to_s
190
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode
191
+ Source::ConstantPath.qualified_name_or_nil(node.target)
192
+ end
193
+ end
194
+
195
+ # The qualified prefix a meta-new constant write names its class under, given the lexical prefix the write sits
196
+ # in — the segments every scope-free pre-pass pushes before descending into the block, and the key
197
+ # {ScopeIndexer::DeclarationArtifacts.record_meta_new_constant?} files the discovered class under. Nil for a
198
+ # target that is neither form.
199
+ #
200
+ # A PATH write takes exactly what a compact `class Holder::Thing` header at the same position takes
201
+ # ({Source::ConstantPath.declaration_prefix}), rooted reset included. These pre-passes run before any scope
202
+ # exists, so the namespace cannot be resolved the way {ScopeIndexer::ConstantTables.constant_path_write_key}
203
+ # resolves it for the typed table — but the two land on the same name anyway, because registering
204
+ # `Admin::Holder::Thing` here is what makes `Admin::Holder` a namespace
205
+ # {ScopeIndexer::ConstantTables.resolved_write_namespace} knows.
206
+ #
207
+ # Where the namespace is NOT under the enclosure — a top-level `Loner` written as `Loner::Made = Struct.new(…)`
208
+ # inside `module Admin` — that qualification is a guess, and {ScopeIndexer#synthesize_namespace_prefixes} then
209
+ # answers a read of `Loner` there with the `Admin::Loner` it invented. That is the same answer the equivalent
210
+ # `class Loner::Made` header has always produced, from the same two functions: the two spellings of one
211
+ # declaration stay consistent rather than one of them carrying a second approximation of its own.
212
+ #
213
+ # Issue #963 — `Const ||= …` names its class exactly as `Const = …` does, so the two or-write spellings take
214
+ # the branch of the write shape they are the conditional form of.
215
+ # `self_base` names the rebound self for a `self::`-anchored target — inside a
216
+ # meta-new block `self` is the class the write names, so `self::X = Class.new`
217
+ # opens `K::X`, not the lexical `X`.
218
+ def meta_new_child_prefix(node, qualified_prefix, self_base = nil)
219
+ case node
220
+ when Prism::ConstantWriteNode, Prism::ConstantOrWriteNode
221
+ qualified_prefix + [node.name.to_s]
222
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode
223
+ target = node.target
224
+ if self_anchored_tail(target)
225
+ # A `self::` target anchors on the rebound self when one is named; nil
226
+ # `self_base` means `self` IS the lexical enclosure, so the lenient render
227
+ # is the right name — `self::S = Class.new` inside `class C` is `C::S`.
228
+ self_anchored_decl_prefix(target, self_base) ||
229
+ (self_base.nil? &&
230
+ Source::ConstantPath.declaration_prefix(qualified_prefix, target))
231
+ else
232
+ # Any other base must render a real constant path — `var::K = Class.new`
233
+ # writes whatever `var` holds, a class no source spelling reaches, so the
234
+ # lenient `declaration_prefix` render (`C::K`) would be a name the write
235
+ # never produced.
236
+ Source::ConstantPath.qualified_name_or_nil(target) &&
237
+ Source::ConstantPath.declaration_prefix(qualified_prefix, target)
238
+ end
239
+ end
240
+ end
241
+
242
+ # The three contexts a `K = Class.new { … }`-shaped write hands its children: the
243
+ # factory call's receiver and arguments evaluate in the ENCLOSING self and cref —
244
+ # the write has not landed yet — while the block body's `self` is the class the
245
+ # write names (`child_prefix`, or `[]` when the write names nothing below an
246
+ # unnameable cref or a dynamic base) and its `Module.nesting` stays lexical.
247
+ # Returns `[enclosing_parts, body, body_self]`; nil when the rvalue opens no
248
+ # recognised meta-new block. `self_base` names a rebound enclosing self for a
249
+ # `self::` write target.
250
+ def meta_new_block_split(node, qualified_prefix, self_base, singleton_cref)
251
+ call = meta_new_block_call(node)
252
+ return nil unless call
253
+
254
+ child_prefix = meta_new_child_prefix(node, qualified_prefix, self_base)
255
+ body_self =
256
+ if singleton_cref && !meta_new_path_target_nameable?(node, self_base)
257
+ []
258
+ else
259
+ child_prefix || []
260
+ end
261
+ enclosing = [call.receiver, *call.arguments&.arguments.to_a].compact
262
+ [enclosing, meta_new_block_body(node), body_self]
263
+ end
264
+
265
+ # The three values an eval-family block gives a leaf walk: the receiver and arguments
266
+ # that keep the enclosing context, the block body, and the rebound self's prefix —
267
+ # `[]` when the receiver names nothing. `def`-family leaves and `self::` declarations
268
+ # anchor on the third value while `Module.nesting` stays lexical, so the walk keeps
269
+ # `qualified_prefix` for declarations and swaps only the def-owner channel.
270
+ def eval_block_split(node, qualified_prefix, self_owner, singleton_cref)
271
+ return nil unless receiver_eval_call?(node)
272
+
273
+ self_prefix = self_owner || qualified_prefix
274
+ unnameable = unnameable_eval_self?(false, self_owner, qualified_prefix,
275
+ singleton_cref)
276
+ eval_prefix = eval_receiver_prefix(node, self_prefix, qualified_prefix,
277
+ unnameable_self: unnameable) || []
278
+ # An unnameable enclosing self — a `class <<` body, an ownerless eval or factory
279
+ # block — means a bare/`self`/`self::` receiver names nothing these leaf tables can
280
+ # key on; `eval_receiver_prefix` still answers `self_prefix` for the bare form, so
281
+ # the ownerless marker is applied here.
282
+ eval_prefix = EMPTY_PREFIX if unnameable && !eval_named_receiver?(node)
283
+ [[node.receiver, *node.arguments&.arguments.to_a].compact, node.block.body, eval_prefix]
284
+ end
285
+
286
+ # The wider form: any call whose block rebinds `self` to the receiver — `instance_eval` and
287
+ # `instance_exec` included. Walks that read `self`-anchored facts use this gate; walks that
288
+ # own `def` leaves additionally split on {INSTANCE_EVAL_CALLS}, whose `def`s bind on the
289
+ # receiver's singleton rather than the receiver.
290
+ def receiver_eval_call?(node)
291
+ node.is_a?(Prism::CallNode) && node.block.is_a?(Prism::BlockNode) && RECEIVER_EVAL_CALLS.include?(node.name)
292
+ end
293
+
294
+ # The owner an eval-family block body evaluates under, given the enclosing body's SELF
295
+ # prefix — `def_owner_prefix || qualified_prefix` at the call sites, because `self`
296
+ # inside an eval body is the enclosing eval's receiver, not the lexical class — and the
297
+ # LEXICAL prefix, which answers the constant-receiver case: constant lookup in an eval
298
+ # body stays lexical, so `Y.class_eval` written inside `M::Y.class_eval` at top level
299
+ # re-opens the TOP-LEVEL `Y`, never `M::Y` again (a class is not a member of its own
300
+ # constant table). A bare or `self` receiver keeps the enclosing self — `class_eval`'s
301
+ # self IS the block's self — and a constant receiver names its own class, the same
302
+ # convention {ScopeIndexer#singleton_class_prefix} applies to `class <<`. A receiver that renders no
303
+ # static path — a variable, a call, a dynamic-base `expr::Bar` — is DECLINED like
304
+ # {ScopeIndexer::ConstantTables.constant_path_write_key} declines dynamic write targets: `qualified_name`'s
305
+ # lenient render would drop the dynamic segment and file the body's defs under a class the eval
306
+ # never opened.
307
+ def eval_receiver_prefix(node, self_prefix, lexical_prefix, unnameable_self: false)
308
+ receiver = node.receiver
309
+ return self_prefix if receiver.nil? || receiver.is_a?(Prism::SelfNode)
310
+
311
+ # `self::Foo` / `self::Foo::Bar` names the enclosing self's path — the same
312
+ # resolution {ScopeIndexer::ConstantTables.constant_path_write_key} gives a `self::` write target. When the
313
+ # enclosing self names nothing ({#unnameable_eval_self?}) the read raises
314
+ # NameError at runtime unless the constant lives on that self — so the receiver
315
+ # declines rather than filing the body under the lexical class.
316
+ if (tail = self_anchored_tail(receiver))
317
+ return nil if unnameable_self
318
+
319
+ return collapse_object_owner(self_prefix + tail)
320
+ end
321
+
322
+ rendered = Source::ConstantPath.qualified_name_or_nil(receiver)
323
+ return nil unless rendered
324
+
325
+ eval_constant_receiver_prefix(node, receiver, rendered, lexical_prefix)
326
+ end
327
+
328
+ # The tail of {#eval_receiver_prefix} once the receiver renders a static
329
+ # constant name — also the `class << Y` operand's resolution
330
+ # ({ScopeIndexer#singleton_class_prefix}), which follows the same lexical rule.
331
+ #
332
+ # A `::`-rooted receiver names the top level — `::B` inside `class A::B`
333
+ # is still `B`, so the root check precedes both the self-reopen shortcut
334
+ # and the lexical walk ({Source::ConstantPath}'s contract: callers doing
335
+ # Ruby's lexical constant lookup MUST consult `rooted?` out of band). An
336
+ # unqualified (or partially-qualified) receiver then resolves through
337
+ # `Module.nesting` — `X` inside `class S` names `S::X` whenever that
338
+ # constant exists — so the first `<nesting entry>::X` the file declares
339
+ # wins, innermost first, and the as-written name stands when no rung
340
+ # declares it: the same `<entry>::<first segment>` walk
341
+ # {ScopeIndexer::ConstantTables.resolved_write_namespace} performs for a `Foo::BAR = …` namespace,
342
+ # against the same segment-approximation {lexical_nesting_for_prefix}
343
+ # gives when no scope carries the real chain. A `S::X` declared only in
344
+ # ANOTHER file still resolves as-written — the set
345
+ # {#eval_file_declared_names} consults is the defining file's own answer,
346
+ # matching the `in_source_constants` limitation {ScopeIndexer::ConstantTables.resolved_write_namespace}
347
+ # documents.
348
+ def eval_constant_receiver_prefix(node, path_node, rendered, lexical_prefix)
349
+ # `class << ::X = expr` spells its root on the write's target, not the
350
+ # write node itself.
351
+ path_node = path_node.target if path_node.is_a?(Prism::ConstantPathWriteNode)
352
+ return rendered.split("::") if Source::ConstantPath.rooted?(path_node)
353
+ return rendered.split("::") if lexical_prefix.empty?
354
+ return lexical_prefix if lexical_prefix.last == rendered
355
+
356
+ segments = rendered.split("::")
357
+ declared = eval_file_declared_names(node)
358
+ lexical_nesting_for_prefix(lexical_prefix).each do |entry|
359
+ candidate = "#{entry}::#{segments.first}"
360
+ return candidate.split("::") + segments[1..] if declared.include?(candidate)
361
+ end
362
+ segments
363
+ end
364
+
365
+ # The file's declared-constant oracle {eval_constant_receiver_prefix}
366
+ # consults: every class/module declaration plus constant write the file
367
+ # contains, expanded to every enclosing prefix (declaring `S::A::B`
368
+ # proves `S::A` exists). Memoized on the `Prism::Source` every node's
369
+ # location shares — threading the set through the dozen eval-consuming
370
+ # walks would dwarf the resolution it feeds, so the one re-parse this
371
+ # needs happens once per file. The memo is a {TypingState} Memo, a
372
+ # WeakKeyMap keyed on the source: the entry's lifetime is the parse's own
373
+ # (the map holds the key weakly), the set is a pure function of the
374
+ # source's text, and a worker parsing the same text gets its own source
375
+ # and its own set. Not run-scoped: an entry holds no run's tables.
376
+ DECLARED_NAMES = TypingState.memo(
377
+ :scope_index_declared_names,
378
+ audit: "scope index declared names",
379
+ reason: "a WeakKeyMap keyed on the Prism::Source; the frozen set is a pure function of the source's text"
380
+ )
381
+ private_constant :DECLARED_NAMES
382
+
383
+ def eval_file_declared_names(node)
384
+ source = node.location.send(:source)
385
+ return scan_declared_names(source) if MemoAudit.enabled? && MemoAudit.suspended?
386
+
387
+ table = TypingState.current.weak_key_map(DECLARED_NAMES)
388
+ stored = table[source]
389
+ if stored.nil?
390
+ return table[source] = MemoAudit.key_audit("scope index declared names") { scan_declared_names(source) }
391
+ end
392
+
393
+ MemoAudit.check("scope index declared names", stored, source.source.bytesize) { scan_declared_names(source) }
394
+ end
395
+
396
+ def scan_declared_names(source)
397
+ names = Set.new
398
+ collect_declared_constant_names(Prism.parse(source.source).value, [], names)
399
+ names.freeze
400
+ end
401
+
402
+ # The walk behind {eval_file_declared_names}: every qualified constant
403
+ # name a declaration introduces, `class`/`module`/`class <<` bodies and
404
+ # `Const = …` writes alike. Deliberately NOT {ScopeIndexer::ClassDeclarations} —
405
+ # that walk resolves eval receivers through {eval_receiver_prefix},
406
+ # which consults this oracle and would reenter it mid-build; this one
407
+ # never asks the question it feeds. `def` bodies cannot contain constant
408
+ # declarations (a `class`/`X =` inside one is a SyntaxError), so they
409
+ # are skipped outright. Under an unnameable cref (`class <<`, an
410
+ # anonymous factory block) bare and `self::` declarations name nothing —
411
+ # the same refusal the discovery walks make — while explicit-base paths
412
+ # still re-anchor lexically.
413
+ def collect_declared_constant_names(node, qualified_prefix, names, unnameable_cref: false)
414
+ return unless node.is_a?(Prism::Node)
415
+
416
+ case node
417
+ when Prism::ClassNode, Prism::ModuleNode
418
+ return collect_declared_class_decl(node, qualified_prefix, names, unnameable_cref)
419
+ when Prism::SingletonClassNode
420
+ collect_declared_constant_names(node.expression, qualified_prefix, names,
421
+ unnameable_cref: unnameable_cref)
422
+ return collect_declared_constant_names(node.body, qualified_prefix, names,
423
+ unnameable_cref: true)
424
+ when Prism::ConstantWriteNode, Prism::ConstantOrWriteNode,
425
+ Prism::ConstantAndWriteNode, Prism::ConstantOperatorWriteNode
426
+ return collect_declared_bare_write(node, qualified_prefix, names, unnameable_cref)
427
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode,
428
+ Prism::ConstantPathAndWriteNode, Prism::ConstantPathOperatorWriteNode
429
+ return collect_declared_path_write(node, qualified_prefix, names, unnameable_cref)
430
+ when Prism::ConstantTargetNode
431
+ add_declared_name(names, (qualified_prefix + [node.name.to_s]).join("::")) unless unnameable_cref
432
+ return
433
+ when Prism::DefNode
434
+ return
435
+ when Prism::CallNode
436
+ return if collect_declared_anonymous_factory?(node, qualified_prefix, names,
437
+ unnameable_cref)
438
+ end
439
+ node.compact_child_nodes.each do |child|
440
+ collect_declared_constant_names(child, qualified_prefix, names,
441
+ unnameable_cref: unnameable_cref)
442
+ end
443
+ end
444
+
445
+ # The `class`/`module` arm of {collect_declared_constant_names}: the
446
+ # header's own qualified name plus the body under it.
447
+ def collect_declared_class_decl(node, qualified_prefix, names, unnameable_cref)
448
+ prefix = declared_constant_path_prefix(node.constant_path, qualified_prefix,
449
+ unnameable_cref)
450
+ add_declared_name(names, prefix.join("::")) if prefix
451
+ if node.is_a?(Prism::ClassNode)
452
+ collect_declared_constant_names(node.superclass, qualified_prefix, names,
453
+ unnameable_cref: unnameable_cref)
454
+ end
455
+ collect_declared_constant_names(node.body, prefix || qualified_prefix, names,
456
+ unnameable_cref: unnameable_cref)
457
+ end
458
+
459
+ # The bare `CONST =` arm of {collect_declared_constant_names}: the write
460
+ # lands on the enclosing cref, so an unnameable one declines the name.
461
+ def collect_declared_bare_write(node, qualified_prefix, names, unnameable_cref)
462
+ prefix = qualified_prefix + [node.name.to_s]
463
+ add_declared_name(names, prefix.join("::")) unless unnameable_cref
464
+ collect_rvalue_declared_names(node.value, prefix, qualified_prefix, names,
465
+ unnameable_cref)
466
+ end
467
+
468
+ # The `Path::CONST =` arm of {collect_declared_constant_names}.
469
+ def collect_declared_path_write(node, qualified_prefix, names, unnameable_cref)
470
+ prefix = declared_constant_write_prefix(node.target, qualified_prefix,
471
+ unnameable_cref)
472
+ add_declared_name(names, prefix.join("::")) if prefix
473
+ collect_rvalue_declared_names(node.value, prefix || qualified_prefix,
474
+ qualified_prefix, names, unnameable_cref)
475
+ end
476
+
477
+ # The unnamed-factory arm of {collect_declared_constant_names}: a
478
+ # `Class.new`/`Module.new`/`Data.define`/`Struct.new` block's cref is the
479
+ # anonymous class — declarations inside name nothing these tables carry.
480
+ # (An eval-family block is NOT special here: `Module.nesting` does not
481
+ # change, so `class Y` inside `X.class_eval` still declares under the
482
+ # lexical prefix — the generic child walk covers it.)
483
+ def collect_declared_anonymous_factory?(node, qualified_prefix, names, unnameable_cref)
484
+ return false unless meta_new_constant_rvalue?(node) && node.block
485
+
486
+ node.compact_child_nodes.each do |child|
487
+ next if child.equal?(node.block)
488
+
489
+ collect_declared_constant_names(child, qualified_prefix, names,
490
+ unnameable_cref: unnameable_cref)
491
+ end
492
+ collect_declared_constant_names(node.block, qualified_prefix, names,
493
+ unnameable_cref: true)
494
+ true
495
+ end
496
+
497
+ # The rvalue half of {collect_declared_constant_names}'s write arm: a
498
+ # `Const = <factory> do … end` block's cref is the new class, so the block
499
+ # declares under the WRITTEN prefix; the call's other children and any
500
+ # non-factory rvalue keep the enclosing context.
501
+ def collect_rvalue_declared_names(value, written_prefix, qualified_prefix, names,
502
+ unnameable_cref)
503
+ if value.is_a?(Prism::CallNode) && meta_new_constant_rvalue?(value) && value.block
504
+ value.compact_child_nodes.each do |child|
505
+ next if child.equal?(value.block)
506
+
507
+ collect_declared_constant_names(child, qualified_prefix, names,
508
+ unnameable_cref: unnameable_cref)
509
+ end
510
+ collect_declared_constant_names(value.block, written_prefix, names,
511
+ unnameable_cref: unnameable_cref)
512
+ else
513
+ collect_declared_constant_names(value, qualified_prefix, names,
514
+ unnameable_cref: unnameable_cref)
515
+ end
516
+ end
517
+
518
+ # The qualified prefix a `class`/`module` header declares: a `::`-rooted
519
+ # path re-anchors at the top level; a bare or `self::` path under an
520
+ # unnameable cref names nothing; every explicit base re-anchors
521
+ # lexically — the same split {declaration_prefix} gives the discovery
522
+ # walks.
523
+ def declared_constant_path_prefix(path, qualified_prefix, unnameable_cref)
524
+ return Source::ConstantPath.qualified_name_or_nil(path)&.split("::") if Source::ConstantPath.rooted?(path)
525
+ return nil if unnameable_cref &&
526
+ (path.is_a?(Prism::ConstantReadNode) || self_anchored_tail(path))
527
+
528
+ Source::ConstantPath.declaration_prefix(qualified_prefix, path)
529
+ end
530
+
531
+ # The qualified name a `Path::CONST =` write declares — the write walk's
532
+ # own convention: `::`-rooted and `self::`-anchored paths resolve first,
533
+ # every other base files AS WRITTEN (`S2::C = 2` inside `class S` keys
534
+ # `S2::C`, matching {ScopeIndexer::ConstantTables.constant_path_write_key}'s as-written fallback
535
+ # rather than the header rule's lexical re-anchor).
536
+ def declared_constant_write_prefix(path, qualified_prefix, unnameable_cref)
537
+ return Source::ConstantPath.qualified_name_or_nil(path)&.split("::") if Source::ConstantPath.rooted?(path)
538
+
539
+ if (tail = self_anchored_tail(path))
540
+ return nil if unnameable_cref
541
+
542
+ return qualified_prefix + tail
543
+ end
544
+ Source::ConstantPath.qualified_name_or_nil(path)&.split("::")
545
+ end
546
+
547
+ # Every enclosing prefix of a declared name joins the oracle — a file
548
+ # that declares `S::A::B` necessarily has `S` and `S::A` to declare it
549
+ # under, so `A::B.class_eval` inside `class S` resolves `S::A::B`.
550
+ def add_declared_name(names, qualified_name)
551
+ segments = qualified_name.split("::")
552
+ segments.each_index { |i| names << segments.first(i + 1).join("::") }
553
+ end
554
+
555
+ # The constant segments under a `self::`-anchored path — `self::A::B` → `["A", "B"]` —
556
+ # or nil for every other base (`::`-rooted, constant, or dynamic).
557
+ def self_anchored_tail(node)
558
+ return nil unless node.is_a?(Prism::ConstantPathNode)
559
+
560
+ segments = []
561
+ current = node
562
+ while current.is_a?(Prism::ConstantPathNode)
563
+ segments.unshift(current.name.to_s)
564
+ current = current.parent
565
+ end
566
+ current.is_a?(Prism::SelfNode) ? segments : nil
567
+ end
568
+
569
+ # `Object`'s constants ARE the top-level constants — every discovery table keys them
570
+ # bare, the same collapse {ScopeIndexer::ConstantTables.self_write_name} applies — so a `self::`-anchored path
571
+ # rooted at `Object` drops the segment rather than filing under a name nothing else
572
+ # produces.
573
+ def collapse_object_owner(prefix)
574
+ prefix.first == OBJECT_OWNER ? prefix[1..] : prefix
575
+ end
576
+
577
+ # Whether an eval-family call's block rebinds `self` to the receiver spelled in source — false
578
+ # for the bare and `self`-receiver forms, whose block keeps the ENCLOSING self (including the
579
+ # singleton class inside `class << self`).
580
+ def eval_named_receiver?(node)
581
+ !(node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode))
582
+ end
583
+
584
+ EMPTY_NESTING = [].freeze
585
+
586
+ # The empty qualified prefix. Distinct from {EMPTY_NESTING} only in what it MEANS: the two are handed
587
+ # to different parameters.
588
+ EMPTY_PREFIX = [].freeze
589
+
590
+ # The four recognised class-creating rvalue shapes at constant-write position — the same set
591
+ # {#meta_new_block_body} recognises, so the cross-file table and the per-file block-as-method walk agree on what
592
+ # counts as a declaration.
593
+ def meta_new_constant_rvalue?(rvalue)
594
+ class_new_call?(rvalue) || module_new_call?(rvalue) ||
595
+ data_define_call?(rvalue) || struct_new_call?(rvalue)
596
+ end
597
+
598
+ # Whether a `class` / `module` header still names its declaration when it sits below an
599
+ # unnameable singleton cref. A BARE header (`class D`) lands on the singleton's own
600
+ # namespace — `#<singleton>::D` names nothing — and so does a `self::`-anchored or
601
+ # dynamic base. Every other path — `::T`, `C::D`, `Foo::Bar` — resolves its base
602
+ # lexically and stays nameable under the same approximation
603
+ # {Source::ConstantPath.declaration_prefix} uses everywhere else.
604
+ def decl_nameable_under_cref?(node)
605
+ path = node.constant_path
606
+ !path.is_a?(Prism::ConstantReadNode) &&
607
+ !Source::ConstantPath.qualified_name_or_nil(path).nil?
608
+ end
609
+
610
+ # Whether a class/module header's body walks under the unnameable-cref marker: the
611
+ # header opens `#<singleton>::Name` when the enclosing cref is unnameable and the
612
+ # header itself names nothing reachable — a bare `D`, or a `self::` path no
613
+ # rebound self covers. An EMPTY `self_decl` is the decline form (a `self::`
614
+ # header under a self nothing names); a non-empty one re-anchors the body at a
615
+ # real cref even below an unnameable enclosure.
616
+ def unnameable_decl?(node, self_decl, singleton_cref)
617
+ return true if self_decl && self_decl.empty?
618
+ return false if self_decl&.any?
619
+
620
+ singleton_cref && !decl_nameable_under_cref?(node)
621
+ end
622
+
623
+ # The context a `class`/`module` header gives its body: `[self_decl, child_prefix,
624
+ # child_cref]` — the `self::`-anchored prefix when the header rides a rebound
625
+ # self (nil otherwise), the body's qualified prefix, and the unnameable-cref
626
+ # marker. nil when the header renders no prefix, which a parsed header cannot
627
+ # but keeps every caller total.
628
+ def decl_body_context(node, qualified_prefix, self_base, singleton_cref)
629
+ self_decl = self_anchored_decl_prefix(node.constant_path, self_base)
630
+ child_prefix = self_decl ||
631
+ Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
632
+ return nil unless child_prefix
633
+
634
+ [self_decl, child_prefix, unnameable_decl?(node, self_decl, singleton_cref)]
635
+ end
636
+
637
+ # The qualified prefix a `self::`-anchored declaration or write target names when
638
+ # `self_base` names the REBOUND self — a `def_owner` override, a mixin `current_class`,
639
+ # or a meta-new class prefix. nil `self_base` means `self` is the lexical class — the
640
+ # lenient render of {Source::ConstantPath.declaration_prefix} already resolves it — and
641
+ # `[]` marks a self no name exists for (an anonymous or singleton block), where the
642
+ # path stays unnameable. nil return: not a `self::` path, or no override applies.
643
+ def self_anchored_decl_prefix(path, self_base)
644
+ return nil unless self_base && (tail = self_anchored_tail(path))
645
+ return [] if self_base.empty?
646
+
647
+ self_base + tail
648
+ end
649
+
650
+ # Whether a meta-new constant write's TARGET still names its class below an unnameable
651
+ # singleton cref — the path-write twin of {#decl_nameable_under_cref?}. `::K`, `C::K`,
652
+ # `Foo::F` resolve their base lexically; a bare `K =` / `K ||=` lands on the singleton's
653
+ # own constant table, and a `self::` or dynamic base names nothing reachable — unless
654
+ # `self_base` names a rebound self (`K = Class.new { self::X = … }` writes `K::X`).
655
+ def meta_new_path_target_nameable?(node, self_base = nil)
656
+ target =
657
+ case node
658
+ when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode then node.target
659
+ else return false
660
+ end
661
+ return self_base.any? if self_base && self_anchored_tail(target)
662
+
663
+ !Source::ConstantPath.qualified_name_or_nil(target).nil?
664
+ end
665
+
666
+ # Recognises `Data.define(*Symbol)` and `Data.define(*Symbol) do ... end` at constant-write rvalue position. The
667
+ # receiver MUST be the bare `Data` constant (or `::Data`); other receivers (a local variable, a method call
668
+ # return) are rejected because their identity is not statically known. Issue #2968 — a splatted member list
669
+ # (`Data.define(*FIELDS)`) still creates the class; {ScopeIndexer::SharedWalkTables.meta_members_open?} marks
670
+ # its members unknown.
671
+ def data_define_call?(node)
672
+ return false unless node.is_a?(Prism::CallNode)
673
+ return false unless node.name == :define
674
+ return false unless meta_constant_receiver?(node.receiver, :Data)
675
+
676
+ args = node.arguments&.arguments || []
677
+ args.all? { |arg| meta_member_argument?(arg) }
678
+ end
679
+
680
+ # Recognises `Struct.new(*Symbol)` and `Struct.new(*Symbol, keyword_init: <expr>)` at constant-write rvalue
681
+ # position. A trailing `KeywordHashNode` (the `keyword_init: ...` form) is accepted but does not contribute to
682
+ # member discovery; every other argument MUST be a `Prism::SymbolNode`. At least one Symbol member is required —
683
+ # `Struct.new()` is a degenerate form callers don't typically use.
684
+ #
685
+ # Issue #2968 — a leading String (`Struct.new("Named", :a)`, the class `Struct::Named`) and a splatted member
686
+ # list (`Struct.new(*FIELDS)`) create the class just the same.
687
+ # {ScopeIndexer::SharedWalkTables.meta_member_positionals} drops the String;
688
+ # {ScopeIndexer::SharedWalkTables.meta_members_open?} marks a splatted list's members unknown.
689
+ def struct_new_call?(node)
690
+ return false unless meta_call_with_name?(node, :Struct, :new)
691
+
692
+ args = node.arguments&.arguments || []
693
+ positional = struct_new_positionals(args)
694
+ return false if positional.nil? || positional.empty?
695
+
696
+ positional = positional.drop(1) if positional.first.is_a?(Prism::StringNode)
697
+ positional.all? { |arg| meta_member_argument?(arg) }
698
+ end
699
+
700
+ def meta_member_argument?(arg)
701
+ arg.is_a?(Prism::SymbolNode) || arg.is_a?(Prism::SplatNode)
702
+ end
703
+
704
+ # Recognises `Module.new` and `Module.new(&block)` / `Module.new do ... end` at constant-write rvalue position.
705
+ # The block body is the anonymous module's `module_eval` body; defs inside it bind methods on the named constant
706
+ # (`Const = Module.new do ...; def foo; ...; end; end`). Arguments are NOT inspected because `Module.new`
707
+ # accepts no positionals — Ruby raises ArgumentError if any are passed — so a malformed call falls through the
708
+ # walker without affecting analysis.
709
+ def module_new_call?(node)
710
+ meta_call_with_name?(node, :Module, :new)
711
+ end
712
+
713
+ # Recognises `Class.new`, `Class.new(super_class)`, and the block form `Class.new { ... }`. Like
714
+ # `module_new_call?`, the block body is walked as the anonymous class's body. The optional `super_class`
715
+ # positional is accepted but does NOT route through `ancestor` discovery in this slice — the synthesised class
716
+ # still answers method lookups via its own body's defs, mirroring how `Struct.new` / `Data.define` are handled.
717
+ def class_new_call?(node)
718
+ meta_call_with_name?(node, :Class, :new)
719
+ end
720
+
721
+ def meta_call_with_name?(node, receiver_name, method_name)
722
+ return false unless node.is_a?(Prism::CallNode)
723
+ return false unless node.name == method_name
724
+
725
+ meta_constant_receiver?(node.receiver, receiver_name)
726
+ end
727
+
728
+ def struct_new_positionals(args)
729
+ args.last.is_a?(Prism::KeywordHashNode) ? args[0..-2] : args
730
+ end
731
+
732
+ def meta_constant_receiver?(node, expected_name)
733
+ case node
734
+ when Prism::ConstantReadNode
735
+ node.name == expected_name
736
+ when Prism::ConstantPathNode
737
+ node.parent.nil? && node.name == expected_name
738
+ end
739
+ end
740
+
741
+ private_class_method :unwrap_or_guard, :freeze_tail?, :meta_constant_write_name, :scan_declared_names,
742
+ :collect_declared_constant_names, :collect_declared_class_decl,
743
+ :collect_declared_bare_write, :collect_declared_path_write,
744
+ :collect_declared_anonymous_factory?, :collect_rvalue_declared_names,
745
+ :declared_constant_path_prefix, :declared_constant_write_prefix, :add_declared_name,
746
+ :meta_member_argument?, :meta_constant_receiver?
747
+ end
748
+ end
749
+ end
750
+ end