2023-09-30 02:13:05 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/NonStrictTypeChecker.h"
|
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
2023-10-06 20:02:32 +01:00
|
|
|
#include "Luau/Ast.h"
|
2023-12-02 07:46:57 +00:00
|
|
|
#include "Luau/Common.h"
|
|
|
|
#include "Luau/IostreamHelpers.h"
|
2023-10-06 20:02:32 +01:00
|
|
|
#include "Luau/ModuleResolver.h"
|
2023-11-17 18:46:18 +00:00
|
|
|
#include "Luau/VisitType.h"
|
2023-12-02 07:46:57 +00:00
|
|
|
|
2023-10-06 20:02:32 +01:00
|
|
|
#include "ScopedFlags.h"
|
2023-09-30 02:13:05 +01:00
|
|
|
#include "doctest.h"
|
2023-10-06 20:02:32 +01:00
|
|
|
#include <iostream>
|
2023-09-30 02:13:05 +01:00
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
#define NONSTRICT_REQUIRE_ERR_AT_POS(pos, result, idx) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
auto pos_ = (pos); \
|
|
|
|
bool foundErr = false; \
|
|
|
|
int index = 0; \
|
|
|
|
for (const auto& err : result.errors) \
|
|
|
|
{ \
|
|
|
|
if (err.location.begin == pos_) \
|
|
|
|
{ \
|
|
|
|
foundErr = true; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
index++; \
|
|
|
|
} \
|
|
|
|
REQUIRE_MESSAGE(foundErr, "Expected error at " << pos_); \
|
|
|
|
idx = index; \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define NONSTRICT_REQUIRE_CHECKED_ERR(pos, name, result) \
|
2023-11-17 18:46:18 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2023-12-02 07:46:57 +00:00
|
|
|
int errIndex; \
|
|
|
|
NONSTRICT_REQUIRE_ERR_AT_POS(pos, result, errIndex); \
|
|
|
|
auto err = get<CheckedFunctionCallError>(result.errors[errIndex]); \
|
|
|
|
REQUIRE(err != nullptr); \
|
|
|
|
CHECK_EQ(err->checkedFunctionName, name); \
|
2023-11-17 18:46:18 +00:00
|
|
|
} while (false)
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
#define NONSTRICT_REQUIRE_FUNC_DEFINITION_ERR(pos, argname, result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
int errIndex; \
|
|
|
|
NONSTRICT_REQUIRE_ERR_AT_POS(pos, result, errIndex); \
|
|
|
|
auto err = get<NonStrictFunctionDefinitionError>(result.errors[errIndex]); \
|
|
|
|
REQUIRE(err != nullptr); \
|
|
|
|
CHECK_EQ(err->argument, argname); \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
2023-10-06 20:02:32 +01:00
|
|
|
struct NonStrictTypeCheckerFixture : Fixture
|
|
|
|
{
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
NonStrictTypeCheckerFixture()
|
|
|
|
{
|
|
|
|
registerHiddenTypes(&frontend);
|
2024-04-25 23:26:09 +01:00
|
|
|
registerTestTypes();
|
2023-12-02 07:46:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
CheckResult checkNonStrict(const std::string& code)
|
2023-10-06 20:02:32 +01:00
|
|
|
{
|
2023-10-13 21:20:12 +01:00
|
|
|
ScopedFastFlag flags[] = {
|
2023-12-02 07:46:57 +00:00
|
|
|
{FFlag::DebugLuauDeferredConstraintResolution, true},
|
2023-10-13 21:20:12 +01:00
|
|
|
};
|
2023-10-21 02:10:30 +01:00
|
|
|
LoadDefinitionFileResult res = loadDefinition(definitions);
|
|
|
|
LUAU_ASSERT(res.success);
|
2023-10-13 21:20:12 +01:00
|
|
|
return check(Mode::Nonstrict, code);
|
|
|
|
}
|
2023-09-30 02:13:05 +01:00
|
|
|
|
2024-04-25 23:26:09 +01:00
|
|
|
CheckResult checkNonStrictModule(const std::string& moduleName)
|
|
|
|
{
|
|
|
|
ScopedFastFlag flags[] = {
|
|
|
|
{FFlag::DebugLuauDeferredConstraintResolution, true},
|
|
|
|
};
|
|
|
|
LoadDefinitionFileResult res = loadDefinition(definitions);
|
|
|
|
LUAU_ASSERT(res.success);
|
|
|
|
return frontend.check(moduleName);
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
std::string definitions = R"BUILTIN_SRC(
|
2024-06-07 18:51:12 +01:00
|
|
|
@checked declare function abs(n: number): number
|
|
|
|
@checked declare function lower(s: string): string
|
2023-11-17 18:46:18 +00:00
|
|
|
declare function cond() : boolean
|
2024-06-07 18:51:12 +01:00
|
|
|
@checked declare function contrived(n : Not<number>) : number
|
2023-12-15 21:29:06 +00:00
|
|
|
|
|
|
|
-- interesting types of things that we would like to mark as checked
|
2024-06-07 18:51:12 +01:00
|
|
|
@checked declare function onlyNums(...: number) : number
|
|
|
|
@checked declare function mixedArgs(x: string, ...: number) : number
|
|
|
|
@checked declare function optionalArg(x: string?) : number
|
2024-02-02 21:32:42 +00:00
|
|
|
declare foo: {
|
|
|
|
bar: @checked (number) -> number,
|
|
|
|
}
|
2024-03-09 00:47:53 +00:00
|
|
|
|
2024-06-07 18:51:12 +01:00
|
|
|
@checked declare function optionalArgsAtTheEnd1(x: string, y: number?, z: number?) : number
|
|
|
|
@checked declare function optionalArgsAtTheEnd2(x: string, y: number?, z: string) : number
|
2024-04-05 21:45:09 +01:00
|
|
|
|
|
|
|
type DateTypeArg = {
|
|
|
|
year: number,
|
|
|
|
month: number,
|
|
|
|
day: number,
|
|
|
|
hour: number?,
|
|
|
|
min: number?,
|
|
|
|
sec: number?,
|
|
|
|
isdst: boolean?,
|
|
|
|
}
|
|
|
|
|
|
|
|
declare os : {
|
|
|
|
time: @checked (time: DateTypeArg?) -> number
|
|
|
|
}
|
2024-04-25 23:26:09 +01:00
|
|
|
|
2024-06-07 18:51:12 +01:00
|
|
|
@checked declare function require(target : any) : any
|
2023-10-06 20:02:32 +01:00
|
|
|
)BUILTIN_SRC";
|
2023-10-13 21:20:12 +01:00
|
|
|
};
|
2023-10-06 20:02:32 +01:00
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
TEST_SUITE_BEGIN("NonStrictTypeCheckerTest");
|
2023-10-06 20:02:32 +01:00
|
|
|
|
2023-12-15 21:29:06 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "interesting_checked_functions")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
onlyNums(1,1,1)
|
|
|
|
onlyNums(1, "a")
|
|
|
|
|
|
|
|
mixedArgs("a", 1, 2)
|
|
|
|
mixedArgs(1, 1, 1)
|
|
|
|
mixedArgs("a", true)
|
|
|
|
|
|
|
|
optionalArg(nil)
|
|
|
|
optionalArg("a")
|
|
|
|
optionalArg(3)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(4, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 12), "onlyNums", result); // onlyNums(1, "a")
|
|
|
|
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(5, 10), "mixedArgs", result); // mixedArgs(1, 1, 1)
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(6, 15), "mixedArgs", result); // mixedArgs("a", true)
|
|
|
|
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(10, 12), "optionalArg", result); // optionalArg(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "simple_negation_caching_example")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = 3
|
|
|
|
abs(x)
|
|
|
|
abs(x)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
result = checkNonStrict(R"(
|
|
|
|
local x = 3
|
|
|
|
contrived(x)
|
|
|
|
contrived(x)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 10), "contrived", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 10), "contrived", result);
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:46:18 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "simple_non_strict_failure")
|
2023-10-06 20:02:32 +01:00
|
|
|
{
|
2023-11-17 18:46:18 +00:00
|
|
|
CheckResult result = checkNonStrict(R"BUILTIN_SRC(
|
2023-10-13 21:20:12 +01:00
|
|
|
abs("hi")
|
|
|
|
)BUILTIN_SRC");
|
2023-11-17 18:46:18 +00:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(1, 4), "abs", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "nested_function_calls_constant")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x
|
|
|
|
abs(lower(x))
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 4), "abs", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_else_warns_with_never_local")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x : never
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
else
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 8), "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(5, 10), "lower", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_else_warns_nil_branches")
|
|
|
|
{
|
|
|
|
auto result = checkNonStrict(R"(
|
|
|
|
local x
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
else
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 8), "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(5, 10), "lower", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_else_doesnt_warn_else_branch")
|
|
|
|
{
|
|
|
|
auto result = checkNonStrict(R"(
|
|
|
|
local x : string = "hi"
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
else
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 8), "abs", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_no_else")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x : string
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-10-06 20:02:32 +01:00
|
|
|
}
|
|
|
|
|
2023-11-17 18:46:18 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_no_else_err_in_cond")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
2023-12-02 07:46:57 +00:00
|
|
|
local x : string = ""
|
2023-11-17 18:46:18 +00:00
|
|
|
if abs(x) then
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 7), "abs", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_else_expr_should_warn")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x : never
|
|
|
|
local y = if cond() then abs(x) else lower(x)
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 29), "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 43), "lower", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_else_expr_doesnt_warn_else_branch")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x : string = "hi"
|
|
|
|
local y = if cond() then abs(x) else lower(x)
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 29), "abs", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "sequencing_if_checked_call")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x
|
|
|
|
if cond() then
|
|
|
|
x = 5
|
|
|
|
else
|
|
|
|
x = nil
|
|
|
|
end
|
|
|
|
lower(x)
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(7, 6), "lower", result);
|
|
|
|
}
|
2023-11-17 18:46:18 +00:00
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_unrelated_checked_calls")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function h(x, y)
|
|
|
|
abs(x)
|
|
|
|
lower(y)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_basic_no_errors")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
abs(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_basic_errors")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x : string)
|
|
|
|
abs(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 8), "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_failure")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
abs(lower(x))
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 8), "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_sequencing_errors")
|
2023-11-17 18:46:18 +00:00
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
abs(x)
|
|
|
|
lower(x)
|
|
|
|
end
|
2023-12-02 07:46:57 +00:00
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(3, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 8), "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 10), "lower", result);
|
|
|
|
NONSTRICT_REQUIRE_FUNC_DEFINITION_ERR(Position(1, 11), "x", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "local_fn_produces_error")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = 5
|
|
|
|
local function y() lower(x) end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 25), "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "fn_expr_produces_error")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = 5
|
|
|
|
local y = function() lower(x) end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 27), "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_if_warns_never")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
else
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
end
|
2023-11-17 18:46:18 +00:00
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(3, 12), "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(5, 14), "lower", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_if_no_else")
|
2023-11-17 18:46:18 +00:00
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
2023-12-02 07:46:57 +00:00
|
|
|
function f(x)
|
|
|
|
if cond() then
|
|
|
|
abs(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_if_assignment_errors")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
if cond() then
|
|
|
|
x = 5
|
|
|
|
else
|
|
|
|
x = nil
|
|
|
|
end
|
|
|
|
lower(x)
|
2023-11-17 18:46:18 +00:00
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-12-02 07:46:57 +00:00
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(7, 10), "lower", result);
|
2023-11-17 18:46:18 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "function_def_if_assignment_no_errors")
|
2023-11-17 18:46:18 +00:00
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
2023-12-02 07:46:57 +00:00
|
|
|
function f(x : string | number)
|
|
|
|
if cond() then
|
|
|
|
x = 5
|
|
|
|
else
|
|
|
|
x = "hi"
|
|
|
|
end
|
2023-11-17 18:46:18 +00:00
|
|
|
abs(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2023-12-02 07:46:57 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "local_only_one_warning")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = 5
|
|
|
|
lower(x)
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(2, 6), "lower", result);
|
|
|
|
}
|
2023-11-17 18:46:18 +00:00
|
|
|
|
2023-12-15 21:29:06 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "phi_node_assignment")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = "a" -- x1
|
|
|
|
if cond() then
|
|
|
|
x = 3 -- x2
|
|
|
|
end
|
|
|
|
lower(x) -- phi {x1, x2}
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2024-02-02 21:32:42 +00:00
|
|
|
} //
|
2023-12-15 21:29:06 +00:00
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "phi_node_assignment_err")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x = nil
|
|
|
|
if cond() then
|
|
|
|
if cond() then
|
|
|
|
x = 5
|
|
|
|
end
|
|
|
|
abs(x)
|
|
|
|
else
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(8, 10), "lower", result);
|
|
|
|
}
|
|
|
|
|
2024-02-02 21:32:42 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "tblprop_is_checked")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
foo.bar("hi")
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(1, 8), "foo.bar", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "incorrect_arg_count")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
foo.bar(1,2,3)
|
|
|
|
abs(3, "hi");
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
auto r1 = get<CheckedFunctionIncorrectArgs>(result.errors[0]);
|
|
|
|
auto r2 = get<CheckedFunctionIncorrectArgs>(result.errors[1]);
|
|
|
|
LUAU_ASSERT(r1);
|
|
|
|
LUAU_ASSERT(r2);
|
|
|
|
CHECK_EQ("abs", r1->functionName);
|
|
|
|
CHECK_EQ("foo.bar", r2->functionName);
|
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:53 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "optionals_in_checked_function_can_be_omitted")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
optionalArgsAtTheEnd1("a")
|
|
|
|
optionalArgsAtTheEnd1("a", 3)
|
|
|
|
optionalArgsAtTheEnd1("a", nil, 3)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "optionals_in_checked_function_in_middle_cannot_be_omitted")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
optionalArgsAtTheEnd2("a", "a") -- error
|
|
|
|
optionalArgsAtTheEnd2("a", nil, "b")
|
|
|
|
optionalArgsAtTheEnd2("a", 3, "b")
|
|
|
|
optionalArgsAtTheEnd2("a", "b", "c") -- error
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(3, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(1, 27), "optionalArgsAtTheEnd2", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(Position(4, 27), "optionalArgsAtTheEnd2", result);
|
|
|
|
auto r1 = get<CheckedFunctionIncorrectArgs>(result.errors[2]);
|
|
|
|
LUAU_ASSERT(r1);
|
|
|
|
CHECK_EQ(3, r1->expected);
|
|
|
|
CHECK_EQ(2, r1->actual);
|
|
|
|
}
|
|
|
|
|
2024-04-05 21:45:09 +01:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "non_testable_type_throws_ice")
|
|
|
|
{
|
2024-08-02 15:30:04 +01:00
|
|
|
CHECK_THROWS_AS(
|
|
|
|
checkNonStrict(R"(
|
2024-04-05 21:45:09 +01:00
|
|
|
os.time({year = 0, month = 0, day = 0, min = 0, isdst = nil})
|
|
|
|
)"),
|
2024-08-02 15:30:04 +01:00
|
|
|
Luau::InternalCompilerError
|
|
|
|
);
|
2024-04-05 21:45:09 +01:00
|
|
|
}
|
|
|
|
|
2024-04-25 23:26:09 +01:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "non_strict_shouldnt_warn_on_require_module")
|
|
|
|
{
|
|
|
|
fileResolver.source["Modules/A"] = R"(
|
|
|
|
--!strict
|
|
|
|
type t = {x : number}
|
|
|
|
local e : t = {x = 3}
|
|
|
|
return e
|
|
|
|
)";
|
|
|
|
fileResolver.sourceTypes["Modules/A"] = SourceCode::Module;
|
|
|
|
|
|
|
|
fileResolver.source["Modules/B"] = R"(
|
|
|
|
--!nonstrict
|
|
|
|
local E = require(script.Parent.A)
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult result = checkNonStrictModule("Modules/B");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2024-05-31 20:18:18 +01:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "nonstrict_shouldnt_warn_on_valid_buffer_use")
|
|
|
|
{
|
|
|
|
loadDefinition(R"(
|
|
|
|
declare buffer: {
|
|
|
|
create: @checked (size: number) -> buffer,
|
|
|
|
readi8: @checked (b: buffer, offset: number) -> number,
|
|
|
|
writef64: @checked (b: buffer, offset: number, value: number) -> (),
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local b = buffer.create(100)
|
|
|
|
buffer.writef64(b, 0, 5)
|
|
|
|
buffer.readi8(b, 0)
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2023-09-30 02:13:05 +01:00
|
|
|
TEST_SUITE_END();
|