Skip to content

Commit 75687b2

Browse files
committed
Fix Tuple index infer
Fix #595
1 parent d04abef commit 75687b2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

crates/emmylua_code_analysis/src/compilation/test/tuple_test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@ mod tests {
4747
);
4848
assert_eq!(ty, expected_ty);
4949
}
50+
51+
#[test]
52+
fn test_issue_595() {
53+
let mut ws = VirtualWorkspace::new();
54+
ws.check_code_for(
55+
DiagnosticCode::AssignTypeMismatch,
56+
r#"
57+
local ret --- @type [integer?]
58+
local h = ret[#ret] -- type is integer??
59+
if h then
60+
--- @type integer
61+
local _ = h
62+
end
63+
"#,
64+
);
65+
}
5066
}

crates/emmylua_code_analysis/src/semantic/infer/infer_index.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,12 @@ fn infer_tuple_member(
451451
};
452452
}
453453
LuaType::Integer => {
454-
let mut result = Vec::new();
454+
let mut result = LuaType::Unknown;
455455
for typ in tuple_type.get_types() {
456-
result.push(typ.clone());
456+
result = TypeOps::Union.apply(db, &result, typ);
457457
}
458-
result.push(LuaType::Nil);
459-
return Ok(LuaType::Union(LuaUnionType::from_vec(result).into()));
458+
result = TypeOps::Union.apply(db, &result, &LuaType::Nil);
459+
return Ok(result);
460460
}
461461
_ => {}
462462
},

0 commit comments

Comments
 (0)