2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2021-11-05 10:07:18 +08:00
|
|
|
#include "Luau/Scope.h"
|
2023-01-04 01:33:19 +08:00
|
|
|
#include "Luau/Type.h"
|
2023-01-07 00:07:19 +08:00
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
#include "Luau/TypeReduction.h"
|
2023-01-04 01:33:19 +08:00
|
|
|
#include "Luau/VisitType.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
#include "ScopedFlags.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_SUITE_BEGIN("TypeTests");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "primitives_are_equal")
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
REQUIRE_EQ(builtinTypes->booleanType, builtinTypes->booleanType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "bound_type_is_equal_to_that_which_it_is_bound")
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
Type bound(BoundType(builtinTypes->booleanType));
|
|
|
|
REQUIRE_EQ(bound, *builtinTypes->booleanType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "equivalent_cyclic_tables_are_equal")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type cycleOne{TypeVariant(TableType())};
|
|
|
|
TableType* tableOne = getMutable<TableType>(&cycleOne);
|
2021-10-30 04:25:12 +08:00
|
|
|
tableOne->props["self"] = {&cycleOne};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type cycleTwo{TypeVariant(TableType())};
|
|
|
|
TableType* tableTwo = getMutable<TableType>(&cycleTwo);
|
2021-10-30 04:25:12 +08:00
|
|
|
tableTwo->props["self"] = {&cycleTwo};
|
|
|
|
|
|
|
|
CHECK_EQ(cycleOne, cycleTwo);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "different_cyclic_tables_are_not_equal")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type cycleOne{TypeVariant(TableType())};
|
|
|
|
TableType* tableOne = getMutable<TableType>(&cycleOne);
|
2021-10-30 04:25:12 +08:00
|
|
|
tableOne->props["self"] = {&cycleOne};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type cycleTwo{TypeVariant(TableType())};
|
|
|
|
TableType* tableTwo = getMutable<TableType>(&cycleTwo);
|
2021-10-30 04:25:12 +08:00
|
|
|
tableTwo->props["this"] = {&cycleTwo};
|
|
|
|
|
|
|
|
CHECK_NE(cycleOne, cycleTwo);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "return_type_of_function_is_not_parenthesized_if_just_one_value")
|
|
|
|
{
|
|
|
|
auto emptyArgumentPack = TypePackVar{TypePack{}};
|
2023-03-11 03:20:04 +08:00
|
|
|
auto returnPack = TypePackVar{TypePack{{builtinTypes->numberType}}};
|
|
|
|
auto returnsTwo = Type(FunctionType(frontend.globals.globalScope->level, &emptyArgumentPack, &returnPack));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::string res = toString(&returnsTwo);
|
|
|
|
CHECK_EQ("() -> number", res);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "return_type_of_function_is_parenthesized_if_not_just_one_value")
|
|
|
|
{
|
|
|
|
auto emptyArgumentPack = TypePackVar{TypePack{}};
|
2023-03-11 03:20:04 +08:00
|
|
|
auto returnPack = TypePackVar{TypePack{{builtinTypes->numberType, builtinTypes->numberType}}};
|
|
|
|
auto returnsTwo = Type(FunctionType(frontend.globals.globalScope->level, &emptyArgumentPack, &returnPack));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::string res = toString(&returnsTwo);
|
|
|
|
CHECK_EQ("() -> (number, number)", res);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "return_type_of_function_is_parenthesized_if_tail_is_free")
|
|
|
|
{
|
|
|
|
auto emptyArgumentPack = TypePackVar{TypePack{}};
|
2023-04-08 03:56:27 +08:00
|
|
|
auto free = FreeTypePack(TypeLevel());
|
2021-10-30 04:25:12 +08:00
|
|
|
auto freePack = TypePackVar{TypePackVariant{free}};
|
2023-03-11 03:20:04 +08:00
|
|
|
auto returnPack = TypePackVar{TypePack{{builtinTypes->numberType}, &freePack}};
|
|
|
|
auto returnsTwo = Type(FunctionType(frontend.globals.globalScope->level, &emptyArgumentPack, &returnPack));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::string res = toString(&returnsTwo);
|
|
|
|
CHECK_EQ(res, "() -> (number, a...)");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "subset_check")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
UnionType super, sub, notSub;
|
2023-03-11 03:20:04 +08:00
|
|
|
super.options = {builtinTypes->numberType, builtinTypes->stringType, builtinTypes->booleanType};
|
|
|
|
sub.options = {builtinTypes->numberType, builtinTypes->stringType};
|
|
|
|
notSub.options = {builtinTypes->numberType, builtinTypes->nilType};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK(isSubset(super, sub));
|
|
|
|
CHECK(!isSubset(super, notSub));
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterate_over_UnionType")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
UnionType utv;
|
2023-03-11 03:20:04 +08:00
|
|
|
utv.options = {builtinTypes->numberType, builtinTypes->stringType, builtinTypes->anyType};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : &utv)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
CHECK(result == utv.options);
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterating_over_nested_UnionTypes")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type subunion{UnionType{}};
|
|
|
|
UnionType* innerUtv = getMutable<UnionType>(&subunion);
|
2023-03-11 03:20:04 +08:00
|
|
|
innerUtv->options = {builtinTypes->numberType, builtinTypes->stringType};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
UnionType utv;
|
2023-03-11 03:20:04 +08:00
|
|
|
utv.options = {builtinTypes->anyType, &subunion};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : &utv)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
REQUIRE_EQ(result.size(), 3);
|
2023-03-11 03:20:04 +08:00
|
|
|
CHECK_EQ(result[0], builtinTypes->anyType);
|
|
|
|
CHECK_EQ(result[2], builtinTypes->stringType);
|
|
|
|
CHECK_EQ(result[1], builtinTypes->numberType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterator_detects_cyclic_UnionTypes_and_skips_over_them")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type atv{UnionType{}};
|
|
|
|
UnionType* utv1 = getMutable<UnionType>(&atv);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type btv{UnionType{}};
|
|
|
|
UnionType* utv2 = getMutable<UnionType>(&btv);
|
2023-03-11 03:20:04 +08:00
|
|
|
utv2->options.push_back(builtinTypes->numberType);
|
|
|
|
utv2->options.push_back(builtinTypes->stringType);
|
2021-10-30 04:25:12 +08:00
|
|
|
utv2->options.push_back(&atv);
|
|
|
|
|
|
|
|
utv1->options.push_back(&btv);
|
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : utv2)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
REQUIRE_EQ(result.size(), 2);
|
2023-03-11 03:20:04 +08:00
|
|
|
CHECK_EQ(result[0], builtinTypes->numberType);
|
|
|
|
CHECK_EQ(result[1], builtinTypes->stringType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterator_descends_on_nested_in_first_operator*")
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
Type tv1{UnionType{{builtinTypes->stringType, builtinTypes->numberType}}};
|
|
|
|
Type tv2{UnionType{{&tv1, builtinTypes->booleanType}}};
|
2023-01-04 01:33:19 +08:00
|
|
|
auto utv = get<UnionType>(&tv2);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : utv)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
REQUIRE_EQ(result.size(), 3);
|
2023-03-11 03:20:04 +08:00
|
|
|
CHECK_EQ(result[0], builtinTypes->stringType);
|
|
|
|
CHECK_EQ(result[1], builtinTypes->numberType);
|
|
|
|
CHECK_EQ(result[2], builtinTypes->booleanType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_vector_iter_ctor")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
Type tv1{UnionType{{builtinTypes->stringType, builtinTypes->numberType}}};
|
|
|
|
Type tv2{UnionType{{&tv1, builtinTypes->booleanType}}};
|
2023-01-04 01:33:19 +08:00
|
|
|
auto utv = get<UnionType>(&tv2);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> actual(begin(utv), end(utv));
|
2023-03-11 03:20:04 +08:00
|
|
|
std::vector<TypeId> expected{builtinTypes->stringType, builtinTypes->numberType, builtinTypes->booleanType};
|
2021-10-30 04:25:12 +08:00
|
|
|
CHECK_EQ(actual, expected);
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_empty_union")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type tv{UnionType{}};
|
|
|
|
auto utv = get<UnionType>(&tv);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> actual(begin(utv), end(utv));
|
|
|
|
CHECK(actual.empty());
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_only_cyclic_union")
|
2022-08-12 04:42:54 +08:00
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type tv{UnionType{}};
|
|
|
|
auto utv = getMutable<UnionType>(&tv);
|
2022-08-12 04:42:54 +08:00
|
|
|
utv->options.push_back(&tv);
|
|
|
|
utv->options.push_back(&tv);
|
|
|
|
|
|
|
|
std::vector<TypeId> actual(begin(utv), end(utv));
|
|
|
|
CHECK(actual.empty());
|
|
|
|
}
|
|
|
|
|
2022-08-19 05:04:33 +08:00
|
|
|
|
|
|
|
/* FIXME: This test is pretty weird. It would be much nicer if we could
|
|
|
|
* perform this operation without a TypeChecker so that we don't have to jam
|
|
|
|
* all this state into it to make stuff work.
|
|
|
|
*/
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "substitution_skip_failure")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv11{FreeType{TypeLevel{}}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TypePackVar tp24{TypePack{{&ftv11}}};
|
|
|
|
TypePackVar tp17{TypePack{}};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv23{FunctionType{&tp24, &tp17}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvConnection2{TableType{}};
|
|
|
|
TableType* ttvConnection2_ = getMutable<TableType>(&ttvConnection2);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvConnection2_->instantiatedTypeParams.push_back(&ftv11);
|
|
|
|
ttvConnection2_->props["f"] = {&ftv23};
|
|
|
|
|
|
|
|
TypePackVar tp21{TypePack{{&ftv11}}};
|
|
|
|
TypePackVar tp20{TypePack{}};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv19{FunctionType{&tp21, &tp20}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvSignal{TableType{}};
|
|
|
|
TableType* ttvSignal_ = getMutable<TableType>(&ttvSignal);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvSignal_->instantiatedTypeParams.push_back(&ftv11);
|
|
|
|
ttvSignal_->props["f"] = {&ftv19};
|
|
|
|
|
|
|
|
// Back edge
|
|
|
|
ttvConnection2_->props["signal"] = {&ttvSignal};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type gtvK2{GenericType{}};
|
|
|
|
Type gtvV2{GenericType{}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvTweenResult2{TableType{}};
|
|
|
|
TableType* ttvTweenResult2_ = getMutable<TableType>(&ttvTweenResult2);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvTweenResult2_->instantiatedTypeParams.push_back(>vK2);
|
|
|
|
ttvTweenResult2_->instantiatedTypeParams.push_back(>vV2);
|
|
|
|
|
|
|
|
TypePackVar tp13{TypePack{{&ttvTweenResult2}}};
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv12{FunctionType{&tp13, &tp17}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvConnection{TableType{}};
|
|
|
|
TableType* ttvConnection_ = getMutable<TableType>(&ttvConnection);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvConnection_->instantiatedTypeParams.push_back(&ttvTweenResult2);
|
|
|
|
ttvConnection_->props["f"] = {&ftv12};
|
|
|
|
ttvConnection_->props["signal"] = {&ttvSignal};
|
|
|
|
|
|
|
|
TypePackVar tp9{TypePack{}};
|
|
|
|
TypePackVar tp10{TypePack{{&ttvConnection}}};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv8{FunctionType{&tp9, &tp10}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvTween{TableType{}};
|
|
|
|
TableType* ttvTween_ = getMutable<TableType>(&ttvTween);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvTween_->instantiatedTypeParams.push_back(>vK2);
|
|
|
|
ttvTween_->instantiatedTypeParams.push_back(>vV2);
|
|
|
|
ttvTween_->props["f"] = {&ftv8};
|
|
|
|
|
|
|
|
TypePackVar tp4{TypePack{}};
|
|
|
|
TypePackVar tp5{TypePack{{&ttvTween}}};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv3{FunctionType{&tp4, &tp5}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// Back edge
|
|
|
|
ttvTweenResult2_->props["f"] = {&ftv3};
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type gtvK{GenericType{}};
|
|
|
|
Type gtvV{GenericType{}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttvTweenResult{TableType{}};
|
|
|
|
TableType* ttvTweenResult_ = getMutable<TableType>(&ttvTweenResult);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttvTweenResult_->instantiatedTypeParams.push_back(>vK);
|
|
|
|
ttvTweenResult_->instantiatedTypeParams.push_back(>vV);
|
|
|
|
ttvTweenResult_->props["f"] = {&ftv3};
|
|
|
|
|
|
|
|
TypeId root = &ttvTweenResult;
|
|
|
|
|
2023-04-14 20:05:27 +08:00
|
|
|
ModulePtr currentModule = std::make_shared<Module>();
|
|
|
|
Anyification anyification(¤tModule->internalTypes, frontend.globals.globalScope, builtinTypes, &frontend.iceHandler, builtinTypes->anyType,
|
|
|
|
builtinTypes->anyTypePack);
|
|
|
|
std::optional<TypeId> any = anyification.substitute(root);
|
|
|
|
|
|
|
|
REQUIRE(!anyification.normalizationTooComplex);
|
|
|
|
REQUIRE(any.has_value());
|
|
|
|
CHECK_EQ("{| f: t1 |} where t1 = () -> {| f: () -> {| f: ({| f: t1 |}) -> (), signal: {| f: (any) -> () |} |} |}", toString(*any));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 10:42:00 +08:00
|
|
|
TEST_CASE("tagging_tables")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ttv{TableType{}};
|
2021-11-05 10:42:00 +08:00
|
|
|
CHECK(!Luau::hasTag(&ttv, "foo"));
|
|
|
|
Luau::attachTag(&ttv, "foo");
|
|
|
|
CHECK(Luau::hasTag(&ttv, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_classes")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type base{ClassType{"Base", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"}};
|
2021-11-05 10:42:00 +08:00
|
|
|
CHECK(!Luau::hasTag(&base, "foo"));
|
|
|
|
Luau::attachTag(&base, "foo");
|
|
|
|
CHECK(Luau::hasTag(&base, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_subclasses")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type base{ClassType{"Base", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"}};
|
|
|
|
Type derived{ClassType{"Derived", {}, &base, std::nullopt, {}, nullptr, "Test"}};
|
2021-11-05 10:42:00 +08:00
|
|
|
|
|
|
|
CHECK(!Luau::hasTag(&base, "foo"));
|
|
|
|
CHECK(!Luau::hasTag(&derived, "foo"));
|
|
|
|
|
|
|
|
Luau::attachTag(&base, "foo");
|
|
|
|
CHECK(Luau::hasTag(&base, "foo"));
|
|
|
|
CHECK(Luau::hasTag(&derived, "foo"));
|
|
|
|
|
|
|
|
Luau::attachTag(&derived, "bar");
|
|
|
|
CHECK(!Luau::hasTag(&base, "bar"));
|
|
|
|
CHECK(Luau::hasTag(&derived, "bar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_functions")
|
|
|
|
{
|
|
|
|
TypePackVar empty{TypePack{}};
|
2023-01-04 01:33:19 +08:00
|
|
|
Type ftv{FunctionType{&empty, &empty}};
|
2021-11-05 10:42:00 +08:00
|
|
|
CHECK(!Luau::hasTag(&ftv, "foo"));
|
|
|
|
Luau::attachTag(&ftv, "foo");
|
|
|
|
CHECK(Luau::hasTag(&ftv, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_props")
|
|
|
|
{
|
|
|
|
Property prop{};
|
|
|
|
CHECK(!Luau::hasTag(prop, "foo"));
|
|
|
|
Luau::attachTag(prop, "foo");
|
|
|
|
CHECK(Luau::hasTag(prop, "foo"));
|
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
struct VisitCountTracker final : TypeOnceVisitor
|
2021-12-03 07:20:08 +08:00
|
|
|
{
|
|
|
|
std::unordered_map<TypeId, unsigned> tyVisits;
|
|
|
|
std::unordered_map<TypePackId, unsigned> tpVisits;
|
|
|
|
|
2022-05-06 07:52:48 +08:00
|
|
|
void cycle(TypeId) override {}
|
|
|
|
void cycle(TypePackId) override {}
|
2021-12-03 07:20:08 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypeId ty, const T& t)
|
|
|
|
{
|
2022-05-06 07:52:48 +08:00
|
|
|
return visit(ty);
|
2021-12-03 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypePackId tp, const T&)
|
2022-05-06 07:52:48 +08:00
|
|
|
{
|
|
|
|
return visit(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(TypeId ty) override
|
|
|
|
{
|
|
|
|
tyVisits[ty]++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(TypePackId tp) override
|
2021-12-03 07:20:08 +08:00
|
|
|
{
|
|
|
|
tpVisits[tp]++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "visit_once")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
type T = { a: number, b: () -> () }
|
|
|
|
local b: (T, T, T) -> T
|
|
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
TypeId bType = requireType("b");
|
|
|
|
|
|
|
|
VisitCountTracker tester;
|
2022-05-27 04:33:48 +08:00
|
|
|
tester.traverse(bType);
|
2021-12-03 07:20:08 +08:00
|
|
|
|
|
|
|
for (auto [_, count] : tester.tyVisits)
|
|
|
|
CHECK_EQ(count, 1);
|
|
|
|
|
|
|
|
for (auto [_, count] : tester.tpVisits)
|
|
|
|
CHECK_EQ(count, 1);
|
|
|
|
}
|
|
|
|
|
2022-01-28 05:29:34 +08:00
|
|
|
TEST_CASE("isString_on_string_singletons")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type helloString{SingletonType{StringSingleton{"hello"}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
CHECK(isString(&helloString));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isString_on_unions_of_various_string_singletons")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type helloString{SingletonType{StringSingleton{"hello"}}};
|
|
|
|
Type byeString{SingletonType{StringSingleton{"bye"}}};
|
|
|
|
Type union_{UnionType{{&helloString, &byeString}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
|
|
|
|
CHECK(isString(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("proof_that_isString_uses_all_of")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type helloString{SingletonType{StringSingleton{"hello"}}};
|
|
|
|
Type byeString{SingletonType{StringSingleton{"bye"}}};
|
|
|
|
Type booleanType{PrimitiveType{PrimitiveType::Boolean}};
|
|
|
|
Type union_{UnionType{{&helloString, &byeString, &booleanType}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
|
|
|
|
CHECK(!isString(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isBoolean_on_boolean_singletons")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type trueBool{SingletonType{BooleanSingleton{true}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
CHECK(isBoolean(&trueBool));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isBoolean_on_unions_of_true_or_false_singletons")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type trueBool{SingletonType{BooleanSingleton{true}}};
|
|
|
|
Type falseBool{SingletonType{BooleanSingleton{false}}};
|
|
|
|
Type union_{UnionType{{&trueBool, &falseBool}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
|
|
|
|
CHECK(isBoolean(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("proof_that_isBoolean_uses_all_of")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type trueBool{SingletonType{BooleanSingleton{true}}};
|
|
|
|
Type falseBool{SingletonType{BooleanSingleton{false}}};
|
|
|
|
Type stringType{PrimitiveType{PrimitiveType::String}};
|
|
|
|
Type union_{UnionType{{&trueBool, &falseBool, &stringType}}};
|
2022-01-28 05:29:34 +08:00
|
|
|
|
|
|
|
CHECK(!isBoolean(&union_));
|
|
|
|
}
|
|
|
|
|
2022-06-17 08:52:23 +08:00
|
|
|
TEST_CASE("content_reassignment")
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
Type myAny{AnyType{}, /*presistent*/ true};
|
2022-06-17 08:52:23 +08:00
|
|
|
myAny.documentationSymbol = "@global/any";
|
|
|
|
|
|
|
|
TypeArena arena;
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
TypeId futureAny = arena.addType(FreeType{TypeLevel{}});
|
2022-06-17 08:52:23 +08:00
|
|
|
asMutable(futureAny)->reassign(myAny);
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
CHECK(get<AnyType>(futureAny) != nullptr);
|
2022-06-17 08:52:23 +08:00
|
|
|
CHECK(!futureAny->persistent);
|
|
|
|
CHECK(futureAny->documentationSymbol == "@global/any");
|
|
|
|
CHECK(futureAny->owningArena == &arena);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_END();
|