2023-09-30 01:22:06 +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 18:31:16 +01:00
|
|
|
#include "Luau/Common.h"
|
|
|
|
#include "Luau/Ast.h"
|
|
|
|
#include "Luau/ModuleResolver.h"
|
2023-11-17 18:15:31 +00:00
|
|
|
#include "Luau/VisitType.h"
|
2023-10-06 18:31:16 +01:00
|
|
|
#include "ScopedFlags.h"
|
2023-09-30 01:22:06 +01:00
|
|
|
#include "doctest.h"
|
2023-10-06 18:31:16 +01:00
|
|
|
#include <iostream>
|
2023-09-30 01:22:06 +01:00
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2023-11-17 18:15:31 +00:00
|
|
|
#define NONSTRICT_REQUIRE_CHECKED_ERR(index, name, result) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
REQUIRE(index < result.errors.size()); \
|
|
|
|
auto err##index = get<CheckedFunctionCallError>(result.errors[index]); \
|
|
|
|
REQUIRE(err##index != nullptr); \
|
|
|
|
CHECK_EQ((err##index)->checkedFunctionName, name); \
|
|
|
|
} while (false)
|
|
|
|
|
2023-10-06 18:31:16 +01:00
|
|
|
struct NonStrictTypeCheckerFixture : Fixture
|
|
|
|
{
|
|
|
|
|
2023-10-13 20:38:31 +01:00
|
|
|
CheckResult checkNonStrict(const std::string& code)
|
2023-10-06 18:31:16 +01:00
|
|
|
{
|
2023-10-13 20:38:31 +01:00
|
|
|
ScopedFastFlag flags[] = {
|
|
|
|
{"LuauCheckedFunctionSyntax", true},
|
2023-10-20 21:36:26 +01:00
|
|
|
{"DebugLuauDeferredConstraintResolution", true},
|
2023-10-13 20:38:31 +01:00
|
|
|
};
|
2023-10-20 21:36:26 +01:00
|
|
|
LoadDefinitionFileResult res = loadDefinition(definitions);
|
|
|
|
LUAU_ASSERT(res.success);
|
2023-10-13 20:38:31 +01:00
|
|
|
return check(Mode::Nonstrict, code);
|
|
|
|
}
|
2023-09-30 01:22:06 +01:00
|
|
|
|
2023-10-13 20:38:31 +01:00
|
|
|
std::string definitions = R"BUILTIN_SRC(
|
2023-10-06 18:31:16 +01:00
|
|
|
declare function @checked abs(n: number): number
|
2023-11-17 18:15:31 +00:00
|
|
|
declare function @checked lower(s: string): string
|
|
|
|
declare function cond() : boolean
|
2023-10-06 18:31:16 +01:00
|
|
|
)BUILTIN_SRC";
|
2023-10-13 20:38:31 +01:00
|
|
|
};
|
2023-10-06 18:31:16 +01:00
|
|
|
|
|
|
|
|
2023-10-13 20:38:31 +01:00
|
|
|
TEST_SUITE_BEGIN("NonStrictTypeCheckerTest");
|
2023-10-06 18:31:16 +01:00
|
|
|
|
2023-11-17 18:15:31 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "simple_non_strict_failure")
|
2023-10-06 18:31:16 +01:00
|
|
|
{
|
2023-11-17 18:15:31 +00:00
|
|
|
CheckResult result = checkNonStrict(R"BUILTIN_SRC(
|
2023-10-13 20:38:31 +01:00
|
|
|
abs("hi")
|
|
|
|
)BUILTIN_SRC");
|
2023-11-17 18:15:31 +00:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "nested_function_calls_constant")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x
|
|
|
|
abs(lower(x))
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(1, "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(1, "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 18:31:16 +01:00
|
|
|
}
|
|
|
|
|
2023-11-17 18:15:31 +00:00
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "if_then_no_else_err_in_cond")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
local x : string
|
|
|
|
if abs(x) then
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(1, "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "sequencing_errors")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function f(x)
|
|
|
|
abs(x)
|
|
|
|
lower(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(0, "abs", result);
|
|
|
|
NONSTRICT_REQUIRE_CHECKED_ERR(1, "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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(0, "lower", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NonStrictTypeCheckerFixture, "sequencing_unrelated_checked_calls")
|
|
|
|
{
|
|
|
|
CheckResult result = checkNonStrict(R"(
|
|
|
|
function h(x, y)
|
|
|
|
abs(x)
|
|
|
|
lower(y)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-30 01:22:06 +01:00
|
|
|
TEST_SUITE_END();
|