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,282 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../type"
6
+ require_relative "../../source/node_children"
7
+ require_relative "../declaration_walk/traversal"
8
+ require_relative "../declaration_walk/rules"
9
+ require_relative "census_scope"
10
+ require_relative "class_cvars_collector"
11
+ require_relative "empty_seed"
12
+
13
+ module Rigor
14
+ module Inference
15
+ module ScopeIndexer
16
+ # The class-variable census of one file (slice 7 phase 6), moved out of `scope_indexer.rb` whole (ADR-116
17
+ # WD9, U5). {.build_class_cvar_index} is the entry `ScopeIndexer.index` calls: the `class_cvars` discovery
18
+ # table, built by {ClassCvarsCollector} on the shared {DeclarationWalk}. The collector, a sibling file, calls
19
+ # the other three public functions: {.collect_def_cvar_writes} and {.collect_body_cvar_writes} gather a
20
+ # body's writes into its table, and {.finished_cvar_census} closes it. The rest is private: the gather and
21
+ # its recording steps, and the walker the collector replaced ({.legacy_class_cvar_index} and its arms), kept
22
+ # as the oracle `RIGOR_SHADOW_RULE_WALK` checks the collector against. {JoinOnlyCvarWrite} is the mark a
23
+ # joining write carries until the census is finished; {CvarHierarchy}, a sibling file, reads it too.
24
+ #
25
+ # It asks {DeclarationWalk::Rules} for each declaration context and {CensusScope}, at two sites, for the scope
26
+ # a census types a body's rvalues under, which the ivar and the constant censuses also ask. It calls nothing
27
+ # left in `scope_indexer.rb`. `scope_indexer.rb` requires this file, and this file does not require it back.
28
+ # It reads `ScopeIndexer::EMPTY_NESTING` lexically, as {NodePropagation} reads the indexer's constants, and
29
+ # two constants of the ivar census by a path, {ClassIvarIndex::IVAR_BARRIER_NODES} and
30
+ # {ClassIvarIndex::BindingTargets}. It does not require that module's file, which requires the dispatcher and
31
+ # so the `rbs` gem, where this one loads without either. So the file loads alone, and until
32
+ # `scope_indexer.rb` has loaded only {.finished_cvar_census} answers every call: the entry is a `NameError`,
33
+ # and a gather answers for a body it skips and is a `NameError` for one it would type.
34
+ module ClassCvarCensus
35
+ module_function
36
+
37
+ # Slice 7 phase 6 — class-cvar pre-pass. Same shape as the ivar pre-pass but collects
38
+ # `Prism::ClassVariableWriteNode` writes inside ANY def body (instance or singleton) of the enclosing class,
39
+ # because Ruby cvars are shared across both facets. The resulting table is seeded into both instance and
40
+ # singleton method bodies through `Scope#class_cvars_for`.
41
+ #
42
+ # ADR-116 WD5 — built by {ClassCvarsCollector} on the shared {DeclarationWalk}; `RIGOR_SHADOW_RULE_WALK`
43
+ # checks it against {.legacy_class_cvar_index}.
44
+ def build_class_cvar_index(root, default_scope)
45
+ collector = ClassCvarsCollector.new
46
+ DeclarationWalk.run(root, [collector],
47
+ DeclarationWalk::Context.root(scope: default_scope, nesting: EMPTY_NESTING))
48
+ DeclarationWalk::Shadow.verified(:class_cvars, default_scope.source_path, collector.table) do
49
+ legacy_class_cvar_index(root, default_scope)
50
+ end
51
+ # Issue #3219 — the per-class census the shadow oracle checks, then joined along each superclass tree.
52
+ collector.hierarchy_table(default_scope)
53
+ end
54
+
55
+ # The walker {ClassCvarsCollector} replaced, kept as the shadow harness's oracle until ADR-116 WD5 has
56
+ # ported every table walker onto {DeclarationWalk}.
57
+ def legacy_class_cvar_index(root, default_scope)
58
+ accumulator = {}
59
+ walk_class_cvars(root, [], default_scope, accumulator)
60
+ finished_cvar_census(accumulator)
61
+ end
62
+
63
+ # `def_owner` names the class a `def` leaf's cvar writes belong to when `self` is
64
+ # rebound — see {ClassIvarIndex.walk_class_ivars}. `[]` marks a self no name covers.
65
+ def walk_class_cvars(node, qualified_prefix, default_scope, accumulator, def_owner: nil,
66
+ singleton_cref: false)
67
+ return unless node.is_a?(Prism::Node)
68
+
69
+ case node
70
+ when Prism::ClassNode, Prism::ModuleNode
71
+ return if walk_cvars_declaration?(node, qualified_prefix, default_scope, accumulator,
72
+ def_owner, singleton_cref)
73
+ when Prism::SingletonClassNode
74
+ # The expression evaluates in the enclosing cref; the body's cref is the
75
+ # unnameable singleton class — the marker lifts only at a nameable header.
76
+ walk_class_cvars(node.expression, qualified_prefix, default_scope, accumulator,
77
+ def_owner: def_owner, singleton_cref: singleton_cref)
78
+ if node.body
79
+ # `self` below `class <<` is the singleton class — `self::`-anchored receivers
80
+ # decline; `@@x` keeps the lexical cref either way, so the body walks ownerless.
81
+ walk_class_cvars(node.body, qualified_prefix, default_scope, accumulator,
82
+ def_owner: [], singleton_cref: true)
83
+ end
84
+ return
85
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode,
86
+ Prism::ConstantOrWriteNode, Prism::ConstantPathOrWriteNode
87
+ return if walk_cvars_meta_new?(node, qualified_prefix, default_scope, accumulator,
88
+ def_owner, singleton_cref)
89
+ when Prism::DefNode
90
+ # `@@x` inside a `def` resolves through the LEXICAL cref — `Module.nesting` is the
91
+ # same inside a meta-new or eval block, so `def_owner` (the rebound self) must not
92
+ # feed this: `K = Class.new { def m = @@x }` inside `class C` writes `C::@@x`.
93
+ collect_def_cvar_writes(node, qualified_prefix, default_scope, accumulator)
94
+ return
95
+ when Prism::CallNode
96
+ return if walk_cvars_meta_call?(node, qualified_prefix, default_scope, accumulator,
97
+ def_owner, singleton_cref)
98
+ return if walk_cvars_eval_call?(node, qualified_prefix, default_scope, accumulator,
99
+ def_owner, singleton_cref)
100
+ end
101
+
102
+ node.rigor_each_child do |child|
103
+ walk_class_cvars(child, qualified_prefix, default_scope, accumulator,
104
+ def_owner: def_owner, singleton_cref: singleton_cref)
105
+ end
106
+ end
107
+
108
+ # The `K = Class.new { … }` arm of {.walk_class_cvars}: the block's `def` cvar
109
+ # writes belong to the class the write names; its declarations stay lexical.
110
+ def walk_cvars_meta_new?(node, qualified_prefix, default_scope, accumulator,
111
+ def_owner, singleton_cref)
112
+ split = DeclarationWalk::Rules.meta_new_block_split(node, qualified_prefix, def_owner, singleton_cref)
113
+ return false unless split
114
+
115
+ enclosing, body, body_self = split
116
+ enclosing.each do |part|
117
+ walk_class_cvars(part, qualified_prefix, default_scope, accumulator,
118
+ def_owner: def_owner, singleton_cref: singleton_cref)
119
+ end
120
+ if body
121
+ walk_class_cvars(body, qualified_prefix, default_scope, accumulator,
122
+ def_owner: body_self, singleton_cref: singleton_cref)
123
+ end
124
+ true
125
+ end
126
+
127
+ # The class/module arm of {.walk_class_cvars}: under an unnameable cref a
128
+ # bare/`self::` header opens `#<singleton>::Name` — ownerless; nameable headers
129
+ # re-anchor at a real cref.
130
+ def walk_cvars_declaration?(node, qualified_prefix, default_scope, accumulator,
131
+ def_owner, singleton_cref)
132
+ ctx = DeclarationWalk::Rules.decl_body_context(node, qualified_prefix, def_owner, singleton_cref)
133
+ return false unless ctx
134
+
135
+ _self_decl, child_prefix, child_cref = ctx
136
+ return true unless node.body
137
+
138
+ collect_body_cvar_writes(node.body, child_cref ? [] : child_prefix,
139
+ DeclarationWalk::Rules.scope_entering_declaration(default_scope, node.constant_path),
140
+ accumulator)
141
+ walk_class_cvars(node.body, child_cref ? [] : child_prefix,
142
+ DeclarationWalk::Rules.scope_entering_declaration(default_scope, node.constant_path),
143
+ accumulator, singleton_cref: child_cref)
144
+ true
145
+ end
146
+
147
+ # The bare `Class.new { … }`-family call arm of {.walk_class_cvars}: the block's
148
+ # `def` cvar writes belong to an unnameable class.
149
+ def walk_cvars_meta_call?(node, qualified_prefix, default_scope, accumulator,
150
+ def_owner, singleton_cref)
151
+ return false unless DeclarationWalk::Rules.meta_new_constant_rvalue?(node) &&
152
+ node.block.is_a?(Prism::BlockNode)
153
+
154
+ [node.receiver, *node.arguments&.arguments.to_a].compact.each do |part|
155
+ walk_class_cvars(part, qualified_prefix, default_scope, accumulator,
156
+ def_owner: def_owner, singleton_cref: singleton_cref)
157
+ end
158
+ if (body = node.block.body)
159
+ walk_class_cvars(body, qualified_prefix, default_scope, accumulator,
160
+ def_owner: [], singleton_cref: singleton_cref)
161
+ end
162
+ true
163
+ end
164
+
165
+ # The eval-family arm of {.walk_class_cvars}: the block's `self` is the receiver for
166
+ # `self::` declarations (`X.class_eval { class self::D; def m = @@x }` reads
167
+ # `X::D::@@x`), while `@@x` writes inside `def`s keep the lexical cref either way.
168
+ def walk_cvars_eval_call?(node, qualified_prefix, default_scope, accumulator,
169
+ def_owner, singleton_cref)
170
+ split = DeclarationWalk::Rules.eval_block_split(node, qualified_prefix, def_owner, singleton_cref)
171
+ return false unless split
172
+
173
+ enclosing, body, body_self = split
174
+ enclosing.each do |part|
175
+ walk_class_cvars(part, qualified_prefix, default_scope, accumulator,
176
+ def_owner: def_owner, singleton_cref: singleton_cref)
177
+ end
178
+ if body
179
+ walk_class_cvars(body, qualified_prefix, default_scope, accumulator,
180
+ def_owner: body_self, singleton_cref: singleton_cref)
181
+ end
182
+ true
183
+ end
184
+
185
+ def collect_def_cvar_writes(def_node, qualified_prefix, default_scope, accumulator)
186
+ return if def_node.body.nil? || qualified_prefix.empty?
187
+
188
+ class_name = qualified_prefix.join("::")
189
+ body_scope = CensusScope.census_body_scope(default_scope, qualified_prefix,
190
+ Type::Combinator.nominal_of(class_name))
191
+ gather_cvar_writes(def_node.body, body_scope, class_name, accumulator, join_only: false)
192
+ end
193
+
194
+ # Issue #3204 — the writes a `class` / `module` body makes outside its `def`s (`@@x = nil` at the top of
195
+ # the body, in a block or a `class << self` there): Ruby resolves each through the body's lexical cref,
196
+ # so it is the same cvar a sibling `def` reads. Such a write only joins an entry a `def` body's write of
197
+ # the same class creates: the census misses most of the other ways a cvar is written
198
+ # (`||=`, `+=`, an including class, a subclass, another file, `class_variable_set` from outside), so a
199
+ # body-only seed would be precise where Ruby's value is not, and stays unbound. The rvalue is typed with
200
+ # the class itself as `self`. The gather stops at `def`, `class` and `module`, which their own events enter.
201
+ def collect_body_cvar_writes(body, qualified_prefix, default_scope, accumulator)
202
+ return if body.nil? || qualified_prefix.empty?
203
+
204
+ class_name = qualified_prefix.join("::")
205
+ body_scope = CensusScope.census_body_scope(default_scope, qualified_prefix,
206
+ Type::Combinator.singleton_of(class_name))
207
+ gather_cvar_writes(body, body_scope, class_name, accumulator, join_only: true)
208
+ end
209
+
210
+ def gather_cvar_writes(node, scope, class_name, accumulator, join_only:)
211
+ return unless node.is_a?(Prism::Node)
212
+
213
+ if node.is_a?(Prism::ClassVariableWriteNode)
214
+ record_cvar_write(accumulator, class_name, node.name,
215
+ EmptySeed.erase(node.value, scope.type_of(node.value)), join_only: join_only)
216
+ end
217
+ record_reflective_cvar_write(node, scope, class_name, accumulator) if node.is_a?(Prism::CallNode)
218
+ # Issue #1217 — `rescue => @@c` / `for @@c in xs` write through a target node. Each enters as the
219
+ # `Dynamic[top]` floor, the rescue reference too. The floor dates from when the census keyed each class
220
+ # alone, so a superclass's `@@c = nil` was not in a subclass's entry; #3219's {CvarHierarchy} join now
221
+ # adds it, and the floor is kept.
222
+ ClassIvarIndex::BindingTargets.names(node, Prism::ClassVariableTargetNode).each do |name|
223
+ record_cvar_write(accumulator, class_name, name, Type::Combinator.untyped, join_only: join_only)
224
+ end
225
+ return if ClassIvarIndex::IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
226
+
227
+ node.rigor_each_child { |c| gather_cvar_writes(c, scope, class_name, accumulator, join_only: join_only) }
228
+ end
229
+
230
+ # Issue #3204 — `class_variable_set(:@@x, v)` on an implicit or `self` receiver writes the cvar a sibling
231
+ # `def` reads, as `@@x = v` does. It only joins, like a class-body write: a dynamic name, a `send`, an
232
+ # explicit receiver or another file may write the same cvar unseen.
233
+ def record_reflective_cvar_write(node, scope, class_name, accumulator)
234
+ return unless node.name == :class_variable_set && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode))
235
+
236
+ args = node.arguments&.arguments
237
+ return unless args&.size == 2
238
+
239
+ name_node, value = args
240
+ # A name that is not a cvar's never meets a `def` write's entry, so the join drops it.
241
+ name = name_node.unescaped if name_node.is_a?(Prism::SymbolNode) || name_node.is_a?(Prism::StringNode)
242
+ return unless name
243
+
244
+ record_cvar_write(accumulator, class_name, name.to_sym, EmptySeed.erase(value, scope.type_of(value)),
245
+ join_only: true)
246
+ end
247
+
248
+ # A class-body or `class_variable_set` write no `def` body's write has met yet (issue #3204): it joins an
249
+ # entry a creating write makes later, and {.finished_cvar_census} drops it when none does.
250
+ JoinOnlyCvarWrite = Data.define(:type)
251
+
252
+ # Every census write unions with what the entry already holds, so the seed does not depend on the order of
253
+ # the writes. A `join_only` write (a class or module body's, or `class_variable_set`) never creates an entry
254
+ # on its own: it is held as a {JoinOnlyCvarWrite} until a `def` body's write of the same cvar arrives.
255
+ def record_cvar_write(accumulator, class_name, name, type, join_only:)
256
+ entries = (accumulator[class_name] ||= {})
257
+ existing = entries[name]
258
+ entries[name] =
259
+ if existing.nil? then join_only ? JoinOnlyCvarWrite.new(type) : type
260
+ elsif existing.is_a?(JoinOnlyCvarWrite)
261
+ joined = Type::Combinator.union(existing.type, type)
262
+ join_only ? JoinOnlyCvarWrite.new(joined) : joined
263
+ else Type::Combinator.union(existing, type)
264
+ end
265
+ end
266
+
267
+ # The census as the discovery index holds it: each cvar only joining writes reached dropped, each class's
268
+ # entry frozen, then the whole.
269
+ def finished_cvar_census(accumulator)
270
+ accumulator.filter_map do |class_name, entries|
271
+ kept = entries.reject { |_, type| type.is_a?(JoinOnlyCvarWrite) }
272
+ [class_name, kept.freeze] unless kept.empty?
273
+ end.to_h.freeze
274
+ end
275
+
276
+ private_class_method :legacy_class_cvar_index, :walk_class_cvars, :walk_cvars_meta_new?,
277
+ :walk_cvars_declaration?, :walk_cvars_meta_call?, :walk_cvars_eval_call?,
278
+ :gather_cvar_writes, :record_reflective_cvar_write, :record_cvar_write
279
+ end
280
+ end
281
+ end
282
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../declaration_walk/traversal"
4
+ require_relative "cvar_hierarchy"
4
5
 
