Skip to content

Commit 0bf0fa2

Browse files
committed
fix metatable
1 parent 2dfb1e8 commit 0bf0fa2

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,36 @@ mod test {
7070
"#,
7171
);
7272

73-
// let ty = ws.expr_ty("a");
74-
// disable test temp
75-
// assert_eq!(ws.humanize_type(ty), "switch");
73+
let ty = ws.expr_ty("a");
74+
assert_eq!(ws.humanize_type(ty), "switch");
75+
}
76+
77+
#[test]
78+
fn test_issue_599() {
79+
let mut ws = VirtualWorkspace::new();
80+
81+
ws.def(
82+
r#"
83+
---@class Class.Config
84+
---@field abc string
85+
local ClassConfigMeta = {}
86+
87+
---@type table<string, Class.Config>
88+
local _classConfigMap = {}
89+
90+
91+
---@param name string
92+
---@return Class.Config
93+
local function getConfig(name)
94+
local config = _classConfigMap[name]
95+
if not config then
96+
A = setmetatable({ name = name }, { __index = ClassConfigMeta })
97+
end
98+
end
99+
"#,
100+
);
101+
102+
let ty = ws.expr_ty("A");
103+
assert_eq!(ws.humanize_type(ty), "Class.Config");
76104
}
77105
}

crates/emmylua_code_analysis/src/semantic/infer/infer_call/infer_setmetatable.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use emmylua_parser::{LuaAstNode, LuaCallExpr, LuaExpr, LuaIndexKey};
22

33
use crate::{
4-
infer_expr, semantic::infer::InferResult, DbIndex, InFiled, InferFailReason, LuaInferCache,
5-
LuaInstanceType, LuaType,
4+
infer_expr,
5+
semantic::{infer::InferResult, member::find_members_with_key},
6+
DbIndex, InFiled, InferFailReason, LuaInferCache, LuaInstanceType, LuaMemberKey, LuaType,
67
};
78

89
pub fn infer_setmetatable_call(
@@ -114,5 +115,15 @@ fn infer_metatable_index_type(
114115
};
115116

116117
let meta_type = infer_expr(db, cache, metatable)?;
118+
if let Some(meta_members) =
119+
find_members_with_key(db, &meta_type, LuaMemberKey::Name("__index".into()), false)
120+
{
121+
if let Some(meta_member) = meta_members.first() {
122+
if meta_member.typ.is_custom_type() {
123+
return Ok((meta_member.typ.clone(), true));
124+
}
125+
}
126+
}
127+
117128
Ok((meta_type, false))
118129
}

0 commit comments

Comments
 (0)