Skip to content

✨ feat: add type predicate-based narrowing for union types in if/elif branches#1367

Merged
harehare merged 8 commits intomainfrom
feat/type-narrowing
Mar 1, 2026
Merged

✨ feat: add type predicate-based narrowing for union types in if/elif branches#1367
harehare merged 8 commits intomainfrom
feat/type-narrowing

Conversation

@harehare
Copy link
Copy Markdown
Owner

@harehare 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
@harehare harehare changed the title feat(type-narrowing): add type predicate-based narrowing for union types in if/elif branches feat add type predicate-based narrowing for union types in if/elif branches Mar 1, 2026
@harehare harehare changed the title feat add type predicate-based narrowing for union types in if/elif branches ✨ feat: add type predicate-based narrowing for union types in if/elif branches Mar 1, 2026
Base automatically changed from fix/typechecker-undefined-field-error to main March 1, 2026 04:41
@harehare harehare requested a review from Copilot March 1, 2026 06:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Ref symbols within branch subtrees.
  • Introduce TypeNarrowing/NarrowingEntry storage in the inference context and add Type::subtract to 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.

Copilot AI review requested due to automatic review settings March 1, 2026 08:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Copilot AI review requested due to automatic review settings March 1, 2026 12:37
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 1, 2026

Merging this PR will not alter performance

✅ 29 untouched benchmarks


Comparing feat/type-narrowing (b829496) with main (06c2b02)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (76e5ed6) during the generation of this report, so 06c2b02 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

@harehare harehare merged commit 460ce4e into main Mar 1, 2026
7 checks passed
@harehare harehare deleted the feat/type-narrowing branch March 1, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants