Skip to content

Commit 75166d3

Browse files
committed
fix(generic): avoid callable return fallback on shape miss
1 parent f81dcfb commit 75166d3

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,34 @@ mod test {
200200
// unresolved and carried through a named callback value.
201201
assert_eq!(ws.expr_ty("classify_string_unresolved"), ws.ty("string"));
202202
}
203+
204+
#[test]
205+
fn test_apply_return_infer_leaves_result_unknown_when_no_callable_member_matches_arg_shape() {
206+
let mut ws = VirtualWorkspace::new();
207+
ws.def(
208+
r#"
209+
---@generic A, R
210+
---@param f fun(x: A): R
211+
---@param x A
212+
---@return R
213+
local function apply(f, x)
214+
return f(x)
215+
end
216+
217+
---@alias FnInt fun(x: integer): integer
218+
---@alias FnString fun(x: string): string
219+
220+
---@type FnInt | FnString
221+
local run
222+
223+
---@type boolean
224+
local b
225+
226+
result = apply(run, b)
227+
"#,
228+
);
229+
230+
let result_ty = ws.expr_ty("result");
231+
assert_eq!(result_ty, ws.ty("unknown"));
232+
}
203233
}

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type/instantiate_func_generic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ pub fn infer_callable_return_from_remaining_args(
267267
return Ok(None);
268268
}
269269

270-
infer_callable_return_from_arg_types(context, callable_type, Some(&call_arg_types), true)
270+
// If the remaining arg types are concrete and no callable member accepts them, leave the
271+
// return unresolved instead of unioning returns from incompatible overloads. The broad
272+
// fallback is only for cases where those arg shapes could not be inferred at all.
273+
infer_callable_return_from_arg_types(context, callable_type, Some(&call_arg_types), false)
271274
}
272275

273276
fn instantiate_callable_from_arg_types(

0 commit comments

Comments
 (0)