luau-analyzer-sys 0.1.1

A high-performance, embedded Luau type-checking and analysis engine written in Rust. This crate provides bindings to the Luau analyzer, allowing you to integrate static analysis and code intelligence directly into your applications.
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "Fixture.h"

#include "ScopedFlags.h"
#include "doctest.h"

using namespace Luau;

LUAU_FASTFLAG(DebugLuauForceOldSolver)
LUAU_FASTFLAG(LuauExplicitTypeInstantiationSupport)
LUAU_FASTFLAG(LuauReplacerRespectsReboundGenerics)
LUAU_FASTFLAG(LuauVisitCallTypeArgsInDfg)

TEST_SUITE_BEGIN("TypeInferExplicitTypeInstantiations");

#define SUBCASE_BOTH_SOLVERS() \

    for (bool enabled : {true, false}) \
        if (ScopedFastFlag sffSolver{FFlag::DebugLuauForceOldSolver, !enabled}; true) \
    SUBCASE(enabled ? "New solver" : "Old solver")

TEST_CASE_FIXTURE(Fixture, "as_expression_correct")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(): T
            return nil :: any
        end

        local correct = f<<number>>() + 5
        )");

        LUAU_REQUIRE_NO_ERRORS(result);
    }
}

TEST_CASE_FIXTURE(Fixture, "as_expression_incorrect")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(): T
            return nil :: any
        end

        local incorrect = f<<string>>() + 5
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);

        if (!FFlag::DebugLuauForceOldSolver)
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Operator '+' could not be applied to operands of types string and number; there is no corresponding overload for __add"
            );
        }
        else
        {
            REQUIRE_EQ(toString(result.errors[0]), "Expected this to be 'number', but got 'string'");
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "as_stmt_correct")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(a: T, b: T)
            return nil :: any
        end

        f<<number | string>>(1, "a")
        )");

        LUAU_REQUIRE_NO_ERRORS(result);
    }
}

TEST_CASE_FIXTURE(Fixture, "as_stmt_incorrect")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(a: T, b: T)
            return nil :: any
        end

        f<<number | boolean>>(1, "a")
        )");

        if (!FFlag::DebugLuauForceOldSolver)
        {
            LUAU_REQUIRE_ERROR_COUNT(1, result);

            // clang-format off
            std::string expected =
                "Expected this to be 'boolean | number', but got 'string';\n"
                "this is because\n"
                "\t * the 1st component of the union is `number`, and `string` is not a subtype of `number`\n"
                "\t * the 2nd component of the union is `boolean`, and `string` is not a subtype of `boolean`"
            ;
            // clang-format on

            CHECK_LONG_STRINGS_EQ(expected, toString(result.errors.at(0)));
        }
        else
        {
            LUAU_REQUIRE_ERROR_COUNT(1, result);
            REQUIRE_EQ(
                toString(result.errors[0]), "Expected this to be 'boolean | number', but got 'string'; none of the union options are compatible"
            );
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "multiple_calls")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(): T
            return nil :: any
        end

        local a: number = f<<number>>()
        local b: string = f<<string>>()
        )");

        LUAU_REQUIRE_NO_ERRORS(result);
    }
}

TEST_CASE_FIXTURE(Fixture, "anonymous_type_inferred")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T, U>(): { a: T, b: U }
            return nil :: any
        end

        local correct: { a: number, b: string } = f<<number>>()
        local incorrect: { a: number, b: string } = f<<string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);

        REQUIRE_EQ(result.errors[0].location.begin.line, 7);
        LUAU_REQUIRE_ERROR(result, TypeMismatch);
    }
}

