2021-11-05 15:47:21 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Luau/Quantify.h"
|
|
|
|
|
|
|
|
#include "Luau/VisitTypeVar.h"
|
|
|
|
|
2022-04-15 00:57:43 +01:00
|
|
|
LUAU_FASTFLAG(LuauTypecheckOptPass)
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Quantifier
|
|
|
|
{
|
|
|
|
TypeLevel level;
|
|
|
|
std::vector<TypeId> generics;
|
|
|
|
std::vector<TypePackId> genericPacks;
|
2022-04-15 00:57:43 +01:00
|
|
|
bool seenGenericType = false;
|
|
|
|
bool seenMutableType = false;
|
2021-11-05 15:47:21 +00:00
|
|
|
|
2022-02-18 01:18:01 +00:00
|
|
|
Quantifier(TypeLevel level)
|
|
|
|
: level(level)
|
2021-11-05 15:47:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cycle(TypeId) {}
|
|
|
|
void cycle(TypePackId) {}
|
|
|
|
|
|
|
|
bool operator()(TypeId ty, const FreeTypeVar& ftv)
|
|
|
|
{
|
2022-04-15 00:57:43 +01:00
|
|
|
if (FFlag::LuauTypecheckOptPass)
|
|
|
|
seenMutableType = true;
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
if (!level.subsumes(ftv.level))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*asMutable(ty) = GenericTypeVar{level};
|
|
|
|
generics.push_back(ty);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypeId ty, const T& t)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool operator()(TypePackId, const T&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-15 00:57:43 +01:00
|
|
|
bool operator()(TypeId ty, const ConstrainedTypeVar&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
bool operator()(TypeId ty, const TableTypeVar&)
|
|
|
|
{
|
|
|
|
TableTypeVar& ttv = *getMutable<TableTypeVar>(ty);
|
|
|
|
|
2022-04-15 00:57:43 +01:00
|
|
|
if (FFlag::LuauTypecheckOptPass)
|
|
|
|
{
|
|
|
|
if (ttv.state == TableState::Generic)
|
|
|
|
seenGenericType = true;
|
|
|
|
|
|
|
|
if (ttv.state == TableState::Free)
|
|
|
|
seenMutableType = true;
|
|
|
|
}
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
if (ttv.state == TableState::Sealed || ttv.state == TableState::Generic)
|
|
|
|
return false;
|
|
|
|
if (!level.subsumes(ttv.level))
|
2022-04-15 00:57:43 +01:00
|
|
|
{
|
|
|
|
if (FFlag::LuauTypecheckOptPass && ttv.state == TableState::Unsealed)
|
|
|
|
seenMutableType = true;
|
2021-11-05 15:47:21 +00:00
|
|
|
return false;
|
2022-04-15 00:57:43 +01:00
|
|
|
}
|
2021-11-05 15:47:21 +00:00
|
|
|
|
|
|
|
if (ttv.state == TableState::Free)
|
2022-04-15 00:57:43 +01:00
|
|
|
{
|
2021-11-05 15:47:21 +00:00
|
|
|
ttv.state = TableState::Generic;
|
2022-04-15 00:57:43 +01:00
|
|
|
|
|
|
|
if (FFlag::LuauTypecheckOptPass)
|
|
|
|
seenGenericType = true;
|
|
|
|
}
|
2021-11-05 15:47:21 +00:00
|
|
|
else if (ttv.state == TableState::Unsealed)
|
|
|
|
ttv.state = TableState::Sealed;
|
|
|
|
|
|
|
|
ttv.level = level;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(TypePackId tp, const FreeTypePack& ftp)
|
|
|
|
{
|
2022-04-15 00:57:43 +01:00
|
|
|
if (FFlag::LuauTypecheckOptPass)
|
|
|
|
seenMutableType = true;
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
if (!level.subsumes(ftp.level))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*asMutable(tp) = GenericTypePack{level};
|
|
|
|
genericPacks.push_back(tp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-18 01:18:01 +00:00
|
|
|
void quantify(TypeId ty, TypeLevel level)
|
2021-11-05 15:47:21 +00:00
|
|
|
{
|
2022-02-18 01:18:01 +00:00
|
|
|
Quantifier q{level};
|
2022-01-07 01:46:53 +00:00
|
|
|
DenseHashSet<void*> seen{nullptr};
|
|
|
|
visitTypeVarOnce(ty, q, seen);
|
2021-11-05 15:47:21 +00:00
|
|
|
|
|
|
|
FunctionTypeVar* ftv = getMutable<FunctionTypeVar>(ty);
|
|
|
|
LUAU_ASSERT(ftv);
|
|
|
|
ftv->generics = q.generics;
|
|
|
|
ftv->genericPacks = q.genericPacks;
|
2022-04-15 00:57:43 +01:00
|
|
|
|
|
|
|
if (FFlag::LuauTypecheckOptPass && ftv->generics.empty() && ftv->genericPacks.empty() && !q.seenMutableType && !q.seenGenericType)
|
|
|
|
ftv->hasNoGenerics = true;
|
2021-11-05 15:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Luau
|