2023-07-08 04:10:48 +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-07-28 23:13:53 +08:00
|
|
|
#include "Luau/DenseHash.h"
|
2023-07-08 04:10:48 +08:00
|
|
|
#include "Luau/Type.h"
|
2023-08-05 03:18:54 +08:00
|
|
|
#include "Luau/UnifierSharedState.h"
|
2023-07-08 04:10:48 +08:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2023-07-28 23:13:53 +08:00
|
|
|
#include <unordered_map>
|
2023-07-08 04:10:48 +08:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
struct DiffPathNode
|
|
|
|
{
|
|
|
|
// TODO: consider using Variants to simplify toString implementation
|
|
|
|
enum Kind
|
|
|
|
{
|
|
|
|
TableProperty,
|
|
|
|
FunctionArgument,
|
|
|
|
FunctionReturn,
|
|
|
|
Union,
|
|
|
|
Intersection,
|
2023-07-28 23:13:53 +08:00
|
|
|
Negation,
|
2023-07-08 04:10:48 +08:00
|
|
|
};
|
|
|
|
Kind kind;
|
|
|
|
// non-null when TableProperty
|
|
|
|
std::optional<Name> tableProperty;
|
2023-07-15 02:08:53 +08:00
|
|
|
// non-null when FunctionArgument (unless variadic arg), FunctionReturn (unless variadic arg), Union, or Intersection (i.e. anonymous fields)
|
|
|
|
std::optional<size_t> index;
|
2023-07-08 04:10:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not use for leaf nodes
|
|
|
|
*/
|
|
|
|
DiffPathNode(Kind kind)
|
|
|
|
: kind(kind)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:08:53 +08:00
|
|
|
DiffPathNode(Kind kind, std::optional<Name> tableProperty, std::optional<size_t> index)
|
2023-07-08 04:10:48 +08:00
|
|
|
: kind(kind)
|
|
|
|
, tableProperty(tableProperty)
|
|
|
|
, index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString() const;
|
|
|
|
|
|
|
|
static DiffPathNode constructWithTableProperty(Name tableProperty);
|
2023-07-15 02:08:53 +08:00
|
|
|
|
|
|
|
static DiffPathNode constructWithKindAndIndex(Kind kind, size_t index);
|
|
|
|
|
|
|
|
static DiffPathNode constructWithKind(Kind kind);
|
2023-07-08 04:10:48 +08:00
|
|
|
};
|
2023-07-15 02:08:53 +08:00
|
|
|
|
2023-07-08 04:10:48 +08:00
|
|
|
struct DiffPathNodeLeaf
|
|
|
|
{
|
|
|
|
std::optional<TypeId> ty;
|
|
|
|
std::optional<Name> tableProperty;
|
2023-07-15 02:08:53 +08:00
|
|
|
std::optional<int> minLength;
|
|
|
|
bool isVariadic;
|
2023-07-28 23:13:53 +08:00
|
|
|
// TODO: Rename to anonymousIndex, for both union and Intersection
|
|
|
|
std::optional<size_t> unionIndex;
|
|
|
|
DiffPathNodeLeaf(
|
|
|
|
std::optional<TypeId> ty, std::optional<Name> tableProperty, std::optional<int> minLength, bool isVariadic, std::optional<size_t> unionIndex)
|
2023-07-08 04:10:48 +08:00
|
|
|
: ty(ty)
|
|
|
|
, tableProperty(tableProperty)
|
2023-07-15 02:08:53 +08:00
|
|
|
, minLength(minLength)
|
|
|
|
, isVariadic(isVariadic)
|
2023-07-28 23:13:53 +08:00
|
|
|
, unionIndex(unionIndex)
|
2023-07-08 04:10:48 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-15 02:08:53 +08:00
|
|
|
static DiffPathNodeLeaf detailsNormal(TypeId ty);
|
|
|
|
|
|
|
|
static DiffPathNodeLeaf detailsTableProperty(TypeId ty, Name tableProperty);
|
|
|
|
|
2023-07-28 23:13:53 +08:00
|
|
|
static DiffPathNodeLeaf detailsUnionIndex(TypeId ty, size_t index);
|
|
|
|
|
2023-07-15 02:08:53 +08:00
|
|
|
static DiffPathNodeLeaf detailsLength(int minLength, bool isVariadic);
|
|
|
|
|
2023-07-08 04:10:48 +08:00
|
|
|
static DiffPathNodeLeaf nullopts();
|
|
|
|
};
|
2023-07-15 02:08:53 +08:00
|
|
|
|
2023-07-08 04:10:48 +08:00
|
|
|
struct DiffPath
|
|
|
|
{
|
|
|
|
std::vector<DiffPathNode> path;
|
|
|
|
|
|
|
|
std::string toString(bool prependDot) const;
|
|
|
|
};
|
|
|
|
struct DiffError
|
|
|
|
{
|
|
|
|
enum Kind
|
|
|
|
{
|
|
|
|
Normal,
|
2023-07-28 23:13:53 +08:00
|
|
|
MissingTableProperty,
|
|
|
|
MissingUnionMember,
|
|
|
|
MissingIntersectionMember,
|
|
|
|
IncompatibleGeneric,
|
2023-07-08 04:10:48 +08:00
|
|
|
LengthMismatchInFnArgs,
|
|
|
|
LengthMismatchInFnRets,
|
|
|
|
};
|
|
|
|
Kind kind;
|
|
|
|
|
|
|
|
DiffPath diffPath;
|
|
|
|
DiffPathNodeLeaf left;
|
|
|
|
DiffPathNodeLeaf right;
|
|
|
|
|
|
|
|
std::string leftRootName;
|
|
|
|
std::string rightRootName;
|
|
|
|
|
|
|
|
DiffError(Kind kind, DiffPathNodeLeaf left, DiffPathNodeLeaf right, std::string leftRootName, std::string rightRootName)
|
|
|
|
: kind(kind)
|
|
|
|
, left(left)
|
|
|
|
, right(right)
|
|
|
|
, leftRootName(leftRootName)
|
|
|
|
, rightRootName(rightRootName)
|
|
|
|
{
|
|
|
|
checkValidInitialization(left, right);
|
|
|
|
}
|
|
|
|
DiffError(Kind kind, DiffPath diffPath, DiffPathNodeLeaf left, DiffPathNodeLeaf right, std::string leftRootName, std::string rightRootName)
|
|
|
|
: kind(kind)
|
|
|
|
, diffPath(diffPath)
|
|
|
|
, left(left)
|
|
|
|
, right(right)
|
|
|
|
, leftRootName(leftRootName)
|
|
|
|
, rightRootName(rightRootName)
|
|
|
|
{
|
|
|
|
checkValidInitialization(left, right);
|
|
|
|
}
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
std::string toString(bool multiLine = false) const;
|
2023-07-08 04:10:48 +08:00
|
|
|
|
|
|
|
private:
|
2023-08-11 22:42:37 +08:00
|
|
|
std::string toStringALeaf(std::string rootName, const DiffPathNodeLeaf& leaf, const DiffPathNodeLeaf& otherLeaf, bool multiLine) const;
|
2023-07-08 04:10:48 +08:00
|
|
|
void checkValidInitialization(const DiffPathNodeLeaf& left, const DiffPathNodeLeaf& right);
|
|
|
|
void checkNonMissingPropertyLeavesHaveNulloptTableProperty() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DifferResult
|
|
|
|
{
|
|
|
|
std::optional<DiffError> diffError;
|
|
|
|
|
|
|
|
DifferResult() {}
|
|
|
|
DifferResult(DiffError diffError)
|
|
|
|
: diffError(diffError)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void wrapDiffPath(DiffPathNode node);
|
|
|
|
};
|
|
|
|
struct DifferEnvironment
|
|
|
|
{
|
|
|
|
TypeId rootLeft;
|
|
|
|
TypeId rootRight;
|
2023-08-11 22:42:37 +08:00
|
|
|
std::optional<std::string> externalSymbolLeft;
|
|
|
|
std::optional<std::string> externalSymbolRight;
|
2023-07-28 23:13:53 +08:00
|
|
|
DenseHashMap<TypeId, TypeId> genericMatchedPairs;
|
2023-08-05 03:18:54 +08:00
|
|
|
DenseHashMap<TypePackId, TypePackId> genericTpMatchedPairs;
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
DifferEnvironment(
|
|
|
|
TypeId rootLeft, TypeId rootRight, std::optional<std::string> externalSymbolLeft, std::optional<std::string> externalSymbolRight)
|
2023-08-05 03:18:54 +08:00
|
|
|
: rootLeft(rootLeft)
|
|
|
|
, rootRight(rootRight)
|
2023-08-11 22:42:37 +08:00
|
|
|
, externalSymbolLeft(externalSymbolLeft)
|
|
|
|
, externalSymbolRight(externalSymbolRight)
|
2023-08-05 03:18:54 +08:00
|
|
|
, genericMatchedPairs(nullptr)
|
|
|
|
, genericTpMatchedPairs(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isProvenEqual(TypeId left, TypeId right) const;
|
|
|
|
bool isAssumedEqual(TypeId left, TypeId right) const;
|
|
|
|
void recordProvenEqual(TypeId left, TypeId right);
|
|
|
|
void pushVisiting(TypeId left, TypeId right);
|
|
|
|
void popVisiting();
|
|
|
|
std::vector<std::pair<TypeId, TypeId>>::const_reverse_iterator visitingBegin() const;
|
|
|
|
std::vector<std::pair<TypeId, TypeId>>::const_reverse_iterator visitingEnd() const;
|
2023-08-11 22:42:37 +08:00
|
|
|
std::string getDevFixFriendlyNameLeft() const;
|
|
|
|
std::string getDevFixFriendlyNameRight() const;
|
2023-08-05 03:18:54 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// TODO: consider using DenseHashSet
|
|
|
|
std::unordered_set<std::pair<TypeId, TypeId>, TypeIdPairHash> provenEqual;
|
|
|
|
// Ancestors of current types
|
|
|
|
std::unordered_set<std::pair<TypeId, TypeId>, TypeIdPairHash> visiting;
|
|
|
|
std::vector<std::pair<TypeId, TypeId>> visitingStack;
|
2023-07-08 04:10:48 +08:00
|
|
|
};
|
|
|
|
DifferResult diff(TypeId ty1, TypeId ty2);
|
2023-08-11 22:42:37 +08:00
|
|
|
DifferResult diffWithSymbols(TypeId ty1, TypeId ty2, std::optional<std::string> symbol1, std::optional<std::string> symbol2);
|
2023-07-08 04:10:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True if ty is a "simple" type, i.e. cannot contain types.
|
|
|
|
* string, number, boolean are simple types.
|
|
|
|
* function and table are not simple types.
|
|
|
|
*/
|
|
|
|
bool isSimple(TypeId ty);
|
|
|
|
|
|
|
|
} // namespace Luau
|