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,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../diagnostic"
5
+ require_relative "rule_ids"
6
+
7
+ module Rigor
8
+ module Analysis
9
+ module CheckRules
10
+ # ADR-116 WD1 — `flow.unreachable-branch`: an `if` / `unless` whose predicate is a literal of known
11
+ # truthiness and whose dead branch is observable.
12
+ #
13
+ # The entry point is {.diagnostic}, called by `CheckRules.main_pass_node_diagnostics`. It reads no
14
+ # other rule module and calls nothing in `CheckRules`. The `unreachable_clause` collector is a
15
+ # different rule and lives in {UnreachableClauseCollector}.
16
+ module UnreachableBranch
17
+ module_function
18
+
19
+ # v0.1.2 — `flow.unreachable-branch`. Fires when an `IfNode` / `UnlessNode` whose predicate is a
20
+ # literal `true` / `false` / `nil` (or a literal numeric / string / symbol whose Ruby truthiness is
21
+ # known at-a-glance) has an observable dead branch. The diagnostic points at the dead branch (not
22
+ # the predicate) so the squiggle lands on the code that never runs.
23
+ #
24
+ # Conservative envelope — by deliberate v0.1.2 design:
25
+ # - Only **literal-shaped** predicates fire. Inferred-constant predicates (`x.method?` that happens
26
+ # to fold to `Constant<bool>`) are intentionally skipped — Rigor's loop / mutation /
27
+ # RBS-strictness modelling is incomplete enough that an inferred constant can be a false positive
28
+ # (e.g. accumulator `arr << x` doesn't widen the carrier; defensive `module.name.nil?` checks
29
+ # against anonymous-class nil that the RBS `Module#name -> String` sig hides). The literal-only
30
+ # envelope captures the clear "user wrote `if false`" case without false alarms.
31
+ # - Empty dead branches (e.g. `if false; end` with no body) are skipped — there is no useful
32
+ # location to point at.
33
+ # - Postfix-`if` / `unless` modifiers with a literal predicate ARE flagged (`expr if false` body
34
+ # never runs, exactly like the block form).
35
+ # - Elsif chains (`subsequent` is itself an `IfNode`) ARE flagged — the entire downstream chain is
36
+ # unreachable when the outer predicate is a constant literal.
37
+ #
38
+ # Inferred-constant predicates are the `flow.always-truthy-condition` rule's ({AlwaysTruthyConditionCollector}).
39
+ def diagnostic(path, node, scope_index)
40
+ scope = scope_index[node]
41
+ return nil if scope.nil?
42
+
43
+ polarity = literal_predicate_polarity(node.predicate)
44
+ return nil if polarity.nil?
45
+
46
+ dead_branch = unreachable_branch_for(node, polarity == :truthy)
47
+ return nil if dead_branch.nil?
48
+
49
+ build_unreachable_branch_diagnostic(path, dead_branch, polarity)
50
+ end
51
+
52
+ TRUTHY_LITERAL_NODES = [
53
+ Prism::TrueNode, Prism::IntegerNode, Prism::FloatNode,
54
+ Prism::StringNode, Prism::SymbolNode, Prism::RegularExpressionNode
55
+ ].freeze
56
+ private_constant :TRUTHY_LITERAL_NODES
57
+
58
+ FALSEY_LITERAL_NODES = [Prism::FalseNode, Prism::NilNode].freeze
59
+ private_constant :FALSEY_LITERAL_NODES
60
+
61
+ # Returns `:truthy` / `:falsey` for a syntactically-literal predicate, or nil for anything else.
62
+ # `TrueNode`, `FalseNode`, `NilNode` are the unambiguous cases. Numeric / string / symbol literals
63
+ # are always truthy in Ruby (any non-`false` / non-`nil` value is truthy, including `0` and `""`).
64
+ def literal_predicate_polarity(predicate)
65
+ return :truthy if TRUTHY_LITERAL_NODES.any? { |klass| predicate.is_a?(klass) }
66
+ return :falsey if FALSEY_LITERAL_NODES.any? { |klass| predicate.is_a?(klass) }
67
+
68
+ nil
69
+ end
70
+
71
+ # Returns the dead-branch node for a literal-predicate if/unless, or nil when no observable branch
72
+ # is dead.
73
+ def unreachable_branch_for(node, truthy)
74
+ dead =
75
+ case node
76
+ when Prism::IfNode then truthy ? node.subsequent : node.statements
77
+ when Prism::UnlessNode then truthy ? node.statements : node.else_clause
78
+ end
79
+ dead unless dead.nil?
80
+ end
81
+
82
+ def build_unreachable_branch_diagnostic(path, dead_branch, polarity)
83
+ Diagnostic.from_node(
84
+ dead_branch,
85
+ rule: RULE_UNREACHABLE_BRANCH,
86
+ path: path,
87
+ message: "unreachable branch: literal predicate is always #{polarity}",
88
+ severity: :warning
89
+ )
90
+ end
91
+
92
+ private_class_method :literal_predicate_polarity, :unreachable_branch_for,
93
+ :build_unreachable_branch_diagnostic
94
+ end
95
+ end
96
+ end
97
+ end
@@ -6,6 +6,8 @@ require_relative "../../source/node_children"
6
6
  require_relative "../../inference/narrowing"
