Skip to content

Commit

Permalink
Sync to upstream/release/533
Browse files Browse the repository at this point in the history
  • Loading branch information
zeux committed Jun 24, 2022
1 parent 88b3984 commit 6d14bda
Show file tree
Hide file tree
Showing 54 changed files with 970 additions and 693 deletions.
14 changes: 10 additions & 4 deletions Analysis/include/Luau/Constraint.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Location.h"
#include "Luau/NotNull.h"
#include "Luau/Variant.h"

#include <string>
#include <memory>
#include <vector>

Expand Down Expand Up @@ -47,18 +47,24 @@ struct InstantiationConstraint
TypeId superType;
};

using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint>;
// name(namedType) = name
struct NameConstraint
{
TypeId namedType;
std::string name;
};

using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint, NameConstraint>;
using ConstraintPtr = std::unique_ptr<struct Constraint>;

struct Constraint
{
Constraint(ConstraintV&& c, Location location);
explicit Constraint(ConstraintV&& c);

Constraint(const Constraint&) = delete;
Constraint& operator=(const Constraint&) = delete;

ConstraintV c;
Location location;
std::vector<NotNull<Constraint>> dependencies;
};

Expand Down
41 changes: 25 additions & 16 deletions Analysis/include/Luau/ConstraintGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,7 @@
namespace Luau
{

struct Scope2
{
// The parent scope of this scope. Null if there is no parent (i.e. this
// is the module-level scope).
Scope2* parent = nullptr;
// All the children of this scope.
std::vector<Scope2*> children;
std::unordered_map<Symbol, TypeId> bindings; // TODO: I think this can be a DenseHashMap
TypePackId returnType;
// All constraints belonging to this scope.
std::vector<ConstraintPtr> constraints;

std::optional<TypeId> lookup(Symbol sym);
};
struct Scope2;

struct ConstraintGraphBuilder
{
Expand All @@ -47,6 +34,10 @@ struct ConstraintGraphBuilder
// A mapping of AST node to TypePackId.
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
DenseHashMap<const AstExpr*, TypeId> astOriginalCallTypes{nullptr};
// Types resolved from type annotations. Analogous to astTypes.
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
// Type packs resolved from type annotations. Analogous to astTypePacks.
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};

explicit ConstraintGraphBuilder(TypeArena* arena);

Expand All @@ -73,9 +64,8 @@ struct ConstraintGraphBuilder
* Adds a new constraint with no dependencies to a given scope.
* @param scope the scope to add the constraint to. Must not be null.
* @param cv the constraint variant to add.
* @param location the location to attribute to the constraint.
*/
void addConstraint(Scope2* scope, ConstraintV cv, Location location);
void addConstraint(Scope2* scope, ConstraintV cv);

/**
* Adds a constraint to a given scope.
Expand All @@ -99,6 +89,7 @@ struct ConstraintGraphBuilder
void visit(Scope2* scope, AstStatReturn* ret);
void visit(Scope2* scope, AstStatAssign* assign);
void visit(Scope2* scope, AstStatIf* ifStatement);
void visit(Scope2* scope, AstStatTypeAlias* alias);

TypePackId checkExprList(Scope2* scope, const AstArray<AstExpr*>& exprs);

Expand All @@ -124,6 +115,24 @@ struct ConstraintGraphBuilder
* @param fn the function expression to check.
*/
void checkFunctionBody(Scope2* scope, AstExprFunction* fn);

/**
* Resolves a type from its AST annotation.
* @param scope the scope that the type annotation appears within.
* @param ty the AST annotation to resolve.
* @return the type of the AST annotation.
**/
TypeId resolveType(Scope2* scope, AstType* ty);

/**
* Resolves a type pack from its AST annotation.
* @param scope the scope that the type annotation appears within.
* @param tp the AST annotation to resolve.
* @return the type pack of the AST annotation.
**/
TypePackId resolveTypePack(Scope2* scope, AstTypePack* tp);

TypePackId resolveTypePack(Scope2* scope, const AstTypeList& list);
};

/**
Expand Down
5 changes: 3 additions & 2 deletions Analysis/include/Luau/ConstraintSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct ConstraintSolver
bool tryDispatch(const PackSubtypeConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const GeneralizationConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const InstantiationConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const NameConstraint& c, NotNull<const Constraint> constraint);

void block(NotNull<const Constraint> target, NotNull<const Constraint> constraint);
/**
Expand Down Expand Up @@ -85,15 +86,15 @@ struct ConstraintSolver
* @param subType the sub-type to unify.
* @param superType the super-type to unify.
*/
void unify(TypeId subType, TypeId superType, Location location);
void unify(TypeId subType, TypeId superType);

/**
* Creates a new Unifier and performs a single unification operation. Commits
* the result.
* @param subPack the sub-type pack to unify.
* @param superPack the super-type pack to unify.
*/
void unify(TypePackId subPack, TypePackId superPack, Location location);
void unify(TypePackId subPack, TypePackId superPack);

