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
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Type.h"
|
2023-09-01 18:58:27 +01:00
|
|
|
#include "Luau/TypePack.h"
|
2023-08-25 18:23:55 +01:00
|
|
|
#include "Luau/UnifierSharedState.h"
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct TryPair;
|
2023-08-25 18:23:55 +01:00
|
|
|
struct InternalErrorReporter;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
2023-09-01 18:58:27 +01:00
|
|
|
class TypeIds;
|
2023-08-18 19:15:41 +01:00
|
|
|
class Normalizer;
|
|
|
|
struct NormalizedType;
|
2023-09-01 18:58:27 +01:00
|
|
|
struct NormalizedClassType;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
struct SubtypingResult
|
2023-08-18 19:15:41 +01:00
|
|
|
{
|
|
|
|
// Did the test succeed?
|
|
|
|
bool isSubtype = false;
|
|
|
|
bool isErrorSuppressing = false;
|
|
|
|
bool normalizationTooComplex = false;
|
|
|
|
|
|
|
|
// If so, what constraints are implied by this relation?
|
|
|
|
// If not, what happened?
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
void andAlso(const SubtypingResult& other);
|
|
|
|
void orElse(const SubtypingResult& other);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
2023-09-01 18:58:27 +01:00
|
|
|
// Only negates the `isSubtype`.
|
|
|
|
static SubtypingResult negate(const SubtypingResult& result);
|
2023-08-25 18:23:55 +01:00
|
|
|
static SubtypingResult all(const std::vector<SubtypingResult>& results);
|
|
|
|
static SubtypingResult any(const std::vector<SubtypingResult>& results);
|
2023-08-18 19:15:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Subtyping
|
|
|
|
{
|
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
2023-08-25 18:23:55 +01:00
|
|
|
NotNull<TypeArena> arena;
|
2023-08-18 19:15:41 +01:00
|
|
|
NotNull<Normalizer> normalizer;
|
2023-08-25 18:23:55 +01:00
|
|
|
NotNull<InternalErrorReporter> iceReporter;
|
|
|
|
|
|
|
|
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-01 18:58:27 +01:00
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SeenSet seenTypes;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
// TODO cache
|
|
|
|
// TODO cyclic types
|
|
|
|
// TODO recursion limits
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult isSubtype(TypeId subTy, TypeId superTy);
|
|
|
|
SubtypingResult isSubtype(TypePackId subTy, TypePackId superTy);
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
private:
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult isSubtype_(TypeId subTy, TypeId superTy);
|
|
|
|
SubtypingResult isSubtype_(TypePackId subTy, TypePackId superTy);
|
|
|
|
|
2023-08-18 19:15:41 +01:00
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult isSubtype_(const TryPair<const SubTy*, const SuperTy*>& pair);
|
|
|
|
|
|
|
|
SubtypingResult isSubtype_(TypeId subTy, const UnionType* superUnion);
|
|
|
|
SubtypingResult isSubtype_(const UnionType* subUnion, TypeId superTy);
|
|
|
|
SubtypingResult isSubtype_(TypeId subTy, const IntersectionType* superIntersection);
|
|
|
|
SubtypingResult isSubtype_(const IntersectionType* subIntersection, TypeId superTy);
|
|
|
|
SubtypingResult isSubtype_(const PrimitiveType* subPrim, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isSubtype_(const SingletonType* subSingleton, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isSubtype_(const SingletonType* subSingleton, const SingletonType* superSingleton);
|
|
|
|
SubtypingResult isSubtype_(const TableType* subTable, const TableType* superTable);
|
2023-09-01 18:58:27 +01:00
|
|
|
SubtypingResult isSubtype_(const MetatableType* subMt, const MetatableType* superMt);
|
|
|
|
SubtypingResult isSubtype_(const MetatableType* subMt, const TableType* superTable);
|
|
|
|
SubtypingResult isSubtype_(const ClassType* subClass, const ClassType* superClass);
|
|
|
|
SubtypingResult isSubtype_(const ClassType* subClass, const TableType* superTable); // Actually a class <: shape.
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult isSubtype_(const FunctionType* subFunction, const FunctionType* superFunction);
|
2023-09-01 18:58:27 +01:00
|
|
|
SubtypingResult isSubtype_(const PrimitiveType* subPrim, const TableType* superTable);
|
|
|
|
SubtypingResult isSubtype_(const SingletonType* subSingleton, const TableType* superTable);
|
|
|
|
|
2023-08-25 18:23:55 +01:00
|
|
|
SubtypingResult isSubtype_(const NormalizedType* subNorm, const NormalizedType* superNorm);
|
2023-09-01 18:58:27 +01:00
|
|
|
SubtypingResult isSubtype_(const NormalizedClassType& subClass, const NormalizedClassType& superClass, const TypeIds& superTables);
|
|
|
|
SubtypingResult isSubtype_(const TypeIds& subTypes, const TypeIds& superTypes);
|
|
|
|
|
|
|
|
SubtypingResult isSubtype_(const VariadicTypePack* subVariadic, const VariadicTypePack* superVariadic);
|
2023-08-25 18:23:55 +01:00
|
|
|
|
|
|
|
bool bindGeneric(TypeId subTp, TypeId superTp);
|
|
|
|
bool bindGeneric(TypePackId subTp, TypePackId superTp);
|
|
|
|
|
|
|
|
template <typename T, typename Container>
|
|
|
|
TypeId makeAggregateType(const Container& container, TypeId orElse);
|
|
|
|
|
|
|
|
[[noreturn]]
|
|
|
|
void unexpected(TypePackId tp);
|
2023-08-18 19:15:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|