7
7
  require_relative "inferred_param_guard"
8
8
  require_relative "clause_coverage"
9
+ require_relative "../diagnostic"
10
+ require_relative "rule_ids"
9
11
 
10
12
  module Rigor
11
13
  module Analysis
@@ -110,6 +112,46 @@ module Rigor
110
112
  @results.freeze
111
113
  end
112
114
 
115
+ # ADR-116 WD1 — the message builder for this collector's results, moved here from `CheckRules`.
116
+ # {.diagnostics} is called by `CheckRules.diagnose` through its collector table; the rest is private to it.
117
+
118
+ # ADR-47 — `flow.unreachable-clause`. One diagnostic per `when` / `in` clause
119
+ # the flow engine's narrowing proves can never match (its narrowed
120
+ # subject is `bot`), and per `else` it proves never runs. The squiggle lands
121
+ # on the dead clause's body, mirroring `flow.unreachable-branch`.
122
+ def self.diagnostics(path, results)
123
+ results.map do |result|
124
+ build_unreachable_clause_diagnostic(path, result)
125
+ end
126
+ end
127
+
128
+ def self.build_unreachable_clause_diagnostic(path, result)
129
+ Diagnostic.from_node(
130
+ result.body,
131
+ rule: RULE_UNREACHABLE_CLAUSE,
132
+ path: path,
133
+ message: unreachable_clause_message(result),
134
+ severity: :warning
135
+ )
136
+ end
137
+
138
+ def self.unreachable_clause_message(result)
139
+ subject = result.subject_name
140
+ kw = result.keyword
141
+ case result.kind
142
+ when :prior_exhaustion
143
+ "unreachable `#{kw} #{result.condition_source}': `#{subject}' is already covered " \
144
+ "by an earlier `#{kw}' clause"
145
+ when :exhausted_else
146
+ "unreachable `else': the `#{kw}' clauses already cover every value `#{subject}' can take here"
147
+ else # :disjoint
148
+ "unreachable `#{kw} #{result.condition_source}': `#{subject}' can never be " \
149
+ "#{result.condition_source} here (the flow proves the subject disjoint)"
150
+ end
151
+ end
152
+
153
+ private_class_method :build_unreachable_clause_diagnostic, :unreachable_clause_message
154
+
113
155
  private
114
156
 