TEST_CASE_FIXTURE(Fixture, "type_packs")
{
    ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

    // FIXME: This triggers a GenericTypePackCountMismatch error, and it's not obvious if the
    // code for explicit types is broken, or if subtyping is broken.
    ScopedFastFlag oldSolver{FFlag::DebugLuauForceOldSolver, true};

    CheckResult result = check(R"(
    --!strict
    local function f<T..., U...>(...: T...): U... end

    local a: number, b: string = f<<(boolean, {}), (number, string)>>(true, {})
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(Fixture, "type_packs_method")
{
    ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

    // FIXME: This triggers a GenericTypePackCountMismatch error, and it's not obvious if the
    // code for explicit types is broken, or if subtyping is broken.
    ScopedFastFlag oldSolver{FFlag::DebugLuauForceOldSolver, true};

    CheckResult result = check(R"(
    --!strict
    local t: {
        f: <T..., U...>(self: any, T...) -> U...,
    } = nil :: any

    local a: number, b: string = t:f<<(boolean, {}), (number, string)>>(true, {})
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(Fixture, "type_packs_incorrect")
{
    ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

    // FIXME: This triggers a GenericTypePackCountMismatch error, and it's not obvious if the
    // code for explicit types is broken, or if subtyping is broken.
    ScopedFastFlag oldSolver{FFlag::DebugLuauForceOldSolver, true};

    CheckResult result = check(R"(
    --!strict
    local function f<T..., U...>(...: T...): U... end

    local a: number, b: string = f<<(boolean, {}), (number, string)>>(true, "uh oh")
    )");

    LUAU_REQUIRE_ERROR(result, TypeMismatch);
}

TEST_CASE_FIXTURE(Fixture, "type_packs_incorrect_method")
{
    ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

    // FIXME: This triggers a GenericTypePackCountMismatch error, and it's not obvious if the
    // code for explicit types is broken, or if subtyping is broken.
    ScopedFastFlag oldSolver{FFlag::DebugLuauForceOldSolver, true};

    CheckResult result = check(R"(
    --!strict
    local t: {
        f: <T..., U...>(self: any, T...) -> U...,
    } = nil :: any

    local a: number, b: string = t:f<<(boolean, {}), (number, string)>>(true, "uh oh")
    )");

    LUAU_REQUIRE_ERROR(result, TypeMismatch);
}

TEST_CASE_FIXTURE(Fixture, "dot_index_call")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = {
            f = function<T>(): T
                return nil :: any
            end,
        }

        local correct: number = t.f<<number>>()
        local incorrect: number = t.f<<string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        REQUIRE_EQ(result.errors[0].location.begin.line, 9);
    }
}

TEST_CASE_FIXTURE(Fixture, "method_index_call")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = {
            f = function<T>(self: any): T
                return nil :: any
            end,
        }

        local correct: number = t:f<<number>>()
        local incorrect: number = t:f<<string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 9);
    }
}

TEST_CASE_FIXTURE(Fixture, "stored_as_variable")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>(): T
            return nil :: any
        end

        local fNumber = f<<number>>

        local correct: number = fNumber()
        local incorrect: string = fNumber()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 9);
    }
}

TEST_CASE_FIXTURE(Fixture, "not_a_function")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local oops = 3
        local stub = oops<<number>>
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, InstantiateGenericsOnNonFunction);

        REQUIRE_EQ(toString(result.errors[0]), "Cannot instantiate type parameters on something without type parameters.");
    }
}

TEST_CASE_FIXTURE(BuiltinsFixture, "metatable_call")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = setmetatable({}, {
            __call = function<T>(self): T
                return nil :: any
            end,
        })

        t<<number>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, InstantiateGenericsOnNonFunction);
        REQUIRE_EQ(toString(result.errors[0]), "Luau does not currently support explicitly instantiating a table with a `__call` metamethod. \

                You may be able to work around this by creating a function that calls the table, and using that instead.");
    }
}

TEST_CASE_FIXTURE(Fixture, "method_call_incomplete")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = {
            f = function<T, U>(self: any): T | U
                return nil :: any
            end,
        }

        local correct: number | string = t:f<<number>>()
        local incorrect: number | string = t:f<<boolean>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 9);
    }
}

