mirror of
https://github.com/luau-lang/luau.git
synced 2024-11-15 14:25:44 +08:00
0b2755f964
* Progress toward a diffing algorithm for types. We hope that this will be useful for writing clearer error messages. * Add a missing recursion limiter in `Unifier::tryUnifyTables`. This was causing a crash in certain situations. * Luau heap graph enumeration improvements: * Weak references are not reported * Added tag as a fallback name of non-string table links * Included top Luau function information in thread name to understand where thread might be suspended * Constant folding for `math.pi` and `math.huge` at -O2 * Optimize `string.format` and `%*` * This change makes string interpolation 1.5x-2x faster depending on the number and type of formatted components, assuming a few are using primitive types, and reduces associated GC pressure. New type checker: * Initial work toward tracking the upper and lower bounds of types accurately. Native code generation (JIT): * Add IrCmd::CHECK_TRUTHY for improved assert fast-calls * Do not compute type map for modules without types * Capture metatable+readonly state for NEW_TABLE IR instructions * Replace JUMP_CMP_ANY with CMP_ANY and existing JUMP_EQ_INT * Add support for exits to VM with reentry lock in VmExit --------- Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
191 lines
5.3 KiB
C++
191 lines
5.3 KiB
C++
// 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/Type.h"
|
|
#include "Luau/UnifierSharedState.h"
|
|
#include <optional>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
namespace Luau
|
|
{
|
|
struct DiffPathNode
|
|
{
|
|
// TODO: consider using Variants to simplify toString implementation
|
|
enum Kind
|
|
{
|
|
TableProperty,
|
|
FunctionArgument,
|
|
FunctionReturn,
|
|
Union,
|
|
Intersection,
|
|
Negation,
|
|
};
|
|
Kind kind;
|
|
// non-null when TableProperty
|
|
std::optional<Name> tableProperty;
|
|
// non-null when FunctionArgument (unless variadic arg), FunctionReturn (unless variadic arg), Union, or Intersection (i.e. anonymous fields)
|
|
std::optional<size_t> index;
|
|
|
|
/**
|
|
* Do not use for leaf nodes
|
|
*/
|
|
DiffPathNode(Kind kind)
|
|
: kind(kind)
|
|
{
|
|
}
|
|
|
|
DiffPathNode(Kind kind, std::optional<Name> tableProperty, std::optional<size_t> index)
|
|
: kind(kind)
|
|
, tableProperty(tableProperty)
|
|
, index(index)
|
|
{
|
|
}
|
|
|
|
std::string toString() const;
|
|
|
|
static DiffPathNode constructWithTableProperty(Name tableProperty);
|
|
|
|
static DiffPathNode constructWithKindAndIndex(Kind kind, size_t index);
|
|
|
|
static DiffPathNode constructWithKind(Kind kind);
|
|
};
|
|
|
|
struct DiffPathNodeLeaf
|
|
{
|
|
std::optional<TypeId> ty;
|
|
std::optional<Name> tableProperty;
|
|
std::optional<int> minLength;
|
|
bool isVariadic;
|
|
// 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)
|
|
: ty(ty)
|
|
, tableProperty(tableProperty)
|
|
, minLength(minLength)
|
|
, isVariadic(isVariadic)
|
|
, unionIndex(unionIndex)
|
|
{
|
|
}
|
|
|
|
static DiffPathNodeLeaf detailsNormal(TypeId ty);
|
|
|
|
static DiffPathNodeLeaf detailsTableProperty(TypeId ty, Name tableProperty);
|
|
|
|
static DiffPathNodeLeaf detailsUnionIndex(TypeId ty, size_t index);
|
|
|
|
static DiffPathNodeLeaf detailsLength(int minLength, bool isVariadic);
|
|
|
|
static DiffPathNodeLeaf nullopts();
|
|
};
|
|
|
|
struct DiffPath
|
|
{
|
|
std::vector<DiffPathNode> path;
|
|
|
|
std::string toString(bool prependDot) const;
|
|
};
|
|
struct DiffError
|
|
{
|
|
enum Kind
|
|
{
|
|
Normal,
|
|
MissingTableProperty,
|
|
MissingUnionMember,
|
|
MissingIntersectionMember,
|
|
IncompatibleGeneric,
|
|
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);
|
|
}
|
|
|
|
std::string toString() const;
|
|
|
|
private:
|
|
std::string toStringALeaf(std::string rootName, const DiffPathNodeLeaf& leaf, const DiffPathNodeLeaf& otherLeaf) const;
|
|
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;
|
|
DenseHashMap<TypeId, TypeId> genericMatchedPairs;
|
|
DenseHashMap<TypePackId, TypePackId> genericTpMatchedPairs;
|
|
|
|
DifferEnvironment(TypeId rootLeft, TypeId rootRight)
|
|
: rootLeft(rootLeft)
|
|
, rootRight(rootRight)
|
|
, 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;
|
|
|
|
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;
|
|
};
|
|
DifferResult diff(TypeId ty1, TypeId ty2);
|
|
|
|
/**
|
|
* 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
|