2023-01-07 00:07:19 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/TypeReduction.h"
|
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct ReductionFixture : Fixture
|
|
|
|
{
|
2023-01-20 20:02:39 +08:00
|
|
|
TypeReductionOptions typeReductionOpts{/* allowTypeReductionsFromOtherArenas */ true};
|
|
|
|
ToStringOptions toStringOpts{true};
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
TypeArena arena;
|
|
|
|
InternalErrorReporter iceHandler;
|
|
|
|
UnifierSharedState unifierState{&iceHandler};
|
2023-01-20 20:02:39 +08:00
|
|
|
TypeReduction reduction{NotNull{&arena}, builtinTypes, NotNull{&iceHandler}, typeReductionOpts};
|
2023-01-07 00:07:19 +08:00
|
|
|
|
|
|
|
ReductionFixture()
|
|
|
|
{
|
|
|
|
registerHiddenTypes(&frontend);
|
|
|
|
createSomeClasses(&frontend);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeId reductionof(TypeId ty)
|
|
|
|
{
|
|
|
|
std::optional<TypeId> reducedTy = reduction.reduce(ty);
|
|
|
|
REQUIRE(reducedTy);
|
|
|
|
return *reducedTy;
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
std::optional<TypeId> tryReduce(const std::string& annotation)
|
|
|
|
{
|
|
|
|
check("type _Res = " + annotation);
|
|
|
|
return reduction.reduce(requireTypeAlias("_Res"));
|
|
|
|
}
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
TypeId reductionof(const std::string& annotation)
|
2023-01-07 00:07:19 +08:00
|
|
|
{
|
2023-01-20 20:02:39 +08:00
|
|
|
check("type _Res = " + annotation);
|
|
|
|
return reductionof(requireTypeAlias("_Res"));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
std::string toStringFull(TypeId ty)
|
2023-01-07 00:07:19 +08:00
|
|
|
{
|
2023-01-20 20:02:39 +08:00
|
|
|
return toString(ty, toStringOpts);
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("TypeReductionTests");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "cartesian_product_exceeded")
|
|
|
|
{
|
|
|
|
ScopedFastInt sfi{"LuauTypeReductionCartesianProductLimit", 5};
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
type T
|
|
|
|
= string
|
|
|
|
& (number | string | boolean)
|
|
|
|
& (number | string | boolean)
|
2023-01-07 00:07:19 +08:00
|
|
|
)");
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(!reduction.reduce(requireTypeAlias("T")));
|
|
|
|
// LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
// CHECK("Code is too complex to typecheck! Consider simplifying the code around this area" == toString(result.errors[0]));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "cartesian_product_exceeded_with_normal_limit")
|
|
|
|
{
|
2023-01-20 20:02:39 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
type T
|
|
|
|
= string -- 1 = 1
|
|
|
|
& (number | string | boolean) -- 1 * 3 = 3
|
|
|
|
& (number | string | boolean) -- 3 * 3 = 9
|
|
|
|
& (number | string | boolean) -- 9 * 3 = 27
|
|
|
|
& (number | string | boolean) -- 27 * 3 = 81
|
|
|
|
& (number | string | boolean) -- 81 * 3 = 243
|
|
|
|
& (number | string | boolean) -- 243 * 3 = 729
|
|
|
|
& (number | string | boolean) -- 729 * 3 = 2187
|
|
|
|
& (number | string | boolean) -- 2187 * 3 = 6561
|
|
|
|
& (number | string | boolean) -- 6561 * 3 = 19683
|
|
|
|
& (number | string | boolean) -- 19683 * 3 = 59049
|
|
|
|
& (number | string) -- 59049 * 2 = 118098
|
2023-01-07 00:07:19 +08:00
|
|
|
)");
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(!reduction.reduce(requireTypeAlias("T")));
|
|
|
|
// LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
// CHECK("Code is too complex to typecheck! Consider simplifying the code around this area" == toString(result.errors[0]));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "cartesian_product_is_zero")
|
|
|
|
{
|
|
|
|
ScopedFastInt sfi{"LuauTypeReductionCartesianProductLimit", 5};
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CheckResult result = check(R"(
|
|
|
|
type T
|
|
|
|
= string
|
|
|
|
& (number | string | boolean)
|
|
|
|
& (number | string | boolean)
|
|
|
|
& never
|
2023-01-07 00:07:19 +08:00
|
|
|
)");
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 04:36:28 +08:00
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "stress_test_recursion_limits")
|
|
|
|
{
|
|
|
|
TypeId ty = arena.addType(IntersectionType{{builtinTypes->numberType, builtinTypes->stringType}});
|
|
|
|
for (size_t i = 0; i < 20'000; ++i)
|
|
|
|
{
|
|
|
|
TableType table;
|
|
|
|
table.state = TableState::Sealed;
|
|
|
|
table.props["x"] = {ty};
|
|
|
|
ty = arena.addType(IntersectionType{{arena.addType(table), arena.addType(table)}});
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!reduction.reduce(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "caching")
|
|
|
|
{
|
|
|
|
SUBCASE("free_tables")
|
|
|
|
{
|
|
|
|
TypeId ty1 = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(ty1)->state = TableState::Free;
|
|
|
|
getMutable<TableType>(ty1)->props["x"] = {builtinTypes->stringType};
|
|
|
|
|
|
|
|
TypeId ty2 = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(ty2)->state = TableState::Sealed;
|
|
|
|
|
|
|
|
TypeId intersectionTy = arena.addType(IntersectionType{{ty1, ty2}});
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{- x: string -} & {| |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
|
|
|
|
getMutable<TableType>(ty1)->state = TableState::Sealed;
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| x: string |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("unsealed_tables")
|
|
|
|
{
|
|
|
|
TypeId ty1 = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(ty1)->state = TableState::Unsealed;
|
|
|
|
getMutable<TableType>(ty1)->props["x"] = {builtinTypes->stringType};
|
|
|
|
|
|
|
|
TypeId ty2 = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(ty2)->state = TableState::Sealed;
|
|
|
|
|
|
|
|
TypeId intersectionTy = arena.addType(IntersectionType{{ty1, ty2}});
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| x: string |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
|
|
|
|
getMutable<TableType>(ty1)->state = TableState::Sealed;
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| x: string |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("free_types")
|
|
|
|
{
|
|
|
|
TypeId ty1 = arena.freshType(nullptr);
|
|
|
|
TypeId ty2 = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(ty2)->state = TableState::Sealed;
|
|
|
|
|
|
|
|
TypeId intersectionTy = arena.addType(IntersectionType{{ty1, ty2}});
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("a & {| |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
|
|
|
|
*asMutable(ty1) = BoundType{ty2};
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| |}" == toStringFull(reductionof(intersectionTy)));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("we_can_see_that_the_cache_works_if_we_mutate_a_normally_not_mutated_type")
|
|
|
|
{
|
|
|
|
TypeId ty1 = arena.addType(BoundType{builtinTypes->stringType});
|
|
|
|
TypeId ty2 = builtinTypes->numberType;
|
|
|
|
|
|
|
|
TypeId intersectionTy = arena.addType(IntersectionType{{ty1, ty2}});
|
|
|
|
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(reductionof(intersectionTy))); // Bound<string> & number ~ never
|
2023-01-14 04:36:28 +08:00
|
|
|
|
|
|
|
*asMutable(ty1) = BoundType{ty2};
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(reductionof(intersectionTy))); // Bound<number> & number ~ number, but the cache is `never`.
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ptr_eq_irreducible_unions")
|
|
|
|
{
|
|
|
|
TypeId unionTy = arena.addType(UnionType{{builtinTypes->stringType, builtinTypes->numberType}});
|
|
|
|
TypeId reducedTy = reductionof(unionTy);
|
|
|
|
REQUIRE(unionTy == reducedTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ptr_eq_irreducible_intersections")
|
|
|
|
{
|
|
|
|
TypeId intersectionTy = arena.addType(IntersectionType{{builtinTypes->stringType, arena.addType(GenericType{"G"})}});
|
|
|
|
TypeId reducedTy = reductionof(intersectionTy);
|
|
|
|
REQUIRE(intersectionTy == reducedTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ptr_eq_free_table")
|
|
|
|
{
|
|
|
|
TypeId tableTy = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(tableTy)->state = TableState::Free;
|
|
|
|
|
|
|
|
TypeId reducedTy = reductionof(tableTy);
|
|
|
|
REQUIRE(tableTy == reducedTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("ptr_eq_unsealed_table")
|
|
|
|
{
|
|
|
|
TypeId tableTy = arena.addType(TableType{});
|
|
|
|
getMutable<TableType>(tableTy)->state = TableState::Unsealed;
|
|
|
|
|
|
|
|
TypeId reducedTy = reductionof(tableTy);
|
|
|
|
REQUIRE(tableTy == reducedTy);
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
} // caching
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "intersections_without_negations")
|
|
|
|
{
|
|
|
|
SUBCASE("string_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("never_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("never & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_never")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & never");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("unknown_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("unknown & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_unknown")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & unknown");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("any_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("any & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_any")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & any");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string | number) & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_string_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & (string | number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(string & "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_and_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean & true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("true" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_and_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(boolean & "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_and_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" & "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_and_b")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" & "b")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_and_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" & true)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_and_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(true & false)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_and_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & fun");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("() -> ()" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_and_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent & Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Child" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_and_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Child & Parent");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Child" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_and_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Child & Unrelated");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & {}");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & () -> ()");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_and_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & {}");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_and_class")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_and_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & () -> ()");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(() -> ()) & (() -> ())" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_and_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{} & {}");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_and_metatable")
|
|
|
|
{
|
|
|
|
// No setmetatable in ReductionFixture, so we mix and match.
|
|
|
|
BuiltinsFixture fixture;
|
|
|
|
fixture.check(R"(
|
|
|
|
type Ty = {} & typeof(setmetatable({}, {}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(fixture.requireTypeAlias("Ty"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{ @metatable { }, { } } & {| |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_and_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" & string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reducible_function_and_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("((string | string) -> (number | number)) & fun");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(string) -> number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_error")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & err");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("*error-type* & string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_table_p_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { p: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_table_p_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { p: string }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| p: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_x_table_p_string_and_table_x_table_p_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ x: { p: string } } & { x: { p: number } }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_and_table_q")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { q: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| p: string, q: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_tag_a_or_table_tag_b_and_table_b")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ tag: string, a: number } | { tag: number, b: string }) & { b: string }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| a: number, b: string, tag: string |} | {| b: string, tag: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_string_number_indexer_and_table_string_number_indexer")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ [string]: number } & { [string]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_string_number_indexer_and_empty_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ [string]: number } & {}");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("empty_table_table_string_number_indexer")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{} & { [string]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_number_indexer_and_number_number_indexer")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ [string]: number } & { [number]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{number} & {| [string]: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_indexer_number_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { [number]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [number]: number, p: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_indexer_string_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { [string]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number, p: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_table_p_string_plus_indexer_string_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { p: string, [string]: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number, p: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
2023-01-14 04:36:28 +08:00
|
|
|
|
2023-02-03 20:34:12 +08:00
|
|
|
SUBCASE("array_number_and_array_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{number} & {string}");
|
|
|
|
CHECK("{never}" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("array_string_and_array_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{string} & {string}");
|
|
|
|
CHECK("{string}" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("array_string_or_number_and_array_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{string | number} & {string}");
|
|
|
|
CHECK("{string}" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
2023-01-14 04:36:28 +08:00
|
|
|
SUBCASE("fresh_type_and_string")
|
|
|
|
{
|
|
|
|
TypeId freshTy = arena.freshType(nullptr);
|
|
|
|
TypeId ty = reductionof(arena.addType(IntersectionType{{freshTy, builtinTypes->stringType}}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("a & string" == toStringFull(ty));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_fresh_type")
|
|
|
|
{
|
|
|
|
TypeId freshTy = arena.freshType(nullptr);
|
|
|
|
TypeId ty = reductionof(arena.addType(IntersectionType{{builtinTypes->stringType, freshTy}}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("a & string" == toStringFull(ty));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("generic_and_string")
|
|
|
|
{
|
|
|
|
TypeId genericTy = arena.addType(GenericType{"G"});
|
|
|
|
TypeId ty = reductionof(arena.addType(IntersectionType{{genericTy, builtinTypes->stringType}}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("G & string" == toStringFull(ty));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_generic")
|
|
|
|
{
|
|
|
|
TypeId genericTy = arena.addType(GenericType{"G"});
|
|
|
|
TypeId ty = reductionof(arena.addType(IntersectionType{{builtinTypes->stringType, genericTy}}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("G & string" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_and_child_or_parent_and_anotherchild_or_parent_and_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent & (Child | AnotherChild | Unrelated)");
|
|
|
|
CHECK("AnotherChild | Child" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_and_child_or_parent_and_anotherchild_or_parent_and_unrelated_2")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(Parent & Child) | (Parent & AnotherChild) | (Parent & Unrelated)");
|
|
|
|
CHECK("AnotherChild | Child" == toString(ty));
|
2023-01-14 04:36:28 +08:00
|
|
|
}
|
2023-01-28 05:28:45 +08:00
|
|
|
|
|
|
|
SUBCASE("top_table_and_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("tbl & {}");
|
|
|
|
CHECK("{| |}" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("top_table_and_non_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("tbl & \"foo\"");
|
|
|
|
CHECK("never" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("top_table_and_metatable")
|
|
|
|
{
|
|
|
|
BuiltinsFixture fixture;
|
|
|
|
registerHiddenTypes(&fixture.frontend);
|
|
|
|
fixture.check(R"(
|
|
|
|
type Ty = tbl & typeof(setmetatable({}, {}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(fixture.requireTypeAlias("Ty"));
|
|
|
|
CHECK("{ @metatable { }, { } }" == toString(ty));
|
|
|
|
}
|
2023-01-07 00:07:19 +08:00
|
|
|
} // intersections_without_negations
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "intersections_with_negations")
|
|
|
|
{
|
|
|
|
SUBCASE("nil_and_not_nil")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("nil & Not<nil>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("nil_and_not_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("nil & Not<false>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("nil" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_nil_and_not_nil")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string?) & Not<nil>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_nil_and_not_false_or_nil")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string?) & Not<false | nil>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_nil_and_not_false_and_not_nil")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string?) & Not<false> & Not<nil>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_false_and_bool")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<false> & boolean");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("true" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_and_not_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & Not<fun>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_and_not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () & Not<string>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("() -> ()" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_and_string_or_nil")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> & (string | nil))");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"((string & ~"a")?)" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_and_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> & "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_and_b")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> & "b")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("b")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_and_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<string> & "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_bool_and_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<boolean> & true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_and_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string> & true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("true" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_and_not_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent & Not<Child>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent & ~Child" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_child_and_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Child> & Parent");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent & ~Child" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_and_not_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Child & Not<Parent>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_parent_and_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Parent> & Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_parent_and_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Parent> & Unrelated");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("unrelated_and_not_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Unrelated & Not<Parent>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_unrelated_and_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Unrelated> & Parent");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_and_not_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent & Not<Unrelated>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reducible_function_and_not_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("((string | string) -> (number | number)) & Not<fun>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_not_error")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string & Not<err>");
|
2023-02-03 20:34:12 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_table_p_not_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { p: Not<number> }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| p: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_p_string_and_table_p_not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ p: string } & { p: Not<string> }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("table_x_table_p_string_and_table_x_table_p_not_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ x: { p: string } } & { x: { p: Not<number> } }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| x: {| p: string |} |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
2023-01-28 05:28:45 +08:00
|
|
|
|
2023-02-03 20:34:12 +08:00
|
|
|
SUBCASE("table_or_nil_and_truthy")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ x: number | string }?) & Not<false?>");
|
|
|
|
CHECK("{| x: number | string |}" == toString(ty));
|
|
|
|
}
|
|
|
|
|
2023-01-28 05:28:45 +08:00
|
|
|
SUBCASE("not_top_table_and_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<tbl> & {}");
|
|
|
|
CHECK("never" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_top_table_and_metatable")
|
|
|
|
{
|
|
|
|
BuiltinsFixture fixture;
|
|
|
|
registerHiddenTypes(&fixture.frontend);
|
|
|
|
fixture.check(R"(
|
|
|
|
type Ty = Not<tbl> & typeof(setmetatable({}, {}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(fixture.requireTypeAlias("Ty"));
|
|
|
|
CHECK("never" == toString(ty));
|
|
|
|
}
|
2023-01-07 00:07:19 +08:00
|
|
|
} // intersections_with_negations
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "unions_without_negations")
|
|
|
|
{
|
|
|
|
SUBCASE("never_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("never | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_never")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | never");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("unknown_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("unknown | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_unknown")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | unknown");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("any_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("any | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("any" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_any")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | any");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("any" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_string_and_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (string & number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | number");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("number_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("number | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string | number) | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number_or_string_2")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (number | string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_string_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (string | number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_string_or_number_or_boolean")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (string | number | boolean)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean | number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_string_or_boolean_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (string | boolean | number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean | number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_boolean_or_string_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | (boolean | string | number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean | number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_string_or_number_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(boolean | string | number) | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean | number | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean | true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean | false");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_true_or_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean | true | false");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("boolean" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(string | "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_b")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | "b")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a" | "b")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_b_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | "b" | string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("unknown_or_any")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("unknown | any");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("any_or_unknown")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("any | unknown");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_or_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () | fun");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("function" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("fun | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("function | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_or_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Child | Parent");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Unrelated");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent | Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_child_or_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Child | Unrelated");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent | Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_unrelated_or_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Unrelated | Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent | Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_child_or_unrelated_or_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Child | Unrelated | Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Parent | Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string | true" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | () -> ()");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(() -> ()) | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_err")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | err");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("*error-type* | string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
2023-01-28 05:28:45 +08:00
|
|
|
|
|
|
|
SUBCASE("top_table_or_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("tbl | {}");
|
|
|
|
CHECK("table" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("top_table_or_metatable")
|
|
|
|
{
|
|
|
|
BuiltinsFixture fixture;
|
|
|
|
registerHiddenTypes(&fixture.frontend);
|
|
|
|
fixture.check(R"(
|
|
|
|
type Ty = tbl | typeof(setmetatable({}, {}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(fixture.requireTypeAlias("Ty"));
|
|
|
|
CHECK("table" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("top_table_or_non_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("tbl | number");
|
|
|
|
CHECK("number | table" == toString(ty));
|
|
|
|
}
|
2023-01-07 00:07:19 +08:00
|
|
|
} // unions_without_negations
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "unions_with_negations")
|
|
|
|
{
|
|
|
|
SUBCASE("string_or_not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | Not<string>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string> | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_number_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<number> | string");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_not_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | Not<number>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_hi_or_string_and_not_hi")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"hi"> | (string & Not<"hi">))");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"(~"hi")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_and_not_hi_or_not_hi")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"((string & Not<"hi">) | Not<"hi">)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"(~"hi")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_not_never")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | Not<never>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_not_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | Not<"a">)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"(~"a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_not_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | Not<"a">)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_not_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(string | Not<"a">)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<string> | "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a" | ~string)" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | Not<string>)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"("a" | ~string)" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_number_or_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<number> | "a")");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("a_or_not_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"("a" | Not<number>)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_not_b")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | Not<"b">)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_not_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean | Not<false>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("boolean_or_not_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("boolean | Not<true>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("false_or_not_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("false | Not<false>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("true_or_not_false")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("true | Not<false>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~false" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_boolean_or_true")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<boolean> | true");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~false" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_false_or_not_boolean")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<false> | Not<boolean>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~false" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("function_type_or_not_function")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> () | Not<fun>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(() -> ()) | ~function" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_parent_or_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Parent> | Child");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Child | ~Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_or_not_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Child | Not<Parent>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("Child | ~Parent" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_not_child")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Not<Child>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_child_or_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Child> | Parent");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("parent_or_not_unrelated")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Parent | Not<Unrelated>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~Unrelated" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_string_and_not_a")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<string> | (string & Not<"a">))");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"(~"a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string> | Not<string>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_not_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string> | Not<number>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_not_boolean")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | Not<boolean>)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_a_or_boolean")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(Not<"a"> | boolean)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"(~"a")" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_err")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("string | Not<err>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("string | ~*error-type*" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
2023-01-28 05:28:45 +08:00
|
|
|
|
|
|
|
SUBCASE("not_top_table_or_table")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<tbl> | {}");
|
|
|
|
CHECK("{| |} | ~table" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_top_table_or_metatable")
|
|
|
|
{
|
|
|
|
BuiltinsFixture fixture;
|
|
|
|
registerHiddenTypes(&fixture.frontend);
|
|
|
|
fixture.check(R"(
|
|
|
|
type Ty = Not<tbl> | typeof(setmetatable({}, {}))
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(fixture.requireTypeAlias("Ty"));
|
|
|
|
CHECK("{ @metatable { }, { } } | ~table" == toString(ty));
|
|
|
|
}
|
2023-01-07 00:07:19 +08:00
|
|
|
} // unions_with_negations
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "tables")
|
|
|
|
{
|
|
|
|
SUBCASE("reduce_props")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ x: string | string, y: number | number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| x: string, y: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_indexers")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ [string | string]: number | number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| [string]: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_instantiated_type_parameters")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
type Foo<T> = { x: T }
|
|
|
|
local foo: Foo<string | string> = { x = "hello" }
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireType("foo"));
|
|
|
|
CHECK("Foo<string>" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_instantiated_type_pack_parameters")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
type Foo<T...> = { x: () -> T... }
|
|
|
|
local foo: Foo<string | string, number | number> = { x = function() return "hi", 5 end }
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireType("foo"));
|
|
|
|
CHECK("Foo<string, number>" == toString(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_tables_within_tables")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{ x: { y: string & number } }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
2023-02-03 20:34:12 +08:00
|
|
|
|
|
|
|
SUBCASE("array_of_never")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("{never}");
|
|
|
|
CHECK("{never}" == toStringFull(ty));
|
|
|
|
}
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "metatables")
|
|
|
|
{
|
|
|
|
SUBCASE("reduce_table_part")
|
|
|
|
{
|
|
|
|
TableType table;
|
2023-01-20 20:02:39 +08:00
|
|
|
table.state = TableState::Sealed;
|
2023-01-07 00:07:19 +08:00
|
|
|
table.props["x"] = {arena.addType(UnionType{{builtinTypes->stringType, builtinTypes->stringType}})};
|
|
|
|
TypeId tableTy = arena.addType(std::move(table));
|
|
|
|
|
|
|
|
TypeId ty = reductionof(arena.addType(MetatableType{tableTy, arena.addType(TableType{})}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{ @metatable { }, {| x: string |} }" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_metatable_part")
|
|
|
|
{
|
|
|
|
TableType table;
|
2023-01-20 20:02:39 +08:00
|
|
|
table.state = TableState::Sealed;
|
2023-01-07 00:07:19 +08:00
|
|
|
table.props["x"] = {arena.addType(UnionType{{builtinTypes->stringType, builtinTypes->stringType}})};
|
|
|
|
TypeId tableTy = arena.addType(std::move(table));
|
|
|
|
|
|
|
|
TypeId ty = reductionof(arena.addType(MetatableType{arena.addType(TableType{}), tableTy}));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{ @metatable {| x: string |}, { } }" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "functions")
|
|
|
|
{
|
|
|
|
SUBCASE("reduce_parameters")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string | string) -> ()");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(string) -> ()" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_returns")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> (string | string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("() -> string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_parameters_and_returns")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string | string) -> (number | number)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("(string) -> number" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_tail")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> ...(string | string)");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("() -> (...string)" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_head_and_tail")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("() -> (string | string, number | number, ...(boolean | boolean))");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("() -> (string, number, ...boolean)" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("reduce_overloaded_functions")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("((number | number) -> ()) & ((string | string) -> ())");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("((number) -> ()) & ((string) -> ())" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
} // functions
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "negations")
|
|
|
|
{
|
|
|
|
SUBCASE("not_unknown")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<unknown>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_never")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<never>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_any")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<any>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("any" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_not_reduction")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<Not<never>>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("never" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_or_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string | number>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~number & ~string" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_string_and_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<string & number>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("unknown" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("not_error")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<err>");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("~*error-type*" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
} // negations
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "discriminable_unions")
|
|
|
|
{
|
|
|
|
SUBCASE("cat_or_dog_and_dog")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(({ tag: "cat", catfood: string } | { tag: "dog", dogfood: string }) & { tag: "dog" })");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"({| dogfood: string, tag: "dog" |})" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("cat_or_dog_and_not_dog")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof(R"(({ tag: "cat", catfood: string } | { tag: "dog", dogfood: string }) & { tag: Not<"dog"> })");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK(R"({| catfood: string, tag: "cat" |})" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number_and_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ tag: string, a: number } | { tag: number, b: string }) & { tag: string }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| a: number, tag: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("string_or_number_and_number")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ tag: string, a: number } | { tag: number, b: string }) & { tag: number }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| b: string, tag: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_or_unrelated_and_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ tag: Child, x: number } | { tag: Unrelated, y: string }) & { tag: Parent }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| tag: Child, x: number |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("child_or_unrelated_and_not_parent")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("({ tag: Child, x: number } | { tag: Unrelated, y: string }) & { tag: Not<Parent> }");
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("{| tag: Unrelated, y: string |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "cycles")
|
|
|
|
{
|
|
|
|
SUBCASE("recursively_defined_function")
|
|
|
|
{
|
|
|
|
check("type F = (f: F) -> ()");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("F"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = (t1) -> ()" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("recursively_defined_function_and_function")
|
|
|
|
{
|
|
|
|
check("type F = (f: F & fun) -> ()");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("F"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = (function & t1) -> ()" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("recursively_defined_table")
|
|
|
|
{
|
|
|
|
check("type T = { x: T }");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("T"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = {| x: t1 |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("recursively_defined_table_and_table")
|
|
|
|
{
|
|
|
|
check("type T = { x: T & {} }");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("T"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = {| x: t1 & {| |} |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("recursively_defined_table_and_table_2")
|
|
|
|
{
|
|
|
|
check("type T = { x: T } & { x: number }");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("T"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = {| x: number |} & {| x: t1 |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("recursively_defined_table_and_table_3")
|
|
|
|
{
|
|
|
|
check("type T = { x: T } & { x: T }");
|
|
|
|
|
|
|
|
TypeId ty = reductionof(requireTypeAlias("T"));
|
2023-01-20 20:02:39 +08:00
|
|
|
CHECK("t1 where t1 = {| x: t1 |} & {| x: t1 |}" == toStringFull(ty));
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "string_singletons")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("(string & Not<\"A\">)?");
|
|
|
|
CHECK("(string & ~\"A\")?" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(ReductionFixture, "string_singletons_2")
|
|
|
|
{
|
|
|
|
TypeId ty = reductionof("Not<\"A\"> & Not<\"B\"> & (string?)");
|
|
|
|
CHECK("(string & ~\"A\" & ~\"B\")?" == toStringFull(ty));
|
|
|
|
}
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
TEST_SUITE_END();
|