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
|
|
|
|
|
2023-11-11 05:10:07 +08:00
|
|
|
#include "Luau/Set.h"
|
2023-10-21 09:10:30 +08:00
|
|
|
#include "Luau/TypeFwd.h"
|
2023-10-14 04:20:12 +08:00
|
|
|
#include "Luau/TypePairHash.h"
|
2023-10-21 09:10:30 +08:00
|
|
|
#include "Luau/TypePath.h"
|
2024-07-13 01:03:36 +08:00
|
|
|
#include "Luau/TypeFunction.h"
|
2024-02-03 05:32:42 +08:00
|
|
|
#include "Luau/TypeCheckLimits.h"
|
2023-11-11 05:10:07 +08:00
|
|
|
#include "Luau/DenseHash.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;
|
2023-09-02 01:58:27 +08:00
|
|
|
struct NormalizedClassType;
|
2023-09-08 08:13:49 +08:00
|
|
|
struct NormalizedFunctionType;
|
2024-02-24 04:08:34 +08:00
|
|
|
struct NormalizedStringType;
|
|
|
|
struct NormalizedType;
|
|
|
|
struct Property;
|
2023-11-11 05:10:07 +08:00
|
|
|
struct Scope;
|
|
|
|
struct TableIndexer;
|
2024-02-24 04:08:34 +08:00
|
|
|
struct TypeArena;
|
|
|
|
struct TypeCheckLimits;
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2023-11-18 02:46:18 +08:00
|
|
|
enum class SubtypingVariance
|
|
|
|
{
|
|
|
|
// Used for an empty key. Should never appear in actual code.
|
|
|
|
Invalid,
|
|
|
|
Covariant,
|
2023-12-02 15:46:57 +08:00
|
|
|
// This is used to identify cases where we have a covariant + a
|
|
|
|
// contravariant reason and we need to merge them.
|
|
|
|
Contravariant,
|
2023-11-18 02:46:18 +08:00
|
|
|
Invariant,
|
|
|
|
};
|
|
|
|
|
2023-10-21 09:10:30 +08:00
|
|
|
struct SubtypingReasoning
|
|
|
|
{
|
2023-12-02 15:46:57 +08:00
|
|
|
// The path, relative to the _root subtype_, where subtyping failed.
|
2023-10-21 09:10:30 +08:00
|
|
|
Path subPath;
|
2023-12-02 15:46:57 +08:00
|
|
|
// The path, relative to the _root supertype_, where subtyping failed.
|
2023-10-21 09:10:30 +08:00
|
|
|
Path superPath;
|
2023-11-18 02:46:18 +08:00
|
|
|
SubtypingVariance variance = SubtypingVariance::Covariant;
|
2023-10-21 09:10:30 +08:00
|
|
|
|
|
|
|
bool operator==(const SubtypingReasoning& other) const;
|
|
|
|
};
|
2023-09-16 01:26:59 +08:00
|
|
|
|
2023-11-11 05:10:07 +08:00
|
|
|
struct SubtypingReasoningHash
|
|
|
|
{
|
|
|
|
size_t operator()(const SubtypingReasoning& r) const;
|
|
|
|
};
|
|
|
|
|
2023-12-02 15:46:57 +08:00
|
|
|
using SubtypingReasonings = DenseHashSet<SubtypingReasoning, SubtypingReasoningHash>;
|
|
|
|
static const SubtypingReasoning kEmptyReasoning = SubtypingReasoning{TypePath::kEmpty, TypePath::kEmpty, SubtypingVariance::Invalid};
|
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
struct SubtypingResult
|
2023-08-19 02:15:41 +08:00
|
|
|
{
|
|
|
|
bool isSubtype = false;
|
|
|
|
bool normalizationTooComplex = false;
|
2023-10-14 04:20:12 +08:00
|
|
|
bool isCacheable = true;
|
2024-02-16 10:04:39 +08:00
|
|
|
ErrorVec errors;
|
2023-10-21 09:10:30 +08:00
|
|
|
/// The reason for isSubtype to be false. May not be present even if
|
|
|
|
/// isSubtype is false, depending on the input types.
|
2023-12-02 15:46:57 +08:00
|
|
|
SubtypingReasonings reasoning{kEmptyReasoning};
|
2023-10-21 09:10:30 +08:00
|
|
|
|
2023-09-16 01:26:59 +08:00
|
|
|
SubtypingResult& andAlso(const SubtypingResult& other);
|
|
|
|
SubtypingResult& orElse(const SubtypingResult& other);
|
2023-10-21 09:10:30 +08:00
|
|
|
SubtypingResult& withBothComponent(TypePath::Component component);
|
|
|
|
SubtypingResult& withSuperComponent(TypePath::Component component);
|
|
|
|
SubtypingResult& withSubComponent(TypePath::Component component);
|
|
|
|
SubtypingResult& withBothPath(TypePath::Path path);
|
|
|
|
SubtypingResult& withSubPath(TypePath::Path path);
|
|
|
|
SubtypingResult& withSuperPath(TypePath::Path path);
|
2024-02-16 10:04:39 +08:00
|
|
|
SubtypingResult& withErrors(ErrorVec& err);
|
2024-02-24 04:08:34 +08:00
|
|
|
SubtypingResult& withError(TypeError err);
|
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
|
|
|
};
|
|
|
|
|
2023-10-14 04:20:12 +08:00
|
|
|
struct SubtypingEnvironment
|
|
|
|
{
|
|
|
|
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};
|
|
|
|
|
2024-04-06 04:45:09 +08:00
|
|
|
/*
|
|
|
|
* See the test cyclic_tables_are_assumed_to_be_compatible_with_classes for
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* An empty value is equivalent to a nonexistent key.
|
|
|
|
*/
|
|
|
|
DenseHashMap<TypeId, TypeId> substitutions{nullptr};
|
|
|
|
|
2023-10-14 04:20:12 +08:00
|
|
|
DenseHashMap<std::pair<TypeId, TypeId>, SubtypingResult, TypePairHash> ephemeralCache{{}};
|
2024-02-24 04:08:34 +08:00
|
|
|
|
|
|
|
/// Applies `mappedGenerics` to the given type.
|
2024-07-13 01:03:36 +08:00
|
|
|
/// This is used specifically to substitute for generics in type function instances.
|
2024-02-24 04:08:34 +08:00
|
|
|
std::optional<TypeId> applyMappedGenerics(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId ty);
|
2023-10-14 04:20:12 +08:00
|
|
|
};
|
|
|
|
|
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;
|
2024-02-03 05:32:42 +08:00
|
|
|
TypeCheckLimits limits;
|
2023-09-16 01:26:59 +08:00
|
|
|
|
2023-08-26 01:23:55 +08:00
|
|
|
enum class Variance
|
|
|
|
{
|
|
|
|
Covariant,
|
|
|
|
Contravariant
|
|
|
|
};
|
|
|
|
|
|
|
|
Variance variance = Variance::Covariant;
|
|
|
|
|
2023-11-11 05:10:07 +08:00
|
|
|
using SeenSet = Set<std::pair<TypeId, TypeId>, TypePairHash>;
|
2023-09-02 01:58:27 +08:00
|
|
|
|
2023-11-11 05:10:07 +08:00
|
|
|
SeenSet seenTypes{{}};
|
2023-08-19 02:15:41 +08:00
|
|
|
|
2024-08-02 22:30:04 +08:00
|
|
|
Subtyping(
|
|
|
|
NotNull<BuiltinTypes> builtinTypes,
|
|
|
|
NotNull<TypeArena> typeArena,
|
|
|
|
NotNull<Normalizer> normalizer,
|
|
|
|
NotNull<InternalErrorReporter> iceReporter,
|
|
|
|
NotNull<Scope> scope
|
|
|
|
);
|
2023-10-14 04:20:12 +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-10-14 04:20:12 +08:00
|
|
|
// Only used by unit tests to test that the cache works.
|
|
|
|
const DenseHashMap<std::pair<TypeId, TypeId>, SubtypingResult, TypePairHash>& peekCache() const
|
|
|
|
{
|
|
|
|
return resultCache;
|
|
|
|
}
|
|
|
|
|
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-10-14 04:20:12 +08:00
|
|
|
DenseHashMap<std::pair<TypeId, TypeId>, SubtypingResult, TypePairHash> resultCache{{}};
|
|
|
|
|
|
|
|
SubtypingResult cache(SubtypingEnvironment& env, SubtypingResult res, TypeId subTy, TypeId superTy);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, TypeId subTy, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, TypePackId subTy, TypePackId superTy);
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isContravariantWith(SubtypingEnvironment& env, SubTy&& subTy, SuperTy&& superTy);
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isInvariantWith(SubtypingEnvironment& env, SubTy&& subTy, SuperTy&& superTy);
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TryPair<const SubTy*, const SuperTy*>& pair);
|
2023-09-16 01:26:59 +08:00
|
|
|
|
|
|
|
template<typename SubTy, typename SuperTy>
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isContravariantWith(SubtypingEnvironment& env, 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-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isInvariantWith(SubtypingEnvironment& env, const TryPair<const SubTy*, const SuperTy*>& pair);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, TypeId subTy, const UnionType* superUnion);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const UnionType* subUnion, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, TypeId subTy, const IntersectionType* superIntersection);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const IntersectionType* subIntersection, TypeId superTy);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const NegationType* subNegation, TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TypeId subTy, const NegationType* superNegation);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const PrimitiveType* subPrim, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const PrimitiveType* superPrim);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const SingletonType* superSingleton);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TableType* subTable, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const MetatableType* subMt, const MetatableType* superMt);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const MetatableType* subMt, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const ClassType* subClass, const ClassType* superClass);
|
2024-04-06 04:45:09 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, TypeId subTy, const ClassType* subClass, TypeId superTy, const TableType* superTable);
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const FunctionType* subFunction, const FunctionType* superFunction);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const PrimitiveType* subPrim, const TableType* superTable);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const SingletonType* subSingleton, const TableType* superTable);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TableIndexer& subIndexer, const TableIndexer& superIndexer);
|
2024-02-24 04:08:34 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const Property& subProperty, const Property& superProperty, const std::string& name);
|
2023-10-14 04:20:12 +08:00
|
|
|
|
2024-04-20 05:48:02 +08:00
|
|
|
SubtypingResult isCovariantWith(
|
2024-08-02 22:30:04 +08:00
|
|
|
SubtypingEnvironment& env,
|
|
|
|
const std::shared_ptr<const NormalizedType>& subNorm,
|
|
|
|
const std::shared_ptr<const NormalizedType>& superNorm
|
|
|
|
);
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const NormalizedClassType& subClass, const NormalizedClassType& superClass);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const NormalizedClassType& subClass, const TypeIds& superTables);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const NormalizedStringType& subString, const NormalizedStringType& superString);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const NormalizedStringType& subString, const TypeIds& superTables);
|
|
|
|
SubtypingResult isCovariantWith(
|
2024-08-02 22:30:04 +08:00
|
|
|
SubtypingEnvironment& env,
|
|
|
|
const NormalizedFunctionType& subFunction,
|
|
|
|
const NormalizedFunctionType& superFunction
|
|
|
|
);
|
2023-10-14 04:20:12 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TypeIds& subTypes, const TypeIds& superTypes);
|
|
|
|
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const VariadicTypePack* subVariadic, const VariadicTypePack* superVariadic);
|
2024-07-20 02:20:47 +08:00
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TypeFunctionInstanceType* subFunctionInstance, const TypeId superTy);
|
|
|
|
SubtypingResult isCovariantWith(SubtypingEnvironment& env, const TypeId subTy, const TypeFunctionInstanceType* superFunctionInstance);
|
2023-10-14 04:20:12 +08:00
|
|
|
|
|
|
|
bool bindGeneric(SubtypingEnvironment& env, TypeId subTp, TypeId superTp);
|
|
|
|
bool bindGeneric(SubtypingEnvironment& env, TypePackId subTp, TypePackId superTp);
|
2023-08-26 01:23:55 +08:00
|
|
|
|
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);
|
|
|
|
|
2024-07-20 02:20:47 +08:00
|
|
|
std::pair<TypeId, ErrorVec> handleTypeFunctionReductionResult(const TypeFunctionInstanceType* functionInstance);
|
2024-02-03 05:32:42 +08:00
|
|
|
|
|
|
|
[[noreturn]] void unexpected(TypeId ty);
|
2023-09-16 01:26:59 +08:00
|
|
|
[[noreturn]] void unexpected(TypePackId tp);
|
2023-08-19 02:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|