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,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../type"
4
+ require_relative "../diagnostic"
5
+ require_relative "override_support"
6
+ require_relative "rule_ids"
7
+
8
+ module Rigor
9
+ module Analysis
10
+ module CheckRules
11
+ # ADR-116 WD1 — `def.override-return-widened` (ADR-35 slice 2): an override whose declared return is
12
+ # wider than the one it inherits.
13
+ #
14
+ # The one entry point is {.diagnostic}, called by `CheckRules.main_pass_node_diagnostics`. It reads
15
+ # {OverrideSupport} and calls nothing else in `CheckRules`.
16
+ module OverrideReturnWidened
17
+ module_function
18
+
19
+ # ADR-35 slice 2 — `def.override-return-widened`. The Liskov signature rule for returns
20
+ # (covariance): an override may *narrow* the return it inherits (return a more specific type) but
21
+ # MUST NOT *widen* it. A caller holding the supertype uses the result as the parent's return type;
22
+ # a wider override return breaks that use.
23
+ #
24
+ # WD1 gate (proper, type-direction): both the override and the shadowed ancestor method must carry
25
+ # an explicitly-authored RBS signature. The override side is gated by `defined_on?` (the RBS
26
+ # method is declared on the overriding class itself, not merely inherited); the parent side is the
27
+ # nearest project-discovered ancestor whose RBS declares the method. Inference-only either side →
28
+ # silent.
29
+ #
30
+ # Fires only on a proven (`:no`) widening; generic / `untyped` / `self` parent returns degrade to
31
+ # `Dynamic[Top]` and accept everything, so they stay silent (FP-safe). `self`/`instance` are
32
+ # translated with `self_type: nil` on both sides, so a parent `-> self` and an override `-> self`
33
+ # never fire.
34
+ def diagnostic(path, def_node, scope_index)
35
+ resolved = OverrideSupport.resolve_authored_override(def_node, scope_index)
36
+ return nil if resolved.nil?
37
+
38
+ scope, override_method, parent_class, parent_method = resolved
39
+ parent_type_vars = OverrideSupport.ancestor_instantiation_type_vars(scope, parent_class)
40
+ override_return = OverrideSupport.declared_return_union(override_method, scope.environment)
41
+ parent_return = OverrideSupport.declared_return_union(parent_method, scope.environment,
42
+ type_vars: parent_type_vars)
43
+ return nil if override_return.nil? || parent_return.nil?
44
+ return nil if OverrideSupport.dynamic_top?(parent_return) # untyped / unbound-generic parent contract
45
+ return nil if void_over_nil?(override_method, parent_return)
46
+
47
+ return nil unless parent_return.accepts(override_return).no?
48
+
49
+ build_override_return_widened_diagnostic(
50
+ path, def_node, parent_class, parent_return, override_return
51
+ )
52
+ end
53
+
54
+ # A `-> void` override of a parent return of exactly `nil` (or `NilClass`) is exempt (#3046): both
55
+ # say "no useful value", and callers cannot use a void return. Keyed on the declared `void` of every
56
+ # arm (the translator reads `void` as top, so a `-> top` override still fires); any non-nil parent
57
+ # return still fires.
58
+ def void_over_nil?(override_method, parent_return)
59
+ return false unless nil_type?(parent_return)
60
+
61
+ override_method.method_types.all? { |mt| mt.type.return_type.is_a?(::RBS::Types::Bases::Void) }
62
+ end
63
+
64
+ # `nil`, or the nominal `NilClass`, whose only value it is.
65
+ def nil_type?(type)
66
+ return type.value.nil? if type.is_a?(Type::Constant)
67
+
68
+ type.is_a?(Type::Nominal) && type.class_name == "NilClass" && type.type_args.empty?
69
+ end
70
+
71
+ def build_override_return_widened_diagnostic(path, def_node, parent_class, parent_return, override_return)
72
+ Diagnostic.from_name_loc(
73
+ def_node,
74
+ rule: RULE_OVERRIDE_RETURN_WIDENED,
75
+ path: path,
76
+ message: "return type of `#{def_node.name}' widened from #{parent_return.describe(:short)} " \
77
+ "to #{override_return.describe(:short)} (overrides #{parent_class}##{def_node.name}); " \
78
+ "breaks substitutability",
79
+ severity: :warning,
80
+ method_name: def_node.name.to_s
81
+ )
82
+ end
83
+
84
+ private_class_method :void_over_nil?, :nil_type?, :build_override_return_widened_diagnostic
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,246 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../reflection"
4
+ require_relative "../../scope"
5
+ require_relative "../../type"
6
+ require_relative "../../inference/definer_resolution"
7
+ require_relative "../../inference/rbs_type_translator"
8
+
9
+ module Rigor
10
+ module Analysis
11
+ module CheckRules
12
+ # ADR-116 WD1 — the resolution the three Liskov override rules share, and the helpers {ReturnType} shares
13
+ # with them: the authored-override walk ({.resolve_authored_override}), the first ancestor an override
14
+ # reaches ({.overridden_ancestors_start}), the generic-instantiation map
15
+ # ({.ancestor_instantiation_type_vars}), {.defined_on?}, {.declared_return_union} and {.dynamic_top?}.
16
+ #
17
+ # Called by {OverrideVisibility}, {OverrideReturnWidened}, {OverrideParamNarrowed} and {ReturnType}; it
18
+ # calls nothing in `CheckRules`. The last three stay here: the override rules and the walk above call them too.
19
+ module OverrideSupport
20
+ module_function
21
+
22
+ # ADR-35 WD10 (issue #1716) — the object-lifecycle hooks the three override rules skip. `initialize` is
23
+ # reached through `Class#new` on the class the caller names, and the copy hooks are handed an instance
24
+ # of the receiver's own class by `dup` / `clone`, so neither is called with the PARENT's declared
25
+ # arguments through a parent-typed reference; `new` / `dup` / `clone` discard all four returns.
26
+ LIFECYCLE_HOOKS = %i[initialize initialize_copy initialize_dup initialize_clone].freeze
27
+
28
+ # `type_vars:` — ADR-35 WD9 tier 1. When the caller supplies a generic-instantiation
29
+ # substitution map (parent-side, keyed by the parent class's declared type-parameter names),
30
+ # a `-> T` return is translated at its instantiated type (`-> Integer`) rather than degrading
31
+ # to `Dynamic[Top]`. The default empty map is the pre-WD9 behaviour, under which any type
32
+ # variable degrades to `Dynamic[Top]` and the rule stays silent.
33
+ def declared_return_union(method_def, environment, type_vars: {})
34
+ translated = method_def.method_types.filter_map do |mt|
35
+ Inference::RbsTypeTranslator.translate(
36
+ mt.type.return_type,
37
+ self_type: nil, instance_type: nil, type_vars: type_vars,
38
+ alias_expander: environment&.rbs_loader
39
+ )
40
+ rescue StandardError
41
+ nil
42
+ end
43
+ return nil if translated.empty?
44
+
45
+ translated.size == 1 ? translated.first : Type::Combinator.union(*translated)
46
+ end
47
+
48
+ def dynamic_top?(type)
49
+ type.is_a?(Type::Dynamic) || (type.respond_to?(:top?) && type.top?.yes?)
50
+ end
51
+
52
+ # The project ancestors whose definitions `class_name`'s own `def` OVERRIDES are the entries AFTER the
53
+ # class on its {Scope::ResolutionChain}, nearest first; {#nearest_ancestor_visibility} and
54
+ # {#nearest_ancestor_method_def} each read the first of them that answers their question through
55
+ # `Inference::DefinerResolution` (ADR-119 C1c), `from:` the position after the class. Both are instance
56
+ # side only: the singleton override rules do not exist, and the singleton side's own read lands with
57
+ # ADR-119's singleton design.
58
+ #
59
+ # Issue #1568 — the walk this replaced was breadth-first from the class's direct ancestors, and those
60
+ # include the modules the class PREPENDS. A prepended module sits BEFORE the class in Ruby's order: its
61
+ # `def` overrides the class's, not the other way round, so `prepend P` with a public `P#foo` and a
62
+ # private `C#foo` reported `C#foo` as reducing `P#foo`'s visibility. It also reached `Base` before a
63
+ # module included through an included module (#1567's order).
64
+ #
65
+ # ADR-119 WD3 — for the two override lints *absent* always counts: a chain that does not settle (a mixin
66
+ # whose position the tables cannot vouch for, a fork whose worlds disagree, a conditional or
67
+ # block-scoped `include`), or an external ancestor ahead of the parent that RBS does not know or knows
68
+ # and declares the name in, is `UNKNOWN`, and the lint stays silent rather than answer from the
69
+ # breadth-first order the walk used before (`Scope::ResolutionChain::MasterOrder`) where the order is
70
+ # not known. The two lints read only the `[owner, value]` of a `Known`.
71
+ #
72
+ # ADR-46 slice 3 — an ancestor the project does not declare ends the walk's reach into it, and a class
73
+ # defined LATER under that name would change the answer; `DefinerResolution` files the negative class
74
+ # edge of each external entry it tests.
75
+ def overridden_ancestors_start(scope, class_name)
76
+ # Issue #2979 — past the entries of the names bound to the same class (`K = Struct.new("Named", …)`):
77
+ # a `def` there redefines the class's method rather than overriding an ancestor's.
78
+ chain = Scope::ResolutionChain.for(scope, class_name.to_s, :instance, :methods)
79
+ chain.ancestors_start(scope, class_name.to_s)
80
+ end
81
+
82
+ # The authored-override resolution shared by the Liskov override rules
83
+ # (`def.override-return-widened` and `def.override-param-narrowed`): the def must be an instance
84
+ # method whose own class declares it in RBS, and a project-discovered ancestor must also declare
85
+ # it. Returns `[scope, override_method, parent_class, parent_method]`, or nil (the rule does not
86
+ # fire) when any gate is unmet.
87
+ def resolve_authored_override(def_node, scope_index)
88
+ return nil unless def_node.receiver.nil? # instance methods only (singleton: follow-on)
89
+ return nil if LIFECYCLE_HOOKS.include?(def_node.name) # ADR-35 WD10: outside substitutability
90
+
91
+ scope = scope_index[def_node]
92
+ return nil if scope.nil?
93
+
94
+ self_type = scope.self_type
95
+ return nil unless self_type.respond_to?(:class_name)
96
+
97
+ class_name = self_type.class_name.to_s
98
+ method_name = def_node.name
99
+
100
+ override_method = safe_instance_method_definition(class_name, method_name, scope)
101
+ return nil if override_method.nil?
102
+ return nil unless defined_on?(override_method, class_name)
103
+
104
+ parent = nearest_ancestor_method_def(scope, class_name, method_name)
105
+ return nil if parent.nil?
106
+
107
+ parent_class, parent_method = parent
108
+ [scope, override_method, parent_class, parent_method]
109
+ end
110
+
111
+ # `[defining_class, RBS::Definition::Method]` for the nearest project-discovered ancestor whose
112
+ # RBS declares `method_name` (not the starting class's own declaration), or nil.
113
+ def nearest_ancestor_method_def(scope, class_name, method_name)
114
+ from = overridden_ancestors_start(scope, class_name)
115
+ return nil if from.nil?
116
+
117
+ passed_owners = []
118
+ case Inference::DefinerResolution.resolve(scope, class_name, method_name, :instance,
119
+ question: :override, from: from) do |ancestor|
120
+ override_parent_answer(scope, ancestor, method_name, passed_owners)
121
+ end
122
+ in Inference::DefinerResolution::Known(owner:, answer:)
123
+ # The nearest declaration must be the one that answers: an ancestor asked past for a project
124
+ # owner the walk never reached (one the Ruby chain does not hold) leaves the rule silent.
125
+ unless answer.last == RBS_ONLY_OWNER ||
126
+ passed_owners.any? { |passed| passed != normalize_class_name(owner) }
127
+ method_def = safe_instance_method_definition(owner, method_name, scope)
128
+ method_def && [owner, method_def]
129
+ end
130
+ in Inference::DefinerResolution::UNKNOWN # the case/in contract needs one arm per answer
131
+ nil
132
+ in Inference::DefinerResolution::ABSENT # rubocop:disable Lint/DuplicateBranch -- see above
133
+ nil
134
+ end
135
+ end
136
+
137
+ # The answer an RBS-only owner gives the override walk: it ends the walk, and the rule stays silent.
138
+ RBS_ONLY_OWNER = :rbs_only_owner
139
+ private_constant :RBS_ONLY_OWNER
140
+
141
+ # One project ancestor's answer to the override walk. The signature text is the answer two worlds
142
+ # compare: an `RBS::Definition::Method` has no structural equality of its own. Only an ancestor whose
143
+ # own RBS declares the method answers with it (#3046). One that inherits the declaration from another
144
+ # project class or module is asked past, so that owner answers at its own entry; the owner is recorded
145
+ # in `passed_owners`, which the answering owner must match. One that inherits it from an RBS-only owner
146
+ # the project chain cannot see ends the walk silent ({RBS_ONLY_OWNER}): that owner is the nearest
147
+ # declaration, and RBS-only ancestors are #130's.
148
+ def override_parent_answer(scope, ancestor, method_name, passed_owners)
149
+ method_def = safe_instance_method_definition(ancestor, method_name, scope)
150
+ return nil if method_def.nil?
151
+ return method_def.method_types.map(&:to_s) if declared_by?(method_def, ancestor)
152
+
153
+ owner = method_def.implemented_in || method_def.defined_in
154
+ return RBS_ONLY_OWNER unless owner && scope.discovered_classes.key?(normalize_class_name(owner))
155
+
156
+ passed_owners << normalize_class_name(owner)
157
+ nil
158
+ end
159
+
160
+ # True when `ancestor`'s own RBS declares `method_def`: on its body, or through an interface it
161
+ # includes itself (RBS records the including class as `implemented_in`).
162
+ def declared_by?(method_def, ancestor)
163
+ return true if defined_on?(method_def, ancestor)
164
+ return false unless method_def.defined_in&.interface?
165
+
166
+ normalize_class_name(method_def.implemented_in) == normalize_class_name(ancestor)
167
+ end
168
+
169
+ def safe_instance_method_definition(class_name, method_name, scope)
170
+ Reflection.instance_method_definition(class_name, method_name, scope: scope)
171
+ rescue StandardError
172
+ nil
173
+ end
174
+
175
+ # True when `method_def`'s RBS declaration lives on `class_name` itself (rather than being
176
+ # inherited from an ancestor).
177
+ def defined_on?(method_def, class_name)
178
+ defined_in = method_def.defined_in
179
+ return false if defined_in.nil?
180
+
181
+ normalize_class_name(defined_in.to_s) == normalize_class_name(class_name)
182
+ end
183
+
184
+ def normalize_class_name(name)
185
+ name.to_s.delete_prefix("::")
186
+ end
187
+
188
+ # ADR-35 WD9 tier 1 — generic-instantiation-aware comparison. Builds the substitution map
189
+ # `{ parent_type_param_name => instantiated Rigor::Type }` for the parent contract as the
190
+ # overriding subclass instantiates it (RBS `class Sub < Parent[Concrete]` / `include
191
+ # _Iface[Concrete]`). Mirrors the ADR-4 Phase 2d dispatcher zip (`build_type_vars`): the
192
+ # parent's declared type-parameter names zipped against the subclass-side instantiation args.
193
+ #
194
+ # Returns `{}` — restoring the pre-WD9 behaviour under which any parent type variable degrades
195
+ # to `Dynamic[Top]` and the rule stays silent — whenever the subclass does not instantiate the
196
+ # ancestor generically, the RBS definition cannot be built, arities disagree, or an argument
197
+ # fails to translate. This is the FP-safety contract: substitution only ever *adds* precision
198
+ # (a `-> T` compared at `-> Integer`); an unbound / unresolved generic keeps degrading to
199
+ # silence, never a false `:no`.
200
+ def ancestor_instantiation_type_vars(scope, parent_class)
201
+ self_type = scope.self_type
202
+ return {} unless self_type.respond_to?(:class_name)
203
+
204
+ args = ancestor_instantiation_args(scope, self_type.class_name.to_s, parent_class)
205
+ return {} if args.nil? || args.empty?
206
+
207
+ param_names = Reflection.class_type_param_names(parent_class, scope: scope)
208
+ return {} if param_names.empty? || param_names.size != args.size
209
+
210
+ translated = args.map do |arg|
211
+ Inference::RbsTypeTranslator.translate(
212
+ arg, self_type: nil, instance_type: nil, type_vars: {},
213
+ alias_expander: scope.environment&.rbs_loader
214
+ )
215
+ rescue StandardError
216
+ nil
217
+ end
218
+ return {} if translated.any?(&:nil?)
219
+
220
+ param_names.zip(translated).to_h
221
+ end
222
+
223
+ # The RBS type arguments the subclass applies to `parent_class` in its ancestry, or nil when
224
+ # the subclass has no RBS definition or does not name that ancestor. Reads the resolved
225
+ # instance-ancestor list (`RBS::Definition#ancestors`), whose `Ancestor::Instance` entries carry
226
+ # the instantiation `.args` for each superclass / included module. Fail-soft: any RBS build
227
+ # error yields nil, which the caller treats as "no instantiation" and degrades to silence.
228
+ def ancestor_instantiation_args(scope, subclass_name, parent_class)
229
+ definition = Reflection.instance_definition(subclass_name, scope: scope)
230
+ return nil if definition.nil?
231
+
232
+ target = normalize_class_name(parent_class)
233
+ ancestor = definition.ancestors.ancestors.find do |anc|
234
+ anc.respond_to?(:args) && normalize_class_name(anc.name.to_s) == target
235
+ end
236
+ ancestor&.args
237
+ rescue ::RBS::BaseError, StandardError
238
+ nil
239
+ end
240
+
241
+ private_class_method :nearest_ancestor_method_def, :override_parent_answer, :declared_by?,
242
+ :safe_instance_method_definition, :normalize_class_name, :ancestor_instantiation_args
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../inference/definer_resolution"
4
+ require_relative "../diagnostic"
5
+ require_relative "override_support"
6
+ require_relative "rule_ids"
7
+
8
+ module Rigor
9
+ module Analysis
10
+ module CheckRules
11
+ # ADR-116 WD1 — `def.override-visibility-reduced` (ADR-35 slice 1): an instance-method override that
12
+ # reduces the visibility it inherits from a project ancestor.
13
+ #
14
+ # The one entry point is {.diagnostic}, called by `CheckRules.main_pass_node_diagnostics`. It reads
15
+ # {OverrideSupport} and `Inference::DefinerResolution` and calls nothing else in `CheckRules`.
16
+ module OverrideVisibility
17
+ module_function
18
+
19
+ # ADR-35 slice 1 — the public > protected > private ordering used to decide whether an override reduces
20
+ # visibility. The override rules' ancestor walk is bounded by `Scope::ANCESTOR_WALK_LIMIT`, the
21
+ # resolution chain's.
22
+ VISIBILITY_RANK = { public: 2, protected: 1, private: 0 }.freeze
23
+
24
+ # The names CRuby makes private on every non-singleton `def` whatever section it is written in
25
+ # (`vm_method.c` `rb_method_entry_make`), so a source-discovered visibility for them is not the
26
+ # runtime one and `def.override-visibility-reduced` cannot prove a reduction.
27
+ IMPLICITLY_PRIVATE_METHODS = (OverrideSupport::LIFECYCLE_HOOKS + %i[respond_to_missing?]).freeze
28
+
29
+ # ADR-35 slice 1 — `def.override-visibility-reduced`. The Liskov signature rule for visibility: an
30
+ # instance-method override MUST NOT reduce the visibility it inherits (public → protected/private,
31
+ # or protected → private), because a caller holding the supertype that invokes the method breaks
32
+ # when handed the subtype.
33
+ #
34
+ # Slice-1 scope (ADR-35 WD1, visibility carve-out): both the override and the shadowed method must
35
+ # have a STATICALLY OBSERVABLE visibility. The override's visibility is read from the
36
+ # source-discovered table; the parent is resolved against the project-discovered ancestor chain
37
+ # (user-source classes / modules only — RBS-known ancestors, whose accessibility RBS models as
38
+ # public/private only, are a deferred follow-on). When either side is not observable the rule stays
39
+ # silent.
40
+ def diagnostic(path, def_node, scope_index)
41
+ return nil unless def_node.receiver.nil? # instance methods only
42
+ return nil if IMPLICITLY_PRIVATE_METHODS.include?(def_node.name)
43
+
44
+ scope = scope_index[def_node]
45
+ return nil if scope.nil?
46
+
47
+ self_type = scope.self_type
48
+ return nil unless self_type.respond_to?(:class_name)
49
+
50
+ class_name = self_type.class_name.to_s
51
+ method_name = def_node.name
52
+
53
+ override_visibility = scope.discovered_method_visibility(class_name, method_name)
54
+ return nil if override_visibility.nil?
55
+ # Issue #2013 — a contested slot (a later `define_method` or alias gave the name another visibility, or a
56
+ # write the walk cannot place) proves no reduction.
57
+ return nil if scope.discovery.contested?(:discovered_method_visibilities, [class_name, method_name])
58
+
59
+ parent = nearest_ancestor_visibility(scope, class_name, method_name)
60
+ return nil if parent.nil?
61
+
62
+ parent_class, parent_visibility = parent
63
+ # Unknown ancestor visibility (e.g. the defining file was not in the analyzed set) → cannot
64
+ # prove a reduction, stay silent.
65
+ return nil if parent_visibility.nil?
66
+ return nil unless visibility_reduced?(parent_visibility, override_visibility)
67
+ # Issue #1569 — a module function's instance copy is private because `module_function` made it so; the
68
+ # author's intent is the public singleton copy, so that reduction is not reported.
69
+ return nil if scope.module_function_def?(class_name, def_node)
70
+
71
+ build_override_visibility_diagnostic(
72
+ path, def_node, parent_class, parent_visibility, override_visibility
73
+ )
74
+ end
75
+
76
+ # Returns true when `override_visibility` is strictly more restrictive than `parent_visibility`
77
+ # under the public > protected > private ordering.
78
+ #
79
+ # The nil guard below is defence, not dead code. `VISIBILITY_RANK` is a closed literal hash read
80
+ # with a dynamic key, so a symbol outside the table reads as nil at runtime. The engine folds that
81
+ # read to the nil-free value union `0 | 1 | 2`, which `internal-spec/inference-engine.md` declares
82
+ # OPTIMISTIC rather than proof — and since issue #313 that mark survives the `.nil?` fold and the
83
+ # `||` composition, so no suppression directive is needed here.
84
+ def visibility_reduced?(parent_visibility, override_visibility)
85
+ parent_rank = VISIBILITY_RANK[parent_visibility]
86
+ override_rank = VISIBILITY_RANK[override_visibility]
87
+ return false if parent_rank.nil? || override_rank.nil?
88
+
89
+ override_rank < parent_rank
90
+ end
91
+
92
+ # `[defining_class, visibility]` for the nearest user-source ancestor that defines an instance
93
+ # method `method_name`, or nil.
94
+ def nearest_ancestor_visibility(scope, class_name, method_name)
95
+ from = OverrideSupport.overridden_ancestors_start(scope, class_name)
96
+ return nil if from.nil?
97
+
98
+ case Inference::DefinerResolution.resolve(scope, class_name, method_name, :instance,
99
+ question: :override, from: from) do |ancestor|
100
+ # Stop at the nearest ancestor that DEFINES the method; its visibility may be nil (unknown) —
101
+ # the caller treats unknown as "cannot prove a reduction" and stays silent. Never fabricate
102
+ # `:public` from a missing entry (that produced a large false-positive cluster on
103
+ # cross-file Rails concerns). The `:unrecorded` marker stands for that nil in the answer.
104
+ next unless scope.user_def_for(ancestor, method_name)
105
+
106
+ scope.discovered_method_visibility(ancestor, method_name) || :unrecorded
107
+ end
108
+ in Inference::DefinerResolution::Known(answer: [owner, visibility])
109
+ [owner, visibility == :unrecorded ? nil : visibility]
110
+ in Inference::DefinerResolution::UNKNOWN # the case/in contract needs one arm per answer
111
+ nil
112
+ in Inference::DefinerResolution::ABSENT # rubocop:disable Lint/DuplicateBranch -- see above
113
+ nil
114
+ end
115
+ end
116
+
117
+ def build_override_visibility_diagnostic(path, def_node, parent_class, parent_visibility, override_visibility)
118
+ Diagnostic.from_name_loc(
119
+ def_node,
120
+ rule: RULE_OVERRIDE_VISIBILITY_REDUCED,
121
+ path: path,
122
+ message: "visibility of `#{def_node.name}' reduced from #{parent_visibility} to " \
123
+ "#{override_visibility} (overrides #{parent_class}##{def_node.name}); " \
124
+ "breaks substitutability",
125
+ severity: :warning,
126
+ method_name: def_node.name.to_s
127
+ )
128
+ end
129
+
130
+ private_class_method :visibility_reduced?, :nearest_ancestor_visibility,
131
+ :build_override_visibility_diagnostic
132
+ end
133
+ end
134
+ end
135
+ end
@@ -51,7 +51,8 @@ module Rigor
51
51
  # the return memo.
