Skip to content

fix(type): canonicalize identical callable unions - #1022

Merged
CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:topic/issue-1020-callable-unions
Apr 7, 2026
Merged

CppCXY merged 1 commit into
EmmyLuaLs:mainfrom
lewis6991:topic/issue-1020-callable-unions

Conversation

@lewis6991

Copy link
Copy Markdown
Collaborator

Summary

  • deduplicate semantically identical callable members when building unions
  • keep v.func from pairs(t) from reaching pcall as a raw callable union
  • add the exact pcall can not infer correct #1020 regression

Testing

  • cargo test -p emmylua_code_analysis pcall_test
  • cargo test -p emmylua_code_analysis member_infer_test
  • cargo test -p emmylua_code_analysis for_range_var_infer_test

Fixes #1020

@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 improves the union type merging logic by introducing a more comprehensive equality check that accounts for callable types, such as function signatures and doc functions. It also includes a new test case to verify that pcall correctly preserves return types within loops. The review feedback highlights several performance optimization opportunities, including pre-resolving callable types during union construction to minimize redundant lookups and adding a fast-path comparison for identical unions to avoid unnecessary O(N^2) operations.

Deduplicate semantically identical callable members when building
unions.

Fixes EmmyLuaLs#1020
@lewis6991
lewis6991 force-pushed the topic/issue-1020-callable-unions branch from 8200729 to 8d0a469 Compare April 7, 2026 10:28
@lewis6991

Copy link
Copy Markdown
Collaborator Author

/gemini review

@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 introduces logic to canonicalize callable types within unions, ensuring that equivalent Signature and DocFunction variants are collapsed into a single representation. It also adds test cases to verify pcall return type preservation and the new union collapsing behavior. Feedback was provided regarding the performance of the canonicalize_callable_union function, specifically suggesting that pre-calculating callable shapes for union members would avoid redundant database lookups and allocations during the merging process.

Comment on lines +127 to +148
fn canonicalize_callable_union(db: &DbIndex, ty: LuaType) -> LuaType {
let LuaType::Union(union) = ty else {
return ty;
};

let mut types = Vec::new();
for member in union.into_vec() {
let member_callable = as_callable_type(db, &member);
if types.iter().any(|existing| {
existing == &member
|| as_callable_type(db, existing)
.as_ref()
.zip(member_callable.as_ref())
.is_some_and(|(existing, member)| existing == member)
}) {
continue;
}
types.push(member);
}

LuaType::from_vec(types)
}

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.

medium

The canonicalize_callable_union function performs redundant and potentially expensive operations inside its inner loop. Specifically, as_callable_type is called repeatedly for the same existing members as the outer loop progresses. For Signature types, as_callable_type involves database lookups and allocations (via to_doc_func_type).

Pre-calculating the callable shapes for each member and storing them alongside the types would significantly improve performance, especially for larger unions or frequent type operations.

fn canonicalize_callable_union(db: &DbIndex, ty: LuaType) -> LuaType {
    let LuaType::Union(union) = ty else {
        return ty;
    };

    let members = union.into_vec();
    let mut result_types = Vec::with_capacity(members.len());
    let mut callables = Vec::with_capacity(members.len());

    for member in members {
        let member_callable = as_callable_type(db, &member);
        let is_duplicate = result_types.iter().zip(callables.iter()).any(|(existing, e_call)| {
            existing == &member
                || (member_callable.is_some() && member_callable == *e_call)
        });

        if !is_duplicate {
            result_types.push(member);
            callables.push(member_callable);
        }
    }

    LuaType::from_vec(result_types)
}

@CppCXY
CppCXY merged commit 5e06d3c into EmmyLuaLs:main Apr 7, 2026
16 checks passed
@lewis6991
lewis6991 deleted the topic/issue-1020-callable-unions branch April 7, 2026 12:19
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.

pcall can not infer correct

2 participants