2023-05-12 18:50:47 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/TypeFamily.h"
|
2023-09-08 01:13:49 +01:00
|
|
|
|
2023-09-22 20:12:15 +01:00
|
|
|
#include "Luau/ConstraintSolver.h"
|
2023-10-06 20:02:32 +01:00
|
|
|
#include "Luau/NotNull.h"
|
2023-09-08 01:13:49 +01:00
|
|
|
#include "Luau/TxnLog.h"
|
2023-05-12 18:50:47 +01:00
|
|
|
#include "Luau/Type.h"
|
|
|
|
|
2024-01-12 22:25:27 +00:00
|
|
|
#include "ClassFixture.h"
|
2023-05-12 18:50:47 +01:00
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
|
|
|
LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution)
|
|
|
|
|
|
|
|
struct FamilyFixture : Fixture
|
|
|
|
{
|
|
|
|
TypeFamily swapFamily;
|
|
|
|
|
|
|
|
FamilyFixture()
|
|
|
|
: Fixture(true, false)
|
|
|
|
{
|
|
|
|
swapFamily = TypeFamily{/* name */ "Swap",
|
|
|
|
/* reducer */
|
2024-02-16 02:04:39 +00:00
|
|
|
[](TypeId instance, std::vector<TypeId> tys, std::vector<TypePackId> tps,
|
|
|
|
NotNull<TypeFamilyContext> ctx) -> TypeFamilyReductionResult<TypeId> {
|
2023-05-12 18:50:47 +01:00
|
|
|
LUAU_ASSERT(tys.size() == 1);
|
2023-10-06 20:02:32 +01:00
|
|
|
TypeId param = follow(tys.at(0));
|
2023-05-12 18:50:47 +01:00
|
|
|
|
|
|
|
if (isString(param))
|
|
|
|
{
|
2023-10-06 20:02:32 +01:00
|
|
|
return TypeFamilyReductionResult<TypeId>{ctx->builtins->numberType, false, {}, {}};
|
2023-05-12 18:50:47 +01:00
|
|
|
}
|
|
|
|
else if (isNumber(param))
|
|
|
|
{
|
2023-10-06 20:02:32 +01:00
|
|
|
return TypeFamilyReductionResult<TypeId>{ctx->builtins->stringType, false, {}, {}};
|
2023-05-12 18:50:47 +01:00
|
|
|
}
|
2023-10-06 20:02:32 +01:00
|
|
|
else if (is<BlockedType>(param) || is<PendingExpansionType>(param) || is<TypeFamilyInstanceType>(param) ||
|
|
|
|
(ctx->solver && ctx->solver->hasUnresolvedConstraints(param)))
|
2023-05-12 18:50:47 +01:00
|
|
|
{
|
|
|
|
return TypeFamilyReductionResult<TypeId>{std::nullopt, false, {param}, {}};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return TypeFamilyReductionResult<TypeId>{std::nullopt, true, {}, {}};
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
unfreeze(frontend.globals.globalTypes);
|
|
|
|
TypeId t = frontend.globals.globalTypes.addType(GenericType{"T"});
|
|
|
|
GenericTypeDefinition genericT{t};
|
|
|
|
|
|
|
|
ScopePtr globalScope = frontend.globals.globalScope;
|
|
|
|
globalScope->exportedTypeBindings["Swap"] =
|
|
|
|
TypeFun{{genericT}, frontend.globals.globalTypes.addType(TypeFamilyInstanceType{NotNull{&swapFamily}, {t}, {}})};
|
|
|
|
freeze(frontend.globals.globalTypes);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("TypeFamilyTests");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "basic_type_family")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type A = Swap<number>
|
|
|
|
type B = Swap<string>
|
|
|
|
type C = Swap<boolean>
|
|
|
|
|
|
|
|
local x = 123
|
|
|
|
local y: Swap<typeof(x)> = "foo"
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK("string" == toString(requireTypeAlias("A")));
|
|
|
|
CHECK("number" == toString(requireTypeAlias("B")));
|
|
|
|
CHECK("Swap<boolean>" == toString(requireTypeAlias("C")));
|
|
|
|
CHECK("string" == toString(requireType("y")));
|
|
|
|
CHECK("Type family instance Swap<boolean> is uninhabited" == toString(result.errors[0]));
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "family_as_fn_ret")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local swapper: <T>(T) -> Swap<T>
|
|
|
|
local a = swapper(123)
|
|
|
|
local b = swapper("foo")
|
|
|
|
local c = swapper(false)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK("string" == toString(requireType("a")));
|
|
|
|
CHECK("number" == toString(requireType("b")));
|
|
|
|
CHECK("Swap<boolean>" == toString(requireType("c")));
|
|
|
|
CHECK("Type family instance Swap<boolean> is uninhabited" == toString(result.errors[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "family_as_fn_arg")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local swapper: <T>(Swap<T>) -> T
|
|
|
|
local a = swapper(123)
|
|
|
|
local b = swapper(false)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
// FIXME: Can we constrain these to `never` or `unknown`?
|
|
|
|
CHECK("a" == toString(requireType("a")));
|
|
|
|
CHECK("a" == toString(requireType("b")));
|
|
|
|
CHECK("Type family instance Swap<a> is uninhabited" == toString(result.errors[0]));
|
|
|
|
CHECK("Type family instance Swap<a> is uninhabited" == toString(result.errors[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "resolve_deep_families")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local x: Swap<Swap<Swap<string>>>
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK("number" == toString(requireType("x")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "unsolvable_family")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local impossible: <T>(Swap<T>) -> Swap<Swap<T>>
|
|
|
|
local a = impossible(123)
|
|
|
|
local b = impossible(true)
|
|
|
|
)");
|
|
|
|
|
2023-06-24 07:19:39 +01:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
for (size_t i = 0; i < 2; ++i)
|
2023-05-12 18:50:47 +01:00
|
|
|
{
|
|
|
|
CHECK(toString(result.errors[i]) == "Type family instance Swap<a> is uninhabited");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "table_internal_families")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local t: <T>({T}) -> {Swap<T>}
|
|
|
|
local a = t({1, 2, 3})
|
|
|
|
local b = t({"a", "b", "c"})
|
|
|
|
local c = t({true, false, true})
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK(toString(requireType("a")) == "{string}");
|
|
|
|
CHECK(toString(requireType("b")) == "{number}");
|
|
|
|
CHECK(toString(requireType("c")) == "{Swap<boolean>}");
|
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance Swap<boolean> is uninhabited");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(FamilyFixture, "function_internal_families")
|
|
|
|
{
|
|
|
|
// This test is broken right now, but it's not because of type families. See
|
|
|
|
// CLI-71143.
|
|
|
|
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local f0: <T>(T) -> (() -> T)
|
|
|
|
local f: <T>(T) -> (() -> Swap<T>)
|
|
|
|
local a = f(1)
|
|
|
|
local b = f("a")
|
|
|
|
local c = f(true)
|
|
|
|
local d = f0(1)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK(toString(requireType("a")) == "() -> string");
|
|
|
|
CHECK(toString(requireType("b")) == "() -> number");
|
|
|
|
CHECK(toString(requireType("c")) == "() -> Swap<boolean>");
|
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance Swap<boolean> is uninhabited");
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:37:30 +01:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "add_family_at_work")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function add(a, b)
|
|
|
|
return a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
local a = add(1, 2)
|
|
|
|
local b = add(1, "foo")
|
|
|
|
local c = add("foo", 1)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
CHECK(toString(requireType("a")) == "number");
|
|
|
|
CHECK(toString(requireType("b")) == "Add<number, string>");
|
|
|
|
CHECK(toString(requireType("c")) == "Add<string, number>");
|
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance Add<number, string> is uninhabited");
|
|
|
|
CHECK(toString(result.errors[1]) == "Type family instance Add<string, number> is uninhabited");
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "internal_families_raise_errors")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local function innerSum(a, b)
|
|
|
|
local _ = a + b
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2023-07-14 19:08:53 +01:00
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance Add<a, b> depends on generic function parameters but does not appear in the function "
|
|
|
|
"signature; this construct cannot be type-checked at this time");
|
2023-05-25 22:36:34 +01:00
|
|
|
}
|
|
|
|
|
2024-01-12 22:25:27 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_families_can_be_shadowed")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type add<T> = string -- shadow add
|
|
|
|
|
|
|
|
-- this should be ok
|
|
|
|
function hi(f: add<unknown>)
|
|
|
|
return string.format("hi %s", f)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- this should still work totally fine (and use the real type family)
|
|
|
|
function plus(a, b)
|
|
|
|
return a + b
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK(toString(requireType("hi")) == "(string) -> string");
|
|
|
|
CHECK(toString(requireType("plus")) == "<a, b>(a, b) -> add<a, b>");
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "type_families_inhabited_with_normalization")
|
|
|
|
{
|
2023-09-22 20:12:15 +01:00
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
2023-05-25 22:36:34 +01:00
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local useGridConfig : any
|
|
|
|
local columns = useGridConfig("columns", {}) or 1
|
|
|
|
local gutter = useGridConfig('gutter', {}) or 0
|
|
|
|
local margin = useGridConfig('margin', {}) or 0
|
|
|
|
return function(frameAbsoluteWidth: number)
|
|
|
|
local cellAbsoluteWidth = (frameAbsoluteWidth - 2 * margin + gutter) / columns - gutter
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2024-01-12 22:25:27 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_works")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = keyof<MyObject>
|
|
|
|
|
|
|
|
local function ok(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"x\" | \"y\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"x\" | \"y\" | \"z\"", toString(tpm->givenTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_works_with_metatables")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local metatable = { __index = {w = 1} }
|
|
|
|
local obj = setmetatable({x = 1, y = 2, z = 3}, metatable)
|
|
|
|
type MyObject = typeof(obj)
|
|
|
|
type KeysOfMyObject = keyof<MyObject>
|
|
|
|
|
|
|
|
local function ok(idx: KeysOfMyObject): "w" | "x" | "y" | "z" return idx end
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"x\" | \"y\" | \"z\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"w\" | \"x\" | \"y\" | \"z\"", toString(tpm->givenTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_errors_if_it_has_nontable_part")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = keyof<MyObject | boolean>
|
|
|
|
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
)");
|
|
|
|
|
2024-02-02 21:32:42 +00:00
|
|
|
// FIXME(CLI-95289): we should actually only report the type family being uninhabited error at its first use, I think?
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2024-01-12 22:25:27 +00:00
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance keyof<MyObject | boolean> is uninhabited");
|
|
|
|
CHECK(toString(result.errors[1]) == "Type family instance keyof<MyObject | boolean> is uninhabited");
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_string_indexer")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type MyOtherObject = { [string]: number }
|
|
|
|
type KeysOfMyOtherObject = keyof<MyOtherObject>
|
|
|
|
type KeysOfMyObjects = keyof<MyObject | MyOtherObject>
|
|
|
|
|
|
|
|
local function ok(idx: KeysOfMyOtherObject): "z" return idx end
|
|
|
|
local function err(idx: KeysOfMyObjects): "z" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"z\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("string", toString(tpm->givenTp));
|
|
|
|
|
|
|
|
tpm = get<TypePackMismatch>(result.errors[1]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"z\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"x\" | \"y\" | \"z\"", toString(tpm->givenTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_common_subset_if_union_of_differing_tables")
|
2024-01-12 22:25:27 +00:00
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type MyOtherObject = { w: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = keyof<MyObject | MyOtherObject>
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
local function err(idx: KeysOfMyObject): "z" return idx end
|
2024-01-12 22:25:27 +00:00
|
|
|
)");
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"z\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"y\" | \"z\"", toString(tpm->givenTp));
|
2024-01-12 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_type_family_never_for_empty_table")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type KeyofEmpty = keyof<{}>
|
|
|
|
|
|
|
|
local foo = ((nil :: any) :: KeyofEmpty)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK(toString(requireType("foo")) == "never");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "rawkeyof_type_family_works")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = rawkeyof<MyObject>
|
|
|
|
|
|
|
|
local function ok(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"x\" | \"y\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"x\" | \"y\" | \"z\"", toString(tpm->givenTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "rawkeyof_type_family_ignores_metatables")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local metatable = { __index = {w = 1} }
|
|
|
|
local obj = setmetatable({x = 1, y = 2, z = 3}, metatable)
|
|
|
|
type MyObject = typeof(obj)
|
|
|
|
type KeysOfMyObject = rawkeyof<MyObject>
|
|
|
|
|
|
|
|
local function ok(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"x\" | \"y\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"x\" | \"y\" | \"z\"", toString(tpm->givenTp));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "rawkeyof_type_family_errors_if_it_has_nontable_part")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = rawkeyof<MyObject | boolean>
|
|
|
|
|
|
|
|
local function err(idx: KeysOfMyObject): "x" | "y" | "z" return idx end
|
|
|
|
)");
|
|
|
|
|
2024-02-02 21:32:42 +00:00
|
|
|
// FIXME(CLI-95289): we should actually only report the type family being uninhabited error at its first use, I think?
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2024-01-12 22:25:27 +00:00
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance rawkeyof<MyObject | boolean> is uninhabited");
|
|
|
|
CHECK(toString(result.errors[1]) == "Type family instance rawkeyof<MyObject | boolean> is uninhabited");
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "rawkeyof_type_family_common_subset_if_union_of_differing_tables")
|
2024-01-12 22:25:27 +00:00
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type MyObject = { x: number, y: number, z: number }
|
|
|
|
type MyOtherObject = { w: number, y: number, z: number }
|
|
|
|
type KeysOfMyObject = rawkeyof<MyObject | MyOtherObject>
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
local function err(idx: KeysOfMyObject): "z" return idx end
|
2024-01-12 22:25:27 +00:00
|
|
|
)");
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"z\"", toString(tpm->wantedTp));
|
|
|
|
CHECK_EQ("\"y\" | \"z\"", toString(tpm->givenTp));
|
2024-01-12 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "rawkeyof_type_family_never_for_empty_table")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type RawkeyofEmpty = rawkeyof<{}>
|
|
|
|
|
|
|
|
local foo = ((nil :: any) :: RawkeyofEmpty)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK(toString(requireType("foo")) == "never");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ClassFixture, "keyof_type_family_works_on_classes")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type KeysOfMyObject = keyof<BaseClass>
|
|
|
|
|
2024-03-01 18:45:26 +00:00
|
|
|
local function ok(idx: KeysOfMyObject): "BaseMethod" | "BaseField" | "Touched" return idx end
|
2024-01-12 22:25:27 +00:00
|
|
|
local function err(idx: KeysOfMyObject): "BaseMethod" return idx end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
TypePackMismatch* tpm = get<TypePackMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tpm);
|
|
|
|
CHECK_EQ("\"BaseMethod\"", toString(tpm->wantedTp));
|
2024-03-01 18:45:26 +00:00
|
|
|
CHECK_EQ("\"BaseField\" | \"BaseMethod\" | \"Touched\"", toString(tpm->givenTp));
|
2024-01-12 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ClassFixture, "keyof_type_family_errors_if_it_has_nonclass_part")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type KeysOfMyObject = keyof<BaseClass | boolean>
|
|
|
|
|
|
|
|
local function err(idx: KeysOfMyObject): "BaseMethod" | "BaseField" return idx end
|
|
|
|
)");
|
|
|
|
|
2024-02-02 21:32:42 +00:00
|
|
|
// FIXME(CLI-95289): we should actually only report the type family being uninhabited error at its first use, I think?
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2024-01-12 22:25:27 +00:00
|
|
|
CHECK(toString(result.errors[0]) == "Type family instance keyof<BaseClass | boolean> is uninhabited");
|
|
|
|
CHECK(toString(result.errors[1]) == "Type family instance keyof<BaseClass | boolean> is uninhabited");
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
TEST_CASE_FIXTURE(ClassFixture, "keyof_type_family_common_subset_if_union_of_differing_classes")
|
2024-01-12 22:25:27 +00:00
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type KeysOfMyObject = keyof<BaseClass | Vector2>
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
local function ok(idx: KeysOfMyObject): never return idx end
|
2024-01-12 22:25:27 +00:00
|
|
|
)");
|
|
|
|
|
2024-01-19 18:04:46 +00:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2024-01-12 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 00:47:53 +00:00
|
|
|
TEST_CASE_FIXTURE(ClassFixture, "vector2_multiply_is_overloaded")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local v = Vector2.New(1, 2)
|
|
|
|
|
|
|
|
local v2 = v * 1.5
|
|
|
|
local v3 = v * v
|
|
|
|
local v4 = v * "Hello" -- line 5
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
CHECK(5 == result.errors[0].location.begin.line);
|
|
|
|
CHECK(5 == result.errors[0].location.end.line);
|
|
|
|
|
|
|
|
CHECK("Vector2" == toString(requireType("v2")));
|
|
|
|
CHECK("Vector2" == toString(requireType("v3")));
|
|
|
|
CHECK("mul<Vector2, string>" == toString(requireType("v4")));
|
|
|
|
}
|
|
|
|
|
2024-01-12 22:25:27 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_rfc_example")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local animals = {
|
|
|
|
cat = { speak = function() print "meow" end },
|
|
|
|
dog = { speak = function() print "woof woof" end },
|
|
|
|
monkey = { speak = function() print "oo oo" end },
|
|
|
|
fox = { speak = function() print "gekk gekk" end }
|
|
|
|
}
|
|
|
|
|
|
|
|
type AnimalType = keyof<typeof(animals)>
|
|
|
|
|
|
|
|
function speakByType(animal: AnimalType)
|
|
|
|
animals[animal].speak()
|
|
|
|
end
|
|
|
|
|
|
|
|
speakByType("dog") -- ok
|
|
|
|
speakByType("cactus") -- errors
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
2024-01-27 03:20:56 +00:00
|
|
|
TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
|
|
|
|
REQUIRE(tm);
|
|
|
|
CHECK_EQ("\"cat\" | \"dog\" | \"fox\" | \"monkey\"", toString(tm->wantedType));
|
|
|
|
CHECK_EQ("\"cactus\"", toString(tm->givenType));
|
2024-01-12 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:51:12 +00:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "keyof_oss_crash_gh1161")
|
|
|
|
{
|
|
|
|
if (!FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local EnumVariants = {
|
|
|
|
["a"] = 1, ["b"] = 2, ["c"] = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
type EnumKey = keyof<typeof(EnumVariants)>
|
|
|
|
|
|
|
|
function fnA<T>(i: T): keyof<T> end
|
|
|
|
|
|
|
|
function fnB(i: EnumKey) end
|
|
|
|
|
|
|
|
local result = fnA(EnumVariants)
|
|
|
|
fnB(result)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
CHECK(get<FunctionExitsWithoutReturning>(result.errors[0]));
|
|
|
|
}
|
|
|
|
|
2023-05-12 18:50:47 +01:00
|
|
|
TEST_SUITE_END();
|