emmylua_code_analysis 0.24.0

A library for analyzing lua code.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::cmp::Ordering;
use std::sync::Arc;

use crate::{
    InferFailReason, check_type_compact,
    db_index::{DbIndex, LuaFunctionType, LuaType},
    semantic::infer::InferCallFuncResult,
};

pub(crate) fn callable_accepts_args(
    db: &DbIndex,
    func: &LuaFunctionType,
    expr_types: &[LuaType],
    is_colon_call: bool,
    arg_count: Option<usize>,
) -> bool {
    let arg_count = arg_count.unwrap_or(expr_types.len());
    if func.get_params().len() < arg_count && !is_func_last_param_variadic(func) {
        return false;
    }

    for (arg_index, expr_type) in expr_types.iter().enumerate() {
        let Some(param_index) = get_call_param_index(func, arg_index, is_colon_call) else {
            continue;
        };
        let Some(param_type) = get_func_param_type(func, param_index) else {
            return false;
        };

        if !param_type.is_any() && check_type_compact(db, &param_type, expr_type).is_err() {
            return false;
        }
    }

    true
}

pub fn resolve_signature_by_args(
    db: &DbIndex,
    overloads: &[Arc<LuaFunctionType>],
    expr_types: &[LuaType],
    is_colon_call: bool,
    arg_count: Option<usize>,
    declined_no_flow_args: &[bool],
) -> InferCallFuncResult {
    let expr_len = expr_types.len();
    let arg_count = arg_count.unwrap_or(expr_len);
    let has_declined_no_flow_arg = declined_no_flow_args.iter().any(|declined| *declined);
    let mut need_resolve_funcs = match overloads.len() {
        0 => return Err(InferFailReason::None),
        1 => return Ok(Arc::clone(&overloads[0])),
        _ => overloads
            .iter()
            .map(|it| Some(it.clone()))
            .collect::<Vec<_>>(),
    };

    if expr_len == 0 {
        for overload in overloads {
            let param_len = overload.get_params().len();
            if param_len == 0 {
                return Ok(overload.clone());
            }
        }
    }

    let mut best_match_result = need_resolve_funcs[0]
        .clone()
        .expect("Match result should exist");
    for (arg_index, expr_type) in expr_types.iter().enumerate() {
        let mut current_match_result = ParamMatchResult::Not;
        let declined_no_flow_arg = declined_no_flow_args
            .get(arg_index)
            .copied()
            .unwrap_or(false);
        for opt_func in &mut need_resolve_funcs {
            let func = match opt_func.as_ref() {
                None => continue,
                Some(func) => func,
            };
            let param_len = func.get_params().len();
            if param_len < arg_count && !is_func_last_param_variadic(func) {
                *opt_func = None;
                continue;
            }

            let Some(param_index) = get_call_param_index(func, arg_index, is_colon_call) else {
                continue;
            };
            let Some(param_type) = get_func_param_type(func, param_index) else {
                *opt_func = None;
                continue;
            };

            let match_result = if declined_no_flow_arg && expr_type.is_unknown() {
                // Declined no-flow args are compatible with any overload, but they do
                // not prove that a specific row won.
                ParamMatchResult::Any
            } else if param_type.is_any() {
                ParamMatchResult::Any
            } else if check_type_compact(db, &param_type, expr_type).is_ok() {
                ParamMatchResult::Type
            } else {
                ParamMatchResult::Not
            };

            if match_result > current_match_result {
                current_match_result = match_result;
                best_match_result = func.clone();
            }

            if match_result == ParamMatchResult::Not {
                *opt_func = None;
                continue;
            }
        }

        if current_match_result == ParamMatchResult::Not {
            break;
        }
    }

    let mut rest_need_resolve_funcs = need_resolve_funcs
        .iter()
        .filter_map(|it| it.clone())
        .map(Some)
        .collect::<Vec<_>>();

    let rest_len = rest_need_resolve_funcs.len();
    match rest_len {
        0 => {
            if has_declined_no_flow_arg {
                return Err(InferFailReason::None);
            }
            return Ok(best_match_result);
        }
        1 => {
            return Ok(rest_need_resolve_funcs[0]
                .clone()
                .expect("Resolve function should exist"));
        }
        _ => {}
    }

    if !has_declined_no_flow_arg
        && let Some(func) = choose_more_specific_callable(
            db,
            &rest_need_resolve_funcs,
            expr_types,
            is_colon_call,
            declined_no_flow_args,
        )
    {
        return Ok(func);
    }

    let start_param_index = expr_len;
    let mut max_param_len = 0;
    for func in rest_need_resolve_funcs.iter().flatten() {
        let mut param_len = func.get_params().len();
        if func
            .get_params()
            .last()
            .is_some_and(|last_param| last_param.0 == "...")
        {
            param_len = param_len.saturating_sub(1);
        }
        if param_len > max_param_len {
            max_param_len = param_len;
        }
    }

    for param_index in start_param_index..max_param_len {
        let mut current_match_result = ParamMatchResult::Not;
        for (i, opt_func) in rest_need_resolve_funcs.iter_mut().enumerate() {
            let func = match opt_func.as_ref() {
                None => continue,
                Some(func) => func,
            };
            let param_len = func.get_params().len();
            let Some(param_index) = get_call_param_index(func, param_index, is_colon_call) else {
                continue;
            };
            let match_result = if param_index >= param_len {
                if func
                    .get_params()
                    .last()
                    .is_some_and(|last_param_info| last_param_info.0 == "...")
                {
                    ParamMatchResult::Any
                } else if has_declined_no_flow_arg {
                    ParamMatchResult::Type
                } else {
                    return Ok(func.clone());
                }
            } else {
                let param_info = func
                    .get_params()
                    .get(param_index)
                    .expect("Param index should exist");
                let param_type = param_info.1.clone().unwrap_or(LuaType::Any);
                if param_type.is_any() {
                    ParamMatchResult::Any
                } else if param_type.is_nullable() {
                    ParamMatchResult::Type
                } else {
                    ParamMatchResult::Not
                }
            };

            if match_result > current_match_result {
                current_match_result = match_result;
                best_match_result = func.clone();
            }

            if match_result == ParamMatchResult::Not {
                *opt_func = None;
                continue;
            }

            if !has_declined_no_flow_arg
                && match_result >= ParamMatchResult::Any
                && i + 1 == rest_len
                && param_index + 1 == func.get_params().len()
            {
                return Ok(func.clone());
            }
        }

        if current_match_result == ParamMatchResult::Not {
            break;
        }
    }

    if !has_declined_no_flow_arg {
        return Ok(best_match_result);
    }

    let mut remaining_funcs = rest_need_resolve_funcs.into_iter().flatten();
    let Some(first) = remaining_funcs.next() else {
        return Err(InferFailReason::None);
    };

    if remaining_funcs.all(|func| func.get_ret() == first.get_ret()) {
        Ok(first)
    } else {
        Err(InferFailReason::None)
    }
}

