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
|
|
|
|
#include "Luau/TypeUtils.h"
|
|
|
|
|
2023-10-20 21:36:26 +01:00
|
|
|
#include "Luau/Common.h"
|
2022-08-18 22:04:33 +01:00
|
|
|
#include "Luau/Normalize.h"
|
2021-11-05 02:07:18 +00:00
|
|
|
#include "Luau/Scope.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/ToString.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
|
2022-10-21 18:33:43 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2023-08-04 18:01:35 +01:00
|
|
|
LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
std::optional<TypeId> findMetatableEntry(
|
2023-01-03 17:33:19 +00:00
|
|
|
NotNull<BuiltinTypes> builtinTypes, ErrorVec& errors, TypeId type, const std::string& entry, Location location)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
type = follow(type);
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
std::optional<TypeId> metatable = getMetatable(type, builtinTypes);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (!metatable)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
TypeId unwrapped = follow(*metatable);
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<AnyType>(unwrapped))
|
|
|
|
return builtinTypes->anyType;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
const TableType* mtt = getTableType(unwrapped);
|
2021-10-29 21:25:12 +01:00
|
|
|
if (!mtt)
|
|
|
|
{
|
2022-07-08 02:05:31 +01:00
|
|
|
errors.push_back(TypeError{location, GenericError{"Metatable was not a table"}});
|
2021-10-29 21:25:12 +01:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = mtt->props.find(entry);
|
|
|
|
if (it != mtt->props.end())
|
2023-04-28 12:55:55 +01:00
|
|
|
return it->second.type();
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-09-08 22:44:50 +01:00
|
|
|
std::optional<TypeId> findTablePropertyRespectingMeta(
|
2023-01-03 17:33:19 +00:00
|
|
|
NotNull<BuiltinTypes> builtinTypes, ErrorVec& errors, TypeId ty, const std::string& name, Location location)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<AnyType>(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
return ty;
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
if (const TableType* tableType = getTableType(ty))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
const auto& it = tableType->props.find(name);
|
|
|
|
if (it != tableType->props.end())
|
2023-04-28 12:55:55 +01:00
|
|
|
return it->second.type();
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
std::optional<TypeId> mtIndex = findMetatableEntry(builtinTypes, errors, ty, "__index", location);
|
2022-02-18 00:41:20 +00:00
|
|
|
int count = 0;
|
2021-10-29 21:25:12 +01:00
|
|
|
while (mtIndex)
|
|
|
|
{
|
|
|
|
TypeId index = follow(*mtIndex);
|
2022-02-18 00:41:20 +00:00
|
|
|
|
2022-05-20 00:46:52 +01:00
|
|
|
if (count >= 100)
|
|
|
|
return std::nullopt;
|
2022-02-18 00:41:20 +00:00
|
|
|
|
2022-05-20 00:46:52 +01:00
|
|
|
++count;
|
2022-02-18 00:41:20 +00:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
if (const auto& itt = getTableType(index))
|
|
|
|
{
|
|
|
|
const auto& fit = itt->props.find(name);
|
|
|
|
if (fit != itt->props.end())
|
2023-04-28 12:55:55 +01:00
|
|
|
return fit->second.type();
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
2023-01-03 17:33:19 +00:00
|
|
|
else if (const auto& itf = get<FunctionType>(index))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-06-17 01:54:42 +01:00
|
|
|
std::optional<TypeId> r = first(follow(itf->retTypes));
|
2021-10-29 21:25:12 +01:00
|
|
|
if (!r)
|
2023-01-03 17:33:19 +00:00
|
|
|
return builtinTypes->nilType;
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
|
|
|
return *r;
|
|
|
|
}
|
2023-01-03 17:33:19 +00:00
|
|
|
else if (get<AnyType>(index))
|
|
|
|
return builtinTypes->anyType;
|
2021-10-29 21:25:12 +01:00
|
|
|
else
|
|
|
|
errors.push_back(TypeError{location, GenericError{"__index should either be a function or table. Got " + toString(index)}});
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
mtIndex = findMetatableEntry(builtinTypes, errors, *mtIndex, "__index", location);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-09-15 23:13:58 +01:00
|
|
|
std::pair<size_t, std::optional<size_t>> getParameterExtents(const TxnLog* log, TypePackId tp, bool includeHiddenVariadics)
|
2022-09-02 00:00:14 +01:00
|
|
|
{
|
|
|
|
size_t minCount = 0;
|
|
|
|
size_t optionalCount = 0;
|
|
|
|
|
|
|
|
auto it = begin(tp, log);
|
|
|
|
auto endIter = end(tp);
|
|
|
|
|
|
|
|
while (it != endIter)
|
|
|
|
{
|
|
|
|
TypeId ty = *it;
|
|
|
|
if (isOptional(ty))
|
|
|
|
++optionalCount;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
minCount += optionalCount;
|
|
|
|
optionalCount = 0;
|
|
|
|
minCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
if (it.tail() && isVariadicTail(*it.tail(), *log, includeHiddenVariadics))
|
2022-09-02 00:00:14 +01:00
|
|
|
return {minCount, std::nullopt};
|
|
|
|
else
|
|
|
|
return {minCount, minCount + optionalCount};
|
|
|
|
}
|
|
|
|
|
2023-03-03 13:45:38 +00:00
|
|
|
TypePack extendTypePack(
|
|
|
|
TypeArena& arena, NotNull<BuiltinTypes> builtinTypes, TypePackId pack, size_t length, std::vector<std::optional<TypeId>> overrides)
|
2022-09-29 23:11:54 +01:00
|
|
|
{
|
2022-12-02 10:46:05 +00:00
|
|
|
TypePack result;
|
2022-09-29 23:11:54 +01:00
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
while (true)
|
2022-09-29 23:11:54 +01:00
|
|
|
{
|
2022-12-02 10:46:05 +00:00
|
|
|
pack = follow(pack);
|
|
|
|
|
|
|
|
if (const TypePack* p = get<TypePack>(pack))
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < p->head.size() && result.head.size() < length)
|
|
|
|
{
|
|
|
|
result.head.push_back(p->head[i]);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.head.size() == length)
|
|
|
|
{
|
|
|
|
if (i == p->head.size())
|
|
|
|
result.tail = p->tail;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TypePackId newTail = arena.addTypePack(TypePack{});
|
|
|
|
TypePack* newTailPack = getMutable<TypePack>(newTail);
|
|
|
|
|
|
|
|
newTailPack->head.insert(newTailPack->head.begin(), p->head.begin() + i, p->head.end());
|
|
|
|
newTailPack->tail = p->tail;
|
2022-09-29 23:11:54 +01:00
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
result.tail = newTail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (p->tail)
|
|
|
|
{
|
|
|
|
pack = *p->tail;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There just aren't enough types in this pack to satisfy the request.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (const VariadicTypePack* vtp = get<VariadicTypePack>(pack))
|
|
|
|
{
|
|
|
|
while (result.head.size() < length)
|
|
|
|
result.head.push_back(vtp->ty);
|
|
|
|
result.tail = pack;
|
2022-09-29 23:11:54 +01:00
|
|
|
return result;
|
2022-12-02 10:46:05 +00:00
|
|
|
}
|
|
|
|
else if (FreeTypePack* ftp = getMutable<FreeTypePack>(pack))
|
|
|
|
{
|
|
|
|
// If we need to get concrete types out of a free pack, we choose to
|
|
|
|
// interpret this as proof that the pack must have at least 'length'
|
|
|
|
// elements. We mint fresh types for each element we're extracting
|
|
|
|
// and rebind the free pack to be a TypePack containing them. We
|
|
|
|
// also have to create a new tail.
|
2022-09-29 23:11:54 +01:00
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
TypePack newPack;
|
|
|
|
newPack.tail = arena.freshTypePack(ftp->scope);
|
2023-03-03 13:45:38 +00:00
|
|
|
size_t overridesIndex = 0;
|
2022-12-02 10:46:05 +00:00
|
|
|
while (result.head.size() < length)
|
|
|
|
{
|
2023-03-03 13:45:38 +00:00
|
|
|
TypeId t;
|
|
|
|
if (overridesIndex < overrides.size() && overrides[overridesIndex])
|
|
|
|
{
|
|
|
|
t = *overrides[overridesIndex];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-04 18:01:35 +01:00
|
|
|
if (FFlag::DebugLuauDeferredConstraintResolution)
|
|
|
|
{
|
|
|
|
FreeType ft{ftp->scope, builtinTypes->neverType, builtinTypes->unknownType};
|
|
|
|
t = arena.addType(ft);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
t = arena.freshType(ftp->scope);
|
2023-03-03 13:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newPack.head.push_back(t);
|
2022-12-02 10:46:05 +00:00
|
|
|
result.head.push_back(newPack.head.back());
|
2023-03-03 13:45:38 +00:00
|
|
|
overridesIndex++;
|
2022-12-02 10:46:05 +00:00
|
|
|
}
|
2022-09-29 23:11:54 +01:00
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
asMutable(pack)->ty.emplace<TypePack>(std::move(newPack));
|
2022-09-29 23:11:54 +01:00
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (const Unifiable::Error* etp = getMutable<Unifiable::Error>(pack))
|
|
|
|
{
|
|
|
|
while (result.head.size() < length)
|
2023-01-03 17:33:19 +00:00
|
|
|
result.head.push_back(builtinTypes->errorRecoveryType());
|
2022-12-02 10:46:05 +00:00
|
|
|
|
|
|
|
result.tail = pack;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the pack is blocked or generic, we can't extract.
|
|
|
|
// Return whatever we've got with this pack as the tail.
|
|
|
|
result.tail = pack;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 23:11:54 +01:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:33:43 +01:00
|
|
|
std::vector<TypeId> reduceUnion(const std::vector<TypeId>& types)
|
|
|
|
{
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
for (TypeId t : types)
|
|
|
|
{
|
|
|
|
t = follow(t);
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<NeverType>(t))
|
2022-10-21 18:33:43 +01:00
|
|
|
continue;
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<ErrorType>(t) || get<AnyType>(t))
|
2022-10-21 18:33:43 +01:00
|
|
|
return {t};
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
if (const UnionType* utv = get<UnionType>(t))
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
for (TypeId ty : utv)
|
|
|
|
{
|
|
|
|
ty = follow(ty);
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<NeverType>(ty))
|
2022-10-21 18:33:43 +01:00
|
|
|
continue;
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<ErrorType>(ty) || get<AnyType>(ty))
|
2022-10-21 18:33:43 +01:00
|
|
|
return {ty};
|
|
|
|
|
|
|
|
if (result.end() == std::find(result.begin(), result.end(), ty))
|
|
|
|
result.push_back(ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (std::find(result.begin(), result.end(), t) == result.end())
|
|
|
|
result.push_back(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::optional<TypeId> tryStripUnionFromNil(TypeArena& arena, TypeId ty)
|
|
|
|
{
|
2023-01-03 17:33:19 +00:00
|
|
|
if (const UnionType* utv = get<UnionType>(ty))
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
if (!std::any_of(begin(utv), end(utv), isNil))
|
|
|
|
return ty;
|
|
|
|
|
|
|
|
std::vector<TypeId> result;
|
|
|
|
|
|
|
|
for (TypeId option : utv)
|
|
|
|
{
|
|
|
|
if (!isNil(option))
|
|
|
|
result.push_back(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.empty())
|
|
|
|
return std::nullopt;
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
return result.size() == 1 ? result[0] : arena.addType(UnionType{std::move(result)});
|
2022-10-21 18:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
TypeId stripNil(NotNull<BuiltinTypes> builtinTypes, TypeArena& arena, TypeId ty)
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
ty = follow(ty);
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
if (get<UnionType>(ty))
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
std::optional<TypeId> cleaned = tryStripUnionFromNil(arena, ty);
|
|
|
|
|
|
|
|
// If there is no union option without 'nil'
|
|
|
|
if (!cleaned)
|
2023-01-03 17:33:19 +00:00
|
|
|
return builtinTypes->nilType;
|
2022-10-21 18:33:43 +01:00
|
|
|
|
|
|
|
return follow(*cleaned);
|
|
|
|
}
|
|
|
|
|
|
|
|
return follow(ty);
|
|
|
|
}
|
|
|
|
|
2023-07-28 12:37:00 +01:00
|
|
|
ErrorSuppression shouldSuppressErrors(NotNull<Normalizer> normalizer, TypeId ty)
|
|
|
|
{
|
|
|
|
const NormalizedType* normType = normalizer->normalize(ty);
|
|
|
|
|
|
|
|
if (!normType)
|
|
|
|
return ErrorSuppression::NormalizationFailed;
|
|
|
|
|
|
|
|
return (normType->shouldSuppressErrors()) ? ErrorSuppression::Suppress : ErrorSuppression::DoNotSuppress;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorSuppression shouldSuppressErrors(NotNull<Normalizer> normalizer, TypePackId tp)
|
|
|
|
{
|
|
|
|
auto [tys, tail] = flatten(tp);
|
|
|
|
|
|
|
|
// check the head, one type at a time
|
|
|
|
for (TypeId ty : tys)
|
|
|
|
{
|
|
|
|
auto result = shouldSuppressErrors(normalizer, ty);
|
|
|
|
if (result != ErrorSuppression::DoNotSuppress)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the tail if we have one and it's finite
|
2023-09-22 19:10:49 +01:00
|
|
|
if (tail && tp != tail && finite(*tail))
|
2023-07-28 12:37:00 +01:00
|
|
|
return shouldSuppressErrors(normalizer, *tail);
|
|
|
|
|
|
|
|
return ErrorSuppression::DoNotSuppress;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a useful helper because it is often the case that we are looking at specifically a pair of types that might suppress.
|
|
|
|
ErrorSuppression shouldSuppressErrors(NotNull<Normalizer> normalizer, TypeId ty1, TypeId ty2)
|
|
|
|
{
|
|
|
|
auto result = shouldSuppressErrors(normalizer, ty1);
|
|
|
|
|
|
|
|
// if ty1 is do not suppress, ty2 determines our overall behavior
|
|
|
|
if (result == ErrorSuppression::DoNotSuppress)
|
|
|
|
return shouldSuppressErrors(normalizer, ty2);
|
|
|
|
|
|
|
|
// otherwise, ty1 is either suppress or normalization failure which are both the appropriate overarching result
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorSuppression shouldSuppressErrors(NotNull<Normalizer> normalizer, TypePackId tp1, TypePackId tp2)
|
|
|
|
{
|
|
|
|
auto result = shouldSuppressErrors(normalizer, tp1);
|
|
|
|
|
|
|
|
// if tp1 is do not suppress, tp2 determines our overall behavior
|
|
|
|
if (result == ErrorSuppression::DoNotSuppress)
|
|
|
|
return shouldSuppressErrors(normalizer, tp2);
|
|
|
|
|
|
|
|
// otherwise, tp1 is either suppress or normalization failure which are both the appropriate overarching result
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
} // namespace Luau
|