Skip to content

Implement flow-sensitive SELECT narrowing for TYPEOF guards in AND expressions#14

Merged
starseeker merged 7 commits intotreatfrom
copilot/implement-flow-sensitive-narrowing
Feb 7, 2026
Merged

Implement flow-sensitive SELECT narrowing for TYPEOF guards in AND expressions#14
starseeker merged 7 commits intotreatfrom
copilot/implement-flow-sensitive-narrowing

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 6, 2026

Flow-Sensitive Narrowing Implementation - COMPLETE ✓

  • 1. Add refinement context structure to track type narrowing
  • 2. Implement pattern matching for TYPEOF guards
  • 3. Implement refinement collection from AND conjuncts
  • 4. Modify operator resolution for AND
  • 5. Apply refinements during identifier resolution
  • 6. Validate type narrowing safety
  • 7. Create test schemas
  • 8. Run tests and verify
  • 9. Code cleanup
    • Remove debug output
    • Update .gitignore for generated files
    • Remove accidentally committed build artifacts (root level)
    • Remove 5,622 generated entity/ and type/ files
    • Remove generated schema.h file
    • Address code review feedback (NULL check, named constant)

Implementation complete! Flow-sensitive narrowing allows QUERY expressions on SELECT-typed variables when guarded by TYPEOF checks in AND expressions.

Changes Summary:

  • Core Implementation: 3 files modified (resolve.h, resolve.c, expr.c)
  • Test Schema: 1 file added (flow_narrowing_test.exp)
  • Cleanup: Removed 5,623 generated files from git tracking

Acceptance Criteria Met:

✅ AP242 schema parses without PE056 errors
✅ No unsafe QUERY acceptance
✅ No OR/NOT refinement leakage
✅ Nested AND support
✅ All build artifacts excluded from repository

Original prompt

PR 3 — Option B flow-sensitive narrowing (conservative and correct)

Goal: Make the AP242 pattern work without rewriting schemas, by narrowing SELECT variables in the RHS of an AND when guarded by ('' IN TYPEOF(var)).

This PR must avoid the earlier concerns: no OR/NOT leakage, robust TYPEOF detection, robust string literal detection, no huge ABI churn if possible.
Agent input

Implement flow-sensitive narrowing with these strict rules:

Only apply narrowing for AND:

For expression A AND B, resolve A normally.
Collect refinements from A only from conjuncts that are guaranteed true if B executes.
    Implement collect_refinements_from_conjunction(A):
        if node is AND(x,y): recurse into both
        else if node matches predicate pattern: extract refinement
        else: do not recurse further
    Do not collect refinements from subexpressions under OR, NOT, function calls, etc.

Refinement predicate pattern (minimum viable):

Match ('<TypeNameString>' IN TYPEOF(<identifier>))
Make matching robust based on actual AST:
    Do not rely on expr->symbol.name == "TYPEOF" unless that is confirmed correct.
    Use the same resolved builtin object / token kind TYPEOF uses in this codebase.
    Ensure string literal detection uses the literal node kind, not “has string type”.

Type safety:

Only refine if the variable’s static type is a SELECT and the target type is a member (directly or via nested SELECT).
Type name lookup must support schema-qualified names in the string literal:
    'SCHEMA.TYPE' → try TYPE in current schema if full lookup fails
    case-insensitivity is ok but be careful not to hide errors; use normalization rather than “guessing”.

Application:

When resolving an identifier expression for that variable inside B, use the refined type for return_type.
Scope:
    refinements apply only while resolving B (RHS of that AND)
    nested ANDs inside B should inherit refinements (since they are still in the RHS execution path)

Implementation approach:

Prefer minimal invasive changes:
    If feasible, don’t change every operator resolver signature.
    Special-case OP_AND in EXP_resolve or wherever operator dispatch happens, passing a context into recursive resolves only where needed.

Tests:

Add a small schema that mirrors AP242:
    TYPE set_representation_item = SET OF INTEGER;
    TYPE target = SELECT(set_representation_item, INTEGER);
    ENTITY e; item: target; WHERE wr: ('SET_REPRESENTATION_ITEM' IN TYPEOF(item)) AND SIZEOF(QUERY(x <* item | x > 0)) >= 0; END_ENTITY;
Add a nested AND test to ensure refinements flow into nested RHS.
Do not add “must fail” tests unless the harness supports them.

Acceptance criteria:

data/ap242/242_mim_lf.exp parses/resolves past styled_item without PE056.
No new acceptance of QUERY-on-SELECT without a guard/cast.
No refinement applied under OR guards.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits February 7, 2026 00:06
…ookup)

Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Copilot AI changed the title [WIP] Add flow-sensitive narrowing for AP242 pattern Implement flow-sensitive SELECT narrowing for TYPEOF guards in AND expressions Feb 7, 2026
Copilot AI requested a review from starseeker February 7, 2026 00:17
@starseeker
Copy link
Copy Markdown
Owner

@copilot there is an entity folder with a massive number of files - please clean this up.

These files were accidentally committed in 6d1bc20. They are build artifacts
from exp2cxx schema compilation and should not be in the repository.
The directories are already in .gitignore.

Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Feb 7, 2026

@copilot there is an entity folder with a massive number of files - please clean this up.

Cleaned up in commit 6a62686. Removed 5,622 generated files (4,818 entity/ + 804 type/) that were accidentally committed. The directories remain in .gitignore to prevent future commits.

@starseeker
Copy link
Copy Markdown
Owner

@copilot we still have a schema.h in the repository that says it was generated by exp2cxx

This file was generated by exp2cxx in commit 6d1bc20 and should not be
tracked in the repository.

Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Feb 7, 2026

@copilot we still have a schema.h in the repository that says it was generated by exp2cxx

Removed in commit f202ded. The schema.h file was generated by exp2cxx and has been removed from tracking. Also added /schema.h to .gitignore to prevent future commits.

@starseeker starseeker marked this pull request as ready for review February 7, 2026 00:29
@starseeker starseeker merged commit f4345e9 into treat Feb 7, 2026
@starseeker starseeker deleted the copilot/implement-flow-sensitive-narrowing branch February 7, 2026 00:31
starseeker added a commit that referenced this pull request Feb 9, 2026
…pressions (#14)

Implement flow-sensitive narrowing - ap242 schema can now be read

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: starseeker <238416+starseeker@users.noreply.github.com>
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