52
52
  #
53
53
  # - a value COPIED out of such a constant before the predicate reads it (issue #667) — into a local
54
- # (`m = AppConfig::MODE; m == :x`), into an ivar (`@mode = AppConfig::MODE` in `initialize`), or
54
+ # (`m = AppConfig::MODE; m == :x`), into an ivar (`@mode = AppConfig::MODE` in `initialize`) or a class
55
+ # variable (`@@mode = AppConfig::MODE` in `def self.setup`, issue #3443), or
55
56
  # through a same-file alias (`MODE2 = AppConfig::MODE`). A copy has no spelling for the root walk to
56
57
  # recognise, so the provenance is stamped on the value where the copy happens and travels with it:
57
58
  # the first two on the `Scope` flow carrier `published_constant_sourced` (dropped by any rebinding,
@@ -78,10 +79,10 @@ module Rigor
78
79
  return false if node.nil? || depth > MAX_DEPTH || scope.nil?
79
80
 
80
81
  case node
81
- when Prism::ConstantReadNode then scope.published_constant?(node.name.to_s)
82
- when Prism::ConstantPathNode then published_path?(node, scope)
82
+ when Prism::ConstantReadNode, Prism::ConstantPathNode then published_constant_node?(node, scope)
83
83
  when Prism::LocalVariableReadNode then scope.published_constant_sourced?(:local, node.name)
