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,823 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../source/constant_path"
6
+ require_relative "../../source/node_children"
7
+ require_relative "../anonymous_meta_class"
8
+ require_relative "../declaration_walk/traversal"
9
+ require_relative "../declaration_walk/rules"
10
+ require_relative "superclasses_collector"
11
+ require_relative "member_layouts_collector"
12
+ require_relative "def_nestings_collector"
13
+ require_relative "factory_call_resolver"
14
+ require_relative "constant_tables"
15
+
16
+ module Rigor
17
+ module Inference
18
+ module ScopeIndexer
19
+ # The tables ONE shared {DeclarationWalk} builds for a file (ADR-116 WD5), moved out of `scope_indexer.rb`
20
+ # whole (ADR-116 WD9, U4): the as-written superclass table with its header-nesting twin, the def-nesting
21
+ # table, and the `Data.define` and `Struct.new` member layouts. {.declaration_walk_tables} is the entry
22
+ # `scope_indexer.rb` calls, once per file on each of its two index paths: it runs {SuperclassesCollector},
23
+ # {MemberLayoutsCollector} and {DefNestingsCollector}, each a sibling file, in one descent.
24
+ #
25
+ # Seven more functions are public, because another file calls each. The collectors call the four recorders:
26
+ # {.record_declaration_ancestry} and {.record_anonymous_meta_superclass} the first,
27
+ # {.record_data_member_layout} and {.record_struct_member_layout} the second. {CvarHierarchy} asks
28
+ # {.recorded_ancestor_name} for a superclass as the table spells it. And `ScopeIndexer.record_meta_member_names`
29
+ # asks {.meta_member_names} and {.meta_members_open?} for the member list of a factory call: that reading
30
+ # moved here with the layout tables, which are its table, so that the module called back three functions and
31
+ # not five. The rest is private: a builder per table that runs its collector alone, for the specs
32
+ # ({.build_superclass_tables}, {.build_def_nestings}, {.build_data_member_layouts},
33
+ # {.build_struct_member_layouts}); the `verified_*` step that hands each table to `RIGOR_SHADOW_RULE_WALK`;
34
+ # and the walkers the collectors replaced, kept as that check's oracles ({.legacy_superclass_tables},
35
+ # {.legacy_def_nestings}, {.legacy_data_member_layouts}, {.legacy_struct_member_layouts} and their arms).
36
+ #
37
+ # It asks {DeclarationWalk::Rules} for each declaration context, and two sibling files it requires for what
38
+ # were the second and third functions it called back, until #3400 moved them: {FactoryCallResolver} for the
39
+ # factory call an expression unwinds to, and {ConstantTables} for which block rebinds `self`
40
+ # (`ConstantTables.rebound_block_self`, for the mixin names a header nesting is keyed by). It calls one
41
+ # function left in `scope_indexer.rb`: `ScopeIndexer.add_header_nesting` (the insert of the header-nesting
42
+ # table, which stays beside the merges the cross-file fold shares with it). `scope_indexer.rb` requires this
43
+ # file, and this file does not require it back. It reads `ScopeIndexer::EMPTY_NESTING`, `EMPTY_PREFIX`,
44
+ # `RECEIVER_EVAL_CALLS` and `MIXIN_CALL_NAMES` lexically, as {NodePropagation} reads the indexer's constants,
45
+ # and `ConstantTables.rebound_block_self` reads `RECEIVER_EVAL_CALLS` and `OPAQUE_SELF` the same way. So the
46
+ # file loads alone, and until `scope_indexer.rb` has loaded only the functions that reach none of those six
47
+ # names answer.
48
+ module SharedWalkTables # rubocop:disable Metrics/ModuleLength
49
+ module_function
50
+
51
+ # The literal Symbol member names of a `Data.define` / `Struct.new` call ({.meta_member_positionals}). A
52
+ # splatted argument contributes none; {.meta_members_open?} says the list is then incomplete.
53
+ def meta_member_names(call_node)
54
+ meta_member_positionals(call_node).filter_map { |arg| arg.unescaped.to_sym if arg.is_a?(Prism::SymbolNode) }
55
+ end
56
+
57
+ # Issue #2968 — whether a `Data.define` / `Struct.new` call's member list holds a splat, so its members are not
58
+ # all literal and the reader set is unknown. A layout table or a closed reader set must not be built from it.
59
+ def meta_members_open?(call_node)
60
+ meta_member_positionals(call_node).any?(Prism::SplatNode)
61
+ end
62
+
63
+ # The member arguments of a `Data.define` / `Struct.new` call. For `Struct.new` the trailing `keyword_init:`
64
+ # hash is stripped ({DeclarationWalk::Rules.struct_new_positionals}). A leading class-name String
65
+ # (`Struct.new("Named", :a)`) is neither a Symbol nor a splat, so it is no member and does not open the list.
66
+ def meta_member_positionals(call_node)
67
+ raw = call_node.arguments&.arguments || []
68
+ DeclarationWalk::Rules.struct_new_call?(call_node) ? DeclarationWalk::Rules.struct_new_positionals(raw) : raw
69
+ end
70
+
71
+ # Issue #681 — `{Prism::DefNode => Module.nesting}` for every `def` the file declares inside a class or
72
+ # module body, keyed by node IDENTITY so the answer follows the DECLARATION rather than the class a
73
+ # call happens to dispatch on. `Inference::ExpressionTyper#build_user_method_body_scope` rebuilds a
74
+ # callee's body scope from the RECEIVER'S TYPE alone when it needs that callee's return, so unlike the
75
+ # census scopes (#692) there is no prefix in hand to stamp: recording the chain where the declaration is
76
+ # indexed is what makes the re-walk and the declaration walk agree BY CONSTRUCTION, which is the
77
+ # argument #685 used to make {Reflection.lexical_nesting_chain} the single owner.
78
+ #
79
+ # Keyed on the def node and not on `(class, method)` because an inherited body is re-walked with the
80
+ # SUBCLASS as receiver: `class Admin::CompactBase` declares `make`, `module Admin; class Child <
81
+ # CompactBase` calls it, and the bare `Post` inside `make` names `::Post` — the constant the compact
82
+ # declaration that OWNS the body reaches, not the one the receiver's spelling would suggest.
83
+ #
84
+ # Not a leaf of the fused methods/def-nodes walk: it needs the CHAIN threaded, which the fused walk does
85
+ # not carry (a singleton-class body and a `Class.new` block body both push a qualified prefix while
86
+ # pushing no `Module.nesting` entry). {DefNestingsCollector} declines at every `Prism::DefNode`, so a
87
+ # def-dense file pays only the declaration spine.
88
+ #
89
+ # ADR-116 WD5 — production builds this table in {.declaration_walk_tables}' shared run, and nothing in
90
+ # `lib` calls this builder. It runs the collector alone and stays for the specs, which compare it with
91
+ # the shared run; `RIGOR_SHADOW_RULE_WALK` checks it against {.legacy_def_nestings}.
92
+ def build_def_nestings(root)
93
+ collector = DefNestingsCollector.new
94
+ DeclarationWalk.run(root, [collector], superclass_walk_root(nil))
95
+ verified_def_nestings(root, collector.table)
96
+ end
97
+
98
+ def verified_def_nestings(root, table, source_path = nil)
99
+ DeclarationWalk::Shadow.verified(:def_nestings, source_path, table) { legacy_def_nestings(root) }
100
+ end
101
+
102
+ # The walker {DefNestingsCollector} replaced, kept as the shadow harness's oracle until ADR-116 WD5 has
103
+ # ported every table walker onto {DeclarationWalk}.
104
+ def legacy_def_nestings(root)
105
+ accumulator = {}.compare_by_identity
106
+ walk_def_nestings(root, EMPTY_NESTING, accumulator)
107
+ accumulator.freeze
108
+ end
109
+
110
+ # Records `nesting` for every `def` reachable from `node`. Only a `class` / `module` keyword pushes an
111
+ # entry (qualified against the entry already on top, so a compact `class Admin::X` contributes ONE);
112
+ # every other body — a singleton class, a `Class.new` / `Module.new` block, any other block — inherits
113
+ # the chain unchanged, because Ruby pushes no cref for them.
114
+ #
115
+ # Issue #716 — a top-level `def` records the EMPTY chain, and that is a recorded answer rather than the
116
+ # absence of one. Ruby's `Module.nesting` in a top-level body IS `[]`, so `def helper = Post.new` names
117
+ # `::Post` wherever it is later called from; recording nothing left
118
+ # {Reflection.lexical_nesting_chain} peeling the CALLER's qualified name, which answered `Admin::Post`
119
+ # for a call made from inside `module Admin`. The table is keyed by node identity and populated only by
120
+ # this walk, so "present with `[]`" (walked, and top level) stays distinguishable from "absent" (no
121
+ # declaration walk built this scope — a plugin-constructed scope, an anonymous `Class.new` re-entered
122
+ # through `Scope#evaluate`), which keeps the peel where it is still the only available answer.
123
+ # `self_base` names a rebound `self` for `self::`-anchored headers — a meta-new
124
+ # block's class; `[]` marks a self no name covers.
125
+ def walk_def_nestings(node, nesting, accumulator, self_base: nil, singleton_cref: false)
126
+ return unless node.is_a?(Prism::Node)
127
+
128
+ case node
129
+ when Prism::ClassNode, Prism::ModuleNode
130
+ return walk_def_nesting_declaration(node, nesting, accumulator, self_base,
131
+ singleton_cref)
132
+ when Prism::SingletonClassNode
133
+ # The expression evaluates in the enclosing cref; the body's cref is unnameable —
134
+ # and `self` below it is the singleton class, which `self::` headers decline on.
135
+ walk_def_nestings(node.expression, nesting, accumulator,
136
+ self_base: self_base, singleton_cref: singleton_cref)
137
+ if node.body
138
+ walk_def_nestings(node.body, nesting, accumulator,
139
+ self_base: EMPTY_PREFIX, singleton_cref: true)
140
+ end
141
+ return
142
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
143
+ Prism::ConstantPathOrWriteNode
144
+ return if walk_def_nesting_meta_new?(node, nesting, accumulator, self_base,
145
+ singleton_cref)
146
+ when Prism::DefNode
147
+ accumulator[node] = nesting unless nesting.nil?
148
+ return
149
+ when Prism::CallNode
150
+ return if walk_def_nesting_eval?(node, nesting, accumulator, self_base, singleton_cref)
151
+ end
152
+
153
+ node.rigor_each_child do |child|
154
+ walk_def_nestings(child, nesting, accumulator,
155
+ self_base: self_base, singleton_cref: singleton_cref)
156
+ end
157
+ end
158
+
159
+ # The class/module arm of {.walk_def_nestings}: under an unnameable cref a
160
+ # bare/`self::` header pushes `#<singleton>::Name` — a rung nothing can spell — so
161
+ # the chain keeps what it had rather than filing a fabricated `C::D`; nameable
162
+ # headers re-anchor at a real cref.
163
+ def walk_def_nesting_declaration(node, nesting, accumulator, self_base, singleton_cref)
164
+ return unless node.body
165
+
166
+ self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, self_base)
167
+ child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
168
+ child_nesting =
169
+ if child_cref
170
+ nesting
171
+ elsif self_decl
172
+ [self_decl.join("::"), *nesting].freeze
173
+ else
174
+ Source::ConstantPath.pushed_nesting(nesting, node.constant_path)
175
+ end
176
+ walk_def_nestings(node.body, child_nesting, accumulator, singleton_cref: child_cref)
177
+ end
178
+
179
+ # The meta-new arm of {.walk_def_nestings}: the block rebinds only `self` — a
180
+ # `self::` header inside anchors on the class the write names (`K::SX` below
181
+ # `K = Class.new`), while `Module.nesting`, the table's payload, stays lexical.
182
+ def walk_def_nesting_meta_new?(node, nesting, accumulator, self_base, singleton_cref)
183
+ split = DeclarationWalk::Rules.meta_new_block_split(node, nesting_lexical_prefix(nesting), self_base,
184
+ singleton_cref)
185
+ return false unless split
186
+
187
+ enclosing, body, body_self = split
188
+ enclosing.each do |part|
189
+ walk_def_nestings(part, nesting, accumulator, self_base: self_base,
190
+ singleton_cref: singleton_cref)
191
+ end
192
+ if body
193
+ walk_def_nestings(body, nesting, accumulator, self_base: body_self,
194
+ singleton_cref: singleton_cref)
195
+ end
196
+ true
197
+ end
198
+
199
+ # The eval-family arm of {.walk_def_nestings}: `self` rebinds to the receiver for
200
+ # `self::` headers while `Module.nesting` stays lexical. `instance_eval` belongs
201
+ # here too — it moves `self` for constant anchoring even though its `def`s bind
202
+ # on the receiver's singleton.
203
+ def walk_def_nesting_eval?(node, nesting, accumulator, self_base, singleton_cref)
204
+ return false unless node.block.is_a?(Prism::BlockNode) &&
205
+ RECEIVER_EVAL_CALLS.include?(node.name)
206
+
207
+ [node.receiver, *node.arguments&.arguments.to_a].compact.each do |part|
208
+ walk_def_nestings(part, nesting, accumulator, self_base: self_base,
209
+ singleton_cref: singleton_cref)
210
+ end
211
+ lexical = nesting_lexical_prefix(nesting)
212
+ self_prefix = self_base || lexical
213
+ unnameable = DeclarationWalk::Rules.unnameable_eval_self?(false, self_base, lexical, singleton_cref)
214
+ eval_self = DeclarationWalk::Rules.eval_receiver_prefix(node, self_prefix, lexical,
215
+ unnameable_self: unnameable) || []
216
+ if (body = node.block.body)
217
+ walk_def_nestings(body, nesting, accumulator, self_base: eval_self,
218
+ singleton_cref: singleton_cref)
219
+ end
220
+ true
221
+ end
222
+
223
+ # The innermost lexical cref a def-nesting rung list names, as a prefix array — the
224
+ # shape {DeclarationWalk::Rules.meta_new_child_prefix} and {DeclarationWalk::Rules.eval_receiver_prefix} take.
225
+ def nesting_lexical_prefix(nesting)
226
+ nesting&.first ? nesting.first.split("::") : []
227
+ end
228
+
229
+ # ADR-24 slice 2 — per-class table mapping a fully qualified user class to its superclass name AS WRITTEN at the
230
+ # `class Foo < Bar` declaration. Only constant superclasses are recorded (`class Foo < Struct.new(...)` and
231
+ # other non-constant superclasses produce no entry). The as-written name is resolved to a qualified class at the
232
+ # call site against the nesting the declaration's HEADER sits in — see {Scope#ancestor_name_candidates}, which
233
+ # reads the second table this walk builds.
234
+ #
235
+ # Issue #682 — `header_nestings` maps the same qualified class name to that nesting: `Module.nesting` where the
236
+ # `class` / `module` KEYWORD is written, which is the body's chain minus the declaration's own entry. It is
237
+ # recorded here, with the walk's prefix in hand, because it is unrecoverable afterwards: `class Admin::Widget`
238
+ # and `module Admin; class Widget` render the identical `"Admin::Widget"`, and an ancestor name is resolved in
239
+ # the enclosing cref — `[]` for the first spelling, `["Admin"]` for the second. A class reopened under two
240
+ # spellings keeps the LAST one walked, matching how `superclasses` itself merges.
241
+ #
242
+ # ADR-116 WD5 — built by {SuperclassesCollector} on the shared {DeclarationWalk}, following the two
243
+ # legacy variants it declares; `RIGOR_SHADOW_RULE_WALK` checks both tables against
244
+ # {.legacy_superclass_tables}. Production builds both in {.declaration_walk_tables}' shared run, and
245
+ # nothing in `lib` calls this builder: it runs the collector alone and stays for the specs, which
246
+ # compare it with the shared run.
247
+ #
248
+ # @return the `[superclasses, header_nestings]` pair
249
+ def build_superclass_tables(root, source_path = nil)
250
+ collector = SuperclassesCollector.new
251
+ DeclarationWalk.run(root, [collector], superclass_walk_root(source_path))
252
+ verified_superclass_tables(root, source_path, collector.tables)
253
+ end
254
+
255
+ # The root context the superclass tables are walked from: the ancestry nesting starts empty and the
256
+ # anonymous-class key carries the file's path.
257
+ def superclass_walk_root(source_path, scope: nil)
258
+ DeclarationWalk::Context.root(scope: scope, nesting: EMPTY_NESTING, source_path: source_path)
259
+ end
260
+
261
+ # `tables` once the shadow harness has compared them with the legacy walker's, when it is on.
262
+ def verified_superclass_tables(root, source_path, tables)
263
+ DeclarationWalk::Shadow.verified(:superclass_tables, source_path, tables) do
264
+ legacy_superclass_tables(root, source_path)
265
+ end
266
+ end
267
+
268
+ # The walker {SuperclassesCollector} replaced, kept as the shadow harness's oracle until ADR-116 WD5 has
269
+ # ported every table walker onto {DeclarationWalk}.
270
+ def legacy_superclass_tables(root, source_path = nil)
271
+ accumulator = { superclasses: {}, header_nestings: {} }
272
+ walk_class_superclasses(root, [], accumulator, source_path)
273
+ [accumulator[:superclasses].freeze, accumulator[:header_nestings].freeze]
274
+ end
275
+
276
+ # ADR-116 WD5 — the file's superclass, header-nesting, def-nesting and member-layout tables from ONE
277
+ # shared {DeclarationWalk}: the file is descended once for all of them, where the legacy walkers
278
+ # descended it once per table. `RIGOR_SHADOW_RULE_WALK` checks each table against its legacy walker.
279
+ #
280
+ # @return `{ superclasses:, header_nestings:, def_nestings:, data_member_layouts:, struct_member_layouts: }`
281
+ def declaration_walk_tables(root, source_path = nil)
282
+ superclasses = SuperclassesCollector.new
283
+ layouts = MemberLayoutsCollector.new
284
+ def_nestings = DefNestingsCollector.new
285
+ DeclarationWalk.run(root, [superclasses, layouts, def_nestings], superclass_walk_root(source_path))
286
+ supers, header_nestings = verified_superclass_tables(root, source_path, superclasses.tables)
287
+ data, struct = verified_member_layouts(root, layouts.tables, source_path)
288
+ { superclasses: supers, header_nestings: header_nestings,
289
+ def_nestings: verified_def_nestings(root, def_nestings.table, source_path),
290
+ data_member_layouts: data, struct_member_layouts: struct }
291
+ end
292
+
293
+ # `self_base` names a rebound `self` for `self::`-anchored headers — a meta-new
294
+ # block's class; `[]` marks a self no name covers.
295
+ def walk_class_superclasses(node, qualified_prefix, accumulator, source_path = nil,
296
+ nesting = EMPTY_NESTING, self_base: nil, singleton_cref: false)
297
+ return unless node.is_a?(Prism::Node)
298
+
299
+ case node
300
+ when Prism::CallNode
301
+ return if walk_superclass_eval_call?(node, qualified_prefix, accumulator, source_path,
302
+ nesting, self_base, singleton_cref)
303
+
304
+ record_anonymous_meta_superclass(node, accumulator[:superclasses], source_path)
305
+ when Prism::ClassNode, Prism::ModuleNode
306
+ return if walk_superclass_declaration?(node, qualified_prefix, accumulator, nesting,
307
+ self_base, singleton_cref)
308
+ when Prism::SingletonClassNode
309
+ # The expression evaluates in the enclosing cref; the body's cref is unnameable.
310
+ return walk_singleton_superclasses(node, qualified_prefix, accumulator, source_path,
311
+ nesting, self_base, singleton_cref)
312
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode,
313
+ Prism::ConstantOrWriteNode, Prism::ConstantPathOrWriteNode
314
+ return if walk_superclass_meta_new?(node, qualified_prefix, accumulator, source_path,
315
+ nesting, self_base, singleton_cref)
316
+ end
317
+
318
+ node.rigor_each_child do |child|
319
+ walk_class_superclasses(child, qualified_prefix, accumulator, source_path, nesting,
320
+ self_base: self_base, singleton_cref: singleton_cref)
321
+ end
322
+ end
323
+
324
+ # The eval-family arm of {.walk_class_superclasses}: `self::` headers anchor on the
325
+ # receiver — `X.class_eval { class self::D < S }` files `X::D`'s ancestry — while bare
326
+ # headers and the nesting rungs stay lexical.
327
+ def walk_superclass_eval_call?(node, qualified_prefix, accumulator, source_path, nesting,
328
+ self_base, singleton_cref)
329
+ split = DeclarationWalk::Rules.eval_block_split(node, qualified_prefix, self_base, singleton_cref)
330
+ return false unless split
331
+
332
+ enclosing, body, body_self = split
333
+ enclosing.each do |part|
334
+ walk_class_superclasses(part, qualified_prefix, accumulator, source_path, nesting,
335
+ self_base: self_base, singleton_cref: singleton_cref)
336
+ end
337
+ if body
338
+ walk_class_superclasses(body, qualified_prefix, accumulator, nil, nesting,
339
+ self_base: body_self, singleton_cref: singleton_cref)
340
+ end
341
+ true
342
+ end
343
+
344
+ # The `K = Class.new { … }` arm of {.walk_class_superclasses}: the block's `self` is the
345
+ # class the write names; its declarations stay lexical.
346
+ def walk_superclass_meta_new?(node, qualified_prefix, accumulator, source_path, nesting,
347
+ self_base, singleton_cref)
348
+ split = DeclarationWalk::Rules.meta_new_block_split(node, qualified_prefix, self_base, singleton_cref)
349
+ return false unless split
350
+
351
+ enclosing, body, body_self = split
352
+ enclosing.each do |part|
353
+ walk_class_superclasses(part, qualified_prefix, accumulator, source_path, nesting,
354
+ self_base: self_base, singleton_cref: singleton_cref)
355
+ end
356
+ if body
357
+ walk_class_superclasses(body, qualified_prefix, accumulator, nil, nesting,
358
+ self_base: body_self, singleton_cref: singleton_cref)
359
+ end
360
+ true
361
+ end
362
+
363
+ # The class/module arm of {.walk_class_superclasses}: under an unnameable cref a
364
+ # bare/`self::` header opens `#<singleton>::Name` — ancestry/nesting facts for it
365
+ # would publish under a `C::D` MRI never creates, so they are skipped and the body
366
+ # walks ownerless; nameable headers re-anchor at a real cref.
367
+ def walk_superclass_declaration?(node, qualified_prefix, accumulator, nesting,
368
+ self_base, singleton_cref)
369
+ ctx = DeclarationWalk::Rules.decl_body_context(node, qualified_prefix, self_base, singleton_cref)
370
+ return false unless ctx
371
+
372
+ self_decl, child_prefix, child_cref = ctx
373
+ record_declaration_ancestry(node, nesting, child_prefix, accumulator) unless child_cref
374
+ child_nesting =
375
+ if child_cref
376
+ nesting
377
+ elsif self_decl
378
+ [self_decl.join("::"), *nesting].freeze
379
+ else
380
+ Source::ConstantPath.pushed_nesting(nesting, node.constant_path) || nesting
381
+ end
382
+ return true unless node.body
383
+
384
+ walk_class_superclasses(node.body, child_cref ? [] : child_prefix, accumulator, nil,
385
+ child_nesting, singleton_cref: child_cref)
386
+ true
387
+ end
388
+
389
+ def walk_singleton_superclasses(node, qualified_prefix, accumulator, source_path, nesting,
390
+ self_base, singleton_cref)
391
+ walk_class_superclasses(node.expression, qualified_prefix, accumulator, source_path,
392
+ nesting, self_base: self_base, singleton_cref: singleton_cref)
393
+ return unless node.body
394
+
395
+ # `self` below `class <<` is the singleton class — `self::` headers decline.
396
+ walk_class_superclasses(node.body, qualified_prefix, accumulator, source_path,
397
+ nesting, self_base: EMPTY_PREFIX, singleton_cref: true)
398
+ end
399
+
400
+ # One declaration's two ancestry facts: the as-written superclass name (classes only), and the
401
+ # `Module.nesting` its header is written in. `nesting` is the chain in force OUTSIDE the header — the
402
+ # header is evaluated before the body is entered, so the declaration's own entry is not on the ladder
403
+ # its superclass name walks — which is why the walk pushes only when it recurses into the body. The
404
+ # chain is threaded rather than derived from the enclosing prefix because a rooted header resets that
405
+ # prefix without resetting Ruby's nesting ([#708](https://github.com/rigortype/rigor/issues/708)).
406
+ def record_declaration_ancestry(node, nesting, child_prefix, accumulator)
407
+ full = child_prefix.join("::")
408
+ names = declared_ancestor_names(node)
409
+ ScopeIndexer.add_header_nesting(accumulator[:header_nestings], full, nesting, names) if names
410
+ return unless node.is_a?(Prism::ClassNode)
411
+
412
+ accumulator[:superclasses][full] = recorded_ancestor_name(node.superclass) if node.superclass
413
+ end
414
+
415
+ # Issue #722 residue 1 / #637 — the recorded ancestor name, carrying the ROOTED marker when the site
416
+ # wrote one. `Source::ConstantPath.qualified_name` renders segments only, so `class Rooted < ::Base`
417
+ # recorded the same `"Base"` a bare `< Base` does, and the resolver then walked the enclosing nesting
418
+ # and answered `A::Base` — a class Ruby never looks at, on a spelling whose whole point is that it
419
+ # does not.
420
+ #
421
+ # The marker rides the existing table's VALUE rather than a new table. It is still a String, so the
422
+ # per-file index, the cross-file fold and the ADR-85 seed bundle carry it unchanged; the three
423
+ # resolvers that read it strip and anchor. `IncrementalSnapshot::SCHEMA` is bumped anyway, because an
424
+ # older blob's un-rooted value is indistinguishable from "not rooted" and a warm run would keep the
425
+ # pre-fix answer for every unchanged file.
426
+ def recorded_ancestor_name(node)
427
+ name = Source::ConstantPath.qualified_name(node)
428
+ return name if name.nil? || !Source::ConstantPath.rooted?(node)
429
+
430
+ "::#{name}"
431
+ end
432
+
433
+ # The ancestor names this declaration SITE writes — the superclass in its header, and every mixin
434
+ # argument in its own body — or nil when the site writes none. Only a site that writes one
435
+ # contributes a header nesting, and the names are what key it: Ruby resolves each ancestor name in
436
+ # the cref of the site that WROTE it, and a class's sites need not agree on that cref.
437
+ #
438
+ # Issue #708's review: a per-CLASS union of every site's chain is wrong for a rooted reopen inside
439
+ # another namespace. `class ::Foo; def extra = 1; end` written in `module Outer` contributed `["Outer"]`
440
+ # to `Foo`, and the ancestor rung then resolved `Foo`'s superclass `Base` as `Outer::Base` — a class
441
+ # Ruby never looks at, ahead of the right one, on a site that named no ancestor at all. A site that
442
+ # writes no ancestor name has no ancestor for its cref to govern, so it has nothing to say here.
443
+ #
444
+ # Issue #728: dropping those sites was necessary and not sufficient. When the rooted reopen genuinely
445
+ # names a mixin — `class ::Foo; include Helper; end` in `module Outer` — the site is recorded, and a
446
+ # per-class union then hands its `["Outer"]` to `Foo`'s superclass `Base` as well, which is the same
447
+ # `Outer::Base` on the same repro. Keying by the written name is what separates them: `Base` resolves
448
+ # in the empty chain of the top-level site that wrote `< Base`, `Helper` in the `["Outer"]` chain of
449
+ # the site that wrote `include Helper`, which is what Ruby does for each independently.
450
+ #
451
+ # This keeps what the union was FOR. It was added because last-writer-wins let one rails TEST file
452
+ # reopening `ActiveRecord::Relation` compactly beat the library declaration, costing the class all nine
453
+ # of its `include`d modules; that reopen writes no ancestor name either, so it contributes nothing
454
+ # rather than winning — the protection comes from the recording rule instead of from a merge rule.
455
+ #
456
+ # The mixin scan counts only a call whose `self` is the declaration itself, so it stops at every construct that
457
+ # rebinds `self`: a nested `class` / `module` (that body's calls belong to the nested class), a `class << self`
458
+ # body, and a block `ConstantTables.rebound_block_self` classifies as rebinding — the `Class.new` / `Module.new`
459
+ # / `Struct.new` / `Data.define` and `class_eval` family. An `include` written in any of those attaches to
460
+ # something other than this class, so treating it as this site's ancestor name reinstates exactly the defect
461
+ # above with a different spelling. {MixinTables.walk_class_includes} classifies the same shapes the same way
462
+ # since [#749](https://github.com/rigortype/rigor/pull/749), so the two walks agree on which site owns a mixin.
463
+ #
464
+ # It stays structural rather than exhaustive: `self.include M` and `send(:include, M)` are recorded by neither
465
+ # walk, so the class's ancestry agrees. `Recv.class_eval { include M }` is the one shape where they diverge in
466
+ # the surviving direction — the includes walk names `Recv`, this one cannot see the site from `Recv`'s own
467
+ # declaration — and that is what {ScopeIndexer.add_header_nesting}'s unkeyed entry answers.
468
+ def declared_ancestor_names(node)
469
+ names = []
470
+ superclass = node.is_a?(Prism::ClassNode) ? node.superclass : nil
471
+ if superclass
472
+ recorded = recorded_ancestor_name(superclass)
473
+ names << recorded if recorded
474
+ end
475
+ body = node.body
476
+ mixin = body ? collect_mixin_names(body, names) : false
477
+ return names if superclass || mixin
478
+
479
+ nil
480
+ end
481
+
482
+ # Appends each mixin argument this body names to `names`, and answers whether it writes a mixin call at all — a
483
+ # `include some_method` writes one while naming nothing renderable, and the site still owns a chain for the
484
+ # names it does write. Arguments are rendered exactly as {MixinTables.record_mixin_call} renders them, so a
485
+ # name recorded here is the same String the includes table stores and the resolver asks for.
486
+ def collect_mixin_names(node, names)
487
+ return false unless node.is_a?(Prism::Node)
488
+ return false if node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode) ||
489
+ node.is_a?(Prism::SingletonClassNode)
490
+
491
+ found = mixin_call?(node)
492
+ record_mixin_names(node, names) if found
493
+ rebinds = ConstantTables.rebound_block_self(node, EMPTY_PREFIX)
494
+ node.rigor_each_child do |child|
495
+ next if rebinds && child.is_a?(Prism::BlockNode)
496
+
497
+ found = true if collect_mixin_names(child, names)
498
+ end
499
+ found
500
+ end
501
+
502
+ def mixin_call?(node)
503
+ node.is_a?(Prism::CallNode) && node.receiver.nil? && MIXIN_CALL_NAMES.include?(node.name)
504
+ end
505
+
506
+ def record_mixin_names(node, names)
507
+ node.arguments&.arguments&.each do |arg|
508
+ mod = Source::ConstantPath.qualified_name(arg)
509
+ names << mod if mod
510
+ end
511
+ end
512
+
513
+ # #319 — `Class.new(Parent) do ... end` names its superclass in the first positional. Recording it under the
514
+ # call site's anonymous name keeps `Parent`'s surface reachable from the block body (whose `self_type` is now
515
+ # `Singleton[<anonymous>]`) and from an instance of the resulting class, so giving the anonymous class an
516
+ # identity does not cost the inheritance the old `Singleton[Parent]` answer carried for free.
517
+ def record_anonymous_meta_superclass(call_node, accumulator, source_path)
518
+ return unless AnonymousMetaClass.block_form_receiver(call_node) == :Class
519
+
520
+ arg = call_node.arguments&.arguments&.first
521
+ return if arg.nil?
522
+
523
+ # Issue #722 residue 1 — deliberately NOT marked rooted here, unlike the `class X < ::Parent`
524
+ # header. Marking it changed nothing observable: `Made = Class.new(::Base)` inside a namespace that
525
+ # shadows `Base` still resolved the shadow, so the anonymous class's ancestry is reached by a path
526
+ # that does not read this value, and shipping a marker no resolver honours would be a change with
527
+ # no gate. The residue stays on #722 with that finding attached.
528
+ superclass = Source::ConstantPath.qualified_name(arg)
529
+ return if superclass.nil?
530
+
531
+ name = AnonymousMetaClass.name_for(call_node, source_path)
532
+ accumulator[name] = superclass if name
533
+ end
534
+
535
+ # ADR-48 — per qualified class name -> ordered `Data.define` member-name list, for both the named-subclass form
536
+ # (`class Point < Data.define(:x, :y)`) and the constant-assigned form (`Point = Data.define(:x, :y)`). Only
537
+ # `Data.define` is recorded: `Struct.new` instances are mutable, so member-value folding would be unsound
538
+ # (the Struct sibling table is {.build_struct_member_layouts}, ADR-48 § "Struct follow-up"). Consumed by
539
+ # {Inference::MethodDispatcher::DataFolding} via {Scope#data_member_layout}.
540
+ #
541
+ # ADR-116 WD5 — built with its Struct sibling by {MemberLayoutsCollector}. Production builds both in
542
+ # {.declaration_walk_tables}' shared run, and nothing in `lib` calls this builder: it stays for the specs,
543
+ # and walks both layout tables to return this one.
544
+ def build_data_member_layouts(root)
545
+ member_layout_tables(root).first
546
+ end
547
+
548
+ # `[data_member_layouts, struct_member_layouts]` from a {MemberLayoutsCollector} run of its own, checked by
549
+ # `RIGOR_SHADOW_RULE_WALK` against the two legacy walkers. Spec-only, like the two builders on it.
550
+ def member_layout_tables(root)
551
+ collector = MemberLayoutsCollector.new
552
+ DeclarationWalk.run(root, [collector], superclass_walk_root(nil))
553
+ verified_member_layouts(root, collector.tables)
554
+ end
555
+
556
+ def verified_member_layouts(root, tables, source_path = nil)
557
+ DeclarationWalk::Shadow.verified(:member_layouts, source_path, tables) do
558
+ [legacy_data_member_layouts(root), legacy_struct_member_layouts(root)]
559
+ end
560
+ end
561
+
562
+ # The walker {MemberLayoutsCollector} replaced for the `Data.define` table, kept as the shadow harness's
563
+ # oracle until ADR-116 WD5 has ported every table walker onto {DeclarationWalk}.
564
+ def legacy_data_member_layouts(root)
565
+ accumulator = {}
566
+ walk_data_member_layouts(root, [], accumulator)
567
+ accumulator.freeze
568
+ end
569
+
570
+ # `self_base` names a rebound `self` for `self::`-anchored declarations and write
571
+ # targets — a meta-new block's class; `[]` marks a self no name covers.
572
+ def walk_data_member_layouts(node, qualified_prefix, accumulator, self_base: nil,
573
+ singleton_cref: false)
574
+ return unless node.is_a?(Prism::Node)
575
+
576
+ case node
577
+ when Prism::ClassNode, Prism::ModuleNode
578
+ return walk_layout_declaration(node, qualified_prefix, accumulator, self_base,
579
+ singleton_cref, :data)
580
+ when Prism::SingletonClassNode
581
+ # The expression evaluates in the enclosing cref; the body's cref is unnameable.
582
+ return walk_singleton_member_layouts(node, qualified_prefix, accumulator,
583
+ :walk_data_member_layouts, self_base,
584
+ singleton_cref)
585
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
586
+ Prism::ConstantPathOrWriteNode
587
+ # A meta-new constant write under an unnameable cref lands on the singleton's own
588
+ # table unless it is a path write — declining a bare target rather than keying
589
+ # the block's class by the lexical prefix.
590
+ unless singleton_cref && !DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_base)
591
+ child_prefix = DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_base)
592
+ end
593
+ if child_prefix
594
+ record_data_member_layout(accumulator, child_prefix, DeclarationWalk::Rules.meta_new_rvalue(node))
595
+ end
596
+ return if walk_data_layout_meta_new?(node, qualified_prefix, accumulator,
597
+ self_base, singleton_cref)
598
+ when Prism::CallNode
599
+ return if walk_member_layout_eval_call?(node, qualified_prefix, accumulator,
600
+ self_base, singleton_cref,
601
+ :walk_data_member_layouts)
602
+ end
603
+
604
+ node.rigor_each_child do |child|
605
+ walk_data_member_layouts(child, qualified_prefix, accumulator,
606
+ self_base: self_base, singleton_cref: singleton_cref)
607
+ end
608
+ end
609
+
610
+ # The `K = Data.define { … }`-family arm of {.walk_data_member_layouts}: the
611
+ # factory call's receiver and arguments keep the enclosing context; the block's
612
+ # `self` is the class the write names.
613
+ def walk_data_layout_meta_new?(node, qualified_prefix, accumulator, self_base,
614
+ singleton_cref)
615
+ split = DeclarationWalk::Rules.meta_new_block_split(node, qualified_prefix, self_base, singleton_cref)
616
+ return false unless split
617
+
618
+ enclosing, body, body_self = split
619
+ enclosing.each do |part|
620
+ walk_data_member_layouts(part, qualified_prefix, accumulator,
621
+ self_base: self_base, singleton_cref: singleton_cref)
622
+ end
623
+ if body
624
+ walk_data_member_layouts(body, qualified_prefix, accumulator,
625
+ self_base: body_self, singleton_cref: singleton_cref)
626
+ end
627
+ true
628
+ end
629
+
630
+ # The class/module-declaration arm shared by the member-layout walks: a `class` header can
631
+ # itself be a `Data.define`/`Struct.new` subclass, and under an unnameable cref a bare/`self::`
632
+ # header opens `#<singleton>::Name` — the record is skipped and the body walks ownerless;
633
+ # nameable headers re-anchor at a real cref.
634
+ def walk_layout_declaration(node, qualified_prefix, accumulator, self_base,
635
+ singleton_cref, kind)
636
+ self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, self_base)
637
+ child_prefix = self_decl ||
638
+ Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
639
+ return unless child_prefix
640
+
641
+ child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
642
+ if node.is_a?(Prism::ClassNode) && !child_cref
643
+ record = kind == :data ? :record_data_member_layout : :record_struct_member_layout
644
+ send(record, accumulator, child_prefix, node.superclass, allow_outer_block: false)
645
+ end
646
+ return unless node.body
647
+
648
+ walk = kind == :data ? :walk_data_member_layouts : :walk_struct_member_layouts
649
+ send(walk, node.body, child_cref ? [] : child_prefix, accumulator,
650
+ singleton_cref: child_cref)
651
+ end
652
+
653
+ # Records `qualified -> [members]` when `expr` is a `Data.define(*Symbol)` call with at least one literal-Symbol
654
+ # member.
655
+ def record_data_member_layout(accumulator, qualified_parts, expr, allow_outer_block: true)
656
+ expr = FactoryCallResolver.resolve_meta_factory_call(expr, allow_outer_block: allow_outer_block)
657
+ return unless expr && DeclarationWalk::Rules.data_define_call?(expr)
658
+ return if meta_members_open?(expr)
659
+
660
+ members = meta_member_names(expr)
661
+ return if members.empty?
662
+
663
+ accumulator[qualified_parts.join("::")] = members.freeze
664
+ end
665
+
666
+ # ADR-48 Struct follow-up — the `Struct.new(...)` sibling of {.build_data_member_layouts}. A separate, additive
667
+ # table so the existing `Data.define` value-shape contract (a bare `[Symbol]`) is untouched: a Struct entry
668
+ # carries `{ members:, keyword_init: }` because the dispatcher needs the flag to fold the matching `.new` call
669
+ # form (positional vs keyword) without manufacturing a wrong map.
670
+ #
671
+ # ADR-116 WD5 — production builds it in {.declaration_walk_tables}' shared run; this builder is spec-only,
672
+ # like {.build_data_member_layouts}.
673
+ def build_struct_member_layouts(root)
674
+ member_layout_tables(root).last
675
+ end
676
+
677
+ # The walker {MemberLayoutsCollector} replaced for the `Struct.new` table, kept as the shadow harness's
678
+ # oracle until ADR-116 WD5 has ported every table walker onto {DeclarationWalk}.
679
+ def legacy_struct_member_layouts(root)
680
+ accumulator = {}
681
+ walk_struct_member_layouts(root, [], accumulator)
682
+ accumulator.freeze
683
+ end
684
+
685
+ def walk_struct_member_layouts(node, qualified_prefix, accumulator, self_base: nil,
686
+ singleton_cref: false)
687
+ return unless node.is_a?(Prism::Node)
688
+
689
+ case node
690
+ when Prism::ClassNode, Prism::ModuleNode
691
+ return walk_layout_declaration(node, qualified_prefix, accumulator, self_base,
692
+ singleton_cref, :struct)
693
+ when Prism::SingletonClassNode
694
+ # The expression evaluates in the enclosing cref; the body's cref is unnameable.
695
+ return walk_singleton_member_layouts(node, qualified_prefix, accumulator,
696
+ :walk_struct_member_layouts, self_base,
697
+ singleton_cref)
698
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
699
+ Prism::ConstantPathOrWriteNode
700
+ # A meta-new constant write under an unnameable cref lands on the singleton's own
701
+ # table unless it is a path write — declining a bare target rather than keying
702
+ # the block's class by the lexical prefix.
703
+ unless singleton_cref && !DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_base)
704
+ child_prefix = DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_base)
705
+ end
706
+ if child_prefix
707
+ record_struct_member_layout(accumulator, child_prefix, DeclarationWalk::Rules.meta_new_rvalue(node))
708
+ end
709
+ return if walk_struct_layout_meta_new?(node, qualified_prefix, accumulator,
710
+ self_base, singleton_cref)
711
+ when Prism::CallNode
712
+ return if walk_member_layout_eval_call?(node, qualified_prefix, accumulator,
713
+ self_base, singleton_cref,
714
+ :walk_struct_member_layouts)
715
+ end
716
+
717
+ node.rigor_each_child do |child|
718
+ walk_struct_member_layouts(child, qualified_prefix, accumulator,
719
+ self_base: self_base, singleton_cref: singleton_cref)
720
+ end
721
+ end
722
+
723
+ # The `K = Struct.new { … }`-family arm of {.walk_struct_member_layouts}: the
724
+ # factory call's receiver and arguments keep the enclosing context; the block's
725
+ # `self` is the class the write names.
726
+ def walk_struct_layout_meta_new?(node, qualified_prefix, accumulator, self_base,
727
+ singleton_cref)
728
+ split = DeclarationWalk::Rules.meta_new_block_split(node, qualified_prefix, self_base, singleton_cref)
729
+ return false unless split
730
+
731
+ enclosing, body, body_self = split
732
+ enclosing.each do |part|
733
+ walk_struct_member_layouts(part, qualified_prefix, accumulator,
734
+ self_base: self_base, singleton_cref: singleton_cref)
735
+ end
736
+ if body
737
+ walk_struct_member_layouts(body, qualified_prefix, accumulator,
738
+ self_base: body_self, singleton_cref: singleton_cref)
739
+ end
740
+ true
741
+ end
742
+
743
+ # The eval-family arm shared by the two member-layout walks: the call's receiver and
744
+ # arguments keep the enclosing context while the block body's `self` is the receiver —
745
+ # a named receiver re-anchors `self::` headers to a real class even under `class <<`;
746
+ # a bare/`self` receiver keeps the enclosing (possibly unnameable) self.
747
+ def walk_member_layout_eval_call?(node, qualified_prefix, accumulator, self_base,
748
+ singleton_cref, walk)
749
+ return false unless DeclarationWalk::Rules.receiver_eval_call?(node)
750
+
751
+ [node.receiver, *node.arguments&.arguments.to_a].compact.each do |part|
752
+ send(walk, part, qualified_prefix, accumulator,
753
+ self_base: self_base, singleton_cref: singleton_cref)
754
+ end
755
+ unnameable = DeclarationWalk::Rules.unnameable_eval_self?(false, self_base, qualified_prefix, singleton_cref)
756
+ eval_self = DeclarationWalk::Rules.eval_receiver_prefix(node, self_base || qualified_prefix, qualified_prefix,
757
+ unnameable_self: unnameable) || []
758
+ if (body = node.block.body)
759
+ send(walk, body, qualified_prefix, accumulator,
760
+ self_base: eval_self, singleton_cref: singleton_cref)
761
+ end
762
+ true
763
+ end
764
+
765
+ # The `class <<` arm shared by the two member-layout walks: the expression evaluates in
766
+ # the enclosing cref while the body's cref is the unnameable singleton class.
767
+ def walk_singleton_member_layouts(node, qualified_prefix, accumulator, walk, self_base,
768
+ singleton_cref)
769
+ send(walk, node.expression, qualified_prefix, accumulator,
770
+ self_base: self_base, singleton_cref: singleton_cref)
771
+ return unless node.body
772
+
773
+ # `self` below `class <<` is the singleton class — `self::` headers decline.
774
+ send(walk, node.body, qualified_prefix, accumulator,
775
+ self_base: EMPTY_PREFIX, singleton_cref: true)
776
+ end
777
+
778
+ # Records `qualified -> { members:, keyword_init: }` when `expr` is a `Struct.new(*Symbol [, keyword_init:
779
+ # <bool>])` call with at least one literal-Symbol member.
780
+ def record_struct_member_layout(accumulator, qualified_parts, expr, allow_outer_block: true)
781
+ expr = FactoryCallResolver.resolve_meta_factory_call(expr, allow_outer_block: allow_outer_block)
782
+ return unless expr && DeclarationWalk::Rules.struct_new_call?(expr)
783
+ return if meta_members_open?(expr)
784
+
785
+ members = meta_member_names(expr)
786
+ return if members.empty?
787
+
788
+ accumulator[qualified_parts.join("::")] = {
789
+ members: members.freeze,
790
+ keyword_init: struct_new_keyword_init?(expr)
791
+ }.freeze
792
+ end
793
+
794
+ # True when a `Struct.new` call carries `keyword_init: true` as a literal in its trailing keyword hash. A
795
+ # non-literal value (or its absence) reads as `false` — the conservative positional default.
796
+ def struct_new_keyword_init?(call_node)
797
+ args = call_node.arguments&.arguments || []
798
+ last = args.last
799
+ return false unless last.is_a?(Prism::KeywordHashNode)
800
+
801
+ last.elements.any? do |assoc|
802
+ assoc.is_a?(Prism::AssocNode) &&
803
+ assoc.key.is_a?(Prism::SymbolNode) && assoc.key.unescaped == "keyword_init" &&
804
+ assoc.value.is_a?(Prism::TrueNode)
805
+ end
806
+ end
807
+
808
+ private_class_method :meta_member_positionals, :build_def_nestings, :verified_def_nestings,
809
+ :legacy_def_nestings, :walk_def_nestings, :walk_def_nesting_declaration,
810
+ :walk_def_nesting_meta_new?, :walk_def_nesting_eval?, :nesting_lexical_prefix,
811
+ :build_superclass_tables, :superclass_walk_root, :verified_superclass_tables,
812
+ :legacy_superclass_tables, :walk_class_superclasses, :walk_superclass_eval_call?,
813
+ :walk_superclass_meta_new?, :walk_superclass_declaration?, :walk_singleton_superclasses,
814
+ :declared_ancestor_names, :collect_mixin_names, :mixin_call?, :record_mixin_names,
815
+ :build_data_member_layouts, :member_layout_tables, :verified_member_layouts,
816
+ :legacy_data_member_layouts, :walk_data_member_layouts, :walk_data_layout_meta_new?,
817
+ :walk_layout_declaration, :build_struct_member_layouts, :legacy_struct_member_layouts,
818
+ :walk_struct_member_layouts, :walk_struct_layout_meta_new?,
819
+ :walk_member_layout_eval_call?, :walk_singleton_member_layouts, :struct_new_keyword_init?
820
+ end
821
+ end
822
+ end
823
+ end