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:34:35 +08:00
|
|
|
#include "Luau/Scope.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/TypeInfer.h"
|
2023-01-05 04:53:17 +08:00
|
|
|
#include "Luau/Type.h"
|
|
|
|
#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-05 04:53:17 +08:00
|
|
|
TEST_SUITE_BEGIN("TypeTests");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "primitives_are_equal")
|
|
|
|
{
|
|
|
|
REQUIRE_EQ(typeChecker.booleanType, typeChecker.booleanType);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "bound_type_is_equal_to_that_which_it_is_bound")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type bound(BoundType(typeChecker.booleanType));
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE_EQ(bound, *typeChecker.booleanType);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "equivalent_cyclic_tables_are_equal")
|
|
|
|
{
|
2023-01-05 04:53:17 +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-05 04:53:17 +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-05 04:53:17 +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-05 04:53:17 +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{}};
|
|
|
|
auto returnPack = TypePackVar{TypePack{{typeChecker.numberType}}};
|
2023-01-05 04:53:17 +08:00
|
|
|
auto returnsTwo = Type(FunctionType(typeChecker.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{}};
|
|
|
|
auto returnPack = TypePackVar{TypePack{{typeChecker.numberType, typeChecker.numberType}}};
|
2023-01-05 04:53:17 +08:00
|
|
|
auto returnsTwo = Type(FunctionType(typeChecker.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{}};
|
|
|
|
auto free = Unifiable::Free(TypeLevel());
|
|
|
|
auto freePack = TypePackVar{TypePackVariant{free}};
|
|
|
|
auto returnPack = TypePackVar{TypePack{{typeChecker.numberType}, &freePack}};
|
2023-01-05 04:53:17 +08:00
|
|
|
auto returnsTwo = Type(FunctionType(typeChecker.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-05 04:53:17 +08:00
|
|
|
UnionType super, sub, notSub;
|
2021-10-30 04:25:12 +08:00
|
|
|
super.options = {typeChecker.numberType, typeChecker.stringType, typeChecker.booleanType};
|
|
|
|
sub.options = {typeChecker.numberType, typeChecker.stringType};
|
|
|
|
notSub.options = {typeChecker.numberType, typeChecker.nilType};
|
|
|
|
|
|
|
|
CHECK(isSubset(super, sub));
|
|
|
|
CHECK(!isSubset(super, notSub));
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterate_over_UnionType")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
UnionType utv;
|
2021-10-30 04:25:12 +08:00
|
|
|
utv.options = {typeChecker.numberType, typeChecker.stringType, typeChecker.anyType};
|
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : &utv)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
CHECK(result == utv.options);
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterating_over_nested_UnionTypes")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type subunion{UnionType{}};
|
|
|
|
UnionType* innerUtv = getMutable<UnionType>(&subunion);
|
2021-10-30 04:25:12 +08:00
|
|
|
innerUtv->options = {typeChecker.numberType, typeChecker.stringType};
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
UnionType utv;
|
2021-10-30 04:25:12 +08:00
|
|
|
utv.options = {typeChecker.anyType, &subunion};
|
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId ty : &utv)
|
|
|
|
result.push_back(ty);
|
|
|
|
|
|
|
|
REQUIRE_EQ(result.size(), 3);
|
|
|
|
CHECK_EQ(result[0], typeChecker.anyType);
|
|
|
|
CHECK_EQ(result[2], typeChecker.stringType);
|
|
|
|
CHECK_EQ(result[1], typeChecker.numberType);
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterator_detects_cyclic_UnionTypes_and_skips_over_them")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type atv{UnionType{}};
|
|
|
|
UnionType* utv1 = getMutable<UnionType>(&atv);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
Type btv{UnionType{}};
|
|
|
|
UnionType* utv2 = getMutable<UnionType>(&btv);
|
2021-10-30 04:25:12 +08:00
|
|
|
utv2->options.push_back(typeChecker.numberType);
|
|
|
|
utv2->options.push_back(typeChecker.stringType);
|
|
|
|
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);
|
|
|
|
CHECK_EQ(result[0], typeChecker.numberType);
|
|
|
|
CHECK_EQ(result[1], typeChecker.stringType);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "iterator_descends_on_nested_in_first_operator*")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type tv1{UnionType{{typeChecker.stringType, typeChecker.numberType}}};
|
|
|
|
Type tv2{UnionType{{&tv1, typeChecker.booleanType}}};
|
|
|
|
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);
|
|
|
|
CHECK_EQ(result[0], typeChecker.stringType);
|
|
|
|
CHECK_EQ(result[1], typeChecker.numberType);
|
|
|
|
CHECK_EQ(result[2], typeChecker.booleanType);
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_vector_iter_ctor")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type tv1{UnionType{{typeChecker.stringType, typeChecker.numberType}}};
|
|
|
|
Type tv2{UnionType{{&tv1, typeChecker.booleanType}}};
|
|
|
|
auto utv = get<UnionType>(&tv2);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
std::vector<TypeId> actual(begin(utv), end(utv));
|
|
|
|
std::vector<TypeId> expected{typeChecker.stringType, typeChecker.numberType, typeChecker.booleanType};
|
|
|
|
CHECK_EQ(actual, expected);
|
|
|
|
}
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_empty_union")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "UnionTypeIterator_with_only_cyclic_union")
|
2022-08-12 05:01:33 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type tv{UnionType{}};
|
|
|
|
auto utv = getMutable<UnionType>(&tv);
|
2022-08-12 05:01:33 +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:32:08 +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-05 04:53:17 +08:00
|
|
|
Type ftv11{FreeType{TypeLevel{}}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TypePackVar tp24{TypePack{{&ftv11}}};
|
|
|
|
TypePackVar tp17{TypePack{}};
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
Type ftv23{FunctionType{&tp24, &tp17}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
Type ftv19{FunctionType{&tp21, &tp20}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
Type gtvK2{GenericType{}};
|
|
|
|
Type gtvV2{GenericType{}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
Type ftv12{FunctionType{&tp13, &tp17}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
Type ftv8{FunctionType{&tp9, &tp10}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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-05 04:53:17 +08:00
|
|
|
Type ftv3{FunctionType{&tp4, &tp5}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// Back edge
|
|
|
|
ttvTweenResult2_->props["f"] = {&ftv3};
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
Type gtvK{GenericType{}};
|
|
|
|
Type gtvV{GenericType{}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-05 04:53:17 +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;
|
|
|
|
|
|
|
|
typeChecker.currentModule = std::make_shared<Module>();
|
2023-01-05 04:53:17 +08:00
|
|
|
typeChecker.currentModule->scopes.emplace_back(Location{}, std::make_shared<Scope>(builtinTypes->anyTypePack));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TypeId result = typeChecker.anyify(typeChecker.globalScope, root, Location{});
|
|
|
|
|
2022-01-07 09:46:53 +08:00
|
|
|
CHECK_EQ("{| f: t1 |} where t1 = () -> {| f: () -> {| f: ({| f: t1 |}) -> (), signal: {| f: (any) -> () |} |} |}", toString(result));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
TEST_CASE("tagging_tables")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type ttv{TableType{}};
|
2021-11-05 23:47:21 +08:00
|
|
|
CHECK(!Luau::hasTag(&ttv, "foo"));
|
|
|
|
Luau::attachTag(&ttv, "foo");
|
|
|
|
CHECK(Luau::hasTag(&ttv, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_classes")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type base{ClassType{"Base", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"}};
|
2021-11-05 23:47:21 +08:00
|
|
|
CHECK(!Luau::hasTag(&base, "foo"));
|
|
|
|
Luau::attachTag(&base, "foo");
|
|
|
|
CHECK(Luau::hasTag(&base, "foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("tagging_subclasses")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type base{ClassType{"Base", {}, std::nullopt, std::nullopt, {}, nullptr, "Test"}};
|
|
|
|
Type derived{ClassType{"Derived", {}, &base, std::nullopt, {}, nullptr, "Test"}};
|
2021-11-05 23:47:21 +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-05 04:53:17 +08:00
|
|
|
Type ftv{FunctionType{&empty, &empty}};
|
2021-11-05 23:47:21 +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-05 04:53:17 +08:00
|
|
|
struct VisitCountTracker final : TypeOnceVisitor
|
2021-12-03 14:41:04 +08:00
|
|
|
{
|
|
|
|
std::unordered_map<TypeId, unsigned> tyVisits;
|
|
|
|
std::unordered_map<TypePackId, unsigned> tpVisits;
|
|
|
|
|
2022-05-06 08:03:43 +08:00
|
|
|
void cycle(TypeId) override {}
|
|
|
|
void cycle(TypePackId) override {}
|
2021-12-03 14:41:04 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypeId ty, const T& t)
|
|
|
|
{
|
2022-05-06 08:03:43 +08:00
|
|
|
return visit(ty);
|
2021-12-03 14:41:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypePackId tp, const T&)
|
2022-05-06 08:03:43 +08:00
|
|
|
{
|
|
|
|
return visit(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(TypeId ty) override
|
|
|
|
{
|
|
|
|
tyVisits[ty]++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(TypePackId tp) override
|
2021-12-03 14:41:04 +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 06:08:16 +08:00
|
|
|
tester.traverse(bType);
|
2021-12-03 14:41:04 +08:00
|
|
|
|
|
|
|
for (auto [_, count] : tester.tyVisits)
|
|
|
|
CHECK_EQ(count, 1);
|
|
|
|
|
|
|
|
for (auto [_, count] : tester.tpVisits)
|
|
|
|
CHECK_EQ(count, 1);
|
|
|
|
}
|
|
|
|
|
2022-01-28 07:46:05 +08:00
|
|
|
TEST_CASE("isString_on_string_singletons")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type helloString{SingletonType{StringSingleton{"hello"}}};
|
2022-01-28 07:46:05 +08:00
|
|
|
CHECK(isString(&helloString));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isString_on_unions_of_various_string_singletons")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type helloString{SingletonType{StringSingleton{"hello"}}};
|
|
|
|
Type byeString{SingletonType{StringSingleton{"bye"}}};
|
|
|
|
Type union_{UnionType{{&helloString, &byeString}}};
|
2022-01-28 07:46:05 +08:00
|
|
|
|
|
|
|
CHECK(isString(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("proof_that_isString_uses_all_of")
|
|
|
|
{
|
2023-01-05 04:53:17 +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 07:46:05 +08:00
|
|
|
|
|
|
|
CHECK(!isString(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isBoolean_on_boolean_singletons")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type trueBool{SingletonType{BooleanSingleton{true}}};
|
2022-01-28 07:46:05 +08:00
|
|
|
CHECK(isBoolean(&trueBool));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("isBoolean_on_unions_of_true_or_false_singletons")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type trueBool{SingletonType{BooleanSingleton{true}}};
|
|
|
|
Type falseBool{SingletonType{BooleanSingleton{false}}};
|
|
|
|
Type union_{UnionType{{&trueBool, &falseBool}}};
|
2022-01-28 07:46:05 +08:00
|
|
|
|
|
|
|
CHECK(isBoolean(&union_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("proof_that_isBoolean_uses_all_of")
|
|
|
|
{
|
2023-01-05 04:53:17 +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 07:46:05 +08:00
|
|
|
|
|
|
|
CHECK(!isBoolean(&union_));
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:58:21 +08:00
|
|
|
TEST_CASE("content_reassignment")
|
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type myAny{AnyType{}, /*presistent*/ true};
|
2022-06-11 00:58:21 +08:00
|
|
|
myAny.documentationSymbol = "@global/any";
|
|
|
|
|
|
|
|
TypeArena arena;
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TypeId futureAny = arena.addType(FreeType{TypeLevel{}});
|
2022-06-11 00:58:21 +08:00
|
|
|
asMutable(futureAny)->reassign(myAny);
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
CHECK(get<AnyType>(futureAny) != nullptr);
|
2022-06-11 00:58:21 +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();
|