84
84
  when Prism::InstanceVariableReadNode then scope.published_constant_sourced?(:ivar, node.name)
85
+ when Prism::ClassVariableReadNode then scope.published_constant_sourced?(:cvar, node.name)
85
86
  when Prism::CallNode then rooted_call?(node, scope, depth)
86
87
  else rooted_through_composition?(node, scope, depth)
87
88
  end
@@ -111,14 +112,6 @@ module Rigor
111
112
  callee_reads_published_constant?(node, scope)
112
113
  end
113
114
 
114
- # The reference AS WRITTEN (`AppConfig::MODE`), so `Scope#published_constant?` can match its local
115
- # exemption exactly rather than on the last segment. A path that renders no qualified name falls back
116
- # to its own last segment, which is all there is to ask about.
117
- def published_path?(node, scope)
118
- rendered = constant_spelling(node)
119
- !rendered.nil? && scope.published_constant?(rendered)
120
- end
121
-
122
115
  # The one hop. Resolves the callee through the ordinary `Scope` accessors (so the ADR-46 recorder sees
123
116
  # the read and the caller gains an edge to the callee — the conservative direction) and scans its
124
117
  # body. Fails soft to false: a resolution this cannot make is not a reason to withhold.
@@ -129,14 +122,28 @@ module Rigor
129
122
  def callee_reads_published_constant?(node, scope)
