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
|
2022-04-08 05:29:01 +08:00
|
|
|
#include "Luau/Clone.h"
|
2023-08-11 22:42:37 +08:00
|
|
|
#include "Luau/Common.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/Module.h"
|
2023-04-15 02:06:22 +08:00
|
|
|
#include "Luau/Parser.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
#include "ScopedFlags.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
LUAU_FASTFLAG(LuauSolverV2);
|
2023-12-02 15:46:57 +08:00
|
|
|
LUAU_FASTFLAG(DebugLuauFreezeArena);
|
|
|
|
LUAU_FASTINT(LuauTypeCloneIterationLimit);
|
2022-04-15 07:57:43 +08:00
|
|
|
|
2021-10-30 04:25:12 +08: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}));
|
|
|
|
}
|
|
|
|
|
2023-04-15 02:06:22 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "is_within_comment_parse_result")
|
|
|
|
{
|
|
|
|
std::string src = R"(
|
|
|
|
--!strict
|
|
|
|
local foo = {}
|
|
|
|
function foo:bar() end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
foo:
|
|
|
|
]] foo:bar()
|
|
|
|
|
|
|
|
--[[]]--[[]] -- Two distinct comments that have zero characters of space between them.
|
|
|
|
)";
|
|
|
|
|
|
|
|
Luau::Allocator alloc;
|
|
|
|
Luau::AstNameTable names{alloc};
|
|
|
|
Luau::ParseOptions parseOptions;
|
|
|
|
parseOptions.captureComments = true;
|
|
|
|
Luau::ParseResult parseResult = Luau::Parser::parse(src.data(), src.size(), names, alloc, parseOptions);
|
|
|
|
|
|
|
|
CHECK_EQ(5, parseResult.commentLocations.size());
|
|
|
|
|
|
|
|
CHECK(isWithinComment(parseResult, Position{1, 15}));
|
|
|
|
CHECK(isWithinComment(parseResult, Position{6, 16}));
|
|
|
|
CHECK(isWithinComment(parseResult, Position{9, 13}));
|
|
|
|
CHECK(isWithinComment(parseResult, Position{9, 14}));
|
|
|
|
|
|
|
|
CHECK(!isWithinComment(parseResult, Position{2, 15}));
|
|
|
|
CHECK(!isWithinComment(parseResult, Position{7, 10}));
|
|
|
|
CHECK(!isWithinComment(parseResult, Position{7, 11}));
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_clone_persistent_primitive")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// numberType is persistent. We leave it as-is.
|
2023-03-11 04:21:07 +08:00
|
|
|
TypeId newNumber = clone(builtinTypes->numberType, dest, cloneState);
|
|
|
|
CHECK_EQ(newNumber, builtinTypes->numberType);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_non_persistent_primitive")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
// Create a new number type that isn't persistent
|
2023-03-11 04:21:07 +08:00
|
|
|
unfreeze(frontend.globals.globalTypes);
|
|
|
|
TypeId oldNumber = frontend.globals.globalTypes.addType(PrimitiveType{PrimitiveType::Number});
|
|
|
|
freeze(frontend.globals.globalTypes);
|
2022-04-15 07:57:43 +08:00
|
|
|
TypeId newNumber = clone(oldNumber, dest, cloneState);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK_NE(newNumber, oldNumber);
|
|
|
|
CHECK_EQ(*oldNumber, *newNumber);
|
|
|
|
CHECK_EQ("number", toString(newNumber));
|
2023-01-05 04:53:17 +08:00
|
|
|
CHECK_EQ(1, dest.types.size());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_cyclic_table")
|
|
|
|
{
|
2023-02-25 05:49:38 +08:00
|
|
|
// Under DCR, we don't seal the outer occurrance of the table `Cyclic` which
|
|
|
|
// breaks this test. I'm not sure if that behaviour change is important or
|
|
|
|
// not, but it's tangental to the core purpose of this test.
|
|
|
|
|
|
|
|
ScopedFastFlag sff[] = {
|
2024-08-31 04:16:51 +08:00
|
|
|
{FFlag::LuauSolverV2, false},
|
2023-02-25 05:49:38 +08:00
|
|
|
};
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
TypeId ty = requireType("Cyclic");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2023-02-25 05:49:38 +08:00
|
|
|
TypeId cloneTy = clone(ty, dest, cloneState);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
TableType* ttv = getMutable<TableType>(cloneTy);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(ttv != nullptr);
|
|
|
|
|
|
|
|
CHECK_EQ(std::optional<std::string>{"Cyclic"}, ttv->syntheticName);
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
TypeId methodType = ttv->props["get"].type();
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(methodType != nullptr);
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
const FunctionType* ftv = get<FunctionType>(methodType);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(ftv != nullptr);
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
std::optional<TypeId> methodReturnType = first(ftv->retTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(methodReturnType);
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
CHECK_MESSAGE(methodReturnType == cloneTy, toString(methodType, {true}) << " should be pointer identical to " << toString(cloneTy, {true}));
|
2023-08-19 02:15:41 +08:00
|
|
|
CHECK_EQ(2, dest.typePacks.size()); // one for the function args, and another for its return type
|
|
|
|
CHECK_EQ(2, dest.types.size()); // One table and one function
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:49:38 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_cyclic_table_2")
|
|
|
|
{
|
|
|
|
TypeArena src;
|
|
|
|
|
|
|
|
TypeId tableTy = src.addType(TableType{});
|
|
|
|
TableType* tt = getMutable<TableType>(tableTy);
|
|
|
|
REQUIRE(tt);
|
|
|
|
|
|
|
|
TypeId methodTy = src.addType(FunctionType{src.addTypePack({}), src.addTypePack({tableTy})});
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
tt->props["get"].setType(methodTy);
|
2023-02-25 05:49:38 +08:00
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2023-02-25 05:49:38 +08:00
|
|
|
TypeId cloneTy = clone(tableTy, dest, cloneState);
|
|
|
|
TableType* ctt = getMutable<TableType>(cloneTy);
|
|
|
|
REQUIRE(ctt);
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
TypeId clonedMethodType = ctt->props["get"].type();
|
2023-02-25 05:49:38 +08:00
|
|
|
REQUIRE(clonedMethodType);
|
|
|
|
|
|
|
|
const FunctionType* cmf = get<FunctionType>(clonedMethodType);
|
|
|
|
REQUIRE(cmf);
|
|
|
|
|
|
|
|
std::optional<TypeId> cloneMethodReturnType = first(cmf->retTypes);
|
|
|
|
REQUIRE(bool(cloneMethodReturnType));
|
|
|
|
|
|
|
|
CHECK(*cloneMethodReturnType == cloneTy);
|
|
|
|
}
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "builtin_types_point_into_globalTypes_arena")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
return {sign=math.sign}
|
|
|
|
)");
|
|
|
|
dumpErrors(result);
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
ModulePtr module = frontend.moduleResolver.getModule("MainModule");
|
2023-01-07 05:14:35 +08:00
|
|
|
std::optional<TypeId> exports = first(module->returnType);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(bool(exports));
|
|
|
|
|
|
|
|
REQUIRE(isInArena(*exports, module->interfaceTypes));
|
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TableType* exportsTable = getMutable<TableType>(*exports);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(exportsTable != nullptr);
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
TypeId signType = exportsTable->props["sign"].type();
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(signType != nullptr);
|
|
|
|
|
|
|
|
CHECK(!isInArena(signType, module->interfaceTypes));
|
2023-03-11 04:21:07 +08:00
|
|
|
CHECK(isInArena(signType, frontend.globals.globalTypes));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_union")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-03-11 04:21:07 +08:00
|
|
|
unfreeze(frontend.globals.globalTypes);
|
|
|
|
TypeId oldUnion = frontend.globals.globalTypes.addType(UnionType{{builtinTypes->numberType, builtinTypes->stringType}});
|
|
|
|
freeze(frontend.globals.globalTypes);
|
2022-04-15 07:57:43 +08:00
|
|
|
TypeId newUnion = clone(oldUnion, dest, cloneState);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK_NE(newUnion, oldUnion);
|
|
|
|
CHECK_EQ("number | string", toString(newUnion));
|
2023-01-05 04:53:17 +08:00
|
|
|
CHECK_EQ(1, dest.types.size());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "deepClone_intersection")
|
|
|
|
{
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-03-11 04:21:07 +08:00
|
|
|
unfreeze(frontend.globals.globalTypes);
|
|
|
|
TypeId oldIntersection = frontend.globals.globalTypes.addType(IntersectionType{{builtinTypes->numberType, builtinTypes->stringType}});
|
|
|
|
freeze(frontend.globals.globalTypes);
|
2022-04-15 07:57:43 +08:00
|
|
|
TypeId newIntersection = clone(oldIntersection, dest, cloneState);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
CHECK_NE(newIntersection, oldIntersection);
|
|
|
|
CHECK_EQ("number & string", toString(newIntersection));
|
2023-01-05 04:53:17 +08:00
|
|
|
CHECK_EQ(1, dest.types.size());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_class")
|
|
|
|
{
|
2024-08-02 22:30:04 +08:00
|
|
|
Type exampleMetaClass{ClassType{
|
|
|
|
"ExampleClassMeta",
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-03-11 04:21:07 +08:00
|
|
|
{"__add", {builtinTypes->anyType}},
|
2021-10-30 04:25:12 +08:00
|
|
|
},
|
2024-08-02 22:30:04 +08:00
|
|
|
std::nullopt,
|
|
|
|
std::nullopt,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
"Test",
|
|
|
|
{}
|
|
|
|
}};
|
|
|
|
Type exampleClass{ClassType{
|
|
|
|
"ExampleClass",
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-03-11 04:21:07 +08:00
|
|
|
{"PropOne", {builtinTypes->numberType}},
|
|
|
|
{"PropTwo", {builtinTypes->stringType}},
|
2021-10-30 04:25:12 +08:00
|
|
|
},
|
2024-08-02 22:30:04 +08:00
|
|
|
std::nullopt,
|
|
|
|
&exampleMetaClass,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
"Test",
|
|
|
|
{}
|
|
|
|
}};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-04-15 07:57:43 +08:00
|
|
|
TypeId cloned = clone(&exampleClass, dest, cloneState);
|
2023-01-05 04:53:17 +08:00
|
|
|
const ClassType* ctv = get<ClassType>(cloned);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(ctv != nullptr);
|
|
|
|
|
|
|
|
REQUIRE(ctv->metatable);
|
2023-01-05 04:53:17 +08:00
|
|
|
const ClassType* metatable = get<ClassType>(*ctv->metatable);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE(metatable);
|
|
|
|
|
|
|
|
CHECK_EQ("ExampleClass", ctv->name);
|
|
|
|
CHECK_EQ("ExampleClassMeta", metatable->name);
|
|
|
|
}
|
|
|
|
|
2022-04-22 05:44:27 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_free_types")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2024-08-31 04:16:51 +08:00
|
|
|
ScopedFastFlag sff{FFlag::LuauSolverV2, false};
|
2023-08-05 03:18:54 +08:00
|
|
|
|
|
|
|
TypeArena arena;
|
|
|
|
TypeId freeTy = freshType(NotNull{&arena}, builtinTypes, nullptr);
|
2021-10-30 04:25:12 +08:00
|
|
|
TypePackVar freeTp(FreeTypePack{TypeLevel{}});
|
|
|
|
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-08-05 03:18:54 +08:00
|
|
|
TypeId clonedTy = clone(freeTy, dest, cloneState);
|
2023-01-05 04:53:17 +08:00
|
|
|
CHECK(get<FreeType>(clonedTy));
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
cloneState = {builtinTypes};
|
2022-04-15 07:57:43 +08:00
|
|
|
TypePackId clonedTp = clone(&freeTp, dest, cloneState);
|
2022-04-22 05:44:27 +08:00
|
|
|
CHECK(get<FreeTypePack>(clonedTp));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-04-22 05:44:27 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_free_tables")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-01-05 04:53:17 +08:00
|
|
|
Type tableTy{TableType{}};
|
|
|
|
TableType* ttv = getMutable<TableType>(&tableTy);
|
2021-10-30 04:25:12 +08:00
|
|
|
ttv->state = TableState::Free;
|
|
|
|
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-04-15 07:57:43 +08:00
|
|
|
TypeId cloned = clone(&tableTy, dest, cloneState);
|
2023-01-05 04:53:17 +08:00
|
|
|
const TableType* clonedTtv = get<TableType>(cloned);
|
2022-04-22 05:44:27 +08:00
|
|
|
CHECK_EQ(clonedTtv->state, TableState::Free);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-05-14 03:36:37 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "clone_self_property")
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2024-08-24 00:35:30 +08:00
|
|
|
// CLI-117082 ModuleTests.clone_self_property we don't infer self correctly, instead replacing it with unknown.
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2024-08-24 00:35:30 +08:00
|
|
|
return;
|
2021-10-30 04:25:12 +08:00
|
|
|
fileResolver.source["Module/A"] = R"(
|
|
|
|
--!nonstrict
|
|
|
|
local a = {}
|
2022-03-18 08:46:04 +08:00
|
|
|
function a:foo(x: number)
|
2021-10-30 04:25:12 +08: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-18 08:46:04 +08: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-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_iteration_limit")
|
|
|
|
{
|
2024-04-13 01:18:49 +08:00
|
|
|
ScopedFastInt sfi{FInt::LuauTypeCloneIterationLimit, 2000};
|
2023-08-11 22:42:37 +08:00
|
|
|
|
|
|
|
TypeArena src;
|
|
|
|
|
|
|
|
TypeId table = src.addType(TableType{});
|
|
|
|
TypeId nested = table;
|
|
|
|
|
2024-04-13 01:18:49 +08:00
|
|
|
int nesting = 2500;
|
|
|
|
for (int i = 0; i < nesting; i++)
|
2023-08-11 22:42:37 +08:00
|
|
|
{
|
|
|
|
TableType* ttv = getMutable<TableType>(nested);
|
|
|
|
ttv->props["a"].setType(src.addType(TableType{}));
|
|
|
|
nested = ttv->props["a"].type();
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState cloneState{builtinTypes};
|
|
|
|
|
|
|
|
TypeId ty = clone(table, dest, cloneState);
|
|
|
|
CHECK(get<ErrorType>(ty));
|
|
|
|
|
|
|
|
// Cloning it again is an important test.
|
|
|
|
TypeId ty2 = clone(table, dest, cloneState);
|
|
|
|
CHECK(get<ErrorType>(ty2));
|
|
|
|
}
|
|
|
|
|
2023-05-20 03:37:30 +08:00
|
|
|
// Unions should never be cyclic, but we should clone them correctly even if
|
|
|
|
// they are.
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "clone_cyclic_union")
|
|
|
|
{
|
|
|
|
TypeArena src;
|
|
|
|
|
|
|
|
TypeId u = src.addType(UnionType{{builtinTypes->numberType, builtinTypes->stringType}});
|
|
|
|
UnionType* uu = getMutable<UnionType>(u);
|
|
|
|
REQUIRE(uu);
|
|
|
|
|
|
|
|
uu->options.push_back(u);
|
|
|
|
|
|
|
|
TypeArena dest;
|
2023-08-11 22:42:37 +08:00
|
|
|
CloneState cloneState{builtinTypes};
|
2023-05-20 03:37:30 +08:00
|
|
|
|
|
|
|
TypeId cloned = clone(u, dest, cloneState);
|
|
|
|
REQUIRE(cloned);
|
|
|
|
|
|
|
|
const UnionType* clonedUnion = get<UnionType>(cloned);
|
|
|
|
REQUIRE(clonedUnion);
|
|
|
|
REQUIRE(3 == clonedUnion->options.size());
|
|
|
|
|
|
|
|
CHECK(builtinTypes->numberType == clonedUnion->options[0]);
|
|
|
|
CHECK(builtinTypes->stringType == clonedUnion->options[1]);
|
|
|
|
CHECK(cloned == clonedUnion->options[2]);
|
|
|
|
}
|
|
|
|
|
2022-06-11 00:58:21 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "any_persistance_does_not_leak")
|
|
|
|
{
|
|
|
|
fileResolver.source["Module/A"] = R"(
|
|
|
|
export type A = B
|
|
|
|
type B = A
|
|
|
|
)";
|
|
|
|
|
|
|
|
FrontendOptions opts;
|
|
|
|
opts.retainFullTypeGraphs = false;
|
|
|
|
CheckResult result = frontend.check("Module/A", opts);
|
|
|
|
LUAU_REQUIRE_ERRORS(result);
|
|
|
|
|
|
|
|
auto mod = frontend.moduleResolver.getModule("Module/A");
|
2023-01-07 05:14:35 +08:00
|
|
|
auto it = mod->exportedTypeBindings.find("A");
|
|
|
|
REQUIRE(it != mod->exportedTypeBindings.end());
|
2023-04-22 06:14:26 +08:00
|
|
|
|
2024-08-31 04:16:51 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-04-22 06:14:26 +08:00
|
|
|
CHECK(toString(it->second.type) == "any");
|
|
|
|
else
|
|
|
|
CHECK(toString(it->second.type) == "*error-type*");
|
2022-06-11 00:58:21 +08:00
|
|
|
}
|
|
|
|
|
2022-08-12 05:01:33 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "do_not_clone_reexports")
|
|
|
|
{
|
|
|
|
fileResolver.source["Module/A"] = R"(
|
2023-07-15 02:08:53 +08:00
|
|
|
export type A = {p : number}
|
|
|
|
return {}
|
2022-08-12 05:01:33 +08:00
|
|
|
)";
|
|
|
|
|
|
|
|
fileResolver.source["Module/B"] = R"(
|
2023-07-15 02:08:53 +08:00
|
|
|
local a = require(script.Parent.A)
|
|
|
|
export type B = {q : a.A}
|
|
|
|
return {}
|
2022-08-12 05:01:33 +08:00
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult result = frontend.check("Module/B");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
ModulePtr modA = frontend.moduleResolver.getModule("Module/A");
|
|
|
|
ModulePtr modB = frontend.moduleResolver.getModule("Module/B");
|
|
|
|
REQUIRE(modA);
|
|
|
|
REQUIRE(modB);
|
2023-01-07 05:14:35 +08:00
|
|
|
auto modAiter = modA->exportedTypeBindings.find("A");
|
|
|
|
auto modBiter = modB->exportedTypeBindings.find("B");
|
|
|
|
REQUIRE(modAiter != modA->exportedTypeBindings.end());
|
|
|
|
REQUIRE(modBiter != modB->exportedTypeBindings.end());
|
2022-08-12 05:01:33 +08:00
|
|
|
TypeId typeA = modAiter->second.type;
|
|
|
|
TypeId typeB = modBiter->second.type;
|
2023-01-05 04:53:17 +08:00
|
|
|
TableType* tableB = getMutable<TableType>(typeB);
|
2022-08-12 05:01:33 +08:00
|
|
|
REQUIRE(tableB);
|
2023-04-29 03:55:13 +08:00
|
|
|
CHECK(typeA == tableB->props["q"].type());
|
2022-08-12 05:01:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "do_not_clone_types_of_reexported_values")
|
|
|
|
{
|
|
|
|
fileResolver.source["Module/A"] = R"(
|
2023-07-15 02:08:53 +08:00
|
|
|
local exports = {a={p=5}}
|
|
|
|
return exports
|
2022-08-12 05:01:33 +08:00
|
|
|
)";
|
|
|
|
|
|
|
|
fileResolver.source["Module/B"] = R"(
|
2023-07-15 02:08:53 +08:00
|
|
|
local a = require(script.Parent.A)
|
|
|
|
local exports = {b=a.a}
|
|
|
|
return exports
|
2022-08-12 05:01:33 +08:00
|
|
|
)";
|
|
|
|
|
|
|
|
CheckResult result = frontend.check("Module/B");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
ModulePtr modA = frontend.moduleResolver.getModule("Module/A");
|
|
|
|
REQUIRE(modA);
|
2023-09-30 09:13:05 +08:00
|
|
|
ModulePtr modB = frontend.moduleResolver.getModule("Module/B");
|
2022-08-12 05:01:33 +08:00
|
|
|
REQUIRE(modB);
|
2023-09-30 09:13:05 +08:00
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
std::optional<TypeId> typeA = first(modA->returnType);
|
2022-08-12 05:01:33 +08:00
|
|
|
REQUIRE(typeA);
|
2023-09-30 09:13:05 +08:00
|
|
|
std::optional<TypeId> typeB = first(modB->returnType);
|
2022-08-12 05:01:33 +08:00
|
|
|
REQUIRE(typeB);
|
2023-09-30 09:13:05 +08:00
|
|
|
|
2023-01-05 04:53:17 +08:00
|
|
|
TableType* tableA = getMutable<TableType>(*typeA);
|
2023-09-30 09:13:05 +08:00
|
|
|
REQUIRE_MESSAGE(tableA, "Expected a table, but got " << toString(*typeA));
|
2023-01-05 04:53:17 +08:00
|
|
|
TableType* tableB = getMutable<TableType>(*typeB);
|
2023-09-30 09:13:05 +08:00
|
|
|
REQUIRE_MESSAGE(tableB, "Expected a table, but got " << toString(*typeB));
|
|
|
|
|
2023-04-29 03:55:13 +08:00
|
|
|
CHECK(tableA->props["a"].type() == tableB->props["b"].type());
|
2022-08-12 05:01:33 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 02:15:41 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "clone_table_bound_to_table_bound_to_table")
|
|
|
|
{
|
|
|
|
TypeArena arena;
|
|
|
|
|
|
|
|
TypeId a = arena.addType(TableType{TableState::Free, TypeLevel{}});
|
|
|
|
getMutable<TableType>(a)->name = "a";
|
|
|
|
|
|
|
|
TypeId b = arena.addType(TableType{TableState::Free, TypeLevel{}});
|
|
|
|
getMutable<TableType>(b)->name = "b";
|
|
|
|
|
|
|
|
TypeId c = arena.addType(TableType{TableState::Free, TypeLevel{}});
|
|
|
|
getMutable<TableType>(c)->name = "c";
|
|
|
|
|
|
|
|
getMutable<TableType>(a)->boundTo = b;
|
|
|
|
getMutable<TableType>(b)->boundTo = c;
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState state{builtinTypes};
|
|
|
|
TypeId res = clone(a, dest, state);
|
|
|
|
|
|
|
|
REQUIRE(dest.types.size() == 1);
|
|
|
|
|
|
|
|
auto tableA = get<TableType>(res);
|
|
|
|
REQUIRE_MESSAGE(tableA, "Expected table, got " << res);
|
|
|
|
REQUIRE(tableA->name == "c");
|
|
|
|
REQUIRE(!tableA->boundTo);
|
|
|
|
}
|
|
|
|
|
2023-11-04 07:45:04 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "clone_a_bound_type_to_a_persistent_type")
|
|
|
|
{
|
|
|
|
TypeArena arena;
|
|
|
|
|
|
|
|
TypeId boundTo = arena.addType(BoundType{builtinTypes->numberType});
|
|
|
|
REQUIRE(builtinTypes->numberType->persistent);
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState state{builtinTypes};
|
|
|
|
TypeId res = clone(boundTo, dest, state);
|
|
|
|
|
|
|
|
REQUIRE(res == follow(boundTo));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "clone_a_bound_typepack_to_a_persistent_typepack")
|
|
|
|
{
|
|
|
|
TypeArena arena;
|
|
|
|
|
|
|
|
TypePackId boundTo = arena.addTypePack(BoundTypePack{builtinTypes->neverTypePack});
|
|
|
|
REQUIRE(builtinTypes->neverTypePack->persistent);
|
|
|
|
|
|
|
|
TypeArena dest;
|
|
|
|
CloneState state{builtinTypes};
|
|
|
|
TypePackId res = clone(boundTo, dest, state);
|
|
|
|
|
|
|
|
REQUIRE(res == follow(boundTo));
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TEST_SUITE_END();
|