115
157
  def walk(node, in_loop_or_block:)
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../reflection"
4
+ require_relative "../diagnostic"
5
+ require_relative "receiver_predicates"
6
+
7
+ module Rigor
8
+ module Analysis
9
+ module CheckRules
10
+ # ADR-116 WD1 — `call.unresolved-toplevel` (ADR-34): an implicit-self call at toplevel scope whose name
11
+ # resolves against no same-file `def`, `pre_eval:` patch, Kernel / Object RBS surface, `main` singleton
12
+ # method or mixin of Object.
13
+ #
14
+ # The one entry point is {.diagnostic}, called by `CheckRules.call_node_diagnostics`. The module reads
15
+ # {ReceiverPredicates} and calls nothing else in `CheckRules`; the eval-block ranges it consults are
16
+ # computed once per file by `CheckRules.receiver_eval_block_ranges` and handed in. It also asks
17
+ # {RebindingBlockCallees}.
18
+ module UnresolvedToplevel
19
+ module_function
20
+
21
+ # ADR-34 — `call.unresolved-toplevel`. Fires on an
22
+ # implicit-self call (no explicit receiver) at toplevel
23
+ # scope (`scope.toplevel?`, i.e. outside any class /
24
+ # module body) whose name does not resolve against:
25
+ #
26
+ # 1. A same-file toplevel `def` via
27
+ # {Scope#top_level_def_for}.
28
+ # 2. The ADR-17 `ProjectPatchedMethods` registry under
29
+ # `(Object, name, :instance)` — projects declare
30
+ # their toplevel-injecting monkey-patches in
31
+ # `.rigor.yml`'s `pre_eval:` array as the canonical
32
+ # opt-out per ADR-34 WD2.
33
+ # 3. The standard `Kernel` / `Object` private-method
34
+ # surface (`puts`, `p`, `require`, `loop`, `raise`,
35
+ # …) drawn from the loaded RBS environment.
36
+ # 4. The private singleton methods of the top-level `main`
37
+ # object (`using`, `include`, `public`, `private`,
38
+ # `define_method`), read from RBS core's
39
+ # `RBS::Unnamed::TopLevelSelfClass`, plus
40
+ # `ruby2_keywords` (issue #1383).
41
+ # 5. The instance methods of a module the project mixes
42
+ # into `Object` — a top-level `include M`, or an
43
+ # `include M` in `class Object` — or any call at all
44
+ # when such a module's surface is unknown (issue #1697).
45
+ #
46
+ # The rule deliberately does NOT generalise to
47
+ # implicit-self calls inside `def` / `class` / `module`
48
+ # bodies — ADR-24 WD3's lenient-on-unresolved default
49
+ # stays in force there. ADR-24 WD4's gated class-body
50
+ # diagnostic is a separate decision this ADR does not
51
+ # open.
52
+ #
53
+ # Authored severity is `:warning`; the severity profile
54
+ # remaps it (`strict` → `:error`, `balanced` →
55
+ # `:warning`, `lenient` → `:off` / suppressed).
56
+ def diagnostic(path, call_node, scope_index, eval_ranges = nil, lexical_sites = nil)
57
+ return nil unless call_node.receiver.nil?
58
+
59
+ scope = scope_index[call_node]
60
+ return nil if scope.nil?
61
+ return nil unless scope.toplevel?
62
+
63
+ name = call_node.name
64
+ return nil if scope.top_level_def_for(name)
65
+ return nil if ReceiverPredicates.source_declared_method?(scope, "Object", name, :instance)
66
+ return nil if Rigor::Reflection.instance_method_definition("Object", name, scope: scope)
67
+ # Issue #1708 — `main` is an Object, so a `pre_eval:` patch on `Kernel` or `BasicObject` defines the name too.
68
+ return nil if ReceiverPredicates.project_patched_through_ancestors?(scope, "Object", name, :instance)
69
+ return nil if main_singleton_method?(name, scope)
70
+ return nil if Inference::ObjectMixins.may_define?(scope, name, receiver: :implicit)
71
+ # `Target.class_eval { def added = 1; def use_added = added }` files the defs on Target
72
+ # but leaves the eval body with a nil `self_type`, so `toplevel?` still holds. An eval
73
+ # body is morally a class body, so ADR-34 stays silent there — including on a genuinely
74
+ # undefined name inside the block. Ranges are computed once per file.
75
+ return nil if call_inside_receiver_eval_ranges?(eval_ranges, call_node)
76
+ return nil if lexically_declined_toplevel_call?(call_node, scope, lexical_sites, scope_index)
77
+
78
+ build_unresolved_toplevel_diagnostic(path, call_node)
79
+ end
80
+
81
+ # The `call.unresolved-toplevel` declines that read the file's syntax, asked last because they walk it.
82
+ def lexically_declined_toplevel_call?(call_node, scope, lexical_sites, scope_index = nil)
83
+ # ADR-121 WD7 — a refinement of `Object` (or one Rigor cannot read) in effect here may define the name.
84
+ return true if ReceiverPredicates.refined_method_in_effect?("Object", call_node, scope, :instance,
85
+ lexical_sites)
86
+ return false if lexical_sites.nil?
87
+
88
+ # Issue #1817 — a block handed to a project method that may `module_eval` / `instance_exec` it runs under
89
+ # another `self`, so a call in it is not a top-level one ({RebindingBlockCallees}).
90
+ RebindingBlockCallees.inside_rebound_block?(call_node, scope, lexical_sites.block_calls, scope_index)
91
+ end
92
+
93
+ # Issue #1383 — RBS core declares `main`'s private singleton methods on
94
+ # `RBS::Unnamed::TopLevelSelfClass`, so the set follows RBS rather than a hand-kept
95
+ # list. CRuby 4.0's `main` also has `ruby2_keywords`, which RBS does not declare.
96
+ # rbs 3.x has no `TopLevelSelfClass`, so there only `ruby2_keywords` is covered.
97
+ # A call inside a top-level block (`describe do include M end`) is judged here too;
98
+ # silencing it is the cheaper error.
99
+ MAIN_SINGLETON_CLASS_NAME = "RBS::Unnamed::TopLevelSelfClass"
100
+ MAIN_SINGLETON_METHODS_MISSING_FROM_RBS = %i[ruby2_keywords].freeze
101
+ private_constant :MAIN_SINGLETON_CLASS_NAME, :MAIN_SINGLETON_METHODS_MISSING_FROM_RBS
102
+
103
+ def main_singleton_method?(name, scope)
104
+ return true if MAIN_SINGLETON_METHODS_MISSING_FROM_RBS.include?(name)
105
+ # `TopLevelSelfClass < Object`: when the project's own RBS broke `Object`, building it would only
106
+ # fail again and report a second, unfixable class name beside `Object` (#696's report).
107
+ return false if Rigor::Reflection.instance_definition("Object", scope: scope).nil?
108
+
109
+ !Rigor::Reflection.instance_method_definition(MAIN_SINGLETON_CLASS_NAME, name, scope: scope).nil?
110
+ end
111
+
112
+ def call_inside_receiver_eval_ranges?(eval_ranges, call_node)
113
+ return false if eval_ranges.nil? || eval_ranges.empty?
114
+
115
+ loc = call_node.location
116
+ eval_ranges.any? do |start_offset, end_offset|
117
+ loc.start_offset >= start_offset && loc.end_offset <= end_offset
118
+ end
119
+ end
120
+
121
+ def build_unresolved_toplevel_diagnostic(path, call_node)
122
+ Diagnostic.from_message_loc(
123
+ call_node,
124
+ path: path,
125
+ message: "unresolved toplevel call to `#{call_node.name}`. " \
126
+ "If a project file defines `#{call_node.name}` via a toplevel " \
127
+ "`def` or a monkey-patch on Object/Kernel, list that file in " \
128
+ "`.rigor.yml`'s `pre_eval:` (ADR-17) so the analyzer sees it.",
129
+ severity: :warning,
130
+ rule: RULE_UNRESOLVED_TOPLEVEL,
131
+ method_name: call_node.name.to_s
132
+ )
133
+ end
134
+
135
+ private_class_method :lexically_declined_toplevel_call?, :main_singleton_method?,
136
+ :call_inside_receiver_eval_ranges?, :build_unresolved_toplevel_diagnostic
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,252 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+ require_relative "../../scope"
5
+ require_relative "../../type"
6
+ require_relative "../../memo_audit"
7
+ require_relative "../../typing_state"
8
+ require_relative "../../inference/definer_resolution"
9
+ require_relative "../dependency_recorder"
10
+ require_relative "../diagnostic"
11
+ require_relative "receiver_predicates"
12
+ require_relative "rule_ids"
13
+
14
+ module Rigor
15
+ module Analysis
16
+ module CheckRules
17
+ # ADR-116 WD1 — `def.method-visibility-mismatch` (v0.1.2, Issues #1568 and #1984): an explicit-receiver
18
+ # call to a method the project records `:private`, on an instance receiver or a class-object receiver
19
+ # (`private`, `private :name`, `private_class_method`, `class << self` with `private`).
20
+ #
21
+ # The one entry point is {.diagnostic}, called by `CheckRules.call_node_diagnostics`. It reads
22
+ # {ReceiverPredicates}, `Inference::DefinerResolution` and `Scope::ResolutionChain`, owns the
23
+ # run-scoped memo of the names some class records private, and calls nothing else in `CheckRules`.
24
+ module VisibilityMismatch
25
+ module_function
26
+
27
+ # v0.1.2 — `def.method-visibility-mismatch`. Fires when an explicit-receiver `Prism::CallNode`
28
+ # targets a user-class method whose `discovered_method_visibilities` entry is `:private`. The rule
29
+ # is intentionally narrow:
30
+ #
31
+ # - Only `:private`. `:protected` access depends on subclass tracking the engine does not yet
32
+ # model; broadening waits for that surface.
33
+ # - Only user classes whose visibility table the indexer built. RBS-known classes (stdlib, gems)
34
+ # are NOT consulted yet — RBS visibility is reliable but surfacing it would broaden the rule to a
35
+ # level the per-rule false-positive triage hasn't covered.
36
+ # - Implicit-self calls are skipped (always allowed for private). Calls whose receiver is
37
+ # `Prism::SelfNode` are also skipped — Ruby 2.7+ permits `self.foo` for private methods.
38
+ # - Receiver MUST resolve to a `Type::Nominal` so the rule has a single class identity to query.
39
+ # Unions / Dynamic / shape carriers are skipped.
40
+ # - Issue #1568 — a module the class PREPENDS sits ahead of it in Ruby's order, so a definition there
41
+ # is what the call reaches; the class's own private `def` answers only when nothing prepended
42
+ # defines the name ({#reached_definer_visibility}).
43
+ # - Issue #1984 — a class-object receiver (`Type::Singleton`, `K.x`) is checked against the singleton side:
44
+ # the definition the call reaches on `K`'s singleton chain (its own singleton methods, its extended
45
+ # modules' instance methods, then the superclass's singleton chain) must be recorded `:private`
46
+ # (`private_class_method`, `class << self` with `private`, a private method of an extended module).
47
+ # - Issue #1984 — Ruby hands a private call to `method_missing`, so a `method_missing` or
48
+ # `respond_to_missing?` a project entry on the receiver's chain records, on the side the call reaches,
49
+ # declines the rule ({#missing_hook_on_chain?}).
50
+ def diagnostic(path, call_node, facts, lexical_sites = nil)
51
+ return nil unless explicit_non_self_receiver?(call_node.receiver)
52
+
53
+ scope = facts.scope
54
+ return nil if scope.nil?
55
+
56
+ # ADR-67 WD6b — an inferred-parameter receiver's nominal type is an open-call-site lower bound; a
57
+ # private-method visibility firing against it is an FP by construction. Decline.
58
+ return nil if ReceiverPredicates.inferred_param_receiver?(call_node, scope)
59
+
60
+ receiver_type = facts.receiver_type
61
+ case receiver_type
62
+ when Type::Nominal then instance_visibility_mismatch(path, call_node, scope, receiver_type, lexical_sites)
63
+ when Type::Singleton then singleton_visibility_mismatch(path, call_node, scope, receiver_type, lexical_sites)
64
+ end
65
+ end
66
+
67
+ def instance_visibility_mismatch(path, call_node, scope, receiver_type, lexical_sites)
68
+ visibility = scope.discovered_method_visibility(receiver_type.class_name, call_node.name)
69
+ return nil unless visibility == :private
70
+
71
+ reached = reached_definer_visibility(scope, receiver_type.class_name, call_node.name)
72
+ return nil if reached == :undecided
73
+ return nil unless reached.nil? || reached == :private
74
+ return nil if missing_hook_on_chain?(scope, receiver_type.class_name, :instance)
75
+ # ADR-121 WD7 — a refinement in effect (or one Rigor cannot read) may redefine the method, public.
76
+ return nil if ReceiverPredicates.refined_method_in_effect?(receiver_type.class_name, call_node, scope,
77
+ :instance, lexical_sites)
78
+
79
+ build_visibility_mismatch_diagnostic(path, call_node, receiver_type.class_name)
80
+ end
81
+
82
+ # Issue #1984 — the singleton side reads only the reached definer: a private singleton method may be inherited
83
+ # (`private_class_method :new` on a superclass) or come from an extended module, so there is no own-table
84
+ # gate as on the instance side. It reports only:
85
+ #
86
+ # - a constant receiver (`K.x`, `A::K.x`): `self.class.x` or a local may hold a subclass that makes the name
87
+ # public again (`public_class_method :new`, a template-method `def self.x`);
88
+ # - a call inside a `def` ({#load_time_call?}): code that runs while the files load (a class body's
89
+ # `ALL = [K.new]` before its `private_class_method :new`) may run before the call is private;
90
+ # - no class on the chain may have had every singleton name made public (`K.public_class_method(*names)`,
91
+ # `K.singleton_class.class_eval { … }`; {#singleton_wildcard_contested?}).
92
+ def singleton_visibility_mismatch(path, call_node, scope, receiver_type, lexical_sites)
93
+ class_name = receiver_type.class_name
94
+ return nil unless call_node.receiver.is_a?(Prism::ConstantReadNode) ||
95
+ call_node.receiver.is_a?(Prism::ConstantPathNode)
96
+ return nil unless private_singleton_candidate?(scope, call_node.name)
97
+ return nil if load_time_call?(scope, call_node)
98
+ return nil unless reached_definer_visibility(scope, class_name, call_node.name, :singleton) == :private
99
+ return nil if missing_hook_on_chain?(scope, class_name, :singleton)
100
+ return nil if singleton_wildcard_contested?(scope, class_name)
101
+ return nil if ReceiverPredicates.refined_method_in_effect?(class_name, call_node, scope, :singleton,
102
+ lexical_sites)
103
+
104
+ build_visibility_mismatch_diagnostic(path, call_node, "singleton(#{class_name})")
105
+ end
106
+
107
+ # ADR-129 WD1 — a run-scoped Memo: one `[discovery, names]` slot keyed on the discovery index's identity, the
108
+ # only input the name set reads.
109
+ PRIVATE_NAMES_MEMO = TypingState.memo(
110
+ :check_rules_private_singleton_names,
111
+ run_scoped: true, audit: "private singleton names",
112
+ reason: "one [discovery, names] slot keyed on the discovery index's identity; the name " \
113
+ "set reads only its method-visibility tables"
114
+ )
115
+ private_constant :PRIVATE_NAMES_MEMO
116
+
117
+ # Issue #1984 — the table-only gate the singleton side asks before it reads a chain, as the instance side asks
118
+ # its own table first: whether any class records `name` private, on its singleton side or on its instance
119
+ # side (an extended module's private method is private on the extender's singleton). Without it every
120
+ # `Widget.new` would read, and file the dependency edges of, the whole singleton chain. The set is memoised per
121
+ # discovery index.
122
+ def private_singleton_candidate?(scope, name)
123
+ DependencyRecorder.read_visibility("*", name) if DependencyRecorder.active?
124
+ discovery = scope.discovery
125
+ if Inference::MemoAudit.enabled? && Inference::MemoAudit.suspended?
126
+ return private_recorded_names(discovery.discovered_method_visibilities).include?(name.to_sym)
127
+ end
128
+
129
+ state = TypingState.current
130
+ slot = state[PRIVATE_NAMES_MEMO]
131
+ unless slot && slot[0].equal?(discovery)
132
+ names = Inference::MemoAudit.key_audit("private singleton names") do
133
+ private_recorded_names(discovery.discovered_method_visibilities)
134
+ end
135
+ slot = [discovery, names]
136
+ state[PRIVATE_NAMES_MEMO] = slot
137
+ return slot[1].include?(name.to_sym)
138
+ end
139
+ Inference::MemoAudit.check("private singleton names", slot[1], name) do
140
+ private_recorded_names(discovery.discovered_method_visibilities)
141
+ end.include?(name.to_sym)
142
+ end
143
+
144
+ def private_recorded_names(tables)
145
+ tables.each_value.with_object(Set.new) do |table, names|
146
+ table.each { |slot, visibility| names << (slot.is_a?(Array) ? slot.last : slot) if visibility == :private }
147
+ end
148
+ end
149
+
150
+ # Issue #1984 — whether the call is outside every `def` the file's deferred ranges name. A `def` is named
151
+ # wherever it is written in a class body, at the top level or in a `class_eval` block, so a call in it is not
152
+ # load time and may be reported. The top level, class bodies, and blocks and lambdas that are not such a `def`
153
+ # (a `map { … }` or `define_method` block, a `-> { }`) are load time and decline: such code may run while the
154
+ # files load, before a later `private_class_method` or in a load order where the private declaration has not
155
+ # run yet.
156
+ def load_time_call?(scope, call_node)
157
+ rows = scope.discovered_deferred_ranges[scope.source_path]
158
+ return true if rows.nil?
159
+
160
+ offset = call_node.location.start_offset
161
+ # A row with no side (a `def` in a block, a `define_method` name) is a block's range or no range at all.
162
+ rows.none? { |(start, finish, _name, kind, _owner)| !kind.nil? && start <= offset && offset < finish }
163
+ end
164
+
165
+ SINGLETON_WILDCARD_SLOT = %i[singleton *].freeze
166
+ private_constant :SINGLETON_WILDCARD_SLOT
167
+
168
+ # Issue #1984 — whether a project class on `class_name`'s singleton chain has its wildcard singleton slot
169
+ # contested: a call the walk saw may have made any of its singleton methods public.
170
+ def singleton_wildcard_contested?(scope, class_name)
171
+ discovery = scope.discovery
172
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :singleton, :methods)
173
+ chain.entries.any? do |entry|
174
+ DependencyRecorder.read_visibility(entry.name, SINGLETON_WILDCARD_SLOT.last) unless entry.external?
175
+ !entry.external? && discovery.contested?(:discovered_method_visibilities,
176
+ [entry.name, SINGLETON_WILDCARD_SLOT])
177
+ end
178
+ end
179
+
180
+ VISIBILITY_MISSING_HOOKS = %i[method_missing respond_to_missing?].freeze
181
+ private_constant :VISIBILITY_MISSING_HOOKS
182
+
183
+ # Issue #1984 — whether a project entry on `class_name`'s chain for `side` records a `method_missing` or
184
+ # `respond_to_missing?` on the side that entry stands for (a singleton entry's singleton methods, a mixin's
185
+ # instance methods), or the chain cannot be read whole. Ruby sends a private call to `method_missing`, so
186
+ # such a hook may answer it.
187
+ def missing_hook_on_chain?(scope, class_name, side)
188
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, side, :methods)
189
+ return true if chain.truncated?
190
+
191
+ chain.entries.any? do |entry|
192
+ next false if entry.external?
193
+
194
+ kind = entry.side == :singleton ? :singleton : :instance
195
+ VISIBILITY_MISSING_HOOKS.any? { |hook| records_hook?(scope, entry.name, hook, kind) }
196
+ end
197
+ end
198
+
199
+ def records_hook?(scope, owner, hook, kind)
200
+ return true if scope.discovered_method?(owner, hook, kind)
201
+
202
+ !(kind == :singleton ? scope.singleton_def_for(owner, hook) : scope.user_def_for(owner, hook)).nil?
203
+ end
204
+
205
+ # Issue #1568, ADR-119 C1c — the recorded visibility of the definition a call on `class_name` reaches: the
206
+ # first project entry on the class's {Scope::ResolutionChain} from its head that records the name, so a
207
+ # module the class PREPENDS answers ahead of the class. `Inference::DefinerResolution` reads it with the
208
+ # `:visibility` question's default answer. It is `:undecided` wherever the chain does not settle the order
209
+ # (an unpositioned or forked mixin, a conditional include) or an ancestor ahead of the definer may define
210
+ # the name (one RBS does not know), and the caller declines; a definer whose visibility slot ADR-119's
211
+ # producers contest is not alone (`DefinerResolution` asks the next candidate too), so it is `:undecided`
212
+ # there without a read of the table here. nil is nothing recording the name at all, and the class's own
213
+ # visibility stands. An ancestor the project does not declare is passed over, as the method lookup passes it.
214
+ def reached_definer_visibility(scope, class_name, method_name, side = :instance)
215
+ case Inference::DefinerResolution.resolve(scope, class_name, method_name, side, question: :visibility)
216
+ in Inference::DefinerResolution::Known(answer:)
217
+ answer
218
+ in Inference::DefinerResolution::UNKNOWN
219
+ :undecided
220
+ in Inference::DefinerResolution::ABSENT
221
+ nil
222
+ end
223
+ end
224
+
225
+ def explicit_non_self_receiver?(receiver)
226
+ return false if receiver.nil?
227
+ return false if receiver.is_a?(Prism::SelfNode)
228
+
229
+ true
230
+ end
231
+
232
+ def build_visibility_mismatch_diagnostic(path, call_node, receiver_name)
233
+ Diagnostic.from_message_loc(
234
+ call_node,
235
+ rule: RULE_VISIBILITY_MISMATCH,
236
+ path: path,
237
+ message: "private method `#{call_node.name}' called on #{receiver_name} receiver",
238
+ severity: :error,
239
+ receiver_type: receiver_name,
240
+ method_name: call_node.name.to_s
241
+ )
242
+ end
243
+
244
+ private_class_method :instance_visibility_mismatch, :singleton_visibility_mismatch,
245
+ :private_singleton_candidate?, :private_recorded_names, :load_time_call?,
246
+ :singleton_wildcard_contested?, :missing_hook_on_chain?, :records_hook?,
247
+ :reached_definer_visibility, :explicit_non_self_receiver?,
248
+ :build_visibility_mismatch_diagnostic
249
+ end
250
+ end
251
+ end
252
+ end
@@ -3,6 +3,8 @@
3
3
  require "prism"