130
123
  return false if scope.published_constant_names.empty? && !Analysis::DependencyRecorder.recording?
131
124
 
132
- def_node = resolve_callee(node, scope)
125
+ def_node, body_self = resolve_callee(node, scope)
133
126
  return false if def_node.nil?
134
127
 
135
- body_reads_published_constant?(def_node.body, scope)
128
+ body_reads_published_constant?(def_node.body, callee_body_scope(def_node, body_self, scope))
136
129
  rescue StandardError
137
130
  false
138
131
  end
139
132
 
133
+ # Issue #3437 — the scope the callee's body is asked under. `Scope#published_constant?` matches a file's
134
+ # own constant by the names a reference can resolve to WHERE IT IS WRITTEN, and the body's constants are
135
+ # written in the callee, not at the call: `AppConfig.production?` called from the top level reads `MODE`
136
+ # inside `module AppConfig`. So the scope carries the `Module.nesting` the declaration walk recorded for
137
+ # the def and the owner the call resolved it on, which are what `ExpressionTyper` types the same body
138
+ # under. An unrecorded nesting stays nil, and the reader peels the owner's name as it does there. A
139
+ # top-level def's `body_self` is nil and its unrecorded nesting is the top level's empty one, so no class
140
+ # the CALL sits in lends the body a namespace.
141
+ def callee_body_scope(def_node, body_self, scope)
142
+ nesting = scope.discovery.recorded_def_nesting(def_node)
143
+ nesting ||= [] if body_self.nil?
144
+ scope.with_self_type(body_self).with_lexical_nesting(nesting)
145
+ end
146
+
140
147
  # `foo` / `self.foo` resolve against the enclosing self. Every other receiver resolves through its
