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
|
@@ -11,9 +11,13 @@ require_relative "../analysis/fact_store"
|
|
|
11
11
|
require_relative "../builtins/regex_refinement"
|
|
12
12
|
require_relative "guard_rebinding"
|
|
13
13
|
require_relative "intervening_ivar_reset"
|
|
14
|
-
require_relative "key_presence_guard"
|
|
15
14
|
require_relative "last_line"
|
|
16
15
|
require_relative "match_rebinding"
|
|
16
|
+
require_relative "narrowing/class_dispatch"
|
|
17
|
+
require_relative "narrowing/complements"
|
|
18
|
+
require_relative "narrowing/case_literals"
|
|
19
|
+
require_relative "narrowing/presence_predicates"
|
|
20
|
+
require_relative "narrowing/regex_match"
|
|
17
21
|
require_relative "operand_effects"
|
|
18
22
|
require_relative "optimistic_origin"
|
|
19
23
|
require_relative "project_method_ownership"
|
|
@@ -48,17 +52,10 @@ module Rigor
|
|
|
48
52
|
# docs/type-specification/control-flow-analysis.md for the binding contract.
|
|
49
53
|
# rubocop:disable-next Metrics/ModuleLength
|
|
50
54
|
module Narrowing
|
|
51
|
-
TRUSTED_EQUALITY_LITERAL_CLASSES = [String, Symbol, Integer, TrueClass, FalseClass, NilClass].freeze
|
|
52
|
-
SINGLETON_LITERAL_CLASSES = [TrueClass, FalseClass, NilClass].freeze
|
|
53
55
|
# `scope` is the analysed file's seeded scope where the caller has one, and nil otherwise (the
|
|
54
56
|
# Fact-shaped and RSpec-matcher entries hold an environment but no scope). Only the #898 singleton
|
|
55
57
|
# path reads it, and only to withhold a `Bot`, so a nil scope is the pre-#898 answer exactly.
|
|
56
58
|
ClassNarrowingContext = Data.define(:exact, :polarity, :environment, :scope)
|
|
57
|
-
# A recognised `=~` regex pattern operand: the pattern `source` string and whether it was
|
|
58
|
-
# compiled in extended (`//x`) mode. Extraction from a literal `RegularExpressionNode` and from
|
|
59
|
-
# a value-pinned `Constant[Regexp]` share this carrier so the participation walk and the
|
|
60
|
-
# extended-mode bail read one shape.
|
|
61
|
-
RegexMatchPattern = Data.define(:source, :extended)
|
|
62
59
|
# Issue #1429 — a predicate receiver whose binding an edge can narrow ({.receiver_slot}): a local, an instance
|
|
63
60
|
# variable or a global (`name` its Symbol), or a constant reference (`name` its {.constant_key}). `current` is
|
|
64
61
|
# the type the receiver reads before the predicate, a global's read type when the scope does not bind it.
|
|
@@ -72,8 +69,7 @@ module Rigor
|
|
|
72
69
|
# program replaces with another class's instance, so it counts with the literal carriers
|
|
73
70
|
# ({.disjoint_nominal_guard?}, `respond_to?`'s edge).
|
|
74
71
|
VALUE_PINNED_CLASSES = %w[NilClass TrueClass FalseClass].freeze
|
|
75
|
-
private_constant :
|
|
76
|
-
:RegexMatchPattern, :ReceiverSlot, :UNRECORDED_GUARD_GLOBALS, :VALUE_PINNED_CLASSES
|
|
72
|
+
private_constant :ClassNarrowingContext, :ReceiverSlot, :UNRECORDED_GUARD_GLOBALS, :VALUE_PINNED_CLASSES
|
|
77
73
|
|
|
78
74
|
# Issue #1017 — the tallest stack of conditionals a condition may carry and still contribute facts. A
|
|
79
75
|
# conditional guard analyses its predicate and both arms, so each level can triple the analysis; past this
|
|
@@ -93,9 +89,9 @@ module Rigor
|
|
|
93
89
|
def narrow_truthy(type)
|
|
94
90
|
case type
|
|
95
91
|
when Type::Constant
|
|
96
|
-
falsey_value?(type.value) ? Type::Combinator.bot : type
|
|
92
|
+
Complements.falsey_value?(type.value) ? Type::Combinator.bot : type
|
|
97
93
|
when Type::Nominal
|
|
98
|
-
falsey_nominal?(type) ? Type::Combinator.bot : type
|
|
94
|
+
Complements.falsey_nominal?(type) ? Type::Combinator.bot : type
|
|
99
95
|
when Type::Union
|
|
100
96
|
Type::Combinator.union(*type.members.map { |m| narrow_truthy(m) })
|
|
101
97
|
else
|
|
@@ -107,10 +103,10 @@ module Rigor
|
|
|
107
103
|
# that cannot inhabit a falsey value collapse to `Bot`.
|
|
108
104
|
def narrow_falsey(type)
|
|
109
105
|
case type
|
|
110
|
-
when Type::Constant then falsey_value?(type.value) ? type : Type::Combinator.bot
|
|
111
|
-
when Type::Nominal then falsey_nominal?(type) ? type : Type::Combinator.bot
|
|
106
|
+
when Type::Constant then Complements.falsey_value?(type.value) ? type : Type::Combinator.bot
|
|
107
|
+
when Type::Nominal then Complements.falsey_nominal?(type) ? type : Type::Combinator.bot
|
|
112
108
|
when Type::Union then Type::Combinator.union(*type.members.map { |m| narrow_falsey(m) })
|
|
113
|
-
else narrow_falsey_other(type)
|
|
109
|
+
else Complements.narrow_falsey_other(type)
|
|
114
110
|
end
|
|
115
111
|
end
|
|
116
112
|
|
|
@@ -123,7 +119,7 @@ module Rigor
|
|
|
123
119
|
when Type::Constant then type.value.nil? ? type : Type::Combinator.bot
|
|
124
120
|
when Type::Nominal then type.class_name == "NilClass" ? type : Type::Combinator.bot
|
|
125
121
|
when Type::Union then Type::Combinator.union(*type.members.map { |m| narrow_nil(m) })
|
|
126
|
-
else narrow_nil_other(type)
|
|
122
|
+
else Complements.narrow_nil_other(type)
|
|
127
123
|
end
|
|
128
124
|
end
|
|
129
125
|
|
|
@@ -205,12 +201,12 @@ module Rigor
|
|
|
205
201
|
# extracted from a mixed union such as `Integer | nil` without manufacturing a new
|
|
206
202
|
# positive domain from the comparison alone.
|
|
207
203
|
def narrow_equal(type, literal)
|
|
208
|
-
return type unless trusted_equality_literal?(literal)
|
|
204
|
+
return type unless Complements.trusted_equality_literal?(literal)
|
|
209
205
|
|
|
210
|
-
if singleton_literal?(literal)
|
|
211
|
-
narrow_singleton_equal(type, literal)
|
|
212
|
-
elsif finite_trusted_literal_domain?(type)
|
|
213
|
-
narrow_finite_equal(type, literal)
|
|
206
|
+
if Complements.singleton_literal?(literal)
|
|
207
|
+
Complements.narrow_singleton_equal(type, literal)
|
|
208
|
+
elsif Complements.finite_trusted_literal_domain?(type)
|
|
209
|
+
Complements.narrow_finite_equal(type, literal)
|
|
214
210
|
else
|
|
215
211
|
type
|
|
216
212
|
end
|
|
@@ -220,12 +216,12 @@ module Rigor
|
|
|
220
216
|
# from an already-known domain but do not create an unbounded difference type when the
|
|
221
217
|
# domain is broad or dynamic.
|
|
222
218
|
def narrow_not_equal(type, literal)
|
|
223
|
-
return type unless trusted_equality_literal?(literal)
|
|
219
|
+
return type unless Complements.trusted_equality_literal?(literal)
|
|
224
220
|
|
|
225
|
-
if singleton_literal?(literal)
|
|
226
|
-
narrow_singleton_not_equal(type, literal)
|
|
227
|
-
elsif finite_trusted_literal_domain?(type)
|
|
228
|
-
narrow_finite_not_equal(type, literal)
|
|
221
|
+
if Complements.singleton_literal?(literal)
|
|
222
|
+
Complements.narrow_singleton_not_equal(type, literal)
|
|
223
|
+
elsif Complements.finite_trusted_literal_domain?(type)
|
|
224
|
+
Complements.narrow_finite_not_equal(type, literal)
|
|
229
225
|
else
|
|
230
226
|
type
|
|
231
227
|
end
|
|
@@ -344,7 +340,7 @@ module Rigor
|
|
|
344
340
|
def narrow_class(type, class_name, exact: false, environment: Environment.default, scope: nil)
|
|
345
341
|
context = ClassNarrowingContext.new(exact: exact, polarity: :positive, environment: environment,
|
|
346
342
|
scope: scope)
|
|
347
|
-
narrow_class_dispatch(type, class_name, context)
|
|
343
|
+
ClassDispatch.narrow_class_dispatch(type, class_name, context)
|
|
348
344
|
end
|
|
349
345
|
|
|
350
346
|
# Issue #1429 — true when `type` holds a member whose `Bot` under `is_a?(class_name)` rests only on Rigor's
|
|
@@ -370,7 +366,7 @@ module Rigor
|
|
|
370
366
|
def narrow_not_class(type, class_name, exact: false, environment: Environment.default, scope: nil)
|
|
371
367
|
context = ClassNarrowingContext.new(exact: exact, polarity: :negative, environment: environment,
|
|
372
368
|
scope: scope)
|
|
373
|
-
narrow_class_dispatch(type, class_name, context)
|
|
369
|
+
ClassDispatch.narrow_class_dispatch(type, class_name, context)
|
|
374
370
|
end
|
|
375
371
|
|
|
376
372
|
# Negation pair for `assert_value is ~refinement` / `predicate-if-* … is ~refinement`
|
|
@@ -401,13 +397,13 @@ module Rigor
|
|
|
401
397
|
when Type::Difference
|
|
402
398
|
return current_type unless refinement_type.removed.is_a?(Type::Constant)
|
|
403
399
|
|
|
404
|
-
complement_difference(current_type, refinement_type)
|
|
400
|
+
Complements.complement_difference(current_type, refinement_type)
|
|
405
401
|
when Type::IntegerRange
|
|
406
|
-
complement_integer_range(current_type, refinement_type)
|
|
402
|
+
Complements.complement_integer_range(current_type, refinement_type)
|
|
407
403
|
when Type::Intersection
|
|
408
|
-
complement_intersection(current_type, refinement_type)
|
|
404
|
+
Complements.complement_intersection(current_type, refinement_type)
|
|
409
405
|
when Type::Refined
|
|
410
|
-
complement_refined(current_type, refinement_type)
|
|
406
|
+
Complements.complement_refined(current_type, refinement_type)
|
|
411
407
|
else
|
|
412
408
|
current_type
|
|
413
409
|
end
|
|
@@ -509,9 +505,10 @@ module Rigor
|
|
|
509
505
|
# MUST be evaluated. The subject is narrowed by the conjunction of every condition's
|
|
510
506
|
# falsey edge.
|
|
511
507
|
#
|
|
512
|
-
# The narrowing is best-effort: if the subject is not a
|
|
513
|
-
#
|
|
514
|
-
#
|
|
508
|
+
# The narrowing is best-effort: if the subject is not a slot a guard narrows
|
|
509
|
+
# ({.case_subject_slot}: a local, instance variable, global or constant read, or a write to a
|
|
510
|
+
# local or instance variable) or none of the condition shapes are recognised, both returned
|
|
511
|
+
# scopes equal the input scope. The catalogue mirrors {.case_equality_target_class}: static class/module
|
|
515
512
|
# constants narrow as `is_a?`; integer/float-endpoint ranges narrow to `Numeric`;
|
|
516
513
|
# string-endpoint ranges narrow to `String`; regexp literals narrow as
|
|
517
514
|
# {.regexp_case_equality_type} says; a `nil`, `true` or `false` literal narrows as its
|
|
@@ -546,6 +543,15 @@ module Rigor
|
|
|
546
543
|
[body_scope, falsey_scope]
|
|
547
544
|
end
|
|
548
545
|
|
|
546
|
+
# Issue #3445 — whether a `case`/`when` with no `else` has no "nothing matched" path: its subject reads `Bot` in
|
|
547
|
+
# `scope`, the falsey scope after the last clause. An inferred parameter's `Bot` proves nothing (ADR-67).
|
|
548
|
+
def exhausted_when_case?(node, scope)
|
|
549
|
+
return false unless node.is_a?(Prism::CaseNode) && node.else_clause.nil? && node.conditions.any?
|
|
550
|
+
|
|
551
|
+
slot = case_subject_slot(node.predicate, scope)
|
|
552
|
+
slot&.current.is_a?(Type::Bot) && !(%i[local ivar].include?(slot.kind) && scope.inferred_param?(slot.name))
|
|
553
|
+
end
|
|
554
|
+
|
|
549
555
|
CASE_SUBJECT_KINDS = %i[local ivar global constant].freeze
|
|
550
556
|
private_constant :CASE_SUBJECT_KINDS
|
|
551
557
|
|
|
@@ -600,7 +606,7 @@ module Rigor
|
|
|
600
606
|
end
|
|
601
607
|
|
|
602
608
|
# When every condition is a `RegularExpressionNode` literal, the body edge narrows the
|
|
603
|
-
# match-data globals as `analyse_regex_match_predicate`'s truthy edge does. With multiple
|
|
609
|
+
# match-data globals as `RegexMatch.analyse_regex_match_predicate`'s truthy edge does. With multiple
|
|
604
610
|
# regex conditions (`when /a/, /b/`) the body is reachable through any of them, so only
|
|
605
611
|
# `$~`/`$&` are safely non-nil; numbered groups whose presence differs per alternative stay
|
|
606
612
|
# `String | nil`. With no regex condition the entry scope passes through unchanged.
|
|
@@ -620,14 +626,14 @@ module Rigor
|
|
|
620
626
|
|
|
621
627
|
unconditional =
|
|
622
628
|
if regexes.size == 1
|
|
623
|
-
unconditional_capture_groups(regexes.first.unescaped)
|
|
629
|
+
RegexMatch.unconditional_capture_groups(regexes.first.unescaped)
|
|
624
630
|
else
|
|
625
631
|
Set.new
|
|
626
632
|
end
|
|
627
|
-
truthy, falsey = regex_match_predicate_scopes(scope, unconditional)
|
|
633
|
+
truthy, falsey = RegexMatch.regex_match_predicate_scopes(scope, unconditional)
|
|
628
634
|
return truthy if regexes.size == conditions.size
|
|
629
635
|
|
|
630
|
-
empty_truthy, empty_falsey = regex_match_predicate_scopes(scope, Set.new)
|
|
636
|
+
empty_truthy, empty_falsey = RegexMatch.regex_match_predicate_scopes(scope, Set.new)
|
|
631
637
|
failed = scope
|
|
632
638
|
paths = conditions.map do |condition|
|
|
633
639
|
kind = when_regex_condition_kind(condition, scope)
|
|
@@ -676,13 +682,13 @@ module Rigor
|
|
|
676
682
|
# and so is each numbered group {#yielded_capture_groups} vouches for; every other group is unbound, so it
|
|
677
683
|
# reads the RBS `String?` whatever an earlier match in the method bound it to.
|
|
678
684
|
def yielded_match_scope(scope, pattern)
|
|
679
|
-
regex_match_predicate_scopes(scope, yielded_capture_groups(pattern)).first
|
|
685
|
+
RegexMatch.regex_match_predicate_scopes(scope, yielded_capture_groups(pattern)).first
|
|
680
686
|
end
|
|
681
687
|
|
|
682
688
|
# The pattern sources {#yielded_capture_groups} reads groups from: literal characters, `.`, the anchors, the
|
|
683
689
|
# quantifiers, `|`, plain and `(?:…)` groups, the class escapes, `\n`, `\r` and `\t`, an escaped punctuation
|
|
684
690
|
# character, and a bracket class holding only those with no bracket inside it. The group walk
|
|
685
|
-
# ({
|
|
691
|
+
# ({RegexMatch.unconditional_capture_groups}) reads these right; it misreads sources outside them (#2472, #1476).
|
|
686
692
|
PLAIN_PATTERN_ATOM = %r{[A-Za-z0-9 _\-.,:;=<>!@%&~'"`/^$+*?|(){}#]|\\[wWdDsShHnrt]|\\[^A-Za-z0-9\s]}
|
|
687
693
|
PLAIN_PATTERN_CLASS =
|
|
688
694
|
%r{\[\^?(?:[A-Za-z0-9 _\-.,:;=<>!@%~'"`/^$+*?|(){}#]|\\[wWdDsShHnrt]|\\[^A-Za-z0-9\s])+\]}
|
|
@@ -705,7 +711,7 @@ module Rigor
|
|
|
705
711
|
return NO_GROUPS unless pattern.is_a?(Prism::RegularExpressionNode) && !pattern.extended?
|
|
706
712
|
return NO_GROUPS unless pattern.opening == "/"
|
|
707
713
|
|
|
708
|
-
plain_pattern_source?(pattern.content) ? unconditional_capture_groups(pattern.content) : NO_GROUPS
|
|
714
|
+
plain_pattern_source?(pattern.content) ? RegexMatch.unconditional_capture_groups(pattern.content) : NO_GROUPS
|
|
709
715
|
end
|
|
710
716
|
|
|
711
717
|
def plain_pattern_source?(source)
|
|
@@ -715,10 +721,10 @@ module Rigor
|
|
|
715
721
|
private_class_method :yielded_capture_groups, :plain_pattern_source?
|
|
716
722
|
|
|
717
723
|
# Issue #2443 — true when every member of `type` is a String (a project subclass included) or a `Dynamic` that
|
|
718
|
-
# is untyped or may hold only Strings, Symbols and nil ({
|
|
719
|
-
# block, so it counts neither way.
|
|
724
|
+
# is untyped or may hold only Strings, Symbols and nil ({RegexMatch.match_member_kind}). A `nil` member never runs
|
|
725
|
+
# the block, so it counts neither way.
|
|
720
726
|
def string_match_receiver?(type, scope)
|
|
721
|
-
kinds = match_member_kinds(type, scope) - [:nil]
|
|
727
|
+
kinds = RegexMatch.match_member_kinds(type, scope) - [:nil]
|
|
722
728
|
!kinds.empty? && kinds.all? { |kind| %i[string string_subclass dynamic].include?(kind) }
|
|
723
729
|
end
|
|
724
730
|
|
|
@@ -799,294 +805,6 @@ module Rigor
|
|
|
799
805
|
class << self
|
|
800
806
|
private
|
|
801
807
|
|
|
802
|
-
# Complement of `Difference[base, Constant[v]]` within `current_type`. Walks the
|
|
803
|
-
# current type's union members, keeps each member disjoint from `base` (those values
|
|
804
|
-
# were never in the refinement to begin with), and adds the removed-value `Constant[v]`
|
|
805
|
-
# exactly once when any current member covers it. Members that are fully contained in
|
|
806
|
-
# the refinement (i.e. inside `base` and NOT equal to the removed value) are dropped —
|
|
807
|
-
# they are exactly the values the negation excludes.
|
|
808
|
-
def complement_difference(current_type, difference)
|
|
809
|
-
base = difference.base
|
|
810
|
-
removed = difference.removed
|
|
811
|
-
parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
|
|
812
|
-
|
|
813
|
-
survivors = []
|
|
814
|
-
add_removed = false
|
|
815
|
-
parts.each do |part|
|
|
816
|
-
if base_disjoint?(base, part)
|
|
817
|
-
survivors << part
|
|
818
|
-
elsif part_covers_constant?(part, removed)
|
|
819
|
-
add_removed = true
|
|
820
|
-
end
|
|
821
|
-
end
|
|
822
|
-
survivors << removed if add_removed
|
|
823
|
-
|
|
824
|
-
return current_type if survivors.empty?
|
|
825
|
-
|
|
826
|
-
Type::Combinator.union(*survivors)
|
|
827
|
-
end
|
|
828
|
-
|
|
829
|
-
def base_disjoint?(base, part)
|
|
830
|
-
base.accepts(part, mode: :gradual).no?
|
|
831
|
-
end
|
|
832
|
-
|
|
833
|
-
def part_covers_constant?(part, constant)
|
|
834
|
-
result = part.accepts(constant, mode: :gradual)
|
|
835
|
-
result.yes? || result.maybe?
|
|
836
|
-
end
|
|
837
|
-
|
|
838
|
-
# Complement of an `IntegerRange[a, b]` within `current_type`. Splits the range
|
|
839
|
-
# complement into the two open halves `Integer[..a-1]` and `Integer[b+1..]` (skipping a
|
|
840
|
-
# half when its bound is infinity), then intersects each half with the integer-domain
|
|
841
|
-
# parts of `current_type`. Non-integer parts of a Union receiver (nil, String, …)
|
|
842
|
-
# survive unchanged.
|
|
843
|
-
def complement_integer_range(current_type, range)
|
|
844
|
-
halves = integer_range_complement_halves(range)
|
|
845
|
-
parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
|
|
846
|
-
|
|
847
|
-
survivors = []
|
|
848
|
-
parts.each do |part|
|
|
849
|
-
if integer_member?(part)
|
|
850
|
-
halves.each do |half|
|
|
851
|
-
meet = intersect_integer_part(part, half)
|
|
852
|
-
survivors << meet unless meet.nil? || meet.is_a?(Type::Bot)
|
|
853
|
-
end
|
|
854
|
-
else
|
|
855
|
-
survivors << part
|
|
856
|
-
end
|
|
857
|
-
end
|
|
858
|
-
|
|
859
|
-
return current_type if survivors.empty?
|
|
860
|
-
|
|
861
|
-
Type::Combinator.union(*survivors)
|
|
862
|
-
end
|
|
863
|
-
|
|
864
|
-
# Returns the two open halves of an IntegerRange's complement: the left half
|
|
865
|
-
# `Integer[..a-1]` (when `a` is finite) and the right half `Integer[b+1..]` (when `b` is finite).
|
|
866
|
-
# Universal ranges (both bounds infinite) yield an empty array — the complement is
|
|
867
|
-
# empty.
|
|
868
|
-
def integer_range_complement_halves(range)
|
|
869
|
-
halves = []
|
|
870
|
-
left_max = range.min
|
|
871
|
-
right_min = range.max
|
|
872
|
-
|
|
873
|
-
if left_max.is_a?(Integer)
|
|
874
|
-
halves << Type::Combinator.integer_range(Type::IntegerRange::NEG_INFINITY, left_max - 1)
|
|
875
|
-
end
|
|
876
|
-
if right_min.is_a?(Integer)
|
|
877
|
-
halves << Type::Combinator.integer_range(right_min + 1, Type::IntegerRange::POS_INFINITY)
|
|
878
|
-
end
|
|
879
|
-
halves
|
|
880
|
-
end
|
|
881
|
-
|
|
882
|
-
def integer_member?(part)
|
|
883
|
-
case part
|
|
884
|
-
when Type::Constant then part.value.is_a?(Integer)
|
|
885
|
-
when Type::IntegerRange then true
|
|
886
|
-
when Type::Nominal then part.class_name == "Integer"
|
|
887
|
-
else false
|
|
888
|
-
end
|
|
889
|
-
end
|
|
890
|
-
|
|
891
|
-
# Intersect an integer-domain part with a complement half-range. For a
|
|
892
|
-
# Nominal[Integer] receiver the meet is the half itself; for an existing IntegerRange
|
|
893
|
-
# the meet narrows both bounds; for a Constant[Integer] the meet is the constant when
|
|
894
|
-
# the half covers it, otherwise nil.
|
|
895
|
-
def intersect_integer_part(part, half)
|
|
896
|
-
case part
|
|
897
|
-
when Type::Nominal
|
|
898
|
-
half
|
|
899
|
-
when Type::IntegerRange
|
|
900
|
-
integer_range_meet(part, half)
|
|
901
|
-
when Type::Constant
|
|
902
|
-
half.covers?(part.value) ? part : nil
|
|
903
|
-
end
|
|
904
|
-
end
|
|
905
|
-
|
|
906
|
-
def integer_range_meet(left, right)
|
|
907
|
-
low = numeric_to_bound([integer_bound_value(left.min), integer_bound_value(right.min)].max)
|
|
908
|
-
high = numeric_to_bound([integer_bound_value(left.max), integer_bound_value(right.max)].min)
|
|
909
|
-
return nil if integer_range_disjoint?(low, high)
|
|
910
|
-
|
|
911
|
-
Type::Combinator.integer_range(low, high)
|
|
912
|
-
end
|
|
913
|
-
|
|
914
|
-
def integer_bound_value(bound)
|
|
915
|
-
return -Float::INFINITY if bound == Type::IntegerRange::NEG_INFINITY
|
|
916
|
-
return Float::INFINITY if bound == Type::IntegerRange::POS_INFINITY
|
|
917
|
-
|
|
918
|
-
bound
|
|
919
|
-
end
|
|
920
|
-
|
|
921
|
-
def numeric_to_bound(value)
|
|
922
|
-
return Type::IntegerRange::NEG_INFINITY if value == -Float::INFINITY
|
|
923
|
-
return Type::IntegerRange::POS_INFINITY if value == Float::INFINITY
|
|
924
|
-
|
|
925
|
-
value.to_i
|
|
926
|
-
end
|
|
927
|
-
|
|
928
|
-
def integer_range_disjoint?(low, high)
|
|
929
|
-
return false if low == Type::IntegerRange::NEG_INFINITY
|
|
930
|
-
return false if high == Type::IntegerRange::POS_INFINITY
|
|
931
|
-
|
|
932
|
-
low > high
|
|
933
|
-
end
|
|
934
|
-
|
|
935
|
-
# De Morgan: `D \ (M1 ∩ M2 ∩ …) = (D \ M1) ∪ (D \ M2) ∪ …`. Each member's complement is
|
|
936
|
-
# computed independently within `current_type` and the results are unioned. Members the
|
|
937
|
-
# algebra cannot complement contribute `current_type` itself, so the union widens to
|
|
938
|
-
# `current_type` overall — sound but imprecise.
|
|
939
|
-
def complement_intersection(current_type, intersection)
|
|
940
|
-
per_member = intersection.members.map do |member|
|
|
941
|
-
Narrowing.narrow_not_refinement(current_type, member)
|
|
942
|
-
end
|
|
943
|
-
Type::Combinator.union(*per_member)
|
|
944
|
-
end
|
|
945
|
-
|
|
946
|
-
# v0.0.7 — complement of a `Refined[base, predicate]` refinement within `current_type`.
|
|
947
|
-
# Walks the current type's union members; parts that are disjoint from the refinement's
|
|
948
|
-
# base survive unchanged; parts equal to the refinement itself drop entirely (they were
|
|
949
|
-
# exactly the negated subset); parts that overlap the base contribute `Difference[part,
|
|
950
|
-
# refined]` so downstream narrowing knows the refinement subset is excluded.
|
|
951
|
-
#
|
|
952
|
-
# v0.0.9 — when the predicate has a registered complement (see
|
|
953
|
-
# {Type::Refined::COMPLEMENT_PAIRS}) and the part is exactly the refinement's base, the
|
|
954
|
-
# narrowing returns `Refined[base, complement_predicate]` instead of
|
|
955
|
-
# `Difference[base, refined]`. This is the `~T` symmetry the spec promises:
|
|
956
|
-
# `~lowercase-string` narrows `String` to `non-lowercase-string` rather than
|
|
957
|
-
# `Difference[String, lowercase-string]`.
|
|
958
|
-
#
|
|
959
|
-
# Predicates without a registered complement still fall back to the imprecise but sound
|
|
960
|
-
# `Difference[part, refined]` carrier so behaviour is unchanged for untouched call
|
|
961
|
-
# sites.
|
|
962
|
-
def complement_refined(current_type, refined)
|
|
963
|
-
complement = registered_complement_for(refined)
|
|
964
|
-
parts = current_type.is_a?(Type::Union) ? current_type.members : [current_type]
|
|
965
|
-
survivors = parts.filter_map { |part| complement_refined_part(part, refined, complement) }
|
|
966
|
-
return current_type if survivors.empty?
|
|
967
|
-
|
|
968
|
-
Type::Combinator.union(*survivors)
|
|
969
|
-
end
|
|
970
|
-
|
|
971
|
-
def complement_refined_part(part, refined, complement)
|
|
972
|
-
return nil if part == refined
|
|
973
|
-
return part if base_disjoint?(refined.base, part)
|
|
974
|
-
return complement if complement && part == refined.base
|
|
975
|
-
|
|
976
|
-
Type::Combinator.difference(part, refined)
|
|
977
|
-
end
|
|
978
|
-
|
|
979
|
-
def registered_complement_for(refined)
|
|
980
|
-
complement_id = refined.complement_predicate_id
|
|
981
|
-
return nil if complement_id.nil?
|
|
982
|
-
|
|
983
|
-
Type::Combinator.refined(refined.base, complement_id)
|
|
984
|
-
end
|
|
985
|
-
|
|
986
|
-
def falsey_value?(value)
|
|
987
|
-
value.nil? || value == false
|
|
988
|
-
end
|
|
989
|
-
|
|
990
|
-
def falsey_nominal?(nominal)
|
|
991
|
-
%w[NilClass FalseClass].include?(nominal.class_name)
|
|
992
|
-
end
|
|
993
|
-
|
|
994
|
-
# Carriers that the {.narrow_falsey} fast path does not handle by structural
|
|
995
|
-
# inspection. Singleton/Tuple/HashShape inhabit truthy values, so their falsey fragment
|
|
996
|
-
# is empty; everything else (Top, Dynamic, Bot, and any future carrier) stays
|
|
997
|
-
# conservative and is returned unchanged.
|
|
998
|
-
def narrow_falsey_other(type)
|
|
999
|
-
case type
|
|
1000
|
-
when Type::Singleton, Type::Tuple, Type::HashShape then Type::Combinator.bot
|
|
1001
|
-
else type
|
|
1002
|
-
end
|
|
1003
|
-
end
|
|
1004
|
-
|
|
1005
|
-
# Carriers that the {.narrow_nil} fast path does not handle by structural inspection.
|
|
1006
|
-
# Top/Dynamic narrow to `Constant[nil]` so dispatch resolves through `NilClass`; Bot is
|
|
1007
|
-
# its own nil fragment; the remaining carriers (Singleton, Tuple, HashShape, and any
|
|
1008
|
-
# future carrier whose inhabitants exclude nil) collapse to `Bot`.
|
|
1009
|
-
def narrow_nil_other(type)
|
|
1010
|
-
case type
|
|
1011
|
-
when Type::Dynamic, Type::Top then Type::Combinator.constant_of(nil)
|
|
1012
|
-
when Type::Bot then type
|
|
1013
|
-
else Type::Combinator.bot
|
|
1014
|
-
end
|
|
1015
|
-
end
|
|
1016
|
-
|
|
1017
|
-
def trusted_equality_literal?(literal)
|
|
1018
|
-
literal.is_a?(Type::Constant) &&
|
|
1019
|
-
TRUSTED_EQUALITY_LITERAL_CLASSES.include?(literal.value.class)
|
|
1020
|
-
end
|
|
1021
|
-
|
|
1022
|
-
def singleton_literal?(literal)
|
|
1023
|
-
SINGLETON_LITERAL_CLASSES.include?(literal.value.class)
|
|
1024
|
-
end
|
|
1025
|
-
|
|
1026
|
-
def finite_trusted_literal_domain?(type)
|
|
1027
|
-
case type
|
|
1028
|
-
when Type::Bot then true
|
|
1029
|
-
when Type::Constant then trusted_equality_literal?(type)
|
|
1030
|
-
when Type::Union then type.members.all? { |member| finite_trusted_literal_domain?(member) }
|
|
1031
|
-
else false
|
|
1032
|
-
end
|
|
1033
|
-
end
|
|
1034
|
-
|
|
1035
|
-
def narrow_finite_equal(type, literal)
|
|
1036
|
-
case type
|
|
1037
|
-
when Type::Bot then type
|
|
1038
|
-
when Type::Constant then type == literal ? type : Type::Combinator.bot
|
|
1039
|
-
when Type::Union
|
|
1040
|
-
Type::Combinator.union(*type.members.map { |member| narrow_finite_equal(member, literal) })
|
|
1041
|
-
else Type::Combinator.bot
|
|
1042
|
-
end
|
|
1043
|
-
end
|
|
1044
|
-
|
|
1045
|
-
def narrow_finite_not_equal(type, literal)
|
|
1046
|
-
case type
|
|
1047
|
-
when Type::Constant then type == literal ? Type::Combinator.bot : type
|
|
1048
|
-
when Type::Union
|
|
1049
|
-
Type::Combinator.union(*type.members.map { |member| narrow_finite_not_equal(member, literal) })
|
|
1050
|
-
else type
|
|
1051
|
-
end
|
|
1052
|
-
end
|
|
1053
|
-
|
|
1054
|
-
def narrow_singleton_equal(type, literal)
|
|
1055
|
-
case type
|
|
1056
|
-
when Type::Constant then type == literal ? type : Type::Combinator.bot
|
|
1057
|
-
when Type::Nominal then singleton_nominal_matches?(type, literal) ? type : Type::Combinator.bot
|
|
1058
|
-
when Type::Union
|
|
1059
|
-
Type::Combinator.union(*type.members.map { |member| narrow_singleton_equal(member, literal) })
|
|
1060
|
-
else narrow_singleton_equal_other(type)
|
|
1061
|
-
end
|
|
1062
|
-
end
|
|
1063
|
-
|
|
1064
|
-
def narrow_singleton_equal_other(type)
|
|
1065
|
-
case type
|
|
1066
|
-
when Type::Singleton, Type::Tuple, Type::HashShape then Type::Combinator.bot
|
|
1067
|
-
else type
|
|
1068
|
-
end
|
|
1069
|
-
end
|
|
1070
|
-
|
|
1071
|
-
def narrow_singleton_not_equal(type, literal)
|
|
1072
|
-
case type
|
|
1073
|
-
when Type::Constant then type == literal ? Type::Combinator.bot : type
|
|
1074
|
-
when Type::Nominal then singleton_nominal_matches?(type, literal) ? Type::Combinator.bot : type
|
|
1075
|
-
when Type::Union
|
|
1076
|
-
Type::Combinator.union(*type.members.map { |member| narrow_singleton_not_equal(member, literal) })
|
|
1077
|
-
else type
|
|
1078
|
-
end
|
|
1079
|
-
end
|
|
1080
|
-
|
|
1081
|
-
def singleton_nominal_matches?(nominal, literal)
|
|
1082
|
-
case literal.value
|
|
1083
|
-
when nil then nominal.class_name == "NilClass"
|
|
1084
|
-
when true then nominal.class_name == "TrueClass"
|
|
1085
|
-
when false then nominal.class_name == "FalseClass"
|
|
1086
|
-
else false
|
|
1087
|
-
end
|
|
1088
|
-
end
|
|
1089
|
-
|
|
1090
808
|
def analyse_parentheses(node, scope)
|
|
1091
809
|
return nil if node.body.nil?
|
|
1092
810
|
|
|
@@ -1304,13 +1022,13 @@ module Rigor
|
|
|
1304
1022
|
[truthy, falsey]
|
|
1305
1023
|
end
|
|
1306
1024
|
|
|
1025
|
+
NAMED_CAPTURE_BODY_RE = /\(\?<([A-Za-z_][A-Za-z0-9_]*)>([^()|]*)\)/
|
|
1026
|
+
private_constant :NAMED_CAPTURE_BODY_RE
|
|
1027
|
+
|
|
1307
1028
|
# Extracts `{ capture_name => Refinement }` for every named capture group in the
|
|
1308
1029
|
# `MatchWriteNode`'s wrapped `=~` call whose body the recogniser table accepts. Bodies
|
|
1309
1030
|
# that contain nested groups, anchors, alternation, or anything else outside the curated
|
|
1310
1031
|
# forms drop out and the caller falls back to plain `String`.
|
|
1311
|
-
NAMED_CAPTURE_BODY_RE = /\(\?<([A-Za-z_][A-Za-z0-9_]*)>([^()|]*)\)/
|
|
1312
|
-
private_constant :NAMED_CAPTURE_BODY_RE
|
|
1313
|
-
|
|
1314
1032
|
def match_write_capture_refinements(node)
|
|
1315
1033
|
regex = node.call.is_a?(Prism::CallNode) ? node.call.receiver : nil
|
|
1316
1034
|
return {} unless regex.is_a?(Prism::RegularExpressionNode)
|
|
@@ -1478,16 +1196,20 @@ module Rigor
|
|
|
1478
1196
|
when :==, :!= then analyse_equality_predicate(node, scope, equality: name)
|
|
1479
1197
|
when :=== then analyse_case_equality_predicate(node, scope)
|
|
1480
1198
|
when :=~, :!~ then dispatch_regex_match_predicate(node, scope, name)
|
|
1481
|
-
when :match? then analyse_whole_regex_match_predicate(node, scope)
|
|
1482
|
-
when :key?, :has_key?, :include?, :member? then analyse_key_presence_predicate(node, scope)
|
|
1483
|
-
when :empty?, :any?, :none? then analyse_array_emptiness_predicate(node, scope, name)
|
|
1199
|
+
when :match? then RegexMatch.analyse_whole_regex_match_predicate(node, scope)
|
|
1200
|
+
when :key?, :has_key?, :include?, :member? then PresencePredicates.analyse_key_presence_predicate(node, scope)
|
|
1201
|
+
when :empty?, :any?, :none? then PresencePredicates.analyse_array_emptiness_predicate(node, scope, name)
|
|
1484
1202
|
when :respond_to? then analyse_respond_to_predicate(node, scope)
|
|
1485
1203
|
when :nan?, :finite? then analyse_float_class_predicate(node, scope, name)
|
|
1486
1204
|
end
|
|
1487
1205
|
end
|
|
1488
1206
|
|
|
1489
1207
|
def dispatch_regex_match_predicate(node, scope, name)
|
|
1490
|
-
name == :=~
|
|
1208
|
+
if name == :=~
|
|
1209
|
+
RegexMatch.analyse_regex_match_predicate(node, scope)
|
|
1210
|
+
else
|
|
1211
|
+
RegexMatch.analyse_regex_not_match_predicate(node, scope)
|
|
1212
|
+
end
|
|
1491
1213
|
end
|
|
1492
1214
|
|
|
1493
1215
|
# T3 (template-corpora survey) — `recv.respond_to?(sym)` truthy edge narrows `recv`
|
|
@@ -1513,7 +1235,7 @@ module Rigor
|
|
|
1513
1235
|
return nil if node.block
|
|
1514
1236
|
return nil if node.arguments.nil? || node.arguments.arguments.size != 1
|
|
1515
1237
|
|
|
1516
|
-
sym = static_hash_key(node.arguments.arguments.first)
|
|
1238
|
+
sym = PresencePredicates.static_hash_key(node.arguments.arguments.first)
|
|
1517
1239
|
return nil if sym.nil?
|
|
1518
1240
|
return nil if nilclass_method?(sym, scope)
|
|
1519
1241
|
|
|
@@ -1592,634 +1314,6 @@ module Rigor
|
|
|
1592
1314
|
true
|
|
1593
1315
|
end
|
|
1594
1316
|
|
|
1595
|
-
# ADR-47 §4-4 (Elixir `tuple_size`/non-empty analogue) — a bare `arr.empty?` /
|
|
1596
|
-
# `arr.any?` / `arr.none?` (no block, no args) narrows an Array-typed receiver to
|
|
1597
|
-
# `non-empty-array[T]` on the edge that implies "at least one element":
|
|
1598
|
-
#
|
|
1599
|
-
# - `empty?` → false edge (the array is NOT empty)
|
|
1600
|
-
# - `any?` → true edge (a truthy element exists ⇒ non-empty)
|
|
1601
|
-
# - `none?` → false edge (a truthy element exists ⇒ non-empty)
|
|
1602
|
-
#
|
|
1603
|
-
# The opposite edge is the conservative no-op (`any?`/`none?` falseness does not imply
|
|
1604
|
-
# emptiness — the array may hold only falsey elements; `empty?` truth could narrow to an
|
|
1605
|
-
# empty array but that carrier move is deferred). Only `Nominal[Array, [T]]` receivers
|
|
1606
|
-
# narrow — `Tuple` is already known-length, `Dynamic` is left alone (gradual
|
|
1607
|
-
# guarantee), and a non-Array receiver (`String#empty?`, `Range#any?`, …) bails so the
|
|
1608
|
-
# existing string / predicate paths still run.
|
|
1609
|
-
def analyse_array_emptiness_predicate(node, scope, name)
|
|
1610
|
-
return nil if node.block
|
|
1611
|
-
return nil unless node.arguments.nil? || node.arguments.arguments.empty?
|
|
1612
|
-
|
|
1613
|
-
reader, writer = emptiness_receiver_accessors(node.receiver)
|
|
1614
|
-
return nil if reader.nil?
|
|
1615
|
-
|
|
1616
|
-
current = scope.public_send(reader, node.receiver.name)
|
|
1617
|
-
return nil if current.nil?
|
|
1618
|
-
|
|
1619
|
-
non_empty = narrow_to_non_empty_array(current)
|
|
1620
|
-
return nil if non_empty.equal?(current) # receiver is not an Array shape → opaque
|
|
1621
|
-
|
|
1622
|
-
non_empty_scope = scope.public_send(writer, node.receiver.name, non_empty)
|
|
1623
|
-
name == :any? ? [non_empty_scope, scope] : [scope, non_empty_scope]
|
|
1624
|
-
end
|
|
1625
|
-
|
|
1626
|
-
def emptiness_receiver_accessors(receiver)
|
|
1627
|
-
case receiver
|
|
1628
|
-
when Prism::LocalVariableReadNode then %i[local with_local]
|
|
1629
|
-
when Prism::InstanceVariableReadNode then %i[ivar with_ivar]
|
|
1630
|
-
else [nil, nil]
|
|
1631
|
-
end
|
|
1632
|
-
end
|
|
1633
|
-
|
|
1634
|
-
# Refines `Array[T]` (and every Array member of a Union) to `non-empty-array[T]`;
|
|
1635
|
-
# returns the input unchanged when nothing applies, so the caller can detect "no
|
|
1636
|
-
# narrowing".
|
|
1637
|
-
def narrow_to_non_empty_array(type)
|
|
1638
|
-
case type
|
|
1639
|
-
when Type::Nominal
|
|
1640
|
-
return type unless type.class_name == "Array"
|
|
1641
|
-
|
|
1642
|
-
Type::Combinator.non_empty_array(type.type_args.first || Type::Combinator.top)
|
|
1643
|
-
when Type::Union
|
|
1644
|
-
Type::Combinator.union(*type.members.map { |member| narrow_to_non_empty_array(member) })
|
|
1645
|
-
else
|
|
1646
|
-
type
|
|
1647
|
-
end
|
|
1648
|
-
end
|
|
1649
|
-
|
|
1650
|
-
# ADR-47 §4-3 (Elixir `is_map_key/2` analogue) — `h.key?(:foo)` narrows the receiver on
|
|
1651
|
-
# the truthy edge: a `HashShape` whose `:foo` is an OPTIONAL key has it promoted to
|
|
1652
|
-
# REQUIRED, so a subsequent `h[:foo]` reads the declared value type instead of `value |
|
|
1653
|
-
# nil` (the optionality nil is gone). Sound because key presence removes only the
|
|
1654
|
-
# optionality-injected nil, never the value's own intrinsic nil (`h = {foo: nil}` keeps
|
|
1655
|
-
# `h[:foo]` nil-typed). The falsey edge is left unchanged — "key absent" is the
|
|
1656
|
-
# conservative no-op. Only literal `Symbol`/`String` arguments and
|
|
1657
|
-
# `LocalVariableReadNode`/`InstanceVariableReadNode` receivers narrow; everything else
|
|
1658
|
-
# (Dynamic, `Nominal[Hash]`, method-chain receivers) bails to no narrowing. A non-literal
|
|
1659
|
-
# key goes to {#analyse_computed_key_presence_predicate} (issue #1703).
|
|
1660
|
-
def analyse_key_presence_predicate(node, scope)
|
|
1661
|
-
return nil if node.arguments.nil?
|
|
1662
|
-
return nil unless node.arguments.arguments.size == 1
|
|
1663
|
-
|
|
1664
|
-
# A literal key promotes a shape's optional key only for `key?` / `has_key?`; `include?` / `member?` reach
|
|
1665
|
-
# the computed-key guard alone, so a String or Array receiver's `include?` keeps its own narrowings.
|
|
1666
|
-
key = static_hash_key(node.arguments.arguments.first) if %i[key? has_key?].include?(node.name)
|
|
1667
|
-
return analyse_computed_key_presence_predicate(node, scope) if key.nil?
|
|
1668
|
-
|
|
1669
|
-
case node.receiver
|
|
1670
|
-
when Prism::LocalVariableReadNode
|
|
1671
|
-
key_presence_scopes(node.receiver.name, key, scope, reader: :local, writer: :with_local)
|
|
1672
|
-
when Prism::InstanceVariableReadNode
|
|
1673
|
-
key_presence_scopes(node.receiver.name, key, scope, reader: :ivar, writer: :with_ivar)
|
|
1674
|
-
end
|
|
1675
|
-
end
|
|
1676
|
-
|
|
1677
|
-
# Issue #1703 — `H.key?(k)` / `has_key?` / `include?` / `member?` with a non-literal key (a local, an ivar,
|
|
1678
|
-
# or a reader chain such as `prop.column_type`) on a closed hash shape held in a local, an ivar or a
|
|
1679
|
-
# constant. The truthy edge records the guard, and a later `H[k]` of the same receiver and structurally the
|
|
1680
|
-
# same key drops the miss `nil` ({KeyPresenceGuard}). The falsey edge is the no-op: an
|
|
1681
|
-
# absent computed key says nothing a shape can record. Nil when the guard does not apply, so a collection's
|
|
1682
|
-
# `include?` still reaches the membership and String predicates.
|
|
1683
|
-
def analyse_computed_key_presence_predicate(node, scope)
|
|
1684
|
-
truthy = KeyPresenceGuard.record(node, scope)
|
|
1685
|
-
truthy && [truthy, scope]
|
|
1686
|
-
end
|
|
1687
|
-
|
|
1688
|
-
def key_presence_scopes(name, key, scope, reader:, writer:)
|
|
1689
|
-
current = scope.public_send(reader, name)
|
|
1690
|
-
return nil if current.nil?
|
|
1691
|
-
|
|
1692
|
-
truthy = narrow_hash_key_present(current, key)
|
|
1693
|
-
# §4-3 false edge — `h.key?(:foo)` being false proves `:foo` is absent, so on that
|
|
1694
|
-
# edge `h[:foo]` reads `nil`. Remove the (optional) key from the shape; a required
|
|
1695
|
-
# key makes the false edge dead and an unknown key is already nil, both no-ops.
|
|
1696
|
-
falsey = narrow_hash_key_absent(current, key)
|
|
1697
|
-
return nil if truthy.equal?(current) && falsey.equal?(current) # predicate is opaque
|
|
1698
|
-
|
|
1699
|
-
truthy_scope = truthy.equal?(current) ? scope : scope.public_send(writer, name, truthy)
|
|
1700
|
-
falsey_scope = falsey.equal?(current) ? scope : scope.public_send(writer, name, falsey)
|
|
1701
|
-
[truthy_scope, falsey_scope]
|
|
1702
|
-
end
|
|
1703
|
-
|
|
1704
|
-
def static_hash_key(node)
|
|
1705
|
-
case node
|
|
1706
|
-
when Prism::SymbolNode then node.unescaped.to_sym
|
|
1707
|
-
when Prism::StringNode then node.unescaped
|
|
1708
|
-
end
|
|
1709
|
-
end
|
|
1710
|
-
|
|
1711
|
-
# Promotes `key` from optional to required across a HashShape (or every HashShape
|
|
1712
|
-
# member of a Union). Returns the input unchanged when nothing applies, so the caller
|
|
1713
|
-
# can detect "no narrowing".
|
|
1714
|
-
def narrow_hash_key_present(type, key)
|
|
1715
|
-
case type
|
|
1716
|
-
when Type::HashShape
|
|
1717
|
-
promote_hash_key(type, key)
|
|
1718
|
-
when Type::Union
|
|
1719
|
-
Type::Combinator.union(*type.members.map { |member| narrow_hash_key_present(member, key) })
|
|
1720
|
-
else
|
|
1721
|
-
type
|
|
1722
|
-
end
|
|
1723
|
-
end
|
|
1724
|
-
|
|
1725
|
-
def promote_hash_key(shape, key)
|
|
1726
|
-
return shape unless shape.pairs.key?(key) # unknown key → no value type to assert
|
|
1727
|
-
return shape unless shape.optional_key?(key) # already required → nothing to promote
|
|
1728
|
-
|
|
1729
|
-
Type::HashShape.new(
|
|
1730
|
-
shape.pairs,
|
|
1731
|
-
required_keys: shape.required_keys + [key],
|
|
1732
|
-
optional_keys: shape.optional_keys - [key],
|
|
1733
|
-
read_only_keys: shape.read_only_keys,
|
|
1734
|
-
extra_keys: shape.extra_keys
|
|
1735
|
-
)
|
|
1736
|
-
end
|
|
1737
|
-
|
|
1738
|
-
# §4-3 false edge — drops `key` from a HashShape (or every HashShape member of a Union)
|
|
1739
|
-
# so the proven-absent key reads `nil`. Returns the input unchanged when nothing
|
|
1740
|
-
# applies (caller detects "no narrowing"). Only an *optional* present key is removed: a
|
|
1741
|
-
# required key makes `key?` always true (the false edge is dead, leave the shape opaque)
|
|
1742
|
-
# and a key absent from `pairs` already reads `nil` on a closed shape. On an open shape an
|
|
1743
|
-
# undeclared key keeps reading `untyped` on this edge — sound but imprecise, since the shape
|
|
1744
|
-
# carries no per-key exclusion to record what the guard just proved absent.
|
|
1745
|
-
def narrow_hash_key_absent(type, key)
|
|
1746
|
-
case type
|
|
1747
|
-
when Type::HashShape
|
|
1748
|
-
remove_hash_key(type, key)
|
|
1749
|
-
when Type::Union
|
|
1750
|
-
Type::Combinator.union(*type.members.map { |member| narrow_hash_key_absent(member, key) })
|
|
1751
|
-
else
|
|
1752
|
-
type
|
|
1753
|
-
end
|
|
1754
|
-
end
|
|
1755
|
-
|
|
1756
|
-
def remove_hash_key(shape, key)
|
|
1757
|
-
return shape unless shape.pairs.key?(key)
|
|
1758
|
-
return shape unless shape.optional_key?(key)
|
|
1759
|
-
|
|
1760
|
-
Type::HashShape.new(
|
|
1761
|
-
shape.pairs.except(key),
|
|
1762
|
-
required_keys: shape.required_keys,
|
|
1763
|
-
optional_keys: shape.optional_keys - [key],
|
|
1764
|
-
read_only_keys: shape.read_only_keys - [key],
|
|
1765
|
-
extra_keys: shape.extra_keys
|
|
1766
|
-
)
|
|
1767
|
-
end
|
|
1768
|
-
|
|
1769
|
-
# Survey item (b): `/regex/ =~ str` and `str =~ /regex/` bind the regex match-data
|
|
1770
|
-
# globals on each edge ({#regex_match_edges}), gated on the subject (#1494): an edge is
|
|
1771
|
-
# narrowed only when the receiver proves what the frame holds on it
|
|
1772
|
-
# ({#match_receiver_edges}), and an edge it cannot vouch for keeps the match globals
|
|
1773
|
-
# forgotten. A `String?` subject keeps the match edge only, since `NilClass#=~` answers
|
|
1774
|
-
# nil with the previous match still bound; an untyped one is read the same way.
|
|
1775
|
-
#
|
|
1776
|
-
# Returns nil (no narrowing) when the receiver / argument pair does not resolve to exactly one
|
|
1777
|
-
# regex pattern we can count (see {#regex_match_pattern}).
|
|
1778
|
-
def analyse_regex_match_predicate(node, scope)
|
|
1779
|
-
gated_regex_match_edges(node, scope)
|
|
1780
|
-
end
|
|
1781
|
-
|
|
1782
|
-
# Issue #1379 — `subject !~ pattern` is `Kernel#!~`, which returns `!(subject =~ pattern)`. It runs the same
|
|
1783
|
-
# match in the caller's frame, so its edges are {#analyse_regex_match_predicate}'s swapped: the falsey edge is
|
|
1784
|
-
# the match edge (`$~` a `MatchData`, an unconditional `$N` a `String`, and the whole-receiver refinement), the
|
|
1785
|
-
# truthy edge the no-match edge (`$~` and `$N` nil). The same patterns are recognised and the same ones
|
|
1786
|
-
# declined (a non-regex operand, an ambiguous constant, `//x`), and the receiver gates the edges alike.
|
|
1787
|
-
def analyse_regex_not_match_predicate(node, scope)
|
|
1788
|
-
gated_regex_match_edges(node, scope)&.reverse
|
|
1789
|
-
end
|
|
1790
|
-
|
|
1791
|
-
# `[match_edge, no_match_edge]` of a `=~` / `!~` predicate, each kept only where
|
|
1792
|
-
# {#match_receiver_edges} lets it be; an edge it cannot vouch for is the entry scope with the
|
|
1793
|
-
# match globals forgotten, since the call may still have rebound them. The whole-receiver
|
|
1794
|
-
# refinement is kept only when the local it would narrow is known to be a String on the
|
|
1795
|
-
# match edge ({#refinable_operand?}).
|
|
1796
|
-
def gated_regex_match_edges(node, scope)
|
|
1797
|
-
edges = regex_match_edges(node, scope, refine: refinable_operand?(node, scope))
|
|
1798
|
-
return nil if edges.nil?
|
|
1799
|
-
|
|
1800
|
-
match, no_match = edges
|
|
1801
|
-
forgotten = scope.forget_match_globals
|
|
1802
|
-
case match_receiver_edges(node, scope)
|
|
1803
|
-
when :both then [match, no_match]
|
|
1804
|
-
when :match then [match, forgotten]
|
|
1805
|
-
else [forgotten, forgotten]
|
|
1806
|
-
end
|
|
1807
|
-
end
|
|
1808
|
-
|
|
1809
|
-
# The ungated edges of a match:
|
|
1810
|
-
#
|
|
1811
|
-
# - Match edge (`=~` returned an Integer position — the match succeeded): `$~` to
|
|
1812
|
-
# `Nominal[MatchData]`; `$&` and `$1..$N` (where N is the number of capture groups in
|
|
1813
|
-
# the regex source) to `Nominal[String]`. This is the same optimistic-narrowing shape
|
|
1814
|
-
# the existing `analyse_match_write` uses for named captures inside `if /(?<x>...)/
|
|
1815
|
-
# =~ str` — optional groups in the regex source (`(\d+)?`) would bind `$N` to `nil`
|
|
1816
|
-
# at runtime, but the floor here matches the common idiom (required captures) and
|
|
1817
|
-
# lets `unless /(\d+)/ =~ s; raise; end; $1.to_i` resolve cleanly.
|
|
1818
|
-
# - No-match edge (`=~` returned nil): `$~` and every numbered / back-reference
|
|
1819
|
-
# global bound to `Constant<nil>`.
|
|
1820
|
-
#
|
|
1821
|
-
# `refine: false` leaves out the whole-receiver refinement, which names a String.
|
|
1822
|
-
def regex_match_edges(node, scope, refine:)
|
|
1823
|
-
return nil if node.arguments.nil?
|
|
1824
|
-
return nil unless node.arguments.arguments.size == 1
|
|
1825
|
-
|
|
1826
|
-
arg = node.arguments.arguments.first
|
|
1827
|
-
pattern = regex_match_pattern(node.receiver, arg, scope)
|
|
1828
|
-
return nil if pattern.nil?
|
|
1829
|
-
# Extended mode (`//x`) lets the pattern carry free whitespace and `#` comments, and a
|
|
1830
|
-
# comment may contain a literal `(` — which the light char-scan walker would miscount as a
|
|
1831
|
-
# capturing group, shifting `$N` indices and narrowing the wrong global. That is a
|
|
1832
|
-
# false-negative-class misnarrowing, so bail rather than risk it. The literal path shares
|
|
1833
|
-
# this bail: `RegularExpressionNode` carries the same latent miscount.
|
|
1834
|
-
return nil if pattern.extended
|
|
1835
|
-
|
|
1836
|
-
unconditional = unconditional_capture_groups(pattern.source)
|
|
1837
|
-
truthy, falsey = regex_match_predicate_scopes(scope, unconditional)
|
|
1838
|
-
# #164 — layer whole-receiver refinement onto the truthy edge without disturbing the
|
|
1839
|
-
# match-global logic above. A successful `str =~ /\A\d+\z/` proves the WHOLE string
|
|
1840
|
-
# matched, so the string operand narrows to the imported refinement the anchored pattern
|
|
1841
|
-
# names. The falsey edge is untouched: a failed match proves nothing about the shape.
|
|
1842
|
-
truthy = apply_whole_receiver_refinement(truthy, node.receiver, arg, pattern.source, scope) if refine
|
|
1843
|
-
[truthy, falsey]
|
|
1844
|
-
end
|
|
1845
|
-
|
|
1846
|
-
# Which edges of `recv =~ pattern` / `recv !~ pattern` the receiver lets {#gated_regex_match_edges} narrow:
|
|
1847
|
-
# `:both`, `:match` (the match edge only) or nil. `Kernel#!~` dispatches `=~` on the receiver:
|
|
1848
|
-
#
|
|
1849
|
-
# - A Regexp receiver (the pattern on the left) runs `Regexp#=~`, which sets `$~` on both outcomes, a `nil`
|
|
1850
|
-
# argument included: `:both`.
|
|
1851
|
-
# - `String#=~` and `Symbol#=~` do the same: `:both` when every member of the receiver's type is a String
|
|
1852
|
-
# (a project subclass of String included) or a Symbol.
|
|
1853
|
-
# - `NilClass#=~` answers nil and leaves `$~` alone, so a nil receiver reaches the no-match edge with the
|
|
1854
|
-
# previous match still bound; it never reaches the match edge. A `Dynamic` receiver may be nil at run time
|
|
1855
|
-
# whatever its static facet says. So does a safe-navigation call, which answers nil without matching on a
|
|
1856
|
-
# nil receiver.
|
|
1857
|
-
#
|
|
1858
|
-
# The two operators part over a receiver that is none of those ({#match_operator_edges},
|
|
1859
|
-
# {#not_match_operator_edges}).
|
|
1860
|
-
def match_receiver_edges(node, scope)
|
|
1861
|
-
return nil if node.receiver.nil?
|
|
1862
|
-
|
|
1863
|
-
edges =
|
|
1864
|
-
if regex_operand_pattern(node.receiver, scope).is_a?(RegexMatchPattern)
|
|
1865
|
-
:both
|
|
1866
|
-
else
|
|
1867
|
-
kinds = match_member_kinds(scope.type_of(node.receiver), scope)
|
|
1868
|
-
node.name == :=~ ? match_operator_edges(kinds) : not_match_operator_edges(kinds)
|
|
1869
|
-
end
|
|
1870
|
-
return edges unless node.safe_navigation?
|
|
1871
|
-
|
|
1872
|
-
:match if edges && node.name == :=~
|
|
1873
|
-
end
|
|
1874
|
-
|
|
1875
|
-
STRING_MATCH_KINDS = %i[string string_subclass symbol].freeze
|
|
1876
|
-
private_constant :STRING_MATCH_KINDS
|
|
1877
|
-
|
|
1878
|
-
# `recv =~ pattern`'s match edge is the truthy one. Only a member known to define its own `=~` in the
|
|
1879
|
-
# project keeps it out (#1494): that method binds `$~` in its own frame, not the caller's. Any other member
|
|
1880
|
-
# either matches in the caller's frame, answers nil (`NilClass`), or has no `=~` at all (`Integer` since
|
|
1881
|
-
# Ruby 3.2 drops `Object#=~`) and so never reaches the edge; a receiver the analyzer cannot classify keeps the
|
|
1882
|
-
# edge, as every `=~` did before #1494.
|
|
1883
|
-
def match_operator_edges(kinds)
|
|
1884
|
-
return nil if kinds.include?(:custom)
|
|
1885
|
-
return :both if kinds.all? { |kind| STRING_MATCH_KINDS.include?(kind) }
|
|
1886
|
-
|
|
1887
|
-
:match
|
|
1888
|
-
end
|
|
1889
|
-
|
|
1890
|
-
# `recv !~ pattern`'s match edge is the falsey one, which a receiver whose `=~` is not the caller's match
|
|
1891
|
-
# also reaches, so it is kept only when every member is a String, a Symbol, nil, or a `Dynamic` that is
|
|
1892
|
-
# untyped or whose facet holds only those (#1379).
|
|
1893
|
-
def not_match_operator_edges(kinds)
|
|
1894
|
-
return :both if kinds.all? { |kind| STRING_MATCH_KINDS.include?(kind) }
|
|
1895
|
-
|
|
1896
|
-
:match if kinds.all? { |kind| (STRING_MATCH_KINDS + %i[nil dynamic]).include?(kind) }
|
|
1897
|
-
end
|
|
1898
|
-
|
|
1899
|
-
# Whether the local operand {#apply_whole_receiver_refinement} would narrow holds only Strings and nil, which
|
|
1900
|
-
# the match edge excludes. The refinement names a String, so a member that may be a Symbol — a Symbol, or an
|
|
1901
|
-
# untyped `Dynamic` that may be one at run time — keeps it out: `return unless raw =~ /\A\d+\z/` on an untyped
|
|
1902
|
-
# `raw` must not make `raw.to_proc` an undefined method (#1379, #1494).
|
|
1903
|
-
def refinable_operand?(node, scope)
|
|
1904
|
-
operand = [node.receiver, *node.arguments&.arguments].find { |o| o.is_a?(Prism::LocalVariableReadNode) }
|
|
1905
|
-
type = operand && scope.local(operand.name)
|
|
1906
|
-
return true if type.nil?
|
|
1907
|
-
|
|
1908
|
-
match_member_kinds(type, scope).all? { |kind| %i[string nil].include?(kind) }
|
|
1909
|
-
end
|
|
1910
|
-
|
|
1911
|
-
def match_member_kinds(type, scope)
|
|
1912
|
-
(type.is_a?(Type::Union) ? type.members : [type]).map { |member| match_member_kind(member, scope) }
|
|
1913
|
-
end
|
|
1914
|
-
|
|
1915
|
-
# `:custom` for a member whose class the project gives a `=~` of its own (in its ancestry, a reopened
|
|
1916
|
-
# `String` included); `:string` or `:symbol` for a member {.narrow_not_class} proves a String or a Symbol (a
|
|
1917
|
-
# value or refinement of either, or a class the environment orders below it); `:string_subclass` for a
|
|
1918
|
-
# project class whose superclass chain reaches `String`, which the environment cannot order; `:nil`;
|
|
1919
|
-
# `:dynamic` for a `Dynamic` whose facet is untyped or holds only Strings, Symbols and nil; or nil for anything
|
|
1920
|
-
# else.
|
|
1921
|
-
def match_member_kind(member, scope)
|
|
1922
|
-
return match_dynamic_kind(member, scope) if member.is_a?(Type::Dynamic)
|
|
1923
|
-
return :nil if narrow_nil(member) == member && !member.is_a?(Type::Bot)
|
|
1924
|
-
return :custom if custom_match_operator?(member, scope)
|
|
1925
|
-
|
|
1926
|
-
environment = scope.environment
|
|
1927
|
-
{ "String" => :string, "Symbol" => :symbol }.each do |class_name, kind|
|
|
1928
|
-
return kind if narrow_not_class(member, class_name, environment: environment, scope: scope).is_a?(Type::Bot)
|
|
1929
|
-
end
|
|
1930
|
-
:string_subclass if project_string_subclass?(member, scope)
|
|
1931
|
-
end
|
|
1932
|
-
|
|
1933
|
-
def match_dynamic_kind(member, scope)
|
|
1934
|
-
facet = member.static_facet
|
|
1935
|
-
return :dynamic if Type::Combinator.untyped?(member)
|
|
1936
|
-
|
|
1937
|
-
facet_kinds = match_member_kinds(facet, scope)
|
|
1938
|
-
:dynamic if facet_kinds.all? { |kind| (STRING_MATCH_KINDS + [:nil]).include?(kind) }
|
|
1939
|
-
end
|
|
1940
|
-
|
|
1941
|
-
def custom_match_operator?(member, scope)
|
|
1942
|
-
targets = ProjectMethodOwnership.targets(member)
|
|
1943
|
-
return false if targets.nil?
|
|
1944
|
-
|
|
1945
|
-
targets.any? { |class_name, kind| ProjectMethodOwnership.defines?(class_name, :=~, kind, scope) }
|
|
1946
|
-
end
|
|
1947
|
-
|
|
1948
|
-
def project_string_subclass?(member, scope)
|
|
1949
|
-
return false unless member.is_a?(Type::Nominal) && scope.known_user_class?(member.class_name)
|
|
1950
|
-
|
|
1951
|
-
scope.external_ancestor_name_candidates(member.class_name, mixins: false)
|
|
1952
|
-
.flatten.any? { |name| name.to_s.delete_prefix("::") == "String" }
|
|
1953
|
-
end
|
|
1954
|
-
|
|
1955
|
-
# `str.match?(/\A\d+\z/)` — `String#match?` is a pure boolean predicate whose truthy edge
|
|
1956
|
-
# proves a property of the ENTIRE receiver. When the argument is a fully `\A…\z`-anchored
|
|
1957
|
-
# single-char-class pattern recognised by {RegexRefinement.for_whole_pattern}, narrow the
|
|
1958
|
-
# receiver local to the matching imported refinement on the truthy edge; the falsey edge
|
|
1959
|
-
# passes through unchanged (failing one pattern proves nothing about the string's shape).
|
|
1960
|
-
# A non-literal / unanchored / `\Z` / line-anchored / `//x` pattern falls through to no
|
|
1961
|
-
# narrowing.
|
|
1962
|
-
#
|
|
1963
|
-
# The refinement names a String, so it shares `=~`'s gate (#1955): a receiver that may be a Symbol, or an
|
|
1964
|
-
# untyped one, is left as it is ({#refinable_operand?}).
|
|
1965
|
-
def analyse_whole_regex_match_predicate(node, scope)
|
|
1966
|
-
return nil unless node.receiver.is_a?(Prism::LocalVariableReadNode)
|
|
1967
|
-
return nil if node.arguments.nil? || node.arguments.arguments.size != 1
|
|
1968
|
-
return nil unless refinable_operand?(node, scope)
|
|
1969
|
-
|
|
1970
|
-
pattern = regex_operand_pattern(node.arguments.arguments.first, scope)
|
|
1971
|
-
return nil unless pattern.is_a?(RegexMatchPattern)
|
|
1972
|
-
return nil if pattern.extended
|
|
1973
|
-
|
|
1974
|
-
refinement = ::Rigor::Builtins::RegexRefinement.for_whole_pattern(pattern.source)
|
|
1975
|
-
return nil if refinement.nil?
|
|
1976
|
-
|
|
1977
|
-
local_name = node.receiver.name
|
|
1978
|
-
return nil if scope.local(local_name).nil?
|
|
1979
|
-
|
|
1980
|
-
[scope.with_local(local_name, refinement), scope]
|
|
1981
|
-
end
|
|
1982
|
-
|
|
1983
|
-
# Narrows the string operand of a `=~` predicate to a whole-receiver refinement when the
|
|
1984
|
-
# regex source is a recognised `\A…\z`-anchored pattern. Returns the (possibly unchanged)
|
|
1985
|
-
# truthy scope; the falsey edge is never touched by this layer.
|
|
1986
|
-
def apply_whole_receiver_refinement(truthy, receiver, arg, source, scope)
|
|
1987
|
-
refinement = ::Rigor::Builtins::RegexRefinement.for_whole_pattern(source)
|
|
1988
|
-
return truthy if refinement.nil?
|
|
1989
|
-
|
|
1990
|
-
# `regex_match_pattern` already resolved the regex operand from a literal or a
|
|
1991
|
-
# `Constant[Regexp]` constant read — never a bare local — so the string subject is
|
|
1992
|
-
# unambiguously the local-read operand, if either operand is one.
|
|
1993
|
-
subject = [receiver, arg].find { |operand| operand.is_a?(Prism::LocalVariableReadNode) }
|
|
1994
|
-
return truthy if subject.nil? || scope.local(subject.name).nil?
|
|
1995
|
-
|
|
1996
|
-
truthy.with_local(subject.name, refinement)
|
|
1997
|
-
end
|
|
1998
|
-
|
|
1999
|
-
# Recognises the regex pattern operand of a `=~` predicate. An operand resolves to a regex when
|
|
2000
|
-
# it is either a syntactic `RegularExpressionNode` literal OR a constant read whose type is a
|
|
2001
|
-
# value-pinned `Constant[Regexp]` (`RE = /.../` and `RE = Regexp.new(...)` both fold to the
|
|
2002
|
-
# carrier in the whole-program constant pre-pass). Returns a single {RegexMatchPattern}, or nil
|
|
2003
|
-
# (no narrowing) when NEITHER operand resolves to a regex, when BOTH do (`/a/ =~ /b/` — no
|
|
2004
|
-
# string subject to bind), or when a constant operand is ambiguous — typed as a `Union` from a
|
|
2005
|
-
# twice-assigned constant, where no single source can be pinned.
|
|
2006
|
-
def regex_match_pattern(left, right, scope)
|
|
2007
|
-
left_pattern = regex_operand_pattern(left, scope)
|
|
2008
|
-
right_pattern = regex_operand_pattern(right, scope)
|
|
2009
|
-
return nil if left_pattern == :ambiguous || right_pattern == :ambiguous
|
|
2010
|
-
|
|
2011
|
-
patterns = [left_pattern, right_pattern].grep(RegexMatchPattern)
|
|
2012
|
-
patterns.size == 1 ? patterns.first : nil
|
|
2013
|
-
end
|
|
2014
|
-
|
|
2015
|
-
# Classifies one `=~` operand: a {RegexMatchPattern} when it resolves to a regex (literal or
|
|
2016
|
-
# `Constant[Regexp]`), `:ambiguous` when a constant operand types as a `Union` (a twice-assigned
|
|
2017
|
-
# constant), or nil when the operand is not a regex at all.
|
|
2018
|
-
def regex_operand_pattern(node, scope)
|
|
2019
|
-
case node
|
|
2020
|
-
when Prism::RegularExpressionNode
|
|
2021
|
-
RegexMatchPattern.new(source: node.unescaped, extended: node.extended?)
|
|
2022
|
-
when Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
2023
|
-
regex_operand_from_constant(node, scope)
|
|
2024
|
-
end
|
|
2025
|
-
end
|
|
2026
|
-
|
|
2027
|
-
# Resolves a constant-reference operand to a regex pattern via the shared lexical-constant
|
|
2028
|
-
# resolution (`Reflection.resolve_constant_type`). `Regexp#options` gives the real
|
|
2029
|
-
# extended-mode flag; `Regexp#source` the pattern. Returns `:ambiguous` for a `Union`-typed
|
|
2030
|
-
# constant so the caller bails, and nil when the constant does not resolve to a `Constant[Regexp]`.
|
|
2031
|
-
def regex_operand_from_constant(node, scope)
|
|
2032
|
-
name = Source::ConstantPath.qualified_name_or_nil(node)
|
|
2033
|
-
return nil if name.nil?
|
|
2034
|
-
|
|
2035
|
-
type = Reflection.resolve_constant_type(name, scope: scope,
|
|
2036
|
-
rooted: Source::ConstantPath.rooted?(node))
|
|
2037
|
-
return :ambiguous if type.is_a?(Type::Union)
|
|
2038
|
-
return nil unless type.is_a?(Type::Constant)
|
|
2039
|
-
|
|
2040
|
-
value = type.value
|
|
2041
|
-
return nil unless value.is_a?(Regexp)
|
|
2042
|
-
|
|
2043
|
-
RegexMatchPattern.new(source: value.source, extended: value.options.anybits?(Regexp::EXTENDED))
|
|
2044
|
-
end
|
|
2045
|
-
|
|
2046
|
-
# Curated set of match globals bound non-nil on every successful `=~` regardless of grouping:
|
|
2047
|
-
# `$~` (the MatchData), `$&` (whole match), `` $` `` (pre-match), `$'` (post-match). Numbered
|
|
2048
|
-
# references (`$1..$N`) and `$+` (the last matched group) are handled separately because they
|
|
2049
|
-
# depend on the regex source — `$+` is gated on the same participation set as `$N` (#177).
|
|
2050
|
-
REGEX_MATCH_GLOBALS = %i[$~ $& $` $'].freeze
|
|
2051
|
-
private_constant :REGEX_MATCH_GLOBALS
|
|
2052
|
-
|
|
2053
|
-
# `unconditional` is the Set of 1-based numbered-capture indices whose group is
|
|
2054
|
-
# guaranteed to participate in any successful match (no optional quantifier on the
|
|
2055
|
-
# group or an ancestor, no alternation in the pattern). Those `$N` are bound to
|
|
2056
|
-
# `String`; every other numbered group present in the pattern stays `String | nil` on
|
|
2057
|
-
# both edges (a truthy match leaves an optional group nil at runtime), so we do not
|
|
2058
|
-
# narrow it on the truthy edge.
|
|
2059
|
-
#
|
|
2060
|
-
# Both edges start from `scope` with the match globals forgotten (#1385): the match rebinds every one of them,
|
|
2061
|
-
# so a `$N` an earlier match narrowed says nothing here. Without that, a `when /(x)/` arm, or the right operand
|
|
2062
|
-
# of `s =~ /(a)(b)(c)/ && t =~ /(x)/`, kept `$2` and `$3` narrowed to `String` where Ruby reads nil.
|
|
2063
|
-
def regex_match_predicate_scopes(scope, unconditional)
|
|
2064
|
-
string_t = Type::Combinator.nominal_of("String")
|
|
2065
|
-
match_data_t = Type::Combinator.nominal_of("MatchData")
|
|
2066
|
-
nil_t = Type::Combinator.constant_of(nil)
|
|
2067
|
-
|
|
2068
|
-
truthy = scope.forget_match_globals
|
|
2069
|
-
falsey = truthy
|
|
2070
|
-
truthy = truthy.with_global(:$~, match_data_t)
|
|
2071
|
-
falsey = falsey.with_global(:$~, nil_t)
|
|
2072
|
-
REGEX_MATCH_GLOBALS.each do |name|
|
|
2073
|
-
next if name == :$~
|
|
2074
|
-
|
|
2075
|
-
truthy = truthy.with_global(name, string_t)
|
|
2076
|
-
falsey = falsey.with_global(name, nil_t)
|
|
2077
|
-
end
|
|
2078
|
-
unconditional.each do |index|
|
|
2079
|
-
name = :"$#{index}"
|
|
2080
|
-
truthy = truthy.with_global(name, string_t)
|
|
2081
|
-
falsey = falsey.with_global(name, nil_t)
|
|
2082
|
-
end
|
|
2083
|
-
# `$+` is the LAST matched group, so it is nil on a successful match whose groups are all
|
|
2084
|
-
# optional (`"b" =~ /(a)?b/` matches yet leaves `$+` nil) or absent (no capture group). It
|
|
2085
|
-
# narrows to `String` only when some group is guaranteed to participate — the same
|
|
2086
|
-
# participation gate `$N` uses (#177). With no unconditional group it is left unbound on both
|
|
2087
|
-
# edges (fall through to the RBS `String?` default), exactly as an optional `$N` is.
|
|
2088
|
-
unless unconditional.empty?
|
|
2089
|
-
truthy = truthy.with_global(:$+, string_t)
|
|
2090
|
-
falsey = falsey.with_global(:$+, nil_t)
|
|
2091
|
-
end
|
|
2092
|
-
[truthy, falsey]
|
|
2093
|
-
end
|
|
2094
|
-
|
|
2095
|
-
# Returns the Set of 1-based numbered-capture indices that are UNCONDITIONAL in
|
|
2096
|
-
# `source`: present on every successful match because no optional quantifier (`?`,
|
|
2097
|
-
# `*`, `{0,…}`, `{,…}`) applies to the group or any ancestor group, and the pattern contains no
|
|
2098
|
-
# alternation (`|`). Optional and alternation-reachable groups are excluded — at
|
|
2099
|
-
# runtime `$N` is `nil` for them even when the overall match succeeds, so narrowing
|
|
2100
|
-
# them to non-nil `String` would be unsound. The walker is intentionally light (char
|
|
2101
|
-
# scan, not a regex-AST parse): backslash escapes are skipped; `(?:…)`, lookahead
|
|
2102
|
-
# `(?=…)`/`(?!…)`, and lookbehind `(?<=…)`/`(?<!…)` do not capture; named groups
|
|
2103
|
-
# `(?<name>…)` / `(?'name'…)` do. Once the pattern has a named group, Ruby numbers only
|
|
2104
|
-
# the named groups and a plain `(…)` does not capture (#1471): `/(?<x>a)(b)/` has one
|
|
2105
|
-
# group, so `$2` is nil. Conservatism is one-directional — when in doubt a group is
|
|
2106
|
-
# treated as conditional (dropped from the Set), never the reverse.
|
|
2107
|
-
def unconditional_capture_groups(source)
|
|
2108
|
-
# `unconditional` collects every capturing index; a group is later removed (with its
|
|
2109
|
-
# whole subtree) when it is optionally quantified, nested under an optional ancestor,
|
|
2110
|
-
# or sits in an alternation branch. `stack` holds one frame per open group (plus a
|
|
2111
|
-
# virtual root frame for the top level) as `[group_index_or_nil, descendant_indices,
|
|
2112
|
-
# alternated?]`. A `|` marks the CURRENT group's frame alternated — its branches are
|
|
2113
|
-
# mutually exclusive, so its descendant captures may be absent on a successful match;
|
|
2114
|
-
# the group itself still participates. Closing a frame rolls its subtree up to the
|
|
2115
|
-
# parent so an optional / alternated ancestor disqualifies it.
|
|
2116
|
-
state = { unconditional: Set.new, stack: [[nil, [], false]], group_index: 0, named: named_groups?(source) }
|
|
2117
|
-
each_structural_char(source) { |pos, chr| scan_group_char(source, pos, chr, state) }
|
|
2118
|
-
# Drain the virtual root: a top-level `|` disqualifies all.
|
|
2119
|
-
finalize_frame(state, state[:stack].pop, optional: false)
|
|
2120
|
-
state[:unconditional]
|
|
2121
|
-
end
|
|
2122
|
-
|
|
2123
|
-
# Yields each position of `source` outside a backslash escape and a character class, with its char.
|
|
2124
|
-
def each_structural_char(source)
|
|
2125
|
-
pos = 0
|
|
2126
|
-
length = source.length
|
|
2127
|
-
while pos < length
|
|
2128
|
-
chr = source[pos]
|
|
2129
|
-
if chr == "\\"
|
|
2130
|
-
pos += 2
|
|
2131
|
-
next
|
|
2132
|
-
end
|
|
2133
|
-
if chr == "["
|
|
2134
|
-
pos = skip_char_class(source, pos) + 1
|
|
2135
|
-
next
|
|
2136
|
-
end
|
|
2137
|
-
yield pos, chr
|
|
2138
|
-
pos += 1
|
|
2139
|
-
end
|
|
2140
|
-
end
|
|
2141
|
-
|
|
2142
|
-
def named_groups?(source)
|
|
2143
|
-
each_structural_char(source) { |pos, chr| break true if chr == "(" && named_group?(source, pos) } == true
|
|
2144
|
-
end
|
|
2145
|
-
|
|
2146
|
-
# Updates the walk `state` at a group-relevant char during
|
|
2147
|
-
# {#unconditional_capture_groups}: `(` pushes a frame, `)` pops and resolves it, `|`
|
|
2148
|
-
# flags the current frame as alternated.
|
|
2149
|
-
def scan_group_char(source, pos, chr, state)
|
|
2150
|
-
case chr
|
|
2151
|
-
when "("
|
|
2152
|
-
idx = nil
|
|
2153
|
-
if capturing_group?(source, pos, named: state[:named])
|
|
2154
|
-
idx = (state[:group_index] += 1)
|
|
2155
|
-
state[:unconditional] << idx
|
|
2156
|
-
end
|
|
2157
|
-
state[:stack].push([idx, [], false])
|
|
2158
|
-
when ")"
|
|
2159
|
-
finalize_frame(state, state[:stack].pop, optional: next_quantifier_optional?(source, pos + 1))
|
|
2160
|
-
when "|"
|
|
2161
|
-
state[:stack].last[2] = true
|
|
2162
|
-
end
|
|
2163
|
-
end
|
|
2164
|
-
|
|
2165
|
-
# Resolves a closed (or virtual-root) group frame: its subtree is its own index plus
|
|
2166
|
-
# every descendant index. The subtree is disqualified when the group is optionally
|
|
2167
|
-
# quantified or its branches are alternated (only the descendants in that case — but a
|
|
2168
|
-
# self subtree always keeps its own index unless optional).
|
|
2169
|
-
def finalize_frame(state, frame, optional:)
|
|
2170
|
-
idx, descendants, alternated = frame
|
|
2171
|
-
subtree = descendants.dup
|
|
2172
|
-
subtree << idx if idx
|
|
2173
|
-
state[:unconditional].subtract(subtree) if optional
|
|
2174
|
-
# Alternation disqualifies the branch contents (descendants),
|
|
2175
|
-
# never the enclosing group itself.
|
|
2176
|
-
state[:unconditional].subtract(descendants) if alternated
|
|
2177
|
-
state[:stack].last && state[:stack].last[1].concat(subtree)
|
|
2178
|
-
end
|
|
2179
|
-
|
|
2180
|
-
# True when the group whose closing paren is at `source[pos]` is followed by a
|
|
2181
|
-
# quantifier that permits zero repetitions (`?`, `*`, `{0…}`, `{,…}`). `+` and `{1,…}`
|
|
2182
|
-
# do NOT make a group optional. A lazy/possessive suffix (`*?`, `*+`) is still
|
|
2183
|
-
# zero-permitting on the base quantifier. `{,…}` counts only with a digit after the
|
|
2184
|
-
# comma — `{,}`, `{,x}` and `{, }` are literal braces, not quantifiers.
|
|
2185
|
-
def next_quantifier_optional?(source, pos)
|
|
2186
|
-
case source[pos]
|
|
2187
|
-
when "?", "*" then true
|
|
2188
|
-
when "{" then source[pos + 1] == "0" ||
|
|
2189
|
-
(source[pos + 1] == "," && source[pos + 2].to_s.match?(/\d/))
|
|
2190
|
-
else false
|
|
2191
|
-
end
|
|
2192
|
-
end
|
|
2193
|
-
|
|
2194
|
-
# A plain `(…)` captures unless the pattern has a named group (`named:`); of the `(?…)` forms only a named
|
|
2195
|
-
# group does.
|
|
2196
|
-
def capturing_group?(source, pos, named:)
|
|
2197
|
-
return !named unless source[pos + 1] == "?"
|
|
2198
|
-
|
|
2199
|
-
named_group?(source, pos)
|
|
2200
|
-
end
|
|
2201
|
-
|
|
2202
|
-
# `(?<name>…)` and `(?'name'…)` are named groups; `(?<=…)`/`(?<!…)` (lookbehind) and
|
|
2203
|
-
# `(?:…)`/`(?=…)`/`(?!…)` are not.
|
|
2204
|
-
def named_group?(source, pos)
|
|
2205
|
-
return false unless source[pos + 1] == "?"
|
|
2206
|
-
return true if source[pos + 2] == "'"
|
|
2207
|
-
|
|
2208
|
-
source[pos + 2] == "<" && source[pos + 3] != "=" && source[pos + 3] != "!"
|
|
2209
|
-
end
|
|
2210
|
-
|
|
2211
|
-
def skip_char_class(source, start)
|
|
2212
|
-
pos = start + 1
|
|
2213
|
-
pos += 1 if source[pos] == "^"
|
|
2214
|
-
pos += 1 if source[pos] == "]" # literal ] as first member
|
|
2215
|
-
while pos < source.length
|
|
2216
|
-
return pos if source[pos] == "]"
|
|
2217
|
-
|
|
2218
|
-
pos += source[pos] == "\\" ? 2 : 1
|
|
2219
|
-
end
|
|
2220
|
-
pos
|
|
2221
|
-
end
|
|
2222
|
-
|
|
2223
1317
|
def dispatch_call_numeric(node, scope, name)
|
|
2224
1318
|
if COMPARISON_OPERATORS.include?(name)
|
|
2225
1319
|
analyse_comparison_predicate(node, scope, comparator: name)
|
|
@@ -2267,7 +1361,7 @@ module Rigor
|
|
|
2267
1361
|
# `x.between?(a, b)` truthy edge narrows to `narrow_integer_comparison(>=, a)` ∩
|
|
2268
1362
|
# `narrow_integer_comparison(<=, b)`. The falsey edge is left unchanged because the
|
|
2269
1363
|
# complement is a two-piece domain (`x < a || x > b`) that the lattice cannot express
|
|
2270
|
-
# precisely. `a` and `b` MUST both be integer literals.
|
|
1364
|
+
# precisely. `a` and `b` MUST both be integer or float literals.
|
|
2271
1365
|
def analyse_between_predicate(node, scope)
|
|
2272
1366
|
return nil unless node.receiver.is_a?(Prism::LocalVariableReadNode)
|
|
2273
1367
|
return nil if node.arguments.nil?
|
|
@@ -2551,13 +1645,13 @@ module Rigor
|
|
|
2551
1645
|
def equality_local_literal(left, right, scope)
|
|
2552
1646
|
if left.is_a?(Prism::LocalVariableReadNode)
|
|
2553
1647
|
literal = static_literal_type(right, scope)
|
|
2554
|
-
return [left.name, literal] if trusted_equality_literal?(literal)
|
|
1648
|
+
return [left.name, literal] if Complements.trusted_equality_literal?(literal)
|
|
2555
1649
|
end
|
|
2556
1650
|
|
|
2557
1651
|
return nil unless right.is_a?(Prism::LocalVariableReadNode)
|
|
2558
1652
|
|
|
2559
1653
|
literal = static_literal_type(left, scope)
|
|
2560
|
-
return [right.name, literal] if trusted_equality_literal?(literal)
|
|
1654
|
+
return [right.name, literal] if Complements.trusted_equality_literal?(literal)
|
|
2561
1655
|
|
|
2562
1656
|
nil
|
|
2563
1657
|
end
|
|
@@ -2788,9 +1882,9 @@ module Rigor
|
|
|
2788
1882
|
# A MULTI-SEGMENT spelling is relative too and gets the same walk (#635). `Type::Nominal`
|
|
2789
1883
|
# written inside `module Rigor` is not "already qualified": Ruby anchors the path's FIRST
|
|
2790
1884
|
# segment through `Module.nesting`, so the constant it names is `Rigor::Type::Nominal`.
|
|
2791
|
-
# Treating any `::` as absolute
|
|
2792
|
-
# knows,
|
|
2793
|
-
# `Dynamic[top]
|
|
1885
|
+
# Treating any `::` as absolute would narrow every such guard to a name no environment
|
|
1886
|
+
# knows, degrading each subsequent reader call on the receiver to
|
|
1887
|
+
# `Dynamic[top]`. Trying the whole path under
|
|
2794
1888
|
# each prefix is the same rule the single-segment case already applies, and it adopts a
|
|
2795
1889
|
# candidate only when the scope actually knows it, so a genuinely absolute spelling still
|
|
2796
1890
|
# falls through to `bare_name` unchanged.
|
|
@@ -2799,11 +1893,9 @@ module Rigor
|
|
|
2799
1893
|
# `class Admin::UsersController` contributes the single entry `["Admin::UsersController"]`, so a
|
|
2800
1894
|
# bare `User` guard there narrows to the top-level `::User` exactly as Ruby resolves it, while
|
|
2801
1895
|
# the nested `module Admin; class UsersController` spelling still reaches `Admin::User`. Peeling
|
|
2802
|
-
# `scope.self_type.class_name`
|
|
2803
|
-
# answered `Admin::User` for both, reporting `undefined method` on correct code across all four
|
|
2804
|
-
# shapes at once. Because the repair lives in the shared derivation, `is_a?`, `case`/`when`, `===`
|
|
2805
|
-
# and the constant typer were fixed together rather than one guard shape at a time. A scope no
|
|
1896
|
+
# `scope.self_type.class_name` cannot tell the two apart (the string is identical), so a scope no
|
|
2806
1897
|
# declaration walk built still falls back to the peel; see {Reflection.lexical_nesting_chain}.
|
|
1898
|
+
# `is_a?`, `case`/`when`, `===` and the constant typer share this derivation.
|
|
2807
1899
|
#
|
|
2808
1900
|
# A path whose head and tail have DIFFERENT owners is the one shape no `<prefix>::<full path>`
|
|
2809
1901
|
# candidate can name (#656), so the whole-path loop falls through to the segment-wise walk
|
|
@@ -2856,14 +1948,15 @@ module Rigor
|
|
|
2856
1948
|
# edge narrows down to `Foo` and the falsey edge subtracts `Foo` from a union.
|
|
2857
1949
|
# - **Range literal receiver** (`(1..10) === x`): the default `Range#===` includes `x`
|
|
2858
1950
|
# iff the endpoints compare it. We conservatively narrow `x` to `Numeric` for
|
|
2859
|
-
# integer-endpoint ranges and to `String` for string-endpoint ranges; other endpoint
|
|
1951
|
+
# integer- or float-endpoint ranges and to `String` for string-endpoint ranges; other endpoint
|
|
2860
1952
|
# types fall through.
|
|
2861
1953
|
# - **Regexp literal receiver** (`/foo/ === x`): the truthy edge narrows `x` as
|
|
2862
1954
|
# {#regexp_case_equality_type} says (its String and Symbol parts, #1955). The falsey edge
|
|
2863
1955
|
# keeps the entry type unchanged because `Regexp#===` returns false for non-Strings AND
|
|
2864
1956
|
# for Strings that simply do not match.
|
|
2865
1957
|
#
|
|
2866
|
-
# Anything else —
|
|
1958
|
+
# Anything else — an argument that is not a local, a single-hop chain or an instance
|
|
1959
|
+
# variable, global or constant read, a dynamic receiver, a custom `===` method — falls
|
|
2867
1960
|
# through to the no-narrowing branch (nil), preserving the entry scope on both edges.
|
|
2868
1961
|
def analyse_case_equality_predicate(node, scope)
|
|
2869
1962
|
return nil if node.arguments.nil?
|
|
@@ -3040,15 +2133,14 @@ module Rigor
|
|
|
3040
2133
|
end
|
|
3041
2134
|
|
|
3042
2135
|
# Slice 4b-1 (ADR-7 § "Slice 4-A/4-B") — single-point RBS::Extended contribution
|
|
3043
|
-
# analyser.
|
|
3044
|
-
# `analyse_rbs_extended_assert_if`) with one path that routes through
|
|
2136
|
+
# analyser. One path routes through
|
|
3045
2137
|
# `RbsExtended.read_flow_contribution` and `Rigor::FlowContribution::Merger.merge`. The
|
|
3046
2138
|
# bundle's `truthy_facts` slot already includes both the `predicate-if-true` and
|
|
3047
|
-
# `assert-if-true` Facts
|
|
2139
|
+
# `assert-if-true` Facts; the
|
|
3048
2140
|
# `falsey_facts` slot mirrors that. The merger composes any future plugin contribution
|
|
3049
2141
|
# at the same site alongside the RBS::Extended bundle without changing this analyser.
|
|
3050
2142
|
#
|
|
3051
|
-
# Conservative envelope
|
|
2143
|
+
# Conservative envelope:
|
|
3052
2144
|
# - Receiver type must be `Type::Nominal`, `Type::Singleton`, or `Type::Constant`.
|
|
3053
2145
|
# - The method must be present in the loader.
|
|
3054
2146
|
# - For each fact, the corresponding positional argument (matched by parameter name in
|
|
@@ -3133,8 +2225,7 @@ module Rigor
|
|
|
3133
2225
|
end
|
|
3134
2226
|
end
|
|
3135
2227
|
|
|
3136
|
-
# Resolves a Fact's target node
|
|
3137
|
-
# reads from the canonical Fact carrier; `:self` routes to the call receiver, otherwise
|
|
2228
|
+
# Resolves a Fact's target node from the canonical Fact carrier; `:self` routes to the call receiver, otherwise
|
|
3138
2229
|
# we look up the matching positional argument by parameter name.
|
|
3139
2230
|
def fact_target_node(fact, call_node, method_def)
|
|
3140
2231
|
if fact.target_kind == :self
|
|
@@ -3257,14 +2348,18 @@ module Rigor
|
|
|
3257
2348
|
return integer_literal_when_result(current, int_literal, falsey_acc)
|
|
3258
2349
|
end
|
|
3259
2350
|
|
|
2351
|
+
symbol = CaseLiterals.symbol_when_result(current, unwrap_parens(condition), falsey_acc)
|
|
2352
|
+
return symbol if symbol
|
|
2353
|
+
|
|
3260
2354
|
if regexp_literal?(condition)
|
|
3261
2355
|
return { truthy: regexp_case_equality_type(current, scope), falsey: falsey_acc, fully_narrowable: true }
|
|
3262
2356
|
end
|
|
3263
2357
|
|
|
3264
2358
|
target = when_condition_class(condition, scope)
|
|
3265
|
-
return
|
|
2359
|
+
return nil unless target
|
|
3266
2360
|
|
|
3267
|
-
|
|
2361
|
+
result = class_when_result(scope, current, target, falsey_acc)
|
|
2362
|
+
CaseLiterals.class_fallback(unwrap_parens(condition), result, falsey_acc)
|
|
3268
2363
|
end
|
|
3269
2364
|
|
|
3270
2365
|
def case_equality_integer_literal(condition)
|
|
@@ -3290,11 +2385,7 @@ module Rigor
|
|
|
3290
2385
|
narrow_integer_comparison(current, :>=, low),
|
|
3291
2386
|
:<=, high
|
|
3292
2387
|
)
|
|
3293
|
-
|
|
3294
|
-
# precisely with a single carrier, so keep the accumulator unchanged.
|
|
3295
|
-
# `fully_narrowable: false` forces the else-branch to see `current` (the unmodified
|
|
3296
|
-
# entry type), which mirrors `between?` falsey behaviour.
|
|
3297
|
-
{ truthy: truthy, falsey: falsey_acc, fully_narrowable: false }
|
|
2388
|
+
{ truthy: truthy, falsey: CaseLiterals.integer_outside_range(falsey_acc, low, high), fully_narrowable: true }
|
|
3298
2389
|
end
|
|
3299
2390
|
|
|
3300
2391
|
def class_when_result(scope, current, target, falsey_acc)
|
|
@@ -3386,7 +2477,7 @@ module Rigor
|
|
|
3386
2477
|
# same walk. A rooted `::Foo` names the top level and never a lexically nearer shadow, so
|
|
3387
2478
|
# it keeps the un-walked spelling. nil for any non-constant shape, as before.
|
|
3388
2479
|
#
|
|
3389
|
-
# PUBLIC,
|
|
2480
|
+
# PUBLIC, unlike nearly every helper in this block (#655): `case`/`when`'s VALUE side lives in
|
|
3390
2481
|
# `ExpressionTyper` and matched on the as-written spelling, so the two halves of one `case`
|
|
3391
2482
|
# disagreed — the flow side narrowed through this walk while the value side resolved the
|
|
3392
2483
|
# pattern against whatever the environment knew under that literal name.
|
|
@@ -3399,280 +2490,6 @@ module Rigor
|
|
|
3399
2490
|
end
|
|
3400
2491
|
public :lexical_class_name
|
|
3401
2492
|
|
|
3402
|
-
# ----- narrow_class / narrow_not_class helpers -----
|
|
3403
|
-
|
|
3404
|
-
# Polarity-aware dispatch table for {.narrow_class} / {.narrow_not_class}. Avoids
|
|
3405
|
-
# duplicating the per-carrier case statement and keeps each public surface a thin
|
|
3406
|
-
# delegate; the per-carrier helpers know which polarity to apply by looking at
|
|
3407
|
-
# `polarity:`.
|
|
3408
|
-
def narrow_class_dispatch(type, class_name, context)
|
|
3409
|
-
case type
|
|
3410
|
-
when Type::Constant then narrow_constant_class(type, class_name, context)
|
|
3411
|
-
when Type::Nominal then narrow_nominal_class(type, class_name, context)
|
|
3412
|
-
when Type::Union then narrow_union_class(type, class_name, context)
|
|
3413
|
-
when Type::Tuple then narrow_shape_class(type, "Array", class_name, context)
|
|
3414
|
-
when Type::HashShape then narrow_shape_class(type, "Hash", class_name, context)
|
|
3415
|
-
when Type::Singleton then narrow_singleton_class(type, class_name, context)
|
|
3416
|
-
else narrow_other_class(type, class_name, context)
|
|
3417
|
-
end
|
|
3418
|
-
end
|
|
3419
|
-
|
|
3420
|
-
def narrow_constant_class(constant, class_name, context)
|
|
3421
|
-
if context.polarity == :positive
|
|
3422
|
-
narrow_constant_to_class(constant, class_name, context)
|
|
3423
|
-
else
|
|
3424
|
-
narrow_constant_not_class(constant, class_name, context)
|
|
3425
|
-
end
|
|
3426
|
-
end
|
|
3427
|
-
|
|
3428
|
-
def narrow_nominal_class(nominal, class_name, context)
|
|
3429
|
-
if context.polarity == :positive
|
|
3430
|
-
narrow_nominal_to_class(nominal, class_name, context)
|
|
3431
|
-
else
|
|
3432
|
-
narrow_nominal_not_class(nominal, class_name, context)
|
|
3433
|
-
end
|
|
3434
|
-
end
|
|
3435
|
-
|
|
3436
|
-
def narrow_union_class(union, class_name, context)
|
|
3437
|
-
Type::Combinator.union(
|
|
3438
|
-
*union.members.map { |member| narrow_class_dispatch(member, class_name, context) }
|
|
3439
|
-
)
|
|
3440
|
-
end
|
|
3441
|
-
|
|
3442
|
-
def narrow_shape_class(shape, projected_class, class_name, context)
|
|
3443
|
-
if context.polarity == :positive
|
|
3444
|
-
narrow_shape_to_class(shape, projected_class, class_name, context)
|
|
3445
|
-
else
|
|
3446
|
-
narrow_shape_not_class(shape, projected_class, class_name, context)
|
|
3447
|
-
end
|
|
3448
|
-
end
|
|
3449
|
-
|
|
3450
|
-
def narrow_singleton_class(singleton, class_name, context)
|
|
3451
|
-
if context.polarity == :positive
|
|
3452
|
-
narrow_singleton_to_class(singleton, class_name, context)
|
|
3453
|
-
else
|
|
3454
|
-
narrow_singleton_not_class(singleton, class_name, context)
|
|
3455
|
-
end
|
|
3456
|
-
end
|
|
3457
|
-
|
|
3458
|
-
def narrow_other_class(type, class_name, context)
|
|
3459
|
-
context.polarity == :positive ? narrow_class_other(type, class_name) : type
|
|
3460
|
-
end
|
|
3461
|
-
|
|
3462
|
-
# Issue #657 — `Bot` is the assertion "this can never match", and only a KNOWN-disjoint ordering
|
|
3463
|
-
# supports it (the reason {#declines_bot?} exists, and the repro it was written against):
|
|
3464
|
-
#
|
|
3465
|
-
# module Taggable; def tag = "t"; end
|
|
3466
|
-
# class Integer; include Taggable; end
|
|
3467
|
-
# x = 1
|
|
3468
|
-
# case x
|
|
3469
|
-
# when Taggable then x.tag # reported unreachable; `1.is_a?(Taggable)` is true
|
|
3470
|
-
# end
|
|
3471
|
-
#
|
|
3472
|
-
# The ordering is `:unknown` because an in-source `include` into a CORE class never reaches the
|
|
3473
|
-
# environment's class ordering — the include is discovered in project source, `Integer`'s ancestry
|
|
3474
|
-
# comes from RBS, and the two are not joined (the precision half, left in #657).
|
|
3475
|
-
#
|
|
3476
|
-
# `:unknown` joins to Dynamic, the same answer {#narrow_nominal_to_class} gives and for the same
|
|
3477
|
-
# reason: keeping the old bound licenses `call.undefined-method` on the guard-proven branch. Keeping
|
|
3478
|
-
# the literal was tried first and did exactly that — `s = "str"` under `when Unrelated` stayed
|
|
3479
|
-
# `Constant["str"]` and reported `undefined method 'other'`, trading one false positive for
|
|
3480
|
-
# another. The value is lost only on the guarded arm, which is where the guard just told us the
|
|
3481
|
-
# environment's picture was incomplete.
|
|
3482
|
-
def narrow_constant_to_class(constant, class_name, context)
|
|
3483
|
-
rigor_class = constant.value.class.name
|
|
3484
|
-
return constant if subclass_of?(rigor_class, class_name, context)
|
|
3485
|
-
return Type::Combinator.untyped if declines_bot?(rigor_class, class_name, context)
|
|
3486
|
-
|
|
3487
|
-
Type::Combinator.bot
|
|
3488
|
-
end
|
|
3489
|
-
|
|
3490
|
-
def narrow_constant_not_class(constant, class_name, context)
|
|
3491
|
-
rigor_class = constant.value.class.name
|
|
3492
|
-
subclass_of?(rigor_class, class_name, context) ? Type::Combinator.bot : constant
|
|
3493
|
-
end
|
|
3494
|
-
|
|
3495
|
-
# Narrow a Nominal under `is_a?(class_name)`: when the nominal's class is already a
|
|
3496
|
-
# subclass of `class_name` (or matches under `exact: true`) preserve it; when
|
|
3497
|
-
# `class_name` is a subclass of the nominal's class (`Nominal[Numeric]` under
|
|
3498
|
-
# `is_a?(Integer)`) narrow DOWN to `Nominal[class_name]`; otherwise (disjoint
|
|
3499
|
-
# hierarchies under `is_a?`, mismatch under `instance_of?`) collapse to `Bot`.
|
|
3500
|
-
# Conservative when the analyzer environment cannot resolve either class.
|
|
3501
|
-
#
|
|
3502
|
-
# A disjoint `Bot` narrows the edge but is not always a verdict that the code is dead: see
|
|
3503
|
-
# {.disjoint_nominal_guard?} (issue #1429).
|
|
3504
|
-
def narrow_nominal_to_class(nominal, class_name, context)
|
|
3505
|
-
return nominal if nominal.class_name == class_name
|
|
3506
|
-
return exact_nominal_to_class(nominal, class_name, context) if context.exact
|
|
3507
|
-
|
|
3508
|
-
case class_ordering(nominal.class_name, class_name, context)
|
|
3509
|
-
when :superclass then Type::Combinator.nominal_of(class_name)
|
|
3510
|
-
when :disjoint then Type::Combinator.bot
|
|
3511
|
-
when :subclass then nominal
|
|
3512
|
-
else
|
|
3513
|
-
# :unknown — the guard proved membership in a class the environment cannot order against the
|
|
3514
|
-
# bound (an RBS-less gem class, typically). Keeping the old bound licenses `undefined-method`
|
|
3515
|
-
# on the guard-proven branch (concurrent-ruby's `done.is_a?(Concurrent::Maybe)` kept `Array`
|
|
3516
|
-
# and fired on `.value`); the honest join is Dynamic — the guard destroyed the old knowledge.
|
|
3517
|
-
Type::Combinator.untyped
|
|
3518
|
-
end
|
|
3519
|
-
end
|
|
3520
|
-
|
|
3521
|
-
# `instance_of?(class_name)` compares the exact class, so its truthy edge holds `class_name` itself. Issue
|
|
3522
|
-
# #1429: when `class_name` is a class below the nominal's (`Numeric` under `instance_of?(Integer)`), that is
|
|
3523
|
-
# the edge's type; the edge was `Bot` before, for every class but the nominal's own. A module is never an
|
|
3524
|
-
# object's class, and any other ordering leaves no class the object can have, so those stay `Bot`.
|
|
3525
|
-
def exact_nominal_to_class(nominal, class_name, context)
|
|
3526
|
-
return Type::Combinator.bot if context.environment.rbs_module?(class_name)
|
|
3527
|
-
return Type::Combinator.bot unless class_ordering(nominal.class_name, class_name, context) == :superclass
|
|
3528
|
-
|
|
3529
|
-
Type::Combinator.nominal_of(class_name)
|
|
3530
|
-
end
|
|
3531
|
-
|
|
3532
|
-
def narrow_nominal_not_class(nominal, class_name, context)
|
|
3533
|
-
return Type::Combinator.bot if nominal.class_name == class_name
|
|
3534
|
-
return nominal if context.exact
|
|
3535
|
-
|
|
3536
|
-
ordering = class_ordering(nominal.class_name, class_name, context)
|
|
3537
|
-
case ordering
|
|
3538
|
-
when :subclass then Type::Combinator.bot
|
|
3539
|
-
else nominal
|
|
3540
|
-
end
|
|
3541
|
-
end
|
|
3542
|
-
|
|
3543
|
-
# A shape projects to `Array` / `Hash`, and reopening either of those to include a project module
|
|
3544
|
-
# leaves the ordering `:unknown` for the same reason #657's `Integer` does — so `[1, 2]` under `when
|
|
3545
|
-
# Taggable` reached the same unsupported `Bot`. The carrier differs; the missing evidence does not.
|
|
3546
|
-
def narrow_shape_to_class(shape, projected_class, class_name, context)
|
|
3547
|
-
return shape if subclass_of?(projected_class, class_name, context)
|
|
3548
|
-
return Type::Combinator.untyped if declines_bot?(projected_class, class_name, context)
|
|
3549
|
-
|
|
3550
|
-
Type::Combinator.bot
|
|
3551
|
-
end
|
|
3552
|
-
|
|
3553
|
-
def narrow_shape_not_class(shape, projected_class, class_name, context)
|
|
3554
|
-
subclass_of?(projected_class, class_name, context) ? Type::Combinator.bot : shape
|
|
3555
|
-
end
|
|
3556
|
-
|
|
3557
|
-
# `Singleton[Foo]` is the *class object* `Foo`, an instance of `Class` (which is a
|
|
3558
|
-
# subclass of `Module`). Asking `Foo.is_a?(Class)` returns true; `Foo.is_a?(Foo)`
|
|
3559
|
-
# returns false unless `Foo` is `Class` itself. We approximate this by treating
|
|
3560
|
-
# singletons uniformly as `Class` instances.
|
|
3561
|
-
#
|
|
3562
|
-
# The approximation answers through `Class`, so a target `Class` cannot be ordered against — a
|
|
3563
|
-
# project module, typically — lands on the same `:unknown` #657 collapsed to `Bot`, and `case Widget
|
|
3564
|
-
# when Meta` was reported unreachable where `Widget` had `extend Meta`.
|
|
3565
|
-
#
|
|
3566
|
-
# Issue #898 — that decline is silent about a target the environment DOES order against `Class`, and
|
|
3567
|
-
# `extend Comparable` is one: both names are in RBS, neither includes the other, so the ordering says
|
|
3568
|
-
# `:disjoint` and the collapse fired on an arm MRI takes (`Widget.is_a?(Comparable)` is true — the
|
|
3569
|
-
# `extend` put `Comparable` in `Widget`'s SINGLETON ancestry, which asking about `Class` cannot see).
|
|
3570
|
-
# {#singleton_extend_declines_bot?} supplies the missing half from the project's own `extend` record.
|
|
3571
|
-
def narrow_singleton_to_class(singleton, class_name, context)
|
|
3572
|
-
return singleton if subclass_of?("Class", class_name, context)
|
|
3573
|
-
return Type::Combinator.untyped if declines_bot?("Class", class_name, context)
|
|
3574
|
-
return Type::Combinator.untyped if singleton_extend_declines_bot?(singleton, class_name, context)
|
|
3575
|
-
|
|
3576
|
-
Type::Combinator.bot
|
|
3577
|
-
end
|
|
3578
|
-
|
|
3579
|
-
# Whether the class object's own `extend` record forbids the `Bot` the `Class` approximation would
|
|
3580
|
-
# otherwise authorise (#898). An extended `M` puts every ancestor of `M` in the singleton's
|
|
3581
|
-
# ancestry, so the question per extended module is `M <= target`: `:equal` (`extend Comparable`
|
|
3582
|
-
# under `when Comparable`) and `:subclass` support the arm, and `:unknown` (`extend Meta` under
|
|
3583
|
-
# `when Comparable`, `Meta` being project source the environment never joined to RBS) cannot rule
|
|
3584
|
-
# it out. `:superclass` and `:disjoint` support NOTHING and must keep the collapse — `Integer`
|
|
3585
|
-
# under `when Integer` orders `:superclass` against an extended `Comparable`, and reading that as
|
|
3586
|
-
# support would retract `case Widget when Integer` from every class that extends a core module.
|
|
3587
|
-
#
|
|
3588
|
-
# The answer is `Dynamic[top]`, never the preserved `Singleton[C]`, and the negative edge
|
|
3589
|
-
# ({#narrow_singleton_not_class}) is untouched — so this record is read ONLY to withhold a claim,
|
|
3590
|
-
# never to make one. That asymmetry is deliberate, and it is what keeps the fix honest against an
|
|
3591
|
-
# extend record that is structurally incomplete: `Widget.extend(m)` at a call site and an `extend`
|
|
3592
|
-
# written in a file outside the analysed set are both invisible to the walk behind
|
|
3593
|
-
# {Scope#singleton_extends_of}, and the declaration-side record joined to it below is no more
|
|
3594
|
-
# complete — RBS says nothing about a class it does not declare. A shape that turned this table
|
|
3595
|
-
# into a positive `:subclass` verdict would have to be right about ABSENCE too, and it cannot be. Preserving
|
|
3596
|
-
# `Singleton[C]` would also hand the arm a receiver whose singleton method table has no `clamp` on
|
|
3597
|
-
# it, trading the unreachable-clause false positive for an undefined-method one — the same trap
|
|
3598
|
-
# #657 recorded on the `Constant` carrier.
|
|
3599
|
-
#
|
|
3600
|
-
# Issue #915 — two sources, read as one. The source record above sees only what Ruby source spells;
|
|
3601
|
-
# a project that declares `extend M` in its own `sig/` puts the same module in the same singleton
|
|
3602
|
-
# ancestry, and the environment's declaration-side answer
|
|
3603
|
-
# ({Environment#singleton_extended_modules}) is consulted alongside it. Neither is asked to be
|
|
3604
|
-
# complete; each can only add a module that IS there, which is all the one-directional read needs.
|
|
3605
|
-
def singleton_extend_declines_bot?(singleton, class_name, context)
|
|
3606
|
-
return false if context.exact
|
|
3607
|
-
|
|
3608
|
-
extended = singleton_ancestor_modules(singleton, context)
|
|
3609
|
-
return false if extended.empty?
|
|
3610
|
-
|
|
3611
|
-
extended.any? do |module_name|
|
|
3612
|
-
%i[equal subclass unknown].include?(class_ordering(module_name, class_name, context))
|
|
3613
|
-
end
|
|
3614
|
-
end
|
|
3615
|
-
|
|
3616
|
-
def singleton_ancestor_modules(singleton, context)
|
|
3617
|
-
scope = context.scope
|
|
3618
|
-
from_source = scope.nil? ? [] : scope.singleton_extends_of(singleton.class_name)
|
|
3619
|
-
from_rbs = context.environment&.singleton_extended_modules(singleton.class_name) || []
|
|
3620
|
-
return from_source if from_rbs.empty?
|
|
3621
|
-
return from_rbs if from_source.empty?
|
|
3622
|
-
|
|
3623
|
-
from_source | from_rbs
|
|
3624
|
-
end
|
|
3625
|
-
|
|
3626
|
-
def narrow_singleton_not_class(singleton, class_name, context)
|
|
3627
|
-
subclass_of?("Class", class_name, context) ? Type::Combinator.bot : singleton
|
|
3628
|
-
end
|
|
3629
|
-
|
|
3630
|
-
# Top/Dynamic narrow to `Nominal[class_name]` so dispatch can resolve through the asked
|
|
3631
|
-
# class; Bot stays Bot.
|
|
3632
|
-
def narrow_class_other(type, class_name)
|
|
3633
|
-
case type
|
|
3634
|
-
when Type::Dynamic, Type::Top then Type::Combinator.nominal_of(class_name)
|
|
3635
|
-
else type
|
|
3636
|
-
end
|
|
3637
|
-
end
|
|
3638
|
-
|
|
3639
|
-
# Whether the positive edge must decline the `Bot` that `subclass_of?` returning false would
|
|
3640
|
-
# otherwise authorise (#657). `subclass_of?` folds `:disjoint` and `:unknown` into one `false`, and
|
|
3641
|
-
# only the first is evidence that the guard can never match; the second is the environment saying it
|
|
3642
|
-
# cannot tell, which any file reopening either side can make true.
|
|
3643
|
-
#
|
|
3644
|
-
# This only ever ADDS a decline: every caller keeps its own `subclass_of?` test first, so the
|
|
3645
|
-
# preserving edge is untouched, and a `:disjoint`, `:equal`, `:subclass` or `:superclass` verdict
|
|
3646
|
-
# never reaches here. There is no ordering it turns INTO a match — the honest answer for
|
|
3647
|
-
# "cannot tell" is Dynamic, never the asked class.
|
|
3648
|
-
#
|
|
3649
|
-
# `instance_of?` is exempt because it compares the exact class: a module is never an object's class,
|
|
3650
|
-
# so that edge's `Bot` rests on the guard's own semantics rather than on the ordering.
|
|
3651
|
-
def declines_bot?(rigor_class_name, target_class_name, context)
|
|
3652
|
-
!context.exact && class_ordering(rigor_class_name, target_class_name, context) == :unknown
|
|
3653
|
-
end
|
|
3654
|
-
|
|
3655
|
-
# Returns `true` when an instance of `rigor_class_name` satisfies
|
|
3656
|
-
# `is_a?(target_class_name)` (or `instance_of?(target_class_name)` when `exact: true`).
|
|
3657
|
-
# Falls back to the safe `false` when either name does not resolve through the analyzer
|
|
3658
|
-
# environment.
|
|
3659
|
-
def subclass_of?(rigor_class_name, target_class_name, context)
|
|
3660
|
-
return rigor_class_name == target_class_name if context.exact
|
|
3661
|
-
|
|
3662
|
-
%i[subclass equal].include?(
|
|
3663
|
-
class_ordering(rigor_class_name, target_class_name, context)
|
|
3664
|
-
)
|
|
3665
|
-
end
|
|
3666
|
-
|
|
3667
|
-
# Compares two class names through the analyzer environment. Returns `:equal` when they
|
|
3668
|
-
# resolve to the same class, `:subclass` when `lhs <= rhs`, `:superclass` when `rhs <=
|
|
3669
|
-
# lhs`, `:disjoint` when neither, and `:unknown` when either name does not resolve.
|
|
3670
|
-
def class_ordering(lhs, rhs, context)
|
|
3671
|
-
return :equal if lhs == rhs
|
|
3672
|
-
|
|
3673
|
-
context.environment.class_ordering(lhs, rhs)
|
|
3674
|
-
end
|
|
3675
|
-
|
|
3676
2493
|
# `x.nil?` on a local, instance variable, global or constant (#1429) read.
|
|
3677
2494
|
def analyse_nil_predicate(receiver, scope)
|
|
3678
2495
|
slot = receiver_slot(receiver, scope)
|