2022-04-14 22:57:15 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
2022-09-23 19:32:10 +01:00
|
|
|
#include "Luau/Common.h"
|
2022-11-04 17:02:37 +00:00
|
|
|
#include "Luau/TypeVar.h"
|
2022-04-14 22:57:15 +01:00
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
#include "Luau/Normalize.h"
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct IsSubtypeFixture : Fixture
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
2022-08-18 22:04:33 +01:00
|
|
|
bool isSubtype(TypeId a, TypeId b)
|
|
|
|
{
|
2022-09-08 22:44:50 +01:00
|
|
|
return ::Luau::isSubtype(a, b, NotNull{getMainModule()->getModuleScope().get()}, singletonTypes, ice);
|
2022-08-18 22:04:33 +01:00
|
|
|
}
|
2022-04-14 22:57:15 +01:00
|
|
|
};
|
2022-11-04 17:02:37 +00:00
|
|
|
} // namespace
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
void createSomeClasses(Frontend& frontend)
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
2022-09-08 22:44:50 +01:00
|
|
|
auto& arena = frontend.globalTypes;
|
2022-04-14 22:57:15 +01:00
|
|
|
|
|
|
|
unfreeze(arena);
|
|
|
|
|
2022-04-21 22:04:22 +01:00
|
|
|
TypeId parentType = arena.addType(ClassTypeVar{"Parent", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"});
|
2022-04-14 22:57:15 +01:00
|
|
|
|
|
|
|
ClassTypeVar* parentClass = getMutable<ClassTypeVar>(parentType);
|
|
|
|
parentClass->props["method"] = {makeFunction(arena, parentType, {}, {})};
|
|
|
|
|
|
|
|
parentClass->props["virtual_method"] = {makeFunction(arena, parentType, {}, {})};
|
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
addGlobalBinding(frontend, "Parent", {parentType});
|
|
|
|
frontend.getGlobalScope()->exportedTypeBindings["Parent"] = TypeFun{{}, parentType};
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-04-21 22:04:22 +01:00
|
|
|
TypeId childType = arena.addType(ClassTypeVar{"Child", {}, parentType, std::nullopt, {}, nullptr, "Test"});
|
2022-04-14 22:57:15 +01:00
|
|
|
|
|
|
|
ClassTypeVar* childClass = getMutable<ClassTypeVar>(childType);
|
|
|
|
childClass->props["virtual_method"] = {makeFunction(arena, childType, {}, {})};
|
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
addGlobalBinding(frontend, "Child", {childType});
|
|
|
|
frontend.getGlobalScope()->exportedTypeBindings["Child"] = TypeFun{{}, childType};
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-04-21 22:04:22 +01:00
|
|
|
TypeId unrelatedType = arena.addType(ClassTypeVar{"Unrelated", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"});
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
addGlobalBinding(frontend, "Unrelated", {unrelatedType});
|
|
|
|
frontend.getGlobalScope()->exportedTypeBindings["Unrelated"] = TypeFun{{}, unrelatedType};
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
for (const auto& [name, ty] : frontend.getGlobalScope()->exportedTypeBindings)
|
2022-05-26 21:33:48 +01:00
|
|
|
persist(ty.type);
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
freeze(arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("isSubtype");
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "primitives")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a = 41
|
|
|
|
local b = 32
|
|
|
|
|
|
|
|
local c = "hello"
|
|
|
|
local d = "world"
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(d, c));
|
|
|
|
CHECK(!isSubtype(d, a));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "functions")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
function a(x: number): number return x end
|
|
|
|
function b(x: number): number return x end
|
|
|
|
|
|
|
|
function c(x: number?): number return x end
|
|
|
|
function d(x: number): number? return x end
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(c, a));
|
|
|
|
CHECK(!isSubtype(d, a));
|
|
|
|
CHECK(isSubtype(a, d));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "functions_and_any")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
function a(n: number) return "string" end
|
|
|
|
function b(q: any) return 5 :: any end
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
2022-11-04 17:02:37 +00:00
|
|
|
// any makes things work even when it makes no sense.
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-11-04 17:02:37 +00:00
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(a, b));
|
2022-04-14 22:57:15 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "variadic_functions_with_no_head")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: (...number) -> ()
|
|
|
|
local b: (...number?) -> ()
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(!isSubtype(a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "variadic_function_with_head")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: (...number) -> ()
|
|
|
|
local b: (number, number) -> ()
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(!isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(a, b));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "union")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: number | string
|
|
|
|
local b: number
|
|
|
|
local c: string
|
|
|
|
local d: number?
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(!isSubtype(a, b));
|
|
|
|
|
|
|
|
CHECK(isSubtype(c, a));
|
|
|
|
CHECK(!isSubtype(a, c));
|
|
|
|
|
|
|
|
CHECK(!isSubtype(d, a));
|
|
|
|
CHECK(!isSubtype(a, d));
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, d));
|
|
|
|
CHECK(!isSubtype(d, b));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "table_with_union_prop")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: {x: number}
|
|
|
|
local b: {x: number?}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(isSubtype(a, b));
|
|
|
|
CHECK(!isSubtype(b, a));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "table_with_any_prop")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: {x: number}
|
|
|
|
local b: {x: any}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(isSubtype(a, b));
|
2022-11-04 17:02:37 +00:00
|
|
|
CHECK(isSubtype(b, a));
|
2022-04-14 22:57:15 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "intersection")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
2022-10-13 23:59:53 +01:00
|
|
|
ScopedFastFlag sffs[]{
|
2022-10-07 00:55:58 +01:00
|
|
|
{"LuauSubtypeNormalizer", true},
|
|
|
|
{"LuauTypeNormalization2", true},
|
|
|
|
};
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
check(R"(
|
|
|
|
local a: number & string
|
|
|
|
local b: number
|
|
|
|
local c: string
|
|
|
|
local d: number & nil
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
|
|
|
|
CHECK(!isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(a, b));
|
|
|
|
|
|
|
|
CHECK(!isSubtype(c, a));
|
|
|
|
CHECK(isSubtype(a, c));
|
|
|
|
|
2022-10-07 00:55:58 +01:00
|
|
|
// These types are both equivalent to never
|
|
|
|
CHECK(isSubtype(d, a));
|
|
|
|
CHECK(isSubtype(a, d));
|
2022-04-14 22:57:15 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "union_and_intersection")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: number & string
|
|
|
|
local b: number | nil
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(!isSubtype(b, a));
|
|
|
|
CHECK(isSubtype(a, b));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "tables")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: {x: number}
|
|
|
|
local b: {x: any}
|
|
|
|
local c: {y: number}
|
|
|
|
local d: {x: number, y: number}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
|
|
|
|
CHECK(isSubtype(a, b));
|
2022-11-04 17:02:37 +00:00
|
|
|
CHECK(isSubtype(b, a));
|
2022-04-14 22:57:15 +01:00
|
|
|
|
|
|
|
CHECK(!isSubtype(c, a));
|
|
|
|
CHECK(!isSubtype(a, c));
|
|
|
|
|
|
|
|
CHECK(isSubtype(d, a));
|
|
|
|
CHECK(!isSubtype(a, d));
|
|
|
|
|
|
|
|
CHECK(isSubtype(d, b));
|
|
|
|
CHECK(!isSubtype(b, d));
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:04:22 +01:00
|
|
|
#if 0
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "table_indexers_are_invariant")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: {[string]: number}
|
|
|
|
local b: {[string]: any}
|
|
|
|
local c: {[string]: number}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
|
|
|
|
CHECK(!isSubtype(b, a));
|
|
|
|
CHECK(!isSubtype(a, b));
|
|
|
|
|
|
|
|
CHECK(isSubtype(c, a));
|
|
|
|
CHECK(isSubtype(a, c));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "mismatched_indexers")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local a: {x: number}
|
|
|
|
local b: {[string]: number}
|
|
|
|
local c: {}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(!isSubtype(a, b));
|
|
|
|
|
|
|
|
CHECK(!isSubtype(c, b));
|
|
|
|
CHECK(isSubtype(b, c));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "cyclic_table")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
type A = {method: (A) -> ()}
|
|
|
|
local a: A
|
|
|
|
|
|
|
|
type B = {method: (any) -> ()}
|
|
|
|
local b: B
|
|
|
|
|
|
|
|
type C = {method: (C) -> ()}
|
|
|
|
local c: C
|
|
|
|
|
|
|
|
type D = {method: (D) -> (), another: (D) -> ()}
|
|
|
|
local d: D
|
|
|
|
|
|
|
|
type E = {method: (A) -> (), another: (E) -> ()}
|
|
|
|
local e: E
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
TypeId c = requireType("c");
|
|
|
|
TypeId d = requireType("d");
|
|
|
|
TypeId e = requireType("e");
|
|
|
|
|
|
|
|
CHECK(isSubtype(b, a));
|
|
|
|
CHECK(!isSubtype(a, b));
|
|
|
|
|
|
|
|
CHECK(isSubtype(c, a));
|
|
|
|
CHECK(isSubtype(a, c));
|
|
|
|
|
|
|
|
CHECK(!isSubtype(d, a));
|
|
|
|
CHECK(!isSubtype(a, d));
|
|
|
|
|
|
|
|
CHECK(isSubtype(e, a));
|
|
|
|
CHECK(!isSubtype(a, e));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "classes")
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
2022-09-08 22:44:50 +01:00
|
|
|
createSomeClasses(frontend);
|
2022-04-14 22:57:15 +01:00
|
|
|
|
2022-08-18 22:04:33 +01:00
|
|
|
check(""); // Ensure that we have a main Module.
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
TypeId p = typeChecker.globalScope->lookupType("Parent")->type;
|
|
|
|
TypeId c = typeChecker.globalScope->lookupType("Child")->type;
|
|
|
|
TypeId u = typeChecker.globalScope->lookupType("Unrelated")->type;
|
|
|
|
|
|
|
|
CHECK(isSubtype(c, p));
|
|
|
|
CHECK(!isSubtype(p, c));
|
|
|
|
CHECK(!isSubtype(u, p));
|
|
|
|
CHECK(!isSubtype(p, u));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(IsSubtypeFixture, "metatable" * doctest::expected_failures{1})
|
2022-04-14 22:57:15 +01:00
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
local T = {}
|
|
|
|
T.__index = T
|
|
|
|
function T.new()
|
|
|
|
return setmetatable({}, T)
|
|
|
|
end
|
|
|
|
|
|
|
|
function T:method() end
|
|
|
|
|
|
|
|
local a: typeof(T.new)
|
|
|
|
local b: {method: (any) -> ()}
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId a = requireType("a");
|
|
|
|
TypeId b = requireType("b");
|
|
|
|
|
|
|
|
CHECK(isSubtype(a, b));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_SUITE_END();
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
struct NormalizeFixture : Fixture
|
|
|
|
{
|
2022-11-04 17:02:37 +00:00
|
|
|
ScopedFastFlag sff0{"LuauNegatedStringSingletons", true};
|
|
|
|
ScopedFastFlag sff1{"LuauNegatedFunctionTypes", true};
|
2022-10-27 23:22:49 +01:00
|
|
|
|
|
|
|
TypeArena arena;
|
|
|
|
InternalErrorReporter iceHandler;
|
|
|
|
UnifierSharedState unifierState{&iceHandler};
|
|
|
|
Normalizer normalizer{&arena, singletonTypes, NotNull{&unifierState}};
|
|
|
|
|
|
|
|
NormalizeFixture()
|
|
|
|
{
|
2022-11-04 17:02:37 +00:00
|
|
|
registerHiddenTypes(*this, arena);
|
2022-10-27 23:22:49 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 17:02:37 +00:00
|
|
|
const NormalizedType* toNormalizedType(const std::string& annotation)
|
2022-10-27 23:22:49 +01:00
|
|
|
{
|
|
|
|
CheckResult result = check("type _Res = " + annotation);
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
std::optional<TypeId> ty = lookupType("_Res");
|
|
|
|
REQUIRE(ty);
|
2022-11-04 17:02:37 +00:00
|
|
|
return normalizer.normalize(*ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeId normal(const std::string& annotation)
|
|
|
|
{
|
|
|
|
const NormalizedType* norm = toNormalizedType(annotation);
|
2022-10-27 23:22:49 +01:00
|
|
|
REQUIRE(norm);
|
|
|
|
return normalizer.typeFromNormal(*norm);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
TEST_SUITE_BEGIN("Normalize");
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "negate_string")
|
|
|
|
{
|
|
|
|
CHECK("number" == toString(normal(R"(
|
|
|
|
(number | string) & Not<string>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "negate_string_from_cofinite_string_intersection")
|
|
|
|
{
|
|
|
|
CHECK("number" == toString(normal(R"(
|
|
|
|
(number | (string & Not<"hello"> & Not<"world">)) & Not<string>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "no_op_negation_is_dropped")
|
|
|
|
{
|
|
|
|
CHECK("number" == toString(normal(R"(
|
|
|
|
number & Not<string>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "union_of_negation")
|
|
|
|
{
|
|
|
|
CHECK("string" == toString(normal(R"(
|
|
|
|
(string & Not<"hello">) | "hello"
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "intersect_truthy")
|
|
|
|
{
|
|
|
|
CHECK("number | string | true" == toString(normal(R"(
|
|
|
|
(string | number | boolean | nil) & Not<false | nil>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "intersect_truthy_expressed_as_intersection")
|
|
|
|
{
|
|
|
|
CHECK("number | string | true" == toString(normal(R"(
|
|
|
|
(string | number | boolean | nil) & Not<false> & Not<nil>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "union_of_union")
|
|
|
|
{
|
|
|
|
CHECK(R"("alpha" | "beta" | "gamma")" == toString(normal(R"(
|
|
|
|
("alpha" | "beta") | "gamma"
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "union_of_negations")
|
|
|
|
{
|
|
|
|
CHECK(R"(string & ~"world")" == toString(normal(R"(
|
|
|
|
(string & Not<"hello"> & Not<"world">) | (string & Not<"goodbye"> & Not<"world">)
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:02:37 +00:00
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "disjoint_negations_normalize_to_string")
|
|
|
|
{
|
|
|
|
CHECK(R"(string)" == toString(normal(R"(
|
|
|
|
(string & Not<"hello"> & Not<"world">) | (string & Not<"goodbye">)
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "negate_boolean")
|
|
|
|
{
|
|
|
|
CHECK("true" == toString(normal(R"(
|
|
|
|
boolean & Not<false>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "negate_boolean_2")
|
|
|
|
{
|
|
|
|
CHECK("never" == toString(normal(R"(
|
|
|
|
true & Not<true>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:02:37 +00:00
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "intersect_function_and_top_function")
|
|
|
|
{
|
|
|
|
CHECK("() -> ()" == toString(normal(R"(
|
|
|
|
fun & (() -> ())
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "intersect_function_and_top_function_reverse")
|
|
|
|
{
|
|
|
|
CHECK("() -> ()" == toString(normal(R"(
|
|
|
|
(() -> ()) & fun
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "union_function_and_top_function")
|
|
|
|
{
|
|
|
|
CHECK("function" == toString(normal(R"(
|
|
|
|
fun | (() -> ())
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "negated_function_is_anything_except_a_function")
|
|
|
|
{
|
|
|
|
CHECK("(boolean | number | string | thread)?" == toString(normal(R"(
|
|
|
|
Not<fun>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "specific_functions_cannot_be_negated")
|
|
|
|
{
|
|
|
|
CHECK(nullptr == toNormalizedType("Not<(boolean) -> boolean>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(NormalizeFixture, "bare_negated_boolean")
|
2022-10-27 23:22:49 +01:00
|
|
|
{
|
|
|
|
// TODO: We don't yet have a way to say number | string | thread | nil | Class | Table | Function
|
2022-11-04 17:02:37 +00:00
|
|
|
CHECK("(function | number | string | thread)?" == toString(normal(R"(
|
2022-10-27 23:22:49 +01:00
|
|
|
Not<boolean>
|
|
|
|
)")));
|
|
|
|
}
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "higher_order_function")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
function apply(f, x)
|
|
|
|
return f(x)
|
|
|
|
end
|
|
|
|
|
|
|
|
local a = apply(function(x: number) return x + x end, 5)
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId aType = requireType("a");
|
|
|
|
CHECK_MESSAGE(isNumber(follow(aType)), "Expected a number but got ", toString(aType));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "higher_order_function_with_annotation")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
function apply<a, b>(f: (a) -> b, x)
|
|
|
|
return f(x)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
CHECK_EQ("<a, b>((a) -> b, a) -> b", toString(requireType("apply")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "cyclic_table_normalizes_sensibly")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local Cyclic = {}
|
|
|
|
function Cyclic.get()
|
|
|
|
return Cyclic
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
TypeId ty = requireType("Cyclic");
|
|
|
|
CHECK_EQ("t1 where t1 = { get: () -> t1 }", toString(ty, {true}));
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:33:48 +01:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "skip_force_normal_on_external_types")
|
|
|
|
{
|
2022-09-08 22:44:50 +01:00
|
|
|
createSomeClasses(frontend);
|
2022-05-26 21:33:48 +01:00
|
|
|
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
export type t0 = { a: Child }
|
|
|
|
export type t1 = { a: typeof(string.byte) }
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "intersection_combine_on_bound_self")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
export type t0 = (((any)&({_:l0.t0,n0:t0,_G:any,}))&({_:any,}))&(((any)&({_:l0.t0,n0:t0,_G:any,}))&({_:any,}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-04-14 22:57:15 +01:00
|
|
|
TEST_SUITE_END();
|