141
148
  # inferred TYPE, never through the name it was spelled with: `Inner.production?` inside `module Outer`
142
149
  # is `Outer::Inner`, and keying the table on the bare `Inner` both misses that (a new warning on
@@ -144,36 +151,65 @@ module Rigor
144
151
  # the defect family #635 fixed in `Narrowing`, so the engine's own resolver answers it rather than a
145
152
  # walk reimplemented here. Going through the type also covers a call-chain receiver for free —
146
153
  # `KlassConfig.new.production?` types `Nominal[KlassConfig]`, whose instance table is the one to read.
154
+ #
155
+ # Answers `[def_node, body_self]`: the def and the `self` its body runs with, which is the receiver's type
156
+ # for a typed receiver, the owner the def was found on for an implicit self, and nil for a top-level def.
147
157
  def resolve_callee(node, scope)
148
158
  receiver = node.receiver
149
159
  return resolve_self_call(node.name, scope) if receiver.nil? || receiver.is_a?(Prism::SelfNode)
150
160
 
151
- resolve_typed_receiver_call(receiver, node.name, scope)
161
+ receiver_type = scope.type_of(receiver)
162
+ [resolve_typed_receiver_call(receiver_type, node.name, scope), receiver_type]
152
163
  end
153
164
 
154
165
  # A `Singleton[X]` receiver names the singleton table (`AppConfig.production?`, a module's own
