mirror of
https://github.com/luau-lang/luau.git
synced 2025-01-05 19:09:11 +00:00
68bd1b2349
# What's changed? * Improved the actual message for the type errors for `cannot call non-function` when attempting to call a union of functions/callable tables. The error now correctly explains the issue is an inability to determine the return type of the call in this situation. * Resolve an issue where tables and metatables were not correctly being cloned during instantiation (fixes #1176). * Refactor `luaM_getnextgcopage` to `luaM_getnextpage` (generally removing `gco` prefix where appropriate). * Optimize `table.move` between tables for large ranges of elements. * Reformat a bunch of code automatically using `clang-format`. ### New Type Solver * Clean up minimally-used or unused constraints in the constraint solver (`InstantiationConstraint`, `SetOpConstraint`, `SingletonOrTopTypeConstraint`). * Add a builtin `singleton` type family to replace `SingletonOrTopTypeConstraint` when inferring refinements. * Fixed a crash involving type path reasoning by recording when type family reduction has taken place in the path. * Improved constraint ordering by blocking on unreduced types families that are not yet proven uninhabitable. * Improved the handling of `SetIndexerConstraints` for both better inference quality and to resolve crashes. * Fix a crash when normalizing cyclic unions of intersections. * Fix a crash when normalizing an intersection with the negation of `unknown`. * Fix a number of crashes caused by missing `follow` calls on `TypeId`s. * Changed type family reduction to correctly use a semantic notion of uninhabited types, rather than checking for `never` types specifically. * Refactor the `union` and `intersect` type families to be variadic. ### Native Code Generation * Improve translation for userdata key get/set and userdata/vector namecall. * Provide `[top level]` and `[anonymous]` as function names to `FunctionStats` as appropriate when no function name is available. * Disable unwind support on Android platforms since it is unsupported. * --- ### Internal Contributors Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> --------- Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Vighnesh <vvijay@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: David Cope <dcope@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
309 lines
9 KiB
C++
309 lines
9 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/Ast.h" // Used for some of the enumerations
|
|
#include "Luau/DenseHash.h"
|
|
#include "Luau/NotNull.h"
|
|
#include "Luau/Variant.h"
|
|
#include "Luau/TypeFwd.h"
|
|
|
|
#include <string>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
namespace Luau
|
|
{
|
|
|
|
enum class ValueContext;
|
|
struct Scope;
|
|
|
|
// if resultType is a freeType, assignmentType <: freeType <: resultType bounds
|
|
struct EqualityConstraint
|
|
{
|
|
TypeId resultType;
|
|
TypeId assignmentType;
|
|
};
|
|
|
|
// subType <: superType
|
|
struct SubtypeConstraint
|
|
{
|
|
TypeId subType;
|
|
TypeId superType;
|
|
};
|
|
|
|
// subPack <: superPack
|
|
struct PackSubtypeConstraint
|
|
{
|
|
TypePackId subPack;
|
|
TypePackId superPack;
|
|
|
|
// HACK!! TODO clip.
|
|
// We need to know which of `PackSubtypeConstraint` are emitted from `AstStatReturn` vs any others.
|
|
// Then we force these specific `PackSubtypeConstraint` to only dispatch in the order of the `return`s.
|
|
bool returns = false;
|
|
};
|
|
|
|
// generalizedType ~ gen sourceType
|
|
struct GeneralizationConstraint
|
|
{
|
|
TypeId generalizedType;
|
|
TypeId sourceType;
|
|
|
|
std::vector<TypeId> interiorTypes;
|
|
};
|
|
|
|
// variables ~ iterate iterator
|
|
// Unpack the iterator, figure out what types it iterates over, and bind those types to variables.
|
|
struct IterableConstraint
|
|
{
|
|
TypePackId iterator;
|
|
TypePackId variables;
|
|
|
|
const AstNode* nextAstFragment;
|
|
DenseHashMap<const AstNode*, TypeId>* astForInNextTypes;
|
|
};
|
|
|
|
// name(namedType) = name
|
|
struct NameConstraint
|
|
{
|
|
TypeId namedType;
|
|
std::string name;
|
|
bool synthetic = false;
|
|
std::vector<TypeId> typeParameters;
|
|
std::vector<TypePackId> typePackParameters;
|
|
};
|
|
|
|
// target ~ inst target
|
|
struct TypeAliasExpansionConstraint
|
|
{
|
|
// Must be a PendingExpansionType.
|
|
TypeId target;
|
|
};
|
|
|
|
struct FunctionCallConstraint
|
|
{
|
|
TypeId fn;
|
|
TypePackId argsPack;
|
|
TypePackId result;
|
|
class AstExprCall* callSite = nullptr;
|
|
std::vector<std::optional<TypeId>> discriminantTypes;
|
|
|
|
// When we dispatch this constraint, we update the key at this map to record
|
|
// the overload that we selected.
|
|
DenseHashMap<const AstNode*, TypeId>* astOverloadResolvedTypes = nullptr;
|
|
};
|
|
|
|
// function_check fn argsPack
|
|
//
|
|
// If fn is a function type and argsPack is a partially solved
|
|
// pack of arguments to be supplied to the function, propagate the argument
|
|
// types of fn into the types of argsPack. This is used to implement
|
|
// bidirectional inference of lambda arguments.
|
|
struct FunctionCheckConstraint
|
|
{
|
|
TypeId fn;
|
|
TypePackId argsPack;
|
|
|
|
class AstExprCall* callSite = nullptr;
|
|
NotNull<DenseHashMap<const AstExpr*, TypeId>> astTypes;
|
|
NotNull<DenseHashMap<const AstExpr*, TypeId>> astExpectedTypes;
|
|
};
|
|
|
|
// prim FreeType ExpectedType PrimitiveType
|
|
//
|
|
// FreeType is bounded below by the singleton type and above by PrimitiveType
|
|
// initially. When this constraint is resolved, it will check that the bounds
|
|
// of the free type are well-formed by subtyping.
|
|
//
|
|
// If they are not well-formed, then FreeType is replaced by its lower bound
|
|
//
|
|
// If they are well-formed and ExpectedType is potentially a singleton (an
|
|
// actual singleton or a union that contains a singleton),
|
|
// then FreeType is replaced by its lower bound
|
|
//
|
|
// else FreeType is replaced by PrimitiveType
|
|
struct PrimitiveTypeConstraint
|
|
{
|
|
TypeId freeType;
|
|
|
|
// potentially gets used to force the lower bound?
|
|
std::optional<TypeId> expectedType;
|
|
|
|
// the primitive type to check against
|
|
TypeId primitiveType;
|
|
};
|
|
|
|
// result ~ hasProp type "prop_name"
|
|
//
|
|
// If the subject is a table, bind the result to the named prop. If the table
|
|
// has an indexer, bind it to the index result type. If the subject is a union,
|
|
// bind the result to the union of its constituents' properties.
|
|
//
|
|
// It would be nice to get rid of this constraint and someday replace it with
|
|
//
|
|
// T <: {p: X}
|
|
//
|
|
// Where {} describes an inexact shape type.
|
|
struct HasPropConstraint
|
|
{
|
|
TypeId resultType;
|
|
TypeId subjectType;
|
|
std::string prop;
|
|
ValueContext context;
|
|
|
|
// We want to track if this `HasPropConstraint` comes from a conditional.
|
|
// If it does, we're going to change the behavior of property look-up a bit.
|
|
// In particular, we're going to return `unknownType` for property lookups
|
|
// on `table` or inexact table types where the property is not present.
|
|
//
|
|
// This allows us to refine table types to have additional properties
|
|
// without reporting errors in typechecking on the property tests.
|
|
bool inConditional = false;
|
|
|
|
// HACK: We presently need types like true|false or string|"hello" when
|
|
// deciding whether a particular literal expression should have a singleton
|
|
// type. This boolean is set to true when extracting the property type of a
|
|
// value that may be a union of tables.
|
|
//
|
|
// For example, in the following code fragment, we want the lookup of the
|
|
// success property to yield true|false when extracting an expectedType in
|
|
// this expression:
|
|
//
|
|
// type Result<T, E> = {success:true, result: T} | {success:false, error: E}
|
|
//
|
|
// local r: Result<number, string> = {success=true, result=9}
|
|
//
|
|
// If we naively simplify the expectedType to boolean, we will erroneously
|
|
// compute the type boolean for the success property of the table literal.
|
|
// This causes type checking to fail.
|
|
bool suppressSimplification = false;
|
|
};
|
|
|
|
// result ~ setProp subjectType ["prop", "prop2", ...] propType
|
|
//
|
|
// If the subject is a table or table-like thing that already has the named
|
|
// property chain, we unify propType with that existing property type.
|
|
//
|
|
// If the subject is a free table, we augment it in place.
|
|
//
|
|
// If the subject is an unsealed table, result is an augmented table that
|
|
// includes that new prop.
|
|
struct SetPropConstraint
|
|
{
|
|
TypeId resultType;
|
|
TypeId subjectType;
|
|
std::vector<std::string> path;
|
|
TypeId propType;
|
|
};
|
|
|
|
// resultType ~ hasIndexer subjectType indexType
|
|
//
|
|
// If the subject type is a table or table-like thing that supports indexing,
|
|
// populate the type result with the result type of such an index operation.
|
|
//
|
|
// If the subject is not indexable, resultType is bound to errorType.
|
|
struct HasIndexerConstraint
|
|
{
|
|
TypeId resultType;
|
|
TypeId subjectType;
|
|
TypeId indexType;
|
|
};
|
|
|
|
// result ~ setIndexer subjectType indexType propType
|
|
//
|
|
// If the subject is a table or table-like thing that already has an indexer,
|
|
// unify its indexType and propType with those from this constraint.
|
|
//
|
|
// If the table is a free or unsealed table, we augment it with a new indexer.
|
|
struct SetIndexerConstraint
|
|
{
|
|
TypeId subjectType;
|
|
TypeId indexType;
|
|
TypeId propType;
|
|
};
|
|
|
|
// resultType ~ unpack sourceTypePack
|
|
//
|
|
// Similar to PackSubtypeConstraint, but with one important difference: If the
|
|
// sourcePack is blocked, this constraint blocks.
|
|
struct UnpackConstraint
|
|
{
|
|
TypePackId resultPack;
|
|
TypePackId sourcePack;
|
|
|
|
// UnpackConstraint is sometimes used to resolve the types of assignments.
|
|
// When this is the case, any LocalTypes in resultPack can have their
|
|
// domains extended by the corresponding type from sourcePack.
|
|
bool resultIsLValue = false;
|
|
};
|
|
|
|
// resultType ~ unpack sourceType
|
|
//
|
|
// The same as UnpackConstraint, but specialized for a pair of types as opposed to packs.
|
|
struct Unpack1Constraint
|
|
{
|
|
TypeId resultType;
|
|
TypeId sourceType;
|
|
|
|
// UnpackConstraint is sometimes used to resolve the types of assignments.
|
|
// When this is the case, any LocalTypes in resultPack can have their
|
|
// domains extended by the corresponding type from sourcePack.
|
|
bool resultIsLValue = false;
|
|
};
|
|
|
|
// ty ~ reduce ty
|
|
//
|
|
// Try to reduce ty, if it is a TypeFamilyInstanceType. Otherwise, do nothing.
|
|
struct ReduceConstraint
|
|
{
|
|
TypeId ty;
|
|
};
|
|
|
|
// tp ~ reduce tp
|
|
//
|
|
// Analogous to ReduceConstraint, but for type packs.
|
|
struct ReducePackConstraint
|
|
{
|
|
TypePackId tp;
|
|
};
|
|
|
|
using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, IterableConstraint, NameConstraint,
|
|
TypeAliasExpansionConstraint, FunctionCallConstraint, FunctionCheckConstraint, PrimitiveTypeConstraint, HasPropConstraint, SetPropConstraint,
|
|
HasIndexerConstraint, SetIndexerConstraint, UnpackConstraint, Unpack1Constraint, ReduceConstraint, ReducePackConstraint, EqualityConstraint>;
|
|
|
|
struct Constraint
|
|
{
|
|
Constraint(NotNull<Scope> scope, const Location& location, ConstraintV&& c);
|
|
|
|
Constraint(const Constraint&) = delete;
|
|
Constraint& operator=(const Constraint&) = delete;
|
|
|
|
NotNull<Scope> scope;
|
|
Location location;
|
|
ConstraintV c;
|
|
|
|
std::vector<NotNull<Constraint>> dependencies;
|
|
|
|
DenseHashSet<TypeId> getFreeTypes() const;
|
|
};
|
|
|
|
using ConstraintPtr = std::unique_ptr<Constraint>;
|
|
|
|
inline Constraint& asMutable(const Constraint& c)
|
|
{
|
|
return const_cast<Constraint&>(c);
|
|
}
|
|
|
|
template<typename T>
|
|
T* getMutable(Constraint& c)
|
|
{
|
|
return ::Luau::get_if<T>(&c.c);
|
|
}
|
|
|
|
template<typename T>
|
|
const T* get(const Constraint& c)
|
|
{
|
|
return getMutable<T>(asMutable(c));
|
|
}
|
|
|
|
} // namespace Luau
|