private:
/**
Expand Down
4 changes: 3 additions & 1 deletion Analysis/include/Luau/ConstraintSolverLogger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details

#include "Luau/ConstraintGraphBuilder.h"
#include "Luau/Constraint.h"
#include "Luau/NotNull.h"
#include "Luau/Scope.h"
#include "Luau/ToString.h"

#include <optional>
Expand Down
45 changes: 39 additions & 6 deletions Analysis/include/Luau/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ struct GenericError
bool operator==(const GenericError& rhs) const;
};

struct InternalError
{
std::string message;

bool operator==(const InternalError& rhs) const;
};

struct CannotCallNonFunction
{
TypeId ty;
Expand Down Expand Up @@ -293,12 +300,12 @@ struct NormalizationTooComplex
}
};

using TypeErrorData =
Variant<TypeMismatch, UnknownSymbol, UnknownProperty, NotATable, CannotExtendTable, OnlyTablesCanHaveMethods, DuplicateTypeDefinition,
CountMismatch, FunctionDoesNotTakeSelf, FunctionRequiresSelf, OccursCheckFailed, UnknownRequire, IncorrectGenericParameterCount, SyntaxError,
CodeTooComplex, UnificationTooComplex, UnknownPropButFoundLikeProp, GenericError, CannotCallNonFunction, ExtraInformation, DeprecatedApiUsed,
ModuleHasCyclicDependency, IllegalRequire, FunctionExitsWithoutReturning, DuplicateGenericParameter, CannotInferBinaryOperation,
MissingProperties, SwappedGenericTypeParameter, OptionalValueAccess, MissingUnionProperty, TypesAreUnrelated, NormalizationTooComplex>;
using TypeErrorData = Variant<TypeMismatch, UnknownSymbol, UnknownProperty, NotATable, CannotExtendTable, OnlyTablesCanHaveMethods,
DuplicateTypeDefinition, CountMismatch, FunctionDoesNotTakeSelf, FunctionRequiresSelf, OccursCheckFailed, UnknownRequire,
IncorrectGenericParameterCount, SyntaxError, CodeTooComplex, UnificationTooComplex, UnknownPropButFoundLikeProp, GenericError, InternalError,
CannotCallNonFunction, ExtraInformation, DeprecatedApiUsed, ModuleHasCyclicDependency, IllegalRequire, FunctionExitsWithoutReturning,
DuplicateGenericParameter, CannotInferBinaryOperation, MissingProperties, SwappedGenericTypeParameter, OptionalValueAccess, MissingUnionProperty,
TypesAreUnrelated, NormalizationTooComplex>;

struct TypeError
{
Expand Down Expand Up @@ -339,7 +346,13 @@ T* get(TypeError& e)

using ErrorVec = std::vector<TypeError>;

struct TypeErrorToStringOptions
{
FileResolver* fileResolver = nullptr;
};

std::string toString(const TypeError& error);
std::string toString(const TypeError& error, TypeErrorToStringOptions options);

bool containsParseErrorName(const TypeError& error);

Expand All @@ -356,4 +369,24 @@ struct InternalErrorReporter
[[noreturn]] void ice(const std::string& message);
};

class InternalCompilerError : public std::exception {
public:
explicit InternalCompilerError(const std::string& message, const std::string& moduleName)
: message(message)
, moduleName(moduleName)
{
}
explicit InternalCompilerError(const std::string& message, const std::string& moduleName, const Location& location)
: message(message)
, moduleName(moduleName)
, location(location)
{
}
virtual const char* what() const throw();

const std::string message;
const std::string moduleName;
const std::optional<Location> location;
};

} // namespace Luau
1 change: 1 addition & 0 deletions Analysis/include/Luau/IostreamHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ std::ostream& operator<<(std::ostream& lhs, const OccursCheckFailed& error);
std::ostream& operator<<(std::ostream& lhs, const UnknownRequire& error);
std::ostream& operator<<(std::ostream& lhs, const UnknownPropButFoundLikeProp& e);
std::ostream& operator<<(std::ostream& lhs, const GenericError& error);
std::ostream& operator<<(std::ostream& lhs, const InternalError& error);
std::ostream& operator<<(std::ostream& lhs, const FunctionExitsWithoutReturning& error);
std::ostream& operator<<(std::ostream& lhs, const MissingProperties& error);
std::ostream& operator<<(std::ostream& lhs, const IllegalRequire& error);
Expand Down
9 changes: 7 additions & 2 deletions Analysis/include/Luau/Module.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// 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/FileResolver.h"
#include "Luau/ParseOptions.h"
#include "Luau/Error.h"
#include "Luau/ParseResult.h"
#include "Luau/Scope.h"
#include "Luau/TypeArena.h"

#include <memory>
Expand All @@ -19,7 +20,9 @@ struct Module;

using ScopePtr = std::shared_ptr<struct Scope>;
using ModulePtr = std::shared_ptr<Module>;
struct Scope2;

class AstType;
class AstTypePack;

/// Root of the AST of a parsed source file
struct SourceModule
Expand Down Expand Up @@ -73,6 +76,8 @@ struct Module
DenseHashMap<const AstExpr*, TypeId> astExpectedTypes{nullptr};
DenseHashMap<const AstExpr*, TypeId> astOriginalCallTypes{nullptr};
DenseHashMap<const AstExpr*, TypeId> astOverloadResolvedTypes{nullptr};
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};

std::unordered_map<Name, TypeId> declaredGlobals;
ErrorVec errors;
Expand Down
4 changes: 2 additions & 2 deletions Analysis/include/Luau/Normalize.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Luau

struct InternalErrorReporter;

bool isSubtype(TypeId superTy, TypeId subTy, InternalErrorReporter& ice);
bool isSubtype(TypePackId superTy, TypePackId subTy, InternalErrorReporter& ice);
bool isSubtype(TypeId subTy, TypeId superTy, InternalErrorReporter& ice);
bool isSubtype(TypePackId subTy, TypePackId superTy, InternalErrorReporter& ice);

std::pair<TypeId, bool> normalize(TypeId ty, TypeArena& arena, InternalErrorReporter& ice);
std::pair<TypeId, bool> normalize(TypeId ty, const ModulePtr& module, InternalErrorReporter& ice);
Expand Down
15 changes: 2 additions & 13 deletions Analysis/include/Luau/RecursionCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <stdexcept>
#include <exception>

LUAU_FASTFLAG(LuauRecursionLimitException);

namespace Luau
{

Expand Down Expand Up @@ -39,21 +37,12 @@ struct RecursionCounter

struct RecursionLimiter : RecursionCounter
{
// TODO: remove ctx after LuauRecursionLimitException is removed
RecursionLimiter(int* count, int limit, const char* ctx)
RecursionLimiter(int* count, int limit)
: RecursionCounter(count)
{
LUAU_ASSERT(ctx);
if (limit > 0 && *count > limit)
{
if (FFlag::LuauRecursionLimitException)
throw RecursionLimitException();
else
{
std::string m = "Internal recursion counter limit exceeded: ";
m += ctx;
throw std::runtime_error(m);
}
throw RecursionLimitException();
}
}
};
Expand Down
18 changes: 18 additions & 0 deletions Analysis/include/Luau/Scope.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Constraint.h"
#include "Luau/Location.h"
#include "Luau/TypeVar.h"

Expand Down Expand Up @@ -64,4 +65,21 @@ struct Scope
std::unordered_map<Name, TypePackId> typeAliasTypePackParameters;
};

struct Scope2
{
// The parent scope of this scope. Null if there is no parent (i.e. this
// is the module-level scope).
Scope2* parent = nullptr;
// All the children of this scope.
std::vector<Scope2*> children;
std::unordered_map<Symbol, TypeId> bindings; // TODO: I think this can be a DenseHashMap
std::unordered_map<Name, TypeId> typeBindings;
TypePackId returnType;
// All constraints belonging to this scope.
std::vector<ConstraintPtr> constraints;

std::optional<TypeId> lookup(Symbol sym);
std::optional<TypeId> lookupTypeBinding(const Name& name);
};

} // namespace Luau
1 change: 0 additions & 1 deletion Analysis/include/Luau/TypeVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ struct FunctionTypeVar
bool hasSelf;
Tags tags;
bool hasNoGenerics = false;
bool generalized = false;
};

enum class TableState
Expand Down
1 change: 1 addition & 0 deletions Analysis/include/Luau/Unifiable.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct Generic
explicit Generic(const Name& name);
explicit Generic(Scope2* scope);
Generic(TypeLevel level, const Name& name);
Generic(Scope2* scope, const Name& name);

int index;
TypeLevel level;
Expand Down
4 changes: 0 additions & 4 deletions Analysis/include/Luau/Unifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ struct Unifier
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);

TypeId widen(TypeId ty);
TypePackId widen(TypePackId tp);
Expand Down
2 changes: 1 addition & 1 deletion Analysis/include/Luau/VisitTypeVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct GenericTypeVarVisitor

void traverse(TypeId ty)
{
RecursionLimiter limiter{&recursionCounter, FInt::LuauVisitRecursionLimit, "TypeVarVisitor"};
RecursionLimiter limiter{&recursionCounter, FInt::LuauVisitRecursionLimit};

if (visit_detail::hasSeen(seen, ty))
{
Expand Down
4 changes: 2 additions & 2 deletions Analysis/src/Clone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ TypePackId clone(TypePackId tp, TypeArena& dest, CloneState& cloneState)
if (tp->persistent)
return tp;

RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit, "cloning TypePackId");
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);

TypePackId& res = cloneState.seenTypePacks[tp];

Expand All @@ -335,7 +335,7 @@ TypeId clone(TypeId typeId, TypeArena& dest, CloneState& cloneState)
if (typeId->persistent)
return typeId;

RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit, "cloning TypeId");
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);

TypeId& res = cloneState.seenTypes[typeId];

Expand Down
Loading

0 comments on commit 6d14bda

Please sign in to comment.