fix: handle intersection type as table subtype#1025
Open
EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
Open
fix: handle intersection type as table subtype#1025EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
EfveZombie wants to merge 2 commits intoEmmyLuaLs:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces support for intersection types in subtyping logic, specifically allowing them to be treated as tables, and adds LuaType::Object to the base type identification. Review feedback indicates that the current implementation for intersections is too restrictive, as it requires all components of an intersection to be tables rather than just one. Additionally, the changes in simple_type.rs are noted as being too broad, potentially treating non-table intersections as table subtypes.
crates/emmylua_code_analysis/src/semantic/type_check/simple_type.rs
Outdated
Show resolved
Hide resolved
45d4919 to
cf3c5f8
Compare
added 2 commits
April 10, 2026 22:25
…T...] & { n: integer } to be assignable to table
…e positives on index access like values[1] for types such as Tuple & Object
cf3c5f8 to
29b2090
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Intersection types like
[T...] & { n: integer }(e.g. the return type oftable.pack) were not recognized as subtypes oftable, causing false positive type mismatch diagnostics when assigning or passing them totable-typed targets.This issue was discovered while investigating commit 6f07707.
Root Cause
Two places in the type checker did not account for
Intersection:simple_type.rs— TheTable | TableConstbranch'smatches!macro did not includeIntersection, sotable = intersection_valuewould fail.sub_type.rs/get_base_type_id— This function maps concrete types to their base type ID (e.g.Array → table), but had no case forIntersection. When all components of an intersection are table-like, the intersection itself should also map totable. Also added the missingObjectmapping.Fix
LuaType::Intersection(_)to theTablecompact-type match insimple_type.rs.Intersectionhandling inget_base_type_id: recursively checks whether all components resolve totable, and if so returnstable.Objectto the existingtablebase-type mapping (was also missing).test_intersection_is_table_subtypecoveringcheck_type,ParamTypeMismatch, andAssignTypeMismatchscenarios.