rigortype 0.2.9 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/docs/handbook/03-narrowing.md +2 -1
- data/docs/handbook/04-tuples-and-shapes.md +7 -3
- data/docs/handbook/06-classes.md +18 -9
- data/docs/handbook/08-understanding-errors.md +1 -0
- data/docs/handbook/11-sig-gen.md +11 -1
- data/docs/handbook/appendix-mypy.md +4 -3
- data/docs/install.md +14 -6
- data/docs/manual/01-installation.md +98 -4
- data/docs/manual/02-cli-reference.md +49 -12
- data/docs/manual/03-configuration.md +34 -2
- data/docs/manual/04-diagnostics.md +23 -0
- data/docs/manual/07-plugins.md +4 -4
- data/docs/manual/09-editor-integration.md +4 -4
- data/docs/manual/11-ci.md +51 -0
- data/docs/manual/12-caching.md +65 -0
- data/lib/rigor/analysis/check_rules/always_truthy_condition_collector.rb +9 -1
- data/lib/rigor/analysis/check_rules/dead_assignment_collector.rb +5 -3
- data/lib/rigor/analysis/check_rules/duplicate_hash_key_collector.rb +128 -0
- data/lib/rigor/analysis/check_rules/inferred_param_guard.rb +60 -0
- data/lib/rigor/analysis/check_rules/ivar_write_collector.rb +3 -2
- data/lib/rigor/analysis/check_rules/return_in_ensure_collector.rb +117 -0
- data/lib/rigor/analysis/check_rules/rule_ids.rb +125 -0
- data/lib/rigor/analysis/check_rules/rule_walk.rb +2 -1
- data/lib/rigor/analysis/check_rules/self_closedness_scanner.rb +2 -1
- data/lib/rigor/analysis/check_rules/shadowed_rescue_collector.rb +283 -0
- data/lib/rigor/analysis/check_rules/unreachable_clause_collector.rb +10 -1
- data/lib/rigor/analysis/check_rules/void_value_use_collector.rb +93 -0
- data/lib/rigor/analysis/check_rules.rb +557 -95
- data/lib/rigor/analysis/dependency_recorder.rb +93 -9
- data/lib/rigor/analysis/dependency_source_inference/walker.rb +2 -1
- data/lib/rigor/analysis/incremental_session.rb +456 -51
- data/lib/rigor/analysis/path_expansion.rb +42 -0
- data/lib/rigor/analysis/plugin_fact_fingerprint.rb +188 -0
- data/lib/rigor/analysis/rule_catalog.rb +226 -1
- data/lib/rigor/analysis/run_cache_key.rb +68 -0
- data/lib/rigor/analysis/run_cache_probe.rb +72 -0
- data/lib/rigor/analysis/runner/diagnostic_aggregator.rb +181 -28
- data/lib/rigor/analysis/runner/pool_coordinator.rb +77 -15
- data/lib/rigor/analysis/runner/project_pre_passes.rb +95 -49
- data/lib/rigor/analysis/runner/run_snapshots.rb +9 -2
- data/lib/rigor/analysis/runner.rb +343 -68
- data/lib/rigor/analysis/severity_stamp.rb +43 -0
- data/lib/rigor/analysis/worker_session.rb +30 -2
- data/lib/rigor/bleeding_edge.rb +37 -8
- data/lib/rigor/builtins/regex_refinement.rb +36 -1
- data/lib/rigor/cache/descriptor.rb +88 -28
- data/lib/rigor/cache/file_digest.rb +158 -0
- data/lib/rigor/cache/incremental_snapshot.rb +41 -5
- data/lib/rigor/cache/rbs_descriptor.rb +56 -10
- data/lib/rigor/cache/store.rb +33 -3
- data/lib/rigor/ci_detector.rb +90 -0
- data/lib/rigor/cli/annotate_command.rb +15 -9
- data/lib/rigor/cli/baseline_command.rb +4 -1
- data/lib/rigor/cli/check_command.rb +212 -27
- data/lib/rigor/cli/coverage_command.rb +6 -1
- data/lib/rigor/cli/docs_command.rb +0 -30
- data/lib/rigor/cli/doctor_command.rb +163 -23
- data/lib/rigor/cli/lsp_command.rb +5 -0
- data/lib/rigor/cli/mcp_command.rb +5 -0
- data/lib/rigor/cli/plugins_command.rb +36 -6
- data/lib/rigor/cli/plugins_renderer.rb +44 -12
- data/lib/rigor/cli/probe_environment.rb +85 -0
- data/lib/rigor/cli/sig_gen_command.rb +36 -9
- data/lib/rigor/cli/skill_command.rb +1 -32
- data/lib/rigor/cli/trace_command.rb +5 -6
- data/lib/rigor/cli/type_of_command.rb +9 -8
- data/lib/rigor/cli/type_scan_command.rb +8 -8
- data/lib/rigor/cli.rb +15 -4
- data/lib/rigor/config_audit.rb +48 -2
- data/lib/rigor/configuration/severity_profile.rb +31 -0
- data/lib/rigor/configuration.rb +156 -8
- data/lib/rigor/environment/default_libraries.rb +35 -0
- data/lib/rigor/environment/rbs_loader.rb +164 -5
- data/lib/rigor/environment.rb +19 -27
- data/lib/rigor/inference/budget_trace.rb +77 -2
- data/lib/rigor/inference/closure_escape_analyzer.rb +14 -1
- data/lib/rigor/inference/def_handle.rb +23 -0
- data/lib/rigor/inference/def_node_resolver.rb +90 -0
- data/lib/rigor/inference/def_return_typer.rb +2 -1
- data/lib/rigor/inference/expression_typer.rb +361 -113
- data/lib/rigor/inference/method_dispatcher/data_folding.rb +30 -5
- data/lib/rigor/inference/method_dispatcher/kernel_dispatch.rb +200 -0
- data/lib/rigor/inference/method_dispatcher/literal_string_folding.rb +22 -16
- data/lib/rigor/inference/method_dispatcher/member_shape_projection.rb +33 -0
- data/lib/rigor/inference/method_dispatcher/overload_selector.rb +11 -0
- data/lib/rigor/inference/method_dispatcher/rbs_dispatch.rb +42 -9
- data/lib/rigor/inference/method_dispatcher/receiver_affinity.rb +5 -0
- data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +28 -35
- data/lib/rigor/inference/method_dispatcher/struct_folding.rb +40 -8
- data/lib/rigor/inference/method_dispatcher.rb +57 -1
- data/lib/rigor/inference/mutation_widening.rb +42 -8
- data/lib/rigor/inference/narrowing.rb +127 -17
- data/lib/rigor/inference/parameter_inference_collector.rb +15 -0
- data/lib/rigor/inference/project_patched_scanner.rb +2 -1
- data/lib/rigor/inference/rbs_type_translator.rb +8 -1
- data/lib/rigor/inference/scope_indexer.rb +451 -36
- data/lib/rigor/inference/statement_evaluator.rb +64 -15
- data/lib/rigor/inference/struct_fold_safety.rb +48 -11
- data/lib/rigor/inference/synthetic_method_scanner.rb +15 -14
- data/lib/rigor/inference/void_origin.rb +25 -0
- data/lib/rigor/inference/void_tail_summary.rb +220 -0
- data/lib/rigor/language_server/completion_provider.rb +2 -1
- data/lib/rigor/language_server/document_symbol_provider.rb +2 -1
- data/lib/rigor/language_server/folding_range_provider.rb +2 -1
- data/lib/rigor/language_server/selection_range_provider.rb +2 -1
- data/lib/rigor/language_server/signature_help_provider.rb +2 -1
- data/lib/rigor/plugin/base.rb +9 -22
- data/lib/rigor/plugin/inflector.rb +12 -3
- data/lib/rigor/plugin/io_boundary.rb +5 -1
- data/lib/rigor/plugin/isolation.rb +81 -11
- data/lib/rigor/plugin/load_error.rb +10 -1
- data/lib/rigor/plugin/loader.rb +96 -14
- data/lib/rigor/plugin/node_rule_walk.rb +5 -3
- data/lib/rigor/plugin/registry.rb +32 -23
- data/lib/rigor/plugin.rb +26 -0
- data/lib/rigor/protection/mutator.rb +3 -2
- data/lib/rigor/reflection.rb +64 -0
- data/lib/rigor/runtime/jit.rb +128 -0
- data/lib/rigor/scope/discovery_index.rb +12 -2
- data/lib/rigor/scope.rb +122 -28
- data/lib/rigor/sig_gen/classification.rb +5 -1
- data/lib/rigor/sig_gen/generator.rb +38 -6
- data/lib/rigor/sig_gen/observation_collector.rb +4 -3
- data/lib/rigor/sig_gen/rbs_validity.rb +44 -0
- data/lib/rigor/sig_gen/renderer.rb +10 -0
- data/lib/rigor/sig_gen/write_result.rb +9 -4
- data/lib/rigor/sig_gen/writer.rb +24 -2
- data/lib/rigor/source/node_children.rb +116 -0
- data/lib/rigor/source/node_locator.rb +3 -1
- data/lib/rigor/source/node_walker.rb +4 -2
- data/lib/rigor/source.rb +1 -0
- data/lib/rigor/type/difference.rb +28 -24
- data/lib/rigor/type/hash_shape.rb +34 -10
- data/lib/rigor/version.rb +1 -1
- data/lib/rigortype.rb +24 -0
- data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/channel_discoverer.rb +4 -2
- data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer/mailer_discoverer.rb +6 -4
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/controller_discoverer.rb +5 -3
- data/plugins/rigor-activejob/lib/rigor/plugin/activejob/job_discoverer.rb +4 -2
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/analyzer.rb +3 -1
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_discoverer.rb +4 -2
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_parser.rb +4 -2
- data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage/analyzer.rb +3 -1
- data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage/attachment_discoverer.rb +4 -2
- data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types.rb +38 -6
- data/plugins/rigor-factorybot/lib/rigor/plugin/factorybot/factory_discoverer.rb +3 -1
- data/plugins/rigor-hanami/lib/rigor/plugin/hanami/action_checker.rb +3 -1
- data/plugins/rigor-mangrove/lib/rigor/plugin/mangrove.rb +10 -0
- data/plugins/rigor-minitest/lib/rigor/plugin/minitest.rb +11 -0
- data/plugins/rigor-pundit/lib/rigor/plugin/pundit/policy_discoverer.rb +3 -1
- data/plugins/rigor-rails/lib/rigor-rails.rb +11 -12
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/grape_api_discoverer.rb +3 -1
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_discoverer.rb +3 -1
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/routes_parser.rb +7 -5
- data/plugins/rigor-rbs-inline/lib/rigor/plugin/rbs_inline.rb +83 -9
- data/plugins/rigor-rbs-inline/lib/rigor-rbs-inline.rb +9 -5
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec/let_scope_index.rb +3 -1
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec/scope_walker.rb +3 -1
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec.rb +11 -0
- data/plugins/rigor-rspec-rails/lib/rigor/plugin/rspec_rails/http_status_codes.rb +4 -1
- data/plugins/rigor-sidekiq/lib/rigor/plugin/sidekiq/worker_discoverer.rb +4 -2
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog_walker.rb +3 -1
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet.rb +74 -14
- data/sig/prism_node_children.rbs +9 -0
- data/sig/rigor/inference.rbs +7 -0
- data/sig/rigor/plugin/base.rbs +2 -3
- data/sig/rigor/reflection.rbs +1 -0
- data/sig/rigor/scope.rbs +12 -1
- data/skills/rigor-ci-setup/SKILL.md +10 -0
- data/skills/rigor-editor-setup/SKILL.md +11 -0
- data/skills/rigor-next-steps/SKILL.md +23 -2
- data/skills/rigor-plugin-author/SKILL.md +3 -3
- data/skills/rigor-plugin-author/references/02-walker-and-types.md +2 -2
- data/skills/rigor-plugin-review/SKILL.md +2 -2
- data/skills/rigor-plugin-review/references/01-best-practices-checklist.md +5 -4
- data/skills/rigor-project-init/SKILL.md +29 -3
- data/skills/rigor-project-init/references/01-detect.md +1 -1
- data/skills/rigor-project-init/references/02-configure.md +39 -5
- data/skills/rigor-project-init/references/03-baseline-and-bugs.md +20 -0
- data/skills/rigor-project-init/references/05-jit-performance.md +125 -0
- metadata +42 -24
- data/lib/rigor/cli/ci_detector.rb +0 -89
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "prism"
|
|
4
4
|
|
|
5
5
|
require_relative "../../source/constant_path"
|
|
6
|
+
require_relative "../../source/node_children"
|
|
6
7
|
|
|
7
8
|
module Rigor
|
|
8
9
|
module Analysis
|
|
@@ -145,7 +146,7 @@ module Rigor
|
|
|
145
146
|
|
|
146
147
|
dispatch(node, hooks, context)
|
|
147
148
|
child_context = descend(node, context)
|
|
148
|
-
node.
|
|
149
|
+
node.rigor_each_child { |child| walk(child, hooks, child_context) }
|
|
149
150
|
end
|
|
150
151
|
|
|
151
152
|
def dispatch(node, hooks, context)
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "prism"
|
|
4
4
|
|
|
5
5
|
require_relative "../../source/constant_path"
|
|
6
|
+
require_relative "../../source/node_children"
|
|
6
7
|
|
|
7
8
|
module Rigor
|
|
8
9
|
module Analysis
|
|
@@ -52,7 +53,7 @@ module Rigor
|
|
|
52
53
|
names << child_prefix.join("::") if name && class_surface_open?(node)
|
|
53
54
|
walk(node.body, child_prefix, names) if node.body
|
|
54
55
|
else
|
|
55
|
-
node.
|
|
56
|
+
node.rigor_each_child { |child| walk(child, prefix, names) }
|
|
56
57
|
end
|
|
57
58
|
end
|
|
58
59
|
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
require_relative "../../reflection"
|
|
6
|
+
require_relative "../../source/constant_path"
|
|
7
|
+
require_relative "../../source/node_children"
|
|
8
|
+
|
|
9
|
+
module Rigor
|
|
10
|
+
module Analysis
|
|
11
|
+
module CheckRules
|
|
12
|
+
# `flow.shadowed-rescue-clause` — a `rescue` clause that can never run because an EARLIER clause of the
|
|
13
|
+
# same `begin` / `def` rescue chain already catches a superclass (or the same class) of every exception
|
|
14
|
+
# class the later clause names (`rescue StandardError => e … rescue ArgumentError => e` — the
|
|
15
|
+
# ArgumentError arm is dead; a bare `rescue` counts as `StandardError` on either side). Modeled on the
|
|
16
|
+
# shadowed-arm half of PHPStan's `CatchWithUnthrownExceptionRule`; the never-thrown half is deliberately
|
|
17
|
+
# out of scope (unprovable in Ruby).
|
|
18
|
+
#
|
|
19
|
+
# The false-positive envelope is ancestry-certainty, not flow analysis — the rule is purely syntactic
|
|
20
|
+
# plus class-hierarchy lookups, so it fires ONLY when:
|
|
21
|
+
#
|
|
22
|
+
# - every exception reference in BOTH the earlier and the later clause is a constant path that resolves
|
|
23
|
+
# (against the clause's lexical namespace) to a CLASS with known ancestry — an RBS / registry core
|
|
24
|
+
# class, or a project class whose `class Foo < Bar` superclass chain is discovered. Any unresolved
|
|
25
|
+
# constant, dynamic expression (`rescue klass_var`), or splat (`rescue *ERRORS`) makes its clause
|
|
26
|
+
# opaque: an opaque earlier clause contributes no coverage, an opaque later clause never fires.
|
|
27
|
+
# - no reference resolves to a MODULE (the `rescue MyGem::Error` module-tag mixin pattern — module
|
|
28
|
+
# `===` semantics are custom, so any clause naming one stays out of every comparison). A
|
|
29
|
+
# project-discovered constant is certified as a class only via the `class Foo < Bar` superclass
|
|
30
|
+
# table; a plain `class Foo` / `module Foo` declaration is indistinguishable in the discovery table
|
|
31
|
+
# and stays silent.
|
|
32
|
+
# - a multi-class arm (`rescue A, B => e`) is shadowed only when EVERY class it names is covered by
|
|
33
|
+
# some earlier comparable clause — one diagnostic per clause, naming the covering earlier clause(s).
|
|
34
|
+
#
|
|
35
|
+
# A later clause naming a SUPERCLASS of an earlier one is the normal narrow-to-wide rescue order and
|
|
36
|
+
# never fires. `retry` in an earlier arm does not un-shadow (the re-raise re-enters from the top).
|
|
37
|
+
# Nested `begin` / `rescue` inside a rescue body compares only clauses of its OWN `BeginNode` — the
|
|
38
|
+
# walk dispatches per `BeginNode` and never crosses into another chain.
|
|
39
|
+
class ShadowedRescueCollector
|
|
40
|
+
# One shadowed clause. `clause` is the dead `Prism::RescueNode`; `clause_source` its rendered
|
|
41
|
+
# `rescue A, B` condition list (`"rescue"` for a bare rescue); `earlier_sources` /
|
|
42
|
+
# `earlier_lines` name the earlier clause(s) whose classes cover it.
|
|
43
|
+
Result = Data.define(:clause, :clause_source, :earlier_sources, :earlier_lines)
|
|
44
|
+
|
|
45
|
+
NODE_CLASSES = [Prism::BeginNode].freeze
|
|
46
|
+
|
|
47
|
+
# Cycle / depth bound for the project `class Foo < Bar` superclass-chain walk.
|
|
48
|
+
SUPERCLASS_WALK_LIMIT = 32
|
|
49
|
+
|
|
50
|
+
# The implicit class of a bare `rescue` / `rescue => e`.
|
|
51
|
+
IMPLICIT_RESCUE_CLASS = "StandardError"
|
|
52
|
+
|
|
53
|
+
def initialize(scope_index)
|
|
54
|
+
@scope_index = scope_index
|
|
55
|
+
@results = []
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Legacy single-collector walk — the oracle the shadow harness (`RIGOR_SHADOW_RULE_WALK=1`) compares
|
|
59
|
+
# the shared {RuleWalk} traversal against. Tracks the lexical class / module prefix itself, exactly
|
|
60
|
+
# as {RuleWalk} threads `qualified_prefix`.
|
|
61
|
+
# @return [Array<Result>] one entry per provably-shadowed rescue clause.
|
|
62
|
+
def collect(root)
|
|
63
|
+
walk(root, [])
|
|
64
|
+
@results.freeze
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# {RuleWalk} entry point. `context.qualified_prefix` carries the enclosing class / module names the
|
|
68
|
+
# exception constants resolve against.
|
|
69
|
+
def visit(node, context = nil)
|
|
70
|
+
prefix = context.respond_to?(:qualified_prefix) ? context.qualified_prefix : []
|
|
71
|
+
collect_begin(node, prefix)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def results
|
|
75
|
+
@results.freeze
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def walk(node, prefix)
|
|
81
|
+
return unless node.is_a?(Prism::Node)
|
|
82
|
+
|
|
83
|
+
collect_begin(node, prefix) if node.is_a?(Prism::BeginNode)
|
|
84
|
+
child_prefix = extend_prefix(node, prefix)
|
|
85
|
+
node.rigor_each_child { |child| walk(child, child_prefix) }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def extend_prefix(node, prefix)
|
|
89
|
+
return prefix unless node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode)
|
|
90
|
+
|
|
91
|
+
name = Source::ConstantPath.qualified_name(node.constant_path)
|
|
92
|
+
name ? prefix + [name] : prefix
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def collect_begin(node, prefix)
|
|
96
|
+
clauses = rescue_chain(node.rescue_clause)
|
|
97
|
+
return if clauses.size < 2
|
|
98
|
+
|
|
99
|
+
scope = scope_for(node)
|
|
100
|
+
return if scope.nil?
|
|
101
|
+
|
|
102
|
+
infos = clauses.map { |clause| clause_info(clause, prefix, scope) }
|
|
103
|
+
infos.each_with_index do |info, index|
|
|
104
|
+
next if index.zero? || info.nil?
|
|
105
|
+
|
|
106
|
+
check_shadowed(info, infos.first(index), scope)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# The ordered `rescue` clauses of one `begin` node (the `subsequent` chain).
|
|
111
|
+
def rescue_chain(clause)
|
|
112
|
+
chain = []
|
|
113
|
+
while clause
|
|
114
|
+
chain << clause
|
|
115
|
+
clause = clause.subsequent
|
|
116
|
+
end
|
|
117
|
+
chain
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# A `Scope` for the environment / discovery lookups. Every scope of the file shares the discovery
|
|
121
|
+
# index and environment, so any scope recorded on the begin node's neighbourhood serves; nil (no
|
|
122
|
+
# scope recorded) keeps the whole chain silent.
|
|
123
|
+
def scope_for(node)
|
|
124
|
+
@scope_index[node] || @scope_index[node.statements] || @scope_index[node.rescue_clause]
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Resolves one clause to `{clause:, names: [String, …]}` where `names` are the certified exception
|
|
128
|
+
# CLASS names it rescues, or nil when the clause is opaque (any entry unresolved / dynamic / splat /
|
|
129
|
+
# module — the comparisons involving it stay silent). A bare `rescue` names the implicit
|
|
130
|
+
# `StandardError`.
|
|
131
|
+
def clause_info(clause, prefix, scope)
|
|
132
|
+
exceptions = clause.exceptions
|
|
133
|
+
if exceptions.empty?
|
|
134
|
+
return unless certified_class?(IMPLICIT_RESCUE_CLASS, scope)
|
|
135
|
+
|
|
136
|
+
return { clause: clause, names: [IMPLICIT_RESCUE_CLASS] }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
names = exceptions.map { |exc| certified_exception_class(exc, prefix, scope) }
|
|
140
|
+
return if names.any?(&:nil?)
|
|
141
|
+
|
|
142
|
+
{ clause: clause, names: names }
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The resolved, certified class name for one exception reference, or nil (opaque). Only constant
|
|
146
|
+
# reads / constant paths participate; the name must resolve lexically and certify as a class with
|
|
147
|
+
# known ancestry.
|
|
148
|
+
def certified_exception_class(node, prefix, scope)
|
|
149
|
+
name = resolve_constant(node, prefix, scope)
|
|
150
|
+
return nil if name.nil?
|
|
151
|
+
|
|
152
|
+
certified_class?(name, scope) ? name : nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Lexical constant resolution: innermost enclosing namespace first, then outward, then top-level —
|
|
156
|
+
# first candidate any knowledge source (project discovery or RBS / registry) recognises wins. An
|
|
157
|
+
# absolute path (`::Foo`) skips the lexical ladder. Returns nil for a non-constant node or a path
|
|
158
|
+
# with a dynamic base.
|
|
159
|
+
def resolve_constant(node, prefix, scope)
|
|
160
|
+
name = Source::ConstantPath.qualified_name_or_nil(node)
|
|
161
|
+
return nil if name.nil?
|
|
162
|
+
|
|
163
|
+
return name if absolute_path?(node)
|
|
164
|
+
|
|
165
|
+
candidates = []
|
|
166
|
+
prefix.size.downto(1) { |i| candidates << (prefix.first(i) + [name]).join("::") }
|
|
167
|
+
candidates << name
|
|
168
|
+
candidates.find { |candidate| known_name?(candidate, scope) }
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def absolute_path?(node)
|
|
172
|
+
return false unless node.is_a?(Prism::ConstantPathNode)
|
|
173
|
+
|
|
174
|
+
parent = node.parent
|
|
175
|
+
return true if parent.nil? # `::Foo`
|
|
176
|
+
|
|
177
|
+
parent.is_a?(Prism::ConstantPathNode) && absolute_path?(parent)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Presence check used to pick the lexical candidate — deliberately broader than certification, so a
|
|
181
|
+
# name that resolves to a project module is FOUND here and then rejected by {#certified_class?}
|
|
182
|
+
# (rather than mis-resolving outward to a same-named core class).
|
|
183
|
+
def known_name?(name, scope)
|
|
184
|
+
scope.discovered_superclasses.key?(name) ||
|
|
185
|
+
scope.discovered_classes.key?(name) ||
|
|
186
|
+
scope.environment.class_known?(name)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# A name is a certified exception-comparable class when it is either a project class with a
|
|
190
|
+
# discovered `class Foo < Bar` superclass entry, or an RBS / registry class that is not a module.
|
|
191
|
+
# A project-discovered name WITHOUT a superclass entry cannot be told apart from a module (the
|
|
192
|
+
# discovery table records both), so it stays uncertified — conservative, per the FP discipline.
|
|
193
|
+
def certified_class?(name, scope)
|
|
194
|
+
return true if scope.discovered_superclasses.key?(name)
|
|
195
|
+
return false if scope.discovered_classes.key?(name)
|
|
196
|
+
|
|
197
|
+
environment = scope.environment
|
|
198
|
+
environment.class_known?(name) && !environment.rbs_module?(name)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Fires when EVERY class the later clause names is covered by some earlier comparable clause.
|
|
202
|
+
def check_shadowed(info, earlier_infos, scope)
|
|
203
|
+
comparable_earlier = earlier_infos.compact
|
|
204
|
+
return if comparable_earlier.empty?
|
|
205
|
+
|
|
206
|
+
covering = info[:names].map do |name|
|
|
207
|
+
comparable_earlier.find { |earlier| earlier[:names].any? { |e| covered_by?(name, e, scope) } }
|
|
208
|
+
end
|
|
209
|
+
return if covering.any?(&:nil?)
|
|
210
|
+
|
|
211
|
+
clauses = covering.uniq
|
|
212
|
+
@results << Result.new(
|
|
213
|
+
clause: info[:clause],
|
|
214
|
+
clause_source: clause_source(info[:clause]),
|
|
215
|
+
earlier_sources: clauses.map { |c| clause_source(c[:clause]) },
|
|
216
|
+
earlier_lines: clauses.map { |c| c[:clause].location.start_line }
|
|
217
|
+
)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def clause_source(clause)
|
|
221
|
+
exceptions = clause.exceptions
|
|
222
|
+
return "rescue" if exceptions.empty?
|
|
223
|
+
|
|
224
|
+
"rescue #{exceptions.map(&:slice).join(', ')}"
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Is `later` the same class as, or a subclass of, `earlier`? RBS / registry ancestry answers first;
|
|
228
|
+
# `:unknown` (a project-defined class on either side) falls through to the discovered
|
|
229
|
+
# `class Foo < Bar` superclass-chain walk. `:superclass` / `:disjoint` are definitive "no".
|
|
230
|
+
def covered_by?(later, earlier, scope)
|
|
231
|
+
return true if later == earlier
|
|
232
|
+
|
|
233
|
+
case Reflection.class_ordering(later, earlier, scope: scope)
|
|
234
|
+
when :equal, :subclass then true
|
|
235
|
+
when :unknown then project_chain_covered?(later, earlier, scope)
|
|
236
|
+
else false
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Walks `later`'s discovered superclass chain (each parent recorded AS WRITTEN at its
|
|
241
|
+
# `class Foo < Bar` site, resolved against the child's namespace) looking for `earlier` — directly,
|
|
242
|
+
# or through an RBS-known ancestor that orders at-or-below `earlier`. Cycle-guarded and depth-capped;
|
|
243
|
+
# a chain that leaves both knowledge sources stays silent.
|
|
244
|
+
def project_chain_covered?(later, earlier, scope)
|
|
245
|
+
supers = scope.discovered_superclasses
|
|
246
|
+
current = later
|
|
247
|
+
seen = {}
|
|
248
|
+
SUPERCLASS_WALK_LIMIT.times do
|
|
249
|
+
parent = supers[current.to_s]
|
|
250
|
+
return false if parent.nil?
|
|
251
|
+
|
|
252
|
+
resolved = resolve_parent(current, parent.to_s, scope)
|
|
253
|
+
return false if seen[resolved]
|
|
254
|
+
|
|
255
|
+
seen[resolved] = true
|
|
256
|
+
return true if resolved == earlier
|
|
257
|
+
|
|
258
|
+
case Reflection.class_ordering(resolved, earlier, scope: scope)
|
|
259
|
+
when :equal, :subclass then return true
|
|
260
|
+
when :unknown then current = resolved
|
|
261
|
+
else return false
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
false
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Resolves a superclass name as written (`Error`) against the child's namespace (`Mail::POP3` →
|
|
268
|
+
# `Mail::Error`), longest prefix first; falls back to the as-written name (which RBS may know, e.g.
|
|
269
|
+
# `StandardError`).
|
|
270
|
+
def resolve_parent(child, parent, scope)
|
|
271
|
+
segments = child.to_s.split("::")[0...-1]
|
|
272
|
+
until segments.empty?
|
|
273
|
+
candidate = "#{segments.join('::')}::#{parent}"
|
|
274
|
+
return candidate if known_name?(candidate, scope)
|
|
275
|
+
|
|
276
|
+
segments.pop
|
|
277
|
+
end
|
|
278
|
+
parent
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
end
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
require "prism"
|
|
4
4
|
|
|
5
|
+
require_relative "../../source/node_children"
|
|
6
|
+
require_relative "inferred_param_guard"
|
|
7
|
+
|
|
5
8
|
module Rigor
|
|
6
9
|
module Analysis
|
|
7
10
|
module CheckRules
|
|
@@ -85,7 +88,7 @@ module Rigor
|
|
|
85
88
|
visit(node) if case_like?(node) && !in_loop_or_block
|
|
86
89
|
|
|
87
90
|
child_in_loop_or_block = in_loop_or_block || enters_loop_or_block?(node)
|
|
88
|
-
node.
|
|
91
|
+
node.rigor_each_child { |child| walk(child, in_loop_or_block: child_in_loop_or_block) }
|
|
89
92
|
end
|
|
90
93
|
|
|
91
94
|
# `case/when` is a `CaseNode`; `case/in` is a `CaseMatchNode`. Both carry `predicate` + `conditions`
|
|
@@ -102,6 +105,12 @@ module Rigor
|
|
|
102
105
|
subject = node.predicate
|
|
103
106
|
return unless subject.is_a?(Prism::LocalVariableReadNode)
|
|
104
107
|
|
|
108
|
+
# ADR-67 WD6b — a `case` subject that is an inferred parameter narrows to `Bot` in the non-matching
|
|
109
|
+
# clauses only because its seeded type is a lower bound; a wider real caller could match them. Decline
|
|
110
|
+
# the whole case so no clause fires unreachable.
|
|
111
|
+
scope = @scope_index[node]
|
|
112
|
+
return if scope && InferredParamGuard.rooted?(subject, scope)
|
|
113
|
+
|
|
105
114
|
entry_type = entry_subject_type(node, subject.name)
|
|
106
115
|
# Only meaningful for a concrete subject type: a `Dynamic` subject can never be proven disjoint
|
|
107
116
|
# (the gradual guarantee), and an already-`Bot` subject is dead code, not a clause-ordering error.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
require_relative "../../source/node_children"
|
|
6
|
+
|
|
7
|
+
module Rigor
|
|
8
|
+
module Analysis
|
|
9
|
+
module CheckRules
|
|
10
|
+
# ADR-100 WD2/WD3 — collects value-context uses of a `top` recovered from an author-declared `-> void`
|
|
11
|
+
# return. The return-typing tier records the recovery on `Scope#void_origins` (keyed by the call node)
|
|
12
|
+
# whenever an RBS `-> void` return widens to `top` on the direct-dispatch path (the receiver's own
|
|
13
|
+
# resolvable class); this collector fires `static.value-use.void` when such a call node sits in a
|
|
14
|
+
# *value* position, and stays silent when it is a bare statement.
|
|
15
|
+
#
|
|
16
|
+
# Value context is read top-down from the consumer node — the only nodes that place a sub-expression in
|
|
17
|
+
# value position — so no parent tracking is needed and a bare-statement void call (a direct child of a
|
|
18
|
+
# `StatementsNode`, never a consumer's value slot) is never reached:
|
|
19
|
+
#
|
|
20
|
+
# - an assignment's right-hand side (`x = logger.log(...)`, and the ivar / cvar / gvar / constant forms),
|
|
21
|
+
# - a call's explicit receiver (`logger.log(...).inspect`),
|
|
22
|
+
# - a call's positional argument (`store(logger.log(...))`).
|
|
23
|
+
#
|
|
24
|
+
# The recording is already FP-narrow (only an author-written `-> void`, only the direct-RBS path), so
|
|
25
|
+
# the collector adds no further type reasoning: membership in `void_origins` is the whole gate. A
|
|
26
|
+
# legitimate `top` value (an author-declared `-> top` return) never enters the table, so it stays
|
|
27
|
+
# silent — the false-positive proof this rule rests on. The rule ships behind the `use-of-void-value`
|
|
28
|
+
# bleeding-edge feature (authored `:warning`, resolved `:off` by every default profile).
|
|
29
|
+
class VoidValueUseCollector
|
|
30
|
+
# The assignment forms whose `value` slot is a value position. `MultiWriteNode` (destructuring) is
|
|
31
|
+
# intentionally excluded — its `value` is a container the void call would have to be spread through,
|
|
32
|
+
# not a direct use, so it is out of this slice's FP-narrow scope.
|
|
33
|
+
WRITE_NODE_CLASSES = [
|
|
34
|
+
Prism::LocalVariableWriteNode, Prism::InstanceVariableWriteNode,
|
|
35
|
+
Prism::ClassVariableWriteNode, Prism::GlobalVariableWriteNode,
|
|
36
|
+
Prism::ConstantWriteNode, Prism::ConstantPathWriteNode
|
|
37
|
+
].freeze
|
|
38
|
+
|
|
39
|
+
Result = Data.define(:void_node, :origin)
|
|
40
|
+
|
|
41
|
+
def initialize(scope_index)
|
|
42
|
+
@scope_index = scope_index
|
|
43
|
+
@results = []
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Walk the whole subtree once, checking each consumer node's value slots. Returns one {Result} per
|
|
47
|
+
# value-context use of a recovered-`void` call.
|
|
48
|
+
# @return [Array<Result>]
|
|
49
|
+
def collect(root)
|
|
50
|
+
walk(root)
|
|
51
|
+
@results.freeze
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def walk(node)
|
|
57
|
+
return unless node.is_a?(Prism::Node)
|
|
58
|
+
|
|
59
|
+
inspect_consumer(node)
|
|
60
|
+
node.rigor_each_child { |child| walk(child) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def inspect_consumer(node)
|
|
64
|
+
case node
|
|
65
|
+
when Prism::CallNode
|
|
66
|
+
record_if_void(node.receiver)
|
|
67
|
+
call_arguments(node).each { |arg| record_if_void(arg) }
|
|
68
|
+
when *WRITE_NODE_CLASSES
|
|
69
|
+
record_if_void(node.value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def call_arguments(call)
|
|
74
|
+
args = call.arguments
|
|
75
|
+
args.nil? ? [] : args.arguments
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# A value node is a recovered-`void` use exactly when it is a call node the return-typing tier
|
|
79
|
+
# recorded in `void_origins` (identity-keyed on that same node). Every other value slot — a literal, a
|
|
80
|
+
# variable read, a non-void call — is absent from the table and stays silent.
|
|
81
|
+
def record_if_void(value_node)
|
|
82
|
+
return unless value_node.is_a?(Prism::CallNode)
|
|
83
|
+
|
|
84
|
+
scope = @scope_index[value_node]
|
|
85
|
+
origin = scope&.void_origins&.[](value_node)
|
|
86
|
+
return if origin.nil?
|
|
87
|
+
|
|
88
|
+
@results << Result.new(void_node: value_node, origin: origin)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|