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,520 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "../../scope"
6
+ require_relative "../../source/constant_path"
7
+ require_relative "../../source/node_children"
8
+ require_relative "../../type"
9
+ require_relative "../../typing_state"
10
+ require_relative "../anonymous_meta_class"
11
+ require_relative "../define_method_block_self"
12
+ require_relative "../fresh_frame_blocks"
13
+ require_relative "../guard_rebinding"
14
+ require_relative "../in_effect_refinements"
15
+ require_relative "../last_line"
16
+ require_relative "../macro_block_self_type"
17
+ require_relative "../narrowing"
18
+ require_relative "../operand_effects"
19
+
20
+ module Rigor
21
+ module Inference
22
+ module ScopeIndexer
23
+ module NodePropagation # rubocop:disable Metrics/ModuleLength
24
+ module_function
25
+
26
+ # Walks `node`'s subtree DFS and fills in scope entries for every Prism node the StatementEvaluator did not
27
+ # visit (i.e. expression- interior nodes like the receiver/args of a CallNode). Those nodes inherit their
28
+ # nearest recorded ancestor's scope.
29
+ #
30
+ # `IfNode` / `UnlessNode` are special-cased: the truthy and falsey branches each get their predicate's narrowed
31
+ # scope before recursing. This handles expression-position conditionals (e.g. `cache[k] = if cond; t; else; e;
32
+ # end` and conditionals nested as call arguments) which are typed by ExpressionTyper without going through
33
+ # `eval_if`'s narrowing path.
34
+ #
35
+ # A block or lambda the evaluator never entered is special-cased too ({#closure_scope}): its own locals shadow
36
+ # the enclosing bindings of the same names. Such a block of a call {FreshFrameBlocks} names enters as the
37
+ # evaluator enters it ({#propagate_call}).
38
+ def propagate(node, table, parent_scope)
39
+ return unless node.is_a?(Prism::Node)
40
+
41
+ recorded = table.key?(node)
42
+ current_scope =
43
+ if recorded
44
+ table[node]
45
+ else
46
+ table[node] = parent_scope
47
+ parent_scope
48
+ end
49
+ evaluate_unentered_flow(node, table, current_scope)
50
+
51
+ case node
52
+ when Prism::IfNode
53
+ propagate_if_branches(node, table, current_scope)
54
+ when Prism::UnlessNode
55
+ propagate_unless_branches(node, table, current_scope)
56
+ when Prism::BlockNode, Prism::LambdaNode
57
+ # An entered block is recorded with its entry scope, parameters bound. An entered `->` is recorded with
58
+ # the ENCLOSING scope ({StatementEvaluator#eval_lambda} enters only its body), so its parameter list
59
+ # still needs the boundary: `f = ->(o, b = (o + 1)) { b }` reads the default's `o` as the parameter. A
60
+ # `when ->(o) { … }` condition is recorded without being entered at all, body included.
61
+ entered = recorded && node.is_a?(Prism::BlockNode)
62
+ child_scope = entered ? current_scope : closure_scope(node, current_scope)
63
+ node.rigor_each_child { |child| propagate(child, table, child_scope) }
64
+ when Prism::CallNode
65
+ propagate_call(node, table, current_scope, recorded: recorded)
66
+ when Prism::RescueNode, Prism::EnsureNode
67
+ # Issue #1360 — a clause the evaluator did not enter (a `begin` in a value position) reads `$!`, `$@` and
68
+ # `$?` unbound, not the enclosing clause's; one it entered keeps what it recorded.
69
+ child_scope = current_scope.forget_error_info.forget_last_status
70
+ node.rigor_each_child { |child| propagate(child, table, child_scope) }
71
+ when Prism::RescueModifierNode
72
+ propagate(node.expression, table, current_scope)
73
+ propagate(node.rescue_expression, table, current_scope.forget_error_info.forget_last_status)
74
+ when Prism::PostExecutionNode then propagate_end_body(node, table, current_scope)
75
+ else propagate_children(node, table, current_scope)
76
+ end
77
+ end
78
+
79
+ # Issues #2196 and #2056 — the constructs whose parts run under a narrowing their own condition or left operand
80
+ # makes, keyed to the part {StatementEvaluator} records first when it enters one. Issue #2214 — a `&&=` on a
81
+ # variable runs its value under the variable's truthy narrowing (`[c &&= c + 1]`).
82
+ UNENTERED_FLOW_ENTRY = {
83
+ Prism::AndNode => :left, Prism::OrNode => :left,
84
+ Prism::IfNode => :predicate, Prism::UnlessNode => :predicate,
85
+ Prism::WhileNode => :predicate, Prism::UntilNode => :predicate,
86
+ Prism::LocalVariableAndWriteNode => :value, Prism::InstanceVariableAndWriteNode => :value,
87
+ Prism::ClassVariableAndWriteNode => :value, Prism::GlobalVariableAndWriteNode => :value,
88
+ Prism::CaseNode => :predicate
89
+ }.freeze
90
+ private_constant :UNENTERED_FLOW_ENTRY
91
+
92
+ # ADR-129 WD1 — a Frame: {#with_unentered_evaluator} installs the walk's evaluation for the walk and restores
93
+ # the outer one, and whether one is installed changes answers (an unentered construct is evaluated only inside a
94
+ # walk).
95
+ UNENTERED_EVALUATOR = TypingState.frame(
96
+ :scope_index_unentered_evaluator,
97
+ reason: "installed around the index walk that evaluates its unentered constructs; the lambda writes the " \
98
+ "walk's own table (the walk's state, not typing state) and reads its converged-loop recording and " \
99
+ "clause widening, and is frozen while installed",
100
+ # G1′: the evaluation types under the Frame, but only the index walk, outside `type_of`, reads it.
101
+ typing_never_reads: true
102
+ )
103
+ private_constant :UNENTERED_EVALUATOR
104
+
105
+ # Issues #2196 and #2056 — an `&&` / `||` / `and` / `or`, an `if` / `unless` / ternary or a `while` / `until`
106
+ # in a value position (an array element, a hash value, a call argument, an interpolation, a multi-value
107
+ # `return`) is typed by `ExpressionTyper` and never entered, so no part of it was recorded and this walk handed
108
+ # every part one scope. A walk that narrows by the condition's edges cannot follow a write inside the narrowed
109
+ # part (`[c && (c = c.size; c.even?)]`, `[x || (set!; @x.upcase)]`) nor one inside a block in an earlier
110
+ # conjunct (`[x.nil? && [1].each { x = "d" } && x.upcase]`). Such a construct is evaluated here instead, from
111
+ # the scope this walk reached it with, under the index's own recorder, so each part is recorded with the scope
112
+ # the statement form records it with. What it does not visit (a dead arm) is walked as before.
113
+ def evaluate_unentered_flow(node, table, current_scope)
114
+ return evaluate_unentered_block(node, table, current_scope) if node.is_a?(Prism::CallNode)
115
+
116
+ entry = UNENTERED_FLOW_ENTRY[node.class]
117
+ return if entry.nil?
118
+
119
+ # An entered construct is already recorded; skipping it is a performance guard, not a behaviour. A `case`
120
+ # without a subject records its first `when` test first.
121
+ part = node.public_send(entry) || (node.conditions.first&.conditions&.first if node.is_a?(Prism::CaseNode))
122
+ return if part.nil? || table.key?(part)
123
+
124
+ evaluate = TypingState.current[UNENTERED_EVALUATOR]
125
+ evaluate&.call(node, current_scope, code_body_nodes(node, current_scope.source_path))
126
+ end
127
+
128
+ # Issue #1246 — the block of a call in a value position (`show([1].map { x = "s"; x.upcase })`, a receiver
129
+ # chain, an array element, a `->`'s body) is never entered: {StatementEvaluator} types the call through
130
+ # `ExpressionTyper`, and this walk could only floor a local the block rebinds ({#closure_scope}). Such a block
131
+ # is entered here instead, alone, from the scope this walk reached its call with, so its body is recorded with
132
+ # the scopes the statement form records: a capture read before the block rebinds it reads its binding, and the
133
+ # block's parameters are bound. The call's receiver and arguments stay with the walk. A class-creating or
134
+ # `refine` block keeps the walk ({#unentered_block_entry}), and a code body inside the block is left out of the
135
+ # recording ({#code_body_nodes}). A `super` block is a `SuperNode`'s, which the evaluator never enters either.
136
+ def evaluate_unentered_block(node, table, current_scope)
137
+ block = node.block
138
+ return unless block.is_a?(Prism::BlockNode)
139
+ return if table.key?(block) || class_body_block_call?(node, current_scope.source_path)
140
+
141
+ evaluate = TypingState.current[UNENTERED_EVALUATOR]
142
+ evaluate&.call(node, current_scope, code_body_nodes(block, current_scope.source_path), block_only: true)
143
+ end
144
+
145
+ # The evaluation of an unentered construct starts with no class frame and no lexical nesting, which only a code
146
+ # body observes: a `def`, a `class` / `module` / `class << …` body, and the block of a class-creating call
147
+ # (`Class.new { … }`, `Struct.new`, `Data.define`, `Module.new`) or of a `refine`, whose `def`s and constants
148
+ # resolve against the class the walk tracks and the evaluation does not. Every node in one is left out of the
149
+ # recording, so the walk reaches it unrecorded and handles it as it did before #2196 ({#unentered_block_entry},
150
+ # {#record_class_body_defs}).
151
+ # Answers nil, allocating nothing, for a construct holding no code body: the common case.
152
+ def code_body_nodes(node, source_path, acc = nil, inside: false)
153
+ inside ||= CODE_BODY_NODES.include?(node.class)
154
+ (acc ||= Set.new.compare_by_identity) << node if inside
155
+ if !inside && node.is_a?(Prism::CallNode) && class_body_block_call?(node, source_path)
156
+ acc = code_body_nodes(node.block, source_path, acc, inside: true)
157
+ node.rigor_each_child do |child|
158
+ acc = code_body_nodes(child, source_path, acc) unless child.equal?(node.block)
159
+ end
160
+ return acc
161
+ end
162
+ node.rigor_each_child { |child| acc = code_body_nodes(child, source_path, acc, inside: inside) }
163
+ acc
164
+ end
165
+
166
+ def class_body_block_call?(node, source_path)
167
+ node.block.is_a?(Prism::BlockNode) &&
168
+ (!AnonymousMetaClass.name_for(node, source_path).nil? || !ScopeIndexer.refine_target(node).nil?)
169
+ end
170
+
171
+ # The evaluation {#evaluate_unentered_flow} runs for the walk of one {#index}, installed for that walk only.
172
+ # Index walks nest (a rule may re-index a file while another file's walk is suspended), so the outer one is
173
+ # restored on exit. A `propagate` called directly while an outer index walk is running would record into the
174
+ # OUTER index's table through it; nothing outside {#index} calls `propagate`.
175
+ def with_unentered_evaluator(evaluate, &)
176
+ TypingState.current.with(UNENTERED_EVALUATOR, evaluate.freeze, &)
177
+ end
178
+
179
+ # Issue #1717 — a `def` / `class` / `module` body opens its own `self`, so an enclosing unentered block's
180
+ # unknown-`self` mark ({Scope#block_self_unknown?}) stops at it, as the evaluator's fresh body scope does.
181
+ # Issue #1246 — it opens its own local table too, so a name in it (a `def`'s parameter, a body local) is a new
182
+ # variable, never the enclosing statement's local of that name: `show(Class.new { def call(o) = o + 1 })` after
183
+ # `o = { x: 1 }` read the parameter as the Hash. Such a name reads `Dynamic[top]` ({#shadow_local}), as in an
184
+ # unentered closure ({#closure_scope}); a body the evaluator entered is recorded and keeps its own scopes. Only
185
+ # the parameters and the body take that table: a `def`'s receiver, a `class`'s name and superclass and a
186
+ # `class << …`'s expression run in the enclosing scope (`class K < base.nope; base = 2; end`).
187
+ # A `case` narrows its clauses ({#propagate_subjectless_case}, {#propagate_subject_case}). Every other node
188
+ # hands its children the scope as is.
189
+ def propagate_children(node, table, current_scope)
190
+ if node.is_a?(Prism::CaseNode) || node.is_a?(Prism::CaseMatchNode)
191
+ return propagate_subjectless_case(node, table, current_scope) if node.predicate.nil?
192
+
193
+ return propagate_subject_case(node, table, current_scope)
194
+ end
195
+ return propagate_code_body(node, table, current_scope) if SELF_OPENING_BODIES.include?(node.class)
196
+
197
+ node.rigor_each_child { |child| propagate(child, table, current_scope) }
198
+ end
199
+
200
+ def propagate_code_body(node, table, current_scope)
201
+ outer_scope = current_scope.with_block_self_unknown(false)
202
+ inner = [node.body]
203
+ inner << node.parameters if node.is_a?(Prism::DefNode)
204
+ inner_scope = node.locals.reduce(outer_scope) { |acc, name| shadow_local(acc, name) }
205
+ node.rigor_each_child do |child|
206
+ propagate(child, table, inner.any? { |part| part.equal?(child) } ? inner_scope : outer_scope)
207
+ end
208
+ end
209
+
210
+ # Issue #1361 — the block of `Thread.new`, `Fiber.new`, `define_method` and the other calls
211
+ # {FreshFrameBlocks.fresh_entry?} names does not read the match-global narrowing of the body it is written in.
212
+ # The evaluator enters it as {FreshFrameBlocks.entry} gives ({MatchRebinding.block_entry}), but a block in a
213
+ # value position — the receiver of `Thread.new { $1 }.value` — is not entered, and its body would read the
214
+ # statement's narrowing. Issue #1359 — nor does such a block read a `$_` narrowing it or the call's operands may
215
+ # set ({LastLine.block_entry}): this walk evaluates nothing, so no `gets` in the body forgets it. Issue #1360 —
216
+ # and the block of a call that keeps it, a closure, reads `$!`, `$@` and `$?` unbound
217
+ # ({FreshFrameBlocks.closure_entry}).
218
+ #
219
+ # Issue #1468 — the arguments and block of a safe-navigation call run only once its receiver is non-nil, so
220
+ # they inherit the scope with the receiver narrowed ({Narrowing.safe_navigation_scope},
221
+ # {Narrowing.safe_navigation_block_scope}); the receiver itself keeps the call's own. This walk hands every
222
+ # argument one scope and threads none, so arguments that write keep the call's: an earlier one may rebind the
223
+ # receiver a later one reads (`return y&.concat((y = nil).to_s, y.upcase)`, where the evaluator threads no
224
+ # operand).
225
+ #
226
+ # Issue #1246 — the block of a value-position call is evaluated before this walk reaches it
227
+ # ({#evaluate_unentered_block}), so it arrives recorded and keeps the evaluator's entry. The block entries
228
+ # above therefore reach only a block that evaluation declines: a class-creating or `refine` block.
229
+ def propagate_call(node, table, current_scope, recorded: false)
230
+ block = node.block
231
+ operands =
232
+ OperandEffects.any?(node.arguments) ? current_scope : Narrowing.safe_navigation_scope(node, current_scope)
233
+ entry = unentered_block_entry(node, block, table, current_scope, call_recorded: recorded)
234
+ if entry.equal?(current_scope) && operands.equal?(current_scope)
235
+ node.rigor_each_child { |child| propagate(child, table, current_scope) }
236
+ return
237
+ end
238
+
239
+ receiver = node.receiver
240
+ node.rigor_each_child do |child|
241
+ child_scope =
242
+ if child.equal?(receiver) then current_scope
243
+ elsif child.equal?(block) && block.is_a?(Prism::BlockNode) then entry
244
+ else operands
245
+ end
246
+ propagate(child, table, child_scope)
247
+ end
248
+ end
249
+
250
+ # Issue #1429 — an `END { }` body runs at exit, after any code that may rebind a guarded global or constant
251
+ # ({GuardRebinding.block_entry}).
252
+ def propagate_end_body(node, table, current_scope)
253
+ child_scope = GuardRebinding.block_entry(current_scope, node, nil)
254
+ node.rigor_each_child { |child| propagate(child, table, child_scope) }
255
+ end
256
+
257
+ def unentered_block_entry(node, block, table, current_scope, call_recorded: false)
258
+ return current_scope unless block.is_a?(Prism::BlockNode) && !table.key?(block)
259
+
260
+ narrowing = unentered_block_narrowing(node, current_scope)
261
+ current_scope = Narrowing.safe_navigation_block_scope(node, current_scope)
262
+ # Issue #1429 — nor a guard's narrowing of a global or constant the body may run after code rebinds.
263
+ current_scope = GuardRebinding.block_entry(current_scope, block, node)
264
+ current_scope = current_scope.entering_block_without_gradual_marks(block)
265
+ entry =
266
+ if FreshFrameBlocks.fresh_entry?(node, current_scope)
267
+ FreshFrameBlocks.entry(current_scope, node)
268
+ else
269
+ LastLine.block_entry(FreshFrameBlocks.closure_entry(current_scope, block, node), block, node)
270
+ end
271
+ # Issue #2196 — a class-body block whose call the evaluator recorded but did not enter (one inside a construct
272
+ # {#evaluate_unentered_flow} evaluated, or a later operand) starts from the scope the call was evaluated in,
273
+ # which may narrow a captured local the body writes; this walk runs the body's statements from one scope, so
274
+ # such a local reads `Dynamic[top]` in it: `[c && Class.new { c = c.size; c.even? }]`. A block whose call was
275
+ # not recorded keeps the enclosing scope as before.
276
+ if call_recorded && class_body_block_call?(node, entry.source_path)
277
+ entry = widen_condition_writes(block, entry)
278
+ end
279
+ anonymous = AnonymousMetaClass.name_for(node, current_scope.source_path)
280
+ return unentered_meta_class_body_entry(block, entry, anonymous, table) if anonymous
281
+
282
+ refined = refined_body_name(node, current_scope)
283
+ record_class_body_defs(block, entry, table, Type::Combinator.nominal_of(refined), nil) if refined
284
+
285
+ # Issue #1717 — the body's `self` is unknown unless narrowed, as on the evaluator's entry; only the
286
+ # `call.undefined-method` mark is set here, never #316's opaque mark (its bind gate reads the evaluator's).
287
+ entry = entry.with_block_self_unknown(true) unless narrowing&.self_type
288
+ return entry unless narrowing
289
+
290
+ MacroBlockSelfType.apply(entry, narrowing, keeps_unknown: current_scope.block_self_narrowing_unknown?(node))
291
+ end
292
+
293
+ # Issue #1120 — the class a `refine X do … end` call's block refines, or nil when the call is not that shape
294
+ # or is a `refine` where `self` is a class (issue #1689: the class's own method, its block an ordinary one).
295
+ # The evaluator asks the same two questions ({StatementEvaluator#refined_class_context}).
296
+ def refined_body_name(node, scope)
297
+ target = ScopeIndexer.refine_target(node)
298
+ return nil if target.nil? || InEffectRefinements.class_body_refine?(scope, node)
299
+
300
+ refined_class_name(target, scope)
301
+ end
302
+
303
+ # Issue #1120 — the name of the class `refine target do … end` refines: the class `target` types as, or the
304
+ # constant as written when it does not type as a class object (a gem class with no RBS). Shared by
305
+ # {StatementEvaluator#refined_class_context} and {#refined_body_name}.
306
+ def refined_class_name(target, scope)
307
+ refined = scope.type_of(target)
308
+ name = refined.is_a?(Type::Singleton) ? refined.class_name : Source::ConstantPath.qualified_name(target)
309
+ name&.delete_prefix("::")
310
+ end
311
+
312
+ # Issue #1696 — the entry of a `Class.new` / `Module.new` / `Struct.new` / `Data.define` block the evaluator
313
+ # never entered because it sits in a value position: `Object.const_set(:K, Class.new { attr_reader :x })`,
314
+ # `[Struct.new(:a) { def b = a }]`. Its `self` is the class `name` ({AnonymousMetaClass.name_for}) names, as
315
+ # {StatementEvaluator#enter_meta_class_body} gives the statement form; left on the enclosing `self`, a
316
+ # top-level one read every macro in it as `call.unresolved-toplevel`. Its `def`s are recorded as
317
+ # {#record_class_body_defs} says.
318
+ def unentered_meta_class_body_entry(block, entry, name, table)
319
+ entry = entry.with_block_self_type(Type::Combinator.singleton_of(name)).with_singleton_class_body(false)
320
+ record_class_body_defs(block, entry, table, Type::Combinator.nominal_of(name), entry.self_type)
321
+ entry
322
+ end
323
+
324
+ # Issue #1696 — records each `def` a value-position class-body block defines ({#each_class_body_def}) with the
325
+ # `self` its body runs on, since no evaluator pass enters it: `instance_self` for a plain `def`, `class_self`
326
+ # for a `def self.m` (skipped when nil). A `refine X` block passes only `Nominal[X]`: Ruby runs its defs on an
327
+ # instance of X, but the block itself on the refinement module, which Rigor does not model, so the block keeps
328
+ # the unknown `self` an unnarrowed block has. A `def obj.m` is left to the walk.
329
+ def record_class_body_defs(block, entry, table, instance_self, class_self)
330
+ each_class_body_def(block.body) do |def_node|
331
+ next if table.key?(def_node)
332
+
333
+ receiver = def_node.receiver
334
+ def_self =
335
+ if receiver.nil? then instance_self
336
+ elsif receiver.is_a?(Prism::SelfNode) then class_self
337
+ end
338
+ table[def_node] = entry.with_block_self_type(def_self) if def_self
339
+ end
340
+ end
341
+
342
+ # The `def`s a class-body block defines on its class: every one in the body, `private def m` included, short
343
+ # of a nested block, lambda or `class` / `module` / `class << …` body, each of which opens its own.
344
+ def each_class_body_def(node, &)
345
+ return unless node.is_a?(Prism::Node)
346
+ return yield(node) if node.is_a?(Prism::DefNode)
347
+ return if CLASS_BODY_DEF_BARRIERS.include?(node.class)
348
+
349
+ node.rigor_each_child { |child| each_class_body_def(child, &) }
350
+ end
351
+
352
+ # The narrowing ({MacroBlockSelfType::Match}) an unentered block's body runs with — its `self` and, for a
353
+ # `block_as_methods:` entry, the declared refinements (issue #1667) — when the call is one whose block the
354
+ # engine narrows: an ADR-16 Tier A `block_as_methods:` match ({MacroBlockSelfType}) or `define_method` on the
355
+ # lexical `self` ({DefineMethodBlockSelf}, issue #963). The evaluator applies the same two narrowings to
356
+ # the blocks it enters ({StatementEvaluator#build_block_entry_scope}, {StatementEvaluator#enter_call_block})
357
+ # and the value pass to every block it types ({ExpressionTyper#block_body_self_narrowing}); without this a
358
+ # block in a value position — `puts(MyApp.get("/x") { redirect "/y" })`, `render json: Alba.serialize(x) {
359
+ # attributes :id }`, `private define_method(:m) { helper }` — kept the enclosing `self`, and every DSL call
360
+ # in it read as a call on that. The receiver is typed only when some plugin declares an entry for the
361
+ # call's name, so a call no entry names costs one table lookup. A miss, or a raise, keeps the entry as
362
+ # built — the false-positive-safe direction.
363
+ def unentered_block_narrowing(node, scope)
364
+ registry = scope.environment&.plugin_registry
365
+ if registry && !registry.empty? && !registry.contribution_index.block_entries_for(node.name).empty?
366
+ receiver_type =
367
+ node.receiver ? scope.type_of(node.receiver) : MacroBlockSelfType.implicit_receiver_type(scope)
368
+ match = MacroBlockSelfType.match_for(scope: scope, call_node: node, receiver_type: receiver_type)
369
+ return match if match
370
+ end
371
+ defined = DefineMethodBlockSelf.narrow_self_type_for(scope: scope, call_node: node)
372
+ defined && MacroBlockSelfType::Match.new(self_type: defined, refinements: InEffectRefinements::EMPTY)
373
+ rescue StandardError
374
+ nil
375
+ end
376
+
377
+ # The scope the children of an unentered block or lambda inherit. The evaluator enters a statement-level
378
+ # call's block ({StatementEvaluator#evaluate_block_if_present}), an assignment's rvalue and a statement-level
379
+ # `->`'s body, but not a closure in a value position — a call argument (`show(xs.map { |o| o + 1 })`), a
380
+ # receiver chain (`xs.map { |o| o + 1 }.sum`), a literal element — nor a `super` call's block, so its body
381
+ # falls to this walk and would inherit the enclosing statement's scope verbatim. That scope still binds an
382
+ # outer `o` the block parameter shadows, and a read of the parameter typed as the outer local: `o = { x: 1 }`
383
+ # made `o + 1` an undefined-method error.
384
+ #
385
+ # Every name in the closure's own local table (Prism's `locals`: its parameters, `;`-locals and the locals
386
+ # its body introduces) is a new variable, and so is the implicit `it` of a block that uses it, which Prism
387
+ # keeps out of `locals` although the binder binds it (`xs.each { show(it.map { it + 1 }) }` read the inner
388
+ # `it` as the outer block's). An enclosing binding of such a name is replaced with `Dynamic[top]`. Not the
389
+ # signature's parameter type: this walk evaluates nothing, and a body write to the name is never threaded,
390
+ # so any narrower claim could be stale. A name the enclosing scope does not bind is left unbound, which
391
+ # reads the same `Dynamic[top]`. Issue #1246 — for the same reason a captured name the body (or a closure
392
+ # nested in it) writes reads `Dynamic[top]` too ({#widen_condition_writes}): `x = 1; show([1].map { x = "s";
393
+ # x.upcase })` read `x` as `1` after the write. A captured name the body only reads keeps the enclosing
394
+ # binding. Issue #1360 — a `->` body and its parameter defaults run whenever the lambda is called, so they read
395
+ # `$!`, `$@` and `$?` unbound ({FreshFrameBlocks.closure_entry}).
396
+ def closure_scope(closure, scope)
397
+ scope = GuardRebinding.block_entry(scope, closure, nil)
398
+ scope = FreshFrameBlocks.closure_entry(scope, closure, nil)
399
+ scope = shadow_local(scope, :it) if closure.parameters.is_a?(Prism::ItParametersNode)
400
+ scope = closure.locals.reduce(scope) { |acc, name| shadow_local(acc, name) }
401
+ widen_condition_writes(closure, scope)
402
+ end
403
+
404
+ def shadow_local(scope, name)
405
+ scope.local(name).nil? ? scope : scope.with_local(name, Type::Combinator.untyped)
406
+ end
407
+
408
+ # Issue #1924 — an `if` / `elsif` / `unless` / ternary the evaluator did not enter (`[if (x ||= "d").empty?
409
+ # then 0 else x.upcase end]`) applies no write its condition makes, so its edges are taken from a scope where
410
+ # every bound local the condition writes reads `Dynamic[top]` ({#widen_condition_writes}), as
411
+ # {#propagate_subjectless_case} takes a `when` condition's. The condition itself reads the scope before it.
412
+ def propagate_if_branches(node, table, current_scope)
413
+ truthy_scope, falsey_scope = conditional_edges(node.predicate, current_scope)
414
+ propagate(node.predicate, table, current_scope) if node.predicate
415
+ propagate(node.statements, table, truthy_scope) if node.statements
416
+ propagate(node.subsequent, table, falsey_scope) if node.subsequent
417
+ end
418
+
419
+ def propagate_unless_branches(node, table, current_scope)
420
+ truthy_scope, falsey_scope = conditional_edges(node.predicate, current_scope)
421
+ propagate(node.predicate, table, current_scope) if node.predicate
422
+ propagate(node.statements, table, falsey_scope) if node.statements
423
+ propagate(node.else_clause, table, truthy_scope) if node.else_clause
424
+ end
425
+
426
+ def conditional_edges(predicate, scope)
427
+ Narrowing.predicate_scopes(predicate, widen_condition_writes(predicate, scope))
428
+ end
429
+
430
+ # Issue #1495 — a `case` without a subject the evaluator did not enter (`[case; when x.nil? then 0; else x.size
431
+ # end]`) narrows by its conditions' edges, as {StatementEvaluator#subjectless_when_scopes} does: each condition
432
+ # under the falsey edges of the conditions before it, each clause under the join of its conditions' truthy
433
+ # edges, and the `else` under the last falsey edge. Like the walk of an `if`, it applies no write a condition
434
+ # makes, so the edges are taken from a scope where every bound local the condition writes reads `Dynamic[top]`
435
+ # ({#widen_condition_writes}): neither the local's old type nor an earlier condition's narrowing of it reaches
436
+ # them, as `when (x = s ? String(s) : nil) then x.upcase` after `x = 1` would.
437
+ def propagate_subjectless_case(node, table, current_scope)
438
+ falsey = current_scope
439
+ node.conditions.each do |when_node|
440
+ truthy_edges = when_node.conditions.map do |condition|
441
+ propagate(condition, table, falsey)
442
+ truthy, falsey = Narrowing.predicate_scopes(condition, widen_condition_writes(condition, falsey))
443
+ truthy
444
+ end
445
+ propagate(when_node, table, truthy_edges.reduce(:join))
446
+ end
447
+ propagate(node.else_clause, table, falsey) if node.else_clause
448
+ end
449
+
450
+ # Issue #1484 — a `case` with a subject the evaluator did not enter (`[case io when StringIO then io.string
451
+ # end]`, `[case io in StringIO then io.string else nil end]`) narrows each clause as the evaluator does
452
+ # ({Narrowing.case_clause_scopes}): the body under the clause's narrowed edge, the next clause and the `else`
453
+ # under its falsey edge. The walk applies no write the subject, a `when` condition or an `in` pattern makes, so
454
+ # every bound local one of them writes reads `Dynamic[top]` from that point on ({#widen_condition_writes}).
455
+ # Nor does it run the evaluator's per-clause `LastLine.forget_if_set`: the statement holding the `case`
456
+ # already forgets a `$_` any of its tests may set. A clause's tests are recorded under the scope the earlier
457
+ # clauses' falsey edges left, where the evaluator's `when` tests read the post-subject scope: a test reading
458
+ # the subject sees what the earlier clauses ruled out (`when NilClass then 0 when v.succ`).
459
+ #
460
+ # A test that calls a method may rebind an instance-variable or global subject before the body reads it
461
+ # (`when String then 1 when reset! then @x.upcase`, where `reset!` writes `@x`), and the walk cannot tell. From
462
+ # the first such test on, the subject reads `Dynamic[top]` on both edges ({#forget_called_subject}). The
463
+ # evaluator forgets a global subject there too (issue #2211). A `case` / `when` the evaluator did not enter is
464
+ # evaluated ({#evaluate_unentered_flow}); this walk handles only a clause that evaluation does not visit.
465
+ def propagate_subject_case(node, table, current_scope)
466
+ subject = node.predicate
467
+ propagate(subject, table, current_scope)
468
+ falsey = widen_condition_writes(subject, current_scope)
469
+ called = false
470
+ node.conditions.each do |clause|
471
+ tests = clause.is_a?(Prism::WhenNode) ? clause.conditions : [clause.pattern]
472
+ tests.each { |test| propagate(test, table, falsey) }
473
+ falsey = tests.reduce(falsey) { |acc, test| widen_condition_writes(test, acc) }
474
+ called ||= tests.any? { |test| runs_call?(test) }
475
+ body, falsey = Narrowing.case_clause_scopes(subject, clause, falsey)
476
+ body, falsey = [body, falsey].map { |scope| forget_called_subject(subject, scope) } if called
477
+ propagate(clause, table, body)
478
+ end
479
+ propagate(node.else_clause, table, falsey) if node.else_clause
480
+ end
481
+
482
+ CODE_RUNNING_NODES = [Prism::CallNode, Prism::SuperNode, Prism::ForwardingSuperNode, Prism::YieldNode].freeze
483
+ private_constant :CODE_RUNNING_NODES
484
+
485
+ # Whether `node` holds a method call, `super` or `yield` anywhere, a lambda body's included.
486
+ def runs_call?(node)
487
+ return false unless node.is_a?(Prism::Node)
488
+ return true if CODE_RUNNING_NODES.include?(node.class)
489
+
490
+ node.rigor_each_child { |child| return true if runs_call?(child) }
491
+ false
492
+ end
493
+
494
+ # `scope` with an instance-variable or global `subject` rebound to `Dynamic[top]`; any other subject as is.
495
+ def forget_called_subject(subject, scope)
496
+ case subject
497
+ when Prism::InstanceVariableReadNode then scope.with_ivar(subject.name, Type::Combinator.untyped)
498
+ when Prism::GlobalVariableReadNode then scope.with_global(subject.name, Type::Combinator.untyped)
499
+ else scope
500
+ end
501
+ end
502
+
503
+ # `scope` with every bound local `condition` writes rebound to `Dynamic[top]`.
504
+ def widen_condition_writes(condition, scope)
505
+ OperandEffects.written_variables(condition).reduce(scope) do |acc, name|
506
+ acc.local(name) ? acc.with_local(name, Type::Combinator.untyped) : acc
507
+ end
508
+ end
509
+
510
+ private_class_method :evaluate_unentered_flow, :evaluate_unentered_block, :code_body_nodes,
511
+ :class_body_block_call?, :propagate_children, :propagate_code_body, :propagate_call,
512
+ :propagate_end_body, :unentered_block_entry, :refined_body_name,
513
+ :unentered_meta_class_body_entry, :record_class_body_defs, :each_class_body_def,
514
+ :unentered_block_narrowing, :closure_scope, :shadow_local, :propagate_if_branches,
515
+ :propagate_unless_branches, :conditional_edges, :propagate_subjectless_case,
516
+ :propagate_subject_case, :runs_call?, :forget_called_subject, :widen_condition_writes
517
+ end
518
+ end
519
+ end
520
+ end