155
166
  # predicates); a `Nominal[X]` receiver names the instance one. Anything else — `Dynamic`, a constant
156
167
  # that resolves to no project class — is not a hop this walk can make.
157
- def resolve_typed_receiver_call(receiver, method_name, scope)
158
- receiver_type = scope.type_of(receiver)
168
+ def resolve_typed_receiver_call(receiver_type, method_name, scope)
159
169
  case receiver_type
160
170
  when Type::Singleton then scope.singleton_def_for(receiver_type.class_name, method_name)
161
171
  when Type::Nominal then scope.user_def_for(receiver_type.class_name, method_name)
162
172
  end
163
173
  end
164
174
 
165
- # A `Singleton[X]` self (a `def self.…` body) names the singleton table; a `Nominal[X]` self names
166
- # the instance one; no class at all is the top-level pseudo-class.
175
+ # A `Singleton[X]` self (a `def self.…` body) names the singleton chain: the class's own `def self.`s, the
176
+ # modules it `extend`s, then its superclass's singleton side. A `Nominal[X]` self names the instance chain:
177
+ # the class's own defs, then its project ancestors' (an `include`d helper, a superclass's predicate). An
178
+ # ancestor-found body runs with the class that defines it as `self`. No class at all is the top-level
179
+ # pseudo-class.
180
+ #
181
+ # Issue #3437 review — a chain that misses falls back to the top-level table, and only then: Ruby finds the
182
+ # class's own or an ancestor's method first, so a def there shadows a top-level one of the same name. A
183
+ # top-level def is a private `Object` method, so an implicit-self call reaches it from any class body;
184
+ # before the ivar census asked under the class's scope, its implicit-self hop resolved at the top level and
185
+ # found it, and a lookup that stopped at the class lost that withholding.
167
186
  def resolve_self_call(method_name, scope)
