Skip to content

Add CFA regression test for while loop with continue statements #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 30, 2025

This PR adds a regression test for Control Flow Analysis (CFA) behavior in while loops with continue statements, addressing issue #987.

The test captures a specific scenario where our TypeScript Go implementation's control flow analysis needed to correctly track variable nullability through while loops containing continue statements:

class A {
  next: A | null = null;
  constructor(readonly children: (A | null)[]) {}
}

function getNodes(): A[] {
  const out: A[] = [];
  let current: A | null = new A([]);

  while (current !== null) {
    let firstChild = null;

    // Critical CFA test case: continue statement after null check
    if (out.length) {
      current = current.next;
      continue;
    }

    // After the continue, current should still be known to be non-null
    for (let i = 0; i < current.children.length; i++) {
      const child = current.children[i];
      if (child) {
        if (!firstChild) {
          firstChild = child;
          firstChild.next = current.next;
        }
        child.next = current.next;
      }
    }

    current = firstChild || current.next;
  }

  return out;
}

The test verifies that:

  • After the while (current !== null) condition, current is correctly narrowed from A | null to A
  • This narrowing is preserved through the continue statement
  • All subsequent references to current within the loop body recognize it as non-null

The generated baselines confirm that our CFA implementation correctly handles this complex control flow pattern, ensuring consistency with the stable TypeScript compiler behavior.

Fixes #1324.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add CFA test from issue Add CFA regression test for while loop with continue statements Jun 30, 2025
@Copilot Copilot AI requested a review from DanielRosenwasser June 30, 2025 20:05
Copilot finished work on behalf of DanielRosenwasser June 30, 2025 20:05
@DanielRosenwasser DanielRosenwasser marked this pull request as ready for review June 30, 2025 22:29
…ts and accept baselines

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Copilot finished work on behalf of DanielRosenwasser June 30, 2025 22:47
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.

Add CFA test from issue
3 participants