2023-08-18 19:15:41 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Luau/Subtyping.h"
|
|
|
|
|
|
|
|
#include "Luau/Common.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
#include "Luau/Error.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
#include "Luau/Normalize.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
#include "Luau/StringUtils.h"
|
|
|
|
#include "Luau/ToString.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
#include "Luau/Type.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
#include "Luau/TypeArena.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
#include "Luau/TypePack.h"
|
|
|
|
#include "Luau/TypeUtils.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
struct VarianceFlipper
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
Subtyping::Variance* variance;
|
|
|
|
Subtyping::Variance oldValue;
|
|
|
|
|
|
|
|
VarianceFlipper(Subtyping::Variance* v)
|
|
|
|
: variance(v)
|
|
|
|
, oldValue(*v)
|
|
|
|
{
|
|
|
|
switch (oldValue)
|
|
|
|
{
|
|
|
|
case Subtyping::Variance::Covariant:
|
|
|
|
*variance = Subtyping::Variance::Contravariant;
|
|
|
|
break;
|
|
|
|
case Subtyping::Variance::Contravariant:
|
|
|
|
*variance = Subtyping::Variance::Covariant;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~VarianceFlipper()
|
|
|
|
{
|
|
|
|
*variance = oldValue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void SubtypingResult::andAlso(const SubtypingResult& other)
|
|
|
|
{
|
|
|
|
isSubtype &= other.isSubtype;
|
|
|
|
// `|=` is intentional here, we want to preserve error related flags.
|
|
|
|
isErrorSuppressing |= other.isErrorSuppressing;
|
|
|
|
normalizationTooComplex |= other.normalizationTooComplex;
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
void SubtypingResult::orElse(const SubtypingResult& other)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
isSubtype |= other.isSubtype;
|
|
|
|
isErrorSuppressing |= other.isErrorSuppressing;
|
|
|
|
normalizationTooComplex |= other.normalizationTooComplex;
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult SubtypingResult::all(const std::vector<SubtypingResult>& results)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult acc{true, false};
|
|
|
|
for (const SubtypingResult& current : results)
|
|
|
|
acc.andAlso(current);
|
2023-08-18 19:15:41 +01:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult SubtypingResult::any(const std::vector<SubtypingResult>& results)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult acc{false, false};
|
|
|
|
for (const SubtypingResult& current : results)
|
|
|
|
acc.orElse(current);
|
2023-08-18 19:15:41 +01:00
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype(TypeId subTy, TypeId superTy)
|
|
|
|
{
|
|
|
|
mappedGenerics.clear();
|
|
|
|
mappedGenericPacks.clear();
|
|
|
|
|
|
|
|
SubtypingResult result = isSubtype_(subTy, superTy);
|
|
|
|
|
|
|
|
for (const auto& [subTy, bounds]: mappedGenerics)
|
|
|
|
{
|
|
|
|
const auto& lb = bounds.lowerBound;
|
|
|
|
const auto& ub = bounds.upperBound;
|
|
|
|
|
|
|
|
TypeId lowerBound = makeAggregateType<UnionType>(lb, builtinTypes->neverType);
|
|
|
|
TypeId upperBound = makeAggregateType<IntersectionType>(ub, builtinTypes->unknownType);
|
|
|
|
|
|
|
|
result.andAlso(isSubtype_(lowerBound, upperBound));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubtypingResult Subtyping::isSubtype(TypePackId subTp, TypePackId superTp)
|
|
|
|
{
|
|
|
|
return isSubtype_(subTp, superTp);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct SeenSetPopper
|
|
|
|
{
|
|
|
|
Subtyping::SeenSet* seenTypes;
|
|
|
|
std::pair<TypeId, TypeId> pair;
|
|
|
|
|
|
|
|
SeenSetPopper(Subtyping::SeenSet* seenTypes, std::pair<TypeId, TypeId> pair)
|
|
|
|
: seenTypes(seenTypes)
|
|
|
|
, pair(pair)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~SeenSetPopper()
|
|
|
|
{
|
|
|
|
seenTypes->erase(pair);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SubtypingResult Subtyping::isSubtype_(TypeId subTy, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
subTy = follow(subTy);
|
|
|
|
superTy = follow(superTy);
|
|
|
|
|
|
|
|
// TODO: Do we care about returning a proof that this is error-suppressing?
|
|
|
|
// e.g. given `a | error <: a | error` where both operands are pointer equal,
|
|
|
|
// then should it also carry the information that it's error-suppressing?
|
|
|
|
// If it should, then `error <: error` should also do the same.
|
|
|
|
if (subTy == superTy)
|
|
|
|
return {true};
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
std::pair<TypeId, TypeId> typePair{subTy, superTy};
|
|
|
|
if (!seenTypes.insert(typePair).second)
|
|
|
|
return {true};
|
|
|
|
|
|
|
|
SeenSetPopper ssp{&seenTypes, typePair};
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
if (auto superUnion = get<UnionType>(superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(subTy, superUnion);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto subUnion = get<UnionType>(subTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(subUnion, superTy);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto superIntersection = get<IntersectionType>(superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(subTy, superIntersection);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto subIntersection = get<IntersectionType>(subTy))
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult result = isSubtype_(subIntersection, superTy);
|
2023-08-18 19:15:41 +01:00
|
|
|
if (result.isSubtype || result.isErrorSuppressing || result.normalizationTooComplex)
|
|
|
|
return result;
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(normalizer->normalize(subTy), normalizer->normalize(superTy));
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else if (get<AnyType>(superTy))
|
|
|
|
return {true}; // This is always true.
|
|
|
|
else if (get<AnyType>(subTy))
|
|
|
|
{
|
|
|
|
// any = unknown | error, so we rewrite this to match.
|
|
|
|
// As per TAPL: A | B <: T iff A <: T && B <: T
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult result = isSubtype_(builtinTypes->unknownType, superTy);
|
|
|
|
result.andAlso(isSubtype_(builtinTypes->errorType, superTy));
|
|
|
|
return result;
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
else if (get<UnknownType>(superTy))
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
LUAU_ASSERT(!get<AnyType>(subTy)); // TODO: replace with ice.
|
|
|
|
LUAU_ASSERT(!get<UnionType>(subTy)); // TODO: replace with ice.
|
|
|
|
LUAU_ASSERT(!get<IntersectionType>(subTy)); // TODO: replace with ice.
|
|
|
|
|
|
|
|
bool errorSuppressing = get<ErrorType>(subTy);
|
|
|
|
return {!errorSuppressing, errorSuppressing};
|
|
|
|
}
|
|
|
|
else if (get<NeverType>(subTy))
|
|
|
|
return {true};
|
|
|
|
else if (get<ErrorType>(superTy))
|
|
|
|
return {false, true};
|
|
|
|
else if (get<ErrorType>(subTy))
|
|
|
|
return {false, true};
|
2023-08-25 18:23:55 +01:00
|
|
|
else if (auto subGeneric = get<GenericType>(subTy); subGeneric && variance == Variance::Covariant)
|
|
|
|
{
|
|
|
|
bool ok = bindGeneric(subTy, superTy);
|
|
|
|
return {ok};
|
|
|
|
}
|
|
|
|
else if (auto superGeneric = get<GenericType>(superTy); superGeneric && variance == Variance::Contravariant)
|
|
|
|
{
|
|
|
|
bool ok = bindGeneric(subTy, superTy);
|
|
|
|
return {ok};
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto p = get2<PrimitiveType, PrimitiveType>(subTy, superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(p);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto p = get2<SingletonType, PrimitiveType>(subTy, superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(p);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto p = get2<SingletonType, SingletonType>(subTy, superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(p);
|
2023-08-18 19:15:41 +01:00
|
|
|
else if (auto p = get2<FunctionType, FunctionType>(subTy, superTy))
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(p);
|
|
|
|
else if (auto p = get2<TableType, TableType>(subTy, superTy))
|
|
|
|
return isSubtype_(p);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
return {false};
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(TypePackId subTp, TypePackId superTp)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
subTp = follow(subTp);
|
|
|
|
superTp = follow(superTp);
|
|
|
|
|
|
|
|
auto [subHead, subTail] = flatten(subTp);
|
|
|
|
auto [superHead, superTail] = flatten(superTp);
|
|
|
|
|
|
|
|
const size_t headSize = std::min(subHead.size(), superHead.size());
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
std::vector<SubtypingResult> results;
|
2023-08-18 19:15:41 +01:00
|
|
|
results.reserve(std::max(subHead.size(), superHead.size()) + 1);
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
if (subTp == superTp)
|
|
|
|
return {true};
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
// Match head types pairwise
|
|
|
|
|
|
|
|
for (size_t i = 0; i < headSize; ++i)
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
results.push_back(isSubtype_(subHead[i], superHead[i]));
|
2023-08-18 19:15:41 +01:00
|
|
|
if (!results.back().isSubtype)
|
|
|
|
return {false};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle mismatched head sizes
|
|
|
|
|
|
|
|
if (subHead.size() < superHead.size())
|
|
|
|
{
|
|
|
|
if (subTail)
|
|
|
|
{
|
|
|
|
if (auto vt = get<VariadicTypePack>(*subTail))
|
|
|
|
{
|
|
|
|
for (size_t i = headSize; i < superHead.size(); ++i)
|
2023-08-25 18:23:55 +01:00
|
|
|
results.push_back(isSubtype_(vt->ty, superHead[i]));
|
|
|
|
}
|
|
|
|
else if (auto gt = get<GenericTypePack>(*subTail))
|
|
|
|
{
|
|
|
|
if (variance == Variance::Covariant)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
// For any non-generic type T:
|
|
|
|
//
|
|
|
|
// <X>(X) -> () <: (T) -> ()
|
|
|
|
|
|
|
|
// Possible optimization: If headSize == 0 then we can just use subTp as-is.
|
|
|
|
std::vector<TypeId> headSlice(begin(superHead), end(superHead) + headSize);
|
|
|
|
TypePackId superTailPack = arena->addTypePack(std::move(headSlice), superTail);
|
|
|
|
|
|
|
|
if (TypePackId* other = mappedGenericPacks.find(*subTail))
|
|
|
|
results.push_back(isSubtype_(*other, superTailPack));
|
|
|
|
else
|
|
|
|
mappedGenericPacks.try_insert(*subTail, superTailPack);
|
|
|
|
|
|
|
|
// FIXME? Not a fan of the early return here. It makes the
|
|
|
|
// control flow harder to reason about.
|
|
|
|
return SubtypingResult::all(results);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For any non-generic type T:
|
|
|
|
//
|
|
|
|
// (T) -> () </: <X>(X) -> ()
|
|
|
|
//
|
|
|
|
return {false};
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
unexpected(*subTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return {false};
|
|
|
|
}
|
|
|
|
else if (subHead.size() > superHead.size())
|
|
|
|
{
|
|
|
|
if (superTail)
|
|
|
|
{
|
|
|
|
if (auto vt = get<VariadicTypePack>(*superTail))
|
|
|
|
{
|
|
|
|
for (size_t i = headSize; i < subHead.size(); ++i)
|
2023-08-25 18:23:55 +01:00
|
|
|
results.push_back(isSubtype_(subHead[i], vt->ty));
|
|
|
|
}
|
|
|
|
else if (auto gt = get<GenericTypePack>(*superTail))
|
|
|
|
{
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
{
|
|
|
|
// For any non-generic type T:
|
|
|
|
//
|
|
|
|
// <X...>(X...) -> () <: (T) -> ()
|
|
|
|
|
|
|
|
// Possible optimization: If headSize == 0 then we can just use subTp as-is.
|
|
|
|
std::vector<TypeId> headSlice(begin(subHead), end(subHead) + headSize);
|
|
|
|
TypePackId subTailPack = arena->addTypePack(std::move(headSlice), subTail);
|
|
|
|
|
|
|
|
if (TypePackId* other = mappedGenericPacks.find(*superTail))
|
|
|
|
results.push_back(isSubtype_(*other, subTailPack));
|
|
|
|
else
|
|
|
|
mappedGenericPacks.try_insert(*superTail, subTailPack);
|
|
|
|
|
|
|
|
// FIXME? Not a fan of the early return here. It makes the
|
|
|
|
// control flow harder to reason about.
|
|
|
|
return SubtypingResult::all(results);
|
|
|
|
}
|
|
|
|
else
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
// For any non-generic type T:
|
|
|
|
//
|
|
|
|
// () -> T </: <X...>() -> X...
|
|
|
|
return {false};
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
unexpected(*superTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return {false};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle tails
|
|
|
|
|
|
|
|
if (subTail && superTail)
|
|
|
|
{
|
|
|
|
if (auto p = get2<VariadicTypePack, VariadicTypePack>(*subTail, *superTail))
|
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
results.push_back(isSubtype_(p.first->ty, p.second->ty));
|
|
|
|
}
|
|
|
|
else if (auto p = get2<GenericTypePack, GenericTypePack>(*subTail, *superTail))
|
|
|
|
{
|
|
|
|
bool ok = bindGeneric(*subTail, *superTail);
|
|
|
|
results.push_back({ok});
|
|
|
|
}
|
|
|
|
else if (get2<VariadicTypePack, GenericTypePack>(*subTail, *superTail))
|
|
|
|
{
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
{
|
|
|
|
// <A...>(A...) -> number <: (...number) -> number
|
|
|
|
bool ok = bindGeneric(*subTail, *superTail);
|
|
|
|
results.push_back({ok});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// (number) -> ...number </: <A...>(number) -> A...
|
|
|
|
results.push_back({false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (get2<GenericTypePack, VariadicTypePack>(*subTail, *superTail))
|
|
|
|
{
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
{
|
|
|
|
// (...number) -> number </: <A...>(A...) -> number
|
|
|
|
results.push_back({false});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// <A...>() -> A... <: () -> ...number
|
|
|
|
bool ok = bindGeneric(*subTail, *superTail);
|
|
|
|
results.push_back({ok});
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else
|
2023-08-25 18:23:55 +01:00
|
|
|
iceReporter->ice(format("Subtyping::isSubtype got unexpected type packs %s and %s", toString(*subTail).c_str(), toString(*superTail).c_str()));
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else if (subTail)
|
|
|
|
{
|
|
|
|
if (get<VariadicTypePack>(*subTail))
|
|
|
|
{
|
|
|
|
return {false};
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
else if (get<GenericTypePack>(*subTail))
|
|
|
|
{
|
|
|
|
bool ok = bindGeneric(*subTail, builtinTypes->emptyTypePack);
|
|
|
|
return {ok};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
unexpected(*subTail);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
else if (superTail)
|
|
|
|
{
|
|
|
|
if (get<VariadicTypePack>(*superTail))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* A variadic type pack ...T can be thought of as an infinite union of finite type packs.
|
|
|
|
* () | (T) | (T, T) | (T, T, T) | ...
|
|
|
|
*
|
|
|
|
* And, per TAPL:
|
|
|
|
* T <: A | B iff T <: A or T <: B
|
|
|
|
*
|
|
|
|
* All variadic type packs are therefore supertypes of the empty type pack.
|
|
|
|
*/
|
|
|
|
}
|
2023-08-25 18:23:55 +01:00
|
|
|
else if (get<GenericTypePack>(*superTail))
|
|
|
|
{
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
{
|
|
|
|
bool ok = bindGeneric(builtinTypes->emptyTypePack, *superTail);
|
|
|
|
results.push_back({ok});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
results.push_back({false});
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
else
|
|
|
|
LUAU_ASSERT(0); // TODO
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
return SubtypingResult::all(results);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const TryPair<const SubTy*, const SuperTy*>& pair)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
return isSubtype_(pair.first, pair.second);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is much simpler than the Unifier implementation because we don't
|
|
|
|
* actually care about potential "cross-talk" between union parts that match the
|
|
|
|
* left side.
|
|
|
|
*
|
|
|
|
* In fact, we're very limited in what we can do: If multiple choices match, but
|
|
|
|
* all of them have non-overlapping constraints, then we're stuck with an "or"
|
|
|
|
* conjunction of constraints. Solving this in the general case is quite
|
|
|
|
* difficult.
|
|
|
|
*
|
|
|
|
* For example, we cannot dispatch anything from this constraint:
|
|
|
|
*
|
|
|
|
* {x: number, y: string} <: {x: number, y: 'a} | {x: 'b, y: string}
|
|
|
|
*
|
|
|
|
* From this constraint, we can know that either string <: 'a or number <: 'b,
|
|
|
|
* but we don't know which!
|
|
|
|
*
|
|
|
|
* However:
|
|
|
|
*
|
|
|
|
* {x: number, y: string} <: {x: number, y: 'a} | {x: number, y: string}
|
|
|
|
*
|
|
|
|
* We can dispatch this constraint because there is no 'or' conjunction. One of
|
|
|
|
* the arms requires 0 matches.
|
|
|
|
*
|
|
|
|
* {x: number, y: string, z: boolean} | {x: number, y: 'a, z: 'b} | {x: number,
|
|
|
|
* y: string, z: 'b}
|
|
|
|
*
|
|
|
|
* Here, we have two matches. One asks for string ~ 'a and boolean ~ 'b. The
|
|
|
|
* other just asks for boolean ~ 'b. We can dispatch this and only commit
|
|
|
|
* boolean ~ 'b. This constraint does not teach us anything about 'a.
|
|
|
|
*/
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(TypeId subTy, const UnionType* superUnion)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
// As per TAPL: T <: A | B iff T <: A || T <: B
|
2023-08-25 18:23:55 +01:00
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-08-18 19:15:41 +01:00
|
|
|
for (TypeId ty : superUnion)
|
2023-08-25 18:23:55 +01:00
|
|
|
subtypings.push_back(isSubtype_(subTy, ty));
|
|
|
|
return SubtypingResult::any(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const UnionType* subUnion, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
// As per TAPL: A | B <: T iff A <: T && B <: T
|
2023-08-25 18:23:55 +01:00
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-08-18 19:15:41 +01:00
|
|
|
for (TypeId ty : subUnion)
|
2023-08-25 18:23:55 +01:00
|
|
|
subtypings.push_back(isSubtype_(ty, superTy));
|
|
|
|
return SubtypingResult::all(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(TypeId subTy, const IntersectionType* superIntersection)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
// As per TAPL: T <: A & B iff T <: A && T <: B
|
2023-08-25 18:23:55 +01:00
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-08-18 19:15:41 +01:00
|
|
|
for (TypeId ty : superIntersection)
|
2023-08-25 18:23:55 +01:00
|
|
|
subtypings.push_back(isSubtype_(subTy, ty));
|
|
|
|
return SubtypingResult::all(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const IntersectionType* subIntersection, TypeId superTy)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
// TODO: Semantic subtyping here.
|
|
|
|
// As per TAPL: A & B <: T iff A <: T || B <: T
|
2023-08-25 18:23:55 +01:00
|
|
|
std::vector<SubtypingResult> subtypings;
|
2023-08-18 19:15:41 +01:00
|
|
|
for (TypeId ty : subIntersection)
|
2023-08-25 18:23:55 +01:00
|
|
|
subtypings.push_back(isSubtype_(ty, superTy));
|
|
|
|
return SubtypingResult::any(subtypings);
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const PrimitiveType* subPrim, const PrimitiveType* superPrim)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
return {subPrim->type == superPrim->type};
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const SingletonType* subSingleton, const PrimitiveType* superPrim)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
if (get<StringSingleton>(subSingleton) && superPrim->type == PrimitiveType::String)
|
|
|
|
return {true};
|
|
|
|
else if (get<BooleanSingleton>(subSingleton) && superPrim->type == PrimitiveType::Boolean)
|
|
|
|
return {true};
|
|
|
|
else
|
|
|
|
return {false};
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const SingletonType* subSingleton, const SingletonType* superSingleton)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
return {*subSingleton == *superSingleton};
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const TableType* subTable, const TableType* superTable)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult result{true};
|
|
|
|
|
|
|
|
for (const auto& [name, prop]: superTable->props)
|
|
|
|
{
|
|
|
|
auto it = subTable->props.find(name);
|
|
|
|
if (it != subTable->props.end())
|
|
|
|
{
|
|
|
|
// Table properties are invariant
|
|
|
|
result.andAlso(isSubtype(it->second.type(), prop.type()));
|
|
|
|
result.andAlso(isSubtype(prop.type(), it->second.type()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return SubtypingResult{false};
|
|
|
|
}
|
2023-08-18 19:15:41 +01:00
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
return result;
|
2023-08-18 19:15:41 +01:00
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult Subtyping::isSubtype_(const FunctionType* subFunction, const FunctionType* superFunction)
|
|
|
|
{
|
|
|
|
SubtypingResult result;
|
|
|
|
{
|
|
|
|
VarianceFlipper vf{&variance};
|
|
|
|
result.orElse(isSubtype_(superFunction->argTypes, subFunction->argTypes));
|
|
|
|
}
|
|
|
|
|
|
|
|
result.andAlso(isSubtype_(subFunction->retTypes, superFunction->retTypes));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubtypingResult Subtyping::isSubtype_(const NormalizedType* subNorm, const NormalizedType* superNorm)
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
if (!subNorm || !superNorm)
|
|
|
|
return {false, true, true};
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult result = isSubtype_(subNorm->tops, superNorm->tops);
|
|
|
|
result.andAlso(isSubtype_(subNorm->booleans, superNorm->booleans));
|
|
|
|
// isSubtype_(subNorm->classes, superNorm->classes);
|
|
|
|
// isSubtype_(subNorm->classes, superNorm->tables);
|
|
|
|
result.andAlso(isSubtype_(subNorm->errors, superNorm->errors));
|
|
|
|
result.andAlso(isSubtype_(subNorm->nils, superNorm->nils));
|
|
|
|
result.andAlso(isSubtype_(subNorm->numbers, superNorm->numbers));
|
2023-08-18 19:15:41 +01:00
|
|
|
result.isSubtype &= Luau::isSubtype(subNorm->strings, superNorm->strings);
|
2023-08-25 18:23:55 +01:00
|
|
|
// isSubtype_(subNorm->strings, superNorm->tables);
|
|
|
|
result.andAlso(isSubtype_(subNorm->threads, superNorm->threads));
|
|
|
|
// isSubtype_(subNorm->tables, superNorm->tables);
|
|
|
|
// isSubtype_(subNorm->tables, superNorm->strings);
|
|
|
|
// isSubtype_(subNorm->tables, superNorm->classes);
|
|
|
|
// isSubtype_(subNorm->functions, superNorm->functions);
|
|
|
|
// isSubtype_(subNorm->tyvars, superNorm->tyvars);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
bool Subtyping::bindGeneric(TypeId subTy, TypeId superTy)
|
|
|
|
{
|
|
|
|
if (variance == Variance::Covariant)
|
|
|
|
{
|
|
|
|
if (!get<GenericType>(subTy))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mappedGenerics[subTy].upperBound.insert(superTy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!get<GenericType>(superTy))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mappedGenerics[superTy].lowerBound.insert(subTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If, when performing a subtyping test, we encounter a generic on the left
|
|
|
|
* side, it is permissible to tentatively bind that generic to the right side
|
|
|
|
* type.
|
|
|
|
*/
|
|
|
|
bool Subtyping::bindGeneric(TypePackId subTp, TypePackId superTp)
|
|
|
|
{
|
|
|
|
if (variance == Variance::Contravariant)
|
|
|
|
std::swap(superTp, subTp);
|
|
|
|
|
|
|
|
if (!get<GenericTypePack>(subTp))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (TypePackId* m = mappedGenericPacks.find(subTp))
|
|
|
|
return *m == superTp;
|
|
|
|
|
|
|
|
mappedGenericPacks[subTp] = superTp;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Container>
|
|
|
|
TypeId Subtyping::makeAggregateType(const Container& container, TypeId orElse)
|
|
|
|
{
|
|
|
|
if (container.empty())
|
|
|
|
return orElse;
|
|
|
|
else if (container.size() == 1)
|
|
|
|
return *begin(container);
|
|
|
|
else
|
|
|
|
return arena->addType(T{std::vector<TypeId>(begin(container), end(container))});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Subtyping::unexpected(TypePackId tp)
|
|
|
|
{
|
|
|
|
iceReporter->ice(format("Unexpected type pack %s", toString(tp).c_str()));
|
|
|
|
}
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
} // namespace Luau
|