168
187
  self_type = scope.self_type
169
188
  owner = self_type.respond_to?(:class_name) ? self_type.class_name : nil
170
- return scope.top_level_def_for(method_name) if owner.nil?
189
+ found = owner.nil? ? nil : resolve_class_self_call(self_type, owner, method_name, scope)
190
+ found || [scope.top_level_def_for(method_name), nil]
191
+ end
171
192
 
172
- if self_type.is_a?(Type::Singleton)
173
- scope.singleton_def_for(owner, method_name)
174
- else
175
- scope.user_def_for(owner, method_name)
176
- end
193
+ def resolve_class_self_call(self_type, owner, method_name, scope)
194
+ singleton = self_type.is_a?(Type::Singleton)
195
+ def_node, definer =
196
+ if singleton
197
+ scope.singleton_def_through_ancestors(owner, method_name)
198
+ else
199
+ scope.user_def_through_ancestors(owner, method_name)
200
+ end
201
+ return nil if def_node.nil?
202
+ return [def_node, self_type] if definer == owner
203
+
204
+ [def_node, definer_self_type(definer, singleton)]
205
+ end
206
+
207
+ # The `self` an ancestor-found body is asked with. Only its class name reaches the question
208
+ # ({Scope#published_constant?} reads that name's `Module.nesting` fallback and `:constants` chain), so a
209
+ # singleton chain's definer (a superclass's `def self.`, a module the class `extend`s) is named by its own
210
+ # name on the side the chain was walked.
211
+ def definer_self_type(definer, singleton)
212
+ singleton ? Type::Combinator.singleton_of(definer) : Type::Combinator.nominal_of(definer)
177
213
  end
