Skip to content

fix(flow): avoid unresolved global replay hang - #1072

Merged
CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:fix/issue-1071-global-flow
May 12, 2026
Merged

CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:fix/issue-1071-global-flow

Conversation

@lewis6991

@lewis6991 lewis6991 commented May 12, 2026 •

Copy link
Copy Markdown
Collaborator

skip flow walking unresolved global queries so the existing global unresolve pass can handle them

Resolves #1071

@github-actions

Copy link
Copy Markdown

Code Review

Issues Found:

  1. Potential infinite recursion risk (get_type_at_flow.rs):

    • The new code calls get_var_ref_type() for unresolved globals, but this function might recursively call back into get_type_at_flow, potentially causing infinite recursion or stack overflow.
    • The early return pattern could bypass the CacheEntry::Ready insertion below, leaving the cache in an inconsistent state.
  2. Missing error handling (get_type_at_flow.rs):

    • The ? operator on get_var_ref_type() will propagate errors, but the calling context may not handle InferFailReason properly for this new code path.
  3. Test coverage concern (test.rs):

    • The test only checks that c is typed as any, but doesn't verify that the performance issue (repeated member access on any types) is actually fixed. A performance regression test would be more appropriate.

Recommendations:

  1. Add recursion guard in get_type_at_flow.rs:
// Add a recursion depth check or visited set
if self.recursion_depth > MAX_RECURSION_DEPTH {
    return Err(InferFailReason::RecursiveInfer);
}
  1. Ensure cache consistency:
if query.var_ref_id.get_decl_id_ref()
    .and_then(|decl_id| self.db.get_decl_index().get_decl(&decl_id))
    .is_some_and(|decl| decl.is_global())
{
    let result = get_var_ref_type(self.db, self.cache, &query.var_ref_id);
    // Still need to insert cache entry even if result is error
    get_flow_var_cache(self.cache, query.var_cache_idx)
        .type_cache
        .insert(type_cache_key, CacheEntry::Ready);
    result?;
}
  1. Add performance benchmark test rather than just type assertion, or at minimum add a comment explaining the performance rationale in the test.

  2. Consider adding a comment explaining why globals need special handling during indexing to prevent future confusion.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses performance issues and potential hangs during type inference by handling unresolved globals more efficiently in the flow-type engine. It also adds a regression test for issue #1071 involving complex arithmetic operations. Feedback was provided to optimize the global check by returning early, which would skip the flow walk entirely and prevent redundant sub-queries in deep assignment chains.

@lewis6991
lewis6991 marked this pull request as draft May 12, 2026 09:54
Unresolved globals are handled by the global unresolve pass. Avoid
walking their flow queries during indexing so repeated self-assignments
do not scan the whole assignment chain before the global type cache is
resolved.

Resolves EmmyLuaLs#1071

Assisted-by: Codex
@lewis6991
lewis6991 force-pushed the fix/issue-1071-global-flow branch from 28c1bfd to afe9127 Compare May 12, 2026 10:02
@lewis6991
lewis6991 marked this pull request as ready for review May 12, 2026 10:04
@CppCXY

CppCXY commented May 12, 2026

Copy link
Copy Markdown
Member

/review

@github-actions

Copy link
Copy Markdown

Code Review

Issues Found:

  1. Potential Performance Concern (Line 503-507 in get_type_at_flow.rs):

    • The infer_global_type call is made inside a hot path (type inference flow). While it probes the global cache, repeated calls for the same global variable could still add overhead.
    • Suggestion: Consider adding a check to see if the global type has already been inferred before calling infer_global_type, or ensure the function itself has an early return if already resolved.
  2. Error Handling (Line 505):

    • The ? operator propagates errors from infer_global_type, which could cause the entire flow inference to fail if the global type inference fails.
    • Suggestion: Consider whether a failure to infer the global type should be a hard error or if it should be handled more gracefully (e.g., logging and continuing).
  3. Test Coverage (test.rs):

    • The test test_issue_1071_repeated_any_member_arithmetic_loads is very long and repetitive. While it tests the specific issue, it may be difficult to maintain.
    • Suggestion: Consider using a loop or generating the test cases programmatically to reduce repetition while still testing the same behavior.
  4. Code Style (test.rs):

    • The test uses r#"..."# raw strings with inconsistent indentation inside the function body.
    • Suggestion: Maintain consistent indentation within the raw string for better readability.

Recommendations:

  1. Add a cache check before calling infer_global_type to avoid redundant work:

    if let Some(decl_id) = query.var_ref_id.get_decl_id_ref()
        && let Some(decl) = self.db.get_decl_index().get_decl(&decl_id)
        && decl.is_global()
        && !self.db.is_global_type_inferred(decl.get_name()) // hypothetical check
    {
        infer_global_type(self.db, decl.get_name())?;
    }
  2. Consider error handling strategy - decide if a global inference failure should be fatal or if the flow should continue with the current type.

  3. Simplify the test by using a helper function or loop to generate the repetitive arithmetic expressions.

  4. Add a comment explaining why the global type inference is needed at this point (the PR description mentions "Probe the global cache only" - this should be documented in the code).

@lewis6991

Copy link
Copy Markdown
Collaborator Author

I'm not sure these copilot reviews are any good. The gemini ones are much better:

My triage:

  1. Bot perf concern on infer_global_type: not compelling. The call reads the global/type indexes; it is already the cache probe. Adding a hypothetical “already inferred” API would
    widen the fix for little value.
  2. Bot error-handling concern: also not compelling. The ? is intentional. If the global type is unresolved, we want to leave flow before inserting CacheEntry::Ready and let the
    unresolve pass handle it.
  3. Bot test-length concern: mostly noise. The long repeated assignments are the regression shape.
  4. Actual issue found while checking PR state: CI is failing Spell Check with Typos because the test has f_abd345; typos suggests abd is a misspelling. That should be fixed by
    renaming that fake field to something neutral.

So: I would ignore the bot’s architecture suggestions, but fix the typo check failure.

@CppCXY

CppCXY commented May 12, 2026 •

Copy link
Copy Markdown
Member

I'm not sure these copilot reviews are any good. The gemini ones are much better:

My triage:

  1. Bot perf concern on infer_global_type: not compelling. The call reads the global/type indexes; it is already the cache probe. Adding a hypothetical “already inferred” API would
    widen the fix for little value.
  2. Bot error-handling concern: also not compelling. The ? is intentional. If the global type is unresolved, we want to leave flow before inserting CacheEntry::Ready and let the
    unresolve pass handle it.
  3. Bot test-length concern: mostly noise. The long repeated assignments are the regression shape.
  4. Actual issue found while checking PR state: CI is failing Spell Check with Typos because the test has f_abd345; typos suggests abd is a misspelling. That should be fixed by
    renaming that fake field to something neutral.

So: I would ignore the bot’s architecture suggestions, but fix the typo check failure.

Not Copilot, just a simple check using the flash version of DeepSeek. That it's very cheap. Currently, the average cost per review is only 0.001$.

@CppCXY
CppCXY merged commit c4525ea into EmmyLuaLs:main May 12, 2026
15 of 16 checks passed
@lewis6991
lewis6991 deleted the fix/issue-1071-global-flow branch May 12, 2026 11:33
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.

Stuck loading

2 participants