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,844 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../type"
6
+ require_relative "../../analysis/dependency_recorder"
7
+ require_relative "../../source/constant_path"
8
+ require_relative "../../source/node_children"
9
+ require_relative "../declaration_walk/rules"
10
+ require_relative "../global_write_census"
11
+ require_relative "../receiver_writer_call"
12
+ require_relative "census_scope"
13
+ require_relative "empty_seed"
14
+
15
+ module Rigor
16
+ module Inference
17
+ module ScopeIndexer
18
+ # The constant tables of one file, moved out of `scope_indexer.rb` whole (ADR-116 WD9, #3400). They are two
19
+ # walks over every constant write and the rules both ask: the typed in-source table (slice 7 phase 9), which
20
+ # types each rvalue; the publication census (#644), which is syntactic and files a descriptor per name; and
21
+ # the write-target rules, which say what `self` a block runs under and which name a write targets.
22
+ #
23
+ # Four functions are public, because another file calls each. {.build_in_source_constants} is the typed
24
+ # walk's entry, which `ScopeIndexer.index`, {PreEvalConstants} and {ConstantIvarValue} call.
25
+ # {.constant_write_census} is the census's: `ScopeIndexer.seed_constant_tables` and the cross-file fold
26
+ # (`ScopeIndexer.merge_constant_literal_tables`) read the tables it answers. {.rebound_block_self} is the
27
+ # one rule other walks ask: the mixin tables (`MixinTables.walk_mixin_call_children`) and
28
+ # {SharedWalkTables}. {.dynamic_target_key} spells the wildcard key of a write through a base nothing names,
29
+ # which the fold files too (`ScopeIndexer.apply_dynamic_target_writes`). The other 36 are private: the two
30
+ # walks with their arms and recording steps, the rest of the rules, and {.constant_writes_for_file}, which
31
+ # no file of `lib/` calls and the specs reach through `send`.
32
+ #
33
+ # It asks {DeclarationWalk::Rules} for each declaration context and {CensusScope} for two things, at one site
34
+ # each: {CensusScope.census_body_scope} (the scope a census types a body's rvalues under, which the ivar and
35
+ # the class-variable censuses ask too) and {CensusScope.census_nesting} (the chain that scope answers
36
+ # `Module.nesting` with). It calls nothing left in `scope_indexer.rb`. Four constants moved with it, the ones
37
+ # only it read: {CONSTANT_UNPUBLISHABLE} (public, as it was), `OPAQUE_SELF_BLOCK_CALLS`, `OBJECT_OWNER` and
38
+ # `CensusTables`. It reads `ScopeIndexer::OPAQUE_SELF`, `RECEIVER_EVAL_CALLS`, `CONSTANT_MEMO` and
39
+ # `DYNAMIC_TARGET_PREFIX` lexically, as {NodePropagation} reads the indexer's constants: other rows of
40
+ # `scope_indexer.rb` or other files read each.
41
+ #
42
+ # `scope_indexer.rb` requires this file, and this file does not require it back. It requires the file of
43
+ # what else it names, with one exception: `in_effect_refinements.rb` reads `Scope::DiscoveryIndex` as it
44
+ # loads and does not require `scope.rb`, so a require of it here would stop this file, and
45
+ # {SharedWalkTables}, which requires this one, from loading alone. `scope_indexer.rb` has loaded both by the
46
+ # time it requires this file. So the file loads alone, and until `scope_indexer.rb` has loaded little of it
47
+ # answers: {.constant_write_census} and {.dynamic_target_key} are a `NameError`, {.rebound_block_self}
48
+ # answers only for a node that is no block-taking call, and {.build_in_source_constants} only for a program
49
+ # with no `class` or `module` header and no call that takes a block.
50
+ module ConstantTables # rubocop:disable Metrics/ModuleLength
51
+ module_function
52
+
53
+ # Issue #644 — the census descriptor for a constant write that publishes no value: an unfoldable or
54
+ # non-literal rvalue, an operator / multi-assign / dynamic-path form, or a name this file writes twice.
55
+ # A Symbol, so it is distinguishable from a `[literal]` descriptor by class alone and rides the ADR-85
56
+ # seed bundle through `Marshal` unchanged.
57
+ CONSTANT_UNPUBLISHABLE = :unpublishable
58
+
59
+ # Slice 7 phase 9 — in-source constant value pre-pass. Walks the entire program (top-level AND inside class /
60
+ # module / def bodies) for `Prism::ConstantWriteNode` and `Prism::ConstantPathWriteNode`, types each rvalue, and
61
+ # accumulates by qualified name. Constants defined inside a class body are qualified with the surrounding class
62
+ # path; a constant written via a path resolves its own namespace through the enclosing nesting first
63
+ # ({.constant_path_write_key}) — which is where this walk and the
64
+ # [#644](https://github.com/rigortype/rigor/issues/644) publication census below stopped agreeing about
65
+ # a path write's name.
66
+ def build_in_source_constants(root, default_scope)
67
+ accumulator = {}
68
+ walk_constant_writes(root, [], default_scope, accumulator)
69
+ accumulator.freeze
70
+ end
71
+
72
+ # The block-taking call whose block `self` this walk cannot name even when the receiver names a
73
+ # class: a `define_method` body runs on the receiver's INSTANCE at call time, an object
74
+ # nothing in the source names, so a `self::` write inside it is declined — gradual for
75
+ # the case where the instance is a class or module, and no guess for the rest.
76
+ OPAQUE_SELF_BLOCK_CALLS = %i[define_method].freeze
77
+ private_constant :OPAQUE_SELF_BLOCK_CALLS
78
+
79
+ # {DeclarationWalk::Rules::OBJECT_OWNER}, bound here: the owner whose constants the census spells BARE.
80
+ OBJECT_OWNER = DeclarationWalk::Rules::OBJECT_OWNER
81
+ private_constant :OBJECT_OWNER
82
+
83
+ def walk_constant_writes(node, qualified_prefix, default_scope, accumulator, self_owner = nil,
84
+ singleton_cref: false)
85
+ return unless node.is_a?(Prism::Node)
86
+
87
+ case node
88
+ when Prism::SingletonClassNode
89
+ return walk_singleton_class_writes(node, qualified_prefix, default_scope, accumulator,
90
+ self_owner, singleton_cref)
91
+ when Prism::ClassNode, Prism::ModuleNode
92
+ return if walk_typed_declaration?(node, qualified_prefix, default_scope, accumulator,
93
+ self_owner, singleton_cref)
94
+ when Prism::ConstantWriteNode
95
+ # A bare write under an unnameable cref — anywhere lexically below `class <<`,
96
+ # including inside an eval or `Class.new` block, since `Module.nesting` never
97
+ # rebinds — lands on that cref's constant table, a name nothing else can
98
+ # produce, so it declines rather than filing under the enclosing class.
99
+ unless singleton_cref
100
+ record_constant_write(node, qualified_prefix, default_scope, accumulator,
101
+ qualified_write_name(qualified_prefix, node.name.to_s), self_owner)
102
+ end
103
+ return
104
+ when Prism::ConstantPathWriteNode
105
+ full = constant_path_write_key(node.target, qualified_prefix, default_scope, self_owner)
106
+ record_constant_write(node, qualified_prefix, default_scope, accumulator, full, self_owner) if full
107
+ return
108
+ end
109
+
110
+ walk_constant_write_children(node, qualified_prefix, default_scope, accumulator, self_owner,
111
+ singleton_cref)
112
+ end
113
+
114
+ # `self` inside a `class <<` body is the singleton: `self`-anchored and bare write
115
+ # targets land on its constant table (unnameable, so both decline), and the body is
116
+ # entered with an OPAQUE self so a `self::`-anchored eval receiver declines — the
117
+ # read raises NameError at runtime unless the constant lives on that singleton.
118
+ def walk_singleton_class_writes(node, qualified_prefix, default_scope, accumulator,
119
+ self_owner, singleton_cref)
120
+ walk_constant_writes(node.expression, qualified_prefix, default_scope, accumulator,
121
+ self_owner, singleton_cref: singleton_cref)
122
+ return unless node.body
123
+
124
+ walk_constant_writes(node.body, qualified_prefix, default_scope, accumulator,
125
+ OPAQUE_SELF, singleton_cref: true)
126
+ end
127
+
128
+ # The class/module arm of {.walk_constant_writes}. Under an unnameable cref a
129
+ # bare/`self::` declaration pushes `#<singleton>::Name` — still unnameable — so the body
130
+ # keeps the OPAQUE self, the cref flag, and the ENCLOSING prefix: `child_prefix`'s
131
+ # `C::D` would seed `C::D::Foo` write candidates no program can produce, and the only
132
+ # nameable rungs below are the enclosing ones. A nameable header re-anchors at a
133
+ # nameable cref. Returns whether the declaration's body was walked.
134
+ def walk_typed_declaration?(node, qualified_prefix, default_scope, accumulator, self_owner,
135
+ singleton_cref)
136
+ self_base = rebound_self_base(self_owner)
137
+ self_base = [] if self_base == OPAQUE_SELF
138
+ self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, self_base)
139
+ child_prefix = self_decl ||
140
+ Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
141
+ return false unless child_prefix && node.body
142
+
143
+ child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
144
+ child_scope = if child_cref
145
+ default_scope
146
+ else
147
+ DeclarationWalk::Rules.scope_entering_declaration(default_scope, node.constant_path)
148
+ end
149
+ walk_constant_writes(node.body, child_cref ? qualified_prefix : child_prefix, child_scope,
150
+ accumulator, child_cref ? OPAQUE_SELF : nil, singleton_cref: child_cref)
151
+ true
152
+ end
153
+
154
+ # The self a `self::`-anchored eval receiver resolves against, given the enclosing
155
+ # body's self — an eval block's own receiver. A String owner splits into its constant
156
+ # segments; an OPAQUE or nil self passes through unchanged — an OPAQUE self stays
157
+ # opaque either way.
158
+ def rebound_self_base(owner)
159
+ owner.is_a?(String) ? owner.split("::") : owner
160
+ end
161
+
162
+ # Recursion into `node`'s children, swapping `self` for the ONE child a self-rebinding call evaluates
163
+ # under a different receiver — its block. Every other child, and every child of a node that rebinds
164
+ # nothing, keeps the `self` it was reached under.
165
+ #
166
+ # The block is identified by its CLASS rather than by identity against `CallNode#block`. These walks
167
+ # recurse over every node kind, so `node` here is the whole `Prism::Node` union and reading a member
168
+ # only a call carries off it is exactly the shape Rigor's own check rejects. The test is equivalent:
169
+ # `rebound` is non-nil only where {.rebound_block_self} already saw a `Prism::BlockNode` in the `block`
170
+ # field, and no other child of a call can be one — a receiver is an expression, arguments sit under
171
+ # `ArgumentsNode`, and a `&blk` pass-through is a `BlockArgumentNode`.
172
+ def walk_constant_write_children(node, qualified_prefix, default_scope, accumulator, self_owner,
173
+ singleton_cref)
174
+ rebound = rebound_block_self(node, qualified_prefix, default_scope, nil, self_owner)
175
+ node.rigor_each_child do |child|
176
+ rebinds_self = rebound && child.is_a?(Prism::BlockNode)
177
+ owner = rebinds_self ? rebound : self_owner
178
+ # A self-rebinding block moves `self` but not `Module.nesting` — the cref flag
179
+ # passes through unchanged, so a bare write inside `Foo.class_eval { … }`
180
+ # under `class <<` still lands on the singleton's table and declines.
181
+ walk_constant_writes(child, qualified_prefix, default_scope, accumulator, owner,
182
+ singleton_cref: singleton_cref)
183
+ end
184
+ end
185
+
186
+ # Issue #705 — the class or module `self` names inside `node`'s BLOCK, or nil when `node` opens no
187
+ # block that rebinds `self` (an ordinary block keeps the `self` it closed over, so a `self::BAR = …`
188
+ # there is still the enclosing declaration's and every walk below this stays as it was).
189
+ #
190
+ # {OPAQUE_SELF} where the new `self` reaches no class or module name: a `class_eval` receiver that is
191
+ # not a static constant path, an {OPAQUE_SELF_BLOCK_CALLS} body, and a `Class.new { … }` /
192
+ # `Module.new` / `Struct.new` / `Data.define` block no enclosing constant write names. Declining there
193
+ # costs a resolution the engine never had; naming the LEXICAL enclosure instead is the guess that fires,
194
+ # because it is the one name Ruby is guaranteed NOT to have written.
195
+ #
196
+ # `meta_owner` is the name such a write DOES give the block's class, threaded one hop down by the
197
+ # caller ({.meta_new_block_owner}) ([#710](https://github.com/rigortype/rigor/issues/710)). It is
198
+ # supplied only by the publication census; the typed walk returns at a `ConstantWriteNode` without
199
+ # descending into its rvalue, so no `self::` write inside one reaches that walk at all.
200
+ #
201
+ # `self_owner` is the enclosing body's self as the walk carries it, unsplit. Every walk asks this at
202
+ # every node it visits, and only an eval call reads the self, so the split waits until one does
203
+ # ([#1502](https://github.com/rigortype/rigor/issues/1502)).
204
+ def rebound_block_self(node, qualified_prefix, default_scope = nil, meta_owner = nil,
205
+ self_owner = nil)
206
+ return nil unless node.is_a?(Prism::CallNode) && node.block.is_a?(Prism::BlockNode)
207
+ return OPAQUE_SELF if OPAQUE_SELF_BLOCK_CALLS.include?(node.name)
208
+ return meta_owner || OPAQUE_SELF if DeclarationWalk::Rules.meta_new_constant_rvalue?(node)
209
+ return nil unless RECEIVER_EVAL_CALLS.include?(node.name)
210
+
211
+ eval_receiver_self(node.receiver, qualified_prefix, default_scope, rebound_self_base(self_owner))
212
+ end
213
+
214
+ # Issue #710 — the qualified name a `Klass = Class.new { … }` gives the block's class, or nil when
215
+ # `node` is not that form. Ruby names the constructed class after the constant it is first assigned to,
216
+ # so `self::X = 7` in the block writes `Klass::X` and nothing about it is opaque. The recognition is
217
+ # {DeclarationWalk::Rules.meta_new_block_body}'s, shared with the block-as-method walk.
218
+ #
219
+ # A constant PATH write (`N::Made = Class.new { … }`) keeps the opaque answer even though that recognition
220
+ # now takes it ([#703](https://github.com/rigortype/rigor/issues/703)). This census keys a path write AS
221
+ # WRITTEN ({.constant_path_write_name}) rather than through the nesting, so naming the block's class here
222
+ # would publish a `self::X = …` inside it under a name Ruby does not give it; declining suppresses the
223
+ # name instead, which is the gradual direction. Moving it belongs with moving the census's own path key.
224
+ #
225
+ # Issue #963 — the `||=` spellings keep the opaque answer for the same reason the path spelling does. This
226
+ # census records every or-write UNPUBLISHABLE (the constant may already hold something else), so naming the
227
+ # block's class here would publish `Const::X` under a name the census has just declined to publish itself.
228
+ # The `.freeze` spelling is a plain `ConstantWriteNode` and is taken; its tail is threaded through by
229
+ # {.walk_constant_write_census}.
230
+ def meta_new_block_owner(node, qualified_prefix)
231
+ return nil unless node.is_a?(Prism::ConstantWriteNode) && DeclarationWalk::Rules.meta_new_block_body(node)
232
+
233
+ qualified_write_name(qualified_prefix, node.name.to_s)
234
+ end
235
+
236
+ # The `self` a `Recv.class_eval { … }` block runs under. An implicit or literal `self` receiver leaves
237
+ # `self` where it was — nil, so nothing about the walk changes. A static constant path resolves the way
238
+ # a path write's own namespace resolves ({.resolved_write_namespace}); anything else is opaque. A nil
239
+ # `default_scope` is the publication census, which has no class knowledge to resolve through and keys a
240
+ # path AS WRITTEN, so the receiver keeps its spelling there too.
241
+ def eval_receiver_self(receiver, qualified_prefix, default_scope, self_prefix = nil)
242
+ return nil if receiver.nil? || receiver.is_a?(Prism::SelfNode)
243
+
244
+ # `self::X` anchors to the enclosing self — `self_prefix` carries the eval-derived
245
+ # owner inside an eval body, so `Y.class_eval { self::X.class_eval { include T } }`
246
+ # still resolves `Y::X`; outside one it is the lexical prefix either way. An
247
+ # unnameable enclosing self keeps the answer opaque.
248
+ if (tail = DeclarationWalk::Rules.self_anchored_tail(receiver))
249
+ return OPAQUE_SELF if self_prefix == OPAQUE_SELF
250
+
251
+ prefix = self_prefix || qualified_prefix
252
+ return DeclarationWalk::Rules.collapse_object_owner(prefix + tail).join("::")
253
+ end
254
+
255
+ name = Source::ConstantPath.qualified_name_or_nil(receiver)
256
+ return OPAQUE_SELF if name.nil?
257
+ return name if default_scope.nil? || qualified_prefix.empty? || Source::ConstantPath.rooted?(receiver)
258
+
259
+ resolved_write_namespace(name, qualified_prefix, default_scope)
260
+ end
261
+
262
+ # Issue #690 — `enclosing_prefix` is the lexical declaration prefix the write SITS UNDER, `full` the
263
+ # qualified name it WRITES. The two coincide for a bare `BAR = …` and diverge for a `Foo::BAR = …`,
264
+ # which is why they are separate parameters. One parameter served both roles, so the path form had to
265
+ # pass it EMPTY to keep the caller-supplied name intact — and that lost the key's qualification and the
266
+ # rvalue's lexical context together, in the same argument.
267
+ #
268
+ # Issue #705 — and the rvalue needs BOTH halves of the context separately as well, because a
269
+ # `class_eval` block moves one and not the other. `Module.nesting` stays lexical, so a bare `Post` in
270
+ # `Other.class_eval { … }` inside `module Admin` is still `Admin::Post`; `self` becomes the RECEIVER, so
271
+ # `self` and an implicit-self call in the same block answer on `Other`. Reading the self type off
272
+ # `enclosing_prefix` typed `self::REF = self` as `singleton(Admin)` and `self::V = build` through
273
+ # `Admin.build` — wrong on correct code, and reachable at a reader the moment the key stopped being a
274
+ # guess. `self_owner` is a String exactly when the walk resolved a rebound `self`.
275
+ def record_constant_write(node, enclosing_prefix, default_scope, accumulator, full, self_owner = nil)
276
+ owner = self_owner.is_a?(String) ? self_owner : enclosing_prefix.join("::")
277
+ body_scope = default_scope
278
+ unless owner.empty?
279
+ body_scope = CensusScope.census_body_scope(default_scope, enclosing_prefix,
280
+ Type::Combinator.singleton_of(owner))
281
+ end
282
+ rvalue_type = meta_new_constant_type(node, full) ||
283
+ EmptySeed.erase(node.value, body_scope.type_of(node.value))
284
+ existing = accumulator[full]
285
+ accumulator[full] = existing ? Type::Combinator.union(existing, rvalue_type) : rvalue_type
286
+ end
287
+
288
+ # The accumulator key for a `Foo::BAR = …`. Ruby resolves the path's NAMESPACE through the nesting at
289
+ # the write site, so `Holder::DEFAULT = …` inside `module Admin` names `Admin::Holder::DEFAULT`
290
+ # whenever `Admin::Holder` is a class or module some source declares. Filing the entry under the
291
+ # as-written spelling put it where no read of the resolved name reaches it: a read spelled
292
+ # `Admin::Holder::DEFAULT` answered `Dynamic[top]`, and the in-namespace spelling fell past the
293
+ # qualified rung of the lexical ladder to the bare one, hit the mis-keyed entry, and got a top-level
294
+ # constant of the same name — `call.undefined-method` on correct code ([#690](https://github.com/rigortype/rigor/issues/690)).
295
+ #
296
+ # Two static forms keep the as-written key because their namespace is not the enclosing one: a write at
297
+ # the top level, and a rooted `::Foo::BAR`, which names the top level by definition. Re-qualifying a
298
+ # namespace the code does not name would file the entry under a guess.
299
+ #
300
+ # A target that renders no static path is NOT a third such form, and must not be handled by rendering
301
+ # it leniently. `self::BAR = …` names whatever `self` is at that point — the enclosing declaration in
302
+ # an ordinary body, the receiver inside a `class_eval` block ([#705](https://github.com/rigortype/rigor/issues/705)),
303
+ # which is why `self_owner` travels with the walk rather than being read off `qualified_prefix`. Any
304
+ # other dynamic base (`klass::BAR`) is attributable to no namespace at all and is DECLINED:
305
+ # `qualified_name`'s lenient render drops the dynamic segment and yields the bare trailing name, so
306
+ # recording it filed a value under a name the write never touched — and once the rvalue is typed under
307
+ # the enclosing nesting, that bare key outranks an RBS top-level constant declaration and hands every
308
+ # reader of the top-level name a receiver Ruby never names there.
309
+ def constant_path_write_key(target, qualified_prefix, default_scope, self_owner = nil)
310
+ return Source::ConstantPath.qualified_name(target) if Source::ConstantPath.rooted?(target)
311
+
312
+ written = Source::ConstantPath.qualified_name_or_nil(target)
313
+ return dynamic_base_write_key(target, qualified_prefix, self_owner) if written.nil?
314
+ return written if qualified_prefix.empty?
315
+
316
+ namespace, _, base = written.rpartition("::")
317
+ return written if namespace.empty?
318
+
319
+ "#{resolved_write_namespace(namespace, qualified_prefix, default_scope)}::#{base}"
320
+ end
321
+
322
+ # The key for a path write whose base is not a static constant path: the name `self` carries for a
323
+ # `self::BAR = …`, and nil — decline to record the write at all — for every other dynamic base.
324
+ def dynamic_base_write_key(target, qualified_prefix, self_owner = nil)
325
+ return nil unless (tail = DeclarationWalk::Rules.self_anchored_tail(target))
326
+
327
+ self_write_name(qualified_prefix, self_owner, tail.join("::"))
328
+ end
329
+
330
+ # The qualified name a `self::BAR = …` writes. `self_owner` is nil in the ordinary case — `self` is the
331
+ # enclosing declaration, so the write takes the key a bare `BAR = …` in the same body takes — a class or
332
+ # module name where a `class_eval`-family block rebound `self` to one, and {OPAQUE_SELF} where it
333
+ # rebound `self` to something unnameable, which declines the write (nil) rather than filing it under a
334
+ # namespace Ruby did not touch ([#705](https://github.com/rigortype/rigor/issues/705)).
335
+ #
336
+ # `Object` is the one owner that is not a namespace of its own: its constants ARE the top-level ones, and
337
+ # every other census key spells those bare. Keying `Object::T` would file the write beside a sibling's
338
+ # plain `T = 2` instead of on top of it, so the conflict between them goes undetected and one of the two
339
+ # values publishes — the direction this whole issue is about.
340
+ def self_write_name(qualified_prefix, self_owner, base)
341
+ return qualified_write_name(qualified_prefix, base) if self_owner.nil?
342
+ return nil if self_owner.equal?(OPAQUE_SELF)
343
+ return base if self_owner == OBJECT_OWNER
344
+
345
+ "#{self_owner}::#{base}"
346
+ end
347
+
348
+ # The namespace of a `Foo::BAR = …`, resolved the way a READ of the same spelling resolves it: the
349
+ # first `<nesting entry>::Foo` that names a known class or module, innermost first, and the as-written
350
+ # name when none does. Only class/module knowledge participates — a value constant owns no constants —
351
+ # and the ancestor rung {Reflection.resolve_constant_type} walks is deliberately skipped, so a
352
+ # namespace no source declares keeps the key it has today rather than moving to a guess.
353
+ def resolved_write_namespace(namespace, qualified_prefix, scope)
354
+ CensusScope.census_nesting(scope, qualified_prefix).each do |entry|
355
+ candidate = "#{entry}::#{namespace}"
356
+ return candidate if known_namespace?(candidate, scope)
357
+ end
358
+ namespace
359
+ end
360
+
361
+ # True when `name` is a class or module some source declares: a project declaration in
362
+ # `discovered_classes` or an RBS-known class object. The [#528](https://github.com/rigortype/rigor/issues/528)
363
+ # synthesized namespace prefixes reach that table for THIS FILE only — the runner's cross-file seed
364
+ # (`discovered_project_index_for_paths`) returns `ClassDeclarations.collect_class_decls`'s raw declarations
365
+ # without the synthesis its sibling `discovered_classes_for_paths` applies — so a namespace known only through
366
+ # ANOTHER file's COMPACT declaration (`class Admin::Holder::Inner`) does not move the key, where an explicit
367
+ # `module Admin; class Holder` there does. Under-resolution, the direction this probe may err in.
368
+ #
369
+ # The RBS half needs no dependency edge — the loaded signature set is part of the run fingerprint — but
370
+ # both project answers do; see {.record_namespace_probe}.
371
+ def known_namespace?(name, scope)
372
+ if scope.discovered_classes.key?(name)
373
+ record_namespace_probe(name, scope, hit: true)
374
+ return true
375
+ end
376
+
377
+ environment = scope.environment
378
+ return true if !environment.nil? && !environment.singleton_for_name(name).nil?
379
+
380
+ record_namespace_probe(name, scope, hit: false)
381
+ false
382
+ end
383
+
384
+ # ADR-46 — the key this census files a path write under is a function of ANOTHER file's class
385
+ # declarations, and no edge the READER records covers it: a reference's negative key is
386
+ # `class:<last segment>` of the name that failed to resolve, whereas here the write's key moves because
387
+ # a MIDDLE segment appeared (`Holder::DEFAULT` inside `module Admin` starts naming
388
+ # `Admin::Holder::DEFAULT` the moment some file declares `Admin::Holder`). Both directions therefore
389
+ # need their own edge, recorded where the probe happens: a POSITIVE one on the file declaring the
390
+ # namespace we resolved through, so deleting it moves the key back, and a NEGATIVE `class:` one on
391
+ # every candidate that missed, so declaring it later moves the key forward. Without them a warm
392
+ # incremental run serves a cached answer computed against a namespace that has since appeared. Gated on
393
+ # the recorder, which is off on every ordinary run.
394
+ def record_namespace_probe(name, scope, hit:)
395
+ return unless Analysis::DependencyRecorder.active?
396
+
397
+ if hit
398
+ scope.discovered_class_sources[name]&.each { |site| Analysis::DependencyRecorder.read_site(site) }
399
+ else
400
+ Analysis::DependencyRecorder.read_missing(:class, name.split("::").last)
401
+ end
402
+ end
403
+
404
+ # Survey item (e): when the rvalue is a recognised `Module.new do ... end` / `Class.new do ... end` /
405
+ # `Struct.new(*sym) do ... end` / `Data.define(*sym) do ... end` form, type the named constant as
406
+ # `Singleton[<full>]` so the discovered-method table registered under `full` becomes reachable through
407
+ # singleton-side dispatch (`Const.[]=` etc.). Returns nil for non-meta-new rvalues so the caller falls back to
408
+ # the default `body_scope.type_of(node.value)` shape.
409
+ def meta_new_constant_type(node, full)
410
+ return nil unless DeclarationWalk::Rules.meta_new_block_body(node)
411
+
412
+ Type::Combinator.singleton_of(full)
413
+ end
414
+
415
+ # Issue #644 — the cross-file VALUE-constant pre-pass, the twin of {ScopeIndexer.record_class_sources} for plain
416
+ # constant ASSIGNMENTS. Returns one file's **publication census**: `{qualified name => descriptor}`,
417
+ # where the descriptor is `[literal]` (a publishable frozen scalar), {CONSTANT_MEMO}, or
418
+ # {CONSTANT_UNPUBLISHABLE}.
419
+ #
420
+ # EVERY constant write is censused, whatever its rvalue and whatever its form, because the census is
421
+ # four things at once and only the first cares about the value:
422
+ #
423
+ # 1. the published table ({ScopeIndexer.finalize_constant_writes}: a name publishes only when exactly one file
424
+ # writes it and that write is a literal),
425
+ # 2. the attribution the ADR-46 positive edge reads,
426
+ # 3. the producer whose per-file DIFF re-checks readers on an incremental run
427
+ # ({Analysis::Incremental.changed_constant_publications}), and
428
+ # 4. the writes a constant compound write finds its binding among when its plain read resolves nothing
429
+ # ({Scope#bound_constant_names}), which is why a `||=`-only name carries its own descriptor.
430
+ #
431
+ # A write the census cannot see does not merely lose precision — it silently bypasses the conflict rule
432
+ # and publishes a value the program does not have. That is why the operator / multi-assign / chained /
433
+ # `self::` forms below are censused as unpublishable rather than skipped.
434
+ #
435
+ # **Why only Symbol / Integer / Float / `true` / `false` publish.** Publishing turns `Dynamic[top]` into a
436
+ # real type at every reader in the project, which is precisely where a wrong answer becomes a diagnostic
437
+ # on correct code (AGENTS.md § "Implementation Guidelines"), so the recognised set is the one whose
438
+ # precision is worth that risk.
439
+ #
440
+ # - `String` / `Array` / `Hash` literals are MUTABLE. A sibling file's `PATH << "/x"` or `LIST.push(...)`
441
+ # moves the value this table pinned, and only the DECLARING file's
442
+ # `MutatedCarrierWidening.widen_mutated_constants` census can see such a mutation. The composite two
443
+ # additionally carry the `HashShape` / `Tuple` hazard
444
+ # {PreEvalConstants} documents (a closed `HashShape` makes `CONFIG.fetch(:b)` fire; a `Tuple` routes
445
+ # through `ShapeDispatch` instead of the RBS overload a call site relied on).
446
+ # - `nil` declines for {PreEvalConstants}' reason: at declaration position it is a placeholder for a
447
+ # value assigned later far more often than it is a genuine `NilClass`.
448
+ # - `true` / `false` DO publish. They are branched on rather than dispatched to, so the value would fuel
449
+ # `flow.always-truthy-condition` at every `if FEATURE_FLAG` in another file; that is withheld at the
450
+ # rule instead ({Analysis::CheckRules::PublishedConstantGuard}), which is the repo's own answer to a
451
+ # value that is real but must not be concluded from, and it keeps the treatment of every published
452
+ # kind the same.
453
+ #
454
+ # Everything else — a method call, a constant alias, `ENV[...]`, a `Data.define` — is not a literal, so
455
+ # the reader keeps today's `Dynamic[top]`. Declining is always the safe answer.
456
+ #
457
+ # A name written TWICE in one file is unpublishable too, which is what makes a conditional
458
+ # `X = :a if c` / `X = :b` pair gradual rather than pinned to whichever arm the walk saw first.
459
+ def constant_writes_for_file(root) = constant_write_census(root).writes
460
+
461
+ # The tables one file's publication census fills in a single walk, carried together so the walk's two
462
+ # `self`-tracking parameters stay within the signature budget. `writes` is the census itself; `seen`
463
+ # the names already recorded, so a repeat write retracts whatever either rvalue was; `declared` the
464
+ # subset whose write NAMES its target, which is the only half {Scope#local_constant_names} may exempt
465
+ # ([#710](https://github.com/rigortype/rigor/issues/710)).
466
+ # `aliases` is `{qualified target name => the names its rvalue can resolve to}` ({.constant_alias_source})
467
+ # for the one unpublishable rvalue shape whose provenance is still knowable
468
+ # ([#667](https://github.com/rigortype/rigor/issues/667)):
469
+ # a plain `MODE2 = AppConfig::MODE`. The census cannot publish a VALUE for it — resolving the source
470
+ # is the typed walk's job, not this syntactic one — but it can record that the name is a rename of
471
+ # another, which is all the withholding guard needs. `write_census` is issue #1367's {GlobalWriteCensus}, fed
472
+ # the same nodes; a node outside every named `class` / `module` body counts as top-level.
473
+ CensusTables = Data.define(:writes, :seen, :declared, :aliases, :write_census)
474
+ private_constant :CensusTables
475
+
476
+ def constant_write_census(root)
477
+ tables = CensusTables.new(writes: {}, seen: Set.new, declared: Set.new, aliases: {},
478
+ write_census: GlobalWriteCensus::Collector.new(InEffectRefinements.new(root)))
479
+ walk_constant_write_census(root, [], tables)
480
+ tables
481
+ end
482
+
483
+ # Mirrors {.walk_constant_writes}'s traversal for the forms it shares — the lexical class/module prefix
484
+ # qualifies a bare `ConstantWriteNode` — and extends it to the forms that walk misses, each censused as
485
+ # unpublishable: an operator / `&&=` / `||=` constant write, a constant target wherever Prism holds one
486
+ # (see {.census_constant_target}), a `self::X =` whose target renders no qualified name, and the inner
487
+ # write of a chain (`C = D = :x`), which is why a `ConstantWriteNode` descends into its own rvalue instead
488
+ # of returning.
489
+ def walk_constant_write_census(node, qualified_prefix, tables, self_owner = nil, meta_owner = nil,
490
+ singleton_cref: false)
491
+ return unless node.is_a?(Prism::Node)
492
+
493
+ # #1154 — the class whose instance `def`s the ivar-writer census credits; none under `class << self`.
494
+ ivar_owner = census_ivar_owner(qualified_prefix, self_owner, singleton_cref)
495
+ case node
496
+ when Prism::SingletonClassNode
497
+ # #1154 — a `class << self` inside an instance `def` opens the instance's singleton class.
498
+ tables.write_census.visit(node, top_level: qualified_prefix.empty?, owner: ivar_owner)
499
+ return walk_census_singleton_class(node, qualified_prefix, tables, self_owner,
500
+ meta_owner, singleton_cref)
501
+ when Prism::ClassNode, Prism::ModuleNode
502
+ return walk_census_declaration(node, qualified_prefix, tables, self_owner, meta_owner,
503
+ singleton_cref)
504
+ else
505
+ census_constant_write(node, qualified_prefix, tables, self_owner, singleton_cref: singleton_cref)
506
+ tables.write_census.visit(node, top_level: qualified_prefix.empty?, owner: ivar_owner,
507
+ receiver_owner: census_receiver_owner(node, qualified_prefix, self_owner))
508
+ end
509
+
510
+ rebound = rebound_block_self(node, qualified_prefix, nil, meta_owner, self_owner)
511
+ # A `ConstantWriteNode`'s only child is its rvalue, so this reaches exactly the call whose block the
512
+ # constant names — and nil everywhere else, leaving every other descent as it was.
513
+ #
514
+ # Issue #963 — except where a `.freeze` tail sits BETWEEN the two. The write names the class one hop
515
+ # further down than it used to, so the name is carried through the tail rather than dropped at it;
516
+ # otherwise the factory call arrives with no owner and {.rebound_block_self} answers {OPAQUE_SELF},
517
+ # suppressing every `self::X = …` the block publishes. Only a tail passes the name on: a nil
518
+ # `child_meta_owner` at any other node still means "this node names nothing".
519
+ # Under an unnameable cref only a definite path write (`::K`, `C::K`) still names
520
+ # its class; a bare `Klass = Class.new` keeps the opaque self instead of a
521
+ # fabricated lexical owner.
522
+ child_meta_owner =
523
+ if singleton_cref
524
+ meta_new_owner_under_cref(node, qualified_prefix)
525
+ else
526
+ meta_new_block_owner(node, qualified_prefix)
527
+ end
528
+ child_meta_owner ||= meta_owner unless DeclarationWalk::Rules.unwrap_freeze_tail(node).equal?(node)
529
+ node.rigor_each_child do |child|
530
+ rebinds_self = rebound && child.is_a?(Prism::BlockNode)
531
+ owner = rebinds_self ? rebound : self_owner
532
+ # `Module.nesting` never rebinds — the cref flag passes through a self-rebinding
533
+ # block unchanged while `self` (and `self::` resolution) follows the receiver.
534
+ walk_constant_write_census(child, qualified_prefix, tables, owner, child_meta_owner,
535
+ singleton_cref: singleton_cref)
536
+ end
537
+ end
538
+
539
+ # #1154 — the class whose instance `def`s the ivar-writer census credits: the class a `Struct.new` /
540
+ # `Data.define` / `Class.new` constant or a `K.class_eval` block opens (`self_owner`), else the lexical class;
541
+ # none under `class << self` or an opaque block. The prefix Array is joined only when an entry is recorded.
542
+ def census_ivar_owner(qualified_prefix, self_owner, singleton_cref)
543
+ return self_owner if self_owner.is_a?(String)
544
+ return nil if self_owner || singleton_cref || qualified_prefix.empty?
545
+
546
+ qualified_prefix
547
+ end
548
+
549
+ # Issue #2488 — the class an explicit-receiver writer call (`E.attr_writer :w`, `E.define_method(:m) { … }`)
550
+ # names, resolved as a `class_eval` receiver is in this census (keyed as written), or nil.
551
+ def census_receiver_owner(node, qualified_prefix, self_owner)
552
+ receiver = ReceiverWriterCall.receiver(node)
553
+ return nil if receiver.nil?
554
+
555
+ name = eval_receiver_self(receiver, qualified_prefix, nil, rebound_self_base(self_owner))
556
+ name if name.is_a?(String)
557
+ end
558
+
559
+ # Under an unnameable cref a bare/`self::` header pushes `#<singleton>::Name` — still
560
+ # unnameable — so the body keeps the OPAQUE self and the cref flag; nameable
561
+ # headers re-anchor at a real cref.
562
+ def walk_census_declaration(node, qualified_prefix, tables, self_owner, meta_owner,
563
+ singleton_cref)
564
+ rebound_self = meta_owner || self_owner
565
+ self_base =
566
+ case rebound_self
567
+ when String then [rebound_self]
568
+ when Symbol then [] # OPAQUE_SELF — a `self::` header names nothing
569
+ end
570
+ # Issue #3407 — the header, every child but the body, runs under the enclosing cref before the body opens:
571
+ # a write in a dynamic header path (`class (A = M)::B`) or a superclass expression (`class C < (A = Base)`).
572
+ node.rigor_each_child do |child|
573
+ next if child.equal?(node.body)
574
+
575
+ walk_constant_write_census(child, qualified_prefix, tables, self_owner, meta_owner,
576
+ singleton_cref: singleton_cref)
577
+ end
578
+ self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, self_base)
579
+ child_prefix = self_decl ||
580
+ Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
581
+ return unless child_prefix && node.body
582
+
583
+ child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
584
+ # The body keeps the enclosing prefix, not `child_prefix`: `#<singleton>::D` is a
585
+ # real cref rung, but the only NAMEABLE rungs below it are the enclosing ones —
586
+ # `Foo::BAR` inside resolves through `C::Foo`/`::Foo`, never a spelled `C::D::Foo`.
587
+ walk_constant_write_census(node.body, child_cref ? qualified_prefix : child_prefix, tables,
588
+ child_cref ? OPAQUE_SELF : nil, singleton_cref: child_cref)
589
+ end
590
+
591
+ # `self` inside a `class <<` body is the singleton: `self`-anchored and bare write
592
+ # targets land on its constant table (unnameable, so both decline) and a
593
+ # `self::`-anchored eval receiver raises NameError at runtime, so the body is entered
594
+ # with an OPAQUE self. The lexical cref below it is unnameable too — the flag only
595
+ # lifts at a `::`-rooted class/module header.
596
+ def walk_census_singleton_class(node, qualified_prefix, tables, self_owner, meta_owner,
597
+ singleton_cref)
598
+ walk_constant_write_census(node.expression, qualified_prefix, tables, self_owner,
599
+ meta_owner, singleton_cref: singleton_cref)
600
+ return unless node.body
601
+
602
+ walk_constant_write_census(node.body, qualified_prefix, tables, OPAQUE_SELF,
603
+ meta_owner, singleton_cref: true)
604
+ end
605
+
606
+ # Censuses `node` when it is any constant-assigning form. Only a plain `ConstantWriteNode` /
607
+ # `ConstantPathWriteNode` can contribute a VALUE; every other form is recorded unpublishable, which
608
+ # suppresses publication of the name exactly as a second file's write would.
609
+ # Writes inside a `class <<` body (`singleton_cref`) to a bare or `self`-anchored
610
+ # target land on the singleton's own constant table — a name nothing else can
611
+ # produce — so they record nothing rather than fabricating an enclosing-class name
612
+ # or a wildcard retraction for a constant they cannot touch.
613
+ def census_constant_write(node, qualified_prefix, tables, self_owner = nil,
614
+ singleton_cref: false)
615
+ case node
616
+ when Prism::ConstantWriteNode
617
+ return if singleton_cref
618
+
619
+ record_constant_write_census(qualified_write_name(qualified_prefix, node.name.to_s),
620
+ constant_literal_value(node.value), tables,
621
+ alias_of: constant_alias_source(node.value, qualified_prefix))
622
+ when Prism::ConstantPathWriteNode
623
+ return if singleton_cref && singleton_self_write?(node.target, self_owner)
624
+
625
+ record_constant_write_census(constant_path_write_name(node.target, qualified_prefix, self_owner),
626
+ constant_path_write_literal(node, self_owner), tables,
627
+ nameable: nameable_write_target?(node.target, self_owner),
628
+ alias_of: constant_alias_source(node.value, qualified_prefix))
629
+ when Prism::ConstantOperatorWriteNode, Prism::ConstantOrWriteNode, Prism::ConstantAndWriteNode
630
+ return if singleton_cref
631
+
632
+ record_constant_write_census(qualified_write_name(qualified_prefix, node.name.to_s), nil, tables,
633
+ memo: node.is_a?(Prism::ConstantOrWriteNode))
634
+ when Prism::ConstantPathOperatorWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathAndWriteNode
635
+ census_path_write(node, qualified_prefix, tables, self_owner, singleton_cref: singleton_cref)
636
+ when Prism::ConstantTargetNode, Prism::ConstantPathTargetNode
637
+ census_constant_target(node, qualified_prefix, tables, self_owner, singleton_cref: singleton_cref)
638
+ end
639
+ end
640
+
641
+ def census_path_write(node, qualified_prefix, tables, self_owner, singleton_cref: false)
642
+ return if singleton_cref && singleton_self_write?(node.target, self_owner)
643
+
644
+ record_constant_write_census(constant_path_write_name(node.target, qualified_prefix, self_owner),
645
+ nil, tables,
646
+ nameable: nameable_write_target?(node.target, self_owner),
647
+ memo: node.is_a?(Prism::ConstantPathOrWriteNode))
648
+ end
649
+
650
+ # A `self`-anchored write target under a self the walk cannot name — inside `class <<`
651
+ # the read raises NameError or lands on the singleton's table, never on a constant the
652
+ # census could spell, so it declines rather than laying a wildcard retraction down.
653
+ def singleton_self_write?(target, self_owner)
654
+ self_owner == OPAQUE_SELF && !DeclarationWalk::Rules.self_anchored_tail(target).nil?
655
+ end
656
+
657
+ # Issue #3407 — a constant target, reached by the walk's ordinary descent wherever Prism holds one: a
658
+ # multiple assignment's lefts, rights and splat (`A, *B = …`), a nested target list (`(A, B), c = …`), a
659
+ # `for` index (`for A in …`) and a `rescue => A` reference. Prism's node set puts a target in no other
660
+ # position: a pattern capture and a block parameter bind locals only. A target's value is a projection or
661
+ # a runtime value this syntactic walk does not evaluate, so every one is censused unpublishable. A path
662
+ # target is keyed by {.constant_path_write_name}, which renders no `ConstantPathTargetNode` and so files
663
+ # it under the wildcard key, the over-retracting direction.
664
+ def census_constant_target(target, qualified_prefix, tables, self_owner = nil, singleton_cref: false)
665
+ if target.is_a?(Prism::ConstantTargetNode)
666
+ return if singleton_cref
667
+
668
+ record_constant_write_census(qualified_write_name(qualified_prefix, target.name.to_s), nil, tables)
669
+ else
670
+ return if singleton_cref && singleton_self_write?(target, self_owner)
671
+
672
+ record_constant_write_census(constant_path_write_name(target, qualified_prefix, self_owner),
673
+ nil, tables, nameable: nameable_write_target?(target, self_owner))
674
+ end
675
+ end
676
+
677
+ # The qualified name a constant-path write targets, for the PUBLICATION census only. `Foo::BAR = …`
678
+ # renders as written — deliberately NOT what {.constant_path_write_key} answers for the typed table,
679
+ # which resolves the namespace through the enclosing nesting
680
+ # ([#690](https://github.com/rigortype/rigor/issues/690)). Moving this one changes which name a value
681
+ # publishes to the whole project, an over-suppression question with its own safety story, so the two
682
+ # censuses key a path write differently until that is settled.
683
+ # `self::BAR = …` names whatever `self` is at that point, which the walk carries in `self_owner`
684
+ # ([#705](https://github.com/rigortype/rigor/issues/705)) — the enclosing lexical namespace in an
685
+ # ordinary body, the receiver inside a `class_eval` block, and the constant a `Klass = Class.new { … }`
686
+ # assigns the block's class to ({.meta_new_block_owner}). Where that `self` is {OPAQUE_SELF}, and for
687
+ # any other dynamic receiver (`klass::BAR = …`), the target is "some class, then `::BAR`" and the name
688
+ # falls back to the WILDCARD {DYNAMIC_TARGET_PREFIX} key, which {ScopeIndexer.finalize_constant_writes} expands
689
+ # into a retraction of every censused name with that last segment
690
+ # ([#668](https://github.com/rigortype/rigor/issues/668)). The bare last segment — the pre-#668
691
+ # fallback — was the single name such a write can never create, so filing it there suppressed the one
692
+ # name the form cannot touch and left published every name it can. The VALUE is what such a write must
693
+ # not contribute either ({.constant_path_write_literal}), and the fallback name is withheld from the
694
+ # local-declaration exemption ({ScopeIndexer.local_constant_name_set}).
695
+ def constant_path_write_name(target, qualified_prefix, self_owner = nil)
696
+ full = Source::ConstantPath.qualified_name_or_nil(target)
697
+ return full if full
698
+
699
+ base = target.name&.to_s
700
+ return nil if base.nil?
701
+ return dynamic_target_key(base) unless (tail = DeclarationWalk::Rules.self_anchored_tail(target))
702
+
703
+ self_write_name(qualified_prefix, self_owner, tail.join("::")) || dynamic_target_key(base)
704
+ end
705
+
706
+ def dynamic_target_key(segment) = "#{DYNAMIC_TARGET_PREFIX}#{segment}"
707
+
708
+ # The publishable literal a path write contributes — none, whenever its base is not statically
709
+ # nameable ([#705](https://github.com/rigortype/rigor/issues/705)). `[Foo].each { |k| k::X = 1 }`
710
+ # renders under the wildcard key, and publishing `1` there handed every reader of a project `X` a value
711
+ # the program never has: `X == 2` folded to `false`, and in the WRITING file — where
712
+ # `Scope#local_constant_names` exempts the name from #644's withholding guard —
713
+ # `flow.always-truthy-condition` then fired on correct code.
714
+ #
715
+ # The NAME still enters the census, because a decline means opposite things in the two censuses. In the
716
+ # typed one, recording no type is gradual. Here, recording nothing would leave ANOTHER file's value for
717
+ # the name trusted — silence is the risky direction, and the census's own gradual answer is to say the
718
+ # name was touched and no value for it is safe.
719
+ def constant_path_write_literal(node, self_owner)
720
+ return nil unless nameable_write_target?(node.target, self_owner)
721
+
722
+ constant_literal_value(node.value)
723
+ end
724
+
725
+ # True when the census can name what the write targets: a static constant path, or a `self::BAR` whose
726
+ # `self` the walk resolved to a class or module.
727
+ def nameable_write_target?(target, self_owner)
728
+ return true if Source::ConstantPath.qualified_name_or_nil(target)
729
+ return false unless DeclarationWalk::Rules.self_anchored_tail(target)
730
+
731
+ !self_owner.equal?(OPAQUE_SELF)
732
+ end
733
+
734
+ def qualified_write_name(qualified_prefix, base_name)
735
+ qualified_prefix.empty? ? base_name : "#{qualified_prefix.join('::')}::#{base_name}"
736
+ end
737
+
738
+ # Records one censused write. `seen` carries the names this file has already written, so a repeat write
739
+ # retracts the publishable descriptor whatever either rvalue was — neither arm of an in-file
740
+ # reassignment is ever published, and the name still counts as written.
741
+ #
742
+ # `nameable` is false only for the wildcard fallback {.constant_path_write_name} takes when nothing
743
+ # names the write's base. Such a key belongs in `writes` — another file's value for a name it reaches
744
+ # is not to be trusted — and NOT in `declared`, which answers the opposite question
745
+ # ([#710](https://github.com/rigortype/rigor/issues/710)). The two are separate tables rather than one
746
+ # richer descriptor because a name can be written both ways in one file, and then the file DID declare
747
+ # it however the two writes are ordered.
748
+ #
749
+ # `memo` marks a `||=` write, which files {CONSTANT_MEMO} for as long as every write of the name in this
750
+ # file is one.
751
+ def record_constant_write_census(full, literal, tables, nameable: true, alias_of: nil, memo: false)
752
+ return if full.nil?
753
+
754
+ tables.declared << full if nameable
755
+ first_write = tables.seen.add?(full)
756
+ tables.writes[full] = census_descriptor(tables.writes[full], first_write, literal, memo)
757
+ record_constant_alias_census(full, alias_of, tables, first_write && nameable)
758
+ end
759
+
760
+ def census_descriptor(previous, first_write, literal, memo)
761
+ return literal || (memo ? CONSTANT_MEMO : CONSTANT_UNPUBLISHABLE) if first_write
762
+
763
+ memo && previous == CONSTANT_MEMO ? CONSTANT_MEMO : CONSTANT_UNPUBLISHABLE
764
+ end
765
+
766
+ # Issue #667 — a name written TWICE is not an alias of anything the walk can name, exactly as it is
767
+ # unpublishable above; the `delete` is what retracts a first write's record when the second arrives.
768
+ def record_constant_alias_census(full, alias_of, tables, keep)
769
+ if keep && alias_of
770
+ tables.aliases[full] = alias_of
771
+ else
772
+ tables.aliases.delete(full)
773
+ end
774
+ end
775
+
776
+ # The constant reference an rvalue names, as the names it can resolve to where it is written, or nil when
777
+ # it names none: the spelling behind each `Module.nesting` entry the walk's prefix stands for, innermost
778
+ # first, then the spelling itself, which is the top-level name and every candidate's last segment
779
+ # ([#3437](https://github.com/rigortype/rigor/issues/3437)). A reference written with a leading `::` names
780
+ # the top level alone. `AppConfig::MODE` renders qualified; a path that renders no qualified name falls
781
+ # back to its own last segment, which is all `Scope#published_constant?` can be asked about anyway.
782
+ #
783
+ # The walk is syntactic and has no ancestry to read, so the ancestors `Scope#published_constant?` adds for
784
+ # a reference are not candidates here: a `COPY = LIMIT` whose `LIMIT` the class inherits from a class this
785
+ # file declares reads as an alias of a foreign constant. That is the withholding direction.
786
+ def constant_alias_source(rvalue, qualified_prefix)
787
+ spelling =
788
+ case rvalue
789
+ when Prism::ConstantReadNode then rvalue.name.to_s
790
+ when Prism::ConstantPathNode then Source::ConstantPath.qualified_name_or_nil(rvalue) || rvalue.name&.to_s
791
+ end
792
+ return nil if spelling.nil?
793
+ return [spelling].freeze if Source::ConstantPath.rooted?(rvalue)
794
+
795
+ nesting = DeclarationWalk::Rules.lexical_nesting_for_prefix(qualified_prefix)
796
+ [*nesting.map { |entry| "#{entry}::#{spelling}" }, spelling].freeze
797
+ end
798
+
799
+ # The frozen-scalar literal fold: `[value]` for a publishable rvalue, nil to decline. The one-element
800
+ # Array is the presence wrapper (`false` is a legitimate published value), and the raw Ruby value is
801
+ # what rides the ADR-85 seed bundle through `Marshal`.
802
+ def constant_literal_value(rvalue)
803
+ case rvalue
804
+ when Prism::SymbolNode then symbol_literal_publication(rvalue)
805
+ when Prism::IntegerNode, Prism::FloatNode then [rvalue.value]
806
+ when Prism::TrueNode then [true]
807
+ when Prism::FalseNode then [false]
808
+ end
809
+ end
810
+
811
+ # `Prism::SymbolNode` is the STATIC symbol node (a dynamic `:"#{x}"` parses as
812
+ # `InterpolatedSymbolNode` and never reaches here), so its unescaped text is the whole value.
813
+ def symbol_literal_publication(node)
814
+ text = node.unescaped
815
+ text.is_a?(String) ? [text.to_sym] : nil
816
+ end
817
+
818
+ # The class name a meta-new write under an unnameable cref still assigns for the OWNER
819
+ # tables (mixins, census) — only a definite `A::K = …` / `::K = …` write, matching
820
+ # {.meta_new_block_owner}'s plain-write requirement for the same #963 reason (an `||=`
821
+ # may never run the rvalue). Nil where the write names nothing.
822
+ def meta_new_owner_under_cref(node, qualified_prefix, self_base = nil)
823
+ return nil unless node.is_a?(Prism::ConstantPathWriteNode)
824
+ return nil unless DeclarationWalk::Rules.meta_new_path_target_nameable?(node, self_base)
825
+
826
+ DeclarationWalk::Rules.meta_new_child_prefix(node, qualified_prefix, self_base)&.join("::")
827
+ end
828
+
829
+ private_class_method :walk_constant_writes, :walk_singleton_class_writes, :walk_typed_declaration?,
830
+ :rebound_self_base, :walk_constant_write_children, :meta_new_block_owner,
831
+ :eval_receiver_self, :record_constant_write, :constant_path_write_key,
832
+ :dynamic_base_write_key, :self_write_name, :resolved_write_namespace, :known_namespace?,
833
+ :record_namespace_probe, :meta_new_constant_type, :constant_writes_for_file,
834
+ :walk_constant_write_census, :census_ivar_owner, :census_receiver_owner,
835
+ :walk_census_declaration, :walk_census_singleton_class, :census_constant_write,
836
+ :census_path_write, :singleton_self_write?, :census_constant_target,
837
+ :constant_path_write_name, :constant_path_write_literal, :nameable_write_target?,
838
+ :qualified_write_name, :record_constant_write_census, :census_descriptor,
839
+ :record_constant_alias_census, :constant_alias_source, :constant_literal_value,
840
+ :symbol_literal_publication, :meta_new_owner_under_cref
841
+ end
842
+ end
843
+ end
844
+ end