fn choose_more_specific_callable(
    db: &DbIndex,
    funcs: &[Option<Arc<LuaFunctionType>>],
    expr_types: &[LuaType],
    is_colon_call: bool,
    declined_no_flow_args: &[bool],
) -> Option<Arc<LuaFunctionType>> {
    if expr_types.is_empty()
        || expr_types.iter().enumerate().all(|(i, expr_type)| {
            declined_no_flow_args.get(i).copied().unwrap_or(false)
                || expr_type.is_any()
                || expr_type.is_unknown()
        })
    {
        return None;
    }

    let mut best: Option<Arc<LuaFunctionType>> = None;
    let mut has_strict_better = false;
    for func in funcs.iter().flatten() {
        let Some(best_func) = best.as_ref() else {
            best = Some(func.clone());
            continue;
        };

        match compare_callable_specificity(
            db,
            func,
            best_func,
            expr_types,
            is_colon_call,
            declined_no_flow_args,
        ) {
            Some(Ordering::Greater) => {
                best = Some(func.clone());
                has_strict_better = true;
            }
            Some(Ordering::Less) => {
                has_strict_better = true;
            }
            Some(Ordering::Equal) => {}
            None => return None,
        }
    }

    if has_strict_better { best } else { None }
}

fn compare_callable_specificity(
    db: &DbIndex,
    a: &LuaFunctionType,
    b: &LuaFunctionType,
    expr_types: &[LuaType],
    is_colon_call: bool,
    declined_no_flow_args: &[bool],
) -> Option<Ordering> {
    let mut result = Ordering::Equal;
    for (arg_index, expr_type) in expr_types.iter().enumerate() {
        if declined_no_flow_args
            .get(arg_index)
            .copied()
            .unwrap_or(false)
            || expr_type.is_any()
            || expr_type.is_unknown()
        {
            continue;
        }

        let param_index = get_call_param_index(a, arg_index, is_colon_call)?;
        let a_param = get_func_param_type(a, param_index)?;
        let b_param = get_func_param_type(b, param_index)?;
        let param_order = compare_param_specificity(db, &a_param, &b_param, expr_type);
        match (result, param_order) {
            (Ordering::Equal, order) => result = order,
            (Ordering::Greater, Ordering::Less) | (Ordering::Less, Ordering::Greater) => {
                return None;
            }
            _ => {}
        }
    }

    Some(result)
}

