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,388 @@
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 "../declaration_walk/rules"
8
+ require_relative "../object_mixins"
9
+ require_relative "constant_tables"
10
+ require_relative "mixin_accumulator"
11
+ require_relative "mixin_effects"
12
+
13
+ module Rigor
14
+ module Inference
15
+ module ScopeIndexer
16
+ # The mixin tables of one file, moved out of `scope_indexer.rb` whole (ADR-116 WD9, #3400): per class or
17
+ # module, the modules it `include`s and the ones it `prepend`s, as written, in instance-ancestor search order
18
+ # (ADR-24 slice 2, #1123, #1173), and the edges whose order is not a fact ({MixinAccumulator}).
19
+ # {.mixin_tables} is the entry: one descent answers the three tables, and the per-file merge
20
+ # (`ScopeIndexer.merge_mixin_tables`) and the cross-file fold (`ScopeIndexer.fold_file_mixin_tables`) call
21
+ # it. The other 16 functions are private: the walk with its arms, the two recording steps, the projection of
22
+ # the accumulator, the owner of a top-level `include` (#1697), and {.build_discovered_includes} and
23
+ # {.build_discovered_prepends}, each one table of the three, which no file of `lib/` calls and the specs reach
24
+ # through `send`.
25
+ #
26
+ # It asks {DeclarationWalk::Rules} for each declaration context, {ConstantTables} for the `self` a block runs
27
+ # under, and {MixinEffects} for what a mixin call reshapes and taints, which the extend walk asks too. It calls
28
+ # two functions left in `scope_indexer.rb`, recognisers the other walkers ask too, at two sites each:
29
+ # `ScopeIndexer.mixin_call_view` and `ScopeIndexer.lexical_self_receiver?`. No constant moved with it. It reads
30
+ # `ScopeIndexer::OPAQUE_SELF`, `INSTANCE_MIXIN_NAMES` and `MIXIN_CALL_NAMES` lexically, as {NodePropagation}
31
+ # reads the indexer's constants: other rows of `scope_indexer.rb` and other modules read each.
32
+ #
33
+ # `scope_indexer.rb` requires this file, and this file does not require it back. It requires the file of what
34
+ # else it names. So the file loads alone, and until `scope_indexer.rb` has loaded the entry answers only for a
35
+ # program with no call but the factory call of a constant write that creates a class: any other call is a
36
+ # `NoMethodError` (`mixin_call_view`).
37
+ module MixinTables
38
+ module_function
39
+
40
+ # ADR-24 slice 2 — per-class/module table mapping a fully qualified user class or module to the list of module
41
+ # names it `include`s / `prepend`s, AS WRITTEN at the mixin call (`include Foo` / `include Foo::Bar`). Only
42
+ # constant arguments are recorded; dynamic mixins (`include some_method`) produce no entry. The names are
43
+ # spelled as written but ordered in instance-ancestor SEARCH order since #1173: prepended modules first
44
+ # (Ruby puts them ahead of the class itself), then includes nearest-first (`include A; include B` searches
45
+ # B first; `include A, B` keeps `["A", "B"]`). `prepend` is ALSO recorded in its own
46
+ # {.build_discovered_prepends} table, which is what tells the two apart at `def`-priority level. `extend` is NOT
47
+ # tracked (it adds singleton methods; ADR-24 slice 2 resolves the instance-side chain).
48
+ def build_discovered_includes(root)
49
+ mixin_tables(root).fetch(:includes)
50
+ end
51
+
52
+ # Issue #1123 — `{qualified class or module name => [module names it `prepend`s, as written]}`,
53
+ # stored in instance-ancestor SEARCH order: a later `prepend` statement puts its module NEARER than
54
+ # an earlier one (`prepend A; prepend B` searches B before A) while the arguments of ONE
55
+ # `prepend A, B` keep call order — exactly the convention {ScopeIndexer.record_extend_targets} documents for
56
+ # `extend`, and the order `Scope::ResolutionChain` reads a body's prepends in. Ruby inserts a
57
+ # prepended module, and its own ancestry, immediately BEFORE the class that prepends it, so these
58
+ # names outrank the class's own `def`s; without the kind recorded separately from {.build_discovered_includes}
59
+ # there was nothing to order on and `prepend` was silently an `include`.
60
+ def build_discovered_prepends(root)
61
+ mixin_tables(root).fetch(:prepends)
62
+ end
63
+
64
+ # One descent, both instance-side mixin tables: the walk classifies each mixin call it sees, so the
65
+ # two tables cost one walk rather than two. Each value is frozen and de-duplicated per class, as the
66
+ # single-table builder always did. `:unpositioned` names the recorded edges whose order is not a fact
67
+ # ({MixinAccumulator}).
68
+ def mixin_tables(root)
69
+ accumulator = MixinAccumulator.new
70
+ accumulator.direct_body(MixinEffects.program_statements(root))
71
+ accumulator.program_body(MixinEffects.program_statements(root))
72
+ walk_class_includes(root, [], nil, accumulator)
73
+ {
74
+ includes: freeze_mixin_lists(accumulator, :include),
75
+ prepends: freeze_mixin_lists(accumulator, :prepend),
76
+ unpositioned: accumulator.unpositioned
77
+ }
78
+ end
79
+
80
+ # The `{include: [...], prepend: [...]}`-valued accumulator {.mixin_tables} fills is keyed by class
81
+ # first and kind second, because one walk feeds both tables; this projects one kind out of it. A class
82
+ # with no names of that kind is DROPPED rather than stored as an empty list, which is the shape the
83
+ # single-table builder always had (consumers take the absence of an entry as "mixes nothing in").
84
+ #
85
+ # Issue #1173 — the `:include` projection prepends the `:prepend` names: a prepended module sits
86
+ # ahead of the class ITSELF in the runtime ancestry, and therefore ahead of every include, so the
87
+ # instance-ancestor search order this table now keeps is `prepends ++ includes`. Both buckets
88
+ # already store nearest-first (see {.write_mixin_targets}), so the concat preserves MRO.
89
+ def freeze_mixin_lists(accumulator, kind)
90
+ accumulator.each_with_object({}) do |(class_name, kinds), out|
91
+ names = mixin_names_for(kinds, kind)
92
+ # The instance-side projection is a SET in search order; the prepend table keeps every statement.
93
+ names = names.uniq if kind == :include
94
+ names = names.freeze
95
+ out[class_name] = names unless names.empty?
96
+ end.freeze
97
+ end
98
+
99
+ # One bucket's frozen list: `:include` is every instance-side mixin in search order (prepends
100
+ # first), `:prepend` the wedge table alone.
101
+ def mixin_names_for(kinds, kind)
102
+ if kind == :include
103
+ (kinds[:prepend] || []) + (kinds[:include] || [])
104
+ else
105
+ kinds[kind] || []
106
+ end
107
+ end
108
+
109
+ def walk_class_includes(node, qualified_prefix, current_class, accumulator,
110
+ singleton_self: false, singleton_cref: false)
111
+ return unless node.is_a?(Prism::Node)
112
+
113
+ case node
114
+ when Prism::ClassNode, Prism::ModuleNode
115
+ return if walk_includes_declaration?(node, qualified_prefix, current_class, accumulator,
116
+ singleton_cref)
117
+ when Prism::SingletonClassNode
118
+ # Issue #728 — `class << self; include M; end` mixes M into the SINGLETON: it contributes class
119
+ # methods, not the instance surface this table feeds. Descending with no owner keeps any nested
120
+ # declaration walked (a `ClassNode` child sets its own owner) while the mixin calls in the
121
+ # singleton body record nothing, which is what `extends` is for — and since #915 the extend walk
122
+ # does take them, so the form is recorded rather than dropped. `self` inside is
123
+ # the singleton — a `self::` receiver below raises NameError at runtime, so it
124
+ # declines rather than resolving against the enclosing class. The `class <<`
125
+ # EXPRESSION itself evaluates in the enclosing context, so it walks without
126
+ # either marker.
127
+ return walk_singleton_class_includes(node, qualified_prefix, current_class, accumulator,
128
+ singleton_self, singleton_cref)
129
+ when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
130
+ Prism::ConstantPathOrWriteNode
131
+ return if walk_includes_meta_new?(node, qualified_prefix, current_class, accumulator,
132
+ singleton_self, singleton_cref)
133
+ when Prism::CallNode
134
+ record_mixin_call(node, qualified_prefix, current_class, accumulator)
135
+ return walk_mixin_call_children(node, qualified_prefix, current_class, accumulator,
136
+ singleton_self, singleton_cref)
137
+ end
138
+
139
+ walk_includes_children(node, qualified_prefix, current_class, accumulator, singleton_self, singleton_cref)
140
+ end
141
+
142
+ # The generic descent of {.walk_class_includes}; a `self.included(base)`-style hook's body walks with
143
+ # its parameters in scope ({MixinAccumulator#with_hook_params}).
144
+ def walk_includes_children(node, qualified_prefix, current_class, accumulator, singleton_self, singleton_cref)
145
+ hook_params = MixinEffects.hook_def_params(node)
146
+ if hook_params
147
+ return accumulator.with_hook_params(hook_params) do
148
+ walk_includes_children_plain(node, qualified_prefix, current_class, accumulator, singleton_self,
149
+ singleton_cref)
150
+ end
151
+ end
152
+
153
+ walk_includes_children_plain(node, qualified_prefix, current_class, accumulator, singleton_self,
154
+ singleton_cref)
155
+ end
156
+
157
+ def walk_includes_children_plain(node, qualified_prefix, current_class, accumulator, singleton_self,
158
+ singleton_cref)
159
+ node.rigor_each_child do |child|
160
+ walk_class_includes(child, qualified_prefix, current_class, accumulator,
161
+ singleton_self: singleton_self, singleton_cref: singleton_cref)
162
+ end
163
+ end
164
+
165
+ # The class/module arm of {.walk_class_includes}: under an unnameable cref a
166
+ # bare/`self::` header opens `#<singleton>::Name` — ownerless; nameable headers
167
+ # re-anchor at a real cref.
168
+ def walk_includes_declaration?(node, qualified_prefix, current_class, accumulator,
169
+ singleton_cref)
170
+ ctx = DeclarationWalk::Rules.decl_body_context(node, qualified_prefix, current_class && [current_class],
171
+ singleton_cref)
172
+ return false unless ctx
173
+ return true unless node.body
174
+
175
+ _self_decl, child_prefix, child_cref = ctx
176
+ accumulator.direct_body(node.body) if accumulator.direct?(node)
177
+ accumulator.class_body(node.body)
178
+ walk_class_includes(node.body, child_cref ? [] : child_prefix,
179
+ child_cref ? nil : child_prefix.join("::"), accumulator,
180
+ singleton_cref: child_cref)
181
+ true
182
+ end
183
+
184
+ # The `class <<` arm of {.walk_class_includes}: the expression evaluates in the enclosing
185
+ # context while the body is the singleton's — ownerless here (`extends` owns its mixins),
186
+ # with `self` unnameable and an unnameable cref.
187
+ def walk_singleton_class_includes(node, qualified_prefix, current_class, accumulator,
188
+ singleton_self, singleton_cref)
189
+ walk_class_includes(node.expression, qualified_prefix, current_class, accumulator,
190
+ singleton_self: singleton_self, singleton_cref: singleton_cref)
191
+ return unless node.body
192
+
193
+ walk_class_includes(node.body, qualified_prefix, nil, accumulator,
194
+ singleton_self: true, singleton_cref: true)
195
+ end
196
+
197
+ # `K = Class.new { include T }` mixes T into the block's class — the nameable `K`, or
198
+ # ownerless when the write lands on an unnameable cref. Returns whether a meta-new block
199
+ # body was found (the write's other children are left to the caller).
200
+ def walk_includes_meta_new?(node, qualified_prefix, current_class, accumulator, singleton_self,
201
+ singleton_cref)
202
+ call = DeclarationWalk::Rules.meta_new_block_call(node)
203
+ return false unless call
204
+
205
+ meta_owner = MixinEffects.meta_new_mixin_owner(node, qualified_prefix, singleton_cref,
206
+ current_class && [current_class])
207
+ # The block rebinds only `self` — `Module.nesting` stays lexical — so the cref
208
+ # flag passes through unchanged: a nested `class Inner` under `class <<` still
209
+ # lands on the singleton's table, while mixin leaves attribute to `meta_owner`.
210
+ body_cref = singleton_cref
211
+ # The factory call's receiver and arguments evaluate in the ENCLOSING context —
212
+ # `K = Class.new(X.class_eval { include T })` still mixes T into X — while the
213
+ # block is the class body of the class the write names.
214
+ if call.receiver
215
+ walk_class_includes(call.receiver, qualified_prefix, current_class, accumulator,
216
+ singleton_self: singleton_self, singleton_cref: singleton_cref)
217
+ end
218
+ call.arguments&.arguments&.each do |arg|
219
+ walk_class_includes(arg, qualified_prefix, current_class, accumulator,
220
+ singleton_self: singleton_self, singleton_cref: singleton_cref)
221
+ end
222
+ if (body = DeclarationWalk::Rules.meta_new_block_body(node))
223
+ walk_class_includes(body, qualified_prefix, meta_owner, accumulator,
224
+ singleton_cref: body_cref)
225
+ end
226
+ true
227
+ end
228
+
229
+ # Issue #728 — a block that REBINDS `self` owns the mixin calls written in it, not the lexically enclosing
230
+ # declaration: `Thing = Class.new { include Taggable }` inside `class Outer` must not register `Taggable` on
231
+ # `Outer`, where `Outer.new.tag` raises `NoMethodError` in MRI. The classification is
232
+ # {ConstantTables.rebound_block_self}, the same one #721 gave its own mixin scan.
233
+ #
234
+ # `Recv.class_eval { include M }` resolves to `Recv` and records there. An OPAQUE rebinding — `Class.new` /
235
+ # `Module.new` / a non-constant `class_eval` receiver — records nothing: the class exists but this walk cannot
236
+ # name it, and naming the lexical enclosure instead is the one answer Ruby is guaranteed not to have written.
237
+ def walk_mixin_call_children(node, qualified_prefix, current_class, accumulator, singleton_self,
238
+ singleton_cref)
239
+ # `self` is unnameable whenever the singleton marker is set OR the body is ownerless
240
+ # under an unnameable cref (`class D` inside `class <<`, a declined eval body) — a
241
+ # `self::` receiver inside either raises NameError or resolves on a table nothing
242
+ # names, so it declines rather than re-anchoring to the lexical class.
243
+ unnameable_self = singleton_self || (singleton_cref && current_class.nil?)
244
+ rebound = ConstantTables.rebound_block_self(node, qualified_prefix, nil, nil,
245
+ unnameable_self ? OPAQUE_SELF : current_class)
246
+ node.rigor_each_child do |child|
247
+ rebinds_self = rebound && child.is_a?(Prism::BlockNode)
248
+ owner =
249
+ if rebinds_self
250
+ rebound == OPAQUE_SELF ? nil : rebound
251
+ else
252
+ current_class
253
+ end
254
+ # A rebound block's own self is the receiver again — nameable only when the rebound
255
+ # is; an OPAQUE rebound keeps the self unnameable — but `Module.nesting` never
256
+ # rebinds, so the unnameable-cref marker crosses the block unchanged.
257
+ child_unnameable = rebinds_self ? rebound == OPAQUE_SELF : unnameable_self
258
+ walk_class_includes(child, qualified_prefix, owner, accumulator,
259
+ singleton_self: child_unnameable, singleton_cref: singleton_cref)
260
+ end
261
+ end
262
+
263
+ # A receiverless `include Foo` / `prepend Foo` written in a declaration body contributes both tables;
264
+ # the receiver form (`Base.prepend(Loud)`, issue #1123) does too — a call form puts the module in the
265
+ # class's instance ancestry exactly as the declaration form does, so `discovered_includes` must carry
266
+ # it or the set-shaped consumers (arity, visibility, undefined-method suppression) would answer
267
+ # differently for the two spellings of one edge.
268
+ def record_mixin_call(node, qualified_prefix, current_class, accumulator)
269
+ MixinEffects.taint_opaque_eval(node, current_class, accumulator)
270
+ view = ScopeIndexer.mixin_call_view(node)
271
+ if view.nil?
272
+ return MixinEffects.taint_computed_mixin_send(node, current_class, accumulator, INSTANCE_MIXIN_NAMES,
273
+ :include)
274
+ end
275
+ return if view.first.nil?
276
+
277
+ kind = view.first
278
+ current_class ||= toplevel_include_owner(node, qualified_prefix, current_class, accumulator)
279
+ effects = MixinEffects.mixin_effects(node, kind, qualified_prefix, current_class, accumulator,
280
+ in_singleton: false)
281
+ .select { |_owner, side| side == :include }
282
+ return if effects.empty?
283
+ return if instance_mixin_recorded?(node, view, qualified_prefix, current_class, accumulator)
284
+
285
+ # A mixin call the tables cannot hold still reshapes the ancestry: taint the whole owner side.
286
+ MixinEffects.taint_unrecorded_mixin(node, kind, view[1], effects, accumulator)
287
+ end
288
+
289
+ # Records the instance-side edges of an `include` / `prepend` call. False when it recorded nothing — the caller
290
+ # then taints the owner instead. `view` is {ScopeIndexer.mixin_call_view}'s, so `Integer.send(:include, Ext)`
291
+ # records `Ext` as `Integer.include(Ext)` does (issue #1722).
292
+ #
293
+ # A receiverless `include` / `prepend` needs an enclosing declaration to file the edge under, and the RECEIVER
294
+ # form (`Base.prepend(Loud)`, issue #1123; `Integer.include(Ext)` and `Integer.send(:include, Ext)`, issue
295
+ # #1722) files it under the class the receiver names. A receiver-form edge is unpositioned wherever it is
296
+ # written ({MixinAccumulator#note}), so a reader that depends on ORDER declines on it, while the set-shaped
297
+ # readers (undefined-method suppression, arity, visibility) see the module as they see an in-body `include`.
298
+ #
299
+ # Issue #2995 — `self.include M`, `self.prepend M` and a receiverless or `self.` `send` / `public_send` /
300
+ # `__send__(:include | :prepend, M)` are the same call on the same `self` as a bare `include M`, so they file
301
+ # the edge under the enclosing class as it does, positioned where it would be ({MixinAccumulator#note}). The
302
+ # walk names that `self` exactly where it names a bare `include`'s: `current_class` is nil wherever it cannot.
303
+ def instance_mixin_recorded?(node, view, qualified_prefix, current_class, accumulator)
304
+ kind, arguments, = view
305
+ return false unless mixin_call_recorded?(kind)
306
+
307
+ targets = arguments.filter_map { |arg| Source::ConstantPath.qualified_name(arg) }
308
+ return false if targets.empty?
309
+
310
+ owner =
311
+ if ScopeIndexer.lexical_self_receiver?(node.receiver)
312
+ current_class
313
+ else
314
+ MixinEffects.prepend_call_receiver(node, qualified_prefix)
315
+ end
316
+ return false if owner.nil?
317
+
318
+ write_mixin_targets(accumulator, owner, targets, prepend: kind == :prepend)
319
+ accumulator.note(node, owner, :include, targets, complete: targets.size == arguments.size)
320
+ true
321
+ end
322
+
323
+ # Issue #1123 — one class's contribution to the two tables. A prepended module lands in BOTH: the
324
+ # include list is the SET of modules a class carries (arity, visibility, reflection and constant-scope
325
+ # consumers read it that way), while the prepend table adds the ORDER and the KIND.
326
+ #
327
+ # Issue #1173 — `include` now stores instance-ancestor SEARCH order too, the same convention
328
+ # `prepend` already keeps: each statement's argument list lands as one unit (`include A, B` keeps
329
+ # `["A", "B"]`, the order Ruby searches them) AHEAD of the earlier statements' (`include A;
330
+ # include B` searches B first). Call order was never a semantic — it is where this list happened
331
+ # to be written — and every order-sensitive consumer (the BFS mixin step, the external-ancestor
332
+ # walk, override visibility) reads nearer-first. A re-`include` of an already-carried name is a
333
+ # runtime no-op (`Module#append_features` is skipped when the module is already an ancestor), so
334
+ # a present target does not re-position itself. The prepend names still join the include list at
335
+ # freeze time — ahead of every include, where Ruby puts them.
336
+ def write_mixin_targets(accumulator, owner, targets, prepend:)
337
+ bucket = accumulator[owner] ||= {}
338
+ kind, other = prepend ? %i[prepend include] : %i[include prepend]
339
+ list = (bucket[kind] ||= [])
340
+ # A target the OTHER kind already carries (`include M; prepend M`) is one body the two tables cannot
341
+ # represent (Ruby's `[M, C, M]`), so the instance side is named unpositioned.
342
+ accumulator.taint(owner, :include) if targets.any? { |target| bucket[other]&.include?(target) }
343
+ # A repeated `include` keeps its first position (Ruby skips it). A repeated `prepend` is KEPT: the
344
+ # prepend table holds every statement, nearest first, so the readers that walk the raw list see what
345
+ # they always saw, and the resolution chain dedupes per owner, first statement winning.
346
+ list.unshift(*(prepend ? targets : targets.reject { |target| list.include?(target) }))
347
+ end
348
+
349
+ # Whether a mixin call's kind reaches the instance-side tables: `include` and `prepend`.
350
+ def mixin_call_recorded?(kind) = MIXIN_CALL_NAMES.include?(kind)
351
+
352
+ # Issue #1697 — a receiverless `include M` written as a statement of the file's own top level is
353
+ # `main.include`, which is `Object.include`: it mixes M into `Object`. It is recorded under its own
354
+ # owner key, `ObjectMixins::TOPLEVEL_INCLUDE_KEY`, rather than on `Object`, so it rides every table an include
355
+ # rides (the declaration signature, `discovered_class_sources`, the seed bundle) while no ancestry
356
+ # walk that types a call reaches it: what Ruby reaches first for a name such an include supplies
357
+ # depends on more than the chain shows (a block that rebinds `self`, an `extend` or a singleton `def`
358
+ # on `main`), so only the diagnostic silences read it (`Inference::ObjectMixins`), and typing is
359
+ # issue #1715's. `main` has no `prepend`, and a top-level `extend M` reaches `main`'s singleton alone, so
360
+ # neither is recorded.
361
+ #
362
+ # Issue #3007 — the call counts wherever {MixinAccumulator::MainSelfCalls} takes its `self` to be `main`: a
363
+ # program statement, or a call under top-level control flow (`if`, `begin`, `case`, …) or in a top-level
364
+ # `def` body. Such a call may not run, so its edge is in the set with no position ({MixinAccumulator#note}),
365
+ # as ADR-119 WD7 records an `include` in a method body. The same call inside a top-level block stays
366
+ # unrecorded: its `self` may not be `main` (a `describe do include M end` or `class_exec` block rebinds it,
367
+ # while `[1].each do include M end` does not, and the walk cannot tell them apart).
368
+ #
369
+ # Issue #2995 — `self.include M`, a receiverless or `self.` `send(:include, M)` and its `__send__` twin are the
370
+ # same `main.include`. `public_send(:include, M)` is not: `main`'s `include` is private, so it raises.
371
+ def toplevel_include_owner(node, qualified_prefix, current_class, accumulator)
372
+ return nil unless current_class.nil? && qualified_prefix.empty?
373
+ return nil unless ScopeIndexer.lexical_self_receiver?(node.receiver) && node.name != :public_send
374
+ return nil unless ScopeIndexer.mixin_call_view(node)&.first == :include
375
+
376
+ accumulator.program_statement?(node) ? ObjectMixins::TOPLEVEL_INCLUDE_KEY : nil
377
+ end
378
+
379
+ private_class_method :build_discovered_includes, :build_discovered_prepends, :freeze_mixin_lists,
380
+ :mixin_names_for, :walk_class_includes, :walk_includes_children,
381
+ :walk_includes_children_plain, :walk_includes_declaration?,
382
+ :walk_singleton_class_includes, :walk_includes_meta_new?, :walk_mixin_call_children,
383
+ :record_mixin_call, :instance_mixin_recorded?, :write_mixin_targets,
384
+ :mixin_call_recorded?, :toplevel_include_owner
385
+ end
386
+ end
387
+ end
388
+ end
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../type"
4
+ require_relative "../string_mutation"
5
+ require_relative "../unknown_store_widening"
6
+ require_relative "mutated_element_census"
7
+
8
+ module Rigor
9
+ module Inference
10
+ module ScopeIndexer
11
+ # The widening of the carriers a file mutates in place (#540), moved out of `scope_indexer.rb` whole (ADR-116
12
+ # WD9, #3400). {LiteralReceiverMutations} names the constants and class variables a call mutates, and the
13
+ # elements a call mutates through a read of one; the functions here give each such entry of a table the type
14
+ # its readers may still rely on.
15
+ #
16
+ # Four functions are public, because another file calls each. `ScopeIndexer.index` calls
17
+ # {.widen_mutated_constants} and {.widen_mutated_cvars} on the two tables it builds, and {ConstantIvarValue}
18
+ # calls the first on the constant table it builds for the ivar census. {MutatedElementCensus}, which
19
+ # {.widen_mutated_elements} asks for the element a chain of reads reaches, asks {.census_mutated_type} and
20
+ # {.census_unpinned_content} back for the type of that element and of a nominal on the way. The other three
21
+ # functions are private steps of those, and the three constants are the module's own.
22
+ #
23
+ # It calls nothing left in `scope_indexer.rb` and reads none of the indexer's constants. `scope_indexer.rb`
24
+ # requires this file, and this file does not require it back, so it loads and answers alone.
25
+ module MutatedCarrierWidening
26
+ module_function
27
+
28
+ NO_ELEMENT_MUTATIONS = Set.new.freeze
29
+ NO_CVAR_ELEMENT_MUTATIONS = {}.freeze
30
+
31
+ # `mutated_elements` — the `[name, steps]` pairs of an element mutation (#3243), widened after every whole-name
32
+ # mutation and shallowest first, so the table is the same whichever order the program spells them in.
33
+ def widen_mutated_constants(accumulator, mutated_names, mutated_elements = NO_ELEMENT_MUTATIONS)
34
+ return accumulator if mutated_names.empty? && mutated_elements.empty?
35
+
36
+ widened = accumulator.dup
37
+ mutated_names.each do |name|
38
+ existing = widened[name]
39
+ next if existing.nil?
40
+
41
+ widened[name] = census_mutated_type(existing)
42
+ end
43
+ widen_mutated_elements(widened, mutated_elements).freeze
44
+ end
45
+
46
+ def widen_mutated_cvars(accumulator, mutated_by_class, elements_by_class = NO_CVAR_ELEMENT_MUTATIONS)
47
+ return accumulator if mutated_by_class.empty? && elements_by_class.empty?
48
+
49
+ widened = accumulator.dup
50
+ (mutated_by_class.keys | elements_by_class.keys).each do |class_name|
51
+ table = widened[class_name]
52
+ next if table.nil?
53
+
54
+ updated = table.dup
55
+ mutated_by_class.fetch(class_name, NO_ELEMENT_MUTATIONS).each do |cvar|
56
+ existing = updated[cvar]
57
+ next if existing.nil?
58
+
59
+ updated[cvar] = census_mutated_type(existing)
60
+ end
61
+ widened[class_name] =
62
+ widen_mutated_elements(updated, elements_by_class.fetch(class_name, NO_ELEMENT_MUTATIONS)).freeze
63
+ end
64
+ widened.freeze
65
+ end
66
+
67
+ # `table` with the element each `[name, steps]` pair reaches widened ({MutatedElementCensus.widen}), as a copy;
68
+ # `table` itself when no pair reaches anything.
69
+ def widen_mutated_elements(table, mutated_elements)
70
+ return table if mutated_elements.empty?
71
+
72
+ MutatedElementCensus.ordered(mutated_elements).reduce(table) do |current, (name, steps)|
73
+ existing = current[name]
74
+ updated = existing && MutatedElementCensus.widen(existing, steps)
75
+ updated ? current.merge(name => updated) : current
76
+ end
77
+ end
78
+
79
+ # The type a mutated constant or class variable is read as. The census records the NAME a call mutated and not
80
+ # the call, so it cannot say what was stored, and `Dynamic` alone does not say it either: a read resolves
81
+ # through the static facet's RBS projection, where a closed `HashShape` answers its known values for any key, a
82
+ # `Tuple` its known elements for any index and an `Array[1 | 2]` `1 | 2`. `H = { a: 1 }; H.default = 0` read
83
+ # `H[:b]` as `1`, and `T = { a: 1 }; T[:b] = 2` read `T[:b]` as `1` too, so `== 0` / `== 2` folded
84
+ # always-falsey.
85
+ #
86
+ # Each carrier member of the facet therefore stops claiming its contents are complete. A literal shape floors to
87
+ # `Hash[untyped, untyped]` and a tuple to `Array[untyped]`, as ADR-58's class-level ivar census floors an ivar
88
+ # seed a method stores into (#1297). Unlike that census, a shape only a lookup mutator (`default=`) touched
89
+ # floors too, since the census does not record the method, so its present-key reads are lost as well (#1421).
90
+ # Keeping the literal's known values beside a `Dynamic[top]` arm left every read carrying them, and every key's
91
+ # at that, since the projection is not keyed, so `STATUS[:name]` answered `false | nil | Dynamic[top]` and a
92
+ # method declared `-> String` returning it drew `def.return-type-mismatch` although a sibling method could store
93
+ # anything there. An `Array` / `Hash` nominal with a value-pinned type argument gains the arm on every type
94
+ # argument, as the unknown-store seam gives it. A class-level nominal (`Hash.new(0)`'s
95
+ # `Hash[Dynamic[top], Integer]`) is left alone: a store of the same class keeps it true, and the arm would
96
+ # silence `COUNTS[k].upcase`. An entry already `Dynamic` is unpinned through its facet, so a carrier an RBS
97
+ # overload join wrapped is not left pinned.
98
+ def census_mutated_type(type)
99
+ type = type.static_facet if type.is_a?(Type::Dynamic)
100
+ members = type.is_a?(Type::Union) ? type.members : [type]
101
+ Type::Combinator.dynamic(Type::Combinator.union(*members.map { |member| census_unpinned_carrier(member) }))
102
+ end
103
+
104
+ # A `Difference` / `Refined` member drops to its unpinned base: a mutation can falsify the removed value or the
105
+ # refinement as well (`clear` empties a `non-empty-array`).
106
+ #
107
+ # Issue #3243 — a String literal floors to `String`, since the mutation can rewrite it (`S << "b"`), and an
108
+ # `Array` / `Hash` nominal first unpins the literal carriers its type arguments hold
109
+ # ({.census_unpinned_content}): `[[1]].shuffle`'s `Array[[1]]` is not value-pinned at the top, yet
110
+ # `SHUF[0].last` read the stale `1`.
111
+ def census_unpinned_carrier(member)
112
+ case member
113
+ when Type::HashShape
114
+ Type::Combinator.nominal_of("Hash", type_args: [Type::Combinator.untyped, Type::Combinator.untyped])
115
+ when Type::Tuple then Type::Combinator.nominal_of("Array", type_args: [Type::Combinator.untyped])
116
+ when Type::Constant then StringMutation.constant?(member) ? Type::Combinator.nominal_of("String") : member
117
+ when Type::Nominal
118
+ # The pin is tested BEFORE the content is unpinned, and on the original member: unpinning floors a
119
+ # literal type argument to its class (`Array["a"]` to `Array[String]`), which is a class-level argument
120
+ # and so not value-pinned, and the `Dynamic[top]` arm never arrived. `NS = ["a"].shuffle` with a method
121
+ # running `NS << 1` then kept claiming every element is a `String`, so `NS[1].odd?` — correct Ruby on the
122
+ # Integer that method stores — drew `call.undefined-method`. Whether the carrier held anything
123
+ # value-pinned at any depth is the question, and only the un-floored member can answer it.
124
+ pinned = UnknownStoreWidening.value_pinned_collection?(member)
125
+ member = census_unpinned_content(member)
126
+ pinned ? UnknownStoreWidening.gradual_content(member) : member
127
+ when Type::Difference, Type::Refined then census_unpinned_carrier(member.base)
128
+ else member
129
+ end
130
+ end
131
+
132
+ CENSUS_CONTENT_CLASSES = %w[Array Hash].freeze
133
+
134
+ # An `Array` / `Hash` nominal with each literal carrier its type arguments hold, at any depth, unpinned as
135
+ # {.census_unpinned_carrier} unpins a member — `Array[[1]]` to `Array[Array[untyped]]` — and every other
136
+ # argument (`Hash.new(0)`'s `Dynamic[top]` and `Integer`) left as it is. Answers `nominal` itself when nothing
137
+ # in it is such a carrier.
138
+ def census_unpinned_content(nominal)
139
+ return nominal unless CENSUS_CONTENT_CLASSES.include?(nominal.class_name) && !nominal.type_args.empty?
140
+
141
+ args = nominal.type_args.map { |arg| census_unpinned_argument(arg) }
142
+ return nominal if args.each_with_index.all? { |arg, index| arg.equal?(nominal.type_args[index]) }
143
+
144
+ Type::Combinator.nominal_of(nominal.class_name, type_args: args)
145
+ end
146
+
147
+ def census_unpinned_argument(arg)
148
+ case arg
149
+ when Type::Tuple, Type::HashShape then census_unpinned_carrier(arg)
150
+ when Type::Constant then StringMutation.constant?(arg) ? Type::Combinator.nominal_of("String") : arg
151
+ when Type::Nominal then census_unpinned_content(arg)
152
+ when Type::Union
153
+ members = arg.members.map { |member| census_unpinned_argument(member) }
154
+ members.each_with_index.all? { |member, index| member.equal?(arg.members[index]) } ? arg : Type::Combinator.union(*members)
155
+ else arg
156
+ end
157
+ end
158
+
159
+ private_class_method :widen_mutated_elements, :census_unpinned_carrier, :census_unpinned_argument
160
+ end
161
+ end
162
+ end
163
+ end