2023-08-19 02:15:41 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Type.h"
|
2023-09-02 01:58:27 +08:00
|
|
|
#include "Luau/TypePack.h"
|
2023-08-26 01:23:55 +08:00
|
|
|
#include "Luau/UnifierSharedState.h"
|
2023-08-19 02:15:41 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct TryPair;
|
2023-08-26 01:23:55 +08:00
|
|
|
struct InternalErrorReporter;
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2023-09-02 01:58:27 +08:00
|
|
|
class TypeIds;
|
2023-08-19 02:15:41 +08:00
|
|
|
class Normalizer;
|
|
|
|
struct NormalizedType;
|
2023-09-02 01:58:27 +08:00
|
|
|
struct NormalizedClassType;
|
2023-09-16 01:26:59 +08:00
|
|
|
struct NormalizedStringType;
|
2023-09-08 08:13:49 +08:00
|
|
|
struct NormalizedFunctionType;
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
struct SubtypingResult
|
2023-08-19 02:15:41 +08:00
|
|
|
{
|
|
|
|
bool isSubtype = false;
|
|
|
|
bool isErrorSuppressing = false;
|
|
|
|
bool normalizationTooComplex = false;
|
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
SubtypingResult& andAlso(const SubtypingResult& other);
|
|
|
|
SubtypingResult& orElse(const SubtypingResult& other);
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2023-09-02 01:58:27 +08:00
|
|
|
// Only negates the `isSubtype`.
|
|
|
|
static SubtypingResult negate(const SubtypingResult& result);
|
2023-08-26 01:23:55 +08:00
|
|
|
static SubtypingResult all(const std::vector<SubtypingResult>& results);
|
|
|
|
static SubtypingResult any(const std::vector<SubtypingResult>& results);
|
2023-08-19 02:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Subtyping
|
|
|
|
{
|
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
2023-08-26 01:23:55 +08:00
|
|
|
NotNull<TypeArena> arena;
|
2023-08-19 02:15:41 +08:00
|
|
|
NotNull<Normalizer> normalizer;
|
2023-08-26 01:23:55 +08:00
|
|
|
NotNull<InternalErrorReporter> iceReporter;
|
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
NotNull<Scope> scope;
|
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
enum class Variance
|
|
|
|
{
|
|
|
|
Covariant,
|
|
|
|
Contravariant
|
|
|
|
};
|
|
|
|
|
|
|
|
Variance variance = Variance::Covariant;
|
|
|
|
|
|
|
|
struct GenericBounds
|
|
|
|
{
|
|
|
|
DenseHashSet<TypeId> lowerBound{nullptr};
|
|
|
|
DenseHashSet<TypeId> upperBound{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we encounter a generic over the course of a subtyping test, we need
|
|
|
|
* to tentatively map that generic onto a type on the other side.
|
|
|
|
*/
|
|
|
|
DenseHashMap<TypeId, GenericBounds> mappedGenerics{nullptr};
|
|
|
|
DenseHashMap<TypePackId, TypePackId> mappedGenericPacks{nullptr};
|
|
|
|
|
|
|
|
using SeenSet = std::unordered_set<std::pair<TypeId, TypeId>, TypeIdPairHash>;
|
2023-09-02 01:58:27 +08:00
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
SeenSet seenTypes;
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
Subtyping(const Subtyping&) = delete;
|
|
|
|
Subtyping& operator=(const Subtyping&) = delete;
|
|
|
|
|
|
|
|
Subtyping(Subtyping&&) = default;
|
|
|
|
Subtyping& operator=(Subtyping&&) = default;
|
|
|
|
|
2023-08-19 02:15:41 +08:00
|
|
|
// TODO cache
|
|
|
|
// TODO cyclic types
|
|
|
|
// TODO recursion limits
|
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
SubtypingResult isSubtype(TypeId subTy, TypeId superTy);
|
|
|
|
SubtypingResult isSubtype(TypePackId subTy, TypePackId superTy);
|
2023-08-19 02:15:41 +08:00
|
|
|
|
|
|
|
private:
|
2023-09-16 01:26:59 +08:00
|
|
|
SubtypingResult isCovariantWith(TypeId subTy, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(TypePackId subTy, TypePackId superTy);
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
|
|
|
SubtypingResult isContravariantWith(SubTy&& subTy, SuperTy&& superTy);
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
|
|
|
SubtypingResult isInvariantWith(SubTy&& subTy, SuperTy&& superTy);
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
|
|
|
SubtypingResult isCovariantWith(const TryPair<const SubTy*, const SuperTy*>& pair);
|
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
|
|
|
SubtypingResult isContravariantWith(const TryPair<const SubTy*, const SuperTy*>& pair);
|
2023-08-26 01:23:55 +08:00
|
|
|
|
2023-08-19 02:15:41 +08:00
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-09-16 01:26:59 +08:00
|
|
|
SubtypingResult isInvariantWith(const TryPair<const SubTy*, const SuperTy*>& pair);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(TypeId subTy, const UnionType* superUnion);
|
|
|
|
SubtypingResult isCovariantWith(const UnionType* subUnion, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(TypeId subTy, const IntersectionType* superIntersection);
|
|
|
|
SubtypingResult isCovariantWith(const IntersectionType* subIntersection, TypeId superTy);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(const NegationType* subNegation, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(const TypeId subTy, const NegationType* superNegation);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(const PrimitiveType* subPrim, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isCovariantWith(const SingletonType* subSingleton, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isCovariantWith(const SingletonType* subSingleton, const SingletonType* superSingleton);
|
|
|
|
SubtypingResult isCovariantWith(const TableType* subTable, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(const MetatableType* subMt, const MetatableType* superMt);
|
|
|
|
SubtypingResult isCovariantWith(const MetatableType* subMt, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(const ClassType* subClass, const ClassType* superClass);
|
|
|
|
SubtypingResult isCovariantWith(const ClassType* subClass, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(const FunctionType* subFunction, const FunctionType* superFunction);
|
|
|
|
SubtypingResult isCovariantWith(const PrimitiveType* subPrim, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(const SingletonType* subSingleton, const TableType* superTable);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedType* subNorm, const NormalizedType* superNorm);
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedClassType& subClass, const NormalizedClassType& superClass);
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedClassType& subClass, const TypeIds& superTables);
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedStringType& subString, const NormalizedStringType& superString);
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedStringType& subString, const TypeIds& superTables);
|
|
|
|
SubtypingResult isCovariantWith(const NormalizedFunctionType& subFunction, const NormalizedFunctionType& superFunction);
|
|
|
|
SubtypingResult isCovariantWith(const TypeIds& subTypes, const TypeIds& superTypes);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(const VariadicTypePack* subVariadic, const VariadicTypePack* superVariadic);
|
2023-08-26 01:23:55 +08:00
|
|
|
|
|
|
|
bool bindGeneric(TypeId subTp, TypeId superTp);
|
|
|
|
bool bindGeneric(TypePackId subTp, TypePackId superTp);
|
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
template<typename T, typename Container>
|
2023-08-26 01:23:55 +08:00
|
|
|
TypeId makeAggregateType(const Container& container, TypeId orElse);
|
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
[[noreturn]] void unexpected(TypePackId tp);
|
2023-08-19 02:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|