5
6
  module Rigor
6
7
  module Inference
@@ -14,13 +15,15 @@ module Rigor
14
15
 
15
16
  def initialize
16
17
  @table = {}
18
+ @headers = CvarHierarchy.headers
17
19
  end
18
20
 
19
21
  # Issue #3204 — a `class` / `module` body's own writes, outside its `def`s, under the body's context;
20
22
  # each only joins an entry a `def` body's write creates. An unnameable cref (a header below
21
23
  # `class << expr`) leaves the prefix empty, and the gather skips it.
22
- def on_declaration(node, _context, body)
23
- ScopeIndexer.collect_body_cvar_writes(node.body, body.prefix, body.scope, @table)
24
+ def on_declaration(node, context, body)
25
+ ClassCvarCensus.collect_body_cvar_writes(node.body, body.prefix, body.scope, @table)
26
+ CvarHierarchy.record(@headers, node, context, body)
24
27
  DeclarationWalk::DESCEND
25
28
  end
26
29
 
@@ -30,12 +33,16 @@ module Rigor
30
33
  # gather reads the body with `def`, `class` and `module` as barriers, and nothing below a `def` reaches
31
34
  # this table.
32
35
  def on_def(node, context)
33
- ScopeIndexer.collect_def_cvar_writes(node, context.prefix, context.scope, @table)
36
+ ClassCvarCensus.collect_def_cvar_writes(node, context.prefix, context.scope, @table)
34
37
  DeclarationWalk::DECLINE
