2023-08-05 01:01:35 +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/DenseHash.h"
|
|
|
|
#include "Luau/NotNull.h"
|
2023-10-21 04:36:26 +08:00
|
|
|
#include "Luau/TypePairHash.h"
|
|
|
|
#include "Luau/TypeCheckLimits.h"
|
|
|
|
#include "Luau/TypeFwd.h"
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct InternalErrorReporter;
|
|
|
|
struct Scope;
|
|
|
|
struct TypeArena;
|
|
|
|
|
|
|
|
enum class OccursCheckResult
|
|
|
|
{
|
|
|
|
Pass,
|
|
|
|
Fail
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Unifier2
|
|
|
|
{
|
|
|
|
NotNull<TypeArena> arena;
|
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
2023-09-23 02:10:49 +08:00
|
|
|
NotNull<Scope> scope;
|
2023-08-05 01:01:35 +08:00
|
|
|
NotNull<InternalErrorReporter> ice;
|
2023-09-23 02:10:49 +08:00
|
|
|
TypeCheckLimits limits;
|
|
|
|
|
|
|
|
DenseHashSet<std::pair<TypeId, TypeId>, TypePairHash> seenTypePairings{{nullptr, nullptr}};
|
|
|
|
DenseHashSet<std::pair<TypePackId, TypePackId>, TypePairHash> seenTypePackPairings{{nullptr, nullptr}};
|
2023-08-05 01:01:35 +08:00
|
|
|
|
2023-11-11 02:05:48 +08:00
|
|
|
DenseHashMap<TypeId, std::vector<TypeId>> expandedFreeTypes{nullptr};
|
|
|
|
|
2024-02-24 02:40:00 +08:00
|
|
|
// Mapping from generic types to free types to be used in instantiation.
|
|
|
|
DenseHashMap<TypeId, TypeId> genericSubstitutions{nullptr};
|
|
|
|
// Mapping from generic type packs to `TypePack`s of free types to be used in instantiation.
|
|
|
|
DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions{nullptr};
|
|
|
|
|
2023-08-05 01:01:35 +08:00
|
|
|
int recursionCount = 0;
|
|
|
|
int recursionLimit = 0;
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
Unifier2(NotNull<TypeArena> arena, NotNull<BuiltinTypes> builtinTypes, NotNull<Scope> scope, NotNull<InternalErrorReporter> ice);
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
/** Attempt to commit the subtype relation subTy <: superTy to the type
|
|
|
|
* graph.
|
|
|
|
*
|
|
|
|
* @returns true if successful.
|
|
|
|
*
|
|
|
|
* Note that incoherent types can and will successfully be unified. We stop
|
|
|
|
* when we *cannot know* how to relate the provided types, not when doing so
|
|
|
|
* would narrow something down to never or broaden it to unknown.
|
|
|
|
*
|
|
|
|
* Presently, the only way unification can fail is if we attempt to bind one
|
|
|
|
* free TypePack to another and encounter an occurs check violation.
|
|
|
|
*/
|
|
|
|
bool unify(TypeId subTy, TypeId superTy);
|
2024-01-27 10:30:40 +08:00
|
|
|
bool unify(const LocalType* subTy, TypeId superFn);
|
2023-09-23 02:10:49 +08:00
|
|
|
bool unify(TypeId subTy, const FunctionType* superFn);
|
|
|
|
bool unify(const UnionType* subUnion, TypeId superTy);
|
|
|
|
bool unify(TypeId subTy, const UnionType* superUnion);
|
|
|
|
bool unify(const IntersectionType* subIntersection, TypeId superTy);
|
|
|
|
bool unify(TypeId subTy, const IntersectionType* superIntersection);
|
2023-12-02 10:04:44 +08:00
|
|
|
bool unify(TableType* subTable, const TableType* superTable);
|
2023-09-23 02:10:49 +08:00
|
|
|
bool unify(const MetatableType* subMetatable, const MetatableType* superMetatable);
|
2023-08-05 01:01:35 +08:00
|
|
|
|
|
|
|
// TODO think about this one carefully. We don't do unions or intersections of type packs
|
|
|
|
bool unify(TypePackId subTp, TypePackId superTp);
|
|
|
|
|
2023-09-23 02:10:49 +08:00
|
|
|
std::optional<TypeId> generalize(TypeId ty);
|
2023-08-05 01:01:35 +08:00
|
|
|
|
2023-09-16 00:27:45 +08:00
|
|
|
private:
|
2023-08-05 01:01:35 +08:00
|
|
|
/**
|
|
|
|
* @returns simplify(left | right)
|
|
|
|
*/
|
|
|
|
TypeId mkUnion(TypeId left, TypeId right);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns simplify(left & right)
|
|
|
|
*/
|
|
|
|
TypeId mkIntersection(TypeId left, TypeId right);
|
|
|
|
|
|
|
|
// Returns true if needle occurs within haystack already. ie if we bound
|
|
|
|
// needle to haystack, would a cyclic TypePack result?
|
|
|
|
OccursCheckResult occursCheck(DenseHashSet<TypePackId>& seen, TypePackId needle, TypePackId haystack);
|
|
|
|
};
|
|
|
|
|
2023-09-16 00:27:45 +08:00
|
|
|
} // namespace Luau
|