2021-10-29 16:25:12 -04: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/Error.h"
|
|
|
|
#include "Luau/Location.h"
|
|
|
|
#include "Luau/TxnLog.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
#include "Luau/Module.h" // FIXME: For TypeArena. It merits breaking out into its own header.
|
2021-11-05 11:47:21 -04:00
|
|
|
#include "Luau/UnifierSharedState.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
enum Variance
|
|
|
|
{
|
|
|
|
Covariant,
|
|
|
|
Invariant
|
|
|
|
};
|
|
|
|
|
2022-02-24 18:53:37 -05:00
|
|
|
// A substitution which replaces singleton types by their wider types
|
|
|
|
struct Widen : Substitution
|
|
|
|
{
|
|
|
|
Widen(TypeArena* arena)
|
|
|
|
: Substitution(TxnLog::empty(), arena)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDirty(TypeId ty) override;
|
|
|
|
bool isDirty(TypePackId ty) override;
|
|
|
|
TypeId clean(TypeId ty) override;
|
|
|
|
TypePackId clean(TypePackId ty) override;
|
|
|
|
bool ignoreChildren(TypeId ty) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Use this more widely.
|
|
|
|
struct UnifierOptions
|
|
|
|
{
|
|
|
|
bool isFunctionCall = false;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
struct Unifier
|
|
|
|
{
|
|
|
|
TypeArena* const types;
|
|
|
|
Mode mode;
|
|
|
|
|
2022-01-06 20:46:53 -05:00
|
|
|
DEPRECATED_TxnLog DEPRECATED_log;
|
2021-10-29 16:25:12 -04:00
|
|
|
TxnLog log;
|
|
|
|
ErrorVec errors;
|
|
|
|
Location location;
|
|
|
|
Variance variance = Covariant;
|
|
|
|
CountMismatch::Context ctx = CountMismatch::Arg;
|
|
|
|
|
2021-11-05 11:47:21 -04:00
|
|
|
UnifierSharedState& sharedState;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-02-24 18:53:37 -05:00
|
|
|
Unifier(TypeArena* types, Mode mode, const Location& location, Variance variance, UnifierSharedState& sharedState,
|
2022-01-06 20:46:53 -05:00
|
|
|
TxnLog* parentLog = nullptr);
|
2022-03-04 11:36:33 -05:00
|
|
|
Unifier(TypeArena* types, Mode mode, std::vector<std::pair<TypeOrPackId, TypeOrPackId>>* sharedSeen, const Location& location,
|
2022-01-06 20:46:53 -05:00
|
|
|
Variance variance, UnifierSharedState& sharedState, TxnLog* parentLog = nullptr);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
// Test whether the two type vars unify. Never commits the result.
|
2022-01-06 20:46:53 -05:00
|
|
|
ErrorVec canUnify(TypeId subTy, TypeId superTy);
|
|
|
|
ErrorVec canUnify(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-01-06 20:46:53 -05:00
|
|
|
/** Attempt to unify.
|
2021-10-29 16:25:12 -04:00
|
|
|
* Populate the vector errors with any type errors that may arise.
|
|
|
|
* Populate the transaction log with the set of TypeIds that need to be reset to undo the unification attempt.
|
|
|
|
*/
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnify(TypeId subTy, TypeId superTy, bool isFunctionCall = false, bool isIntersection = false);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
private:
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnify_(TypeId subTy, TypeId superTy, bool isFunctionCall = false, bool isIntersection = false);
|
2022-02-04 11:45:57 -05:00
|
|
|
void tryUnifyUnionWithType(TypeId subTy, const UnionTypeVar* uv, TypeId superTy);
|
|
|
|
void tryUnifyTypeWithUnion(TypeId subTy, TypeId superTy, const UnionTypeVar* uv, bool cacheEnabled, bool isFunctionCall);
|
|
|
|
void tryUnifyTypeWithIntersection(TypeId subTy, TypeId superTy, const IntersectionTypeVar* uv);
|
|
|
|
void tryUnifyIntersectionWithType(TypeId subTy, const IntersectionTypeVar* uv, TypeId superTy, bool cacheEnabled, bool isFunctionCall);
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnifyPrimitives(TypeId subTy, TypeId superTy);
|
|
|
|
void tryUnifySingletons(TypeId subTy, TypeId superTy);
|
|
|
|
void tryUnifyFunctions(TypeId subTy, TypeId superTy, bool isFunctionCall = false);
|
|
|
|
void tryUnifyTables(TypeId subTy, TypeId superTy, bool isIntersection = false);
|
|
|
|
void DEPRECATED_tryUnifyTables(TypeId subTy, TypeId superTy, bool isIntersection = false);
|
|
|
|
void tryUnifyFreeTable(TypeId subTy, TypeId superTy);
|
|
|
|
void tryUnifySealedTables(TypeId subTy, TypeId superTy, bool isIntersection);
|
|
|
|
void tryUnifyWithMetatable(TypeId subTy, TypeId superTy, bool reversed);
|
|
|
|
void tryUnifyWithClass(TypeId subTy, TypeId superTy, bool reversed);
|
|
|
|
void tryUnifyIndexer(const TableIndexer& subIndexer, const TableIndexer& superIndexer);
|
2022-02-24 18:53:37 -05:00
|
|
|
|
|
|
|
TypeId widen(TypeId ty);
|
2021-11-05 11:47:21 -04:00
|
|
|
TypeId deeplyOptional(TypeId ty, std::unordered_map<TypeId, TypeId> seen = {});
|
2022-02-24 18:53:37 -05:00
|
|
|
|
2022-01-06 20:46:53 -05:00
|
|
|
void cacheResult(TypeId subTy, TypeId superTy);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
public:
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnify(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
private:
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnify_(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
|
|
|
void tryUnifyVariadics(TypePackId subTy, TypePackId superTy, bool reversed, int subOffset = 0);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-01-06 20:46:53 -05:00
|
|
|
void tryUnifyWithAny(TypeId subTy, TypeId anyTy);
|
|
|
|
void tryUnifyWithAny(TypePackId subTy, TypePackId anyTp);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
std::optional<TypeId> findTablePropertyRespectingMeta(TypeId lhsType, Name name);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Report an "infinite type error" if the type "needle" already occurs within "haystack"
|
|
|
|
void occursCheck(TypeId needle, TypeId haystack);
|
2021-12-03 01:41:04 -05:00
|
|
|
void occursCheck(DenseHashSet<TypeId>& seen, TypeId needle, TypeId haystack);
|
2021-10-29 16:25:12 -04:00
|
|
|
void occursCheck(TypePackId needle, TypePackId haystack);
|
2021-12-03 01:41:04 -05:00
|
|
|
void occursCheck(DenseHashSet<TypePackId>& seen, TypePackId needle, TypePackId haystack);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
Unifier makeChildUnifier();
|
|
|
|
|
2022-01-27 18:46:05 -05:00
|
|
|
// A utility function that appends the given error to the unifier's error log.
|
|
|
|
// This allows setting a breakpoint wherever the unifier reports an error.
|
|
|
|
void reportError(TypeError error)
|
|
|
|
{
|
|
|
|
errors.push_back(error);
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
private:
|
|
|
|
bool isNonstrictMode() const;
|
|
|
|
|
|
|
|
void checkChildUnifierTypeMismatch(const ErrorVec& innerErrors, TypeId wantedType, TypeId givenType);
|
2021-11-12 09:27:34 -05:00
|
|
|
void checkChildUnifierTypeMismatch(const ErrorVec& innerErrors, const std::string& prop, TypeId wantedType, TypeId givenType);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
[[noreturn]] void ice(const std::string& message, const Location& location);
|
|
|
|
[[noreturn]] void ice(const std::string& message);
|
2021-12-10 17:05:05 -05:00
|
|
|
|
|
|
|
// Available after regular type pack unification errors
|
|
|
|
std::optional<int> firstPackErrorPos;
|
2021-10-29 16:25:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|