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/BuiltinDefinitions.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
2023-01-05 04:53:17 +08:00
|
|
|
#include "Luau/Type.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
2022-10-15 03:48:41 +08:00
|
|
|
using namespace Luau;
|
2022-04-15 07:57:43 +08:00
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
LUAU_FASTFLAG(LuauSolverV2);
|
2023-12-02 15:46:57 +08:00
|
|
|
LUAU_FASTFLAG(LuauInstantiateInSubtyping);
|
2023-07-28 23:13:53 +08:00
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_BEGIN("TypePackTests");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_multi_return")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function take_two()
|
|
|
|
return 2, 2
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* takeTwoType = get<FunctionType>(requireType("take_two"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(takeTwoType != nullptr);
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
const auto& [returns, tail] = flatten(takeTwoType->retTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK_EQ(2, returns.size());
|
2023-03-11 04:21:07 +08:00
|
|
|
CHECK_EQ(builtinTypes->numberType, follow(returns[0]));
|
|
|
|
CHECK_EQ(builtinTypes->numberType, follow(returns[1]));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK(!tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "empty_varargs_should_return_nil_when_not_in_tail_position")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a, b = ..., 1
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "self_and_varargs_should_work")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local t = {}
|
|
|
|
function t:f(...) end
|
|
|
|
t:f(1)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "last_element_of_return_statement_can_itself_be_a_pack")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function take_two()
|
|
|
|
return 2, 2
|
|
|
|
end
|
|
|
|
|
|
|
|
function take_three()
|
|
|
|
return 1, take_two()
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
dumpErrors(result);
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* takeOneMoreType = get<FunctionType>(requireType("take_three"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(takeOneMoreType != nullptr);
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
const auto& [rets, tail] = flatten(takeOneMoreType->retTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
REQUIRE_EQ(3, rets.size());
|
2023-03-11 04:21:07 +08:00
|
|
|
CHECK_EQ(builtinTypes->numberType, follow(rets[0]));
|
|
|
|
CHECK_EQ(builtinTypes->numberType, follow(rets[1]));
|
|
|
|
CHECK_EQ(builtinTypes->numberType, follow(rets[2]));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK(!tail);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "higher_order_function")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function apply(f, g, x)
|
|
|
|
return f(g(x))
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2022-04-15 07:57:43 +08:00
|
|
|
CHECK_EQ("<a, b..., c...>((b...) -> (c...), (a) -> (b...), a) -> (c...)", toString(requireType("apply")));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "return_type_should_be_empty_if_nothing_is_returned")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function f() end
|
|
|
|
function g() return end
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* fTy = get<FunctionType>(requireType("f"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(fTy != nullptr);
|
2022-06-17 09:05:14 +08:00
|
|
|
CHECK_EQ(0, size(fTy->retTypes));
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* gTy = get<FunctionType>(requireType("g"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(gTy != nullptr);
|
2022-06-17 09:05:14 +08:00
|
|
|
CHECK_EQ(0, size(gTy->retTypes));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "no_return_size_should_be_zero")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function f(a:any) return a end
|
|
|
|
function g() return end
|
|
|
|
function h() end
|
|
|
|
|
|
|
|
g(h())
|
|
|
|
f(g(),h())
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* fTy = get<FunctionType>(requireType("f"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(fTy != nullptr);
|
2022-06-17 09:05:14 +08:00
|
|
|
CHECK_EQ(1, size(follow(fTy->retTypes)));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* gTy = get<FunctionType>(requireType("g"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(gTy != nullptr);
|
2022-06-17 09:05:14 +08:00
|
|
|
CHECK_EQ(0, size(gTy->retTypes));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* hTy = get<FunctionType>(requireType("h"));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(hTy != nullptr);
|
2022-06-17 09:05:14 +08:00
|
|
|
CHECK_EQ(0, size(hTy->retTypes));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "varargs_inference_through_multiple_scopes")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function f(...)
|
|
|
|
do
|
|
|
|
local a: string = ...
|
|
|
|
local b: number = ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
f("foo")
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "multiple_varargs_inference_are_not_confused")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function f(...)
|
|
|
|
local a: string = ...
|
|
|
|
|
|
|
|
return function(...)
|
|
|
|
local b: number = ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
f("foo", "bar")(1, 2)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "parenthesized_varargs_returns_any")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
local value
|
|
|
|
|
|
|
|
local function f(...)
|
|
|
|
value = ...
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("any", toString(requireType("value")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "variadic_packs")
|
|
|
|
{
|
2023-03-11 04:21:07 +08:00
|
|
|
TypeArena& arena = frontend.globals.globalTypes;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
unfreeze(arena);
|
|
|
|
|
2023-03-11 04:21:07 +08:00
|
|
|
TypePackId listOfNumbers = arena.addTypePack(TypePackVar{VariadicTypePack{builtinTypes->numberType}});
|
|
|
|
TypePackId listOfStrings = arena.addTypePack(TypePackVar{VariadicTypePack{builtinTypes->stringType}});
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// clang-format off
|
2023-03-11 04:21:07 +08:00
|
|
|
addGlobalBinding(frontend.globals, "foo",
|
2021-10-30 04:25:12 +08:00
|
|
|
arena.addType(
|
2023-01-05 04:53:17 +08:00
|
|
|
FunctionType{
|
2021-10-30 04:25:12 +08:00
|
|
|
listOfNumbers,
|
2023-03-11 04:21:07 +08:00
|
|
|
arena.addTypePack({builtinTypes->numberType})
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
"@test"
|
|
|
|
);
|
2023-03-11 04:21:07 +08:00
|
|
|
addGlobalBinding(frontend.globals, "bar",
|
2021-10-30 04:25:12 +08:00
|
|
|
arena.addType(
|
2023-01-05 04:53:17 +08:00
|
|
|
FunctionType{
|
2023-03-11 04:21:07 +08:00
|
|
|
arena.addTypePack({{builtinTypes->numberType}, listOfStrings}),
|
|
|
|
arena.addTypePack({builtinTypes->numberType})
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
"@test"
|
|
|
|
);
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
freeze(arena);
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
|
|
|
|
foo(1, 2, 3, "foo")
|
|
|
|
bar(1, "foo", "bar", 3)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
|
2024-01-13 06:25:27 +08:00
|
|
|
CHECK(Location{Position{3, 21}, Position{3, 26}} == result.errors[0].location);
|
|
|
|
CHECK(Location{Position{4, 29}, Position{4, 30}} == result.errors[1].location);
|
|
|
|
|
2023-03-11 04:21:07 +08:00
|
|
|
CHECK_EQ(
|
2024-08-02 22:30:04 +08:00
|
|
|
result.errors[0], (TypeError{Location(Position{3, 21}, Position{3, 26}), TypeMismatch{builtinTypes->numberType, builtinTypes->stringType}})
|
|
|
|
);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-03-11 04:21:07 +08:00
|
|
|
CHECK_EQ(
|
2024-08-02 22:30:04 +08:00
|
|
|
result.errors[1], (TypeError{Location(Position{4, 29}, Position{4, 30}), TypeMismatch{builtinTypes->stringType, builtinTypes->numberType}})
|
|
|
|
);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "variadic_pack_syntax")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
|
|
|
|
local function foo(...: number)
|
|
|
|
end
|
|
|
|
|
|
|
|
foo(1, 2, 3, 4, 5, 6)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ(toString(requireType("foo")), "(...number) -> ()");
|
|
|
|
}
|
|
|
|
|
2023-11-11 05:10:07 +08:00
|
|
|
#if 0
|
2022-03-12 00:55:02 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_pack_hidden_free_tail_infinite_growth")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-12 00:55:02 +08:00
|
|
|
CheckResult result = check(R"(
|
2021-10-30 04:25:12 +08:00
|
|
|
--!nonstrict
|
|
|
|
if _ then
|
|
|
|
_[function(l0)end],l0 = _
|
|
|
|
elseif _ then
|
|
|
|
return l0(nil)
|
|
|
|
elseif 1 / l0(nil) then
|
|
|
|
elseif _ then
|
|
|
|
return #_,l0()
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
2022-03-12 00:55:02 +08:00
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
2023-11-11 05:10:07 +08:00
|
|
|
#endif
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "variadic_argument_tail")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local _ = function():((...any)->(...any),()->())
|
2021-11-05 23:47:21 +08:00
|
|
|
return function() end, function() end
|
2021-10-30 04:25:12 +08:00
|
|
|
end
|
|
|
|
for y in _() do
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:34:35 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_packs")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Packed<T...> = (T...) -> T...
|
|
|
|
local a: Packed<>
|
|
|
|
local b: Packed<number>
|
|
|
|
local c: Packed<string, number>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
auto tf = lookupType("Packed");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "(T...) -> (T...)");
|
|
|
|
CHECK_EQ(toString(requireType("a")), "() -> ()");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "(number) -> number");
|
|
|
|
CHECK_EQ(toString(requireType("c")), "(string, number) -> (string, number)");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
-- (U..., T) cannot be parsed right now
|
|
|
|
type Packed<T, U...> = { f: (a: T, U...) -> (T, U...) }
|
|
|
|
local a: Packed<number>
|
|
|
|
local b: Packed<string, number>
|
|
|
|
local c: Packed<string, number, boolean>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
tf = lookupType("Packed");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "Packed<T, U...>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(*tf, {true}), "{ f: (T, U...) -> (T, U...) }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{| f: (T, U...) -> (T, U...) |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
auto ttvA = get<TableType>(requireType("a"));
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvA);
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Packed<number>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{ f: (number) -> number }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{| f: (number) -> number |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvA->instantiatedTypeParams.size() == 1);
|
|
|
|
REQUIRE(ttvA->instantiatedTypePackParams.size() == 1);
|
|
|
|
CHECK_EQ(toString(ttvA->instantiatedTypeParams[0], {true}), "number");
|
|
|
|
CHECK_EQ(toString(ttvA->instantiatedTypePackParams[0], {true}), "");
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
auto ttvB = get<TableType>(requireType("b"));
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvB);
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Packed<string, number>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(requireType("b"), {true}), "{ f: (string, number) -> (string, number) }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(requireType("b"), {true}), "{| f: (string, number) -> (string, number) |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvB->instantiatedTypeParams.size() == 1);
|
|
|
|
REQUIRE(ttvB->instantiatedTypePackParams.size() == 1);
|
|
|
|
CHECK_EQ(toString(ttvB->instantiatedTypeParams[0], {true}), "string");
|
|
|
|
CHECK_EQ(toString(ttvB->instantiatedTypePackParams[0], {true}), "number");
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
auto ttvC = get<TableType>(requireType("c"));
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvC);
|
|
|
|
CHECK_EQ(toString(requireType("c")), "Packed<string, number, boolean>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(requireType("c"), {true}), "{ f: (string, number, boolean) -> (string, number, boolean) }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(requireType("c"), {true}), "{| f: (string, number, boolean) -> (string, number, boolean) |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
REQUIRE(ttvC->instantiatedTypeParams.size() == 1);
|
|
|
|
REQUIRE(ttvC->instantiatedTypePackParams.size() == 1);
|
|
|
|
CHECK_EQ(toString(ttvC->instantiatedTypeParams[0], {true}), "string");
|
|
|
|
CHECK_EQ(toString(ttvC->instantiatedTypePackParams[0], {true}), "number, boolean");
|
|
|
|
}
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_alias_type_packs_import")
|
2021-11-05 10:34:35 +08:00
|
|
|
{
|
|
|
|
fileResolver.source["game/A"] = R"(
|
|
|
|
export type Packed<T, U...> = { a: T, b: (U...) -> () }
|
|
|
|
return {}
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult aResult = frontend.check("game/A");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(aResult);
|
|
|
|
|
|
|
|
CheckResult bResult = check(R"(
|
|
|
|
local Import = require(game.A)
|
|
|
|
local a: Import.Packed<number>
|
|
|
|
local b: Import.Packed<string, number>
|
|
|
|
local c: Import.Packed<string, number, boolean>
|
|
|
|
local d: { a: typeof(c) }
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(bResult);
|
|
|
|
|
|
|
|
auto tf = lookupImportedType("Import", "Packed");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "Packed<T, U...>");
|
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
{
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{ a: T, b: (U...) -> () }");
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{ a: number, b: () -> () }");
|
|
|
|
CHECK_EQ(toString(requireType("b"), {true}), "{ a: string, b: (number) -> () }");
|
|
|
|
CHECK_EQ(toString(requireType("c"), {true}), "{ a: string, b: (number, boolean) -> () }");
|
|
|
|
CHECK_EQ(toString(requireType("d")), "{ a: Packed<string, number, boolean> }");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{| a: T, b: (U...) -> () |}");
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{| a: number, b: () -> () |}");
|
|
|
|
CHECK_EQ(toString(requireType("b"), {true}), "{| a: string, b: (number) -> () |}");
|
|
|
|
CHECK_EQ(toString(requireType("c"), {true}), "{| a: string, b: (number, boolean) -> () |}");
|
|
|
|
CHECK_EQ(toString(requireType("d")), "{| a: Packed<string, number, boolean> |}");
|
|
|
|
}
|
2021-11-05 10:34:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_pack_type_parameters")
|
2021-11-05 10:34:35 +08:00
|
|
|
{
|
|
|
|
fileResolver.source["game/A"] = R"(
|
|
|
|
export type Packed<T, U...> = { a: T, b: (U...) -> () }
|
|
|
|
return {}
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult cResult = check(R"(
|
|
|
|
local Import = require(game.A)
|
|
|
|
type Alias<S, T, R...> = Import.Packed<S, (T, R...)>
|
|
|
|
local a: Alias<string, number, boolean>
|
|
|
|
|
|
|
|
type B<X...> = Import.Packed<string, X...>
|
|
|
|
type C<X...> = Import.Packed<string, (number, X...)>
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(cResult);
|
|
|
|
|
|
|
|
auto tf = lookupType("Alias");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "Alias<S, T, R...>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(*tf, {true}), "{ a: S, b: (T, R...) -> () }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{| a: S, b: (T, R...) -> () |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{ a: string, b: (number, boolean) -> () }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(requireType("a"), {true}), "{| a: string, b: (number, boolean) -> () |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
|
|
|
|
tf = lookupType("B");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "B<X...>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(*tf, {true}), "{ a: string, b: (X...) -> () }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{| a: string, b: (X...) -> () |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
|
|
|
|
tf = lookupType("C");
|
|
|
|
REQUIRE(tf);
|
|
|
|
CHECK_EQ(toString(*tf), "C<X...>");
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(*tf, {true}), "{ a: string, b: (number, X...) -> () }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(*tf, {true}), "{| a: string, b: (number, X...) -> () |}");
|
2021-11-05 10:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_packs_nested")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Packed1<T...> = (T...) -> (T...)
|
|
|
|
type Packed2<T...> = (Packed1<T...>, T...) -> (Packed1<T...>, T...)
|
|
|
|
type Packed3<T...> = (Packed2<T...>, T...) -> (Packed2<T...>, T...)
|
|
|
|
type Packed4<T...> = (Packed3<T...>, T...) -> (Packed3<T...>, T...)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
auto tf = lookupType("Packed4");
|
|
|
|
REQUIRE(tf);
|
2024-08-02 22:30:04 +08:00
|
|
|
CHECK_EQ(
|
|
|
|
toString(*tf),
|
2021-11-05 10:34:35 +08:00
|
|
|
"((((T...) -> (T...), T...) -> ((T...) -> (T...), T...), T...) -> (((T...) -> (T...), T...) -> ((T...) -> (T...), T...), T...), T...) -> "
|
2024-08-02 22:30:04 +08:00
|
|
|
"((((T...) -> (T...), T...) -> ((T...) -> (T...), T...), T...) -> (((T...) -> (T...), T...) -> ((T...) -> (T...), T...), T...), T...)"
|
|
|
|
);
|
2021-11-05 10:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_pack_variadic")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type X<T...> = (T...) -> (string, T...)
|
|
|
|
|
|
|
|
type D = X<...number>
|
|
|
|
type E = X<(number, ...string)>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
auto d = lookupType("D");
|
|
|
|
REQUIRE(d);
|
|
|
|
auto e = lookupType("E");
|
|
|
|
REQUIRE(e);
|
|
|
|
CHECK_EQ(toString(*d), "(...number) -> (string, ...number)");
|
|
|
|
CHECK_EQ(toString(*e), "(number, ...string) -> (string, number, ...string)");
|
2021-11-05 10:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_pack_multi")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U...> = (T...) -> (U...)
|
|
|
|
type A<S...> = Y<S..., S...>
|
|
|
|
type B<S...> = Y<(number, ...string), S...>
|
|
|
|
|
|
|
|
type Z<T, U...> = (T) -> (U...)
|
|
|
|
type E<S...> = Z<number, S...>
|
|
|
|
type F<S...> = Z<number, (string, S...)>
|
|
|
|
|
|
|
|
type W<T, U..., V...> = (T, U...) -> (T, V...)
|
|
|
|
type H<S..., R...> = W<number, S..., R...>
|
|
|
|
type I<S..., R...> = W<number, (string, S...), R...>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("A")), "(S...) -> (S...)");
|
|
|
|
CHECK_EQ(toString(*lookupType("B")), "(number, ...string) -> (S...)");
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("E")), "(number) -> (S...)");
|
|
|
|
CHECK_EQ(toString(*lookupType("F")), "(number) -> (string, S...)");
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("H")), "(number, S...) -> (number, R...)");
|
|
|
|
CHECK_EQ(toString(*lookupType("I")), "(number, string, S...) -> (number, R...)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_pack_explicit")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type X<T...> = (T...) -> (T...)
|
|
|
|
|
|
|
|
type A<S...> = X<(S...)>
|
|
|
|
type B = X<()>
|
|
|
|
type C = X<(number)>
|
|
|
|
type D = X<(number, string)>
|
|
|
|
type E = X<(...number)>
|
|
|
|
type F = X<(string, ...number)>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("A")), "(S...) -> (S...)");
|
|
|
|
CHECK_EQ(toString(*lookupType("B")), "() -> ()");
|
|
|
|
CHECK_EQ(toString(*lookupType("C")), "(number) -> number");
|
|
|
|
CHECK_EQ(toString(*lookupType("D")), "(number, string) -> (number, string)");
|
|
|
|
CHECK_EQ(toString(*lookupType("E")), "(...number) -> (...number)");
|
|
|
|
CHECK_EQ(toString(*lookupType("F")), "(string, ...number) -> (string, ...number)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_pack_explicit_multi")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U...> = (T...) -> (U...)
|
|
|
|
|
|
|
|
type A = Y<(number, string), (boolean)>
|
|
|
|
type B = Y<(), ()>
|
|
|
|
type C<S...> = Y<...string, (number, S...)>
|
|
|
|
type D<X...> = Y<X..., (number, string, X...)>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("A")), "(number, string) -> boolean");
|
|
|
|
CHECK_EQ(toString(*lookupType("B")), "() -> ()");
|
|
|
|
CHECK_EQ(toString(*lookupType("C")), "(...string) -> (number, S...)");
|
|
|
|
CHECK_EQ(toString(*lookupType("D")), "(X...) -> (number, string, X...)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_pack_explicit_multi_tostring")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U...> = { f: (T...) -> (U...) }
|
|
|
|
|
|
|
|
local a: Y<(number, string), (boolean)>
|
|
|
|
local b: Y<(), ()>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<(number, string), (boolean)>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<(), ()>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_backwards_compatible")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2024-08-17 02:29:33 +08:00
|
|
|
type X<T> = () -> T
|
|
|
|
type Y<T, U> = (T) -> U
|
2021-11-05 10:34:35 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
type A = X<(number)>
|
|
|
|
type B = Y<(number), (boolean)>
|
|
|
|
type C = Y<(number), boolean>
|
2021-11-05 10:34:35 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("A")), "() -> number");
|
|
|
|
CHECK_EQ(toString(*lookupType("B")), "(number) -> boolean");
|
|
|
|
CHECK_EQ(toString(*lookupType("C")), "(number) -> boolean");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_type_packs_errors")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Packed<T, U, V...> = (T, U) -> (V...)
|
|
|
|
local b: Packed<number>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Packed<T, U, V...>' expects at least 2 type arguments, but only 1 is specified");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T, U> = (T, U) -> ()
|
|
|
|
type B<X...> = Packed<number, string, X...>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Packed<T, U>' expects 0 type pack arguments, but 1 is specified");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T..., U...> = (T...) -> (U...)
|
|
|
|
type Other<S...> = Packed<S..., string>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Type parameters must come before type pack parameters");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T, U> = (T) -> U
|
|
|
|
type Other<S...> = Packed<number, S...>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Packed<T, U>' expects 2 type arguments, but only 1 is specified");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T...> = (T...) -> T...
|
|
|
|
local a: Packed
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Type parameter list is required");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T..., U...> = (T...) -> (U...)
|
|
|
|
type Other = Packed<>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Packed<T..., U...>' expects 2 type pack arguments, but none are specified");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Packed<T..., U...> = (T...) -> (U...)
|
|
|
|
type Other = Packed<number, string>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Packed<T..., U...>' expects 2 type pack arguments, but only 1 is specified");
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:20:09 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_explicit")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U = string> = { a: T, b: U }
|
|
|
|
|
|
|
|
local a: Y<number, number> = { a = 2, b = 3 }
|
|
|
|
local b: Y<number> = { a = 2, b = "s" }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, number>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<number, string>");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Y<T = string> = { a: T }
|
|
|
|
|
|
|
|
local a: Y<number> = { a = 2 }
|
|
|
|
local b: Y<> = { a = "s" }
|
|
|
|
local c: Y = { a = "s" }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<string>");
|
|
|
|
CHECK_EQ(toString(requireType("c")), "Y<string>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_self")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U = T> = { a: T, b: U }
|
|
|
|
|
|
|
|
local a: Y<number> = { a = 2, b = 3 }
|
|
|
|
local b: Y<string> = { a = "h", b = "s" }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, number>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<string, string>");
|
|
|
|
|
|
|
|
result = check(R"(
|
|
|
|
type Y<T, U = (T, T) -> string> = { a: T, b: U }
|
|
|
|
|
|
|
|
local a: Y<number>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, (number, number) -> string>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_chained")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U = T, V = U> = { a: T, b: U, c: V }
|
|
|
|
|
|
|
|
local a: Y<number>
|
|
|
|
local b: Y<number, string>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, number, number>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<number, string, string>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_pack_explicit")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T... = (string, number)> = { a: (T...) -> () }
|
|
|
|
local a: Y<>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<string, number>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_pack_self_ty")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U... = ...T> = { a: T, b: (U...) -> T }
|
|
|
|
|
|
|
|
local a: Y<number>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, ...number>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_pack_self_tp")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U... = T...> = { a: (T...) -> U... }
|
|
|
|
local a: Y<number, string>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<(number, string), (number, string)>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_pack_self_chained_tp")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U... = T..., V... = U...> = { a: (T...) -> U..., b: (T...) -> V... }
|
|
|
|
local a: Y<number, string>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<(number, string), (number, string), (number, string)>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_mixed_self")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U = T, V... = ...number, W... = (T, U, V...)> = { a: (T, U, V...) -> W... }
|
|
|
|
local a: Y<number>
|
|
|
|
local b: Y<number, string>
|
|
|
|
local c: Y<number, string, ...boolean>
|
|
|
|
local d: Y<number, string, ...boolean, ...() -> ()>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "Y<number, number, ...number, (number, number, ...number)>");
|
|
|
|
CHECK_EQ(toString(requireType("b")), "Y<number, string, ...number, (number, string, ...number)>");
|
|
|
|
CHECK_EQ(toString(requireType("c")), "Y<number, string, ...boolean, (number, string, ...boolean)>");
|
|
|
|
CHECK_EQ(toString(requireType("d")), "Y<number, string, ...boolean, ...() -> ()>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors")
|
|
|
|
{
|
2024-11-02 03:06:07 +08:00
|
|
|
DOES_NOT_PASS_NEW_SOLVER_GUARD();
|
2024-08-17 02:29:33 +08:00
|
|
|
|
2022-01-15 00:20:09 +08:00
|
|
|
CheckResult result = check(R"(
|
2024-08-17 02:29:33 +08:00
|
|
|
type Y<T = T> = { a: T }
|
|
|
|
local a: Y = { a = 2 }
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Unknown type 'T'");
|
2024-08-17 02:29:33 +08:00
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors2")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T... = T...> = { a: (T...) -> () }
|
|
|
|
local a: Y<>
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Unknown type 'T'");
|
2024-08-17 02:29:33 +08:00
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors3")
|
|
|
|
{
|
2024-11-02 03:06:07 +08:00
|
|
|
DOES_NOT_PASS_NEW_SOLVER_GUARD();
|
2024-08-17 02:29:33 +08:00
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T = string, U... = ...string> = { a: (T) -> U... }
|
|
|
|
local a: Y<...number>
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Generic type 'Y<T, U...>' expects at least 1 type argument, but none are specified");
|
2024-08-17 02:29:33 +08:00
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors4")
|
|
|
|
{
|
2024-11-02 03:06:07 +08:00
|
|
|
DOES_NOT_PASS_NEW_SOLVER_GUARD();
|
2024-08-17 02:29:33 +08:00
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Packed<T> = (T) -> T
|
|
|
|
local a: Packed
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Type parameter list is required");
|
2024-08-17 02:29:33 +08:00
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors5")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T, U = T, V> = { a: T }
|
|
|
|
local a: Y<number>
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
2024-08-17 02:29:33 +08:00
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
|
2024-08-17 02:29:33 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_errors6")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T..., U... = T..., V...> = { a: T }
|
|
|
|
local a: Y<...number>
|
2022-01-15 00:20:09 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_alias_default_export")
|
2022-01-15 00:20:09 +08:00
|
|
|
{
|
|
|
|
fileResolver.source["Module/Types"] = R"(
|
|
|
|
export type A<T, U = string> = { a: T, b: U }
|
|
|
|
export type B<T, U = T> = { a: T, b: U }
|
|
|
|
export type C<T, U = (T, T) -> string> = { a: T, b: U }
|
|
|
|
export type D<T, U = T, V = U> = { a: T, b: U, c: V }
|
|
|
|
export type E<T... = (string, number)> = { a: (T...) -> () }
|
|
|
|
export type F<T, U... = ...T> = { a: T, b: (U...) -> T }
|
|
|
|
export type G<T..., U... = ()> = { b: (U...) -> T... }
|
|
|
|
export type H<T... = ()> = { b: (T...) -> T... }
|
|
|
|
return {}
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult resultTypes = frontend.check("Module/Types");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(resultTypes);
|
|
|
|
|
|
|
|
fileResolver.source["Module/Users"] = R"(
|
|
|
|
local Types = require(script.Parent.Types)
|
|
|
|
|
|
|
|
local a: Types.A<number>
|
|
|
|
local b: Types.B<number>
|
|
|
|
local c: Types.C<number>
|
|
|
|
local d: Types.D<number>
|
|
|
|
local e: Types.E<>
|
|
|
|
local eVoid: Types.E<()>
|
|
|
|
local f: Types.F<number>
|
|
|
|
local g: Types.G<...number>
|
|
|
|
local h: Types.H<>
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult resultUsers = frontend.check("Module/Users");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(resultUsers);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "a")), "A<number, string>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "b")), "B<number, number>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "c")), "C<number, (number, number) -> string>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "d")), "D<number, number, number>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "e")), "E<string, number>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "eVoid")), "E<>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "f")), "F<number, ...number>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "g")), "G<...number, ()>");
|
|
|
|
CHECK_EQ(toString(requireType("Module/Users", "h")), "H<>");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_default_type_skip_brackets")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Y<T... = ...string> = (T...) -> number
|
|
|
|
local a: Y
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(requireType("a")), "(...string) -> number");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_defaults_confusing_types")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type A<T, U = T, V... = ...any, W... = V...> = (T, V...) -> (U, W...)
|
|
|
|
type B = A<string, (number)>
|
|
|
|
type C = A<string, (number), (boolean)>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(toString(*lookupType("B"), {true}), "(string, ...any) -> (number, ...any)");
|
|
|
|
CHECK_EQ(toString(*lookupType("C"), {true}), "(string, boolean) -> (number, boolean)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_alias_defaults_recursive_type")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type F<K = string, V = (K) -> ()> = (K) -> V
|
|
|
|
type R = { m: F<R> }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-09-30 09:13:05 +08:00
|
|
|
CHECK_EQ(toString(*lookupType("R"), {true}), "t1 where t1 = { m: (t1) -> (t1) -> () }");
|
|
|
|
else
|
|
|
|
CHECK_EQ(toString(*lookupType("R"), {true}), "t1 where t1 = {| m: (t1) -> (t1) -> () |}");
|
2022-01-15 00:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "pack_tail_unification_check")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local a: () -> (number, ...string)
|
|
|
|
local b: () -> (number, ...boolean)
|
|
|
|
a = b
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2024-08-17 02:29:33 +08:00
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2024-08-17 02:29:33 +08:00
|
|
|
{
|
|
|
|
const std::string expected =
|
|
|
|
"Type\n"
|
|
|
|
" '() -> (number, ...boolean)'\n"
|
|
|
|
"could not be converted into\n"
|
|
|
|
" '() -> (number, ...string)'; at returns().tail().variadic(), boolean is not a subtype of string";
|
|
|
|
|
|
|
|
CHECK(expected == toString(result.errors[0]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::string expected = R"(Type
|
2023-08-11 22:42:37 +08:00
|
|
|
'() -> (number, ...boolean)'
|
|
|
|
could not be converted into
|
|
|
|
'() -> (number, ...string)'
|
2022-01-15 00:20:09 +08:00
|
|
|
caused by:
|
2023-08-11 22:42:37 +08:00
|
|
|
Type 'boolean' could not be converted into 'string')";
|
2024-08-17 02:29:33 +08:00
|
|
|
CHECK_EQ(expected, toString(result.errors[0]));
|
|
|
|
}
|
2022-01-15 00:20:09 +08:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:46:04 +08:00
|
|
|
// TODO: File a Jira about this
|
|
|
|
/*
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unifying_vararg_pack_with_fixed_length_pack_produces_fixed_length_pack")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function a(x) return 1 end
|
|
|
|
a(...)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
ModulePtr mainModule = getMainModule();
|
|
|
|
REQUIRE(mainModule);
|
|
|
|
REQUIRE(mainModule->hasModuleScope());
|
2022-03-18 08:46:04 +08:00
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
REQUIRE(bool(mainModule->getModuleScope()->varargPack));
|
|
|
|
|
|
|
|
TypePackId varargPack = *mainModule->getModuleScope()->varargPack;
|
2022-03-18 08:46:04 +08:00
|
|
|
|
|
|
|
auto iter = begin(varargPack);
|
|
|
|
auto endIter = end(varargPack);
|
|
|
|
|
|
|
|
CHECK(iter != endIter);
|
|
|
|
++iter;
|
|
|
|
CHECK(iter == endIter);
|
|
|
|
|
|
|
|
CHECK(!iter.tail());
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_ice_if_a_TypePack_is_an_error")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!strict
|
|
|
|
function f(s)
|
|
|
|
print(s)
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
|
|
|
f("foo")("bar")
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "cyclic_type_packs")
|
|
|
|
{
|
|
|
|
// this has a risk of creating cyclic type packs, causing infinite loops / OOMs
|
|
|
|
check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
_ += _(_,...)
|
|
|
|
repeat
|
|
|
|
_ += _(...)
|
|
|
|
until ... + _
|
|
|
|
)");
|
|
|
|
|
|
|
|
check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
_ += _(_(...,...),_(...))
|
|
|
|
repeat
|
|
|
|
until _
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2022-05-27 06:08:16 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "detect_cyclic_typepacks")
|
2022-03-18 08:46:04 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type ( ... ) ( ) ;
|
|
|
|
( ... ) ( - - ... ) ( - ... )
|
|
|
|
type = ( ... ) ;
|
|
|
|
( ... ) ( ) ( ... ) ;
|
|
|
|
( ... ) ""
|
|
|
|
)");
|
|
|
|
|
2022-05-27 06:08:16 +08:00
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
2022-03-18 08:46:04 +08:00
|
|
|
}
|
|
|
|
|
2022-05-27 06:08:16 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "detect_cyclic_typepacks2")
|
2022-03-18 08:46:04 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function _(l0:((typeof((pcall)))|((((t0)->())|(typeof(-67108864)))|(any)))|(any),...):(((typeof(0))|(any))|(any),typeof(-67108864),any)
|
|
|
|
xpcall(_,_,_)
|
|
|
|
_(_,_,_)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
2024-01-13 06:25:27 +08:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
|
|
|
|
CHECK("Unknown type 't0'" == toString(result.errors[0]));
|
|
|
|
CHECK(get<FunctionExitsWithoutReturning>(result.errors[1]));
|
2022-03-18 08:46:04 +08:00
|
|
|
}
|
|
|
|
|
2022-09-16 06:38:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "unify_variadic_tails_in_arguments")
|
|
|
|
{
|
2024-11-02 03:06:07 +08:00
|
|
|
DOES_NOT_PASS_NEW_SOLVER_GUARD();
|
2024-08-17 02:29:33 +08:00
|
|
|
|
2022-09-16 06:38:17 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
function foo(...: string): number
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function bar(...: number): number
|
|
|
|
return foo(...)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Type 'number' could not be converted into 'string'");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unify_variadic_tails_in_arguments_free")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function foo<T...>(...: T...): T...
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
|
|
|
|
function bar(...: number): boolean
|
|
|
|
return foo(...)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2024-08-02 22:30:04 +08:00
|
|
|
CHECK(
|
|
|
|
toString(result.errors.at(0)) ==
|
|
|
|
"Type pack '...number' could not be converted into 'boolean'; type ...number.tail() (...number) is not a subtype of boolean (boolean)"
|
|
|
|
);
|
2023-11-11 05:10:07 +08:00
|
|
|
else
|
|
|
|
CHECK_EQ(toString(result.errors[0]), "Type 'number' could not be converted into 'boolean'");
|
2022-09-16 06:38:17 +08:00
|
|
|
}
|
|
|
|
|
2022-10-07 08:23:29 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_packs_with_tails_in_vararg_adjustment")
|
|
|
|
{
|
2022-12-03 02:09:59 +08:00
|
|
|
std::optional<ScopedFastFlag> sff;
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-12-02 15:46:57 +08:00
|
|
|
sff = {FFlag::LuauInstantiateInSubtyping, true};
|
2022-12-03 02:09:59 +08:00
|
|
|
|
2022-10-07 08:23:29 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function wrapReject<TArg, TResult>(fn: (self: any, ...TArg) -> ...TResult): (self: any, ...TArg) -> ...TResult
|
|
|
|
return function(self, ...)
|
|
|
|
local arguments = { ... }
|
|
|
|
local ok, result = pcall(function()
|
|
|
|
return fn(self, table.unpack(arguments))
|
|
|
|
end)
|
|
|
|
return result
|
|
|
|
end
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-04 03:26:13 +08:00
|
|
|
end
|
2022-10-07 08:23:29 +08:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:09:59 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "generalize_expectedTypes_with_proper_scope")
|
|
|
|
{
|
|
|
|
ScopedFastFlag sff[] = {
|
2024-08-31 04:16:51 +08:00
|
|
|
{FFlag::LuauSolverV2, true},
|
2023-12-02 15:46:57 +08:00
|
|
|
{FFlag::LuauInstantiateInSubtyping, true},
|
2022-12-03 02:09:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function f<TResult>(fn: () -> ...TResult): () -> ...TResult
|
|
|
|
return function()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "fuzz_typepack_iter_follow")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local _
|
|
|
|
local _ = _,_(),_(_)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "fuzz_typepack_iter_follow_2")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function test(name, searchTerm)
|
|
|
|
local found = string.find(name:lower(), searchTerm:lower())
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2023-05-20 03:37:30 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_param_overflow")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type Two<T,U> = { a: T, b: U }
|
|
|
|
local x: Two<number, string, number> = { a = 1, b = 'c' }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_END();
|