2023-08-05 01:01:35 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Luau/Scope.h"
|
|
|
|
#include "Luau/ToString.h"
|
|
|
|
#include "Luau/TypeArena.h"
|
|
|
|
#include "Luau/Unifier2.h"
|
|
|
|
#include "Luau/Error.h"
|
|
|
|
|
|
|
|
#include "ScopedFlags.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
2023-12-02 10:04:44 +08:00
|
|
|
LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution)
|
|
|
|
|
2023-08-05 01:01:35 +08:00
|
|
|
struct Unifier2Fixture
|
|
|
|
{
|
|
|
|
TypeArena arena;
|
|
|
|
BuiltinTypes builtinTypes;
|
|
|
|
Scope scope{builtinTypes.anyTypePack};
|
|
|
|
InternalErrorReporter iceReporter;
|
2023-09-23 02:10:49 +08:00
|
|
|
Unifier2 u2{NotNull{&arena}, NotNull{&builtinTypes}, NotNull{&scope}, NotNull{&iceReporter}};
|
2023-08-05 01:01:35 +08:00
|
|
|
ToStringOptions opts;
|
|
|
|
|
2023-12-02 10:04:44 +08:00
|
|
|
ScopedFastFlag sff{FFlag::DebugLuauDeferredConstraintResolution, true};
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
std::pair<TypeId, FreeType*> freshType()
|
|
|
|
{
|
|
|
|
FreeType ft{&scope, builtinTypes.neverType, builtinTypes.unknownType};
|
|
|
|
|
|
|
|
TypeId ty = arena.addType(ft);
|
|
|
|
FreeType* ftv = getMutable<FreeType>(ty);
|
|
|
|
REQUIRE(ftv != nullptr);
|
|
|
|
|
|
|
|
return {ty, ftv};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString(TypeId ty)
|
|
|
|
{
|
|
|
|
return ::Luau::toString(ty, opts);
|
|
|
|
}
|
2023-09-30 08:22:06 +08:00
|
|
|
|
|
|
|
std::string toString(TypePackId ty)
|
|
|
|
{
|
|
|
|
return ::Luau::toString(ty, opts);
|
|
|
|
}
|
2023-08-05 01:01:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("Unifier2");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "T <: number")
|
|
|
|
{
|
|
|
|
auto [left, freeLeft] = freshType();
|
|
|
|
|
|
|
|
CHECK(u2.unify(left, builtinTypes.numberType));
|
|
|
|
|
|
|
|
CHECK("never" == toString(freeLeft->lowerBound));
|
|
|
|
CHECK("number" == toString(freeLeft->upperBound));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "number <: T")
|
|
|
|
{
|
|
|
|
auto [right, freeRight] = freshType();
|
|
|
|
|
|
|
|
CHECK(u2.unify(builtinTypes.numberType, right));
|
|
|
|
|
|
|
|
CHECK("number" == toString(freeRight->lowerBound));
|
|
|
|
CHECK("unknown" == toString(freeRight->upperBound));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "T <: U")
|
|
|
|
{
|
|
|
|
auto [left, freeLeft] = freshType();
|
|
|
|
auto [right, freeRight] = freshType();
|
|
|
|
|
|
|
|
CHECK(u2.unify(left, right));
|
|
|
|
|
2024-02-24 02:40:00 +08:00
|
|
|
CHECK("'a" == toString(left));
|
|
|
|
CHECK("'a" == toString(right));
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
CHECK("never" == toString(freeLeft->lowerBound));
|
2024-02-24 02:40:00 +08:00
|
|
|
CHECK("unknown" == toString(freeLeft->upperBound));
|
2023-08-05 01:01:35 +08:00
|
|
|
|
2024-02-24 02:40:00 +08:00
|
|
|
CHECK("never" == toString(freeRight->lowerBound));
|
2023-08-05 01:01:35 +08:00
|
|
|
CHECK("unknown" == toString(freeRight->upperBound));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "(string) -> () <: (X) -> Y...")
|
|
|
|
{
|
2023-09-16 00:27:45 +08:00
|
|
|
TypeId stringToUnit = arena.addType(FunctionType{arena.addTypePack({builtinTypes.stringType}), arena.addTypePack({})});
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
auto [x, xFree] = freshType();
|
|
|
|
TypePackId y = arena.freshTypePack(&scope);
|
|
|
|
|
2023-09-16 00:27:45 +08:00
|
|
|
TypeId xToY = arena.addType(FunctionType{arena.addTypePack({x}), y});
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
u2.unify(stringToUnit, xToY);
|
|
|
|
|
|
|
|
CHECK("string" == toString(xFree->upperBound));
|
|
|
|
|
|
|
|
const TypePack* yPack = get<TypePack>(follow(y));
|
|
|
|
REQUIRE(yPack != nullptr);
|
|
|
|
|
|
|
|
CHECK(0 == yPack->head.size());
|
|
|
|
CHECK(!yPack->tail);
|
|
|
|
}
|
|
|
|
|
2023-09-30 08:22:06 +08:00
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "unify_binds_free_subtype_tail_pack")
|
|
|
|
{
|
|
|
|
TypePackId numberPack = arena.addTypePack({builtinTypes.numberType});
|
|
|
|
|
|
|
|
TypePackId freeTail = arena.freshTypePack(&scope);
|
|
|
|
TypeId freeHead = arena.addType(FreeType{&scope, builtinTypes.neverType, builtinTypes.unknownType});
|
|
|
|
TypePackId freeAndFree = arena.addTypePack({freeHead}, freeTail);
|
|
|
|
|
|
|
|
u2.unify(freeAndFree, numberPack);
|
|
|
|
|
|
|
|
CHECK("('a <: number)" == toString(freeAndFree));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "unify_binds_free_supertype_tail_pack")
|
|
|
|
{
|
|
|
|
TypePackId numberPack = arena.addTypePack({builtinTypes.numberType});
|
|
|
|
|
|
|
|
TypePackId freeTail = arena.freshTypePack(&scope);
|
|
|
|
TypeId freeHead = arena.addType(FreeType{&scope, builtinTypes.neverType, builtinTypes.unknownType});
|
|
|
|
TypePackId freeAndFree = arena.addTypePack({freeHead}, freeTail);
|
|
|
|
|
|
|
|
u2.unify(numberPack, freeAndFree);
|
|
|
|
|
|
|
|
CHECK("(number <: 'a)" == toString(freeAndFree));
|
|
|
|
}
|
|
|
|
|
2023-09-16 00:27:45 +08:00
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "generalize_a_type_that_is_bounded_by_another_generalizable_type")
|
|
|
|
{
|
|
|
|
auto [t1, ft1] = freshType();
|
|
|
|
auto [t2, ft2] = freshType();
|
|
|
|
|
|
|
|
// t2 <: t1 <: unknown
|
|
|
|
// unknown <: t2 <: t1
|
|
|
|
|
|
|
|
ft1->lowerBound = t2;
|
|
|
|
ft2->upperBound = t1;
|
|
|
|
ft2->lowerBound = builtinTypes.unknownType;
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
auto t2generalized = u2.generalize(t2);
|
2023-09-16 00:27:45 +08:00
|
|
|
REQUIRE(t2generalized);
|
|
|
|
|
|
|
|
CHECK(follow(t1) == follow(t2));
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
auto t1generalized = u2.generalize(t1);
|
2023-09-16 00:27:45 +08:00
|
|
|
REQUIRE(t1generalized);
|
|
|
|
|
|
|
|
CHECK(builtinTypes.unknownType == follow(t1));
|
|
|
|
CHECK(builtinTypes.unknownType == follow(t2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as generalize_a_type_that_is_bounded_by_another_generalizable_type
|
|
|
|
// except that we generalize the types in the opposite order
|
|
|
|
TEST_CASE_FIXTURE(Unifier2Fixture, "generalize_a_type_that_is_bounded_by_another_generalizable_type_in_reverse_order")
|
|
|
|
{
|
|
|
|
auto [t1, ft1] = freshType();
|
|
|
|
auto [t2, ft2] = freshType();
|
|
|
|
|
|
|
|
// t2 <: t1 <: unknown
|
|
|
|
// unknown <: t2 <: t1
|
|
|
|
|
|
|
|
ft1->lowerBound = t2;
|
|
|
|
ft2->upperBound = t1;
|
|
|
|
ft2->lowerBound = builtinTypes.unknownType;
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
auto t1generalized = u2.generalize(t1);
|
2023-09-16 00:27:45 +08:00
|
|
|
REQUIRE(t1generalized);
|
|
|
|
|
|
|
|
CHECK(follow(t1) == follow(t2));
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
auto t2generalized = u2.generalize(t2);
|
2023-09-16 00:27:45 +08:00
|
|
|
REQUIRE(t2generalized);
|
|
|
|
|
|
|
|
CHECK(builtinTypes.unknownType == follow(t1));
|
|
|
|
CHECK(builtinTypes.unknownType == follow(t2));
|
|
|
|
}
|
|
|
|
|
2023-08-05 01:01:35 +08:00
|
|
|
TEST_SUITE_END();
|