4
4
 
5
5
  require_relative "../../source/node_children"
6
+ require_relative "../diagnostic"
7
+ require_relative "rule_ids"
6
8
 
7
9
  module Rigor
8
10
  module Analysis
@@ -69,6 +71,31 @@ module Rigor
69
71
  @results.freeze
70
72
  end
71
73
 
74
+ # ADR-116 WD1 — the message builder for this collector's results, moved here from `CheckRules`.
75
+ # {.diagnostics} is called by `CheckRules.diagnose` through its collector table; the rest is private to it.
76
+
77
+ # ADR-100 WD2 — `static.value-use.void`. Its value-context slot inspection rides the shared per-node
78
+ # {RuleWalk} like the other collectors, so the file is not re-traversed for it. Each result is a
79
+ # value-context use of a call whose author-declared `-> void` return the engine recovered to `top`.
80
+ def self.diagnostics(path, results)
81
+ results.map do |result|
82
+ build_void_value_use_diagnostic(path, result)
83
+ end
84
+ end
85
+
86
+ def self.build_void_value_use_diagnostic(path, result)
87
+ Diagnostic.from_node(
88
+ result.void_node,
89
+ rule: RULE_VALUE_USE_VOID,
90
+ path: path,
91
+ message: "value use of `void': `#{result.origin.label}' declares `-> void', so its return " \
92
+ "recovers to `top' and should not be used as a value",
93
+ severity: :warning
94
+ )
95
+ end
96
+
97
+ private_class_method :build_void_value_use_diagnostic
98
+
72
99
  private
73
100
 
74
101
  def walk(node)