✨ feat: add type predicate-based narrowing for union types in if/elif branches#1367
Merged
✨ feat: add type predicate-based narrowing for union types in if/elif branches#1367
Conversation
Owner
harehare
commented
Mar 1, 2026
- Implements ConditionNarrowings and TypeNarrowing tracking in constraint generation
- Applies narrowed types to Ref symbols in then/else branches after unification
- Supports is_string, is_number, is_bool, is_none, is_array, is_dict predicates
- Handles negation, logical AND/OR, and compound conditions
- Adds integration tests for type narrowing behavior
…pes in if/elif branches - Implements ConditionNarrowings and TypeNarrowing tracking in constraint generation - Applies narrowed types to Ref symbols in then/else branches after unification - Supports is_string, is_number, is_bool, is_none, is_array, is_dict predicates - Handles negation, logical AND/OR, and compound conditions - Adds integration tests for type narrowing behavior
Contributor
There was a problem hiding this comment.
Pull request overview
Adds type predicate-based narrowing to mq-typechecker so union-typed variables can be treated as more specific types within if/elif branch scopes after unification, enabling better operator/builtin checking behind runtime guards.
Changes:
- Track predicate-derived narrowings during constraint generation and apply them post-unification to
Refsymbols within branch subtrees. - Introduce
TypeNarrowing/NarrowingEntrystorage in the inference context and addType::subtractto compute complement types. - Add integration tests exercising basic narrowing scenarios (predicates, negation,
&&, and union narrowing across branches).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/mq-typechecker/src/constraint.rs | Extracts predicate narrowings from condition expressions and records them for later application. |
| crates/mq-typechecker/src/infer.rs | Adds data structures and context plumbing to store collected narrowings. |
| crates/mq-typechecker/src/lib.rs | Applies recorded narrowings post-unification by overriding Ref symbol types in branch scopes. |
| crates/mq-typechecker/src/types.rs | Adds Type::subtract to compute complement types for union narrowing. |
| crates/mq-typechecker/tests/integration_test.rs | Adds integration tests for the narrowing behavior. |
…nd match patterns
…narrowing analysis
…and improve error handling
… builtin preloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.