Skip to content

Commit 6a1e37c

Browse files
committed
If the variable is mutable, it degrades to a regular type.
Fix #472
1 parent f9570dc commit 6a1e37c

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/bind_type/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,39 @@ use crate::{
1111
pub fn bind_type(
1212
db: &mut DbIndex,
1313
type_owner: LuaTypeOwner,
14-
type_cache: LuaTypeCache,
14+
mut type_cache: LuaTypeCache,
1515
) -> Option<()> {
1616
let decl_type_cache = db.get_type_index().get_type_cache(&type_owner);
1717

1818
if decl_type_cache.is_none() {
19+
// type backward
20+
if type_cache.is_infer() {
21+
if let LuaTypeOwner::Decl(decl_id) = &type_owner {
22+
if let Some(refs) = db
23+
.get_reference_index()
24+
.get_decl_references(&decl_id.file_id, decl_id)
25+
{
26+
if refs.iter().any(|it| it.is_write) {
27+
match &type_cache.as_type() {
28+
LuaType::IntegerConst(_) => {
29+
type_cache = LuaTypeCache::InferType(LuaType::Integer)
30+
}
31+
LuaType::StringConst(_) => {
32+
type_cache = LuaTypeCache::InferType(LuaType::String)
33+
}
34+
LuaType::BooleanConst(_) => {
35+
type_cache = LuaTypeCache::InferType(LuaType::Boolean)
36+
}
37+
LuaType::FloatConst(_) => {
38+
type_cache = LuaTypeCache::InferType(LuaType::Number)
39+
}
40+
_ => {}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
1947
db.get_type_index_mut()
2048
.bind_type(type_owner.clone(), type_cache);
2149
migrate_global_members_when_type_resolve(db, type_owner);

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,4 +716,31 @@ end
716716
"#,
717717
));
718718
}
719+
720+
#[test]
721+
fn test_issue_472() {
722+
let mut ws = VirtualWorkspace::new();
723+
724+
assert!(ws.check_code_for(
725+
DiagnosticCode::UnnecessaryIf,
726+
r#"
727+
worldLightLevel = 0
728+
worldLightColor = 0
729+
Gmae = {}
730+
---@param color integer
731+
---@param level integer
732+
function Game.setWorldLight(color, level)
733+
local previousColor = worldLightColor
734+
local previousLevel = worldLightLevel
735+
736+
worldLightColor = color
737+
worldLightLevel = level
738+
739+
if worldLightColor ~= previousColor or worldLightLevel ~= previousLevel then
740+
-- Do something...
741+
end
742+
end
743+
"#
744+
))
745+
}
719746
}

crates/emmylua_code_analysis/src/db_index/type/type_owner.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ impl LuaTypeCache {
6161
LuaTypeCache::InferType(ty) => ty,
6262
}
6363
}
64+
65+
pub fn is_infer(&self) -> bool {
66+
matches!(self, LuaTypeCache::InferType(_))
67+
}
68+
69+
pub fn is_doc(&self) -> bool {
70+
matches!(self, LuaTypeCache::DocType(_))
71+
}
6472
}
6573

6674
impl std::ops::Deref for LuaTypeCache {

0 commit comments

Comments
 (0)