35
38
  end
36
39
 
37
- # The table as the discovery index holds it ({ScopeIndexer#finished_cvar_census}).
38
- def table = ScopeIndexer.finished_cvar_census(@table)
40
+ # The table as the discovery index holds it ({ClassCvarCensus.finished_cvar_census}).
41
+ def table = ClassCvarCensus.finished_cvar_census(@table)
42
+
43
+ # Issue #3219 — {#table} with each class's entries joined along its in-file superclass tree
44
+ # ({CvarHierarchy.join}); the walk's root context must track `Module.nesting` for a superclass to resolve.
45
+ def hierarchy_table(scope) = ClassCvarCensus.finished_cvar_census(CvarHierarchy.join(@table, @headers, scope))
39
46
  end
40
47
  end
41
48
  end
@@ -0,0 +1,228 @@
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 "../../type"
8
+ require_relative "../declaration_walk/rules"
9
+
10
+ module Rigor
11
+ module Inference
12
+ module ScopeIndexer
13
+ # The cross-file class-declaration walk, moved out of `scope_indexer.rb` whole (ADR-116 WD9, U1).
14
+ # {.collect_class_decls} registers each `class` / `module` declaration of a file, and each constant write
15
+ # that creates a class, under its qualified name; every other function here is an arm or a step of that walk.
16
+ # It asks {DeclarationWalk::Rules} for each declaration context and calls nothing left in `scope_indexer.rb`,
17
+ # which requires this file; this file does not require it back. It reads two of the indexer's constants
18
+ # lexically, `ScopeIndexer::EMPTY_PREFIX` and `ScopeIndexer::RECEIVER_EVAL_CALLS`, so the file loads alone but
19
+ # answers only once `scope_indexer.rb` has loaded: walking a `class <<` body or a call with a block is a
20
+ # `NameError` until then, as the indexer's constants are for {NodePropagation}, which reads them the same way.
21
+ module ClassDeclarations
22
+ module_function
23
+
24
+ # Cross-file counterpart of `DeclarationArtifacts.record_declarations` — registers every `class` / `module`
25
+ # declaration under its qualified name and descends into the body (so `module Foo; class Bar` registers both
26
+ # `Foo` and `Foo::Bar`). `self_prefix` names a REBOUND self for `self::`-anchored declarations — see
27
+ # {DeclarationArtifacts.record_declarations}.
28
+ def collect_class_decls(node, qualified_prefix, accumulator, compacts = nil,
29
+ self_prefix = nil, singleton_cref: false)
30
+ return unless node.is_a?(Prism::Node)
31
+
32
+ case node
33
+ when Prism::ClassNode, Prism::ModuleNode
34
+ return if collect_decl_body?(node, qualified_prefix, accumulator, compacts,
35
+ self_prefix, singleton_cref)
36
+ when Prism::SingletonClassNode
37
+ # The expression evaluates in the ENCLOSING cref — `class << (class D; self; end)`
38
+ # still names `C::D` — while the body's cref is the unnameable singleton.
39
+ collect_class_decls(node.expression, qualified_prefix, accumulator, compacts,
40
+ self_prefix, singleton_cref: singleton_cref)
41
+ if node.body
42
+ collect_class_decls(node.body, qualified_prefix, accumulator, compacts,
43
+ EMPTY_PREFIX, singleton_cref: true)
44
+ end
45
+ return
46
+ when Prism::ConstantWriteNode, Prism::ConstantOrWriteNode,
47
+ Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode
48
+ # `K = Class.new` under an unnameable cref lands on the singleton's own table;
49
+ # a path write resolves its base lexically and stays nameable.
50
+ record_class_new_constant_decl(node, qualified_prefix, accumulator,
51
+ self_prefix: self_prefix, singleton_cref: singleton_cref)
52
+ return if collect_meta_new_constant_decls?(node, qualified_prefix, accumulator, compacts,
53
+ self_prefix, singleton_cref)
54
+ when Prism::CallNode
55
+ return if collect_eval_class_decls?(node, qualified_prefix, accumulator, compacts,
56
+ self_prefix, singleton_cref)
57
+ end
58
+
59
+ node.rigor_each_child do |child|
60
+ collect_class_decls(child, qualified_prefix, accumulator, compacts,
61
+ self_prefix, singleton_cref: singleton_cref)
62
+ end
63
+ end
64
+
65
+ # The class/module arm of {#collect_class_decls}. Under an unnameable cref a bare/`self::`
66
+ # header opens `#<singleton>::Name` — a real class nothing can name — so it is declined
67
+ # rather than published as `C::Name`; the body still walks (rooted headers below it
68
+ # re-anchor). Returns whether the declaration was consumed.
69
+ def collect_decl_body?(node, qualified_prefix, accumulator, compacts, self_prefix,
70
+ singleton_cref)
71
+ self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, self_prefix)
72
+ child_prefix = self_decl ||
73
+ Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
74
+ return false unless child_prefix
75
+
76
+ child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
77
+ unless child_cref
78
+ full = child_prefix.join("::")
79
+ accumulator[full] = Type::Combinator.singleton_of(full)
80
+ record_compact_header(compacts, qualified_prefix, node.constant_path, full) if compacts
81
+ end
82
+ return true unless node.body
83
+
84
+ collect_class_decls(node.body, child_cref ? [] : child_prefix, accumulator, compacts,
85
+ nil, singleton_cref: child_cref)
86
+ true
87
+ end
88
+
89
+ # The eval-family arm of {#collect_class_decls}: same dual context as
90
+ # {DeclarationArtifacts.record_eval_declarations?} — `self::` headers anchor on the receiver, bare
91
+ # headers stay lexical.
92
+ def collect_eval_class_decls?(node, qualified_prefix, accumulator, compacts, self_prefix,
93
+ singleton_cref)
94
+ return false unless node.block.is_a?(Prism::BlockNode) &&
95
+ RECEIVER_EVAL_CALLS.include?(node.name)
96
+
97
+ [node.receiver, *node.arguments&.arguments.to_a].compact.each do |part|
98
+ collect_class_decls(part, qualified_prefix, accumulator, compacts, self_prefix,
99
+ singleton_cref: singleton_cref)
100
+ end
101
+ unnameable =
102
+ DeclarationWalk::Rules.unnameable_eval_self?(false, self_prefix, qualified_prefix, singleton_cref)
103
+ eval_self = DeclarationWalk::Rules.eval_receiver_prefix(
104
+ node, self_prefix || qualified_prefix, qualified_prefix, unnameable_self: unnameable
105
+ ) || []
106
+ if (body = node.block.body)
107
+ collect_class_decls(body, qualified_prefix, accumulator, compacts, eval_self,
108
+ singleton_cref: singleton_cref)
109
+ end
110
+ true
111
+ end
112
+
113
+ # The meta-new arm of {#collect_class_decls}: the factory call's receiver and
114
+ # arguments evaluate in the enclosing context; the block's `self` is the class the
115
+ # write names (`[]` when it names nothing) while the cref stays lexical.
116
+ def collect_meta_new_constant_decls?(node, qualified_prefix, accumulator, compacts,
117
+ self_prefix, singleton_cref)
118
+ call = DeclarationWalk::Rules.meta_new_block_call(node)
119
+ return false unless call
120
+
121
+ child_prefix = DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_prefix)
122
+ meta_self =
123
+ if singleton_cref && !DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_prefix)
124
+ []
125
+ else
126
+ child_prefix || []
127
+ end
128
+ [call.receiver, *call.arguments&.arguments.to_a].compact.each do |part|
129
+ collect_class_decls(part, qualified_prefix, accumulator, compacts,
130
+ self_prefix, singleton_cref: singleton_cref)
131
+ end
132
+ if (body = DeclarationWalk::Rules.meta_new_block_body(node))
133
+ collect_class_decls(body, qualified_prefix, accumulator, compacts,
134
+ meta_self, singleton_cref: singleton_cref)
135
+ end
136
+ true
137
+ end
138
+
139
+ # Issue [#722](https://github.com/rigortype/rigor/issues/722) residue 2 — records what a COMPACT header
140
+ # written inside an enclosing namespace would name if its leading segment fell through to the top level.
141
+ # `class Outer::Leaf` inside `module Wrap` is keyed `Wrap::Outer::Leaf` by the per-node
142
+ # {Source::ConstantPath.declaration_prefix}, but Ruby resolves the leading `Outer` by ORDINARY constant
143
+ # lookup: it reopens `::Outer::Leaf` unless `Wrap::Outer` exists. Whether it exists is a whole-project
144
+ # fact, so the candidate is recorded here and adjudicated once the fold knows every declared name
145
+ # ({ScopeIndexer#compact_header_renames}).
146
+ #
147
+ # @param compacts — `{recorded name => [namespace the header needs, name it falls through to]}`.
148
+ def record_compact_header(compacts, qualified_prefix, constant_path, full)
149
+ return if qualified_prefix.empty?
150
+ return unless constant_path.is_a?(Prism::ConstantPathNode)
151
+ return if Source::ConstantPath.rooted?(constant_path)
152
+
153
+ rendered = Source::ConstantPath.qualified_name(constant_path)
154
+ return unless rendered&.include?("::")
155
+
156
+ compacts[full] = [(qualified_prefix + [rendered.split("::").first]).join("::"), rendered]
157
+ end
158
+
159
+ # T1 (template-corpora survey) — record a class-creating constant write (`Const = Class.new(Super)`, the bare
160
+ # `Class.new` / `Module.new`, and the `Data.define(*sym)` / `Struct.new(*sym)` forms, each with or without a
161
+ # block) in the cross-file discovery table so a reference to `Const` from ANOTHER file under the same namespace
162
+ # resolves to the project class instead of falling through to a same-named class elsewhere
163
+ # (`Liquid::SyntaxError = Class.new(Error)` referenced in a sibling file's `rescue SyntaxError => e`, which
164
+ # otherwise resolved to core `::SyntaxError`). Mirrors the single-file `in_source_constants` answer, which types
165
+ # `Class.new(Super)` as `Singleton[Super]` (the constructed class answers method lookups through Super's chain)
166
+ # and every other recognised form as `Singleton[Const]`.
167
+ #
168
+ # #271 — the Data/Struct forms are here because their absence was an ACTIVE false positive, not merely a missed
169
+ # resolution: a nested `Const = Data.define(...)` invisible cross-file lets Ruby's lexical walk continue to the
170
+ # PARENT namespace's same-named sibling, so `Analysis::PluginFactFingerprint.from_registry(...).opaque?` typed
171
+ # its receiver as the unrelated `Rigor::Analysis::Result` and reported `call.undefined-method` on correct code.
172
+ # Nested `Result` / `Entry` / `Config` Data constants shadowing a sibling are ordinary Ruby, and only the
173
+ # DEFINING file's `in_source_constants` (never part of the project seed) knew about them.
174
+ def record_class_new_constant_decl(node, qualified_prefix, accumulator, self_prefix: nil,
175
+ singleton_cref: false)
176
+ rvalue = DeclarationWalk::Rules.meta_new_rvalue(node)
177
+ return unless rvalue && DeclarationWalk::Rules.meta_new_constant_rvalue?(rvalue)
178
+ return if singleton_cref && !DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_prefix)
179
+
180
+ child_prefix = DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_prefix)
181
+ return if child_prefix.nil? || child_prefix.empty?
182
+
183
+ full = child_prefix.join("::")
184
+ accumulator[full] = Type::Combinator.singleton_of(
185
+ meta_new_constant_decl_name(rvalue, full, qualified_prefix, accumulator)
186
+ )
187
+ end
188
+
189
+ # The name the constant's `Singleton[...]` carries. Only a block-less `Class.new(Super)` borrows its
190
+ # superclass's name (the constructed class answers lookups through `Super`'s chain and declares nothing of its
191
+ # own); every other form — Data/Struct members, any block body — owns methods under its OWN qualified name,
192
+ # which is exactly what the per-file `ConstantTables.meta_new_constant_type` answers.
193
+ def meta_new_constant_decl_name(rvalue, full, qualified_prefix, accumulator)
194
+ return full if rvalue.block || DeclarationWalk::Rules.data_define_call?(rvalue) ||
195
+ DeclarationWalk::Rules.struct_new_call?(rvalue)
196
+
197
+ class_new_superclass_name(rvalue, qualified_prefix, accumulator) || full
198
+ end
199
+
200
+ # Lexically-qualified name of a `Class.new(Super)` superclass argument, or nil when there is no positional
201
+ # superclass (a bare `Class.new` / `Module.new`). When the unqualified super name is a class already discovered
202
+ # under an enclosing-prefix segment, the qualified form is returned (so `Class.new(Error)` inside `module M`
203
+ # resolves to `M::Error`); otherwise the literal name is returned (covering a core / RBS-known superclass
204
+ # spelled bare).
205
+ def class_new_superclass_name(call_node, qualified_prefix, accumulator)
206
+ arg = call_node.arguments&.arguments&.first
207
+ return nil if arg.nil?
208
+
209
+ raw = Source::ConstantPath.qualified_name(arg)
210
+ return nil if raw.nil?
211
+
212
+ prefix = qualified_prefix.dup
213
+ until prefix.empty?
214
+ candidate = (prefix + [raw]).join("::")
215
+ return candidate if accumulator.key?(candidate)
216
+
217
+ prefix.pop
218
+ end
219
+ raw
220
+ end
221
+
222
+ private_class_method :collect_decl_body?, :collect_eval_class_decls?, :collect_meta_new_constant_decls?,
223
+ :record_compact_header, :record_class_new_constant_decl, :meta_new_constant_decl_name,
224
+ :class_new_superclass_name
225
+ end
226
+ end
227
+ end
228
+ end