2021-10-29 16:25:12 -04:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2022-04-07 16:53:47 -04:00
|
|
|
#include "Luau/Clone.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
#include "Luau/Module.h"
|
2021-11-04 22:07:18 -04:00
|
|
|
#include "Luau/Scope.h"
|
2022-04-14 17:57:15 -04:00
|
|
|
#include "Luau/RecursionCounter.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
LUAU_FASTFLAG(LuauLowerBoundsCalculation);
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TEST_SUITE_BEGIN("ModuleTests");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "is_within_comment")
|
|
|
|
{
|
|
|
|
check(R"(
|
|
|
|
--!strict
|
|
|
|
local foo = {}
|
|
|
|
function foo:bar() end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
foo:
|
|
|
|
]] foo:bar()
|
|
|
|
|
|
|
|
--[[]]--[[]] -- Two distinct comments that have zero characters of space between them.
|
|
|
|
)");
|
|
|
|
|
|
|
|
SourceModule* sm = getMainSourceModule();
|
|
|
|
|
|
|
|
CHECK_EQ(5, sm->commentLocations.size());
|
|
|
|
|
|
|
|
CHECK(isWithinComment(*sm, Position{1, 15}));
|
|
|
|
CHECK(isWithinComment(*sm, Position{6, 16}));
|
|
|
|
CHECK(isWithinComment(*sm, Position{9, 13}));
|
|
|
|
CHECK(isWithinComment(*sm, Position{9, 14}));
|
|
|
|
|
|
|
|
CHECK(!isWithinComment(*sm, Position{2, 15}));
|
|
|
|
CHECK(!isWithinComment(*sm, Position{7, 10}));
|
|
|
|
CHECK(!isWithinComment(*sm, Position{7, 11}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_clone_persistent_primitive")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
// numberType is persistent. We leave it as-is.
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId newNumber = clone(typeChecker.numberType, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
CHECK_EQ(newNumber, typeChecker.numberType);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_non_persistent_primitive")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
// Create a new number type that isn't persistent
|
|
|
|
unfreeze(typeChecker.globalTypes);
|
|
|
|
TypeId oldNumber = typeChecker.globalTypes.addType(PrimitiveTypeVar{PrimitiveTypeVar::Number});
|
|
|
|
freeze(typeChecker.globalTypes);
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId newNumber = clone(oldNumber, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
CHECK_NE(newNumber, oldNumber);
|
|
|
|
CHECK_EQ(*oldNumber, *newNumber);
|
|
|
|
CHECK_EQ("number", toString(newNumber));
|
|
|
|
CHECK_EQ(1, dest.typeVars.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_cyclic_table")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local Cyclic = {}
|
|
|
|
function Cyclic.get()
|
|
|
|
return Cyclic
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
/* The inferred type of Cyclic is {get: () -> Cyclic}
|
|
|
|
*
|
|
|
|
* Assert that the return type of get() is the same as the outer table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
TypeId counterType = requireType("Cyclic");
|
|
|
|
|
|
|
|
TypeArena dest;
|
2022-04-14 17:57:15 -04:00
|
|
|
CloneState cloneState;
|
|
|
|
TypeId counterCopy = clone(counterType, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
TableTypeVar* ttv = getMutable<TableTypeVar>(counterCopy);
|
|
|
|
REQUIRE(ttv != nullptr);
|
|
|
|
|
|
|
|
CHECK_EQ(std::optional<std::string>{"Cyclic"}, ttv->syntheticName);
|
|
|
|
|
|
|
|
TypeId methodType = ttv->props["get"].type;
|
|
|
|
REQUIRE(methodType != nullptr);
|
|
|
|
|
|
|
|
const FunctionTypeVar* ftv = get<FunctionTypeVar>(methodType);
|
|
|
|
REQUIRE(ftv != nullptr);
|
|
|
|
|
|
|
|
std::optional<TypeId> methodReturnType = first(ftv->retType);
|
|
|
|
REQUIRE(methodReturnType);
|
|
|
|
|
|
|
|
CHECK_EQ(methodReturnType, counterCopy);
|
2022-04-14 17:57:15 -04:00
|
|
|
if (FFlag::LuauLowerBoundsCalculation)
|
|
|
|
CHECK_EQ(3, dest.typePacks.size()); // function args, its return type, and the hidden any... pack
|
|
|
|
else
|
|
|
|
CHECK_EQ(2, dest.typePacks.size()); // one for the function args, and another for its return type
|
|
|
|
CHECK_EQ(2, dest.typeVars.size()); // One table and one function
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 15:16:50 -04:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "builtin_types_point_into_globalTypes_arena")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
return {sign=math.sign}
|
|
|
|
)");
|
|
|
|
dumpErrors(result);
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
ModulePtr module = frontend.moduleResolver.getModule("MainModule");
|
|
|
|
std::optional<TypeId> exports = first(module->getModuleScope()->returnType);
|
|
|
|
REQUIRE(bool(exports));
|
|
|
|
|
|
|
|
REQUIRE(isInArena(*exports, module->interfaceTypes));
|
|
|
|
|
|
|
|
TableTypeVar* exportsTable = getMutable<TableTypeVar>(*exports);
|
|
|
|
REQUIRE(exportsTable != nullptr);
|
|
|
|
|
|
|
|
TypeId signType = exportsTable->props["sign"].type;
|
|
|
|
REQUIRE(signType != nullptr);
|
|
|
|
|
|
|
|
CHECK(!isInArena(signType, module->interfaceTypes));
|
|
|
|
CHECK(isInArena(signType, typeChecker.globalTypes));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_union")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
unfreeze(typeChecker.globalTypes);
|
|
|
|
TypeId oldUnion = typeChecker.globalTypes.addType(UnionTypeVar{{typeChecker.numberType, typeChecker.stringType}});
|
|
|
|
freeze(typeChecker.globalTypes);
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId newUnion = clone(oldUnion, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
CHECK_NE(newUnion, oldUnion);
|
|
|
|
CHECK_EQ("number | string", toString(newUnion));
|
|
|
|
CHECK_EQ(1, dest.typeVars.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_intersection")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
unfreeze(typeChecker.globalTypes);
|
|
|
|
TypeId oldIntersection = typeChecker.globalTypes.addType(IntersectionTypeVar{{typeChecker.numberType, typeChecker.stringType}});
|
|
|
|
freeze(typeChecker.globalTypes);
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId newIntersection = clone(oldIntersection, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
CHECK_NE(newIntersection, oldIntersection);
|
|
|
|
CHECK_EQ("number & string", toString(newIntersection));
|
|
|
|
CHECK_EQ(1, dest.typeVars.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_class")
|
|
|
|
{
|
|
|
|
TypeVar exampleMetaClass{ClassTypeVar{"ExampleClassMeta",
|
|
|
|
{
|
|
|
|
{"__add", {typeChecker.anyType}},
|
|
|
|
},
|
2022-04-21 17:04:22 -04:00
|
|
|
std::nullopt, std::nullopt, {}, {}, "Test"}};
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeVar exampleClass{ClassTypeVar{"ExampleClass",
|
|
|
|
{
|
|
|
|
{"PropOne", {typeChecker.numberType}},
|
|
|
|
{"PropTwo", {typeChecker.stringType}},
|
|
|
|
},
|
2022-04-21 17:04:22 -04:00
|
|
|
std::nullopt, &exampleMetaClass, {}, {}, "Test"}};
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId cloned = clone(&exampleClass, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
const ClassTypeVar* ctv = get<ClassTypeVar>(cloned);
|
|
|
|
REQUIRE(ctv != nullptr);
|
|
|
|
|
|
|
|
REQUIRE(ctv->metatable);
|
|
|
|
const ClassTypeVar* metatable = get<ClassTypeVar>(*ctv->metatable);
|
|
|
|
REQUIRE(metatable);
|
|
|
|
|
|
|
|
CHECK_EQ("ExampleClass", ctv->name);
|
|
|
|
CHECK_EQ("ExampleClassMeta", metatable->name);
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:04:22 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_free_types")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
2022-04-21 17:04:22 -04:00
|
|
|
ScopedFastFlag sff[]{
|
|
|
|
{"LuauLosslessClone", true},
|
|
|
|
};
|
2021-11-18 17:21:07 -05:00
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeVar freeTy(FreeTypeVar{TypeLevel{}});
|
|
|
|
TypePackVar freeTp(FreeTypePack{TypeLevel{}});
|
|
|
|
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId clonedTy = clone(&freeTy, dest, cloneState);
|
2022-04-21 17:04:22 -04:00
|
|
|
CHECK(get<FreeTypeVar>(clonedTy));
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2021-12-02 18:20:08 -05:00
|
|
|
cloneState = {};
|
2022-04-14 17:57:15 -04:00
|
|
|
TypePackId clonedTp = clone(&freeTp, dest, cloneState);
|
2022-04-21 17:04:22 -04:00
|
|
|
CHECK(get<FreeTypePack>(clonedTp));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-04-21 17:04:22 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_free_tables")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
2022-04-21 17:04:22 -04:00
|
|
|
ScopedFastFlag sff{"LuauLosslessClone", true};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeVar tableTy{TableTypeVar{}};
|
|
|
|
TableTypeVar* ttv = getMutable<TableTypeVar>(&tableTy);
|
|
|
|
ttv->state = TableState::Free;
|
|
|
|
|
|
|
|
TypeArena dest;
|
2021-12-02 18:20:08 -05:00
|
|
|
CloneState cloneState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
TypeId cloned = clone(&tableTy, dest, cloneState);
|
2021-10-29 16:25:12 -04:00
|
|
|
const TableTypeVar* clonedTtv = get<TableTypeVar>(cloned);
|
2022-04-21 17:04:22 -04:00
|
|
|
CHECK_EQ(clonedTtv->state, TableState::Free);
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_constrained_intersection")
|
|
|
|
{
|
|
|
|
TypeArena src;
|
|
|
|
|
|
|
|
TypeId constrained = src.addType(ConstrainedTypeVar{TypeLevel{}, {getSingletonTypes().numberType, getSingletonTypes().stringType}});
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState cloneState;
|
|
|
|
|
|
|
|
TypeId cloned = clone(constrained, dest, cloneState);
|
|
|
|
CHECK_NE(constrained, cloned);
|
|
|
|
|
|
|
|
const ConstrainedTypeVar* ctv = get<ConstrainedTypeVar>(cloned);
|
|
|
|
REQUIRE_EQ(2, ctv->parts.size());
|
|
|
|
CHECK_EQ(getSingletonTypes().numberType, ctv->parts[0]);
|
|
|
|
CHECK_EQ(getSingletonTypes().stringType, ctv->parts[1]);
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:16:50 -04:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "clone_self_property")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
2022-03-17 20:06:25 -04:00
|
|
|
ScopedFastFlag sff{"LuauAnyInIsOptionalIsOptional", true};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
fileResolver.source["Module/A"] = R"(
|
|
|
|
--!nonstrict
|
|
|
|
local a = {}
|
2022-03-17 20:06:25 -04:00
|
|
|
function a:foo(x: number)
|
2021-10-29 16:25:12 -04:00
|
|
|
return -x;
|
|
|
|
end
|
|
|
|
return a;
|
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult result = frontend.check("Module/A");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
fileResolver.source["Module/B"] = R"(
|
|
|
|
--!nonstrict
|
|
|
|
local a = require(script.Parent.A)
|
|
|
|
return a.foo(5)
|
|
|
|
)";
|
|
|
|
|
|
|
|
result = frontend.check("Module/B");
|
|
|
|
|
2022-03-17 20:06:25 -04:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
CHECK_EQ("This function must be called with self. Did you mean to use a colon instead of a dot?", toString(result.errors[0]));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:20:08 -05:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_recursion_limit")
|
|
|
|
{
|
|
|
|
#if defined(_DEBUG) || defined(_NOOPT)
|
|
|
|
int limit = 250;
|
|
|
|
#else
|
2021-12-10 16:17:10 -05:00
|
|
|
int limit = 400;
|
2021-12-02 18:20:08 -05:00
|
|
|
#endif
|
|
|
|
ScopedFastInt luauTypeCloneRecursionLimit{"LuauTypeCloneRecursionLimit", limit};
|
2022-04-14 17:57:15 -04:00
|
|
|
ScopedFastFlag sff{"LuauRecursionLimitException", true};
|
2021-12-02 18:20:08 -05:00
|
|
|
|
|
|
|
TypeArena src;
|
|
|
|
|
|
|
|
TypeId table = src.addType(TableTypeVar{});
|
|
|
|
TypeId nested = table;
|
|
|
|
|
2021-12-10 16:17:10 -05:00
|
|
|
for (int i = 0; i < limit + 100; i++)
|
2021-12-02 18:20:08 -05:00
|
|
|
{
|
|
|
|
TableTypeVar* ttv = getMutable<TableTypeVar>(nested);
|
|
|
|
|
|
|
|
ttv->props["a"].type = src.addType(TableTypeVar{});
|
|
|
|
nested = ttv->props["a"].type;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState cloneState;
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
CHECK_THROWS_AS(clone(table, dest, cloneState), RecursionLimitException);
|
2021-12-02 18:20:08 -05:00
|
|
|
}
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TEST_SUITE_END();
|