2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Luau/AstQuery.h"
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
2021-11-05 10:07:18 +08:00
|
|
|
#include "Luau/Scope.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
#include "Luau/TypeVar.h"
|
|
|
|
#include "Luau/VisitTypeVar.h"
|
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
LUAU_FASTFLAG(LuauFixLocationSpanTableIndexExpr)
|
|
|
|
LUAU_FASTFLAG(LuauEqConstraint)
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("TypeInfer");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_hello_world")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local a = 7");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
TypeId aType = requireType("a");
|
|
|
|
CHECK_EQ(getPrimitiveType(aType), PrimitiveTypeVar::Number);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_propagation")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local a = 7 local b = a");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
TypeId bType = requireType("b");
|
|
|
|
CHECK_EQ(getPrimitiveType(bType), PrimitiveTypeVar::Number);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_error")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local a = 7 local b = 'hi' a = b");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
CHECK_EQ(result.errors[0], (TypeError{Location{Position{0, 35}, Position{0, 36}}, TypeMismatch{
|
|
|
|
requireType("a"),
|
|
|
|
requireType("b"),
|
|
|
|
}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_error_2")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local a = 7 a = 'hi'");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
CHECK_EQ(result.errors[0], (TypeError{Location{Position{0, 18}, Position{0, 22}}, TypeMismatch{
|
|
|
|
requireType("a"),
|
|
|
|
typeChecker.stringType,
|
|
|
|
}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_locals_with_nil_value")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local f = nil; f = 'hello world'");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
TypeId ty = requireType("f");
|
|
|
|
CHECK_EQ(getPrimitiveType(ty), PrimitiveTypeVar::String);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_locals_via_assignment_from_its_call_site")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a
|
|
|
|
function f(x) a = x end
|
|
|
|
f(1)
|
|
|
|
f("foo")
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
CHECK_EQ("number", toString(requireType("a")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_in_nocheck_mode")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nocheck
|
|
|
|
function f(x)
|
|
|
|
return x
|
|
|
|
end
|
|
|
|
-- we get type information even if there's type errors
|
|
|
|
f(1, 2)
|
|
|
|
)");
|
|
|
|
|
|
|
|
CHECK_EQ("(any) -> (...any)", toString(requireType("f")));
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "expr_statement")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check("local foo = 5 foo()");
|
2021-10-30 04:25:12 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "if_statement")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a
|
|
|
|
local b
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
if true then
|
|
|
|
a = 'hello'
|
|
|
|
else
|
|
|
|
b = 999
|
|
|
|
end
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_EQ(*typeChecker.stringType, *requireType("a"));
|
|
|
|
CHECK_EQ(*typeChecker.numberType, *requireType("b"));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "statements_are_topologically_sorted")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
function foo()
|
|
|
|
return bar(999), bar("hi")
|
|
|
|
end
|
|
|
|
|
|
|
|
function bar(i)
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
dumpErrors(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "unify_nearly_identical_recursive_types")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
local o
|
|
|
|
o:method()
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
local p
|
|
|
|
p:method()
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
o = p
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "warn_on_lowercase_parent_property")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local M = require(script.parent.DoesNotMatter)
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
auto ed = get<DeprecatedApiUsed>(result.errors[0]);
|
|
|
|
REQUIRE(ed);
|
|
|
|
|
|
|
|
REQUIRE_EQ("parent", ed->symbol);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "weird_case")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2021-11-05 10:42:00 +08:00
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local function f() return 4 end
|
|
|
|
local d = math.deg(f())
|
2021-11-05 10:42:00 +08:00
|
|
|
)");
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2022-03-18 08:06:25 +08:00
|
|
|
dumpErrors(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_ice_when_failing_the_occurs_check")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!strict
|
|
|
|
local s
|
|
|
|
s(s, 'a')
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(0, result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "occurs_check_does_not_recurse_forever_if_asked_to_traverse_a_cyclic_type")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
function u(t, w)
|
|
|
|
u(u, t)
|
|
|
|
end
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
#if 0
|
|
|
|
// CLI-29798
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "crazy_complexity")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!nonstrict
|
|
|
|
A:A():A():A():A():A():A():A():A():A():A():A()
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
std::cout << "OK! Allocated " << typeChecker.typeVars.size() << " typevars" << std::endl;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
2022-03-18 08:06:25 +08:00
|
|
|
#endif
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_errors_infer_types")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local err = (true).x
|
|
|
|
local c = err.Parent.Reward.GetChildren
|
|
|
|
local d = err.Parent.Reward
|
|
|
|
local e = err.Parent
|
|
|
|
local f = err
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
UnknownProperty* err = get<UnknownProperty>(result.errors[0]);
|
|
|
|
REQUIRE(err != nullptr);
|
|
|
|
CHECK_EQ("boolean", toString(err->table));
|
|
|
|
CHECK_EQ("x", err->key);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_EQ("*unknown*", toString(requireType("c")));
|
|
|
|
CHECK_EQ("*unknown*", toString(requireType("d")));
|
|
|
|
CHECK_EQ("*unknown*", toString(requireType("e")));
|
|
|
|
CHECK_EQ("*unknown*", toString(requireType("f")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "should_be_able_to_infer_this_without_stack_overflowing")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local function f(x, y)
|
|
|
|
return x or y
|
|
|
|
end
|
|
|
|
|
|
|
|
local function dont_crash(x, y)
|
|
|
|
local z: typeof(f(x, y)) = f(x, y)
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "exponential_blowup_from_copying_types")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!strict
|
|
|
|
-- An example of exponential blowup in number of types
|
|
|
|
-- The problem is that if we define function f(a) return x end
|
|
|
|
-- then this has type <t>(t)->T where x:T
|
|
|
|
-- *but* it copies T each time f is applied
|
|
|
|
-- so { left = f("hi"), right = f(5) }
|
|
|
|
-- has type { left : T_L, right : T_R }
|
|
|
|
-- where T_L and T_R are copies of T.
|
|
|
|
-- x0 : T0 where T0 = {}
|
|
|
|
local x0 = {}
|
|
|
|
-- f0 : <t>(t)->T0
|
|
|
|
local function f0(a) return x0 end
|
|
|
|
-- x1 : T1 where T1 = { left : T0_L, right : T0_R }
|
|
|
|
local x1 = { left = f0("hi"), right = f0(5) }
|
|
|
|
-- f1 : <t>(t)->T1
|
|
|
|
local function f1(a) return x1 end
|
|
|
|
-- x2 : T2 where T2 = { left : T1_L, right : T1_R }
|
|
|
|
local x2 = { left = f1("hi"), right = f1(5) }
|
|
|
|
-- f2 : <t>(t)->T2
|
|
|
|
local function f2(a) return x2 end
|
|
|
|
-- etc etc
|
|
|
|
local x3 = { left = f2("hi"), right = f2(5) }
|
|
|
|
local function f3(a) return x3 end
|
|
|
|
local x4 = { left = f3("hi"), right = f3(5) }
|
|
|
|
return x4
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2022-03-18 08:06:25 +08:00
|
|
|
ModulePtr module = getMainModule();
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// If we're not careful about copying, this ends up with O(2^N) types rather than O(N)
|
|
|
|
// (in this case 5 vs 31).
|
|
|
|
CHECK_GE(5, module->interfaceTypes.typeVars.size());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// In these tests, a successful parse is required, so we need the parser to return the AST and then we can test the recursion depth limit in type
|
|
|
|
// checker. We also want it to somewhat match up with production values, so we push up the parser recursion limit a little bit instead.
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "check_type_infer_recursion_limit")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
#if defined(LUAU_ENABLE_ASAN)
|
|
|
|
int limit = 250;
|
|
|
|
#elif defined(_DEBUG) || defined(_NOOPT)
|
|
|
|
int limit = 350;
|
|
|
|
#else
|
|
|
|
int limit = 600;
|
|
|
|
#endif
|
|
|
|
ScopedFastInt luauRecursionLimit{"LuauRecursionLimit", limit + 100};
|
|
|
|
ScopedFastInt luauTypeInferRecursionLimit{"LuauTypeInferRecursionLimit", limit - 100};
|
|
|
|
ScopedFastInt luauCheckRecursionLimit{"LuauCheckRecursionLimit", 0};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_NOTHROW(check("print('Hello!')"));
|
|
|
|
CHECK_THROWS_AS(check("function f() return " + rep("{a=", limit) + "'a'" + rep("}", limit) + " end"), std::runtime_error);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "check_block_recursion_limit")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
#if defined(LUAU_ENABLE_ASAN)
|
|
|
|
int limit = 250;
|
|
|
|
#elif defined(_DEBUG) || defined(_NOOPT)
|
|
|
|
int limit = 350;
|
|
|
|
#else
|
|
|
|
int limit = 600;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ScopedFastInt luauRecursionLimit{"LuauRecursionLimit", limit + 100};
|
|
|
|
ScopedFastInt luauCheckRecursionLimit{"LuauCheckRecursionLimit", limit - 100};
|
|
|
|
|
|
|
|
CheckResult result = check(rep("do ", limit) + "local a = 1" + rep(" end", limit));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK(nullptr != get<CodeTooComplex>(result.errors[0]));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "check_expr_recursion_limit")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
#if defined(LUAU_ENABLE_ASAN)
|
|
|
|
int limit = 250;
|
|
|
|
#elif defined(_DEBUG) || defined(_NOOPT)
|
2022-03-25 05:49:08 +08:00
|
|
|
int limit = 300;
|
2022-03-18 08:06:25 +08:00
|
|
|
#else
|
|
|
|
int limit = 600;
|
|
|
|
#endif
|
|
|
|
ScopedFastInt luauRecursionLimit{"LuauRecursionLimit", limit + 100};
|
|
|
|
ScopedFastInt luauCheckRecursionLimit{"LuauCheckRecursionLimit", limit - 100};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check(R"(("foo"))" + rep(":lower()", limit));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK(nullptr != get<CodeTooComplex>(result.errors[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "globals")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
foo = true
|
|
|
|
foo = "now i'm a string!"
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_EQ("any", toString(requireType("foo")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "globals2")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!nonstrict
|
|
|
|
foo = function() return 1 end
|
|
|
|
foo = "now i'm a string!"
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2022-03-18 08:06:25 +08:00
|
|
|
|
|
|
|
TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tm);
|
|
|
|
CHECK_EQ("() -> (...any)", toString(tm->wantedType));
|
|
|
|
CHECK_EQ("string", toString(tm->givenType));
|
|
|
|
CHECK_EQ("() -> (...any)", toString(requireType("foo")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "globals_are_banned_in_strict_mode")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!strict
|
|
|
|
foo = true
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
UnknownSymbol* us = get<UnknownSymbol>(result.errors[0]);
|
|
|
|
REQUIRE(us);
|
|
|
|
CHECK_EQ("foo", us->name);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "globals_everywhere")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!nonstrict
|
|
|
|
foo = 1
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
if true then
|
|
|
|
bar = 2
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_EQ("any", toString(requireType("foo")));
|
|
|
|
CHECK_EQ("any", toString(requireType("bar")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "correctly_scope_locals_do")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
do
|
|
|
|
local a = 1
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
print(a) -- oops!
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
UnknownSymbol* us = get<UnknownSymbol>(result.errors[0]);
|
|
|
|
REQUIRE(us);
|
|
|
|
CHECK_EQ(us->name, "a");
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "checking_should_not_ice")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_NOTHROW(check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
f,g = ...
|
|
|
|
f(g(...))[...] = nil
|
|
|
|
f,xpcall = ...
|
|
|
|
local value = g(...)(g(...))
|
|
|
|
)"));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_EQ("any", toString(requireType("value")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// TEST_CASE_FIXTURE(Fixture, "infer_method_signature_of_argument")
|
|
|
|
// {
|
|
|
|
// CheckResult result = check(R"(
|
|
|
|
// function f(a)
|
|
|
|
// if a.cond then
|
|
|
|
// return a.method()
|
|
|
|
// end
|
|
|
|
// end
|
|
|
|
// )");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// LUAU_REQUIRE_NO_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// CHECK_EQ("A", toString(requireType("f")));
|
|
|
|
// }
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "cyclic_follow")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
check(R"(
|
2021-10-30 04:25:12 +08:00
|
|
|
--!nonstrict
|
2022-03-18 08:06:25 +08:00
|
|
|
l0,table,_,_,_ = ...
|
|
|
|
_,_,_,_.time(...)._.n0,l0,_ = function(l0)
|
|
|
|
end,_.__index,(_),_.time(_.n0 or _,...)
|
|
|
|
for l0=...,_,"" do
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
2022-03-18 08:06:25 +08:00
|
|
|
_ += not _
|
|
|
|
do end
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
check(R"(
|
2021-10-30 04:25:12 +08:00
|
|
|
--!nonstrict
|
2022-03-18 08:06:25 +08:00
|
|
|
n13,_,table,_,l0,_,_ = ...
|
|
|
|
_,n0[(_)],_,_._(...)._.n39,l0,_._ = function(l84,...)
|
|
|
|
end,_.__index,"",_,l0._(nil)
|
|
|
|
for l0=...,table.n5,_ do
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
2022-03-18 08:06:25 +08:00
|
|
|
_:_(...).n1 /= _
|
|
|
|
do
|
|
|
|
_(_ + _)
|
|
|
|
do end
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
struct FindFreeTypeVars
|
2022-03-05 00:19:20 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
bool foundOne = false;
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
template<typename ID>
|
|
|
|
void cycle(ID)
|
|
|
|
{
|
|
|
|
}
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
template<typename ID, typename T>
|
|
|
|
bool operator()(ID, T)
|
|
|
|
{
|
|
|
|
return !foundOne;
|
|
|
|
}
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
template<typename ID>
|
|
|
|
bool operator()(ID, Unifiable::Free)
|
|
|
|
{
|
|
|
|
foundOne = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_after_error_recovery")
|
2022-03-05 00:19:20 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local x =
|
|
|
|
local a = 7
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TypeId aType = requireType("a");
|
|
|
|
CHECK_EQ(getPrimitiveType(aType), PrimitiveTypeVar::Number);
|
2022-03-05 00:19:20 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
// Check that type checker knows about error expressions
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_after_error_recovery_no_assert")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-18 08:06:25 +08:00
|
|
|
CheckResult result = check("function +() local _ = true end");
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_after_error_recovery_no_replacement_name_in_error")
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
local t = { x = 10, y = 20 }
|
|
|
|
return t.
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
export type = number
|
|
|
|
export type = string
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
function string.() end
|
|
|
|
)");
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
}
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
local function () end
|
|
|
|
local function () end
|
|
|
|
)");
|
2022-03-05 00:19:20 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
local dm = {}
|
|
|
|
function dm.() end
|
|
|
|
function dm.() end
|
|
|
|
)");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "index_expr_should_be_checked")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local foo: any
|
|
|
|
|
|
|
|
print(foo[(true).x])
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
UnknownProperty* up = get<UnknownProperty>(result.errors[0]); // Should probably be NotATable
|
|
|
|
REQUIRE(up);
|
|
|
|
CHECK_EQ("boolean", toString(up->table));
|
|
|
|
CHECK_EQ("x", up->key);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "stringify_nested_unions_with_optionals")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!strict
|
|
|
|
local a: number | (string | boolean) | nil
|
|
|
|
local b: number = a
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2022-03-18 08:06:25 +08:00
|
|
|
TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tm);
|
|
|
|
CHECK_EQ(typeChecker.numberType, tm->wantedType);
|
|
|
|
CHECK_EQ("(boolean | number | string)?", toString(tm->givenType));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "cli_39932_use_unifier_in_ensure_methods")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local x: {number|number} = {1, 2, 3}
|
|
|
|
local y = x[1] - x[2]
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_report_type_errors_within_an_AstStatError")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
foo
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_report_type_errors_within_an_AstExprError")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local a = foo:
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_ice_on_astexprerror")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
local foo = -;
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "luau_resolves_symbols_the_same_way_lua_does")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2022-03-18 08:06:25 +08:00
|
|
|
--!strict
|
|
|
|
function Funky()
|
|
|
|
local a: number = foo
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
local foo: string = 'hello'
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
auto e = result.errors.front();
|
|
|
|
REQUIRE_MESSAGE(get<UnknownSymbol>(e) != nullptr, "Expected UnknownSymbol, but got " << e);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "no_stack_overflow_from_isoptional")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function _(l0:t0): (any, ()->())
|
|
|
|
return 0,_
|
|
|
|
end
|
|
|
|
|
|
|
|
type t0 = t0 | {}
|
|
|
|
_(nil)
|
|
|
|
)");
|
|
|
|
|
|
|
|
CHECK_LE(0, result.errors.size());
|
|
|
|
|
|
|
|
std::optional<TypeFun> t0 = getMainModule()->getModuleScope()->lookupType("t0");
|
|
|
|
REQUIRE(t0);
|
2021-11-19 06:21:07 +08:00
|
|
|
CHECK_EQ("*unknown*", toString(t0->type));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
auto it = std::find_if(result.errors.begin(), result.errors.end(), [](TypeError& err) {
|
|
|
|
return get<OccursCheckFailed>(err);
|
|
|
|
});
|
|
|
|
CHECK(it != result.errors.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "no_stack_overflow_from_isoptional2")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function _(l0:({})|(t0)):((((typeof((xpcall)))|(t96<t0>))|(t13))&(t96<t0>),()->typeof(...))
|
|
|
|
return 0,_
|
|
|
|
end
|
|
|
|
|
|
|
|
type t0<t107> = ((typeof((_G)))|(({})|(t0)))|(t0)
|
|
|
|
_(nil)
|
|
|
|
|
|
|
|
local t: ({})|(t0)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "no_infinite_loop_when_trying_to_unify_uh_this")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function _(l22,l0):((((boolean)|(t0))|(t0))&(()->(()->(()->()->{},(t0<t22>)|(t0)),any)))
|
|
|
|
return function():t0<t0>
|
|
|
|
end
|
|
|
|
end
|
|
|
|
type t0<t0> = ((typeof(_))|(any))|(typeof(_))
|
|
|
|
_()
|
|
|
|
)");
|
|
|
|
|
|
|
|
CHECK_LE(0, result.errors.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "no_heap_use_after_free_error")
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
_ += _:n0(xpcall,_)
|
|
|
|
local l0
|
|
|
|
do end
|
|
|
|
while _ do
|
|
|
|
function _:_()
|
|
|
|
_ += _(_._(_:n0(xpcall,_)))
|
|
|
|
end
|
|
|
|
end
|
2021-10-30 04:25:12 +08:00
|
|
|
)");
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
CHECK_LE(0, result.errors.size());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_type_assertion_value_type")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function f()
|
|
|
|
return {4, "b", 3} :: {string|number}
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_assignment_value_types")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a: (number, number) -> number = function(a, b) return a - b end
|
|
|
|
|
|
|
|
a = function(a, b) return a + b end
|
|
|
|
|
|
|
|
local b: {number|string}
|
|
|
|
local c: {number|string}
|
|
|
|
b, c = {2, "s"}, {"b", 4}
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_assignment_value_types_mutable_lval")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a = {}
|
|
|
|
a.x = 2
|
|
|
|
a = setmetatable(a, { __call = function(x) end })
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_through_group_expr")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function f(a: (number, number) -> number) return a(1, 3) end
|
|
|
|
f(((function(a, b) return a + b end)))
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions1")
|
|
|
|
{
|
2022-01-15 00:06:31 +08:00
|
|
|
CheckResult result = check(R"(local a = if true then "true" else "false")");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
TypeId aType = requireType("a");
|
|
|
|
CHECK_EQ(getPrimitiveType(aType), PrimitiveTypeVar::String);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions2")
|
|
|
|
{
|
2022-01-15 00:06:31 +08:00
|
|
|
// Test expression containing elseif
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a = if false then "a" elseif false then "b" else "c"
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
TypeId aType = requireType("a");
|
|
|
|
CHECK_EQ(getPrimitiveType(aType), PrimitiveTypeVar::String);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions_type_union")
|
|
|
|
{
|
2022-02-18 08:41:20 +08:00
|
|
|
CheckResult result = check(R"(local a: number? = if true then 42 else nil)");
|
2022-01-15 00:06:31 +08:00
|
|
|
|
2022-02-18 08:41:20 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "number?");
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions_expected_type_1")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-01-15 00:06:31 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
type X = {number | string}
|
|
|
|
local a: X = if true then {"1", 2, 3} else {4, 5, 6}
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{number | string}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions_expected_type_2")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a: number? = if true then 1 else nil
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "tc_if_else_expressions_expected_type_3")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function times<T>(n: any, f: () -> T)
|
|
|
|
local result: {T} = {}
|
|
|
|
local res = f()
|
|
|
|
table.insert(result, if true then res else n)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-12-11 05:17:10 +08:00
|
|
|
/*
|
|
|
|
* If it wasn't instantly obvious, we have the fuzzer to thank for this gem of a test.
|
|
|
|
*
|
|
|
|
* We had an issue here where the scope for the `if` block here would
|
|
|
|
* have an elevated TypeLevel even though there is no function nesting going on.
|
|
|
|
* This would result in a free typevar for the type of _ that was much higher than
|
|
|
|
* it should be. This type would be erroneously quantified in the definition of `aaa`.
|
|
|
|
* This in turn caused an ice when evaluating `_()` in the while loop.
|
|
|
|
*/
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "free_typevars_introduced_within_control_flow_constructs_do_not_get_an_elevated_TypeLevel")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
--!strict
|
|
|
|
if _ then
|
|
|
|
_[_], _ = nil
|
|
|
|
_()
|
|
|
|
end
|
|
|
|
|
|
|
|
local aaa = function():typeof(_) return 1 end
|
|
|
|
|
|
|
|
if aaa then
|
|
|
|
while _() do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
// No ice()? No problem.
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a bit elaborate. Bear with me.
|
|
|
|
*
|
|
|
|
* The type of _ becomes free with the first statement. With the second, we unify it with a function.
|
|
|
|
*
|
|
|
|
* At this point, it is important that the newly created fresh types of this new function type are promoted
|
|
|
|
* to the same level as the original free type. If we do not, they are incorrectly ascribed the level of the
|
|
|
|
* containing function.
|
|
|
|
*
|
|
|
|
* If this is allowed to happen, the final lambda erroneously quantifies the type of _ to something ridiculous
|
|
|
|
* just before we typecheck the invocation to _.
|
|
|
|
*/
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "fuzzer_found_this")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
l0, _ = nil
|
|
|
|
|
|
|
|
local function p()
|
|
|
|
_()
|
|
|
|
end
|
|
|
|
|
|
|
|
a = _(
|
|
|
|
function():(typeof(p),typeof(_))
|
|
|
|
end
|
|
|
|
)[nil]
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "recursive_metatable_crash")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function getIt()
|
|
|
|
local y
|
|
|
|
y = setmetatable({}, y)
|
|
|
|
return y
|
|
|
|
end
|
|
|
|
local a = getIt()
|
|
|
|
local b = getIt()
|
|
|
|
local c = a or b
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-01-22 00:23:02 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "bound_typepack_promote")
|
|
|
|
{
|
|
|
|
// No assertions should trigger
|
|
|
|
check(R"(
|
|
|
|
local function p()
|
|
|
|
local this = {}
|
|
|
|
this.pf = foo()
|
|
|
|
function this:IsActive() end
|
|
|
|
function this:Start(o) end
|
|
|
|
return this
|
|
|
|
end
|
|
|
|
|
|
|
|
local function h(tp, o)
|
|
|
|
ep = tp
|
|
|
|
tp:Start(o)
|
|
|
|
tp.pf.Connect(function()
|
|
|
|
ep:IsActive()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on()
|
|
|
|
local t = p()
|
|
|
|
h(t)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2022-02-04 07:09:37 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "cli_50041_committing_txnlog_in_apollo_client_error")
|
|
|
|
{
|
|
|
|
ScopedFastFlag subtypingVariance{"LuauTableSubtypingVariance2", true};
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
--!nolint
|
|
|
|
|
|
|
|
type FieldSpecifier = {
|
|
|
|
fieldName: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReadFieldOptions = FieldSpecifier & { from: number? }
|
|
|
|
|
|
|
|
type Policies = {
|
|
|
|
getStoreFieldName: (self: Policies, fieldSpec: FieldSpecifier) -> string,
|
|
|
|
}
|
|
|
|
|
|
|
|
local Policies = {}
|
|
|
|
|
|
|
|
local function foo(p: Policies)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Policies:getStoreFieldName(specifier: FieldSpecifier): string
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
function Policies:readField(options: ReadFieldOptions)
|
|
|
|
local _ = self:getStoreFieldName(options)
|
|
|
|
--[[
|
|
|
|
Type error:
|
|
|
|
TypeError { "MainModule", Location { { line = 25, col = 16 }, { line = 25, col = 20 } }, TypeMismatch { Policies, {- getStoreFieldName: (tp1) -> (a, b...) -} } }
|
|
|
|
]]
|
|
|
|
foo(self)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_END();
|