fn compare_param_specificity(
    db: &DbIndex,
    a: &LuaType,
    b: &LuaType,
    expr_type: &LuaType,
) -> Ordering {
    if a == b {
        return Ordering::Equal;
    }

    // 字面量实参直接命中对应 overload 时, 该 overload 比基础类型主签名更具体.
    match (expr_type, a, b) {
        (
            LuaType::IntegerConst(expr) | LuaType::DocIntegerConst(expr),
            LuaType::DocIntegerConst(a),
            LuaType::Integer | LuaType::Number,
        ) if expr == a => return Ordering::Greater,
        (
            LuaType::IntegerConst(expr) | LuaType::DocIntegerConst(expr),
            LuaType::Integer | LuaType::Number,
            LuaType::DocIntegerConst(b),
        ) if expr == b => return Ordering::Less,
        (
            LuaType::StringConst(expr) | LuaType::DocStringConst(expr),
            LuaType::DocStringConst(a),
            LuaType::String,
        ) if expr == a => return Ordering::Greater,
        (
            LuaType::StringConst(expr) | LuaType::DocStringConst(expr),
            LuaType::String,
            LuaType::DocStringConst(b),
        ) if expr == b => return Ordering::Less,
        (
            LuaType::BooleanConst(expr) | LuaType::DocBooleanConst(expr),
            LuaType::DocBooleanConst(a),
            LuaType::Boolean,
        ) if expr == a => return Ordering::Greater,
        (
            LuaType::BooleanConst(expr) | LuaType::DocBooleanConst(expr),
            LuaType::Boolean,
            LuaType::DocBooleanConst(b),
        ) if expr == b => return Ordering::Less,
        _ => {}
    }

    match (a.is_any() || a.is_unknown(), b.is_any() || b.is_unknown()) {
        (true, false) => return Ordering::Less,
        (false, true) => return Ordering::Greater,
        _ => {}
    }

    let a_sub_b = check_type_compact(db, b, a).is_ok();
    let b_sub_a = check_type_compact(db, a, b).is_ok();
    match (a_sub_b, b_sub_a) {
        (true, false) => Ordering::Greater,
        (false, true) => Ordering::Less,
        _ => Ordering::Equal,
    }
}

pub(crate) fn is_func_last_param_variadic(func: &LuaFunctionType) -> bool {
    if let Some(last_param) = func.get_params().last() {
        last_param.0 == "..."
    } else {
        false
    }
}

pub(crate) fn get_call_param_index(
    func: &LuaFunctionType,
    arg_index: usize,
    is_colon_call: bool,
) -> Option<usize> {
    let mut param_index = arg_index;
    match (func.is_colon_define(), is_colon_call) {
        (true, false) => {
            if param_index == 0 {
                return None;
            }
            param_index -= 1;
        }
        (false, true) => {
            param_index += 1;
        }
        _ => {}
    }
    Some(param_index)
}

pub(crate) fn get_func_param_type(func: &LuaFunctionType, param_index: usize) -> Option<LuaType> {
    if let Some(param_info) = func.get_params().get(param_index) {
        return Some(param_info.1.clone().unwrap_or(LuaType::Any));
    }

    let last_param_info = func.get_params().last()?;
    if last_param_info.0 == "..." {
        Some(last_param_info.1.clone().unwrap_or(LuaType::Any))
    } else {
        None
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
enum ParamMatchResult {
    Not,
    Any,
    Type,
}