TEST_CASE_FIXTURE(Fixture, "too_many_provided")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T>() end

        f<<number, string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeInstantiationCountMismatch);

        if (!FFlag::DebugLuauForceOldSolver)
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 'f', which is typed as <T>(...any) -> (). Expected at most 1 type parameter, but 2 provided."
            );
        }
        else
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 'f', which is typed as <T>() -> (). Expected at most 1 type parameter, but 2 provided."
            );
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "too_many_provided_type_packs")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local function f<T...>(): (T...) end

        f<<(string, number), (true, false)>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeInstantiationCountMismatch);

        if (!FFlag::DebugLuauForceOldSolver)
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 'f', which is typed as <T...>(...any) -> (T...). Expected at most 1 type pack, but 2 provided."
            );
        }
        else
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 'f', which is typed as <T...>() -> (T...). Expected at most 1 type pack, but 2 provided."
            );
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "too_many_provided_method")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = {
            f = function<T>(self: any) end,
        }

        t:f<<number, string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeInstantiationCountMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 6);

        if (!FFlag::DebugLuauForceOldSolver)
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to function typed as <T>(any) -> (). Expected at most 1 type parameter, but 2 provided."
            );
        }
        else
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 't.f', which is typed as <T>(any) -> (). Expected at most 1 type parameter, but 2 provided."
            );
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "too_many_type_packs_provided_method")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local t = {
            f = function<T...>(self: any): (T...) end,
        }

        t:f<<(number, string), (true, false)>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeInstantiationCountMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 6);

        if (!FFlag::DebugLuauForceOldSolver)
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to function typed as <T...>(any) -> (T...). Expected at most 1 type pack, but 2 provided."
            );
        }
        else
        {
            REQUIRE_EQ(
                toString(result.errors[0]),
                "Too many type parameters passed to 't.f', which is typed as <T...>(any) -> (T...). Expected at most 1 type pack, but 2 provided."
            );
        }
    }
}

TEST_CASE_FIXTURE(Fixture, "function_intersections")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
        --!strict
        local f: (<T>(T) -> T) & (<T>(T?) -> T) = nil :: any
        f<<number>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, InstantiateGenericsOnNonFunction);
        REQUIRE_EQ(result.errors[0].location.begin.line, 3);
        REQUIRE_EQ(toString(result.errors[0]), "Luau does not currently support explicitly instantiating an overloaded function type.");
    }
}

TEST_CASE_FIXTURE(Fixture, "incomplete_type_packs")
{
    SUBCASE_BOTH_SOLVERS()
    {
        ScopedFastFlag semantics{FFlag::LuauExplicitTypeInstantiationSupport, true};

        CheckResult result = check(R"(
            local f: <A, T...>() -> (A, T...) = nil :: any
            local correct: string, b: number, c: boolean = f<<string>>()
            local incorrect: number, b: number, c: boolean = f<<string>>()
        )");

        LUAU_REQUIRE_ERROR_COUNT(1, result);
        LUAU_REQUIRE_ERROR(result, TypeMismatch);
        REQUIRE_EQ(result.errors[0].location.begin.line, 3);
    }
}

TEST_CASE_FIXTURE(Fixture, "replacing_generic_with_generic")
{
    // This really only does the right thing in the new solver.
    ScopedFastFlag sffs[] = {
        {FFlag::DebugLuauForceOldSolver, false},
        {FFlag::LuauExplicitTypeInstantiationSupport, true},
        {FFlag::LuauReplacerRespectsReboundGenerics, true},
    };

    CheckResult result = check(R"(
        local foo: <A, B>() -> (A, B) = nil :: any

        local function bar<T>()
            return foo<<T, number>>()
        end

        local baz, quxx = bar<<string>>()
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
    CHECK_EQ("string", toString(requireType("baz")));
    CHECK_EQ("number", toString(requireType("quxx")));
}

TEST_CASE_FIXTURE(Fixture, "typeof_in_method_call_type_args_no_crash")
{
    ScopedFastFlag sffs[] = {
        {FFlag::LuauExplicitTypeInstantiationSupport, true},
        {FFlag::LuauVisitCallTypeArgsInDfg, true},
    };

    CheckResult result = check(R"(
        local t = {}
        function t:f<T, U>() end

        local x = 5
        globl = 42

        t:f<<typeof(x), string>>()
        t:f<<number, typeof(x)>>()
        t:f<<typeof(globl), unknown>>()
        t:f<<typeof(t.f), unknown>>()
    )");

    LUAU_REQUIRE_ERROR_COUNT(1, result);
    // We assign to an unknown global.
    CHECK(get<UnknownSymbol>(result.errors[0]));
}

TEST_CASE_FIXTURE(Fixture, "typeof_local_in_type_pack_no_crash")
{
    ScopedFastFlag sffs[] = {
        {FFlag::LuauExplicitTypeInstantiationSupport, true},
        {FFlag::LuauVisitCallTypeArgsInDfg, true},
    };

    CheckResult result = check(R"(
        local t = {}
        function t:f<T...>() end

        local x = 5

        t:f<<(string, typeof(x))>>()
    )");

    LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_SUITE_END();