Skip to content

Commit 4aff1f2

Browse files
committed
fix(semantic): narrow rawget doc-function guards
Rawget guards already narrow matching index expressions when the callee is the built-in signature. The same helper can be exposed through a documented function field, where its return type is stored directly on the doc function. Handle std.RawGet returns from doc functions in guard flow so utils.get(t, "x") narrows t.x like rawget(t, "x").
1 parent 192e3d7 commit 4aff1f2

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,33 @@ mod test {
390390
assert_eq!(ws.expr_ty("result"), LuaType::Integer);
391391
}
392392

393+
#[test]
394+
fn test_rawget_doc_function_guard_narrows_matching_index_expr() {
395+
let mut ws = VirtualWorkspace::new_with_init_std_lib();
396+
397+
ws.def(
398+
r#"
399+
---@class T
400+
---@field x? integer
401+
402+
---@type T
403+
local t = {}
404+
405+
---@class Utils
406+
---@field get fun(tbl: T, key: "x"): std.RawGet<T, "x">
407+
408+
---@type Utils
409+
local utils = { get = rawget }
410+
411+
if utils.get(t, "x") then
412+
result = t.x
413+
end
414+
"#,
415+
);
416+
417+
assert_eq!(ws.expr_ty("result"), LuaType::Integer);
418+
}
419+
393420
#[test]
394421
fn test_type_guard_call_narrows_matching_index_expr() {
395422
let mut ws = VirtualWorkspace::new();

crates/emmylua_code_analysis/src/semantic/infer/narrow/condition_flow/call_flow.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ pub fn get_type_at_call_expr(
7676
f,
7777
condition_flow,
7878
),
79+
LuaType::Call(call) => get_type_at_call_expr_by_call(
80+
db,
81+
cache,
82+
var_ref_id,
83+
call_expr,
84+
call,
85+
condition_flow,
86+
),
7987
_ => Ok(ConditionFlowAction::Continue),
8088
}
8189
}

0 commit comments

Comments
 (0)