Skip to content

Commit

Permalink
Sync to upstream/release/544 (#669)
Browse files Browse the repository at this point in the history
- Remove type definitions of
`utf8.nfcnormalize`/`nfdnormalize`/`graphemes` that aren't supported by
standalone Luau library
- Add `lua_costatus` to retrieve extended thread status (similar to
`coroutine.status`)
- Improve GC sweeping performance (2-10% improvement on allocation-heavy
benchmarks)
  • Loading branch information
zeux authored Sep 8, 2022
1 parent b2e357d commit ce2c3b3
Show file tree
Hide file tree
Showing 76 changed files with 1,988 additions and 794 deletions.
7 changes: 5 additions & 2 deletions Analysis/include/Luau/Anyification.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ using ScopePtr = std::shared_ptr<Scope>;
// A substitution which replaces free types by any
struct Anyification : Substitution
{
Anyification(TypeArena* arena, NotNull<Scope> scope, InternalErrorReporter* iceHandler, TypeId anyType, TypePackId anyTypePack);
Anyification(TypeArena* arena, const ScopePtr& scope, InternalErrorReporter* iceHandler, TypeId anyType, TypePackId anyTypePack);
Anyification(TypeArena* arena, NotNull<Scope> scope, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter* iceHandler, TypeId anyType,
TypePackId anyTypePack);
Anyification(TypeArena* arena, const ScopePtr& scope, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter* iceHandler, TypeId anyType,
TypePackId anyTypePack);
NotNull<Scope> scope;
NotNull<SingletonTypes> singletonTypes;
InternalErrorReporter* iceHandler;

TypeId anyType;
Expand Down
12 changes: 10 additions & 2 deletions Analysis/include/Luau/BuiltinDefinitions.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Frontend.h"
#include "Luau/Scope.h"
#include "Luau/TypeInfer.h"

namespace Luau
{

void registerBuiltinTypes(TypeChecker& typeChecker);
void registerBuiltinTypes(Frontend& frontend);

TypeId makeUnion(TypeArena& arena, std::vector<TypeId>&& types);
TypeId makeIntersection(TypeArena& arena, std::vector<TypeId>&& types);

/** Build an optional 't'
*/
TypeId makeOption(TypeChecker& typeChecker, TypeArena& arena, TypeId t);
TypeId makeOption(Frontend& frontend, TypeArena& arena, TypeId t);

/** Small utility function for building up type definitions from C++.
*/
Expand All @@ -41,12 +44,17 @@ void assignPropDocumentationSymbols(TableTypeVar::Props& props, const std::strin

std::string getBuiltinDefinitionSource();

void addGlobalBinding(TypeChecker& typeChecker, const std::string& name, TypeId ty, const std::string& packageName);
void addGlobalBinding(TypeChecker& typeChecker, const std::string& name, Binding binding);
void addGlobalBinding(TypeChecker& typeChecker, const std::string& name, TypeId ty, const std::string& packageName);
void addGlobalBinding(TypeChecker& typeChecker, const ScopePtr& scope, const std::string& name, TypeId ty, const std::string& packageName);
void addGlobalBinding(TypeChecker& typeChecker, const ScopePtr& scope, const std::string& name, Binding binding);
std::optional<Binding> tryGetGlobalBinding(TypeChecker& typeChecker, const std::string& name);
void addGlobalBinding(Frontend& frontend, const std::string& name, TypeId ty, const std::string& packageName);
void addGlobalBinding(Frontend& frontend, const std::string& name, Binding binding);
void addGlobalBinding(Frontend& frontend, const ScopePtr& scope, const std::string& name, TypeId ty, const std::string& packageName);
void addGlobalBinding(Frontend& frontend, const ScopePtr& scope, const std::string& name, Binding binding);
std::optional<Binding> tryGetGlobalBinding(Frontend& frontend, const std::string& name);
Binding* tryGetGlobalBindingRef(TypeChecker& typeChecker, const std::string& name);
TypeId getGlobalBinding(Frontend& frontend, const std::string& name);
TypeId getGlobalBinding(TypeChecker& typeChecker, const std::string& name);

} // namespace Luau
7 changes: 5 additions & 2 deletions Analysis/include/Luau/ConstraintGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Luau
struct Scope;
using ScopePtr = std::shared_ptr<Scope>;

struct DcrLogger;

struct ConstraintGraphBuilder
{
// A list of all the scopes in the module. This vector holds ownership of the
Expand All @@ -30,7 +32,7 @@ struct ConstraintGraphBuilder

ModuleName moduleName;
ModulePtr module;
SingletonTypes& singletonTypes;
NotNull<SingletonTypes> singletonTypes;
const NotNull<TypeArena> arena;
// The root scope of the module we're generating constraints for.
// This is null when the CGB is initially constructed.
Expand Down Expand Up @@ -58,9 +60,10 @@ struct ConstraintGraphBuilder
const NotNull<InternalErrorReporter> ice;

ScopePtr globalScope;
DcrLogger* logger;

ConstraintGraphBuilder(const ModuleName& moduleName, ModulePtr module, TypeArena* arena, NotNull<ModuleResolver> moduleResolver,
NotNull<InternalErrorReporter> ice, const ScopePtr& globalScope);
NotNull<SingletonTypes> singletonTypes, NotNull<InternalErrorReporter> ice, const ScopePtr& globalScope, DcrLogger* logger);

/**
* Fabricates a new free type belonging to a given scope.
Expand Down
11 changes: 7 additions & 4 deletions Analysis/include/Luau/ConstraintSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#include "Luau/Error.h"
#include "Luau/Variant.h"
#include "Luau/Constraint.h"
#include "Luau/ConstraintSolverLogger.h"
#include "Luau/TypeVar.h"
#include "Luau/ToString.h"

#include <vector>

namespace Luau
{

struct DcrLogger;

// TypeId, TypePackId, or Constraint*. It is impossible to know which, but we
// never dereference this pointer.
using BlockedConstraintId = const void*;
Expand Down Expand Up @@ -40,6 +42,7 @@ struct HashInstantiationSignature
struct ConstraintSolver
{
TypeArena* arena;
NotNull<SingletonTypes> singletonTypes;
InternalErrorReporter iceReporter;
// The entire set of constraints that the solver is trying to resolve.
std::vector<NotNull<Constraint>> constraints;
Expand Down Expand Up @@ -69,10 +72,10 @@ struct ConstraintSolver
NotNull<ModuleResolver> moduleResolver;
std::vector<RequireCycle> requireCycles;

ConstraintSolverLogger logger;
DcrLogger* logger;

explicit ConstraintSolver(TypeArena* arena, NotNull<Scope> rootScope, ModuleName moduleName, NotNull<ModuleResolver> moduleResolver,
std::vector<RequireCycle> requireCycles);
explicit ConstraintSolver(TypeArena* arena, NotNull<SingletonTypes> singletonTypes, NotNull<Scope> rootScope, ModuleName moduleName,
NotNull<ModuleResolver> moduleResolver, std::vector<RequireCycle> requireCycles, DcrLogger* logger);

/**
* Attempts to dispatch all pending constraints and reach a type solution
Expand Down
29 changes: 0 additions & 29 deletions Analysis/include/Luau/ConstraintSolverLogger.h

This file was deleted.

131 changes: 131 additions & 0 deletions Analysis/include/Luau/DcrLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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/NotNull.h"
#include "Luau/Scope.h"
#include "Luau/ToString.h"
#include "Luau/Error.h"
#include "Luau/Variant.h"

#include <optional>
#include <string>
#include <vector>

namespace Luau
{

struct ErrorSnapshot
{
std::string message;
Location location;
};

struct BindingSnapshot
{
std::string typeId;
std::string typeString;
Location location;
};

struct TypeBindingSnapshot
{
std::string typeId;
std::string typeString;
};

struct ConstraintGenerationLog
{
std::string source;
std::unordered_map<std::string, Location> constraintLocations;
std::vector<ErrorSnapshot> errors;
};

struct ScopeSnapshot
{
std::unordered_map<Name, BindingSnapshot> bindings;
std::unordered_map<Name, TypeBindingSnapshot> typeBindings;
std::unordered_map<Name, TypeBindingSnapshot> typePackBindings;
std::vector<ScopeSnapshot> children;
};

enum class ConstraintBlockKind
{
TypeId,
TypePackId,
ConstraintId,
};

struct ConstraintBlock
{
ConstraintBlockKind kind;
std::string stringification;
};

struct ConstraintSnapshot
{
std::string stringification;
std::vector<ConstraintBlock> blocks;
};

struct BoundarySnapshot
{
std::unordered_map<std::string, ConstraintSnapshot> constraints;
ScopeSnapshot rootScope;
};

struct StepSnapshot
{
std::string currentConstraint;
bool forced;
std::unordered_map<std::string, ConstraintSnapshot> unsolvedConstraints;
ScopeSnapshot rootScope;
};

struct TypeSolveLog
{
BoundarySnapshot initialState;
std::vector<StepSnapshot> stepStates;
BoundarySnapshot finalState;
};

struct TypeCheckLog
{
std::vector<ErrorSnapshot> errors;
};

using ConstraintBlockTarget = Variant<TypeId, TypePackId, NotNull<const Constraint>>;

struct DcrLogger
{
std::string compileOutput();

void captureSource(std::string source);
void captureGenerationError(const TypeError& error);
void captureConstraintLocation(NotNull<const Constraint> constraint, Location location);

void pushBlock(NotNull<const Constraint> constraint, TypeId block);
void pushBlock(NotNull<const Constraint> constraint, TypePackId block);
void pushBlock(NotNull<const Constraint> constraint, NotNull<const Constraint> block);
void popBlock(TypeId block);
void popBlock(TypePackId block);
void popBlock(NotNull<const Constraint> block);

void captureInitialSolverState(const Scope* rootScope, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);
StepSnapshot prepareStepSnapshot(const Scope* rootScope, NotNull<const Constraint> current, bool force, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);
void commitStepSnapshot(StepSnapshot snapshot);
void captureFinalSolverState(const Scope* rootScope, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);

void captureTypeCheckError(const TypeError& error);
private:
ConstraintGenerationLog generationLog;
std::unordered_map<NotNull<const Constraint>, std::vector<ConstraintBlockTarget>> constraintBlocks;
TypeSolveLog solveLog;
TypeCheckLog checkLog;

ToStringOptions opts;

std::vector<ConstraintBlock> snapshotBlocks(NotNull<const Constraint> constraint);
};

} // namespace Luau
1 change: 1 addition & 0 deletions Analysis/include/Luau/Documentation.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// 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"
Expand Down
3 changes: 3 additions & 0 deletions Analysis/include/Luau/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ struct Frontend
ScopePtr globalScope;

public:
SingletonTypes singletonTypes_;
const NotNull<SingletonTypes> singletonTypes;

FileResolver* fileResolver;
FrontendModuleResolver moduleResolver;
FrontendModuleResolver moduleResolverForAutocomplete;
Expand Down
12 changes: 12 additions & 0 deletions Analysis/include/Luau/JsonEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <type_traits>
#include <string>
#include <optional>
#include <unordered_map>
#include <vector>

#include "Luau/NotNull.h"
Expand Down Expand Up @@ -232,4 +233,15 @@ void write(JsonEmitter& emitter, const std::optional<T>& v)
emitter.writeRaw("null");
}

template<typename T>
void write(JsonEmitter& emitter, const std::unordered_map<std::string, T>& map)
{
ObjectEmitter o = emitter.writeObject();

for (const auto& [k, v] : map)
o.writePair(k, v);

o.finish();
}

} // namespace Luau::Json
2 changes: 1 addition & 1 deletion Analysis/include/Luau/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct Module

// Once a module has been typechecked, we clone its public interface into a separate arena.
// This helps us to force TypeVar ownership into a DAG rather than a DCG.
void clonePublicInterface(InternalErrorReporter& ice);
void clonePublicInterface(NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
};

} // namespace Luau
22 changes: 13 additions & 9 deletions Analysis/include/Luau/Normalize.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once

#include "Luau/Module.h"
#include "Luau/NotNull.h"
Expand All @@ -12,17 +13,20 @@ namespace Luau
struct InternalErrorReporter;
struct Module;
struct Scope;
struct SingletonTypes;

using ModulePtr = std::shared_ptr<Module>;

bool isSubtype(TypeId subTy, TypeId superTy, NotNull<Scope> scope, InternalErrorReporter& ice);
bool isSubtype(TypePackId subTy, TypePackId superTy, NotNull<Scope> scope, InternalErrorReporter& ice);

std::pair<TypeId, bool> normalize(TypeId ty, NotNull<Scope> scope, TypeArena& arena, InternalErrorReporter& ice);
std::pair<TypeId, bool> normalize(TypeId ty, NotNull<Module> module, InternalErrorReporter& ice);
std::pair<TypeId, bool> normalize(TypeId ty, const ModulePtr& module, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(TypePackId ty, NotNull<Scope> scope, TypeArena& arena, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(TypePackId ty, NotNull<Module> module, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(TypePackId ty, const ModulePtr& module, InternalErrorReporter& ice);
bool isSubtype(TypeId subTy, TypeId superTy, NotNull<Scope> scope, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
bool isSubtype(TypePackId subTy, TypePackId superTy, NotNull<Scope> scope, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);

std::pair<TypeId, bool> normalize(
TypeId ty, NotNull<Scope> scope, TypeArena& arena, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
std::pair<TypeId, bool> normalize(TypeId ty, NotNull<Module> module, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
std::pair<TypeId, bool> normalize(TypeId ty, const ModulePtr& module, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(
TypePackId ty, NotNull<Scope> scope, TypeArena& arena, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(TypePackId ty, NotNull<Module> module, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
std::pair<TypePackId, bool> normalize(TypePackId ty, const ModulePtr& module, NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);

} // namespace Luau
2 changes: 2 additions & 0 deletions Analysis/include/Luau/TypeArena.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct TypeArena
TypeId freshType(TypeLevel level);
TypeId freshType(Scope* scope);

TypePackId freshTypePack(Scope* scope);

TypePackId addTypePack(std::initializer_list<TypeId> types);
TypePackId addTypePack(std::vector<TypeId> types, std::optional<TypePackId> tail = {});
TypePackId addTypePack(TypePack pack);
Expand Down
6 changes: 5 additions & 1 deletion Analysis/include/Luau/TypeChecker2.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

#include "Luau/Ast.h"
#include "Luau/Module.h"
#include "Luau/NotNull.h"

namespace Luau
{

void check(const SourceModule& sourceModule, Module* module);
struct DcrLogger;
struct SingletonTypes;

void check(NotNull<SingletonTypes> singletonTypes, DcrLogger* logger, const SourceModule& sourceModule, Module* module);

} // namespace Luau
Loading

0 comments on commit ce2c3b3

Please sign in to comment.