178
214
 
179
215
  def body_reads_published_constant?(root, scope)
@@ -183,14 +219,18 @@ module Rigor
183
219
 
184
220
  # Issue #3186 — the constant spellings the one hop's body scan would ask `Scope#published_constant?` about,
185
221
  # sorted and de-duplicated: every node {#body_reads_published_constant?} visits within its budget, in its
186
- # order. The hop's answer for any reader is a function of this list and of that reader's publication
187
- # tables, so the ADR-89 WD2 gate stores it beside a def's return summary and compares it on an edit: a
188
- # reader the gate skips would otherwise keep a withholding (or a firing) the edited body no longer earns.
222
+ # order. A reference written with a leading `::` keeps it here (`::MODE`), because the question differs:
223
+ # it names the top level alone (issue #3437). The ADR-89 WD2 gate stores the list beside a def's return
224
+ # summary and compares it on an edit: a reader the gate skips would otherwise keep a withholding (or a
225
+ # firing) the edited body no longer earns. The hop's answer also turns on the `Module.nesting` the def is
226
+ # written under ({#callee_body_scope}), which no edit of the def's own text moves.
189
227
  def body_constant_spellings(root)
190
228
  spellings = []
191
229
  scan_body(root) do |node|
192
230
  spelling = constant_spelling(node)
193
- spellings << spelling unless spelling.nil?
231
+ next if spelling.nil?
232
+
233
+ spellings << (Source::ConstantPath.rooted?(node) ? "::#{spelling}" : spelling)
194
234
  end
195
235
  spellings.uniq.sort
196
236
  end
@@ -214,13 +254,17 @@ module Rigor
214
254
  end
215
255
  end
216
256
 
257
+ # The reference AS WRITTEN (`AppConfig::MODE`), so `Scope#published_constant?` can match its local
258
+ # exemption on the names that spelling resolves to rather than on the last segment, with the leading
259
+ # `::` beside it: a rooted reference names the top level alone (issue #3437).
217
260
  def published_constant_node?(node, scope)
218
261
  spelling = constant_spelling(node)
219
- !spelling.nil? && scope.published_constant?(spelling)
262
+ !spelling.nil? && scope.published_constant?(spelling, rooted: Source::ConstantPath.rooted?(node))
220
263
  end
221
264
 
222
- # The name a constant reference asks the publication tables about ({#published_path?}'s rendering for a
223
- # path), or nil for any other node and for a path that renders no name.
265
+ # The name a constant reference asks the publication tables about, un-rooted, or nil for any other node
266
+ # and for a path that renders no name. A path that renders no qualified name (`self::MODE`) falls back
267
+ # to its own last segment, which is all there is to ask about.
224
268
  def constant_spelling(node)
225
269
  case node
226
270
  when Prism::ConstantReadNode then node.name.to_s