2021-10-29 21:25:12 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2023-01-04 20:53:17 +00:00
|
|
|
#include "Luau/Type.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
|
|
|
#include "Luau/Common.h"
|
2022-11-10 22:53:13 +00:00
|
|
|
#include "Luau/ConstraintSolver.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/DenseHash.h"
|
|
|
|
#include "Luau/Error.h"
|
2022-01-21 17:00:19 +00:00
|
|
|
#include "Luau/RecursionCounter.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/StringUtils.h"
|
|
|
|
#include "Luau/ToString.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
#include "Luau/TypePack.h"
|
2023-01-04 20:53:17 +00:00
|
|
|
#include "Luau/VisitType.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <optional>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2022-01-27 23:46:05 +00:00
|
|
|
LUAU_FASTFLAG(DebugLuauFreezeArena)
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
LUAU_FASTINTVARIABLE(LuauTypeMaximumStringifierLength, 500)
|
|
|
|
LUAU_FASTINTVARIABLE(LuauTableTypeMaximumStringifierLength, 0)
|
2022-01-21 17:00:19 +00:00
|
|
|
LUAU_FASTINT(LuauTypeInferRecursionLimit)
|
2022-10-07 01:23:29 +01:00
|
|
|
LUAU_FASTFLAG(LuauInstantiateInSubtyping)
|
2023-03-17 19:20:37 +00:00
|
|
|
LUAU_FASTFLAG(LuauNormalizeBlockedTypes)
|
2023-04-28 20:55:13 +01:00
|
|
|
LUAU_FASTFLAG(DebugLuauReadWriteProperties)
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-06-17 02:05:14 +01:00
|
|
|
std::optional<WithPredicate<TypePackId>> magicFunctionFormat(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate);
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionFormat(MagicFunctionCallContext context);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-07-08 02:22:39 +01:00
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionGmatch(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate);
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionGmatch(MagicFunctionCallContext context);
|
2022-07-08 02:22:39 +01:00
|
|
|
|
2022-07-14 23:52:26 +01:00
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionMatch(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate);
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionMatch(MagicFunctionCallContext context);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionFind(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate);
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionFind(MagicFunctionCallContext context);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
2023-05-05 22:52:49 +01:00
|
|
|
// LUAU_NOINLINE prevents unwrapLazy from being inlined into advance below; advance is important to keep inlineable
|
|
|
|
static LUAU_NOINLINE TypeId unwrapLazy(LazyType* ltv)
|
|
|
|
{
|
|
|
|
TypeId unwrapped = ltv->unwrapped.load();
|
|
|
|
|
|
|
|
if (unwrapped)
|
|
|
|
return unwrapped;
|
|
|
|
|
|
|
|
ltv->unwrap(*ltv);
|
|
|
|
unwrapped = ltv->unwrapped.load();
|
|
|
|
|
|
|
|
if (!unwrapped)
|
|
|
|
throw InternalCompilerError("Lazy Type didn't fill in unwrapped type field");
|
|
|
|
|
|
|
|
if (get<LazyType>(unwrapped))
|
|
|
|
throw InternalCompilerError("Lazy Type cannot resolve to another Lazy Type");
|
|
|
|
|
|
|
|
return unwrapped;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
TypeId follow(TypeId t)
|
|
|
|
{
|
2023-05-05 22:52:49 +01:00
|
|
|
return follow(t, nullptr, [](const void*, TypeId t) -> TypeId {
|
2022-01-07 01:46:53 +00:00
|
|
|
return t;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:52:49 +01:00
|
|
|
TypeId follow(TypeId t, const void* context, TypeId (*mapper)(const void*, TypeId))
|
2022-01-07 01:46:53 +00:00
|
|
|
{
|
2023-05-05 22:52:49 +01:00
|
|
|
auto advance = [context, mapper](TypeId ty) -> std::optional<TypeId> {
|
2023-05-25 22:36:34 +01:00
|
|
|
TypeId mapped = mapper(context, ty);
|
2023-04-21 23:14:26 +01:00
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
if (auto btv = get<Unifiable::Bound<TypeId>>(mapped))
|
|
|
|
return btv->boundTo;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
if (auto ttv = get<TableType>(mapped))
|
|
|
|
return ttv->boundTo;
|
2023-05-05 22:52:49 +01:00
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
if (auto ltv = getMutable<LazyType>(mapped))
|
|
|
|
return unwrapLazy(ltv);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
return std::nullopt;
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TypeId cycleTester = t; // Null once we've determined that there is no cycle
|
|
|
|
if (auto a = advance(cycleTester))
|
|
|
|
cycleTester = *a;
|
|
|
|
else
|
|
|
|
return t;
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
if (!advance(cycleTester)) // Short circuit traversal for the rather common case when advance(advance(t)) == null
|
|
|
|
return cycleTester;
|
2023-05-05 22:52:49 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto a1 = advance(t);
|
|
|
|
if (a1)
|
|
|
|
t = *a1;
|
|
|
|
else
|
|
|
|
return t;
|
|
|
|
|
|
|
|
if (nullptr != cycleTester)
|
|
|
|
{
|
|
|
|
auto a2 = advance(cycleTester);
|
|
|
|
if (a2)
|
|
|
|
{
|
|
|
|
auto a3 = advance(*a2);
|
|
|
|
if (a3)
|
|
|
|
cycleTester = *a3;
|
|
|
|
else
|
|
|
|
cycleTester = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cycleTester = nullptr;
|
|
|
|
|
|
|
|
if (t == cycleTester)
|
2023-01-04 20:53:17 +00:00
|
|
|
throw InternalCompilerError("Luau::follow detected a Type cycle!!");
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<TypeId> flattenIntersection(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
if (!get<IntersectionType>(follow(ty)))
|
2021-10-29 21:25:12 +01:00
|
|
|
return {ty};
|
|
|
|
|
|
|
|
std::unordered_set<TypeId> seen;
|
|
|
|
std::deque<TypeId> queue{ty};
|
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
|
|
|
TypeId current = follow(queue.front());
|
|
|
|
queue.pop_front();
|
|
|
|
|
|
|
|
if (seen.find(current) != seen.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
seen.insert(current);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto itv = get<IntersectionType>(current))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
for (TypeId ty : itv->parts)
|
|
|
|
queue.push_back(ty);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result.push_back(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool isPrim(TypeId ty, PrimitiveType::Type primType)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
auto p = get<PrimitiveType>(follow(ty));
|
2021-10-29 21:25:12 +01:00
|
|
|
return p && p->type == primType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNil(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return isPrim(ty, PrimitiveType::NilType);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isBoolean(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
if (isPrim(ty, PrimitiveType::Boolean) || get<BooleanSingleton>(get<SingletonType>(follow(ty))))
|
2022-02-24 23:53:37 +00:00
|
|
|
return true;
|
2022-01-27 23:46:05 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto utv = get<UnionType>(follow(ty)))
|
2022-02-24 23:53:37 +00:00
|
|
|
return std::all_of(begin(utv), end(utv), isBoolean);
|
2022-01-27 23:46:05 +00:00
|
|
|
|
2022-02-24 23:53:37 +00:00
|
|
|
return false;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNumber(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return isPrim(ty, PrimitiveType::Number);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:36:33 +00:00
|
|
|
// Returns true when ty is a subtype of string
|
2021-10-29 21:25:12 +01:00
|
|
|
bool isString(TypeId ty)
|
|
|
|
{
|
2022-07-14 23:52:26 +01:00
|
|
|
ty = follow(ty);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (isPrim(ty, PrimitiveType::String) || get<StringSingleton>(get<SingletonType>(ty)))
|
2022-02-24 23:53:37 +00:00
|
|
|
return true;
|
2022-01-27 23:46:05 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto utv = get<UnionType>(ty))
|
2022-02-24 23:53:37 +00:00
|
|
|
return std::all_of(begin(utv), end(utv), isString);
|
2022-01-27 23:46:05 +00:00
|
|
|
|
2022-02-24 23:53:37 +00:00
|
|
|
return false;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:36:33 +00:00
|
|
|
// Returns true when ty is a supertype of string
|
|
|
|
bool maybeString(TypeId ty)
|
|
|
|
{
|
2022-06-24 02:56:00 +01:00
|
|
|
ty = follow(ty);
|
2022-04-15 00:57:43 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (isPrim(ty, PrimitiveType::String) || get<AnyType>(ty))
|
2022-07-14 23:52:26 +01:00
|
|
|
return true;
|
2022-03-04 16:36:33 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto utv = get<UnionType>(ty))
|
2022-06-24 02:56:00 +01:00
|
|
|
return std::any_of(begin(utv), end(utv), maybeString);
|
2022-03-04 16:36:33 +00:00
|
|
|
|
2022-06-24 02:56:00 +01:00
|
|
|
return false;
|
2022-03-04 16:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
bool isThread(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return isPrim(ty, PrimitiveType::Thread);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isOptional(TypeId ty)
|
|
|
|
{
|
|
|
|
if (isNil(ty))
|
|
|
|
return true;
|
|
|
|
|
2022-03-18 00:46:04 +00:00
|
|
|
ty = follow(ty);
|
|
|
|
|
2023-01-27 22:28:31 +00:00
|
|
|
if (get<AnyType>(ty) || get<UnknownType>(ty))
|
2022-03-18 00:46:04 +00:00
|
|
|
return true;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
auto utv = get<UnionType>(ty);
|
2022-02-24 23:53:37 +00:00
|
|
|
if (!utv)
|
2022-01-27 23:46:05 +00:00
|
|
|
return false;
|
2022-02-24 23:53:37 +00:00
|
|
|
|
2022-05-20 01:02:24 +01:00
|
|
|
return std::any_of(begin(utv), end(utv), isOptional);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isTableIntersection(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
if (!get<IntersectionType>(follow(ty)))
|
2021-10-29 21:25:12 +01:00
|
|
|
return false;
|
2021-11-05 02:34:35 +00:00
|
|
|
|
|
|
|
std::vector<TypeId> parts = flattenIntersection(ty);
|
|
|
|
return std::all_of(parts.begin(), parts.end(), getTableType);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isOverloadedFunction(TypeId ty)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
if (!get<IntersectionType>(follow(ty)))
|
2021-10-29 21:25:12 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto isFunction = [](TypeId part) -> bool {
|
2023-01-04 20:53:17 +00:00
|
|
|
return get<FunctionType>(part);
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<TypeId> parts = flattenIntersection(ty);
|
|
|
|
return std::all_of(parts.begin(), parts.end(), isFunction);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::optional<TypeId> getMetatable(TypeId type, NotNull<BuiltinTypes> builtinTypes)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-07-14 23:52:26 +01:00
|
|
|
type = follow(type);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (const MetatableType* mtType = get<MetatableType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
return mtType->metatable;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (const ClassType* classType = get<ClassType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
return classType->metatable;
|
2022-02-24 23:53:37 +00:00
|
|
|
else if (isString(type))
|
2022-01-27 23:46:05 +00:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
auto ptv = get<PrimitiveType>(builtinTypes->stringType);
|
2022-02-24 23:53:37 +00:00
|
|
|
LUAU_ASSERT(ptv && ptv->metatable);
|
|
|
|
return ptv->metatable;
|
2022-01-27 23:46:05 +00:00
|
|
|
}
|
2022-02-24 23:53:37 +00:00
|
|
|
|
|
|
|
return std::nullopt;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TableType* getTableType(TypeId type)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-24 23:53:37 +00:00
|
|
|
type = follow(type);
|
2022-02-04 16:45:57 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (const TableType* ttv = get<TableType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
return ttv;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (const MetatableType* mtv = get<MetatableType>(type))
|
|
|
|
return get<TableType>(follow(mtv->table));
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TableType* getMutableTableType(TypeId type)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return const_cast<TableType*>(getTableType(type));
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string* getName(TypeId type)
|
|
|
|
{
|
|
|
|
type = follow(type);
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto mtv = get<MetatableType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
if (mtv->syntheticName)
|
|
|
|
return &*mtv->syntheticName;
|
2022-02-24 23:53:37 +00:00
|
|
|
type = follow(mtv->table);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ttv = get<TableType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
if (ttv->name)
|
|
|
|
return &*ttv->name;
|
|
|
|
if (ttv->syntheticName)
|
|
|
|
return &*ttv->syntheticName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-03-24 22:04:14 +00:00
|
|
|
std::optional<ModuleName> getDefinitionModuleName(TypeId type)
|
|
|
|
{
|
|
|
|
type = follow(type);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ttv = get<TableType>(type))
|
2022-03-24 22:04:14 +00:00
|
|
|
{
|
|
|
|
if (!ttv->definitionModuleName.empty())
|
|
|
|
return ttv->definitionModuleName;
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ftv = get<FunctionType>(type))
|
2022-03-24 22:04:14 +00:00
|
|
|
{
|
|
|
|
if (ftv->definition)
|
|
|
|
return ftv->definition->definitionModuleName;
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ctv = get<ClassType>(type))
|
2022-04-21 22:44:27 +01:00
|
|
|
{
|
|
|
|
if (!ctv->definitionModuleName.empty())
|
|
|
|
return ctv->definitionModuleName;
|
|
|
|
}
|
2022-03-24 22:04:14 +00:00
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool isSubset(const UnionType& super, const UnionType& sub)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
std::unordered_set<TypeId> superTypes;
|
|
|
|
|
|
|
|
for (TypeId id : super.options)
|
|
|
|
superTypes.insert(id);
|
|
|
|
|
|
|
|
for (TypeId id : sub.options)
|
|
|
|
{
|
|
|
|
if (superTypes.find(id) == superTypes.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-14 19:06:22 +01:00
|
|
|
bool hasPrimitiveTypeInIntersection(TypeId ty, PrimitiveType::Type primTy)
|
|
|
|
{
|
|
|
|
TypeId tf = follow(ty);
|
|
|
|
if (isPrim(tf, primTy))
|
|
|
|
return true;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-04-14 19:06:22 +01:00
|
|
|
for (auto t : flattenIntersection(tf))
|
|
|
|
return isPrim(follow(t), primTy);
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
// When typechecking an assignment `x = e`, we typecheck `x:T` and `e:U`,
|
|
|
|
// then instantiate U if `isGeneric(U)` is true, and `maybeGeneric(T)` is false.
|
|
|
|
bool isGeneric(TypeId ty)
|
|
|
|
{
|
2022-10-07 01:23:29 +01:00
|
|
|
LUAU_ASSERT(!FFlag::LuauInstantiateInSubtyping);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
ty = follow(ty);
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ftv = get<FunctionType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return ftv->generics.size() > 0 || ftv->genericPacks.size() > 0;
|
|
|
|
else
|
|
|
|
// TODO: recurse on type synonyms CLI-39914
|
|
|
|
// TODO: recurse on table types CLI-39914
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool maybeGeneric(TypeId ty)
|
|
|
|
{
|
2022-10-07 01:23:29 +01:00
|
|
|
LUAU_ASSERT(!FFlag::LuauInstantiateInSubtyping);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
ty = follow(ty);
|
2023-02-17 23:41:51 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (get<FreeType>(ty))
|
2021-11-12 14:27:34 +00:00
|
|
|
return true;
|
2023-02-17 23:41:51 +00:00
|
|
|
|
|
|
|
if (auto ttv = get<TableType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
// TODO: recurse on table types CLI-39914
|
|
|
|
(void)ttv;
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-17 23:41:51 +00:00
|
|
|
|
|
|
|
if (auto itv = get<IntersectionType>(ty))
|
|
|
|
{
|
|
|
|
return std::any_of(begin(itv), end(itv), maybeGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
return isGeneric(ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2021-11-19 16:10:07 +00:00
|
|
|
bool maybeSingleton(TypeId ty)
|
|
|
|
{
|
|
|
|
ty = follow(ty);
|
2023-01-04 20:53:17 +00:00
|
|
|
if (get<SingletonType>(ty))
|
2021-11-19 16:10:07 +00:00
|
|
|
return true;
|
2023-01-04 20:53:17 +00:00
|
|
|
if (const UnionType* utv = get<UnionType>(ty))
|
2021-11-19 16:10:07 +00:00
|
|
|
for (TypeId option : utv)
|
2023-01-04 20:53:17 +00:00
|
|
|
if (get<SingletonType>(follow(option)))
|
2021-11-19 16:10:07 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2022-01-21 17:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasLength(TypeId ty, DenseHashSet<TypeId>& seen, int* recursionCount)
|
|
|
|
{
|
2022-06-24 02:56:00 +01:00
|
|
|
RecursionLimiter _rl(recursionCount, FInt::LuauTypeInferRecursionLimit);
|
2022-01-21 17:00:19 +00:00
|
|
|
|
|
|
|
ty = follow(ty);
|
|
|
|
|
|
|
|
if (seen.contains(ty))
|
|
|
|
return true;
|
|
|
|
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
if (isString(ty) || isPrim(ty, PrimitiveType::Table) || get<AnyType>(ty) || get<TableType>(ty) || get<MetatableType>(ty))
|
2022-01-21 17:00:19 +00:00
|
|
|
return true;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto uty = get<UnionType>(ty))
|
2022-01-21 17:00:19 +00:00
|
|
|
{
|
|
|
|
seen.insert(ty);
|
|
|
|
|
|
|
|
for (TypeId part : uty->options)
|
|
|
|
{
|
|
|
|
if (!hasLength(part, seen, recursionCount))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ity = get<IntersectionType>(ty))
|
2022-01-21 17:00:19 +00:00
|
|
|
{
|
|
|
|
seen.insert(ty);
|
|
|
|
|
|
|
|
for (TypeId part : ity->parts)
|
|
|
|
{
|
|
|
|
if (hasLength(part, seen, recursionCount))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-11-19 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
FreeType::FreeType(TypeLevel level)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, level(level)
|
|
|
|
, scope(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeType::FreeType(Scope* scope)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, level{}
|
|
|
|
, scope(scope)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeType::FreeType(Scope* scope, TypeLevel level)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, level(level)
|
|
|
|
, scope(scope)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
FreeType::FreeType(Scope* scope, TypeId lowerBound, TypeId upperBound)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, scope(scope)
|
|
|
|
, lowerBound(lowerBound)
|
|
|
|
, upperBound(upperBound)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
GenericType::GenericType()
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, name("g" + std::to_string(index))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericType::GenericType(TypeLevel level)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, level(level)
|
|
|
|
, name("g" + std::to_string(index))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericType::GenericType(const Name& name)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, name(name)
|
|
|
|
, explicitName(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericType::GenericType(Scope* scope)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, scope(scope)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericType::GenericType(TypeLevel level, const Name& name)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, level(level)
|
|
|
|
, name(name)
|
|
|
|
, explicitName(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericType::GenericType(Scope* scope, const Name& name)
|
|
|
|
: index(Unifiable::freshIndex())
|
|
|
|
, scope(scope)
|
|
|
|
, name(name)
|
|
|
|
, explicitName(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
BlockedType::BlockedType()
|
2023-03-24 18:03:04 +00:00
|
|
|
: index(FFlag::LuauNormalizeBlockedTypes ? Unifiable::freshIndex() : ++DEPRECATED_nextIndex)
|
2022-06-17 02:05:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-17 19:20:37 +00:00
|
|
|
int BlockedType::DEPRECATED_nextIndex = 0;
|
2022-06-17 02:05:14 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
PendingExpansionType::PendingExpansionType(
|
2022-09-02 00:14:03 +01:00
|
|
|
std::optional<AstName> prefix, AstName name, std::vector<TypeId> typeArguments, std::vector<TypePackId> packArguments)
|
|
|
|
: prefix(prefix)
|
|
|
|
, name(name)
|
2022-08-04 23:35:33 +01:00
|
|
|
, typeArguments(typeArguments)
|
|
|
|
, packArguments(packArguments)
|
|
|
|
, index(++nextIndex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
size_t PendingExpansionType::nextIndex = 0;
|
2022-08-04 23:35:33 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(TypePackId argTypes, TypePackId retTypes, std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
|
|
|
, argTypes(argTypes)
|
2022-06-17 02:05:14 +01:00
|
|
|
, retTypes(retTypes)
|
2021-10-29 21:25:12 +01:00
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(TypeLevel level, TypePackId argTypes, TypePackId retTypes, std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
|
|
|
, level(level)
|
2021-10-29 21:25:12 +01:00
|
|
|
, argTypes(argTypes)
|
2022-06-17 02:05:14 +01:00
|
|
|
, retTypes(retTypes)
|
2021-10-29 21:25:12 +01:00
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(
|
2022-09-29 23:23:10 +01:00
|
|
|
TypeLevel level, Scope* scope, TypePackId argTypes, TypePackId retTypes, std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
|
|
|
, level(level)
|
2022-09-29 23:23:10 +01:00
|
|
|
, scope(scope)
|
|
|
|
, argTypes(argTypes)
|
|
|
|
, retTypes(retTypes)
|
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(std::vector<TypeId> generics, std::vector<TypePackId> genericPacks, TypePackId argTypes, TypePackId retTypes,
|
2021-10-29 21:25:12 +01:00
|
|
|
std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
|
|
|
, generics(generics)
|
2021-10-29 21:25:12 +01:00
|
|
|
, genericPacks(genericPacks)
|
|
|
|
, argTypes(argTypes)
|
2022-06-17 02:05:14 +01:00
|
|
|
, retTypes(retTypes)
|
2021-10-29 21:25:12 +01:00
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(TypeLevel level, std::vector<TypeId> generics, std::vector<TypePackId> genericPacks, TypePackId argTypes,
|
2022-06-17 02:05:14 +01:00
|
|
|
TypePackId retTypes, std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
2021-10-29 21:25:12 +01:00
|
|
|
, generics(generics)
|
|
|
|
, genericPacks(genericPacks)
|
2022-12-02 18:09:59 +00:00
|
|
|
, level(level)
|
2021-10-29 21:25:12 +01:00
|
|
|
, argTypes(argTypes)
|
2022-06-17 02:05:14 +01:00
|
|
|
, retTypes(retTypes)
|
2021-10-29 21:25:12 +01:00
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType::FunctionType(TypeLevel level, Scope* scope, std::vector<TypeId> generics, std::vector<TypePackId> genericPacks, TypePackId argTypes,
|
|
|
|
TypePackId retTypes, std::optional<FunctionDefinition> defn, bool hasSelf)
|
2022-12-02 18:09:59 +00:00
|
|
|
: definition(std::move(defn))
|
2022-09-29 23:23:10 +01:00
|
|
|
, generics(generics)
|
|
|
|
, genericPacks(genericPacks)
|
2022-12-02 18:09:59 +00:00
|
|
|
, level(level)
|
|
|
|
, scope(scope)
|
2022-09-29 23:23:10 +01:00
|
|
|
, argTypes(argTypes)
|
|
|
|
, retTypes(retTypes)
|
|
|
|
, hasSelf(hasSelf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:55:13 +01:00
|
|
|
Property::Property() {}
|
|
|
|
|
|
|
|
Property::Property(TypeId readTy, bool deprecated, const std::string& deprecatedSuggestion, std::optional<Location> location, const Tags& tags,
|
|
|
|
const std::optional<std::string>& documentationSymbol)
|
|
|
|
: deprecated(deprecated)
|
|
|
|
, deprecatedSuggestion(deprecatedSuggestion)
|
|
|
|
, location(location)
|
|
|
|
, tags(tags)
|
|
|
|
, documentationSymbol(documentationSymbol)
|
|
|
|
, readTy(readTy)
|
|
|
|
, writeTy(readTy)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!FFlag::DebugLuauReadWriteProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
Property Property::readonly(TypeId ty)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(FFlag::DebugLuauReadWriteProperties);
|
|
|
|
|
|
|
|
Property p;
|
|
|
|
p.readTy = ty;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Property Property::writeonly(TypeId ty)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(FFlag::DebugLuauReadWriteProperties);
|
|
|
|
|
|
|
|
Property p;
|
|
|
|
p.writeTy = ty;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
Property Property::rw(TypeId ty)
|
|
|
|
{
|
|
|
|
return Property::rw(ty, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Property Property::rw(TypeId read, TypeId write)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(FFlag::DebugLuauReadWriteProperties);
|
|
|
|
|
|
|
|
Property p;
|
|
|
|
p.readTy = read;
|
|
|
|
p.writeTy = write;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
Property Property::create(std::optional<TypeId> read, std::optional<TypeId> write)
|
2023-04-28 20:55:13 +01:00
|
|
|
{
|
|
|
|
if (read && !write)
|
|
|
|
return Property::readonly(*read);
|
|
|
|
else if (!read && write)
|
|
|
|
return Property::writeonly(*write);
|
|
|
|
else
|
2023-05-25 22:36:34 +01:00
|
|
|
{
|
|
|
|
LUAU_ASSERT(read && write);
|
|
|
|
return Property::rw(*read, *write);
|
|
|
|
}
|
2023-04-28 20:55:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TypeId Property::type() const
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!FFlag::DebugLuauReadWriteProperties);
|
|
|
|
LUAU_ASSERT(readTy);
|
|
|
|
return *readTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Property::setType(TypeId ty)
|
|
|
|
{
|
2023-05-25 22:36:34 +01:00
|
|
|
LUAU_ASSERT(!FFlag::DebugLuauReadWriteProperties);
|
2023-04-28 20:55:13 +01:00
|
|
|
readTy = ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Property::readType() const
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(FFlag::DebugLuauReadWriteProperties);
|
|
|
|
LUAU_ASSERT(!(bool(readTy) && bool(writeTy)));
|
|
|
|
return readTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Property::writeType() const
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(FFlag::DebugLuauReadWriteProperties);
|
|
|
|
LUAU_ASSERT(!(bool(readTy) && bool(writeTy)));
|
|
|
|
return writeTy;
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:36:34 +01:00
|
|
|
bool Property::isShared() const
|
|
|
|
{
|
|
|
|
return readTy && writeTy && readTy == writeTy;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TableType::TableType(TableState state, TypeLevel level, Scope* scope)
|
2021-10-29 21:25:12 +01:00
|
|
|
: state(state)
|
|
|
|
, level(level)
|
2022-09-29 23:23:10 +01:00
|
|
|
, scope(scope)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TableType::TableType(const Props& props, const std::optional<TableIndexer>& indexer, TypeLevel level, TableState state)
|
2021-10-29 21:25:12 +01:00
|
|
|
: props(props)
|
|
|
|
, indexer(indexer)
|
|
|
|
, state(state)
|
|
|
|
, level(level)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TableType::TableType(const Props& props, const std::optional<TableIndexer>& indexer, TypeLevel level, Scope* scope, TableState state)
|
2022-09-29 23:23:10 +01:00
|
|
|
: props(props)
|
|
|
|
, indexer(indexer)
|
|
|
|
, state(state)
|
|
|
|
, level(level)
|
|
|
|
, scope(scope)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
// Test Types for equivalence
|
|
|
|
// More complex than we'd like because Types can self-reference.
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
bool areSeen(SeenSet& seen, const void* lhs, const void* rhs)
|
|
|
|
{
|
|
|
|
if (lhs == rhs)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto p = std::make_pair(const_cast<void*>(lhs), const_cast<void*>(rhs));
|
|
|
|
if (seen.find(p) != seen.end())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
seen.insert(p);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool areEqual(SeenSet& seen, const FunctionType& lhs, const FunctionType& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
if (areSeen(seen, &lhs, &rhs))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// TODO: check generics CLI-39915
|
|
|
|
|
|
|
|
if (!areEqual(seen, *lhs.argTypes, *rhs.argTypes))
|
|
|
|
return false;
|
|
|
|
|
2022-06-17 02:05:14 +01:00
|
|
|
if (!areEqual(seen, *lhs.retTypes, *rhs.retTypes))
|
2021-10-29 21:25:12 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool areEqual(SeenSet& seen, const TableType& lhs, const TableType& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
if (areSeen(seen, &lhs, &rhs))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (lhs.state != rhs.state)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lhs.props.size() != rhs.props.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bool(lhs.indexer) != bool(rhs.indexer))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lhs.indexer && rhs.indexer)
|
|
|
|
{
|
|
|
|
if (!areEqual(seen, *lhs.indexer->indexType, *rhs.indexer->indexType))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!areEqual(seen, *lhs.indexer->indexResultType, *rhs.indexer->indexResultType))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto l = lhs.props.begin();
|
|
|
|
auto r = rhs.props.begin();
|
|
|
|
|
|
|
|
while (l != lhs.props.end())
|
|
|
|
{
|
|
|
|
if (l->first != r->first)
|
|
|
|
return false;
|
|
|
|
|
2023-04-28 20:55:13 +01:00
|
|
|
if (!areEqual(seen, *l->second.type(), *r->second.type()))
|
2021-10-29 21:25:12 +01:00
|
|
|
return false;
|
|
|
|
++l;
|
|
|
|
++r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
static bool areEqual(SeenSet& seen, const MetatableType& lhs, const MetatableType& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-18 01:18:01 +00:00
|
|
|
if (areSeen(seen, &lhs, &rhs))
|
2022-01-14 16:20:09 +00:00
|
|
|
return true;
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
return areEqual(seen, *lhs.table, *rhs.table) && areEqual(seen, *lhs.metatable, *rhs.metatable);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool areEqual(SeenSet& seen, const Type& lhs, const Type& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto bound = get_if<BoundType>(&lhs.ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return areEqual(seen, *bound->boundTo, rhs);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto bound = get_if<BoundType>(&rhs.ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return areEqual(seen, lhs, *bound->boundTo);
|
|
|
|
|
|
|
|
if (lhs.ty.index() != rhs.ty.index())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const FreeType* lf = get_if<FreeType>(&lhs.ty);
|
|
|
|
const FreeType* rf = get_if<FreeType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lf && rf)
|
|
|
|
return lf->index == rf->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const GenericType* lg = get_if<GenericType>(&lhs.ty);
|
|
|
|
const GenericType* rg = get_if<GenericType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lg && rg)
|
|
|
|
return lg->index == rg->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const PrimitiveType* lp = get_if<PrimitiveType>(&lhs.ty);
|
|
|
|
const PrimitiveType* rp = get_if<PrimitiveType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lp && rp)
|
|
|
|
return lp->type == rp->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const GenericType* lg = get_if<GenericType>(&lhs.ty);
|
|
|
|
const GenericType* rg = get_if<GenericType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lg && rg)
|
|
|
|
return lg->index == rg->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const ErrorType* le = get_if<ErrorType>(&lhs.ty);
|
|
|
|
const ErrorType* re = get_if<ErrorType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (le && re)
|
|
|
|
return le->index == re->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const FunctionType* lf = get_if<FunctionType>(&lhs.ty);
|
|
|
|
const FunctionType* rf = get_if<FunctionType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lf && rf)
|
|
|
|
return areEqual(seen, *lf, *rf);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const TableType* lt = get_if<TableType>(&lhs.ty);
|
|
|
|
const TableType* rt = get_if<TableType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (lt && rt)
|
|
|
|
return areEqual(seen, *lt, *rt);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const MetatableType* lmt = get_if<MetatableType>(&lhs.ty);
|
|
|
|
const MetatableType* rmt = get_if<MetatableType>(&rhs.ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
if (lmt && rmt)
|
|
|
|
return areEqual(seen, *lmt, *rmt);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (get_if<AnyType>(&lhs.ty) && get_if<AnyType>(&rhs.ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
Type* asMutable(TypeId ty)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return const_cast<Type*>(ty);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool Type::operator==(const Type& rhs) const
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
SeenSet seen;
|
|
|
|
return areEqual(seen, *this, rhs);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool Type::operator!=(const Type& rhs) const
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
SeenSet seen;
|
|
|
|
return !areEqual(seen, *this, rhs);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
Type& Type::operator=(const TypeVariant& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
ty = rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
Type& Type::operator=(TypeVariant&& rhs)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
ty = std::move(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
Type& Type::operator=(const Type& rhs)
|
2022-06-10 17:58:21 +01:00
|
|
|
{
|
2022-07-08 02:22:39 +01:00
|
|
|
LUAU_ASSERT(owningArena == rhs.owningArena);
|
|
|
|
LUAU_ASSERT(!rhs.persistent);
|
2022-06-10 17:58:21 +01:00
|
|
|
|
2022-07-08 02:22:39 +01:00
|
|
|
reassign(rhs);
|
2022-06-10 17:58:21 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
TypeId makeFunction(TypeArena& arena, std::optional<TypeId> selfType, std::initializer_list<TypeId> generics,
|
|
|
|
std::initializer_list<TypePackId> genericPacks, std::initializer_list<TypeId> paramTypes, std::initializer_list<std::string> paramNames,
|
|
|
|
std::initializer_list<TypeId> retTypes);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
BuiltinTypes::BuiltinTypes()
|
2022-09-08 23:14:25 +01:00
|
|
|
: arena(new TypeArena)
|
|
|
|
, debugFreezeArena(FFlag::DebugLuauFreezeArena)
|
2023-01-04 20:53:17 +00:00
|
|
|
, nilType(arena->addType(Type{PrimitiveType{PrimitiveType::NilType}, /*persistent*/ true}))
|
|
|
|
, numberType(arena->addType(Type{PrimitiveType{PrimitiveType::Number}, /*persistent*/ true}))
|
|
|
|
, stringType(arena->addType(Type{PrimitiveType{PrimitiveType::String}, /*persistent*/ true}))
|
|
|
|
, booleanType(arena->addType(Type{PrimitiveType{PrimitiveType::Boolean}, /*persistent*/ true}))
|
|
|
|
, threadType(arena->addType(Type{PrimitiveType{PrimitiveType::Thread}, /*persistent*/ true}))
|
|
|
|
, functionType(arena->addType(Type{PrimitiveType{PrimitiveType::Function}, /*persistent*/ true}))
|
|
|
|
, classType(arena->addType(Type{ClassType{"class", {}, std::nullopt, std::nullopt, {}, {}, {}}, /*persistent*/ true}))
|
2023-01-27 22:28:31 +00:00
|
|
|
, tableType(arena->addType(Type{PrimitiveType{PrimitiveType::Table}, /*persistent*/ true}))
|
2023-03-03 20:21:14 +00:00
|
|
|
, emptyTableType(arena->addType(Type{TableType{TableState::Sealed, TypeLevel{}, nullptr}, /*persistent*/ true}))
|
2023-01-04 20:53:17 +00:00
|
|
|
, trueType(arena->addType(Type{SingletonType{BooleanSingleton{true}}, /*persistent*/ true}))
|
|
|
|
, falseType(arena->addType(Type{SingletonType{BooleanSingleton{false}}, /*persistent*/ true}))
|
|
|
|
, anyType(arena->addType(Type{AnyType{}, /*persistent*/ true}))
|
|
|
|
, unknownType(arena->addType(Type{UnknownType{}, /*persistent*/ true}))
|
|
|
|
, neverType(arena->addType(Type{NeverType{}, /*persistent*/ true}))
|
|
|
|
, errorType(arena->addType(Type{ErrorType{}, /*persistent*/ true}))
|
|
|
|
, falsyType(arena->addType(Type{UnionType{{falseType, nilType}}, /*persistent*/ true}))
|
|
|
|
, truthyType(arena->addType(Type{NegationType{falsyType}, /*persistent*/ true}))
|
2023-01-20 20:27:03 +00:00
|
|
|
, optionalNumberType(arena->addType(Type{UnionType{{numberType, nilType}}, /*persistent*/ true}))
|
|
|
|
, optionalStringType(arena->addType(Type{UnionType{{stringType, nilType}}, /*persistent*/ true}))
|
2022-09-08 23:14:25 +01:00
|
|
|
, anyTypePack(arena->addTypePack(TypePackVar{VariadicTypePack{anyType}, /*persistent*/ true}))
|
|
|
|
, neverTypePack(arena->addTypePack(TypePackVar{VariadicTypePack{neverType}, /*persistent*/ true}))
|
2023-01-20 20:27:03 +00:00
|
|
|
, uninhabitableTypePack(arena->addTypePack(TypePackVar{TypePack{{neverType}, neverTypePack}, /*persistent*/ true}))
|
2022-09-08 23:14:25 +01:00
|
|
|
, errorTypePack(arena->addTypePack(TypePackVar{Unifiable::Error{}, /*persistent*/ true}))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
TypeId stringMetatable = makeStringMetatable();
|
2023-01-04 20:53:17 +00:00
|
|
|
asMutable(stringType)->ty = PrimitiveType{PrimitiveType::String, stringMetatable};
|
2021-10-29 21:25:12 +01:00
|
|
|
persist(stringMetatable);
|
2021-12-10 22:05:05 +00:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
freeze(*arena);
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
BuiltinTypes::~BuiltinTypes()
|
2021-12-10 22:05:05 +00:00
|
|
|
{
|
|
|
|
// Destroy the arena with the same memory management flags it was created with
|
|
|
|
bool prevFlag = FFlag::DebugLuauFreezeArena;
|
|
|
|
FFlag::DebugLuauFreezeArena.value = debugFreezeArena;
|
|
|
|
|
|
|
|
unfreeze(*arena);
|
|
|
|
arena.reset(nullptr);
|
|
|
|
|
|
|
|
FFlag::DebugLuauFreezeArena.value = prevFlag;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TypeId BuiltinTypes::makeStringMetatable()
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId optionalNumber = arena->addType(UnionType{{nilType, numberType}});
|
|
|
|
const TypeId optionalString = arena->addType(UnionType{{nilType, stringType}});
|
|
|
|
const TypeId optionalBoolean = arena->addType(UnionType{{nilType, booleanType}});
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
const TypePackId oneStringPack = arena->addTypePack({stringType});
|
|
|
|
const TypePackId anyTypePack = arena->addTypePack(TypePackVar{VariadicTypePack{anyType}, true});
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType formatFTV{arena->addTypePack(TypePack{{stringType}, anyTypePack}), oneStringPack};
|
2021-10-29 21:25:12 +01:00
|
|
|
formatFTV.magicFunction = &magicFunctionFormat;
|
|
|
|
const TypeId formatFn = arena->addType(formatFTV);
|
2022-11-10 22:53:13 +00:00
|
|
|
attachDcrMagicFunction(formatFn, dcrMagicFunctionFormat);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
const TypePackId emptyPack = arena->addTypePack({});
|
|
|
|
const TypePackId stringVariadicList = arena->addTypePack(TypePackVar{VariadicTypePack{stringType}});
|
|
|
|
const TypePackId numberVariadicList = arena->addTypePack(TypePackVar{VariadicTypePack{numberType}});
|
|
|
|
|
|
|
|
const TypeId stringToStringType = makeFunction(*arena, std::nullopt, {}, {}, {stringType}, {}, {stringType});
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId replArgType =
|
|
|
|
arena->addType(UnionType{{stringType, arena->addType(TableType({}, TableIndexer(stringType, stringType), TypeLevel{}, TableState::Generic)),
|
2021-10-29 21:25:12 +01:00
|
|
|
makeFunction(*arena, std::nullopt, {}, {}, {stringType}, {}, {stringType})}});
|
|
|
|
const TypeId gsubFunc = makeFunction(*arena, stringType, {}, {}, {stringType, replArgType, optionalNumber}, {}, {stringType, numberType});
|
|
|
|
const TypeId gmatchFunc =
|
2023-01-04 20:53:17 +00:00
|
|
|
makeFunction(*arena, stringType, {}, {}, {stringType}, {}, {arena->addType(FunctionType{emptyPack, stringVariadicList})});
|
2022-07-08 02:22:39 +01:00
|
|
|
attachMagicFunction(gmatchFunc, magicFunctionGmatch);
|
2022-11-10 22:53:13 +00:00
|
|
|
attachDcrMagicFunction(gmatchFunc, dcrMagicFunctionGmatch);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-09-02 00:14:03 +01:00
|
|
|
const TypeId matchFunc = arena->addType(
|
2023-01-04 20:53:17 +00:00
|
|
|
FunctionType{arena->addTypePack({stringType, stringType, optionalNumber}), arena->addTypePack(TypePackVar{VariadicTypePack{stringType}})});
|
2022-07-14 23:52:26 +01:00
|
|
|
attachMagicFunction(matchFunc, magicFunctionMatch);
|
2022-11-10 22:53:13 +00:00
|
|
|
attachDcrMagicFunction(matchFunc, dcrMagicFunctionMatch);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId findFunc = arena->addType(FunctionType{arena->addTypePack({stringType, stringType, optionalNumber, optionalBoolean}),
|
2022-07-14 23:52:26 +01:00
|
|
|
arena->addTypePack(TypePack{{optionalNumber, optionalNumber}, stringVariadicList})});
|
|
|
|
attachMagicFunction(findFunc, magicFunctionFind);
|
2022-11-10 22:53:13 +00:00
|
|
|
attachDcrMagicFunction(findFunc, dcrMagicFunctionFind);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TableType::Props stringLib = {
|
|
|
|
{"byte", {arena->addType(FunctionType{arena->addTypePack({stringType, optionalNumber, optionalNumber}), numberVariadicList})}},
|
|
|
|
{"char", {arena->addType(FunctionType{numberVariadicList, arena->addTypePack({stringType})})}},
|
2022-07-14 23:52:26 +01:00
|
|
|
{"find", {findFunc}},
|
2021-10-29 21:25:12 +01:00
|
|
|
{"format", {formatFn}}, // FIXME
|
|
|
|
{"gmatch", {gmatchFunc}},
|
|
|
|
{"gsub", {gsubFunc}},
|
|
|
|
{"len", {makeFunction(*arena, stringType, {}, {}, {}, {}, {numberType})}},
|
|
|
|
{"lower", {stringToStringType}},
|
2022-07-14 23:52:26 +01:00
|
|
|
{"match", {matchFunc}},
|
2021-10-29 21:25:12 +01:00
|
|
|
{"rep", {makeFunction(*arena, stringType, {}, {}, {numberType}, {}, {stringType})}},
|
|
|
|
{"reverse", {stringToStringType}},
|
|
|
|
{"sub", {makeFunction(*arena, stringType, {}, {}, {numberType, optionalNumber}, {}, {stringType})}},
|
|
|
|
{"upper", {stringToStringType}},
|
2022-01-27 23:46:05 +00:00
|
|
|
{"split", {makeFunction(*arena, stringType, {}, {}, {optionalString}, {},
|
2023-01-04 20:53:17 +00:00
|
|
|
{arena->addType(TableType{{}, TableIndexer{numberType, stringType}, TypeLevel{}, TableState::Sealed})})}},
|
|
|
|
{"pack", {arena->addType(FunctionType{
|
2021-10-29 21:25:12 +01:00
|
|
|
arena->addTypePack(TypePack{{stringType}, anyTypePack}),
|
|
|
|
oneStringPack,
|
|
|
|
})}},
|
|
|
|
{"packsize", {makeFunction(*arena, stringType, {}, {}, {}, {}, {numberType})}},
|
2023-01-04 20:53:17 +00:00
|
|
|
{"unpack", {arena->addType(FunctionType{
|
2021-10-29 21:25:12 +01:00
|
|
|
arena->addTypePack(TypePack{{stringType, stringType, optionalNumber}}),
|
|
|
|
anyTypePack,
|
|
|
|
})}},
|
|
|
|
};
|
|
|
|
|
|
|
|
assignPropDocumentationSymbols(stringLib, "@luau/global/string");
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TypeId tableType = arena->addType(TableType{std::move(stringLib), std::nullopt, TypeLevel{}, TableState::Sealed});
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (TableType* ttv = getMutable<TableType>(tableType))
|
2023-01-06 21:14:35 +00:00
|
|
|
ttv->name = "typeof(string)";
|
2021-12-10 22:05:05 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
return arena->addType(TableType{{{{"__index", {tableType}}}}, std::nullopt, TypeLevel{}, TableState::Sealed});
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
TypeId BuiltinTypes::errorRecoveryType() const
|
2021-11-19 16:10:07 +00:00
|
|
|
{
|
2022-09-08 23:14:25 +01:00
|
|
|
return errorType;
|
2021-11-19 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
TypePackId BuiltinTypes::errorRecoveryTypePack() const
|
2021-11-19 16:10:07 +00:00
|
|
|
{
|
2022-09-08 23:14:25 +01:00
|
|
|
return errorTypePack;
|
2021-11-19 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
TypeId BuiltinTypes::errorRecoveryType(TypeId guess) const
|
2021-11-19 16:10:07 +00:00
|
|
|
{
|
2022-04-29 02:24:24 +01:00
|
|
|
return guess;
|
2021-11-19 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
TypePackId BuiltinTypes::errorRecoveryTypePack(TypePackId guess) const
|
2021-11-19 16:10:07 +00:00
|
|
|
{
|
2022-04-29 02:24:24 +01:00
|
|
|
return guess;
|
2021-11-19 16:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
void persist(TypeId ty)
|
|
|
|
{
|
|
|
|
std::deque<TypeId> queue{ty};
|
|
|
|
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
|
|
|
TypeId t = queue.front();
|
|
|
|
queue.pop_front();
|
|
|
|
|
|
|
|
if (t->persistent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
asMutable(t)->persistent = true;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto btv = get<BoundType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
queue.push_back(btv->boundTo);
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ftv = get<FunctionType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
persist(ftv->argTypes);
|
2022-06-17 02:05:14 +01:00
|
|
|
persist(ftv->retTypes);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ttv = get<TableType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-03-04 16:36:33 +00:00
|
|
|
LUAU_ASSERT(ttv->state != TableState::Free && ttv->state != TableState::Unsealed);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
for (const auto& [_name, prop] : ttv->props)
|
2023-04-28 20:55:13 +01:00
|
|
|
queue.push_back(prop.type());
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
if (ttv->indexer)
|
|
|
|
{
|
|
|
|
queue.push_back(ttv->indexer->indexType);
|
|
|
|
queue.push_back(ttv->indexer->indexResultType);
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ctv = get<ClassType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
for (const auto& [_name, prop] : ctv->props)
|
2023-04-28 20:55:13 +01:00
|
|
|
queue.push_back(prop.type());
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto utv = get<UnionType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
for (TypeId opt : utv->options)
|
|
|
|
queue.push_back(opt);
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto itv = get<IntersectionType>(t))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
for (TypeId opt : itv->parts)
|
|
|
|
queue.push_back(opt);
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto mtv = get<MetatableType>(t))
|
2021-12-10 22:05:05 +00:00
|
|
|
{
|
|
|
|
queue.push_back(mtv->table);
|
|
|
|
queue.push_back(mtv->metatable);
|
|
|
|
}
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (get<GenericType>(t) || get<AnyType>(t) || get<FreeType>(t) || get<SingletonType>(t) || get<PrimitiveType>(t) || get<NegationType>(t))
|
2021-12-10 22:05:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!"TypeId is not supported in a persist call");
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void persist(TypePackId tp)
|
|
|
|
{
|
|
|
|
if (tp->persistent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
asMutable(tp)->persistent = true;
|
|
|
|
|
|
|
|
if (auto p = get<TypePack>(tp))
|
|
|
|
{
|
|
|
|
for (TypeId ty : p->head)
|
|
|
|
persist(ty);
|
|
|
|
if (p->tail)
|
|
|
|
persist(*p->tail);
|
|
|
|
}
|
2021-12-10 22:05:05 +00:00
|
|
|
else if (auto vtp = get<VariadicTypePack>(tp))
|
|
|
|
{
|
|
|
|
persist(vtp->ty);
|
|
|
|
}
|
|
|
|
else if (get<GenericTypePack>(tp))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!"TypePackId is not supported in a persist call");
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const TypeLevel* getLevel(TypeId ty)
|
|
|
|
{
|
|
|
|
ty = follow(ty);
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
if (auto ftv = get<FreeType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return &ftv->level;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ttv = get<TableType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return &ttv->level;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ftv = get<FunctionType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return &ftv->level;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeLevel* getMutableLevel(TypeId ty)
|
|
|
|
{
|
|
|
|
return const_cast<TypeLevel*>(getLevel(ty));
|
|
|
|
}
|
|
|
|
|
2022-04-15 00:57:43 +01:00
|
|
|
std::optional<TypeLevel> getLevel(TypePackId tp)
|
|
|
|
{
|
|
|
|
tp = follow(tp);
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
if (auto ftv = get<FreeTypePack>(tp))
|
2022-04-15 00:57:43 +01:00
|
|
|
return ftv->level;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const Property* lookupClassProp(const ClassType* cls, const Name& name)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
while (cls)
|
|
|
|
{
|
|
|
|
auto it = cls->props.find(name);
|
|
|
|
if (it != cls->props.end())
|
|
|
|
return &it->second;
|
|
|
|
|
|
|
|
if (cls->parent)
|
2023-01-04 20:53:17 +00:00
|
|
|
cls = get<ClassType>(*cls->parent);
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LUAU_ASSERT(cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
bool isSubclass(const ClassType* cls, const ClassType* parent)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
while (cls)
|
|
|
|
{
|
|
|
|
if (cls == parent)
|
|
|
|
return true;
|
|
|
|
else if (!cls->parent)
|
|
|
|
return false;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
cls = get<ClassType>(*cls->parent);
|
2021-10-29 21:25:12 +01:00
|
|
|
LUAU_ASSERT(cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const std::vector<TypeId>& getTypes(const UnionType* utv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-07-08 02:22:39 +01:00
|
|
|
return utv->options;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const std::vector<TypeId>& getTypes(const IntersectionType* itv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-07-08 02:22:39 +01:00
|
|
|
return itv->parts;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
UnionTypeIterator begin(const UnionType* utv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return UnionTypeIterator{utv};
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
UnionTypeIterator end(const UnionType* utv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return UnionTypeIterator{};
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
IntersectionTypeIterator begin(const IntersectionType* itv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return IntersectionTypeIterator{itv};
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
IntersectionTypeIterator end(const IntersectionType* itv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
return IntersectionTypeIterator{};
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
static std::vector<TypeId> parseFormatString(NotNull<BuiltinTypes> builtinTypes, const char* data, size_t size)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-08-04 15:22:16 +01:00
|
|
|
const char* options = "cdiouxXeEfgGqs*";
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (data[i] == '%')
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i < size && data[i] == '%')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// we just ignore all characters (including flags/precision) up until first alphabetic character
|
2022-08-04 15:22:16 +01:00
|
|
|
while (i < size && !(data[i] > 0 && (isalpha(data[i]) || data[i] == '*')))
|
2021-10-29 21:25:12 +01:00
|
|
|
i++;
|
|
|
|
|
|
|
|
if (i == size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (data[i] == 'q' || data[i] == 's')
|
2023-01-04 20:53:17 +00:00
|
|
|
result.push_back(builtinTypes->stringType);
|
2022-08-04 15:22:16 +01:00
|
|
|
else if (data[i] == '*')
|
2023-01-04 20:53:17 +00:00
|
|
|
result.push_back(builtinTypes->unknownType);
|
2021-10-29 21:25:12 +01:00
|
|
|
else if (strchr(options, data[i]))
|
2023-01-04 20:53:17 +00:00
|
|
|
result.push_back(builtinTypes->numberType);
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
2023-01-04 20:53:17 +00:00
|
|
|
result.push_back(builtinTypes->errorRecoveryType(builtinTypes->anyType));
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-17 02:05:14 +01:00
|
|
|
std::optional<WithPredicate<TypePackId>> magicFunctionFormat(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-06-17 02:05:14 +01:00
|
|
|
auto [paramPack, _predicates] = withPredicate;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
TypeArena& arena = typechecker.currentModule->internalTypes;
|
|
|
|
|
|
|
|
AstExprConstantString* fmt = nullptr;
|
|
|
|
if (auto index = expr.func->as<AstExprIndexName>(); index && expr.self)
|
|
|
|
{
|
|
|
|
if (auto group = index->expr->as<AstExprGroup>())
|
|
|
|
fmt = group->expr->as<AstExprConstantString>();
|
|
|
|
else
|
|
|
|
fmt = index->expr->as<AstExprConstantString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expr.self && expr.args.size > 0)
|
|
|
|
fmt = expr.args.data[0]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!fmt)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> expected = parseFormatString(typechecker.builtinTypes, fmt->value.data, fmt->value.size);
|
2021-10-29 21:25:12 +01:00
|
|
|
const auto& [params, tail] = flatten(paramPack);
|
|
|
|
|
2022-01-06 23:26:14 +00:00
|
|
|
size_t paramOffset = 1;
|
|
|
|
size_t dataOffset = expr.self ? 0 : 1;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
// unify the prefix one argument at a time
|
2022-01-06 23:26:14 +00:00
|
|
|
for (size_t i = 0; i < expected.size() && i + paramOffset < params.size(); ++i)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-01-06 23:26:14 +00:00
|
|
|
Location location = expr.args.data[std::min(i + dataOffset, expr.args.size - 1)]->location;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[i + paramOffset], expected[i], scope, location);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we know the argument count or if we have too many arguments for sure, we can issue an error
|
2022-10-14 20:48:41 +01:00
|
|
|
size_t numActualParams = params.size();
|
|
|
|
size_t numExpectedParams = expected.size() + 1; // + 1 for the format string
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-10-14 20:48:41 +01:00
|
|
|
if (numExpectedParams != numActualParams && (!tail || numExpectedParams < numActualParams))
|
|
|
|
typechecker.reportError(TypeError{expr.location, CountMismatch{numExpectedParams, std::nullopt, numActualParams}});
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-06-17 02:05:14 +01:00
|
|
|
return WithPredicate<TypePackId>{arena.addTypePack({typechecker.stringType})};
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionFormat(MagicFunctionCallContext context)
|
|
|
|
{
|
|
|
|
TypeArena* arena = context.solver->arena;
|
|
|
|
|
|
|
|
AstExprConstantString* fmt = nullptr;
|
|
|
|
if (auto index = context.callSite->func->as<AstExprIndexName>(); index && context.callSite->self)
|
|
|
|
{
|
|
|
|
if (auto group = index->expr->as<AstExprGroup>())
|
|
|
|
fmt = group->expr->as<AstExprConstantString>();
|
|
|
|
else
|
|
|
|
fmt = index->expr->as<AstExprConstantString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context.callSite->self && context.callSite->args.size > 0)
|
|
|
|
fmt = context.callSite->args.data[0]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!fmt)
|
|
|
|
return false;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> expected = parseFormatString(context.solver->builtinTypes, fmt->value.data, fmt->value.size);
|
2022-11-10 22:53:13 +00:00
|
|
|
const auto& [params, tail] = flatten(context.arguments);
|
|
|
|
|
|
|
|
size_t paramOffset = 1;
|
|
|
|
|
|
|
|
// unify the prefix one argument at a time
|
|
|
|
for (size_t i = 0; i < expected.size() && i + paramOffset < params.size(); ++i)
|
|
|
|
{
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[i + paramOffset], expected[i]);
|
2022-11-10 22:53:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we know the argument count or if we have too many arguments for sure, we can issue an error
|
|
|
|
size_t numActualParams = params.size();
|
|
|
|
size_t numExpectedParams = expected.size() + 1; // + 1 for the format string
|
|
|
|
|
|
|
|
if (numExpectedParams != numActualParams && (!tail || numExpectedParams < numActualParams))
|
|
|
|
context.solver->reportError(TypeError{context.callSite->location, CountMismatch{numExpectedParams, std::nullopt, numActualParams}});
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
TypePackId resultPack = arena->addTypePack({context.solver->builtinTypes->stringType});
|
2022-11-10 22:53:13 +00:00
|
|
|
asMutable(context.result)->ty.emplace<BoundTypePack>(resultPack);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
static std::vector<TypeId> parsePatternString(NotNull<BuiltinTypes> builtinTypes, const char* data, size_t size)
|
2022-07-08 02:22:39 +01:00
|
|
|
{
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
int depth = 0;
|
|
|
|
bool parsingSet = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (data[i] == '%')
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if (!parsingSet && i < size && data[i] == 'b')
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
else if (!parsingSet && data[i] == '[')
|
|
|
|
{
|
|
|
|
parsingSet = true;
|
|
|
|
if (i + 1 < size && data[i + 1] == ']')
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
else if (parsingSet && data[i] == ']')
|
|
|
|
{
|
|
|
|
parsingSet = false;
|
|
|
|
}
|
|
|
|
else if (data[i] == '(')
|
|
|
|
{
|
|
|
|
if (parsingSet)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i + 1 < size && data[i + 1] == ')')
|
|
|
|
{
|
|
|
|
i++;
|
2023-03-24 18:03:04 +00:00
|
|
|
result.push_back(builtinTypes->optionalNumberType);
|
2022-07-08 02:22:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++depth;
|
2023-03-24 18:03:04 +00:00
|
|
|
result.push_back(builtinTypes->optionalStringType);
|
2022-07-08 02:22:39 +01:00
|
|
|
}
|
|
|
|
else if (data[i] == ')')
|
|
|
|
{
|
|
|
|
if (parsingSet)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
--depth;
|
|
|
|
|
|
|
|
if (depth < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth != 0 || parsingSet)
|
|
|
|
return std::vector<TypeId>();
|
|
|
|
|
|
|
|
if (result.empty())
|
2023-03-24 18:03:04 +00:00
|
|
|
result.push_back(builtinTypes->optionalStringType);
|
2022-07-08 02:22:39 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionGmatch(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate)
|
|
|
|
{
|
|
|
|
auto [paramPack, _predicates] = withPredicate;
|
|
|
|
const auto& [params, tail] = flatten(paramPack);
|
|
|
|
|
|
|
|
if (params.size() != 2)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
TypeArena& arena = typechecker.currentModule->internalTypes;
|
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t index = expr.self ? 0 : 1;
|
|
|
|
if (expr.args.size > index)
|
|
|
|
pattern = expr.args.data[index]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> returnTypes = parsePatternString(typechecker.builtinTypes, pattern->value.data, pattern->value.size);
|
2022-07-08 02:22:39 +01:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return std::nullopt;
|
|
|
|
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[0], typechecker.stringType, scope, expr.args.data[0]->location);
|
2022-07-08 02:22:39 +01:00
|
|
|
|
|
|
|
const TypePackId emptyPack = arena.addTypePack({});
|
|
|
|
const TypePackId returnList = arena.addTypePack(returnTypes);
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId iteratorType = arena.addType(FunctionType{emptyPack, returnList});
|
2022-07-08 02:22:39 +01:00
|
|
|
return WithPredicate<TypePackId>{arena.addTypePack({iteratorType})};
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionGmatch(MagicFunctionCallContext context)
|
|
|
|
{
|
|
|
|
const auto& [params, tail] = flatten(context.arguments);
|
|
|
|
|
|
|
|
if (params.size() != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TypeArena* arena = context.solver->arena;
|
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t index = context.callSite->self ? 0 : 1;
|
|
|
|
if (context.callSite->args.size > index)
|
|
|
|
pattern = context.callSite->args.data[index]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return false;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> returnTypes = parsePatternString(context.solver->builtinTypes, pattern->value.data, pattern->value.size);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return false;
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[0], context.solver->builtinTypes->stringType);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
const TypePackId emptyPack = arena->addTypePack({});
|
|
|
|
const TypePackId returnList = arena->addTypePack(returnTypes);
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId iteratorType = arena->addType(FunctionType{emptyPack, returnList});
|
2022-11-10 22:53:13 +00:00
|
|
|
const TypePackId resTypePack = arena->addTypePack({iteratorType});
|
|
|
|
asMutable(context.result)->ty.emplace<BoundTypePack>(resTypePack);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-14 23:52:26 +01:00
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionMatch(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate)
|
|
|
|
{
|
|
|
|
auto [paramPack, _predicates] = withPredicate;
|
|
|
|
const auto& [params, tail] = flatten(paramPack);
|
|
|
|
|
|
|
|
if (params.size() < 2 || params.size() > 3)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
TypeArena& arena = typechecker.currentModule->internalTypes;
|
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t patternIndex = expr.self ? 0 : 1;
|
|
|
|
if (expr.args.size > patternIndex)
|
|
|
|
pattern = expr.args.data[patternIndex]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> returnTypes = parsePatternString(typechecker.builtinTypes, pattern->value.data, pattern->value.size);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return std::nullopt;
|
|
|
|
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[0], typechecker.stringType, scope, expr.args.data[0]->location);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId optionalNumber = arena.addType(UnionType{{typechecker.nilType, typechecker.numberType}});
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
size_t initIndex = expr.self ? 1 : 2;
|
|
|
|
if (params.size() == 3 && expr.args.size > initIndex)
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[2], optionalNumber, scope, expr.args.data[initIndex]->location);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
const TypePackId returnList = arena.addTypePack(returnTypes);
|
|
|
|
return WithPredicate<TypePackId>{returnList};
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionMatch(MagicFunctionCallContext context)
|
|
|
|
{
|
|
|
|
const auto& [params, tail] = flatten(context.arguments);
|
|
|
|
|
|
|
|
if (params.size() < 2 || params.size() > 3)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TypeArena* arena = context.solver->arena;
|
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t patternIndex = context.callSite->self ? 0 : 1;
|
|
|
|
if (context.callSite->args.size > patternIndex)
|
|
|
|
pattern = context.callSite->args.data[patternIndex]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return false;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::vector<TypeId> returnTypes = parsePatternString(context.solver->builtinTypes, pattern->value.data, pattern->value.size);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return false;
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[0], context.solver->builtinTypes->stringType);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId optionalNumber = arena->addType(UnionType{{context.solver->builtinTypes->nilType, context.solver->builtinTypes->numberType}});
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
size_t initIndex = context.callSite->self ? 1 : 2;
|
|
|
|
if (params.size() == 3 && context.callSite->args.size > initIndex)
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[2], optionalNumber);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
const TypePackId returnList = arena->addTypePack(returnTypes);
|
|
|
|
asMutable(context.result)->ty.emplace<BoundTypePack>(returnList);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-14 23:52:26 +01:00
|
|
|
static std::optional<WithPredicate<TypePackId>> magicFunctionFind(
|
|
|
|
TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, WithPredicate<TypePackId> withPredicate)
|
|
|
|
{
|
|
|
|
auto [paramPack, _predicates] = withPredicate;
|
|
|
|
const auto& [params, tail] = flatten(paramPack);
|
|
|
|
|
|
|
|
if (params.size() < 2 || params.size() > 4)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
TypeArena& arena = typechecker.currentModule->internalTypes;
|
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t patternIndex = expr.self ? 0 : 1;
|
|
|
|
if (expr.args.size > patternIndex)
|
|
|
|
pattern = expr.args.data[patternIndex]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
bool plain = false;
|
|
|
|
size_t plainIndex = expr.self ? 2 : 3;
|
|
|
|
if (expr.args.size > plainIndex)
|
|
|
|
{
|
|
|
|
AstExprConstantBool* p = expr.args.data[plainIndex]->as<AstExprConstantBool>();
|
|
|
|
plain = p && p->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<TypeId> returnTypes;
|
|
|
|
if (!plain)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
returnTypes = parsePatternString(typechecker.builtinTypes, pattern->value.data, pattern->value.size);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[0], typechecker.stringType, scope, expr.args.data[0]->location);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId optionalNumber = arena.addType(UnionType{{typechecker.nilType, typechecker.numberType}});
|
|
|
|
const TypeId optionalBoolean = arena.addType(UnionType{{typechecker.nilType, typechecker.booleanType}});
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
size_t initIndex = expr.self ? 1 : 2;
|
|
|
|
if (params.size() >= 3 && expr.args.size > initIndex)
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[2], optionalNumber, scope, expr.args.data[initIndex]->location);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
if (params.size() == 4 && expr.args.size > plainIndex)
|
2022-08-18 22:32:08 +01:00
|
|
|
typechecker.unify(params[3], optionalBoolean, scope, expr.args.data[plainIndex]->location);
|
2022-07-14 23:52:26 +01:00
|
|
|
|
|
|
|
returnTypes.insert(returnTypes.begin(), {optionalNumber, optionalNumber});
|
|
|
|
|
|
|
|
const TypePackId returnList = arena.addTypePack(returnTypes);
|
|
|
|
return WithPredicate<TypePackId>{returnList};
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
static bool dcrMagicFunctionFind(MagicFunctionCallContext context)
|
|
|
|
{
|
|
|
|
const auto& [params, tail] = flatten(context.arguments);
|
|
|
|
|
|
|
|
if (params.size() < 2 || params.size() > 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TypeArena* arena = context.solver->arena;
|
2023-01-04 20:53:17 +00:00
|
|
|
NotNull<BuiltinTypes> builtinTypes = context.solver->builtinTypes;
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
AstExprConstantString* pattern = nullptr;
|
|
|
|
size_t patternIndex = context.callSite->self ? 0 : 1;
|
|
|
|
if (context.callSite->args.size > patternIndex)
|
|
|
|
pattern = context.callSite->args.data[patternIndex]->as<AstExprConstantString>();
|
|
|
|
|
|
|
|
if (!pattern)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool plain = false;
|
|
|
|
size_t plainIndex = context.callSite->self ? 2 : 3;
|
|
|
|
if (context.callSite->args.size > plainIndex)
|
|
|
|
{
|
|
|
|
AstExprConstantBool* p = context.callSite->args.data[plainIndex]->as<AstExprConstantBool>();
|
|
|
|
plain = p && p->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<TypeId> returnTypes;
|
|
|
|
if (!plain)
|
|
|
|
{
|
2023-01-04 20:53:17 +00:00
|
|
|
returnTypes = parsePatternString(builtinTypes, pattern->value.data, pattern->value.size);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
if (returnTypes.empty())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[0], builtinTypes->stringType);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
const TypeId optionalNumber = arena->addType(UnionType{{builtinTypes->nilType, builtinTypes->numberType}});
|
|
|
|
const TypeId optionalBoolean = arena->addType(UnionType{{builtinTypes->nilType, builtinTypes->booleanType}});
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
size_t initIndex = context.callSite->self ? 1 : 2;
|
|
|
|
if (params.size() >= 3 && context.callSite->args.size > initIndex)
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[2], optionalNumber);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
if (params.size() == 4 && context.callSite->args.size > plainIndex)
|
2023-08-04 20:18:54 +01:00
|
|
|
context.solver->unify(context.solver->rootScope, context.callSite->location, params[3], optionalBoolean);
|
2022-11-10 22:53:13 +00:00
|
|
|
|
|
|
|
returnTypes.insert(returnTypes.begin(), {optionalNumber, optionalNumber});
|
|
|
|
|
|
|
|
const TypePackId returnList = arena->addTypePack(returnTypes);
|
|
|
|
asMutable(context.result)->ty.emplace<BoundTypePack>(returnList);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
TypeId freshType(NotNull<TypeArena> arena, NotNull<BuiltinTypes> builtinTypes, Scope* scope)
|
|
|
|
{
|
|
|
|
return arena->addType(FreeType{scope, builtinTypes->neverType, builtinTypes->unknownType});
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
std::vector<TypeId> filterMap(TypeId type, TypeIdPredicate predicate)
|
|
|
|
{
|
|
|
|
type = follow(type);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto utv = get<UnionType>(type))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
std::set<TypeId> options;
|
|
|
|
for (TypeId option : utv)
|
|
|
|
if (auto out = predicate(follow(option)))
|
|
|
|
options.insert(*out);
|
|
|
|
|
|
|
|
return std::vector<TypeId>(options.begin(), options.end());
|
|
|
|
}
|
|
|
|
else if (auto out = predicate(type))
|
|
|
|
return {*out};
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-05 15:47:21 +00:00
|
|
|
static Tags* getTags(TypeId ty)
|
|
|
|
{
|
|
|
|
ty = follow(ty);
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ftv = getMutable<FunctionType>(ty))
|
2021-11-05 15:47:21 +00:00
|
|
|
return &ftv->tags;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ttv = getMutable<TableType>(ty))
|
2021-11-05 15:47:21 +00:00
|
|
|
return &ttv->tags;
|
2023-01-04 20:53:17 +00:00
|
|
|
else if (auto ctv = getMutable<ClassType>(ty))
|
2021-11-05 15:47:21 +00:00
|
|
|
return &ctv->tags;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void attachTag(TypeId ty, const std::string& tagName)
|
|
|
|
{
|
2022-01-27 23:46:05 +00:00
|
|
|
if (auto tags = getTags(ty))
|
|
|
|
tags->push_back(tagName);
|
2021-11-05 15:47:21 +00:00
|
|
|
else
|
2022-01-27 23:46:05 +00:00
|
|
|
LUAU_ASSERT(!"This TypeId does not support tags");
|
2021-11-05 15:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void attachTag(Property& prop, const std::string& tagName)
|
|
|
|
{
|
|
|
|
prop.tags.push_back(tagName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We would ideally not expose this because it could cause a footgun.
|
|
|
|
// If the Base class has a tag and you ask if Derived has that tag, it would return false.
|
|
|
|
// Unfortunately, there's already use cases that's hard to disentangle. For now, we expose it.
|
|
|
|
bool hasTag(const Tags& tags, const std::string& tagName)
|
|
|
|
{
|
|
|
|
return std::find(tags.begin(), tags.end(), tagName) != tags.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTag(TypeId ty, const std::string& tagName)
|
|
|
|
{
|
|
|
|
ty = follow(ty);
|
|
|
|
|
|
|
|
// We special case classes because getTags only returns a pointer to one vector of tags.
|
|
|
|
// But classes has multiple vector of tags, represented throughout the hierarchy.
|
2023-01-04 20:53:17 +00:00
|
|
|
if (auto ctv = get<ClassType>(ty))
|
2021-11-05 15:47:21 +00:00
|
|
|
{
|
|
|
|
while (ctv)
|
|
|
|
{
|
|
|
|
if (hasTag(ctv->tags, tagName))
|
|
|
|
return true;
|
|
|
|
else if (!ctv->parent)
|
|
|
|
return false;
|
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
ctv = get<ClassType>(*ctv->parent);
|
2021-11-05 15:47:21 +00:00
|
|
|
LUAU_ASSERT(ctv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto tags = getTags(ty))
|
|
|
|
return hasTag(*tags, tagName);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTag(const Property& prop, const std::string& tagName)
|
|
|
|
{
|
|
|
|
return hasTag(prop.tags, tagName);
|
|
|
|
}
|
|
|
|
|
2022-08-04 23:35:33 +01:00
|
|
|
bool TypeFun::operator==(const TypeFun& rhs) const
|
|
|
|
{
|
|
|
|
return type == rhs.type && typeParams == rhs.typeParams && typePackParams == rhs.typePackParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GenericTypeDefinition::operator==(const GenericTypeDefinition& rhs) const
|
|
|
|
{
|
|
|
|
return ty == rhs.ty && defaultValue == rhs.defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GenericTypePackDefinition::operator==(const GenericTypePackDefinition& rhs) const
|
|
|
|
{
|
|
|
|
return tp == rhs.tp && defaultValue == rhs.defaultValue;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
} // namespace Luau
|