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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/docs/handbook/01-getting-started.md +1 -1
- data/docs/handbook/04-tuples-and-shapes.md +6 -1
- data/docs/handbook/12-lightweight-hkt.md +1 -1
- data/docs/handbook/appendix-typescript.md +28 -17
- data/docs/manual/02-cli-reference.md +10 -0
- data/docs/manual/03-configuration.md +1 -1
- data/docs/manual/04-diagnostics.md +1 -1
- data/docs/manual/10-mcp-server.md +122 -10
- data/docs/manual/plugins/rigor-activerecord.md +7 -0
- data/lib/rigor/analysis/check_rules/always_raises.rb +95 -0
- data/lib/rigor/analysis/check_rules/always_truthy_condition_collector.rb +32 -2
- data/lib/rigor/analysis/check_rules/argument_type.rb +802 -0
- data/lib/rigor/analysis/check_rules/call_facts.rb +2 -2
- data/lib/rigor/analysis/check_rules/dead_assignment_collector.rb +30 -1
- data/lib/rigor/analysis/check_rules/declaration_sourced_guard.rb +4 -5
- data/lib/rigor/analysis/check_rules/duplicate_hash_key_collector.rb +34 -0
- data/lib/rigor/analysis/check_rules/ivar_write_collector.rb +131 -4
- data/lib/rigor/analysis/check_rules/nil_receiver.rb +195 -0
- data/lib/rigor/analysis/check_rules/override_param_narrowed.rb +111 -0
- data/lib/rigor/analysis/check_rules/override_return_widened.rb +88 -0
- data/lib/rigor/analysis/check_rules/override_support.rb +246 -0
- data/lib/rigor/analysis/check_rules/override_visibility.rb +135 -0
- data/lib/rigor/analysis/check_rules/published_constant_guard.rb +75 -31
- data/lib/rigor/analysis/check_rules/raise_non_exception.rb +198 -0
- data/lib/rigor/analysis/check_rules/receiver_predicates.rb +576 -0
- data/lib/rigor/analysis/check_rules/return_in_ensure_collector.rb +52 -4
- data/lib/rigor/analysis/check_rules/return_type.rb +225 -0
- data/lib/rigor/analysis/check_rules/rigor_testing.rb +119 -0
- data/lib/rigor/analysis/check_rules/rule_ids.rb +7 -7
- data/lib/rigor/analysis/check_rules/rule_walk.rb +4 -3
- data/lib/rigor/analysis/check_rules/self_closedness_scanner.rb +2 -0
- data/lib/rigor/analysis/check_rules/self_undefined_method.rb +165 -0
- data/lib/rigor/analysis/check_rules/shadowed_rescue_collector.rb +34 -0
- data/lib/rigor/analysis/check_rules/source_arity.rb +2 -2
- data/lib/rigor/analysis/check_rules/special_global_write.rb +256 -0
- data/lib/rigor/analysis/check_rules/suppression.rb +275 -0
- data/lib/rigor/analysis/check_rules/undefined_method.rb +882 -0
- data/lib/rigor/analysis/check_rules/unreachable_branch.rb +97 -0
- data/lib/rigor/analysis/check_rules/unreachable_clause_collector.rb +42 -0
- data/lib/rigor/analysis/check_rules/unresolved_toplevel.rb +140 -0
- data/lib/rigor/analysis/check_rules/visibility_mismatch.rb +252 -0
- data/lib/rigor/analysis/check_rules/void_value_use_collector.rb +27 -0
- data/lib/rigor/analysis/check_rules/wrong_arity.rb +303 -0
- data/lib/rigor/analysis/check_rules.rb +66 -4931
- data/lib/rigor/analysis/dependency_recorder.rb +1 -1
- data/lib/rigor/analysis/dependency_source_inference/builder.rb +2 -4
- data/lib/rigor/analysis/dependency_source_inference/gem_resolver.rb +1 -2
- data/lib/rigor/analysis/dependency_source_inference/walker.rb +15 -15
- data/lib/rigor/analysis/incremental_session.rb +4 -4
- data/lib/rigor/analysis/path_expansion.rb +1 -1
- data/lib/rigor/analysis/plugin_fact_fingerprint.rb +5 -5
- data/lib/rigor/analysis/reachability/graph.rb +2 -3
- data/lib/rigor/analysis/reachability/plugin_roots.rb +2 -2
- data/lib/rigor/analysis/reachability/scan.rb +2 -2
- data/lib/rigor/analysis/rule_catalog.rb +2 -1
- data/lib/rigor/analysis/run_cache_key.rb +9 -8
- data/lib/rigor/analysis/runner/buffer_pool_dispatcher.rb +2 -2
- data/lib/rigor/analysis/runner/dependency_rows.rb +206 -0
- data/lib/rigor/analysis/runner/diagnostic_aggregator.rb +24 -22
- data/lib/rigor/analysis/runner/effect_cache_entries.rb +69 -0
- data/lib/rigor/analysis/runner/pool_coordinator.rb +6 -8
- data/lib/rigor/analysis/runner/project_pre_passes.rb +3 -5
- data/lib/rigor/analysis/runner/scope_seed.rb +144 -0
- data/lib/rigor/analysis/runner.rb +49 -296
- data/lib/rigor/analysis/template_unit_collector.rb +1 -1
- data/lib/rigor/analysis/template_units.rb +10 -10
- data/lib/rigor/analysis/worker_session.rb +7 -7
- data/lib/rigor/bleeding_edge.rb +3 -2
- data/lib/rigor/builtins/hkt_builtins.rb +17 -17
- data/lib/rigor/builtins/imported_refinements.rb +8 -16
- data/lib/rigor/builtins/regex_refinement.rb +3 -3
- data/lib/rigor/builtins/static_return_refinements.rb +143 -19
- data/lib/rigor/cache/incremental_snapshot.rb +21 -8
- data/lib/rigor/cache/rbs_descriptor.rb +18 -1
- data/lib/rigor/cli/coverage_renderer.rb +15 -1
- data/lib/rigor/cli/coverage_report.rb +19 -1
- data/lib/rigor/cli/diagnostic_formats.rb +3 -2
- data/lib/rigor/cli/docs_command.rb +5 -5
- data/lib/rigor/cli/effects_command.rb +7 -6
- data/lib/rigor/cli/fused_protection_report.rb +7 -7
- data/lib/rigor/cli/lsp_command.rb +1 -1
- data/lib/rigor/cli/mcp_command.rb +5 -2
- data/lib/rigor/cli/options.rb +4 -0
- data/lib/rigor/cli/plugins_command.rb +1 -1
- data/lib/rigor/cli/sig_gen_command.rb +1 -1
- data/lib/rigor/cli/skill_command.rb +5 -4
- data/lib/rigor/cli/triage_renderer.rb +3 -2
- data/lib/rigor/cli/unused_command.rb +4 -4
- data/lib/rigor/cli.rb +14 -0
- data/lib/rigor/configuration/coercion.rb +418 -0
- data/lib/rigor/configuration/defaults.rb +248 -0
- data/lib/rigor/configuration/dependencies.rb +1 -1
- data/lib/rigor/configuration/loader.rb +354 -0
- data/lib/rigor/configuration/severity_profile.rb +4 -4
- data/lib/rigor/configuration.rb +51 -860
- data/lib/rigor/effects/attribution.rb +1 -1
- data/lib/rigor/effects/effect_table.rb +1 -1
- data/lib/rigor/effects/identity.rb +3 -3
- data/lib/rigor/effects/local_ownership.rb +2 -2
- data/lib/rigor/effects/snapshot.rb +1 -1
- data/lib/rigor/effects/summary.rb +5 -4
- data/lib/rigor/environment/member_consistency.rb +10 -0
- data/lib/rigor/environment/rbs_loader.rb +19 -22
- data/lib/rigor/environment/reflection.rb +1 -1
- data/lib/rigor/environment/unreadable_signature.rb +21 -0
- data/lib/rigor/environment.rb +8 -8
- data/lib/rigor/flow_contribution/merger.rb +1 -0
- data/lib/rigor/hashing/xxh3.rb +1 -1
- data/lib/rigor/inference/acceptance.rb +23 -2
- data/lib/rigor/inference/alias_mutation_floor/site_collector.rb +5 -0
- data/lib/rigor/inference/alias_mutation_floor.rb +0 -5
- data/lib/rigor/inference/anonymous_meta_class.rb +2 -2
- data/lib/rigor/inference/block_call_timing.rb +6 -0
- data/lib/rigor/inference/budget_trace.rb +3 -2
- data/lib/rigor/inference/builtins/date_catalog.rb +3 -3
- data/lib/rigor/inference/builtins/method_catalog.rb +3 -3
- data/lib/rigor/inference/builtins/time_catalog.rb +3 -3
- data/lib/rigor/inference/call_ladder/literal_send.rb +98 -0
- data/lib/rigor/inference/call_ladder/overriding_def.rb +98 -0
- data/lib/rigor/inference/call_ladder/project_singleton.rb +362 -0
- data/lib/rigor/inference/call_ladder/refined.rb +212 -0
- data/lib/rigor/inference/call_ladder/top_level_def.rb +222 -0
- data/lib/rigor/inference/call_ladder/user_method.rb +247 -0
- data/lib/rigor/inference/call_ladder.rb +219 -0
- data/lib/rigor/inference/callee_write_census.rb +2 -2
- data/lib/rigor/inference/captured_locals.rb +2 -2
- data/lib/rigor/inference/clause_widening.rb +63 -1
- data/lib/rigor/inference/declaration_walk/context.rb +12 -12
- data/lib/rigor/inference/declaration_walk/rules.rb +750 -0
- data/lib/rigor/inference/declaration_walk/traversal.rb +9 -8
- data/lib/rigor/inference/declaration_walk.rb +4 -3
- data/lib/rigor/inference/def_node_resolver.rb +2 -2
- data/lib/rigor/inference/def_return_typer.rb +14 -6
- data/lib/rigor/inference/deferred_mutation.rb +120 -0
- data/lib/rigor/inference/define_method_block_self.rb +1 -1
- data/lib/rigor/inference/definer_resolution.rb +4 -4
- data/lib/rigor/inference/error_info.rb +8 -8
- data/lib/rigor/inference/expression_typer/composites.rb +348 -0
- data/lib/rigor/inference/expression_typer/per_element_assemblers.rb +233 -0
- data/lib/rigor/inference/expression_typer/writes.rb +218 -0
- data/lib/rigor/inference/expression_typer.rb +275 -1825
- data/lib/rigor/inference/external_ancestor_resolution.rb +2 -2
- data/lib/rigor/inference/fallback.rb +1 -0
- data/lib/rigor/inference/for_yield_slots.rb +126 -0
- data/lib/rigor/inference/genuine_send.rb +92 -0
- data/lib/rigor/inference/global_write_census.rb +2 -2
- data/lib/rigor/inference/hkt_body.rb +2 -0
- data/lib/rigor/inference/in_effect_refinements/walk.rb +2 -2
- data/lib/rigor/inference/index_write_widening.rb +4 -4
- data/lib/rigor/inference/indexed_narrowing.rb +1 -1
- data/lib/rigor/inference/intervening_ivar_reset.rb +46 -9
- data/lib/rigor/inference/ivar_writer_census.rb +1 -1
- data/lib/rigor/inference/loop_store_sites.rb +239 -0
- data/lib/rigor/inference/macro_block_self_type.rb +4 -3
- data/lib/rigor/inference/method_dispatcher/block_folding.rb +3 -2
- data/lib/rigor/inference/method_dispatcher/call_context.rb +84 -14
- data/lib/rigor/inference/method_dispatcher/composite_receiver.rb +144 -0
- data/lib/rigor/inference/method_dispatcher/constant_folding.rb +7 -5
- data/lib/rigor/inference/method_dispatcher/declared_constructor.rb +1 -1
- data/lib/rigor/inference/method_dispatcher/dependency_source.rb +90 -0
- data/lib/rigor/inference/method_dispatcher/discovered_method.rb +78 -0
- data/lib/rigor/inference/method_dispatcher/file_folding.rb +2 -2
- data/lib/rigor/inference/method_dispatcher/hkt_builtin_return.rb +40 -0
- data/lib/rigor/inference/method_dispatcher/iterator_dispatch.rb +1 -6
- data/lib/rigor/inference/method_dispatcher/literal_string_folding.rb +4 -2
- data/lib/rigor/inference/method_dispatcher/match_data_folding.rb +1 -1
- data/lib/rigor/inference/method_dispatcher/meta_introspection.rb +506 -0
- data/lib/rigor/inference/method_dispatcher/overload_selector.rb +2 -0
- data/lib/rigor/inference/method_dispatcher/plugin_contribution.rb +134 -0
- data/lib/rigor/inference/method_dispatcher/project_patched.rb +134 -0
- data/lib/rigor/inference/method_dispatcher/rbs_dispatch.rb +125 -43
- data/lib/rigor/inference/method_dispatcher/reduce_folding.rb +3 -4
- data/lib/rigor/inference/method_dispatcher/regexp_folding.rb +1 -1
- data/lib/rigor/inference/method_dispatcher/set_folding.rb +1 -1
- data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +19 -6
- data/lib/rigor/inference/method_dispatcher/singleton_folding.rb +3 -3
- data/lib/rigor/inference/method_dispatcher/singleton_mixin_dispatch.rb +7 -4
- data/lib/rigor/inference/method_dispatcher/static_refinement.rb +106 -0
- data/lib/rigor/inference/method_dispatcher/synthesized_stub.rb +41 -0
- data/lib/rigor/inference/method_dispatcher/synthetic_method_dispatch.rb +133 -0
- data/lib/rigor/inference/method_dispatcher/universal_object_dispatch.rb +2 -1
- data/lib/rigor/inference/method_dispatcher/user_class_fallback.rb +216 -0
- data/lib/rigor/inference/method_dispatcher.rb +175 -1361
- data/lib/rigor/inference/method_parameter_binder.rb +18 -18
- data/lib/rigor/inference/module_function_state.rb +5 -4
- data/lib/rigor/inference/mutation_widening.rb +20 -7
- data/lib/rigor/inference/narrowing/case_literals.rb +58 -0
- data/lib/rigor/inference/narrowing/class_dispatch.rb +302 -0
- data/lib/rigor/inference/narrowing/complements.rb +322 -0
- data/lib/rigor/inference/narrowing/presence_predicates.rb +205 -0
- data/lib/rigor/inference/narrowing/regex_match.rb +499 -0
- data/lib/rigor/inference/narrowing.rb +86 -1269
- data/lib/rigor/inference/object_mixins.rb +2 -2
- data/lib/rigor/inference/opaque_mutation.rb +214 -0
- data/lib/rigor/inference/optimistic_origin.rb +52 -14
- data/lib/rigor/inference/parameter_inference_collector.rb +1 -1
- data/lib/rigor/inference/pre_eval_constants.rb +11 -9
- data/lib/rigor/inference/precision_scanner.rb +27 -2
- data/lib/rigor/inference/project_patched_methods.rb +3 -3
- data/lib/rigor/inference/range_constant.rb +3 -3
- data/lib/rigor/inference/rbs_type_translator.rb +2 -2
- data/lib/rigor/inference/refine_self.rb +8 -7
- data/lib/rigor/inference/refined_dispatch.rb +2 -2
- data/lib/rigor/inference/return_barrier.rb +24 -9
- data/lib/rigor/inference/scope_indexer/alias_def_nodes.rb +311 -0
- data/lib/rigor/inference/scope_indexer/census_scope.rb +47 -0
- data/lib/rigor/inference/scope_indexer/certainty.rb +2 -2
- data/lib/rigor/inference/scope_indexer/class_cvar_census.rb +282 -0
- data/lib/rigor/inference/scope_indexer/class_cvars_collector.rb +12 -5
- data/lib/rigor/inference/scope_indexer/class_declarations.rb +228 -0
- data/lib/rigor/inference/scope_indexer/class_ivar_index.rb +2028 -0
- data/lib/rigor/inference/scope_indexer/constant_ivar_value.rb +4 -3
- data/lib/rigor/inference/scope_indexer/constant_tables.rb +844 -0
- data/lib/rigor/inference/scope_indexer/cvar_hierarchy.rb +159 -0
- data/lib/rigor/inference/scope_indexer/declaration_artifacts.rb +219 -0
- data/lib/rigor/inference/scope_indexer/declaration_signature.rb +212 -0
- data/lib/rigor/inference/scope_indexer/def_nestings_collector.rb +2 -2
- data/lib/rigor/inference/scope_indexer/factory_call_resolver.rb +53 -0
- data/lib/rigor/inference/scope_indexer/literal_receiver_mutations.rb +266 -0
- data/lib/rigor/inference/scope_indexer/member_layouts_collector.rb +8 -7
- data/lib/rigor/inference/scope_indexer/mixin_accumulator.rb +1 -1
- data/lib/rigor/inference/scope_indexer/mixin_effects.rb +470 -0
- data/lib/rigor/inference/scope_indexer/mixin_tables.rb +388 -0
- data/lib/rigor/inference/scope_indexer/mutated_carrier_widening.rb +163 -0
- data/lib/rigor/inference/scope_indexer/mutated_element_census.rb +211 -0
- data/lib/rigor/inference/scope_indexer/node_propagation.rb +520 -0
- data/lib/rigor/inference/scope_indexer/program_globals.rb +154 -0
- data/lib/rigor/inference/scope_indexer/published_constant_cvars.rb +146 -0
- data/lib/rigor/inference/scope_indexer/published_constant_ivars.rb +181 -0
- data/lib/rigor/inference/scope_indexer/shared_walk_tables.rb +823 -0
- data/lib/rigor/inference/scope_indexer/singleton_class_sources.rb +85 -0
- data/lib/rigor/inference/scope_indexer/superclasses_collector.rb +5 -5
- data/lib/rigor/inference/scope_indexer.rb +2657 -9558
- data/lib/rigor/inference/self_call_ivar_writes.rb +22 -3
- data/lib/rigor/inference/set_mutation.rb +2 -2
- data/lib/rigor/inference/singleton_hook_definers.rb +185 -0
- data/lib/rigor/inference/statement_evaluator/def_arguments_and_assertions.rb +365 -0
- data/lib/rigor/inference/statement_evaluator/pattern_bindings.rb +581 -0
- data/lib/rigor/inference/statement_evaluator.rb +201 -1008
- data/lib/rigor/inference/synthetic_method_scanner.rb +0 -3
- data/lib/rigor/language_server/completion_provider.rb +8 -8
- data/lib/rigor/language_server/diagnostic_publisher.rb +6 -7
- data/lib/rigor/language_server/document_symbol_provider.rb +4 -6
- data/lib/rigor/language_server/folding_range_provider.rb +3 -3
- data/lib/rigor/language_server/hover_provider.rb +2 -2
- data/lib/rigor/language_server/loop.rb +2 -2
- data/lib/rigor/language_server/server.rb +3 -2
- data/lib/rigor/language_server/signature_help_provider.rb +7 -9
- data/lib/rigor/lens/document.rb +10 -1
- data/lib/rigor/lens/file_reader.rb +2 -1
- data/lib/rigor/mcp/loop.rb +48 -6
- data/lib/rigor/mcp/server.rb +248 -23
- data/lib/rigor/mcp/skill_catalog.rb +157 -0
- data/lib/rigor/plugin/base.rb +22 -4
- data/lib/rigor/plugin/effect_attribution.rb +1 -1
- data/lib/rigor/plugin/inflector.rb +2 -2
- data/lib/rigor/plugin/io_boundary.rb +1 -1
- data/lib/rigor/plugin/loader.rb +4 -4
- data/lib/rigor/plugin/macro/heredoc_template.rb +4 -3
- data/lib/rigor/plugin/optimistic_return.rb +20 -0
- data/lib/rigor/plugin/registry.rb +2 -2
- data/lib/rigor/plugin/template_unit.rb +4 -2
- data/lib/rigor/plugin/trust_policy.rb +2 -1
- data/lib/rigor/plugin.rb +1 -1
- data/lib/rigor/protection/mutator.rb +8 -9
- data/lib/rigor/rbs_extended/reporter.rb +2 -2
- data/lib/rigor/rbs_extended.rb +2 -2
- data/lib/rigor/reflection/constant_ancestors.rb +1 -1
- data/lib/rigor/reflection/project_method.rb +2 -2
- data/lib/rigor/reflection.rb +4 -4
- data/lib/rigor/scope/discovery_index.rb +15 -10
- data/lib/rigor/scope/resolution_chain.rb +29 -6
- data/lib/rigor/scope.rb +89 -32
- data/lib/rigor/sig_gen/generator.rb +5 -4
- data/lib/rigor/sig_gen/renderer.rb +3 -3
- data/lib/rigor/sig_gen/writer.rb +8 -6
- data/lib/rigor/source/parameter_envelope.rb +1 -1
- data/lib/rigor/triage/catalogue.rb +4 -4
- data/lib/rigor/type/combinator.rb +2 -2
- data/lib/rigor/type/data_instance.rb +2 -2
- data/lib/rigor/type/struct_instance.rb +3 -3
- data/lib/rigor/type_node/generic.rb +2 -1
- data/lib/rigor/type_of_memo.rb +2 -1
- data/lib/rigor/typing_state.rb +3 -2
- data/lib/rigor/version.rb +1 -1
- data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/analyzer.rb +2 -2
- data/plugins/rigor-actioncable/lib/rigor/plugin/actioncable/channel_discoverer.rb +1 -1
- data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer/mailer_discoverer.rb +4 -4
- data/plugins/rigor-actionmailer/lib/rigor/plugin/actionmailer.rb +1 -1
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/controller_discoverer.rb +4 -3
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/effects.rb +7 -5
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/erb_compiler.rb +4 -2
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/render_locals.rb +1 -1
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack/view_assigns.rb +3 -2
- data/plugins/rigor-actionpack/lib/rigor/plugin/actionpack.rb +7 -7
- data/plugins/rigor-activejob/lib/rigor/plugin/activejob/job_discoverer.rb +1 -1
- data/plugins/rigor-activejob/lib/rigor/plugin/activejob.rb +2 -2
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_discoverer.rb +9 -8
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/model_index.rb +3 -2
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_parser.rb +15 -7
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/schema_table.rb +10 -3
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord/structure_sql_parser.rb +12 -1
- data/plugins/rigor-activerecord/lib/rigor/plugin/activerecord.rb +20 -5
- data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage/attachment_discoverer.rb +1 -1
- data/plugins/rigor-activestorage/lib/rigor/plugin/activestorage.rb +3 -2
- data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext/effects.rb +1 -1
- data/plugins/rigor-activesupport-core-ext/lib/rigor/plugin/activesupport_core_ext.rb +8 -7
- data/plugins/rigor-dry-schema/lib/rigor/plugin/dry_schema.rb +1 -1
- data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types/alias_scanner.rb +1 -1
- data/plugins/rigor-dry-types/lib/rigor/plugin/dry_types.rb +6 -7
- data/plugins/rigor-factorybot/lib/rigor/plugin/factorybot/factory_discoverer.rb +3 -2
- data/plugins/rigor-graphql/lib/rigor/plugin/graphql/type_scanner.rb +4 -3
- data/plugins/rigor-hanami/lib/rigor/plugin/hanami.rb +4 -2
- data/plugins/rigor-minitest/lib/rigor/plugin/minitest.rb +1 -1
- data/plugins/rigor-pundit/lib/rigor/plugin/pundit.rb +1 -1
- data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n/analyzer.rb +5 -3
- data/plugins/rigor-rails-i18n/lib/rigor/plugin/rails_i18n.rb +1 -1
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/devise_routes.rb +4 -5
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/doorkeeper_routes.rb +2 -2
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_discoverer.rb +5 -4
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/helper_table.rb +3 -2
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes/routes_parser.rb +30 -31
- data/plugins/rigor-rails-routes/lib/rigor/plugin/rails_routes.rb +13 -15
- data/plugins/rigor-rbnacl/lib/rigor/plugin/rbnacl.rb +1 -1
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec/let_scope_index.rb +4 -3
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec/matcher_analyzer.rb +16 -13
- data/plugins/rigor-rspec/lib/rigor/plugin/rspec/scope_walker.rb +3 -2
- data/plugins/rigor-shoulda-matchers/lib/rigor/plugin/shoulda_matchers/analyzer.rb +6 -6
- data/plugins/rigor-sidekiq/lib/rigor/plugin/sidekiq/analyzer.rb +3 -3
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/assertion_recognizer.rb +2 -2
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog.rb +3 -2
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/catalog_walker.rb +6 -5
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sig_parser.rb +2 -2
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/sigil_detector.rb +4 -5
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet/type_translator.rb +1 -1
- data/plugins/rigor-sorbet/lib/rigor/plugin/sorbet.rb +3 -4
- data/sig/rigor/analysis/check_rules/suppression.rbs +9 -0
- data/sig/rigor/analysis/dependency_recorder.rbs +9 -0
- data/sig/rigor/inference.rbs +275 -11
- data/sig/rigor/plugin/base.rbs +14 -5
- data/sig/rigor/plugin/optimistic_return.rbs +16 -0
- data/sig/rigor/scope.rbs +7 -1
- data/sig/rigor/source.rbs +4 -0
- data/skills/rigor-mcp-setup/SKILL.md +20 -6
- data/skills/rigor-plugin-review/SKILL.md +3 -3
- data/skills/rigor-type-oracle/references/01-oracle-commands.md +1 -1
- metadata +90 -3
|
@@ -0,0 +1,2028 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
require_relative "../../type"
|
|
6
|
+
require_relative "../../source/constant_path"
|
|
7
|
+
require_relative "../../source/node_children"
|
|
8
|
+
require_relative "../census_foreign_block"
|
|
9
|
+
require_relative "../declaration_walk/rules"
|
|
10
|
+
require_relative "../define_method_block_self"
|
|
11
|
+
require_relative "../error_info"
|
|
12
|
+
require_relative "../hash_lookup_mutation"
|
|
13
|
+
require_relative "../index_write_widening"
|
|
14
|
+
require_relative "../macro_block_self_type"
|
|
15
|
+
require_relative "../method_dispatcher"
|
|
16
|
+
require_relative "../multi_target_binder"
|
|
17
|
+
require_relative "../mutation_widening"
|
|
18
|
+
require_relative "../narrowing"
|
|
19
|
+
require_relative "../receiver_writer_call"
|
|
20
|
+
require_relative "../reflective_ivar_write"
|
|
21
|
+
require_relative "../set_mutation"
|
|
22
|
+
require_relative "../string_mutation"
|
|
23
|
+
require_relative "census_scope"
|
|
24
|
+
require_relative "constant_ivar_value"
|
|
25
|
+
require_relative "declared_ivar_types"
|
|
26
|
+
require_relative "declared_param_writes"
|
|
27
|
+
require_relative "empty_seed"
|
|
28
|
+
require_relative "ivar_contributions"
|
|
29
|
+
|
|
30
|
+
module Rigor
|
|
31
|
+
module Inference
|
|
32
|
+
module ScopeIndexer
|
|
33
|
+
# The class ivar index of one file (slice 7 phase 2, ADR-58), moved out of `scope_indexer.rb` whole (ADR-116
|
|
34
|
+
# WD9, #3400): per class, the type each instance variable holds, joined over the writes the file makes.
|
|
35
|
+
# {.build_class_ivar_index} is the entry, which `ScopeIndexer.index` calls once. It walks every class and
|
|
36
|
+
# module body and types each write's rvalue with no local bound; then it widens the entries for the mutations
|
|
37
|
+
# the file observes, adds the read-before-write `nil`, and joins what other files and the ancestor chain
|
|
38
|
+
# contribute ({IvarContributions}) and what a signature declares ({DeclaredIvarTypes}). The other 99 functions
|
|
39
|
+
# are private: the walk with its arms, the collectors of each kind of body, the recording steps, the held
|
|
40
|
+
# `&&=` and `op=` writes, the censuses of the mutations and of the ivars a method hands back, and the
|
|
41
|
+
# definite-assignment summary that finds a transient `@x = nil` ({.dead_transient_nil_writes}), with the
|
|
42
|
+
# per-class table of instance `def`s it reads ({.collect_class_method_defs}), which no other file of `lib/`
|
|
43
|
+
# calls and the specs reach through `send`.
|
|
44
|
+
#
|
|
45
|
+
# It asks {DeclarationWalk::Rules} for each declaration context and {CensusScope}, at four sites, for the scope
|
|
46
|
+
# a body's rvalues are typed under. It calls four functions left in `scope_indexer.rb`, recognisers the other
|
|
47
|
+
# walkers ask too, at one site each: `ScopeIndexer.def_side`, `def_receiver_targets_lexical_self?`,
|
|
48
|
+
# `each_attr_macro_name` and `attr_macro`. Seventeen constants moved with it, the ones only it read in that
|
|
49
|
+
# file. Four are public: {IVAR_BARRIER_NODES} and {BindingTargets}, which {ClassCvarCensus} reads by a path
|
|
50
|
+
# where it read the indexer's private names lexically, and {MethodEffectResolver} and {ATTR_WRITER_MACROS},
|
|
51
|
+
# public as they were. It reads `ScopeIndexer::INSTANCE_EVAL_CALLS` lexically, as {NodePropagation} reads the
|
|
52
|
+
# indexer's constants: other rows of `scope_indexer.rb` read it.
|
|
53
|
+
#
|
|
54
|
+
# `scope_indexer.rb` requires this file, and this file does not require it back. It requires the file of what
|
|
55
|
+
# else it names, the dispatcher among them and so the `rbs` gem. So the file loads alone, and until
|
|
56
|
+
# `scope_indexer.rb` has loaded the entry answers only for a program with no call outside a `def` and no
|
|
57
|
+
# `def` with a body inside a class or module: such a `def` is a `NoMethodError` (`def_side`), a call one too
|
|
58
|
+
# (`attr_macro`), and the block of an eval-family call a `NameError` (`INSTANCE_EVAL_CALLS`).
|
|
59
|
+
module ClassIvarIndex # rubocop:disable Metrics/ModuleLength
|
|
60
|
+
module_function
|
|
61
|
+
|
|
62
|
+
# Slice 7 phase 2. Builds the class-level ivar accumulator by walking every `Prism::ClassNode` /
|
|
63
|
+
# `Prism::ModuleNode` body, descending into each nested `Prism::DefNode`, and typing every
|
|
64
|
+
# `Prism::InstanceVariableWriteNode` rvalue under a scope that carries the appropriate `self_type` for that def
|
|
65
|
+
# (singleton vs instance). The rvalue is typed with NO local bindings — the pre-pass lacks statement-level
|
|
66
|
+
# threading — so `@x = 1` records `Constant[1]` but `@x = some_local + 1` records `Dynamic[Top]` (since
|
|
67
|
+
# `some_local` is unbound at pre-pass time). Multiple writes to the same ivar union via
|
|
68
|
+
# `Type::Combinator.union`.
|
|
69
|
+
def build_class_ivar_index(root, default_scope, declared_ivar_types: true)
|
|
70
|
+
accumulator = {}
|
|
71
|
+
mutated_ivars = {}
|
|
72
|
+
read_before_write = {}
|
|
73
|
+
init_writes = {}
|
|
74
|
+
# WD3 — per class, then per method, the ivars the method definitely assigns non-nil on every completing path,
|
|
75
|
+
# consulted by `dead_transient_nil_writes` so a ctor that reassigns `@x` indirectly through an unconditional
|
|
76
|
+
# same-class method call (`mask!`) credits the overwrite. Built once per program here, memoised by class.
|
|
77
|
+
method_assign_effects = build_method_assign_effects(root)
|
|
78
|
+
# #2291 — an `@x = LIT` write reads the file's constant table, built on the first such write.
|
|
79
|
+
accumulator[CONSTANT_IVAR_VALUES] = ConstantIvarValue.new(root, default_scope)
|
|
80
|
+
walk_class_ivars(root, [], default_scope, accumulator, mutated_ivars,
|
|
81
|
+
read_before_write, init_writes, method_assign_effects)
|
|
82
|
+
accumulator.delete(CONSTANT_IVAR_VALUES)
|
|
83
|
+
reflective_opaque = accumulator.delete(REFLECTIVE_OPAQUE_CLASSES)
|
|
84
|
+
singleton_writes = accumulator.delete(SINGLETON_IVAR_WRITES)
|
|
85
|
+
exempt_class_body_writes!(init_writes, accumulator.delete(DEFINE_METHOD_WRITES) || {})
|
|
86
|
+
merge_held_ivar_writes!(accumulator)
|
|
87
|
+
record_aliased_ivar_mutations!(root, mutated_ivars)
|
|
88
|
+
widen_mutated_ivar_entries!(accumulator, mutated_ivars)
|
|
89
|
+
# #2170 — an ancestor's writes as this file's census types them, before its read-before-write `nil`.
|
|
90
|
+
writes = instance_side_writes(accumulator, singleton_writes)
|
|
91
|
+
contribute_read_before_write_nil!(accumulator, read_before_write, init_writes)
|
|
92
|
+
make_reflective_classes_gradual!(accumulator, reflective_opaque)
|
|
93
|
+
# #2079 / #2170 — another file's writes, and the chain's modules' and superclasses', join existing entries.
|
|
94
|
+
IvarContributions.join!(accumulator, writes, default_scope)
|
|
95
|
+
# An in-place mutation the file observes widens a joined value too (a same-file ancestor's String literal).
|
|
96
|
+
widen_mutated_ivar_entries!(accumulator, mutated_ivars)
|
|
97
|
+
# #1406 — an RBS `@x: T` or `attr_* x: T` declaration replaces the census's join of the writes.
|
|
98
|
+
DeclaredIvarTypes.apply!(accumulator, default_scope) if declared_ivar_types
|
|
99
|
+
accumulator.transform_values(&:freeze).freeze
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# #2170 — a copy of the census entries for {IvarContributions} to join as an ancestor's own. #2931 — an ivar a
|
|
103
|
+
# singleton-side `def` of the class writes is left out: its entry joins that write, which runs on the module or
|
|
104
|
+
# class object and never on an includer's instance, so the join reads the file's instance-side census entries
|
|
105
|
+
# for it, as another file's reader does.
|
|
106
|
+
def instance_side_writes(accumulator, singleton_writes)
|
|
107
|
+
accumulator.to_h do |class_name, entries|
|
|
108
|
+
skipped = singleton_writes&.[](class_name)
|
|
109
|
+
[class_name, skipped ? entries.except(*skipped) : entries.dup]
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Folds the class-body writes {.collect_class_body_ivar_writes} held under {CLASS_BODY_WRITES} into the
|
|
114
|
+
# `init_writes` exemption, except for an ivar a `define_method` block, or a `block_as_methods:` block bound to
|
|
115
|
+
# the receiver's instance ({.method_body_block_call?}), of the class writes (#1768). Such an ivar had no census
|
|
116
|
+
# entry before the block's writes were counted, so the exemption never applied to it; a class-body `@x = nil`
|
|
117
|
+
# writes the class object's ivar and says nothing about whether an instance's `@x` is set, and exempting it
|
|
118
|
+
# would seed `@x` non-nil from the block's write alone (`if @x` folding always-truthy on an instance whose `@x`
|
|
119
|
+
# is unset). It keeps the read-before-write `nil` instead, which only widens.
|
|
120
|
+
def exempt_class_body_writes!(init_writes, define_method_writes)
|
|
121
|
+
(init_writes.delete(CLASS_BODY_WRITES) || {}).each do |class_name, ivars|
|
|
122
|
+
kept = ivars - (define_method_writes[class_name] || EMPTY_GUARDED_IVARS)
|
|
123
|
+
(init_writes[class_name] ||= Set.new).merge(kept)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# B2.3 — finalize the read-before-write nil contribution. For each class, for each ivar where SOME method body
|
|
128
|
+
# observed a read-before-write AND no `initialize` write exists for that ivar, contribute `Constant[nil]` to the
|
|
129
|
+
# class-wide accumulator.
|
|
130
|
+
#
|
|
131
|
+
# The `initialize` filter is the soundness gate: Ruby semantics guarantee `initialize` runs first (via
|
|
132
|
+
# `Class.new`), so a write there reaches every other method body's read. Read-before-write in a non-init method
|
|
133
|
+
# is then NOT a nil-at-runtime case — it's just AST-order coincidence. Without this filter a normal
|
|
134
|
+
# `def initialize; @x = ... end` / `def use; @x.foo end` class would have `@x` widened with nil, producing FPs
|
|
135
|
+
# at every `@x.foo` call.
|
|
136
|
+
def contribute_read_before_write_nil!(accumulator, read_before_write, init_writes)
|
|
137
|
+
nil_t = Type::Combinator.constant_of(nil)
|
|
138
|
+
read_before_write.each do |class_name, ivar_set|
|
|
139
|
+
init_set = init_writes[class_name] || EMPTY_GUARDED_IVARS
|
|
140
|
+
per_class = accumulator[class_name]
|
|
141
|
+
next if per_class.nil?
|
|
142
|
+
|
|
143
|
+
ivar_set.each do |ivar_name|
|
|
144
|
+
# Soundness gates (in order): (1) `initialize` writes the ivar → it's set
|
|
145
|
+
# before any other method runs, so the
|
|
146
|
+
# read-before-write in a sibling method is
|
|
147
|
+
# NOT a runtime nil case.
|
|
148
|
+
# (2) The accumulator has NO entry for the ivar
|
|
149
|
+
# → some write was deliberately skipped (the
|
|
150
|
+
# falsey-default `@x = nil unless @x` slice's
|
|
151
|
+
# no-seed behaviour). Adding nil here would
|
|
152
|
+
# defeat that skip and re-introduce the
|
|
153
|
+
# `Constant[nil]` FP the skip silenced.
|
|
154
|
+
next if init_set.include?(ivar_name)
|
|
155
|
+
next unless per_class.key?(ivar_name)
|
|
156
|
+
|
|
157
|
+
existing = per_class[ivar_name]
|
|
158
|
+
per_class[ivar_name] = Type::Combinator.union(existing, nil_t)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# An ivar a method RETURNS leaves the object, and every mutation of the returned reference mutates the ivar:
|
|
164
|
+
# `(bucket_for(entry)[key] ||= {})[name] = row` fills whichever of `@self_rows` / `@path_rows` / `@result_rows`
|
|
165
|
+
# `bucket_for` handed back. {record_ivar_mutator_call} cannot see it — the mutation's receiver is a `CallNode`,
|
|
166
|
+
# not an `InstanceVariableReadNode` — so those ivars kept the empty `HashShape` their `initialize` gave them and
|
|
167
|
+
# `@path_rows.empty?` folded to `Constant[true]` on a hash the class fills. Rigor's own
|
|
168
|
+
# `lib/rigor/effects/plugin_facts.rb` is the worked site.
|
|
169
|
+
#
|
|
170
|
+
# Scope is deliberately narrow: the mutation receiver must be a SELF-call (an explicit receiver is a different
|
|
171
|
+
# object), the callee must be an instance method of the same class, and only ivars in the callee's RETURN
|
|
172
|
+
# position count — an ivar merely read in its body (`@rows[owner]` returns the value, not the hash) does not
|
|
173
|
+
# escape. Over-recording here would widen a shape carrier that nothing mutates, which costs precision on every
|
|
174
|
+
# reader in the class.
|
|
175
|
+
#
|
|
176
|
+
# Runs after the main walk so the returned-ivar summary is complete regardless of definition order, and gated on
|
|
177
|
+
# a class actually having a returning method — the common class contributes nothing and pays one `defs` lookup.
|
|
178
|
+
def record_aliased_ivar_mutations!(root, mutated_ivars)
|
|
179
|
+
defs = collect_class_method_defs(root)
|
|
180
|
+
returned = returned_ivars_by_method(defs)
|
|
181
|
+
return if returned.empty?
|
|
182
|
+
|
|
183
|
+
defs.each do |class_name, methods|
|
|
184
|
+
per_class = returned[class_name]
|
|
185
|
+
next if per_class.nil?
|
|
186
|
+
|
|
187
|
+
methods.each_value do |def_node|
|
|
188
|
+
gather_aliased_mutations(def_node.body, class_name, per_class, mutated_ivars)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Per class, then per method, the ivars each method hands back, dropping the empty sets so the caller's
|
|
194
|
+
# `next if per_class.nil?` skips a whole class in one lookup.
|
|
195
|
+
def returned_ivars_by_method(defs)
|
|
196
|
+
defs.each_with_object({}) do |(class_name, methods), acc|
|
|
197
|
+
methods.each do |method_name, def_node|
|
|
198
|
+
ivars = returned_ivars(def_node)
|
|
199
|
+
(acc[class_name] ||= {})[method_name] = ivars unless ivars.empty?
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# The ivars `def_node` can hand back: an explicit `return @x` anywhere in the body, and the body's tail
|
|
205
|
+
# expression when it is (or resolves through branch nodes to) a bare ivar read. A tail that computes something
|
|
206
|
+
# FROM an ivar (`@rows[k]`, `@rows.dup`) hands back a different object and is not an escape.
|
|
207
|
+
def returned_ivars(def_node)
|
|
208
|
+
body = def_node.body
|
|
209
|
+
return EMPTY_IVAR_SET if body.nil?
|
|
210
|
+
|
|
211
|
+
found = Set.new
|
|
212
|
+
collect_explicit_return_ivars(body, found)
|
|
213
|
+
tail_value_nodes(body).each { |node| found << node.name if node.is_a?(Prism::InstanceVariableReadNode) }
|
|
214
|
+
found
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def collect_explicit_return_ivars(node, found)
|
|
218
|
+
return unless node.is_a?(Prism::Node)
|
|
219
|
+
return if IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
|
|
220
|
+
|
|
221
|
+
if node.is_a?(Prism::ReturnNode)
|
|
222
|
+
args = node.arguments&.arguments
|
|
223
|
+
found << args.first.name if args && args.size == 1 && args.first.is_a?(Prism::InstanceVariableReadNode)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
node.rigor_each_child { |c| collect_explicit_return_ivars(c, found) }
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# The expressions a body can evaluate to, resolving through the branch nodes whose value is a sub-expression.
|
|
230
|
+
# Depth-capped for the same reason every other walk here is: a pathologically nested tail is not worth unbounded
|
|
231
|
+
# recursion, and stopping early only under-collects (fewer escapes recorded, never more).
|
|
232
|
+
def tail_value_nodes(node, depth = 0)
|
|
233
|
+
return [] unless node.is_a?(Prism::Node)
|
|
234
|
+
return [] if depth >= TAIL_VALUE_DEPTH_CAP
|
|
235
|
+
|
|
236
|
+
children = tail_value_children(node)
|
|
237
|
+
return [node] if children.nil?
|
|
238
|
+
|
|
239
|
+
children.flat_map { |child| tail_value_nodes(child, depth + 1) }
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The children a node's own value comes from, or nil when the node IS the value. Split out from
|
|
243
|
+
# {tail_value_nodes} so the recursion stays one line and this stays a lookup table.
|
|
244
|
+
def tail_value_children(node)
|
|
245
|
+
case node
|
|
246
|
+
when Prism::StatementsNode then [node.body.last]
|
|
247
|
+
when Prism::ParenthesesNode then [node.body]
|
|
248
|
+
when Prism::BeginNode, Prism::ElseNode, Prism::WhenNode, Prism::InNode then [node.statements]
|
|
249
|
+
when Prism::IfNode then [node.statements, node.subsequent]
|
|
250
|
+
when Prism::UnlessNode then [node.statements, node.else_clause]
|
|
251
|
+
when Prism::CaseNode then node.conditions + [node.else_clause]
|
|
252
|
+
when Prism::OrNode, Prism::AndNode then [node.left, node.right]
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Records every mutation in `node` whose receiver is a self-call to one of `per_class`'s returning methods.
|
|
257
|
+
def gather_aliased_mutations(node, class_name, per_class, mutated_ivars)
|
|
258
|
+
return unless node.is_a?(Prism::Node)
|
|
259
|
+
|
|
260
|
+
mutator, receiver = mutation_target(node)
|
|
261
|
+
escaped_ivars_for_receiver(receiver, per_class)&.each do |ivar_name|
|
|
262
|
+
per_ivar = ((mutated_ivars[class_name] ||= {})[ivar_name] ||= Set.new)
|
|
263
|
+
per_ivar << mutator
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
node.rigor_each_child { |c| gather_aliased_mutations(c, class_name, per_class, mutated_ivars) }
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# The ivars a mutation receiver can be, when the receiver is a same-class self-call. Nil for every other shape.
|
|
270
|
+
def escaped_ivars_for_receiver(receiver, per_class)
|
|
271
|
+
return nil unless receiver.is_a?(Prism::CallNode) && receiver.receiver.nil?
|
|
272
|
+
|
|
273
|
+
per_class[receiver.name]
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Walks the post-collected accumulator and widens any Tuple / HashShape / String-literal entry for an ivar that
|
|
277
|
+
# observed a mutator call anywhere in the same class body. The mutation evidence comes from `gather_ivar_writes`
|
|
278
|
+
# recording every `@ivar.<method>(...)` call whose method is in `MutationWidening::SHAPE_MUTATORS`.
|
|
279
|
+
#
|
|
280
|
+
# The widening uses `MutationWidening.widen_for_mutator` — the same primitive
|
|
281
|
+
# `Inference::StatementEvaluator#eval_call` applies for per-method-body widening on a local / ivar receiver. The
|
|
282
|
+
# class-level pass extends that primitive's reach so a `Tuple`-seeded ivar in `initialize` is observed as
|
|
283
|
+
# `Nominal[Array]` at the entry of every OTHER method body in the class — closing the cross-method gap
|
|
284
|
+
# (Redmine 6.1.2 `Redmine::Views::Builders::Structure` is the canonical worked site).
|
|
285
|
+
#
|
|
286
|
+
# Always-safe: the widening can only LOSE precision; the underlying nominal (`Array` / `Hash`) and the element
|
|
287
|
+
# union are preserved.
|
|
288
|
+
def widen_mutated_ivar_entries!(accumulator, mutated_ivars)
|
|
289
|
+
accumulator.each do |class_name, ivars|
|
|
290
|
+
observed = mutated_ivars[class_name]
|
|
291
|
+
next if observed.nil? || observed.empty?
|
|
292
|
+
|
|
293
|
+
ivars.each do |ivar_name, type|
|
|
294
|
+
methods = observed[ivar_name]
|
|
295
|
+
next if methods.nil? || methods.empty?
|
|
296
|
+
|
|
297
|
+
ivars[ivar_name] = widen_type_for_observed_mutators(type, methods)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Walks a class-ivar accumulator entry (which may be a `Union` of multiple write rvalues) and widens any
|
|
303
|
+
# `Tuple`, `HashShape` or String-valued `Constant` member whose corresponding mutator family was observed
|
|
304
|
+
# against the ivar somewhere in the class.
|
|
305
|
+
# Class-level widening is more aggressive than the per-method-body `MutationWidening` primitive: it widens both
|
|
306
|
+
# the SHAPE carrier (Tuple → Array, HashShape → Hash) AND the element types to `Dynamic[Top]`. The justification
|
|
307
|
+
# — once any method mutates the ivar, its post-mutation contents are statically unknown across method
|
|
308
|
+
# boundaries, so preserving the seed-write's element precision would be an unsound over-claim (e.g.
|
|
309
|
+
# `@struct = [{}]; somewhere: @struct << []` makes the next read's element no longer `Constant[{}]`).
|
|
310
|
+
def widen_type_for_observed_mutators(type, observed_methods)
|
|
311
|
+
members = type.is_a?(Type::Union) ? type.members : [type]
|
|
312
|
+
widened = members.map { |m| widen_member_for_observed_mutators(m, observed_methods) }
|
|
313
|
+
Type::Combinator.union(*widened)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def widen_member_for_observed_mutators(member, observed_methods)
|
|
317
|
+
case member
|
|
318
|
+
when Type::Tuple
|
|
319
|
+
return member unless observed_methods.any? { |m| MutationWidening::ARRAY_MUTATORS.include?(m) }
|
|
320
|
+
|
|
321
|
+
Type::Combinator.nominal_of("Array", type_args: [Type::Combinator.untyped])
|
|
322
|
+
when Type::HashShape
|
|
323
|
+
if observed_methods.any? { |m| MutationWidening::HASH_MUTATORS.include?(m) }
|
|
324
|
+
return Type::Combinator.nominal_of("Hash",
|
|
325
|
+
type_args: [Type::Combinator.untyped, Type::Combinator.untyped])
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
widen_member_for_lookup_mutators(member, observed_methods)
|
|
329
|
+
when Type::Constant then widen_constant_for_observed_mutators(member, observed_methods)
|
|
330
|
+
else
|
|
331
|
+
member
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
# `@s = +"ab"` in one method and `@s << "c"` in another: without this arm the seed stayed pinned at every other
|
|
336
|
+
# method's entry, and `@s == "ab"` folded always-truthy on a receiver that holds `"abc"`. A constant-folded Set
|
|
337
|
+
# (`@s = Set["a"]`, then `@s.clear; @s << 1` elsewhere — issue #2294) takes the untyped floor the Tuple arm
|
|
338
|
+
# takes.
|
|
339
|
+
def widen_constant_for_observed_mutators(member, observed_methods)
|
|
340
|
+
if SetMutation.constant?(member)
|
|
341
|
+
return member unless observed_methods.any? { |m| SetMutation::MUTATORS.include?(m) }
|
|
342
|
+
|
|
343
|
+
return Type::Combinator.nominal_of("Set", type_args: [Type::Combinator.untyped])
|
|
344
|
+
end
|
|
345
|
+
return member unless StringMutation.constant?(member) &&
|
|
346
|
+
observed_methods.any? { |m| StringMutation::MUTATORS.include?(m) }
|
|
347
|
+
|
|
348
|
+
Type::Combinator.nominal_of("String")
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# A `HashShape` ivar seed some method gave a default, a default proc or identity keys, and no method stored
|
|
352
|
+
# into, removed from or rewrote: its pairs are the seed's own, so it takes the per-method {HashLookupMutation}
|
|
353
|
+
# widening rather than the untyped floor above — a present key keeps its value across methods, a missing one
|
|
354
|
+
# stops reading `nil`.
|
|
355
|
+
def widen_member_for_lookup_mutators(member, observed_methods)
|
|
356
|
+
observed_methods.reduce(member) do |acc, method_name|
|
|
357
|
+
next acc unless acc.is_a?(Type::HashShape)
|
|
358
|
+
|
|
359
|
+
HashLookupMutation.widen_shape(acc, method_name) || acc
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# `def_owner` names the class a `def` leaf belongs to when `self` is rebound — a
|
|
364
|
+
# meta-new block's class — while `qualified_prefix` stays the lexical cref the
|
|
365
|
+
# whole time (`Module.nesting` never rebinds in a block). `[]` marks a self no
|
|
366
|
+
# name covers (anonymous factory, `class <<` body), where def-keyed facts decline.
|
|
367
|
+
def walk_class_ivars(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
368
|
+
read_before_write = nil, init_writes = nil, method_assign_effects = nil,
|
|
369
|
+
def_owner: nil, singleton_cref: false, defs_singleton: false)
|
|
370
|
+
return unless node.is_a?(Prism::Node)
|
|
371
|
+
|
|
372
|
+
case node
|
|
373
|
+
when Prism::ClassNode, Prism::ModuleNode
|
|
374
|
+
walk_ivars_declaration(node, qualified_prefix, default_scope, accumulator,
|
|
375
|
+
mutated_ivars, read_before_write, init_writes,
|
|
376
|
+
method_assign_effects, def_owner, singleton_cref)
|
|
377
|
+
return
|
|
378
|
+
when Prism::SingletonClassNode
|
|
379
|
+
return walk_singleton_class_ivars(node, qualified_prefix, default_scope, accumulator,
|
|
380
|
+
mutated_ivars, read_before_write, init_writes,
|
|
381
|
+
method_assign_effects, def_owner, singleton_cref)
|
|
382
|
+
when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode,
|
|
383
|
+
Prism::ConstantOrWriteNode, Prism::ConstantPathOrWriteNode,
|
|
384
|
+
Prism::DefNode, Prism::CallNode
|
|
385
|
+
return if walk_ivars_leaf?(node, qualified_prefix, default_scope, accumulator,
|
|
386
|
+
mutated_ivars, read_before_write, init_writes,
|
|
387
|
+
method_assign_effects, def_owner, singleton_cref,
|
|
388
|
+
defs_singleton: defs_singleton)
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
node.rigor_each_child do |child|
|
|
392
|
+
walk_class_ivars(child, qualified_prefix, default_scope, accumulator,
|
|
393
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
394
|
+
def_owner: def_owner, singleton_cref: singleton_cref,
|
|
395
|
+
defs_singleton: defs_singleton)
|
|
396
|
+
end
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# The leaf arms of {.walk_class_ivars}: a `def` collects and stops, a recognised
|
|
400
|
+
# meta-new write or anonymous factory call consumes its own block, and an ordinary
|
|
401
|
+
# call seeds the ADR-38 initializer writes and an `attr_writer` family macro its ivar
|
|
402
|
+
# writes before the ordinary child descent runs.
|
|
403
|
+
# Returns true when the node — and where relevant its block — was consumed.
|
|
404
|
+
def walk_ivars_leaf?(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
405
|
+
read_before_write, init_writes, method_assign_effects,
|
|
406
|
+
def_owner, singleton_cref, defs_singleton: false)
|
|
407
|
+
case node
|
|
408
|
+
when Prism::DefNode
|
|
409
|
+
collect_def_ivar_writes(node, def_owner || qualified_prefix, default_scope, accumulator,
|
|
410
|
+
mutated_ivars, read_before_write, init_writes,
|
|
411
|
+
method_assign_effects, singleton_self: defs_singleton)
|
|
412
|
+
true
|
|
413
|
+
when Prism::CallNode
|
|
414
|
+
return true if walk_ivars_meta_call?(node, qualified_prefix, default_scope, accumulator,
|
|
415
|
+
mutated_ivars, read_before_write, init_writes,
|
|
416
|
+
method_assign_effects, def_owner, singleton_cref,
|
|
417
|
+
defs_singleton: defs_singleton)
|
|
418
|
+
return true if walk_ivars_eval_call?(node, qualified_prefix, default_scope, accumulator,
|
|
419
|
+
mutated_ivars, read_before_write, init_writes,
|
|
420
|
+
method_assign_effects, def_owner, singleton_cref,
|
|
421
|
+
defs_singleton: defs_singleton)
|
|
422
|
+
|
|
423
|
+
owner = def_owner || qualified_prefix
|
|
424
|
+
collect_initializer_block_ivars(node, owner, default_scope, accumulator, mutated_ivars, init_writes)
|
|
425
|
+
if ATTR_WRITER_MACROS.include?(ScopeIndexer.attr_macro(node))
|
|
426
|
+
record_attr_writer_ivars(node, owner, accumulator)
|
|
427
|
+
end
|
|
428
|
+
collect_define_method_block_ivars(node, owner, default_scope, accumulator, mutated_ivars)
|
|
429
|
+
collect_receiver_writer_ivars(node, owner, qualified_prefix, default_scope, accumulator, mutated_ivars)
|
|
430
|
+
false
|
|
431
|
+
else
|
|
432
|
+
walk_ivars_meta_new?(node, qualified_prefix, default_scope, accumulator,
|
|
433
|
+
mutated_ivars, read_before_write, init_writes,
|
|
434
|
+
method_assign_effects, def_owner, singleton_cref)
|
|
435
|
+
end
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# The `K = Class.new { … }` arm of {.walk_class_ivars}: the factory call's receiver and
|
|
439
|
+
# arguments keep the enclosing context; the block's `def`-keyed facts belong to the
|
|
440
|
+
# class the write names while its declarations stay lexical.
|
|
441
|
+
def walk_ivars_meta_new?(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
442
|
+
read_before_write, init_writes, method_assign_effects,
|
|
443
|
+
def_owner, singleton_cref, defs_singleton: false)
|
|
444
|
+
split = DeclarationWalk::Rules.meta_new_block_split(node, qualified_prefix, def_owner, singleton_cref)
|
|
445
|
+
return false unless split
|
|
446
|
+
|
|
447
|
+
enclosing, body, body_self = split
|
|
448
|
+
enclosing.each do |part|
|
|
449
|
+
walk_class_ivars(part, qualified_prefix, default_scope, accumulator,
|
|
450
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
451
|
+
def_owner: def_owner, singleton_cref: singleton_cref,
|
|
452
|
+
defs_singleton: defs_singleton)
|
|
453
|
+
end
|
|
454
|
+
if body
|
|
455
|
+
walk_class_ivars(body, qualified_prefix, default_scope, accumulator,
|
|
456
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
457
|
+
def_owner: body_self, singleton_cref: singleton_cref)
|
|
458
|
+
end
|
|
459
|
+
true
|
|
460
|
+
end
|
|
461
|
+
|
|
462
|
+
# The bare `Class.new { … }`-family call arm of {.walk_class_ivars}: the block's class
|
|
463
|
+
# has no name, so its `def`-keyed ivar facts walk ownerless rather than under the
|
|
464
|
+
# enclosing class. Returns whether an anonymous factory block was walked.
|
|
465
|
+
def walk_ivars_meta_call?(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
466
|
+
read_before_write, init_writes, method_assign_effects,
|
|
467
|
+
def_owner, singleton_cref, defs_singleton: false)
|
|
468
|
+
return false unless DeclarationWalk::Rules.meta_new_constant_rvalue?(node) &&
|
|
469
|
+
node.block.is_a?(Prism::BlockNode)
|
|
470
|
+
|
|
471
|
+
[node.receiver, *node.arguments&.arguments.to_a].compact.each do |part|
|
|
472
|
+
walk_class_ivars(part, qualified_prefix, default_scope, accumulator,
|
|
473
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
474
|
+
def_owner: def_owner, singleton_cref: singleton_cref,
|
|
475
|
+
defs_singleton: defs_singleton)
|
|
476
|
+
end
|
|
477
|
+
if (body = node.block.body)
|
|
478
|
+
walk_class_ivars(body, qualified_prefix, default_scope, accumulator,
|
|
479
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
480
|
+
def_owner: [], singleton_cref: singleton_cref)
|
|
481
|
+
end
|
|
482
|
+
true
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
# The eval-family arm of {.walk_class_ivars}: `def`-keyed ivar facts belong to the
|
|
486
|
+
# receiver's class for `class_eval`, and to the receiver's singleton self for
|
|
487
|
+
# `instance_eval` (`defs_singleton` — `X.instance_eval { def m; @x = 1 }` writes
|
|
488
|
+
# `X`'s own `@x`, typed `singleton(X)` the way `def self.m` is). `self::`
|
|
489
|
+
# declarations anchor on the receiver the same way.
|
|
490
|
+
def walk_ivars_eval_call?(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
491
|
+
read_before_write, init_writes, method_assign_effects,
|
|
492
|
+
def_owner, singleton_cref, defs_singleton: false)
|
|
493
|
+
split = DeclarationWalk::Rules.eval_block_split(node, qualified_prefix, def_owner, singleton_cref)
|
|
494
|
+
return false unless split
|
|
495
|
+
|
|
496
|
+
enclosing, body, body_self = split
|
|
497
|
+
enclosing.each do |part|
|
|
498
|
+
walk_class_ivars(part, qualified_prefix, default_scope, accumulator,
|
|
499
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
500
|
+
def_owner: def_owner, singleton_cref: singleton_cref,
|
|
501
|
+
defs_singleton: defs_singleton)
|
|
502
|
+
end
|
|
503
|
+
if body
|
|
504
|
+
walk_class_ivars(body, qualified_prefix, default_scope, accumulator,
|
|
505
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
506
|
+
def_owner: body_self, singleton_cref: singleton_cref,
|
|
507
|
+
defs_singleton: INSTANCE_EVAL_CALLS.include?(node.name))
|
|
508
|
+
end
|
|
509
|
+
true
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
# The `class <<` arm of {.walk_class_ivars}: the expression evaluates in the enclosing
|
|
513
|
+
# cref — `class << (class D; self; end)` still names `C::D` — while the body's cref is
|
|
514
|
+
# the unnameable singleton class; the marker lifts only at a nameable header.
|
|
515
|
+
def walk_singleton_class_ivars(node, qualified_prefix, default_scope, accumulator, # rubocop:disable Metrics/ParameterLists
|
|
516
|
+
mutated_ivars, read_before_write, init_writes,
|
|
517
|
+
method_assign_effects, def_owner, singleton_cref)
|
|
518
|
+
walk_class_ivars(node.expression, qualified_prefix, default_scope, accumulator,
|
|
519
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
520
|
+
def_owner: def_owner, singleton_cref: singleton_cref)
|
|
521
|
+
return unless node.body
|
|
522
|
+
|
|
523
|
+
# A `def` below `class <<` is a singleton method — its `@x` writes are the class
|
|
524
|
+
# object's own ivars, not instance-ivar facts, so the body walks ownerless.
|
|
525
|
+
walk_class_ivars(node.body, qualified_prefix, default_scope, accumulator,
|
|
526
|
+
mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
527
|
+
def_owner: [], singleton_cref: true)
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
# The declaration arm of {.walk_class_ivars}. Class-body level `@x = nil` writes don't
|
|
531
|
+
# initialise instance ivars at runtime (the class's own singleton ivars and the instance's
|
|
532
|
+
# ivars are separate stores), but they signal "the author KNOWS @x could be nil" and extend
|
|
533
|
+
# the B2.3 soundness gate: an ivar with a class-body write is exempted from the
|
|
534
|
+
# read-before-write nil contribution because the seed already reflects the author's
|
|
535
|
+
# acknowledged nullability via the def-body writes' union. Without this exemption, code that
|
|
536
|
+
# explicitly `@x = nil`s at class-body level then writes `@x = SomeClass.new` inside an
|
|
537
|
+
# instance method gains an unjustified nil widening at every read. Under an unnameable cref
|
|
538
|
+
# a bare/`self::` header opens `#<singleton>::Name` — the write census is skipped and the body
|
|
539
|
+
# walks ownerless; nameable headers re-anchor at a real cref.
|
|
540
|
+
def walk_ivars_declaration(node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
541
|
+
read_before_write, init_writes, method_assign_effects, def_owner, singleton_cref)
|
|
542
|
+
self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(node.constant_path, def_owner)
|
|
543
|
+
child_prefix = self_decl ||
|
|
544
|
+
Source::ConstantPath.declaration_prefix(qualified_prefix, node.constant_path)
|
|
545
|
+
return unless child_prefix && node.body
|
|
546
|
+
|
|
547
|
+
child_cref = DeclarationWalk::Rules.unnameable_decl?(node, self_decl, singleton_cref)
|
|
548
|
+
collect_class_body_ivar_writes(node.body, child_prefix.join("::"), init_writes) if init_writes && !child_cref
|
|
549
|
+
walk_class_ivars(node.body, child_cref ? [] : child_prefix,
|
|
550
|
+
DeclarationWalk::Rules.scope_entering_declaration(default_scope, node.constant_path),
|
|
551
|
+
accumulator, mutated_ivars, read_before_write, init_writes, method_assign_effects,
|
|
552
|
+
singleton_cref: child_cref)
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
def collect_def_ivar_writes(def_node, qualified_prefix, default_scope, accumulator, mutated_ivars, # rubocop:disable Metrics/ParameterLists
|
|
556
|
+
read_before_write = nil, init_writes = nil, method_assign_effects = nil,
|
|
557
|
+
singleton_self: false)
|
|
558
|
+
return if def_node.body.nil? || qualified_prefix.empty?
|
|
559
|
+
# Issue #3155 — `def String.q` writes the ivars of another object.
|
|
560
|
+
return if foreign_def?(def_node, qualified_prefix, false)
|
|
561
|
+
|
|
562
|
+
class_name = qualified_prefix.join("::")
|
|
563
|
+
singleton = singleton_self || def_node.receiver.is_a?(Prism::SelfNode) ||
|
|
564
|
+
ScopeIndexer.def_receiver_targets_lexical_self?(def_node.receiver, qualified_prefix)
|
|
565
|
+
self_type =
|
|
566
|
+
if singleton
|
|
567
|
+
Type::Combinator.singleton_of(class_name)
|
|
568
|
+
else
|
|
569
|
+
Type::Combinator.nominal_of(class_name)
|
|
570
|
+
end
|
|
571
|
+
body_scope = CensusScope.census_body_scope(default_scope, qualified_prefix, self_type)
|
|
572
|
+
|
|
573
|
+
# C2 — transient `@x = nil` dead-write elimination. When a method body opens with an unconditional `@x = nil`
|
|
574
|
+
# (defensive init) and then *definitely* reassigns `@x` to a non-nil value on every completing path (a later
|
|
575
|
+
# unconditional statement-level write, OR an `if/else` whose both branches write `@x`), the opening nil is
|
|
576
|
+
# dead — it can never be observed at method exit. Recording it anyway folds a spurious `nil` constituent into
|
|
577
|
+
# the flow-insensitive class-ivar union, which then poisons reads in OTHER methods (e.g. ipaddr `IN4MASK ^
|
|
578
|
+
# @mask_addr` rejects the resulting `Integer | nil`). The set holds the `object_id`s of the transient write
|
|
579
|
+
# nodes to skip; soundness is post-domination at the top statement level, so dropping the nil never hides a
|
|
580
|
+
# real runtime-nil read.
|
|
581
|
+
dead_writes = dead_transient_nil_writes(def_node.body, class_name, method_assign_effects)
|
|
582
|
+
# #1154 — an `@x = x` from a declared parameter records the parameter's declared type.
|
|
583
|
+
declared = DeclaredParamWrites::EMPTY
|
|
584
|
+
declared = DeclaredParamWrites.collect(def_node, class_name, body_scope) unless singleton
|
|
585
|
+
gather_ivar_writes(def_node.body, body_scope, class_name, accumulator,
|
|
586
|
+
EMPTY_GUARDED_IVARS, mutated_ivars, dead_writes, declared)
|
|
587
|
+
if singleton
|
|
588
|
+
singleton_written = (accumulator[SINGLETON_IVAR_WRITES] ||= {})[class_name] ||= Set.new
|
|
589
|
+
define_method_written_ivars(def_node.body, singleton_written)
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
# B2.3 — collect per-method evidence for the read-before-write nil contribution. The accumulator-level
|
|
593
|
+
# decision ("is this ivar truly read-before-write across the class lifetime?") is finalised at
|
|
594
|
+
# `contribute_read_before_write_nil!` after the whole class body has been walked, using `init_writes` as the
|
|
595
|
+
# soundness gate (an ivar written in `initialize` is initialised before any other method body runs).
|
|
596
|
+
collect_read_before_write_evidence(def_node, class_name, read_before_write, init_writes, default_scope,
|
|
597
|
+
singleton: singleton)
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
# ADR-38 block-form: collects ivar writes from a CallNode's block body (e.g. RSpec `before { @x = … }` /
|
|
601
|
+
# `let(:x) { … }`) and folds them into `init_writes`, suppressing the read-before-write nil contribution the
|
|
602
|
+
# same way a def-form initializer does. The block body is always treated as an initializer (the caller has
|
|
603
|
+
# already verified the method name is declared as a block_method initializer), so there is no read-before-write
|
|
604
|
+
# evidence collection step here.
|
|
605
|
+
def collect_block_ivar_writes(block_node, qualified_prefix, default_scope, accumulator,
|
|
606
|
+
mutated_ivars, init_writes)
|
|
607
|
+
return if block_node.body.nil? || qualified_prefix.empty?
|
|
608
|
+
|
|
609
|
+
class_name = qualified_prefix.join("::")
|
|
610
|
+
self_type = Type::Combinator.nominal_of(class_name)
|
|
611
|
+
body_scope = CensusScope.census_body_scope(default_scope, qualified_prefix, self_type)
|
|
612
|
+
|
|
613
|
+
gather_ivar_writes(block_node.body, body_scope, class_name, accumulator,
|
|
614
|
+
EMPTY_GUARDED_IVARS, mutated_ivars)
|
|
615
|
+
|
|
616
|
+
seen_writes = Set.new
|
|
617
|
+
read_first = Set.new
|
|
618
|
+
detect_read_before_write(block_node.body, seen_writes, read_first, class_name)
|
|
619
|
+
init_set = (init_writes[class_name] ||= Set.new)
|
|
620
|
+
seen_writes.each { |name| init_set << name }
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
# The ADR-38 initializer-call arm of {.walk_class_ivars}: a block-carrying call the
|
|
624
|
+
# owner class declares as a block-form initializer contributes its `@x` writes to
|
|
625
|
+
# `init_writes`. No-op everywhere else.
|
|
626
|
+
def collect_initializer_block_ivars(node, owner, default_scope, accumulator,
|
|
627
|
+
mutated_ivars, init_writes)
|
|
628
|
+
return unless init_writes && !owner.empty? &&
|
|
629
|
+
node.block.is_a?(Prism::BlockNode) &&
|
|
630
|
+
block_initializer?(owner.join("::"), node.name, default_scope)
|
|
631
|
+
|
|
632
|
+
collect_block_ivar_writes(node.block, owner, default_scope,
|
|
633
|
+
accumulator, mutated_ivars, init_writes)
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
# The `attr_writer` family arm of {.walk_class_ivars} (#541). A writer declaration is the class's own
|
|
637
|
+
# statement that `@x` takes values from outside, so it contributes what the hand-written untyped setter
|
|
638
|
+
# `def x=(v) = @x = v` does: `Dynamic[top]`. Without it `@x = nil` in `initialize` was the only write
|
|
639
|
+
# the seed saw, and `if @x` folded always-falsey on a field the class declares assignable. A setter
|
|
640
|
+
# call site (`obj.x = v`) stays out, as ADR-58 WD2 settles; the declaration is class-level evidence
|
|
641
|
+
# in the file the walk reads. `owner` is empty where no class owns an instance-side ivar (a
|
|
642
|
+
# `class << self` body), and the macro contributes nothing there.
|
|
643
|
+
def record_attr_writer_ivars(node, owner, accumulator)
|
|
644
|
+
return if owner.empty?
|
|
645
|
+
|
|
646
|
+
class_name = owner.join("::")
|
|
647
|
+
ScopeIndexer.each_attr_macro_name(node) do |base|
|
|
648
|
+
accumulate_ivar_type(accumulator, class_name, :"@#{base}", Type::Combinator.untyped)
|
|
649
|
+
end
|
|
650
|
+
end
|
|
651
|
+
|
|
652
|
+
# The `define_method` arm of {.walk_class_ivars} (#1768). `define_method(:m) { … }` on the implicit
|
|
653
|
+
# or `self` receiver installs its block as an instance method of `owner`, and Ruby runs the body on
|
|
654
|
+
# the instance, so its `@x = …` writes are the instance's ivars exactly as a `def m` body's are —
|
|
655
|
+
# `DefineMethodBlockSelf` narrows the block's `self` the same way when it is evaluated. Without this
|
|
656
|
+
# arm an ivar `initialize` sets to `nil` and a `define_method` block sets to a String read as `nil`
|
|
657
|
+
# in every sibling `def`. The block is typed under the instance `self`, as a def body is. Only the
|
|
658
|
+
# literal-block form counts: a `define_method(:m, callable)` or `&callable` argument is not walked.
|
|
659
|
+
# `owner` is empty in a `class << self` body (the call defines a class method there) and in an
|
|
660
|
+
# anonymous factory body, and the block contributes nothing there. A `define_method` inside a `def`
|
|
661
|
+
# body is already reached by that body's walk ({.gather_ivar_writes} enters blocks).
|
|
662
|
+
#
|
|
663
|
+
# An ADR-16 `block_as_methods:` call whose entry binds the receiver's instance (`get "/" do … end` in a
|
|
664
|
+
# `Sinatra::Base` subclass under rigor-sinatra) takes the same arm: the plugin declares that its block runs as
|
|
665
|
+
# an instance method of the receiver class, and `MacroBlockSelfType` narrows the block's `self` to that
|
|
666
|
+
# instance.
|
|
667
|
+
def collect_define_method_block_ivars(node, owner, default_scope, accumulator, mutated_ivars)
|
|
668
|
+
return if owner.empty?
|
|
669
|
+
|
|
670
|
+
class_name = owner.join("::")
|
|
671
|
+
return unless method_body_block_call?(node, class_name, default_scope)
|
|
672
|
+
|
|
673
|
+
block = node.block
|
|
674
|
+
return unless block.body
|
|
675
|
+
|
|
676
|
+
body_scope = CensusScope.census_body_scope(default_scope, owner, Type::Combinator.nominal_of(class_name))
|
|
677
|
+
gather_ivar_writes(block.body, body_scope, class_name, accumulator, EMPTY_GUARDED_IVARS, mutated_ivars)
|
|
678
|
+
written = (accumulator[DEFINE_METHOD_WRITES] ||= {})[class_name] ||= Set.new
|
|
679
|
+
define_method_written_ivars(block.body, written)
|
|
680
|
+
end
|
|
681
|
+
|
|
682
|
+
# Issue #2488 — the explicit-receiver arm of {.walk_class_ivars}: `E.attr_writer :w` (or through `send`) records
|
|
683
|
+
# `@w` as `Dynamic[top]` on `E`, as an `attr_writer` in `E`'s body does ({.record_attr_writer_ivars}), and an
|
|
684
|
+
# `E.define_method(:m) { … }` block is walked as an instance body of `E`, as one in `E`'s body is
|
|
685
|
+
# ({.collect_define_method_block_ivars}). `E` resolves as a `class_eval` receiver does; an unnameable one
|
|
686
|
+
# contributes nothing. A call inside a `def` is that body's to walk.
|
|
687
|
+
def collect_receiver_writer_ivars(node, owner, qualified_prefix, default_scope, accumulator, mutated_ivars)
|
|
688
|
+
return if ReceiverWriterCall.receiver(node).nil?
|
|
689
|
+
|
|
690
|
+
target = DeclarationWalk::Rules.eval_receiver_prefix(node, owner, qualified_prefix,
|
|
691
|
+
unnameable_self: owner.empty?)
|
|
692
|
+
return if target.nil? || target.empty?
|
|
693
|
+
|
|
694
|
+
class_name = target.join("::")
|
|
695
|
+
ReceiverWriterCall.writer_ivars(node).each do |ivar|
|
|
696
|
+
accumulate_ivar_type(accumulator, class_name, ivar, Type::Combinator.untyped)
|
|
697
|
+
end
|
|
698
|
+
body = ReceiverWriterCall.define_method_block(node)&.body
|
|
699
|
+
return if body.nil?
|
|
700
|
+
|
|
701
|
+
body_scope = CensusScope.census_body_scope(default_scope, target, Type::Combinator.nominal_of(class_name))
|
|
702
|
+
gather_ivar_writes(body, body_scope, class_name, accumulator, EMPTY_GUARDED_IVARS, mutated_ivars)
|
|
703
|
+
define_method_written_ivars(body, (accumulator[DEFINE_METHOD_WRITES] ||= {})[class_name] ||= Set.new)
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
# The ivars a `define_method` block body writes, in any write form, outside a nested `def` / `class` / `module`.
|
|
707
|
+
def define_method_written_ivars(node, acc)
|
|
708
|
+
return unless node.is_a?(Prism::Node)
|
|
709
|
+
return if IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
|
|
710
|
+
|
|
711
|
+
case node
|
|
712
|
+
when Prism::InstanceVariableWriteNode, Prism::InstanceVariableOrWriteNode,
|
|
713
|
+
Prism::InstanceVariableAndWriteNode, Prism::InstanceVariableOperatorWriteNode,
|
|
714
|
+
Prism::InstanceVariableTargetNode
|
|
715
|
+
acc << node.name
|
|
716
|
+
end
|
|
717
|
+
reflective = ReflectiveIvarWrite.literal_name(node)
|
|
718
|
+
acc << reflective if reflective
|
|
719
|
+
node.rigor_each_child { |child| define_method_written_ivars(child, acc) }
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
# ADR-38 block-form gate: true when a loaded plugin declares `method_name` a block-form initializer for
|
|
723
|
+
# `class_name` (or an ancestor). Mirrors `additional_initializer?` but queries `covers_block_method?` instead of
|
|
724
|
+
# `covers_method?`.
|
|
725
|
+
def block_initializer?(class_name, method_name, default_scope)
|
|
726
|
+
return false if class_name.nil? || default_scope.nil?
|
|
727
|
+
|
|
728
|
+
environment = default_scope.environment
|
|
729
|
+
registry = environment&.plugin_registry
|
|
730
|
+
return false if registry.nil?
|
|
731
|
+
return false if registry.respond_to?(:empty?) && registry.empty?
|
|
732
|
+
return false unless registry.respond_to?(:additional_initializers)
|
|
733
|
+
|
|
734
|
+
registry.additional_initializers.any? do |entry|
|
|
735
|
+
entry.covers_block_method?(method_name) &&
|
|
736
|
+
class_matches_constraint?(class_name, entry.receiver_constraint, environment)
|
|
737
|
+
end
|
|
738
|
+
rescue StandardError
|
|
739
|
+
false
|
|
740
|
+
end
|
|
741
|
+
|
|
742
|
+
# Walks the method body in AST (== execution) order tracking ivar names whose first reference is a read. The set
|
|
743
|
+
# is unioned into the class-wide `read_before_write` accumulator. For `initialize` def bodies, every write
|
|
744
|
+
# target is unioned into `init_writes` instead — used by the finalisation step to suppress nil contribution for
|
|
745
|
+
# ivars the constructor guarantees are initialised. A singleton-side def (`def self.initialize`) is no
|
|
746
|
+
# initializer: `new` never calls it (#3419).
|
|
747
|
+
def collect_read_before_write_evidence(def_node, class_name, read_before_write, init_writes,
|
|
748
|
+
default_scope = nil, singleton: false)
|
|
749
|
+
return if read_before_write.nil? || init_writes.nil?
|
|
750
|
+
|
|
751
|
+
seen_writes = Set.new
|
|
752
|
+
read_first = Set.new
|
|
753
|
+
detect_read_before_write(def_node.body, seen_writes, read_first, class_name)
|
|
754
|
+
|
|
755
|
+
# ADR-38 — `initialize` is the built-in initializer gate; a plugin may declare additional `def`-form
|
|
756
|
+
# initializer methods (minitest `setup`, Rails `after_initialize`, DI setters) on a constrained class. Both
|
|
757
|
+
# fold their writes into `init_writes`, suppressing the read-before-write nil contribution for sibling
|
|
758
|
+
# readers.
|
|
759
|
+
if !singleton && (def_node.name == :initialize ||
|
|
760
|
+
additional_initializer?(class_name, def_node.name, default_scope))
|
|
761
|
+
init_set = (init_writes[class_name] ||= Set.new)
|
|
762
|
+
seen_writes.each { |name| init_set << name }
|
|
763
|
+
# #3419 — `initialize`'s own read of an ivar still unset is evidence too; a plugin's initializer may run
|
|
764
|
+
# after code that sets it, so it gives none.
|
|
765
|
+
read_first = EMPTY_GUARDED_IVARS
|
|
766
|
+
read_first = initializer_own_nil_reads(def_node, class_name) if def_node.name == :initialize
|
|
767
|
+
end
|
|
768
|
+
|
|
769
|
+
return if read_first.empty?
|
|
770
|
+
|
|
771
|
+
rbw_set = (read_before_write[class_name] ||= Set.new)
|
|
772
|
+
read_first.each { |name| rbw_set << name }
|
|
773
|
+
end
|
|
774
|
+
|
|
775
|
+
# #3419 — the ivars `initialize` reads while they still hold `nil`: read before any write, in a top-level
|
|
776
|
+
# statement that comes before the first one that may run other code of the object (a `super`, a `yield`, or a
|
|
777
|
+
# call on the implicit or `self` receiver other than `raise`/`fail`), which may set the ivar first. Nothing
|
|
778
|
+
# else runs on the instance before `initialize`. An ivar the body writes later is in `init_writes`, which
|
|
779
|
+
# keeps the `nil` out of the union.
|
|
780
|
+
def initializer_own_nil_reads(def_node, class_name)
|
|
781
|
+
seen_writes = Set.new
|
|
782
|
+
read_first = Set.new
|
|
783
|
+
top_level_statements(def_node.body).each do |stmt|
|
|
784
|
+
break if runs_object_code?(stmt)
|
|
785
|
+
|
|
786
|
+
detect_read_before_write(stmt, seen_writes, read_first, class_name)
|
|
787
|
+
end
|
|
788
|
+
read_first
|
|
789
|
+
end
|
|
790
|
+
|
|
791
|
+
# True when `node` holds a `super`, a `yield`, or a call on the implicit or `self` receiver other than
|
|
792
|
+
# `raise`/`fail`, outside a nested def, class or lambda.
|
|
793
|
+
def runs_object_code?(node)
|
|
794
|
+
return false unless node.is_a?(Prism::Node)
|
|
795
|
+
return false if OBJECT_CODE_BARRIERS.any? { |klass| node.is_a?(klass) }
|
|
796
|
+
return true if node.is_a?(Prism::SuperNode) || node.is_a?(Prism::ForwardingSuperNode) ||
|
|
797
|
+
node.is_a?(Prism::YieldNode)
|
|
798
|
+
return true if node.is_a?(Prism::CallNode) && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode)) &&
|
|
799
|
+
!%i[raise fail].include?(node.name)
|
|
800
|
+
|
|
801
|
+
node.compact_child_nodes.any? { |c| runs_object_code?(c) }
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
OBJECT_CODE_BARRIERS = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode, Prism::LambdaNode].freeze
|
|
805
|
+
private_constant :OBJECT_CODE_BARRIERS
|
|
806
|
+
|
|
807
|
+
# #3419 — the calls whose literal block a `return` leaves without leaving the method: by name on any receiver
|
|
808
|
+
# (the `nil` key), and by name on the receiver constant the other keys name.
|
|
809
|
+
BLOCK_RETURN_BARRIERS = {
|
|
810
|
+
nil => %i[lambda define_method define_singleton_method at_exit trap].freeze,
|
|
811
|
+
"Thread" => %i[new start fork].freeze, "Fiber" => %i[new].freeze, "Ractor" => %i[new].freeze
|
|
812
|
+
}.freeze
|
|
813
|
+
SEND_CALLS = %i[send public_send __send__].freeze
|
|
814
|
+
private_constant :BLOCK_RETURN_BARRIERS, :SEND_CALLS
|
|
815
|
+
|
|
816
|
+
# ADR-38 — true when a loaded plugin declares `method_name` an additional initializer for `class_name` (or an
|
|
817
|
+
# ancestor). Reads the plugin registry off the pre-pass scope's environment; the receiver-constraint match
|
|
818
|
+
# reuses `Environment#class_ordering` (the same mechanism ADR-16 Tier A's `MacroBlockSelfType` uses). The whole
|
|
819
|
+
# lookup is wrapped so any resolution failure degrades to "no match" — since the gate only ever SUPPRESSES a nil
|
|
820
|
+
# contribution, a missed match is false-positive-safe (it merely leaves the existing nil widening in place).
|
|
821
|
+
def additional_initializer?(class_name, method_name, default_scope)
|
|
822
|
+
return false if class_name.nil? || default_scope.nil?
|
|
823
|
+
|
|
824
|
+
environment = default_scope.environment
|
|
825
|
+
registry = environment&.plugin_registry
|
|
826
|
+
return false if registry.nil?
|
|
827
|
+
return false if registry.respond_to?(:empty?) && registry.empty?
|
|
828
|
+
return false unless registry.respond_to?(:additional_initializers)
|
|
829
|
+
|
|
830
|
+
registry.additional_initializers.any? do |entry|
|
|
831
|
+
entry.covers_method?(method_name) &&
|
|
832
|
+
class_matches_constraint?(class_name, entry.receiver_constraint, environment)
|
|
833
|
+
end
|
|
834
|
+
rescue StandardError
|
|
835
|
+
false
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
def class_matches_constraint?(class_name, constraint, environment)
|
|
839
|
+
return true if class_name == constraint
|
|
840
|
+
return false if environment.nil?
|
|
841
|
+
|
|
842
|
+
ordering = environment.class_ordering(class_name, constraint)
|
|
843
|
+
%i[equal subclass].include?(ordering)
|
|
844
|
+
rescue StandardError
|
|
845
|
+
false
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
IVAR_WRITE_NODES = [
|
|
849
|
+
Prism::InstanceVariableWriteNode,
|
|
850
|
+
Prism::InstanceVariableOrWriteNode,
|
|
851
|
+
Prism::InstanceVariableAndWriteNode,
|
|
852
|
+
Prism::InstanceVariableOperatorWriteNode
|
|
853
|
+
].freeze
|
|
854
|
+
private_constant :IVAR_WRITE_NODES
|
|
855
|
+
|
|
856
|
+
# Walks class-body level statements (i.e. NOT inside any nested DefNode / ClassNode / ModuleNode) and records
|
|
857
|
+
# every `@x = …` write target as a class-body init. Consumed by `contribute_read_before_write_nil!` to exempt
|
|
858
|
+
# ivars the author already knows might be nil (the `@x = nil` at class-body level is the canonical nullability
|
|
859
|
+
# acknowledgement; the instance @x is technically a separate store, but the pragmatic intent is unambiguous).
|
|
860
|
+
#
|
|
861
|
+
# A literal `define_method` block is a method body, not class-body code (#1768): its writes reach the census as
|
|
862
|
+
# a `def` body's do, so they must not exempt the ivar from the read-before-write `nil` either. Only its receiver
|
|
863
|
+
# and arguments are walked.
|
|
864
|
+
def collect_class_body_ivar_writes(node, class_name, init_writes)
|
|
865
|
+
return unless node.is_a?(Prism::Node)
|
|
866
|
+
return if IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
|
|
867
|
+
|
|
868
|
+
return collect_define_method_argument_writes(node, class_name, init_writes) if define_method_block_call?(node)
|
|
869
|
+
|
|
870
|
+
if node.is_a?(Prism::InstanceVariableWriteNode) ||
|
|
871
|
+
node.is_a?(Prism::InstanceVariableOrWriteNode) ||
|
|
872
|
+
node.is_a?(Prism::InstanceVariableAndWriteNode) ||
|
|
873
|
+
node.is_a?(Prism::InstanceVariableOperatorWriteNode)
|
|
874
|
+
((init_writes[CLASS_BODY_WRITES] ||= {})[class_name] ||= Set.new) << node.name
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
node.rigor_each_child do |child|
|
|
878
|
+
collect_class_body_ivar_writes(child, class_name, init_writes)
|
|
879
|
+
end
|
|
880
|
+
end
|
|
881
|
+
|
|
882
|
+
# The arguments of a `define_method` block call, walked by {.collect_class_body_ivar_writes} in place of the
|
|
883
|
+
# call's children so the block body stays out of the class-body init writes.
|
|
884
|
+
def collect_define_method_argument_writes(node, class_name, init_writes)
|
|
885
|
+
node.arguments&.arguments&.each { |arg| collect_class_body_ivar_writes(arg, class_name, init_writes) }
|
|
886
|
+
end
|
|
887
|
+
|
|
888
|
+
# A `define_method` call with a literal block on the implicit or `self` receiver: the block is an instance
|
|
889
|
+
# method body of the class the walk is in (#1768).
|
|
890
|
+
def define_method_block_call?(node)
|
|
891
|
+
DefineMethodBlockSelf.define_method_on_lexical_self?(node) && node.block.is_a?(Prism::BlockNode)
|
|
892
|
+
end
|
|
893
|
+
|
|
894
|
+
# A call with a literal block on the implicit or `self` receiver whose block is an instance method body of
|
|
895
|
+
# `class_name`, the class the walk is in (#1768): `define_method`, or a call an ADR-16 `block_as_methods:` entry
|
|
896
|
+
# declares runs its block as an instance method of the receiver class ({MacroBlockSelfType}). The entry is
|
|
897
|
+
# matched on `Singleton[class_name]`, the receiver such a call has in the class body, through the same
|
|
898
|
+
# receiver-ancestry match the evaluator narrows the block's `self` with, so the census counts exactly the blocks
|
|
899
|
+
# whose `self` is narrowed to the instance.
|
|
900
|
+
#
|
|
901
|
+
# The class-body walk ({.collect_class_body_ivar_writes}) still records a `block_as_methods:` block's writes as
|
|
902
|
+
# class-body writes, and {.exempt_class_body_writes!} takes back every ivar such a block writes.
|
|
903
|
+
def method_body_block_call?(node, class_name, scope)
|
|
904
|
+
return true if define_method_block_call?(node)
|
|
905
|
+
return false unless node.block.is_a?(Prism::BlockNode)
|
|
906
|
+
return false unless scope && (node.receiver.nil? || node.receiver.is_a?(Prism::SelfNode))
|
|
907
|
+
|
|
908
|
+
MacroBlockSelfType.runs_as_receiver_instance_method?(
|
|
909
|
+
scope: scope, call_node: node, receiver_type: Type::Combinator.singleton_of(class_name)
|
|
910
|
+
)
|
|
911
|
+
end
|
|
912
|
+
|
|
913
|
+
def detect_read_before_write(node, seen_writes, read_first, class_name = nil)
|
|
914
|
+
return unless node.is_a?(Prism::Node)
|
|
915
|
+
return if IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
|
|
916
|
+
|
|
917
|
+
read_first << node.name if node.is_a?(Prism::InstanceVariableReadNode) && !seen_writes.include?(node.name)
|
|
918
|
+
|
|
919
|
+
# N1 — parallel / multiple assignment (`@m, @n = [], []`). The ivar targets are `InstanceVariableTargetNode`s,
|
|
920
|
+
# not `InstanceVariableWriteNode`s, so the generic descent below never records them as writes. Left unhandled,
|
|
921
|
+
# an ivar written only via massign in `initialize` stays absent from `init_writes`, and
|
|
922
|
+
# `contribute_read_before_write_nil!` then unions a spurious `nil` into its class-ivar seed — masking a
|
|
923
|
+
# genuinely-typed `@m` (e.g. `Tuple[]`) as `T | nil` at every sibling read. The RHS runs before any
|
|
924
|
+
# target is committed, so descend into `value` FIRST (an ivar read there is read-before-write), then mark
|
|
925
|
+
# every ivar target as written.
|
|
926
|
+
if node.is_a?(Prism::MultiWriteNode)
|
|
927
|
+
detect_read_before_write(node.value, seen_writes, read_first, class_name) if node.value
|
|
928
|
+
detect_multi_write_target_writes(node, seen_writes, read_first, class_name)
|
|
929
|
+
return
|
|
930
|
+
end
|
|
931
|
+
|
|
932
|
+
# Descend BEFORE recording a write — `@x = @x + 1`'s RHS is an `InstanceVariableReadNode` that runs before the
|
|
933
|
+
# write is committed; the read is therefore read-before-write semantically. `each_child` yields the value
|
|
934
|
+
# child before the lvalue target (`compact_child_nodes` field order), matching this order.
|
|
935
|
+
# #2490 — what a block whose `self` is another object reads or writes is that object's.
|
|
936
|
+
each_census_child(node, class_name) do |c|
|
|
937
|
+
detect_read_before_write(c, seen_writes, read_first, class_name)
|
|
938
|
+
end
|
|
939
|
+
|
|
940
|
+
seen_writes << node.name if IVAR_WRITE_NODES.any? { |klass| node.is_a?(klass) }
|
|
941
|
+
# #1931 — a literal `instance_variable_set` is a write too; its value argument, read above, runs first.
|
|
942
|
+
reflective = ReflectiveIvarWrite.literal_name(node)
|
|
943
|
+
seen_writes << reflective if reflective
|
|
944
|
+
end
|
|
945
|
+
|
|
946
|
+
# Records each ivar target of a `MultiWriteNode` / nested `MultiTargetNode` into `seen_writes`, and descends
|
|
947
|
+
# into any non-ivar target (a `CallTargetNode` / `IndexTargetNode` receiver such as `@obj.x, @y = …`) so an ivar
|
|
948
|
+
# read inside a target receiver still counts as read-before-write.
|
|
949
|
+
def detect_multi_write_target_writes(node, seen_writes, read_first, class_name = nil)
|
|
950
|
+
targets = (node.lefts || []) + [node.rest].compact + (node.rights || [])
|
|
951
|
+
targets.each do |target|
|
|
952
|
+
case target
|
|
953
|
+
when Prism::InstanceVariableTargetNode
|
|
954
|
+
seen_writes << target.name
|
|
955
|
+
when Prism::MultiTargetNode
|
|
956
|
+
detect_multi_write_target_writes(target, seen_writes, read_first, class_name)
|
|
957
|
+
when Prism::SplatNode
|
|
958
|
+
inner = target.expression
|
|
959
|
+
seen_writes << inner.name if inner.is_a?(Prism::InstanceVariableTargetNode)
|
|
960
|
+
else
|
|
961
|
+
detect_read_before_write(target, seen_writes, read_first, class_name)
|
|
962
|
+
end
|
|
963
|
+
end
|
|
964
|
+
end
|
|
965
|
+
|
|
966
|
+
IVAR_BARRIER_NODES = [Prism::DefNode, Prism::ClassNode, Prism::ModuleNode].freeze
|
|
967
|
+
|
|
968
|
+
# Issue #2490 — yields the children of `node` a census walk for `class_name` descends into: all of them, or only
|
|
969
|
+
# those {CensusForeignBlock.census_children} keeps when `node` runs code whose `self` is another object. A nil
|
|
970
|
+
# `class_name` keeps them all. A reentered `def` (#2941) is yielded as itself, so a walk that stops at a `def`
|
|
971
|
+
# skips it; `def_bodies:` yields its body instead, for a walk that counts its writes. `reentry: false` (a
|
|
972
|
+
# singleton-side `def`) keeps none of the reentered code.
|
|
973
|
+
def each_census_child(node, class_name, def_bodies: false, reentry: true, &)
|
|
974
|
+
outside = class_name && CensusForeignBlock.census_children(node, class_name, reentry: reentry)
|
|
975
|
+
return node.rigor_each_child(&) unless outside
|
|
976
|
+
|
|
977
|
+
outside.each do |child|
|
|
978
|
+
child = child.body if def_bodies && child.is_a?(Prism::DefNode)
|
|
979
|
+
yield child if child
|
|
980
|
+
end
|
|
981
|
+
end
|
|
982
|
+
|
|
983
|
+
EMPTY_GUARDED_IVARS = Set.new.freeze
|
|
984
|
+
|
|
985
|
+
# Shared empty result for a def that returns no ivar — the overwhelmingly common case.
|
|
986
|
+
EMPTY_IVAR_SET = Set.new.freeze
|
|
987
|
+
private_constant :EMPTY_IVAR_SET
|
|
988
|
+
|
|
989
|
+
# Branch nesting a tail expression is followed through. Stopping early only UNDER-collects escapes.
|
|
990
|
+
TAIL_VALUE_DEPTH_CAP = 8
|
|
991
|
+
private_constant :TAIL_VALUE_DEPTH_CAP
|
|
992
|
+
private_constant :EMPTY_GUARDED_IVARS
|
|
993
|
+
|
|
994
|
+
def gather_ivar_writes(node, scope, class_name, accumulator, guarded_ivars = EMPTY_GUARDED_IVARS, # rubocop:disable Metrics/ParameterLists
|
|
995
|
+
mutated_ivars = nil, dead_writes = nil, declared = DeclaredParamWrites::EMPTY)
|
|
996
|
+
return unless node.is_a?(Prism::Node)
|
|
997
|
+
|
|
998
|
+
if node.is_a?(Prism::InstanceVariableWriteNode)
|
|
999
|
+
unless dead_writes&.include?(node.object_id)
|
|
1000
|
+
record_ivar_write(node, scope, class_name, accumulator,
|
|
1001
|
+
guarded: guarded_ivars.include?(node.name), declared: declared[node])
|
|
1002
|
+
end
|
|
1003
|
+
else
|
|
1004
|
+
record_compound_ivar_write(node, scope, class_name, accumulator)
|
|
1005
|
+
record_reflective_ivar_write(node, scope, class_name, accumulator)
|
|
1006
|
+
end
|
|
1007
|
+
|
|
1008
|
+
# N1 — parallel / multiple assignment (`old, @cb = @cb, block`, `@i, @o, @e, @thr = Open3.popen3(cmd)`). A
|
|
1009
|
+
# direct `InstanceVariableWriteNode` is the only write form this collector handled, so an ivar appearing as a
|
|
1010
|
+
# `MultiWriteNode` target was silently dropped from the class-ivar union — leaving it to seed as pure `nil`
|
|
1011
|
+
# (from a sibling `@cb = nil` ctor write, or absent entirely) and false-fire `if @cb` always-falsey /
|
|
1012
|
+
# `@thr.alive?` undefined-for-nil. Record each ivar target with its tuple-position RHS type where the RHS is
|
|
1013
|
+
# array/tuple-shaped, else the unanalyzable floor (the same `Dynamic[top]` a single write to an unknown RHS
|
|
1014
|
+
# records — an unanalyzable multi-write means unknown, not nil).
|
|
1015
|
+
record_multi_write_ivars(node, scope, class_name, accumulator)
|
|
1016
|
+
record_binding_target_ivars(node, scope, class_name, accumulator)
|
|
1017
|
+
|
|
1018
|
+
record_ivar_mutator_call(node, class_name, mutated_ivars) if mutated_ivars
|
|
1019
|
+
|
|
1020
|
+
# Don't recurse into nested defs, classes, or modules; their ivars belong to their own enclosing class.
|
|
1021
|
+
return if IVAR_BARRIER_NODES.any? { |klass| node.is_a?(klass) }
|
|
1022
|
+
|
|
1023
|
+
if node.is_a?(Prism::IfNode) || node.is_a?(Prism::UnlessNode)
|
|
1024
|
+
walk_conditional_ivar_writes(node, scope, class_name, accumulator, guarded_ivars,
|
|
1025
|
+
mutated_ivars, dead_writes, declared)
|
|
1026
|
+
return
|
|
1027
|
+
end
|
|
1028
|
+
|
|
1029
|
+
# #2490 — a block or `class <<` body whose `self` is another object writes that object's ivars. #2941 — a
|
|
1030
|
+
# `def` in it that runs on a `class_name` instance again counts its writes.
|
|
1031
|
+
# The reentry is for code that runs on an instance again, so a singleton-side `def` takes none.
|
|
1032
|
+
each_census_child(node, class_name, def_bodies: true, reentry: !scope.self_type.is_a?(Type::Singleton)) do |c|
|
|
1033
|
+
gather_ivar_writes(c, scope, class_name, accumulator, guarded_ivars, mutated_ivars, dead_writes, declared)
|
|
1034
|
+
end
|
|
1035
|
+
end
|
|
1036
|
+
|
|
1037
|
+
# #1931 — `instance_variable_set` on `self` writes an ivar no write node shows ({ReflectiveIvarWrite}). A
|
|
1038
|
+
# literal name (`instance_variable_set(:@k, v)`) contributes the value's type to that ivar's entry, as `@k = v`
|
|
1039
|
+
# would. A name the analysis cannot read (`instance_variable_set(:"@#{k}", v)`, the options-hash constructor
|
|
1040
|
+
# idiom) may write any ivar of the class, so every entry of the class turns gradual once the walk ends
|
|
1041
|
+
# ({.make_reflective_classes_gradual!}): a reader in another method otherwise kept a write's literal
|
|
1042
|
+
# (`@timeout = 5` read as `5`) and reported a call on it.
|
|
1043
|
+
def record_reflective_ivar_write(node, scope, class_name, accumulator)
|
|
1044
|
+
return unless ReflectiveIvarWrite.self_write?(node)
|
|
1045
|
+
|
|
1046
|
+
name = ReflectiveIvarWrite.literal_name(node)
|
|
1047
|
+
if name
|
|
1048
|
+
accumulate_ivar_type(accumulator, class_name, name, scope.type_of(ReflectiveIvarWrite.value_node(node)))
|
|
1049
|
+
else
|
|
1050
|
+
(accumulator[REFLECTIVE_OPAQUE_CLASSES] ||= Set.new) << class_name
|
|
1051
|
+
end
|
|
1052
|
+
end
|
|
1053
|
+
|
|
1054
|
+
def make_reflective_classes_gradual!(accumulator, classes)
|
|
1055
|
+
classes&.each do |class_name|
|
|
1056
|
+
entries = accumulator[class_name]
|
|
1057
|
+
next if entries.nil?
|
|
1058
|
+
|
|
1059
|
+
accumulator[class_name] = entries.transform_values { Type::Combinator.untyped }
|
|
1060
|
+
end
|
|
1061
|
+
end
|
|
1062
|
+
|
|
1063
|
+
# Records `@ivar.<method>(...)` calls whose method is in `MutationWidening::SHAPE_MUTATORS` or
|
|
1064
|
+
# `SetMutation::MUTATORS` ({.ivar_census_mutator?}).
|
|
1065
|
+
# The class-ivar pre-pass uses the resulting set to widen the post-collected accumulator entries (see
|
|
1066
|
+
# {.widen_mutated_ivar_entries!}). Always-safe to over-collect: any name that the widening primitive declines is
|
|
1067
|
+
# ignored at finalization.
|
|
1068
|
+
def record_ivar_mutator_call(node, class_name, mutated_ivars)
|
|
1069
|
+
method_name, receiver = mutation_target(node)
|
|
1070
|
+
return if method_name.nil?
|
|
1071
|
+
return unless receiver.is_a?(Prism::InstanceVariableReadNode)
|
|
1072
|
+
return unless ivar_census_mutator?(method_name)
|
|
1073
|
+
|
|
1074
|
+
per_class = (mutated_ivars[class_name] ||= {})
|
|
1075
|
+
per_ivar = (per_class[receiver.name] ||= Set.new)
|
|
1076
|
+
per_ivar << method_name
|
|
1077
|
+
end
|
|
1078
|
+
|
|
1079
|
+
# The Set-only names (`add`, `merge`, … — issue #2294) are recorded here rather than in `SHAPE_MUTATORS`, which
|
|
1080
|
+
# every scan reads by name alone: there `merge` would make `Hash#merge`, which returns a new Hash, a mutation of
|
|
1081
|
+
# its receiver. The census is safe to over-collect, since {.widen_member_for_observed_mutators} reads each name
|
|
1082
|
+
# against the table of the carrier it widens.
|
|
1083
|
+
def ivar_census_mutator?(method_name)
|
|
1084
|
+
MutationWidening::SHAPE_MUTATORS.include?(method_name) || SetMutation::MUTATORS.include?(method_name)
|
|
1085
|
+
end
|
|
1086
|
+
|
|
1087
|
+
# `[mutator name, receiver node]` for a node that mutates its receiver, `nil` otherwise.
|
|
1088
|
+
#
|
|
1089
|
+
# `@h[k] ||= v` and its `&&=` / `+=` siblings store through `[]=` but are not `[]=` CallNodes; missing them left
|
|
1090
|
+
# an `@h = {}` mutated only that way carrying its empty `HashShape` into every sibling method, so `@h.empty?`
|
|
1091
|
+
# folded to `Constant[true]` on a hash the class fills. A `Prism::IndexTargetNode` stores the same way wherever
|
|
1092
|
+
# it appears — a multi-assign `@h[k], x = …`, a `for @h[k] in` index, a `rescue => @h[k]` reference — and this
|
|
1093
|
+
# widening takes no stored value, so it counts in every position.
|
|
1094
|
+
# `IndexWriteWidening::CONTENT_WRITE_NODE_CLASSES` owns that list; it is spelled out here as an explicit
|
|
1095
|
+
# disjunction because that is what narrows `node` to something with a `#receiver` —
|
|
1096
|
+
# `CONTENT_WRITE_NODE_CLASSES.any? { … }` reads as a call on `Prism::Node` and Rigor rejects it, correctly.
|
|
1097
|
+
def mutation_target(node)
|
|
1098
|
+
return [node.name, node.receiver] if node.is_a?(Prism::CallNode)
|
|
1099
|
+
|
|
1100
|
+
if node.is_a?(Prism::IndexOrWriteNode) || node.is_a?(Prism::IndexAndWriteNode) ||
|
|
1101
|
+
node.is_a?(Prism::IndexOperatorWriteNode) || node.is_a?(Prism::IndexTargetNode)
|
|
1102
|
+
return [IndexWriteWidening::MUTATOR, node.receiver]
|
|
1103
|
+
end
|
|
1104
|
+
|
|
1105
|
+
nil
|
|
1106
|
+
end
|
|
1107
|
+
|
|
1108
|
+
# Walk an `IfNode` / `UnlessNode` so writes inside the THEN body that look like defensive ivar initialisation
|
|
1109
|
+
# gain a `nil` union in the seeded type. Without this, `@x = v unless @x` records `Constant[v]` for `@x`, then
|
|
1110
|
+
# the predicate folds to that same constant and `flow.always-truthy-condition` fires against a working program.
|
|
1111
|
+
# Mirrors the falsey-literal skip `record_ivar_or_write` makes for `@x ||= <falsey>` — a `||=` whose rvalue
|
|
1112
|
+
# can only leave the ivar falsey contributes no useful precision either (#1175).
|
|
1113
|
+
#
|
|
1114
|
+
# Polarity-aware on purpose: only the THEN body picks up the guard. The ELSE branch of `if @x; ...; else; @x =
|
|
1115
|
+
# init; end` would otherwise be marked too — but that pattern (write @x in the else of `if @x`) is a separate
|
|
1116
|
+
# idiom whose surrounding reads of `@x` would then surface a nil-receiver FP. The ELSE branch is left ungarded
|
|
1117
|
+
# so those reads continue to type as they did before this fix.
|
|
1118
|
+
def walk_conditional_ivar_writes(node, scope, class_name, accumulator, guarded_ivars, # rubocop:disable Metrics/ParameterLists
|
|
1119
|
+
mutated_ivars = nil, dead_writes = nil, declared = DeclaredParamWrites::EMPTY)
|
|
1120
|
+
then_guards = then_body_guarded_ivars(node)
|
|
1121
|
+
then_guarded = then_guards.empty? ? guarded_ivars : (guarded_ivars | then_guards)
|
|
1122
|
+
|
|
1123
|
+
gather_ivar_writes(node.predicate, scope, class_name, accumulator, guarded_ivars,
|
|
1124
|
+
mutated_ivars, dead_writes, declared)
|
|
1125
|
+
if node.statements
|
|
1126
|
+
gather_ivar_writes(node.statements, scope, class_name, accumulator, then_guarded,
|
|
1127
|
+
mutated_ivars, dead_writes, declared)
|
|
1128
|
+
end
|
|
1129
|
+
branch = node.is_a?(Prism::IfNode) ? node.subsequent : node.else_clause
|
|
1130
|
+
return unless branch
|
|
1131
|
+
|
|
1132
|
+
gather_ivar_writes(branch, scope, class_name, accumulator, guarded_ivars,
|
|
1133
|
+
mutated_ivars, dead_writes, declared)
|
|
1134
|
+
end
|
|
1135
|
+
|
|
1136
|
+
# Returns the set of ivar names that, in the THEN body of this conditional, are statically known to be in a nil
|
|
1137
|
+
# / unset state — i.e. the body really IS the defensive-init half of the idiom. Conservative on purpose: only
|
|
1138
|
+
# the shapes that idiomatically express "the ivar is missing" qualify.
|
|
1139
|
+
#
|
|
1140
|
+
# For `unless P; body; end`, body runs when `P` is falsey:
|
|
1141
|
+
# - `P = @x` (or `@x && other` / `@x || other`) → @x is falsey
|
|
1142
|
+
# - `P = defined?(@x)` → @x is undefined
|
|
1143
|
+
#
|
|
1144
|
+
# For `if P; body; ...`, body runs when `P` is truthy:
|
|
1145
|
+
# - `P = @x.nil?` → @x is nil
|
|
1146
|
+
# - `P = !@x` / `not @x` → @x is falsey
|
|
1147
|
+
def then_body_guarded_ivars(node)
|
|
1148
|
+
names = Set.new
|
|
1149
|
+
if node.is_a?(Prism::UnlessNode)
|
|
1150
|
+
collect_truthy_test_ivars(node.predicate, names)
|
|
1151
|
+
collect_defined_test_ivars(node.predicate, names)
|
|
1152
|
+
else
|
|
1153
|
+
collect_nil_test_ivars(node.predicate, names)
|
|
1154
|
+
end
|
|
1155
|
+
names
|
|
1156
|
+
end
|
|
1157
|
+
|
|
1158
|
+
def collect_truthy_test_ivars(node, names)
|
|
1159
|
+
return unless node.is_a?(Prism::Node)
|
|
1160
|
+
|
|
1161
|
+
case node
|
|
1162
|
+
when Prism::InstanceVariableReadNode
|
|
1163
|
+
names << node.name
|
|
1164
|
+
when Prism::AndNode, Prism::OrNode
|
|
1165
|
+
collect_truthy_test_ivars(node.left, names)
|
|
1166
|
+
collect_truthy_test_ivars(node.right, names)
|
|
1167
|
+
end
|
|
1168
|
+
end
|
|
1169
|
+
|
|
1170
|
+
def collect_defined_test_ivars(node, names)
|
|
1171
|
+
return unless node.is_a?(Prism::Node)
|
|
1172
|
+
|
|
1173
|
+
case node
|
|
1174
|
+
when Prism::DefinedNode
|
|
1175
|
+
target = node.value
|
|
1176
|
+
names << target.name if target.is_a?(Prism::InstanceVariableReadNode)
|
|
1177
|
+
when Prism::AndNode, Prism::OrNode
|
|
1178
|
+
collect_defined_test_ivars(node.left, names)
|
|
1179
|
+
collect_defined_test_ivars(node.right, names)
|
|
1180
|
+
end
|
|
1181
|
+
end
|
|
1182
|
+
|
|
1183
|
+
def collect_nil_test_ivars(node, names)
|
|
1184
|
+
return unless node.is_a?(Prism::Node)
|
|
1185
|
+
|
|
1186
|
+
case node
|
|
1187
|
+
when Prism::CallNode
|
|
1188
|
+
receiver = node.receiver
|
|
1189
|
+
if receiver.is_a?(Prism::InstanceVariableReadNode) &&
|
|
1190
|
+
%i[nil? !].include?(node.name)
|
|
1191
|
+
names << receiver.name
|
|
1192
|
+
end
|
|
1193
|
+
when Prism::AndNode, Prism::OrNode
|
|
1194
|
+
collect_nil_test_ivars(node.left, names)
|
|
1195
|
+
collect_nil_test_ivars(node.right, names)
|
|
1196
|
+
end
|
|
1197
|
+
end
|
|
1198
|
+
|
|
1199
|
+
# WD3 — ADR-41-style hard cap on how deep the same-class-call definite-assignment crediting recurses (the ctor
|
|
1200
|
+
# calls `mask!`, which could itself call another same-class helper). Cycle-guarded independently; the cap bounds
|
|
1201
|
+
# even acyclic chains.
|
|
1202
|
+
SAME_CLASS_CALL_DEPTH_CAP = 3
|
|
1203
|
+
private_constant :SAME_CLASS_CALL_DEPTH_CAP
|
|
1204
|
+
|
|
1205
|
+
# WD3 — builds the per-class definite-assignment summary: per class, then per method, the ivars assigned non-nil
|
|
1206
|
+
# on every completing path. Used so a ctor's `dead_transient_nil_writes` can credit an indirect overwrite
|
|
1207
|
+
# through an unconditionally-called same-class method (ipaddr's `initialize` reassigns `@mask_addr` via
|
|
1208
|
+
# `mask!`).
|
|
1209
|
+
#
|
|
1210
|
+
# Each method's set is computed by the same suffix definite-assignment analysis used for the ctor seed, run from
|
|
1211
|
+
# the method body's first statement for every ivar the method writes anywhere. Same-class calls inside a method
|
|
1212
|
+
# are credited transitively (depth-capped, cycle-guarded) so the resulting FLAT table is correct at depth 0 for
|
|
1213
|
+
# the ctor lookup.
|
|
1214
|
+
def build_method_assign_effects(root)
|
|
1215
|
+
defs = collect_class_method_defs(root)
|
|
1216
|
+
effects = {}
|
|
1217
|
+
memo = {}.compare_by_identity
|
|
1218
|
+
defs.each do |class_name, methods|
|
|
1219
|
+
methods.each do |method_name, def_node|
|
|
1220
|
+
assigns = method_definite_assigns(class_name, method_name, def_node, defs, effects, memo, 0)
|
|
1221
|
+
(effects[class_name] ||= {})[method_name] = assigns unless assigns.empty?
|
|
1222
|
+
end
|
|
1223
|
+
end
|
|
1224
|
+
effects.freeze
|
|
1225
|
+
end
|
|
1226
|
+
|
|
1227
|
+
# Collects `{class_name => {method_name => DefNode}}` for every instance-method def in the program. Singleton
|
|
1228
|
+
# defs (`def self.x`) are excluded — the ctor-call crediting only follows instance-method calls on `self`. Last
|
|
1229
|
+
# def wins on redefinition.
|
|
1230
|
+
# `def_owner` is the meta-new override described on {ScopeIndexer.walk_methods_and_def_nodes}: inside a
|
|
1231
|
+
# `K = Class.new { … }` block `self` is the class the write names, so `def` leaves record under
|
|
1232
|
+
# it while `Module.nesting` — and therefore `prefix` — stays lexical. `[]` marks the anonymous
|
|
1233
|
+
# block of an unnameable write; nil (the default) leaves defs under the lexical prefix.
|
|
1234
|
+
def collect_class_method_defs(root, prefix = [], acc = {}, def_owner: nil, singleton_cref: false,
|
|
1235
|
+
defs_singleton: false)
|
|
1236
|
+
return acc unless root.is_a?(Prism::Node)
|
|
1237
|
+
|
|
1238
|
+
case root
|
|
1239
|
+
when Prism::ClassNode, Prism::ModuleNode
|
|
1240
|
+
return collect_decl_method_defs(root, prefix, acc, def_owner, singleton_cref)
|
|
1241
|
+
when Prism::SingletonClassNode
|
|
1242
|
+
# The expression evaluates in the enclosing cref; the body's cref is unnameable.
|
|
1243
|
+
return collect_singleton_method_defs(root, prefix, acc, def_owner, singleton_cref)
|
|
1244
|
+
when Prism::ConstantWriteNode, Prism::ConstantPathWriteNode, Prism::ConstantOrWriteNode,
|
|
1245
|
+
Prism::ConstantPathOrWriteNode
|
|
1246
|
+
return acc if collect_meta_new_method_defs?(root, prefix, acc, def_owner, singleton_cref)
|
|
1247
|
+
when Prism::DefNode
|
|
1248
|
+
# `defs_singleton` is the instance_eval split — `def` binds on the receiver's
|
|
1249
|
+
# singleton, an instance-def table this is not.
|
|
1250
|
+
return acc if defs_singleton
|
|
1251
|
+
|
|
1252
|
+
record_collected_method_def(root, def_owner || prefix, acc)
|
|
1253
|
+
return acc
|
|
1254
|
+
when Prism::CallNode
|
|
1255
|
+
return acc if collect_call_method_defs?(root, prefix, acc, def_owner, singleton_cref,
|
|
1256
|
+
defs_singleton)
|
|
1257
|
+
end
|
|
1258
|
+
|
|
1259
|
+
root.rigor_each_child do |c|
|
|
1260
|
+
collect_class_method_defs(c, prefix, acc, def_owner: def_owner, singleton_cref: singleton_cref,
|
|
1261
|
+
defs_singleton: defs_singleton)
|
|
1262
|
+
end
|
|
1263
|
+
acc
|
|
1264
|
+
end
|
|
1265
|
+
|
|
1266
|
+
# The `def` leaf of {.collect_class_method_defs}: `def self.x` is a singleton def the
|
|
1267
|
+
# table does not collect; an ownerless prefix files nothing.
|
|
1268
|
+
def record_collected_method_def(root, rec_prefix, acc)
|
|
1269
|
+
(acc[rec_prefix.join("::")] ||= {})[root.name] = root unless rec_prefix.empty? || root.receiver
|
|
1270
|
+
end
|
|
1271
|
+
|
|
1272
|
+
# The call arm of {.collect_class_method_defs}: dispatches to the anonymous meta-new and
|
|
1273
|
+
# eval-family handlers; any other call keeps walking its children below.
|
|
1274
|
+
def collect_call_method_defs?(root, prefix, acc, def_owner, singleton_cref, defs_singleton)
|
|
1275
|
+
collect_anonymous_meta_defs?(root, prefix, acc, def_owner, singleton_cref, defs_singleton) ||
|
|
1276
|
+
collect_eval_method_defs?(root, prefix, acc, def_owner, singleton_cref, defs_singleton)
|
|
1277
|
+
end
|
|
1278
|
+
|
|
1279
|
+
# The meta-new arm of {.collect_class_method_defs}: the write's receiver and arguments
|
|
1280
|
+
# evaluate in the enclosing context; the block's defs belong to the class the write
|
|
1281
|
+
# names, its declarations to the enclosing cref.
|
|
1282
|
+
def collect_meta_new_method_defs?(root, prefix, acc, enclosing_owner, singleton_cref)
|
|
1283
|
+
call = DeclarationWalk::Rules.meta_new_block_call(root)
|
|
1284
|
+
return false unless call
|
|
1285
|
+
|
|
1286
|
+
child_prefix = DeclarationWalk::Rules.meta_new_child_prefix(root, prefix, enclosing_owner)
|
|
1287
|
+
meta_ownerless = singleton_cref &&
|
|
1288
|
+
!DeclarationWalk::Rules.meta_new_path_target_nameable?(root, enclosing_owner)
|
|
1289
|
+
[call.receiver, *call.arguments&.arguments.to_a].compact.each do |part|
|
|
1290
|
+
collect_class_method_defs(part, prefix, acc, def_owner: enclosing_owner,
|
|
1291
|
+
singleton_cref: singleton_cref)
|
|
1292
|
+
end
|
|
1293
|
+
if (body = DeclarationWalk::Rules.meta_new_block_body(root))
|
|
1294
|
+
collect_class_method_defs(body, prefix, acc,
|
|
1295
|
+
def_owner: (meta_ownerless ? nil : child_prefix) || [],
|
|
1296
|
+
singleton_cref: singleton_cref)
|
|
1297
|
+
end
|
|
1298
|
+
true
|
|
1299
|
+
end
|
|
1300
|
+
|
|
1301
|
+
# The bare `Class.new { … }`-family call arm of {.collect_class_method_defs}: the
|
|
1302
|
+
# block's class has no name, so its `def`s belong to no nameable owner — they walk
|
|
1303
|
+
# under the empty prefix rather than the enclosing class.
|
|
1304
|
+
def collect_anonymous_meta_defs?(root, prefix, acc, def_owner, singleton_cref, defs_singleton)
|
|
1305
|
+
return false unless DeclarationWalk::Rules.meta_new_constant_rvalue?(root) &&
|
|
1306
|
+
root.block.is_a?(Prism::BlockNode)
|
|
1307
|
+
|
|
1308
|
+
[root.receiver, *root.arguments&.arguments.to_a].compact.each do |part|
|
|
1309
|
+
collect_class_method_defs(part, prefix, acc, def_owner: def_owner,
|
|
1310
|
+
singleton_cref: singleton_cref,
|
|
1311
|
+
defs_singleton: defs_singleton)
|
|
1312
|
+
end
|
|
1313
|
+
if (body = root.block.body)
|
|
1314
|
+
collect_class_method_defs(body, prefix, acc, def_owner: [],
|
|
1315
|
+
singleton_cref: singleton_cref)
|
|
1316
|
+
end
|
|
1317
|
+
true
|
|
1318
|
+
end
|
|
1319
|
+
|
|
1320
|
+
# The eval-family arm of {.collect_class_method_defs}: the block's `def`s bind on the
|
|
1321
|
+
# receiver — a nameable receiver re-anchors the owner, a bare/`self` receiver keeps the
|
|
1322
|
+
# enclosing self, and a receiver that names nothing walks the body ownerless.
|
|
1323
|
+
def collect_eval_method_defs?(root, prefix, acc, def_owner, singleton_cref, defs_singleton)
|
|
1324
|
+
return false unless DeclarationWalk::Rules.receiver_eval_call?(root)
|
|
1325
|
+
|
|
1326
|
+
[root.receiver, *root.arguments&.arguments.to_a].compact.each do |part|
|
|
1327
|
+
collect_class_method_defs(part, prefix, acc, def_owner: def_owner,
|
|
1328
|
+
singleton_cref: singleton_cref,
|
|
1329
|
+
defs_singleton: defs_singleton)
|
|
1330
|
+
end
|
|
1331
|
+
self_prefix = def_owner || prefix
|
|
1332
|
+
unnameable = DeclarationWalk::Rules.unnameable_eval_self?(false, def_owner, prefix, singleton_cref)
|
|
1333
|
+
eval_prefix = DeclarationWalk::Rules.eval_receiver_prefix(root, self_prefix, prefix,
|
|
1334
|
+
unnameable_self: unnameable) || []
|
|
1335
|
+
if (body = root.block.body)
|
|
1336
|
+
collect_class_method_defs(body, prefix, acc, def_owner: eval_prefix,
|
|
1337
|
+
singleton_cref: singleton_cref,
|
|
1338
|
+
defs_singleton: INSTANCE_EVAL_CALLS.include?(root.name))
|
|
1339
|
+
end
|
|
1340
|
+
true
|
|
1341
|
+
end
|
|
1342
|
+
|
|
1343
|
+
# The class/module arm of {.collect_class_method_defs}: defs inside the body belong to
|
|
1344
|
+
# the declared class — `def_owner` clears — while an unnameable header below an
|
|
1345
|
+
# unnameable cref walks the body ownerless.
|
|
1346
|
+
def collect_decl_method_defs(root, prefix, acc, def_owner, singleton_cref)
|
|
1347
|
+
self_decl = DeclarationWalk::Rules.self_anchored_decl_prefix(root.constant_path, def_owner)
|
|
1348
|
+
child = self_decl || Source::ConstantPath.declaration_prefix(prefix, root.constant_path)
|
|
1349
|
+
if child && root.body
|
|
1350
|
+
# Under an unnameable cref a bare/`self::` header opens `#<singleton>::Name` —
|
|
1351
|
+
# ownerless; nameable headers re-anchor at a real cref.
|
|
1352
|
+
child_cref = DeclarationWalk::Rules.unnameable_decl?(root, self_decl, singleton_cref)
|
|
1353
|
+
collect_class_method_defs(root.body, child_cref ? [] : child, acc,
|
|
1354
|
+
singleton_cref: child_cref)
|
|
1355
|
+
end
|
|
1356
|
+
acc
|
|
1357
|
+
end
|
|
1358
|
+
|
|
1359
|
+
def collect_singleton_method_defs(root, prefix, acc, def_owner, singleton_cref)
|
|
1360
|
+
collect_class_method_defs(root.expression, prefix, acc, def_owner: def_owner,
|
|
1361
|
+
singleton_cref: singleton_cref)
|
|
1362
|
+
if root.body
|
|
1363
|
+
# A `def` below `class <<` is a singleton method — this table collects
|
|
1364
|
+
# instance defs only, so the body walks ownerless.
|
|
1365
|
+
collect_class_method_defs(root.body, prefix, acc, def_owner: [],
|
|
1366
|
+
singleton_cref: true)
|
|
1367
|
+
end
|
|
1368
|
+
acc
|
|
1369
|
+
end
|
|
1370
|
+
|
|
1371
|
+
# Computes the definite-assignment set for one method, memoised per def node. The `memo` cycle-guards: a method
|
|
1372
|
+
# re-entered while its own summary is in progress contributes nothing (sound under-approximation), so mutual
|
|
1373
|
+
# recursion terminates.
|
|
1374
|
+
def method_definite_assigns(class_name, _method_name, def_node, defs, effects, memo, depth)
|
|
1375
|
+
return Set.new if def_node.body.nil?
|
|
1376
|
+
return memo[def_node] if memo.key?(def_node)
|
|
1377
|
+
return Set.new if depth >= SAME_CLASS_CALL_DEPTH_CAP
|
|
1378
|
+
|
|
1379
|
+
memo[def_node] = Set.new # in-progress sentinel (cycle guard)
|
|
1380
|
+
statements = top_level_statements(def_node.body)
|
|
1381
|
+
candidates = ivar_write_targets(def_node.body)
|
|
1382
|
+
# A transient `@x = nil` opener whose own method reassigns it later must still count `@x` as assigned for
|
|
1383
|
+
# callers, so the crediting is computed at the BUILD-time depth.
|
|
1384
|
+
resolver = MethodEffectResolver.new(self, class_name, defs, effects, memo, depth)
|
|
1385
|
+
assigns = Set.new
|
|
1386
|
+
candidates.each do |ivar|
|
|
1387
|
+
if suffix_definitely_assigns_with_resolver?(statements, 0, ivar, class_name, resolver, depth)
|
|
1388
|
+
assigns << ivar
|
|
1389
|
+
end
|
|
1390
|
+
end
|
|
1391
|
+
memo[def_node] = assigns
|
|
1392
|
+
end
|
|
1393
|
+
|
|
1394
|
+
# Every ivar this body assigns a non-nil value to ANYWHERE (the candidate set for the method's
|
|
1395
|
+
# definite-assignment scan).
|
|
1396
|
+
def ivar_write_targets(node, acc = Set.new)
|
|
1397
|
+
return acc unless node.is_a?(Prism::Node)
|
|
1398
|
+
|
|
1399
|
+
acc << node.name if node.is_a?(Prism::InstanceVariableWriteNode) && !nil_literal_value?(node.value)
|
|
1400
|
+
node.rigor_each_child { |c| ivar_write_targets(c, acc) }
|
|
1401
|
+
acc
|
|
1402
|
+
end
|
|
1403
|
+
|
|
1404
|
+
# Build-time variant of `suffix_definitely_assigns?` that resolves same-class calls through the lazy `resolver`
|
|
1405
|
+
# (which recurses into `method_definite_assigns` for not-yet-computed callees) rather than the finished flat
|
|
1406
|
+
# table.
|
|
1407
|
+
def suffix_definitely_assigns_with_resolver?(statements, from, target, class_name, resolver, depth)
|
|
1408
|
+
statements[from..].each do |stmt|
|
|
1409
|
+
outcome = statement_assignment_outcome(stmt, target, class_name, resolver, depth, nil)
|
|
1410
|
+
return true if outcome == :assigned
|
|
1411
|
+
return false if outcome == :terminates_unassigned
|
|
1412
|
+
end
|
|
1413
|
+
false
|
|
1414
|
+
end
|
|
1415
|
+
|
|
1416
|
+
# Adapts `effects.dig(class, method)` for build-time crediting: when the callee summary is not yet in the flat
|
|
1417
|
+
# table, compute it on demand (depth+1) via `method_definite_assigns`.
|
|
1418
|
+
class MethodEffectResolver
|
|
1419
|
+
def initialize(indexer, class_name, defs, effects, memo, depth)
|
|
1420
|
+
@indexer = indexer
|
|
1421
|
+
@class_name = class_name
|
|
1422
|
+
@defs = defs
|
|
1423
|
+
@effects = effects
|
|
1424
|
+
@memo = memo
|
|
1425
|
+
@depth = depth
|
|
1426
|
+
end
|
|
1427
|
+
|
|
1428
|
+
def dig(class_name, method_name)
|
|
1429
|
+
existing = @effects.dig(class_name, method_name)
|
|
1430
|
+
return existing if existing
|
|
1431
|
+
|
|
1432
|
+
def_node = @defs.dig(class_name, method_name)
|
|
1433
|
+
return nil if def_node.nil?
|
|
1434
|
+
|
|
1435
|
+
@indexer.send(:method_definite_assigns, class_name, method_name, def_node, @defs, @effects, @memo,
|
|
1436
|
+
@depth + 1)
|
|
1437
|
+
end
|
|
1438
|
+
end
|
|
1439
|
+
|
|
1440
|
+
# C2 — returns a Set of `object_id`s for transient `@x = nil` writes that a later statement in the same method
|
|
1441
|
+
# body *definitely* overwrites with a non-nil value on every completing path. Such a nil can never be the ivar's
|
|
1442
|
+
# value at method exit, so it must not contribute a `nil` constituent to the (flow-insensitive) class-ivar
|
|
1443
|
+
# union.
|
|
1444
|
+
#
|
|
1445
|
+
# Scope is deliberately narrow and post-domination-sound:
|
|
1446
|
+
# - only the top-level statement sequence of the body is
|
|
1447
|
+
# considered (no writes hidden inside loops / rescue / nested
|
|
1448
|
+
# conditionals count as the "definite" overwrite, except the
|
|
1449
|
+
# one structured `if/else` form below);
|
|
1450
|
+
# - the killing statement is either an unconditional
|
|
1451
|
+
# statement-level `@x = <non-nil>`, OR an `if/else` (with a
|
|
1452
|
+
# real `else`) where BOTH branches' final top-level write to
|
|
1453
|
+
# `@x` is non-nil. Both shapes overwrite `@x` on every path;
|
|
1454
|
+
# - only `@x = nil` literal writes are ever marked dead — a
|
|
1455
|
+
# non-nil transient is left untouched (it is already
|
|
1456
|
+
# precision-additive in the union).
|
|
1457
|
+
def dead_transient_nil_writes(body, class_name = nil, method_assign_effects = nil)
|
|
1458
|
+
statements = top_level_statements(body)
|
|
1459
|
+
return nil if statements.length < 2
|
|
1460
|
+
|
|
1461
|
+
dead = nil
|
|
1462
|
+
|
|
1463
|
+
statements.each_with_index do |stmt, i|
|
|
1464
|
+
next unless stmt.is_a?(Prism::InstanceVariableWriteNode) && nil_literal_value?(stmt.value)
|
|
1465
|
+
|
|
1466
|
+
# The opening `@x = nil` is dead when every completing path of the SUFFIX after it (normal end OR early
|
|
1467
|
+
# `return`, never a `raise`-terminated path) definitely reassigns `@x` non-nil. The suffix analysis credits
|
|
1468
|
+
# an unconditionally-called same-class method's own definite assignments via `method_assign_effects`.
|
|
1469
|
+
if suffix_definitely_assigns?(statements, i + 1, stmt.name, class_name, method_assign_effects)
|
|
1470
|
+
(dead ||= Set.new) << stmt.object_id
|
|
1471
|
+
end
|
|
1472
|
+
end
|
|
1473
|
+
|
|
1474
|
+
dead
|
|
1475
|
+
end
|
|
1476
|
+
|
|
1477
|
+
def top_level_statements(body)
|
|
1478
|
+
return [] if body.nil?
|
|
1479
|
+
return body.body if body.is_a?(Prism::StatementsNode)
|
|
1480
|
+
|
|
1481
|
+
[body]
|
|
1482
|
+
end
|
|
1483
|
+
|
|
1484
|
+
def nil_literal_value?(node)
|
|
1485
|
+
node.is_a?(Prism::NilNode)
|
|
1486
|
+
end
|
|
1487
|
+
|
|
1488
|
+
# True when, starting from `statements[from]`, EVERY path that completes the method (falls off the end OR hits
|
|
1489
|
+
# an early `return`) definitely assigns `target` a non-nil value first. Paths terminated by `raise` are not
|
|
1490
|
+
# completing paths and are ignored (they never observe the ivar at method exit). A path that can fall through
|
|
1491
|
+
# `statements` without assigning fails.
|
|
1492
|
+
def suffix_definitely_assigns?(statements, from, target, class_name, effects)
|
|
1493
|
+
statements[from..].each do |stmt|
|
|
1494
|
+
outcome = statement_assignment_outcome(stmt, target, class_name, effects, 0, nil)
|
|
1495
|
+
# The statement assigned on every continuing path -> the suffix is satisfied no matter what follows.
|
|
1496
|
+
return true if outcome == :assigned
|
|
1497
|
+
# The statement terminates control here (return/raise) and the value it carried did not assign on every path
|
|
1498
|
+
# -> some completing path reached exit without the assignment.
|
|
1499
|
+
return false if outcome == :terminates_unassigned
|
|
1500
|
+
# Otherwise (:falls_through_unassigned) keep scanning the remaining statements.
|
|
1501
|
+
end
|
|
1502
|
+
# Fell off the end with no definite assignment.
|
|
1503
|
+
false
|
|
1504
|
+
end
|
|
1505
|
+
|
|
1506
|
+
# Classifies a single statement's effect on `target`:
|
|
1507
|
+
# :assigned — every path through the statement
|
|
1508
|
+
# that continues OR returns assigns
|
|
1509
|
+
# `target` non-nil (suffix is done);
|
|
1510
|
+
# :terminates_unassigned — the statement ends the method
|
|
1511
|
+
# (return/raise) on some path
|
|
1512
|
+
# without a definite assignment, so
|
|
1513
|
+
# a completing path escaped;
|
|
1514
|
+
# :falls_through_unassigned — control may continue past it
|
|
1515
|
+
# without the assignment (keep
|
|
1516
|
+
# scanning the suffix).
|
|
1517
|
+
def statement_assignment_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1518
|
+
case stmt
|
|
1519
|
+
when Prism::IfNode, Prism::UnlessNode
|
|
1520
|
+
conditional_assignment_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1521
|
+
when Prism::CaseNode
|
|
1522
|
+
case_assignment_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1523
|
+
when Prism::ReturnNode
|
|
1524
|
+
:terminates_unassigned
|
|
1525
|
+
else
|
|
1526
|
+
outcome = simple_statement_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1527
|
+
# #3419 — a statement that does not assign but holds a `return` anywhere (`c and return`, a loop's, a
|
|
1528
|
+
# block's, a `begin`'s) may leave the method there with `target` unassigned.
|
|
1529
|
+
outcome == :falls_through_unassigned && holds_return?(stmt) ? :terminates_unassigned : outcome
|
|
1530
|
+
end
|
|
1531
|
+
end
|
|
1532
|
+
|
|
1533
|
+
# The outcome of a statement that is no conditional and no `return`: a write of `target` or an unconditional
|
|
1534
|
+
# same-class call that assigns it, and otherwise neutral — a bare `raise`/`fail` included, which terminates
|
|
1535
|
+
# without a completing path that observes the seed nil.
|
|
1536
|
+
def simple_statement_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1537
|
+
case stmt
|
|
1538
|
+
when Prism::InstanceVariableWriteNode
|
|
1539
|
+
stmt.name == target && !nil_literal_value?(stmt.value) ? :assigned : :falls_through_unassigned
|
|
1540
|
+
when Prism::CallNode
|
|
1541
|
+
assigns = unconditional_call_assigns?(stmt, target, class_name, effects, depth, visiting)
|
|
1542
|
+
assigns ? :assigned : :falls_through_unassigned
|
|
1543
|
+
else
|
|
1544
|
+
:falls_through_unassigned
|
|
1545
|
+
end
|
|
1546
|
+
end
|
|
1547
|
+
|
|
1548
|
+
# True when `node` holds a `return` that leaves the method: outside a nested def, class, module or lambda,
|
|
1549
|
+
# and outside the block of a {BLOCK_RETURN_BARRIERS} call, whose `return` leaves that block's own frame.
|
|
1550
|
+
def holds_return?(node)
|
|
1551
|
+
return false unless node.is_a?(Prism::Node)
|
|
1552
|
+
return true if node.is_a?(Prism::ReturnNode)
|
|
1553
|
+
return false if OBJECT_CODE_BARRIERS.any? { |klass| node.is_a?(klass) }
|
|
1554
|
+
|
|
1555
|
+
children = node.compact_child_nodes
|
|
1556
|
+
children -= [node.block] if node.is_a?(Prism::CallNode) && block_return_barrier?(node)
|
|
1557
|
+
children.any? { |c| holds_return?(c) }
|
|
1558
|
+
end
|
|
1559
|
+
|
|
1560
|
+
# True when `node` is a call whose literal block runs as a frame of its own (#3419): a lambda, a method body
|
|
1561
|
+
# (`define_method`, called directly or through `send` / `public_send` / `__send__` with the literal name), a
|
|
1562
|
+
# thread, fiber or ractor, or an exit or signal handler. A `proc { … }` or `Proc.new { … }` is no barrier.
|
|
1563
|
+
def block_return_barrier?(node)
|
|
1564
|
+
return false unless node.block.is_a?(Prism::BlockNode)
|
|
1565
|
+
|
|
1566
|
+
name = node.name
|
|
1567
|
+
if SEND_CALLS.include?(name)
|
|
1568
|
+
first = node.arguments&.arguments&.first
|
|
1569
|
+
name = first.unescaped.to_sym if first.is_a?(Prism::SymbolNode) || first.is_a?(Prism::StringNode)
|
|
1570
|
+
end
|
|
1571
|
+
receiver = node.receiver
|
|
1572
|
+
constant = receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)
|
|
1573
|
+
owner = receiver.name.to_s if constant
|
|
1574
|
+
BLOCK_RETURN_BARRIERS.fetch(nil).include?(name) || BLOCK_RETURN_BARRIERS.fetch(owner, []).include?(node.name)
|
|
1575
|
+
end
|
|
1576
|
+
|
|
1577
|
+
# True when a branch body (a StatementsNode / single node) definitely assigns `target` non-nil on every path
|
|
1578
|
+
# that completes the method through it, OR terminates every path by raise (vacuously safe — no completing path
|
|
1579
|
+
# observes the seed nil). Returns false if any path can complete/return without the assignment.
|
|
1580
|
+
# A branch body's (a StatementsNode / single node) outcome for `target`, read as a statement's is: `:assigned`
|
|
1581
|
+
# when it definitely assigns `target` non-nil on every path that completes the method through it, or ends every
|
|
1582
|
+
# path by raise (vacuously safe — no completing path observes the seed nil); `:terminates_unassigned` when a
|
|
1583
|
+
# path through it can return without the assignment; `:falls_through_unassigned` otherwise, an empty or absent
|
|
1584
|
+
# branch included.
|
|
1585
|
+
def branch_assignment_outcome(branch, target, class_name, effects, depth, visiting)
|
|
1586
|
+
stmts = top_level_statements(branch)
|
|
1587
|
+
return :falls_through_unassigned if stmts.empty?
|
|
1588
|
+
|
|
1589
|
+
stmts.each do |stmt|
|
|
1590
|
+
outcome = statement_assignment_outcome(stmt, target, class_name, effects, depth, visiting)
|
|
1591
|
+
return outcome unless outcome == :falls_through_unassigned
|
|
1592
|
+
end
|
|
1593
|
+
# Reached the end of the branch without a definite assignment; safe only if the branch's last statement always
|
|
1594
|
+
# raises (no completing path falls out of it).
|
|
1595
|
+
always_raises?(stmts.last) ? :assigned : :falls_through_unassigned
|
|
1596
|
+
end
|
|
1597
|
+
|
|
1598
|
+
# Joins the outcomes of the arms of a conditional or a `case`, a missing `else` arm falling through: `:assigned`
|
|
1599
|
+
# when every arm assigns, `:terminates_unassigned` when an arm may return without the assignment (#3419: `return
|
|
1600
|
+
# if c` is such an arm), and `:falls_through_unassigned` otherwise.
|
|
1601
|
+
def join_arm_outcomes(outcomes)
|
|
1602
|
+
return :assigned if outcomes.all?(:assigned)
|
|
1603
|
+
return :terminates_unassigned if outcomes.include?(:terminates_unassigned)
|
|
1604
|
+
|
|
1605
|
+
:falls_through_unassigned
|
|
1606
|
+
end
|
|
1607
|
+
|
|
1608
|
+
# `if`/`unless` is a definite assignment of `target` only when BOTH the then and else arms definitely assign (or
|
|
1609
|
+
# raise-out). A missing else arm means the fall-through path skips the assignment -> not definite. Modifier-form
|
|
1610
|
+
# `if`/`unless` (no else, single predicate'd statement) likewise. An `elsif` arm is read for a `return` alone:
|
|
1611
|
+
# it never counts as assigning.
|
|
1612
|
+
def conditional_assignment_outcome(node, target, class_name, effects, depth, visiting)
|
|
1613
|
+
else_branch = node.is_a?(Prism::IfNode) ? node.subsequent : node.else_clause
|
|
1614
|
+
then_outcome = branch_assignment_outcome(node.statements, target, class_name, effects, depth, visiting)
|
|
1615
|
+
else_outcome =
|
|
1616
|
+
case else_branch
|
|
1617
|
+
when Prism::ElseNode
|
|
1618
|
+
branch_assignment_outcome(else_branch.statements, target, class_name, effects, depth, visiting)
|
|
1619
|
+
when Prism::IfNode
|
|
1620
|
+
elsif_outcome = conditional_assignment_outcome(else_branch, target, class_name, effects, depth, visiting)
|
|
1621
|
+
elsif_outcome == :terminates_unassigned ? elsif_outcome : :falls_through_unassigned
|
|
1622
|
+
else
|
|
1623
|
+
:falls_through_unassigned
|
|
1624
|
+
end
|
|
1625
|
+
join_arm_outcomes([then_outcome, else_outcome])
|
|
1626
|
+
end
|
|
1627
|
+
|
|
1628
|
+
# `case` is a definite assignment only when there is a real `else` clause AND every `when`/`in` body plus the
|
|
1629
|
+
# else body definitely assigns (or raises-out). A missing else lets an unmatched subject fall through
|
|
1630
|
+
# unassigned.
|
|
1631
|
+
def case_assignment_outcome(node, target, class_name, effects, depth, visiting)
|
|
1632
|
+
else_clause = node.else_clause
|
|
1633
|
+
branches = node.conditions.map { |c| c.respond_to?(:statements) ? c.statements : nil }
|
|
1634
|
+
branches << else_clause&.statements
|
|
1635
|
+
outcomes = branches.map do |b|
|
|
1636
|
+
branch_assignment_outcome(b, target, class_name, effects, depth, visiting)
|
|
1637
|
+
end
|
|
1638
|
+
join_arm_outcomes(outcomes)
|
|
1639
|
+
end
|
|
1640
|
+
|
|
1641
|
+
# True when `node` (a single statement or its last statement) is an unconditional `raise`/`fail` call that
|
|
1642
|
+
# always terminates the path — used to treat raise-terminated branches as non-completing (they never observe the
|
|
1643
|
+
# seed nil).
|
|
1644
|
+
def always_raises?(node)
|
|
1645
|
+
node = top_level_statements(node).last if node.is_a?(Prism::StatementsNode)
|
|
1646
|
+
return false unless node.is_a?(Prism::CallNode)
|
|
1647
|
+
return false unless node.receiver.nil?
|
|
1648
|
+
|
|
1649
|
+
%i[raise fail].include?(node.name)
|
|
1650
|
+
end
|
|
1651
|
+
|
|
1652
|
+
# True when `call` is an unconditional, statement-level, implicit-self (or `self.`) call to a SAME-CLASS method
|
|
1653
|
+
# whose definite-assignment summary includes `target`. Calls through a block, on another receiver, or to an
|
|
1654
|
+
# unresolved name contribute nothing (the seed nil stays).
|
|
1655
|
+
def unconditional_call_assigns?(call, target, class_name, effects, depth, _visiting)
|
|
1656
|
+
return false if effects.nil? || class_name.nil?
|
|
1657
|
+
return false if depth >= SAME_CLASS_CALL_DEPTH_CAP
|
|
1658
|
+
return false unless call.is_a?(Prism::CallNode)
|
|
1659
|
+
return false unless call.block.nil?
|
|
1660
|
+
# Implicit self (`mask!(x)`) or explicit `self.mask!(x)` only.
|
|
1661
|
+
return false unless call.receiver.nil? || call.receiver.is_a?(Prism::SelfNode)
|
|
1662
|
+
|
|
1663
|
+
assigns = effects.dig(class_name, call.name)
|
|
1664
|
+
return false if assigns.nil?
|
|
1665
|
+
|
|
1666
|
+
assigns.include?(target)
|
|
1667
|
+
end
|
|
1668
|
+
|
|
1669
|
+
def record_ivar_write(node, scope, class_name, accumulator, guarded: false, declared: nil)
|
|
1670
|
+
rvalue_type = declared || accumulator[CONSTANT_IVAR_VALUES]&.type_of(node.value, scope) ||
|
|
1671
|
+
scope.type_of(node.value)
|
|
1672
|
+
|
|
1673
|
+
# `@x = nil unless @x` / `@y = false unless @y` — follow-up to the polarity-aware defensive-init guard fix
|
|
1674
|
+
# (defensive ivar-init with a nil / false rvalue). When the rvalue is itself a falsey
|
|
1675
|
+
# Constant, `union(rvalue, Constant[nil])` collapses (for `nil`) or doesn't widen the type's truthiness
|
|
1676
|
+
# profile (for `false`) — the predicate `unless @x` then folds to a single `Constant[nil]` / `Constant[false]`
|
|
1677
|
+
# and the `flow.always-truthy-condition` / `-always-falsey-` rule false-fires on the
|
|
1678
|
+
# no-op-but-documented-default idiom. Skip the seed contribution for this write — the sibling of
|
|
1679
|
+
# `record_ivar_or_write`'s no-truthy-rvalue skip for `@x ||= <falsey>` (#1175). Other writes to the same ivar
|
|
1680
|
+
# still contribute; the falsey-default write carries no useful precision the predicate hasn't already given
|
|
1681
|
+
# us. See tdiary-core HEAD `ee40c2b` `lib/tdiary/configuration.rb:157` for the worked site.
|
|
1682
|
+
return if guarded && falsey_constant?(rvalue_type)
|
|
1683
|
+
|
|
1684
|
+
rvalue_type = EmptySeed.erase(node.value, rvalue_type)
|
|
1685
|
+
|
|
1686
|
+
rvalue_type = Type::Combinator.union(rvalue_type, Type::Combinator.constant_of(nil)) if guarded
|
|
1687
|
+
accumulate_ivar_type(accumulator, class_name, node.name, rvalue_type)
|
|
1688
|
+
end
|
|
1689
|
+
|
|
1690
|
+
# #1175 — compound writes. ADR-58 § WD5 deferred seeding `||=` with a reopen clause ("a corpus
|
|
1691
|
+
# surfaces a memo-read shape the `union(v, nil)` seed provably improves"); Rigor's own
|
|
1692
|
+
# `unit_scan.rb` is that shape: `@dispatch_top_level ||= true` went unrecorded, so the ivar
|
|
1693
|
+
# kept `Constant[false]` and `unless @dispatch_top_level` folded always-falsey.
|
|
1694
|
+
def record_compound_ivar_write(node, scope, class_name, accumulator)
|
|
1695
|
+
case node
|
|
1696
|
+
when Prism::InstanceVariableOrWriteNode
|
|
1697
|
+
record_ivar_or_write(node, scope, class_name, accumulator)
|
|
1698
|
+
when Prism::InstanceVariableAndWriteNode
|
|
1699
|
+
record_ivar_and_write(node, scope, class_name, accumulator)
|
|
1700
|
+
when Prism::InstanceVariableOperatorWriteNode
|
|
1701
|
+
record_ivar_operator_write(node, scope, class_name, accumulator)
|
|
1702
|
+
end
|
|
1703
|
+
end
|
|
1704
|
+
|
|
1705
|
+
# `@x ||= v` — the memo idiom. The stored value is the old truthy value or `v`, so the rvalue
|
|
1706
|
+
# alone would be an honest contribution; the `nil` member stands in for the read-before-write
|
|
1707
|
+
# state — a `||=`-only ivar is `nil` until the first call runs the write. (That same union is
|
|
1708
|
+
# what the `guarded` flag adds to a plain write, and for the same reason: the flow-insensitive
|
|
1709
|
+
# seed has to know the predicate does not fold.) An rvalue with no truthy part is skipped outright
|
|
1710
|
+
# — `@x ||= false` can only leave `@x` falsey, the same "no useful precision" call the guarded
|
|
1711
|
+
# `@x = nil unless @x` skip makes, and `@x ||= raise "unset"` stores nothing at all. Seeded as
|
|
1712
|
+
# `nil`, the guard bound its own ivar, and `ExpressionTyper::Writes.compound_write_value` read
|
|
1713
|
+
# `truthy(nil) | bot` — an instance method that sig-gen declared `-> bot`.
|
|
1714
|
+
def record_ivar_or_write(node, scope, class_name, accumulator)
|
|
1715
|
+
rvalue_type = EmptySeed.erase(node.value, scope.type_of(node.value))
|
|
1716
|
+
return if Narrowing.narrow_truthy(rvalue_type).is_a?(Type::Bot)
|
|
1717
|
+
|
|
1718
|
+
accumulate_ivar_type(accumulator, class_name, node.name,
|
|
1719
|
+
Type::Combinator.union(rvalue_type, Type::Combinator.constant_of(nil)))
|
|
1720
|
+
end
|
|
1721
|
+
|
|
1722
|
+
# `@x &&= v` writes only when `@x` already holds a truthy value — the ivar was made truthy by an
|
|
1723
|
+
# earlier write, so the rvalue is the contribution; no `nil` member (unlike `||=`, the write
|
|
1724
|
+
# cannot be the first thing to give the ivar a value, and a spurious nil here would fire
|
|
1725
|
+
# possible-nil at reads the pre-existing writes already typed).
|
|
1726
|
+
#
|
|
1727
|
+
# For the same reason the contribution counts only beside a write that can give the ivar a value,
|
|
1728
|
+
# so it waits under {AND_WRITE_CONTRIBUTIONS} until {.merge_held_ivar_writes!} sees the whole class:
|
|
1729
|
+
# that write may come later in source order. An ivar only `&&=` writes stays unseeded, the unbound
|
|
1730
|
+
# target `ExpressionTyper::Writes.compound_write_value` reads as `Dynamic[top]`. Seeded as the rvalue, the
|
|
1731
|
+
# `&&=` bound itself, and `if (@x &&= 1)` folded always-truthy on an ivar that is `nil` at runtime.
|
|
1732
|
+
def record_ivar_and_write(node, scope, class_name, accumulator)
|
|
1733
|
+
pending = (accumulator[AND_WRITE_CONTRIBUTIONS] ||= {})
|
|
1734
|
+
accumulate_ivar_type(pending, class_name, node.name, scope.type_of(node.value))
|
|
1735
|
+
end
|
|
1736
|
+
|
|
1737
|
+
# `@x op= v` stores `@x op v`, so its contribution is the operator dispatched on what the class's
|
|
1738
|
+
# other writes store. Those writes sit in methods Ruby may call in any order, so the write waits
|
|
1739
|
+
# under {OPERATOR_WRITE_CONTRIBUTIONS} until {.merge_held_ivar_writes!} has them all. Dispatched on
|
|
1740
|
+
# the seed as the walk had it so far, a `+=` written above `@x &&= 1.5` never saw the `1.5`, `Float`
|
|
1741
|
+
# fell out of the seed, and `x == 2.5` folded always-falsey on a program that reaches it.
|
|
1742
|
+
def record_ivar_operator_write(node, scope, class_name, accumulator)
|
|
1743
|
+
pending = ((accumulator[OPERATOR_WRITE_CONTRIBUTIONS] ||= {})[class_name] ||= {})
|
|
1744
|
+
(pending[node.name] ||= []) << [node.binary_operator, scope.type_of(node.value), scope.environment]
|
|
1745
|
+
end
|
|
1746
|
+
|
|
1747
|
+
# Folds the held `&&=` and `op=` writes into the seed once the walk has seen every write of the class.
|
|
1748
|
+
# An ivar only `op=` writes is seeded first, from the widened rvalues: the receiver would read as `nil`,
|
|
1749
|
+
# and `nil + v` raises at runtime, so the seed is unconstrained there and the rvalue is the right answer
|
|
1750
|
+
# for the dominant `+=` / `-=` / `|=` families. That seed counts as a write for the `&&=` merge, and the
|
|
1751
|
+
# `op=` results then join on top of the complete seed.
|
|
1752
|
+
def merge_held_ivar_writes!(accumulator)
|
|
1753
|
+
operator_writes = accumulator.delete(OPERATOR_WRITE_CONTRIBUTIONS) || {}
|
|
1754
|
+
seed_operator_only_ivars!(accumulator, operator_writes)
|
|
1755
|
+
merge_ivar_and_writes!(accumulator)
|
|
1756
|
+
merge_ivar_operator_writes!(accumulator, operator_writes)
|
|
1757
|
+
end
|
|
1758
|
+
|
|
1759
|
+
def seed_operator_only_ivars!(accumulator, operator_writes)
|
|
1760
|
+
operator_writes.each do |class_name, ivars|
|
|
1761
|
+
ivars.each do |ivar_name, writes|
|
|
1762
|
+
next if accumulator.dig(class_name, ivar_name)
|
|
1763
|
+
|
|
1764
|
+
rvalues = writes.map { |(_operator, rvalue_type, _environment)| rvalue_type }
|
|
1765
|
+
accumulate_ivar_type(accumulator, class_name, ivar_name,
|
|
1766
|
+
Type::Combinator.widen_value_pinned(Type::Combinator.union(*rvalues)))
|
|
1767
|
+
end
|
|
1768
|
+
end
|
|
1769
|
+
end
|
|
1770
|
+
|
|
1771
|
+
# Folds the held `&&=` contributions into the seed of every ivar another write seeds, and drops the rest.
|
|
1772
|
+
def merge_ivar_and_writes!(accumulator)
|
|
1773
|
+
accumulator.delete(AND_WRITE_CONTRIBUTIONS)&.each do |class_name, ivars|
|
|
1774
|
+
seeded = accumulator[class_name]
|
|
1775
|
+
next if seeded.nil?
|
|
1776
|
+
|
|
1777
|
+
ivars.each do |ivar_name, type|
|
|
1778
|
+
seeded[ivar_name] = Type::Combinator.union(seeded[ivar_name], type) if seeded.key?(ivar_name)
|
|
1779
|
+
end
|
|
1780
|
+
end
|
|
1781
|
+
end
|
|
1782
|
+
|
|
1783
|
+
# The `op=` results join the complete seed. The methods run in any order, so a result is itself the
|
|
1784
|
+
# receiver of another held write: the dispatch repeats once per held write, which covers every chain one
|
|
1785
|
+
# call of each method can form, and stops early once a pass adds nothing. Not ADR-56's {BodyFixpoint}:
|
|
1786
|
+
# iterated to its fixed point, a lone counter's `@n += x` re-dispatched on its own `Dynamic[Integer | …]`
|
|
1787
|
+
# result and gained `Dynamic[top]`, and the widening pass that forces convergence dropped the `0` it
|
|
1788
|
+
# starts at — on nearly every counter ivar in the survey corpus. A chain that has to go on from a receiver
|
|
1789
|
+
# wider than {OPERATOR_CHAIN_UNION_CAP} floors to `Dynamic[top]` instead: each further pass dispatches
|
|
1790
|
+
# every held write on that union, and distinct tuple literals (`@a += [:s1]`, `@a += [:s2]`, …) grow it
|
|
1791
|
+
# by two members per write.
|
|
1792
|
+
def merge_ivar_operator_writes!(accumulator, operator_writes)
|
|
1793
|
+
operator_writes.each do |class_name, ivars|
|
|
1794
|
+
seeded = accumulator[class_name]
|
|
1795
|
+
ivars.each { |ivar_name, writes| seeded[ivar_name] = chain_operator_writes(seeded[ivar_name], writes) }
|
|
1796
|
+
end
|
|
1797
|
+
end
|
|
1798
|
+
|
|
1799
|
+
def chain_operator_writes(seed, writes)
|
|
1800
|
+
writes.size.times do |pass|
|
|
1801
|
+
if pass.positive? && union_arity(Type::Combinator.widen_value_pinned(seed)) > OPERATOR_CHAIN_UNION_CAP
|
|
1802
|
+
return Type::Combinator.untyped
|
|
1803
|
+
end
|
|
1804
|
+
|
|
1805
|
+
joined = Type::Combinator.union(seed, operator_write_results(seed, writes))
|
|
1806
|
+
break if joined == seed
|
|
1807
|
+
|
|
1808
|
+
seed = joined
|
|
1809
|
+
end
|
|
1810
|
+
seed
|
|
1811
|
+
end
|
|
1812
|
+
|
|
1813
|
+
def union_arity(type) = type.is_a?(Type::Union) ? type.members.size : 1
|
|
1814
|
+
|
|
1815
|
+
# A cost guard local to this chain, not ADR-41's `union_size` budget, which stays unwired: it floors
|
|
1816
|
+
# silently, as {BodyFixpoint} does. Forty is the low end of the pathology band ADR-41's Slice 2a names for
|
|
1817
|
+
# such a valve; the widest `op=`-written ivar seed in the survey corpus has seven members.
|
|
1818
|
+
OPERATOR_CHAIN_UNION_CAP = 40
|
|
1819
|
+
private_constant :OPERATOR_CHAIN_UNION_CAP
|
|
1820
|
+
|
|
1821
|
+
# The union of what each held `op=` write stores when `@x` holds `current`. The receiver is widened off
|
|
1822
|
+
# its value-pinned members first, or `Constant[0] + Constant[1]` would fold to a `Constant[1]` that pins
|
|
1823
|
+
# the ivar to one literal; the result is widened for the same reason.
|
|
1824
|
+
def operator_write_results(current, writes)
|
|
1825
|
+
receiver = Type::Combinator.widen_value_pinned(current)
|
|
1826
|
+
results = writes.map do |(operator, rvalue_type, environment)|
|
|
1827
|
+
dispatch = lambda do |type|
|
|
1828
|
+
MethodDispatcher.dispatch(receiver_type: type, method_name: operator, arg_types: [rvalue_type],
|
|
1829
|
+
environment: environment)
|
|
1830
|
+
end
|
|
1831
|
+
stored = dispatch.call(receiver) || partial_operator_result(receiver, rvalue_type, environment, dispatch)
|
|
1832
|
+
Type::Combinator.widen_value_pinned(stored)
|
|
1833
|
+
end
|
|
1834
|
+
Type::Combinator.union(*results)
|
|
1835
|
+
end
|
|
1836
|
+
|
|
1837
|
+
# A union the dispatch declines as a whole because one member does (`nil + 1`, `:none + 1`, a `Dynamic`, a
|
|
1838
|
+
# class the pre-pass cannot resolve). The members it does type are dispatched together; a union with none
|
|
1839
|
+
# falls back to the rvalue. Declined whole, a `reset` storing `nil` beside `@x = 0.5` and `@x += 1` sent
|
|
1840
|
+
# the `+=` to its rvalue, `Float` left the seed, and `l > 1.0` under `l.is_a?(Float)` folded always-falsey.
|
|
1841
|
+
# The typed members are not dispatched one by one: a union of tuples dispatches to one `Array[…]`, member by
|
|
1842
|
+
# member to a tuple per chain.
|
|
1843
|
+
#
|
|
1844
|
+
# A declining member whose class the environment knows lacks the operator, so the write raises and stores
|
|
1845
|
+
# nothing; joined for it, the rvalue put an `Integer` no run can store beside `0.5 | Float | nil`, and a
|
|
1846
|
+
# sibling `def level = @x` declared `-> Float?` reported `def.return-type-mismatch`. Any other declining
|
|
1847
|
+
# member stores what the pre-pass cannot see, and keeps the rvalue it fell back to before. "Lacks" is
|
|
1848
|
+
# read from the RBS: a source monkey patch of a core operator, or an operator the RBS omits, reads as
|
|
1849
|
+
# raising, and so does an unwritten ivar's `nil`, which the seed does not model (`nil ^ true` is `true`).
|
|
1850
|
+
def partial_operator_result(receiver, rvalue_type, environment, dispatch)
|
|
1851
|
+
return rvalue_type unless receiver.is_a?(Type::Union)
|
|
1852
|
+
|
|
1853
|
+
typed, declined = receiver.members.partition { |member| dispatch.call(member) }
|
|
1854
|
+
result = dispatch.call(Type::Combinator.union(*typed)) unless typed.empty?
|
|
1855
|
+
return rvalue_type if result.nil?
|
|
1856
|
+
return result if declined.all? { |member| lacks_operator?(member, environment) }
|
|
1857
|
+
|
|
1858
|
+
Type::Combinator.union(result, rvalue_type)
|
|
1859
|
+
end
|
|
1860
|
+
|
|
1861
|
+
def lacks_operator?(member, environment)
|
|
1862
|
+
member.is_a?(Type::Constant) || (member.is_a?(Type::Nominal) && environment.class_known?(member.class_name))
|
|
1863
|
+
end
|
|
1864
|
+
|
|
1865
|
+
# The accumulator keys {.record_ivar_and_write} and {.record_ivar_operator_write} hold their writes under
|
|
1866
|
+
# until the walk ends. Every other key is a qualified class name, a String, so a Symbol can never collide
|
|
1867
|
+
# with one.
|
|
1868
|
+
AND_WRITE_CONTRIBUTIONS = :and_write_contributions
|
|
1869
|
+
OPERATOR_WRITE_CONTRIBUTIONS = :operator_write_contributions
|
|
1870
|
+
private_constant :AND_WRITE_CONTRIBUTIONS, :OPERATOR_WRITE_CONTRIBUTIONS
|
|
1871
|
+
|
|
1872
|
+
# The same Symbol-key convention for two side tables the walk fills and {.build_class_ivar_index} removes
|
|
1873
|
+
# before anything reads class names: the ivars each class's `define_method` blocks write (in the
|
|
1874
|
+
# accumulator), and the class-body writes held out of `init_writes` until {.exempt_class_body_writes!}.
|
|
1875
|
+
DEFINE_METHOD_WRITES = :define_method_writes
|
|
1876
|
+
CLASS_BODY_WRITES = :class_body_writes
|
|
1877
|
+
# The classes a computed-name `instance_variable_set` may write any ivar of ({.record_reflective_ivar_write}).
|
|
1878
|
+
REFLECTIVE_OPAQUE_CLASSES = :reflective_opaque_classes
|
|
1879
|
+
# The census's {ConstantIvarValue}, which {.record_ivar_write} reads.
|
|
1880
|
+
CONSTANT_IVAR_VALUES = :constant_ivar_values
|
|
1881
|
+
# Issue #2931 — per class, the ivars a singleton-side `def` (`def self.x`) of it writes, which the census files
|
|
1882
|
+
# under the class too (#1958).
|
|
1883
|
+
SINGLETON_IVAR_WRITES = :singleton_ivar_writes
|
|
1884
|
+
private_constant :DEFINE_METHOD_WRITES, :CLASS_BODY_WRITES, :REFLECTIVE_OPAQUE_CLASSES, :CONSTANT_IVAR_VALUES,
|
|
1885
|
+
:SINGLETON_IVAR_WRITES
|
|
1886
|
+
|
|
1887
|
+
# Unions `type` into the class-ivar accumulator for `(class_name, ivar_name)`. Shared by the single-write and
|
|
1888
|
+
# multi-write (parallel-assignment) collectors.
|
|
1889
|
+
def accumulate_ivar_type(accumulator, class_name, ivar_name, type)
|
|
1890
|
+
accumulator[class_name] ||= {}
|
|
1891
|
+
existing = accumulator[class_name][ivar_name]
|
|
1892
|
+
accumulator[class_name][ivar_name] =
|
|
1893
|
+
existing ? Type::Combinator.union(existing, type) : type
|
|
1894
|
+
end
|
|
1895
|
+
|
|
1896
|
+
# N1 — records each `InstanceVariableTargetNode` of a `MultiWriteNode` (parallel / multiple assignment) into the
|
|
1897
|
+
# class-ivar union. The per-slot type comes from `MultiTargetBinder`, the decomposition locals use (issue
|
|
1898
|
+
# #1110): a `Tuple` right-hand side element-wise, an `Array[T]` per slot, a union distributed over its members,
|
|
1899
|
+
# a value with no implicit `to_ary` as `[rhs]`, and anything else — an unanalyzable RHS such as
|
|
1900
|
+
# `Open3.popen3(cmd)` — as the `Dynamic[top]` floor (NOT `nil`: a multi-write we cannot decompose means the
|
|
1901
|
+
# value is *unknown*, mirroring what a single write to an unknown RHS records). Nested and splat targets
|
|
1902
|
+
# (`(@a, @b), *@c = …`) follow the same rules.
|
|
1903
|
+
#
|
|
1904
|
+
# The seed records the binder's default reading and drops its ADR-101 marks, exactly what a single
|
|
1905
|
+
# `@x = xs.first` write records: an `Array[T]` fixed slot seeds `T`, not `T | nil`. The table has no mark
|
|
1906
|
+
# channel, and a `T | nil` seed is not gated everywhere a declaration-sourced nil is — a sibling returning the
|
|
1907
|
+
# ivar against a declared `-> T` would fire `def.return-type-mismatch` on a correct program (ADR-5).
|
|
1908
|
+
#
|
|
1909
|
+
# The ADR-57 slot softening is the opposite case and is turned off (`soften_slots: false`): dropping the `nil`
|
|
1910
|
+
# of a present `X | nil` tuple slot, or a union member's bare `nil`, is honest for a local only because the
|
|
1911
|
+
# binder marks the name, and the seed drops the mark. Without it a sibling's `if @b` / `raise unless @b` would
|
|
1912
|
+
# fold on a `nil` that is reachable, so the seed keeps the genuine `X | nil` the pre-#1110 indexer kept.
|
|
1913
|
+
def record_multi_write_ivars(node, scope, class_name, accumulator)
|
|
1914
|
+
return unless node.is_a?(Prism::MultiWriteNode)
|
|
1915
|
+
|
|
1916
|
+
rhs_type = scope.type_of(node.value)
|
|
1917
|
+
ivars = MultiTargetBinder.bind_marked(node, rhs_type, scope: scope, soften_slots: false).ivars
|
|
1918
|
+
# #2499 — `@a, @b = {}, []` erases each empty seed as a single write does.
|
|
1919
|
+
values = EmptySeed.multi_write_values(node)
|
|
1920
|
+
ivars.each do |name, type|
|
|
1921
|
+
accumulate_ivar_type(accumulator, class_name, name, EmptySeed.erase(values[name], type))
|
|
1922
|
+
end
|
|
1923
|
+
end
|
|
1924
|
+
|
|
1925
|
+
# Issue #1217 — `rescue => @err` and `for @x in xs` (with `for *@x in`, `for @x, y in`) write the ivar through a
|
|
1926
|
+
# target node, not an `InstanceVariableWriteNode`, so the census missed them: a sibling method read the
|
|
1927
|
+
# constructor's `@err = nil` alone and folded `if @err` always-falsey. A rescue reference contributes the
|
|
1928
|
+
# exception instance type the clause names; a `for` index the `Dynamic[top]` floor, since the census does not
|
|
1929
|
+
# type the element, and an unknown value is honest where a missing one is not.
|
|
1930
|
+
def record_binding_target_ivars(node, scope, class_name, accumulator)
|
|
1931
|
+
names = BindingTargets.names(node, Prism::InstanceVariableTargetNode)
|
|
1932
|
+
return if names.empty?
|
|
1933
|
+
|
|
1934
|
+
type = BindingTargets.value_type(node, scope)
|
|
1935
|
+
names.each { |name| accumulate_ivar_type(accumulator, class_name, name, type) }
|
|
1936
|
+
end
|
|
1937
|
+
|
|
1938
|
+
# The names a `rescue =>` reference or a `for` index binds through a target of `target_class`, looking through
|
|
1939
|
+
# a splat and a multi-target index.
|
|
1940
|
+
module BindingTargets
|
|
1941
|
+
EMPTY = [].freeze
|
|
1942
|
+
private_constant :EMPTY
|
|
1943
|
+
|
|
1944
|
+
module_function
|
|
1945
|
+
|
|
1946
|
+
def names(node, target_class)
|
|
1947
|
+
root =
|
|
1948
|
+
case node
|
|
1949
|
+
when Prism::RescueNode then node.reference
|
|
1950
|
+
when Prism::ForNode then node.index
|
|
1951
|
+
end
|
|
1952
|
+
return EMPTY if root.nil?
|
|
1953
|
+
|
|
1954
|
+
collect(root, target_class, [])
|
|
1955
|
+
end
|
|
1956
|
+
|
|
1957
|
+
# What an ivar target is written with: a rescue reference's exception instance, as the in-method binding
|
|
1958
|
+
# types it ({ErrorInfo.rescue_exception_type}), a `for` index's `Dynamic[top]` floor.
|
|
1959
|
+
def value_type(node, scope)
|
|
1960
|
+
return Type::Combinator.untyped unless node.is_a?(Prism::RescueNode)
|
|
1961
|
+
|
|
1962
|
+
ErrorInfo.rescue_exception_type(node) { |exception_node| scope.type_of(exception_node) }
|
|
1963
|
+
end
|
|
1964
|
+
|
|
1965
|
+
def collect(target, target_class, acc)
|
|
1966
|
+
case target
|
|
1967
|
+
when target_class then acc << target.name
|
|
1968
|
+
when Prism::SplatNode then collect(target.expression, target_class, acc) if target.expression
|
|
1969
|
+
when Prism::MultiTargetNode
|
|
1970
|
+
[*target.lefts, target.rest, *target.rights].compact.each { |t| collect(t, target_class, acc) }
|
|
1971
|
+
end
|
|
1972
|
+
acc
|
|
1973
|
+
end
|
|
1974
|
+
end
|
|
1975
|
+
|
|
1976
|
+
def falsey_constant?(type)
|
|
1977
|
+
type.is_a?(Type::Constant) && (type.value.nil? || type.value == false)
|
|
1978
|
+
end
|
|
1979
|
+
|
|
1980
|
+
def foreign_def?(def_node, qualified_prefix, in_singleton_class)
|
|
1981
|
+
ScopeIndexer.def_side(def_node, qualified_prefix, in_singleton_class).nil?
|
|
1982
|
+
end
|
|
1983
|
+
|
|
1984
|
+
# The subset of `ScopeIndexer::ATTR_MACROS` that defines a writer, which the class-ivar pass counts as a write
|
|
1985
|
+
# ({.record_attr_writer_ivars}).
|
|
1986
|
+
ATTR_WRITER_MACROS = %i[attr_writer attr_accessor].freeze
|
|
1987
|
+
|
|
1988
|
+
private_class_method :instance_side_writes, :exempt_class_body_writes!, :contribute_read_before_write_nil!,
|
|
1989
|
+
:record_aliased_ivar_mutations!, :returned_ivars_by_method, :returned_ivars,
|
|
1990
|
+
:collect_explicit_return_ivars, :tail_value_nodes, :tail_value_children,
|
|
1991
|
+
:gather_aliased_mutations, :escaped_ivars_for_receiver, :widen_mutated_ivar_entries!,
|
|
1992
|
+
:widen_type_for_observed_mutators, :widen_member_for_observed_mutators,
|
|
1993
|
+
:widen_constant_for_observed_mutators, :widen_member_for_lookup_mutators,
|
|
1994
|
+
:walk_class_ivars, :walk_ivars_leaf?, :walk_ivars_meta_new?, :walk_ivars_meta_call?,
|
|
1995
|
+
:walk_ivars_eval_call?, :walk_singleton_class_ivars, :walk_ivars_declaration,
|
|
1996
|
+
:collect_def_ivar_writes, :collect_block_ivar_writes, :collect_initializer_block_ivars,
|
|
1997
|
+
:record_attr_writer_ivars, :collect_define_method_block_ivars,
|
|
1998
|
+
:collect_receiver_writer_ivars, :define_method_written_ivars, :block_initializer?,
|
|
1999
|
+
:collect_read_before_write_evidence, :initializer_own_nil_reads, :runs_object_code?,
|
|
2000
|
+
:additional_initializer?,
|
|
2001
|
+
:class_matches_constraint?, :collect_class_body_ivar_writes,
|
|
2002
|
+
:collect_define_method_argument_writes, :define_method_block_call?,
|
|
2003
|
+
:method_body_block_call?, :detect_read_before_write, :detect_multi_write_target_writes,
|
|
2004
|
+
:each_census_child, :gather_ivar_writes, :record_reflective_ivar_write,
|
|
2005
|
+
:make_reflective_classes_gradual!, :record_ivar_mutator_call, :ivar_census_mutator?,
|
|
2006
|
+
:mutation_target, :walk_conditional_ivar_writes, :then_body_guarded_ivars,
|
|
2007
|
+
:collect_truthy_test_ivars, :collect_defined_test_ivars, :collect_nil_test_ivars,
|
|
2008
|
+
:build_method_assign_effects, :collect_class_method_defs, :record_collected_method_def,
|
|
2009
|
+
:collect_call_method_defs?, :collect_meta_new_method_defs?,
|
|
2010
|
+
:collect_anonymous_meta_defs?, :collect_eval_method_defs?, :collect_decl_method_defs,
|
|
2011
|
+
:collect_singleton_method_defs, :method_definite_assigns, :ivar_write_targets,
|
|
2012
|
+
:suffix_definitely_assigns_with_resolver?, :dead_transient_nil_writes,
|
|
2013
|
+
:top_level_statements, :nil_literal_value?, :suffix_definitely_assigns?,
|
|
2014
|
+
:statement_assignment_outcome, :simple_statement_outcome, :holds_return?,
|
|
2015
|
+
:block_return_barrier?,
|
|
2016
|
+
:branch_assignment_outcome, :join_arm_outcomes,
|
|
2017
|
+
:conditional_assignment_outcome, :case_assignment_outcome, :always_raises?,
|
|
2018
|
+
:unconditional_call_assigns?, :record_ivar_write, :record_compound_ivar_write,
|
|
2019
|
+
:record_ivar_or_write, :record_ivar_and_write, :record_ivar_operator_write,
|
|
2020
|
+
:merge_held_ivar_writes!, :seed_operator_only_ivars!, :merge_ivar_and_writes!,
|
|
2021
|
+
:merge_ivar_operator_writes!, :chain_operator_writes, :union_arity,
|
|
2022
|
+
:operator_write_results, :partial_operator_result, :lacks_operator?,
|
|
2023
|
+
:accumulate_ivar_type, :record_multi_write_ivars, :record_binding_target_ivars,
|
|
2024
|
+
:falsey_constant?, :foreign_def?
|
|
2025
|
+
end
|
|
2026
|
+
end
|
|
2027
|
+
end
|
|
2028
|
+
end
|