mirror of
https://github.com/luau-lang/luau.git
synced 2024-12-13 05:20:38 +00:00
140e5a1495
* Fixed incorrect lexeme generated for string parts in the middle of an interpolated string (Fixes https://github.com/Roblox/luau/issues/744) * DeprecatedApi lint can report some issues without type inference information * Fixed performance of autocomplete requests when suggestions have large intersection types (Solves https://github.com/Roblox/luau/discussions/847) * Marked `table.getn`/`foreach`/`foreachi` as deprecated ([RFC: Deprecate table.getn/foreach/foreachi](https://github.com/Roblox/luau/blob/master/rfcs/deprecate-table-getn-foreach.md)) * With -O2 optimization level, we now optimize builtin calls based on known argument/return count. Note that this change can be observable if `getfenv/setfenv` is used to substitute a builtin, especially if arity is different. Fastcall heavy tests show a 1-2% improvement. * Luau can now be built with clang-cl (Fixes https://github.com/Roblox/luau/issues/736) We also made many improvements to our experimental components. For our new type solver: * Overhauled data flow analysis system, fixed issues with 'repeat' loops, global variables and type annotations * Type refinements now work on generic table indexing with a string literal * Type refinements will properly track potentially 'nil' values (like t[x] for a missing key) and their further refinements * Internal top table type is now isomorphic to `{}` which fixes issues when `typeof(v) == 'table'` type refinement is handled * References to non-existent types in type annotations no longer resolve to 'error' type like in old solver * Improved handling of class unions in property access expressions * Fixed default type packs * Unsealed tables can now have metatables * Restored expected types for function arguments And for native code generation: * Added min and max IR instructions mapping to vminsd/vmaxsd on x64 * We now speculatively extract direct execution fast-paths based on expected types of expressions which provides better optimization opportunities inside a single basic block * Translated existing math fastcalls to IR form to improve tag guard removal and constant propagation
161 lines
6.4 KiB
C++
161 lines
6.4 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/Error.h"
|
|
#include "Luau/Location.h"
|
|
#include "Luau/ParseOptions.h"
|
|
#include "Luau/Scope.h"
|
|
#include "Luau/Substitution.h"
|
|
#include "Luau/TxnLog.h"
|
|
#include "Luau/TypeArena.h"
|
|
#include "Luau/UnifierSharedState.h"
|
|
#include "Normalize.h"
|
|
|
|
#include <unordered_set>
|
|
|
|
namespace Luau
|
|
{
|
|
|
|
enum Variance
|
|
{
|
|
Covariant,
|
|
Invariant
|
|
};
|
|
|
|
// A substitution which replaces singleton types by their wider types
|
|
struct Widen : Substitution
|
|
{
|
|
Widen(TypeArena* arena, NotNull<BuiltinTypes> builtinTypes)
|
|
: Substitution(TxnLog::empty(), arena)
|
|
, builtinTypes(builtinTypes)
|
|
{
|
|
}
|
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
|
|
|
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;
|
|
|
|
TypeId operator()(TypeId ty);
|
|
TypePackId operator()(TypePackId ty);
|
|
};
|
|
|
|
// TODO: Use this more widely.
|
|
struct UnifierOptions
|
|
{
|
|
bool isFunctionCall = false;
|
|
};
|
|
|
|
struct Unifier
|
|
{
|
|
TypeArena* const types;
|
|
NotNull<BuiltinTypes> builtinTypes;
|
|
NotNull<Normalizer> normalizer;
|
|
Mode mode;
|
|
|
|
NotNull<Scope> scope; // const Scope maybe
|
|
TxnLog log;
|
|
ErrorVec errors;
|
|
Location location;
|
|
Variance variance = Covariant;
|
|
bool normalize = true; // Normalize unions and intersections if necessary
|
|
bool checkInhabited = true; // Normalize types to check if they are inhabited
|
|
bool useScopes = false; // If true, we use the scope hierarchy rather than TypeLevels
|
|
CountMismatch::Context ctx = CountMismatch::Arg;
|
|
|
|
UnifierSharedState& sharedState;
|
|
|
|
// When the Unifier is forced to unify two blocked types (or packs), they
|
|
// get added to these vectors. The ConstraintSolver can use this to know
|
|
// when it is safe to reattempt dispatching a constraint.
|
|
std::vector<TypeId> blockedTypes;
|
|
std::vector<TypePackId> blockedTypePacks;
|
|
|
|
Unifier(
|
|
NotNull<Normalizer> normalizer, Mode mode, NotNull<Scope> scope, const Location& location, Variance variance, TxnLog* parentLog = nullptr);
|
|
|
|
// Test whether the two type vars unify. Never commits the result.
|
|
ErrorVec canUnify(TypeId subTy, TypeId superTy);
|
|
ErrorVec canUnify(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
|
|
|
/** Attempt to unify.
|
|
* 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.
|
|
*/
|
|
void tryUnify(TypeId subTy, TypeId superTy, bool isFunctionCall = false, bool isIntersection = false);
|
|
|
|
private:
|
|
void tryUnify_(TypeId subTy, TypeId superTy, bool isFunctionCall = false, bool isIntersection = false);
|
|
void tryUnifyUnionWithType(TypeId subTy, const UnionType* uv, TypeId superTy);
|
|
void tryUnifyTypeWithUnion(TypeId subTy, TypeId superTy, const UnionType* uv, bool cacheEnabled, bool isFunctionCall);
|
|
void tryUnifyTypeWithIntersection(TypeId subTy, TypeId superTy, const IntersectionType* uv);
|
|
void tryUnifyIntersectionWithType(TypeId subTy, const IntersectionType* uv, TypeId superTy, bool cacheEnabled, bool isFunctionCall);
|
|
void tryUnifyNormalizedTypes(TypeId subTy, TypeId superTy, const NormalizedType& subNorm, const NormalizedType& superNorm, std::string reason,
|
|
std::optional<TypeError> error = std::nullopt);
|
|
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 tryUnifyScalarShape(TypeId subTy, TypeId superTy, bool reversed);
|
|
void tryUnifyWithMetatable(TypeId subTy, TypeId superTy, bool reversed);
|
|
void tryUnifyWithClass(TypeId subTy, TypeId superTy, bool reversed);
|
|
void tryUnifyNegations(TypeId subTy, TypeId superTy);
|
|
|
|
TypePackId tryApplyOverloadedFunction(TypeId function, const NormalizedFunctionType& overloads, TypePackId args);
|
|
|
|
TypeId widen(TypeId ty);
|
|
TypePackId widen(TypePackId tp);
|
|
|
|
TypeId deeplyOptional(TypeId ty, std::unordered_map<TypeId, TypeId> seen = {});
|
|
|
|
bool canCacheResult(TypeId subTy, TypeId superTy);
|
|
void cacheResult(TypeId subTy, TypeId superTy, size_t prevErrorCount);
|
|
|
|
public:
|
|
void tryUnify(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
|
|
|
private:
|
|
void tryUnify_(TypePackId subTy, TypePackId superTy, bool isFunctionCall = false);
|
|
void tryUnifyVariadics(TypePackId subTy, TypePackId superTy, bool reversed, int subOffset = 0);
|
|
|
|
void tryUnifyWithAny(TypeId subTy, TypeId anyTy);
|
|
void tryUnifyWithAny(TypePackId subTy, TypePackId anyTp);
|
|
|
|
std::optional<TypeId> findTablePropertyRespectingMeta(TypeId lhsType, Name name);
|
|
|
|
TxnLog combineLogsIntoIntersection(std::vector<TxnLog> logs);
|
|
TxnLog combineLogsIntoUnion(std::vector<TxnLog> logs);
|
|
|
|
public:
|
|
// Returns true if the type "needle" already occurs within "haystack" and reports an "infinite type error"
|
|
bool occursCheck(TypeId needle, TypeId haystack);
|
|
bool occursCheck(DenseHashSet<TypeId>& seen, TypeId needle, TypeId haystack);
|
|
bool occursCheck(TypePackId needle, TypePackId haystack);
|
|
bool occursCheck(DenseHashSet<TypePackId>& seen, TypePackId needle, TypePackId haystack);
|
|
|
|
Unifier makeChildUnifier();
|
|
|
|
void reportError(TypeError err);
|
|
LUAU_NOINLINE void reportError(Location location, TypeErrorData data);
|
|
|
|
private:
|
|
bool isNonstrictMode() const;
|
|
TypeMismatch::Context mismatchContext();
|
|
|
|
void checkChildUnifierTypeMismatch(const ErrorVec& innerErrors, TypeId wantedType, TypeId givenType);
|
|
void checkChildUnifierTypeMismatch(const ErrorVec& innerErrors, const std::string& prop, TypeId wantedType, TypeId givenType);
|
|
|
|
[[noreturn]] void ice(const std::string& message, const Location& location);
|
|
[[noreturn]] void ice(const std::string& message);
|
|
|
|
// Available after regular type pack unification errors
|
|
std::optional<int> firstPackErrorPos;
|
|
};
|
|
|
|
void promoteTypeLevels(TxnLog& log, const TypeArena* arena, TypeLevel minLevel, Scope* outerScope, bool useScope, TypePackId tp);
|
|
std::optional<TypeError> hasUnificationTooComplex(const ErrorVec& errors);
|
|
|
|
} // namespace Luau
|