Skip to content

Commit

Permalink
Sync to upstream/release/597 (#1054)
Browse files Browse the repository at this point in the history
# New Type Solver

- Implement bidirectional type inference for higher order functions so
that we can provide a more precise type improving the autocomplete's
human factors.
- We seal all tables, so we changed the stringification to make it a
little lighter on users.
- Fixed a case of array-out-of-bound access.
- Type families no longer depends on `TxnLog` and `Unifier`.
- Type refinements now waits until the free types are sufficiently
solved.

# Native Code Generation

- Remove cached slot lookup for `executeSETTABLEKS` function because it
is a fallback in the event of a cache miss, making the cached slot
lookup redundant.
- Optimized repeated array lookups, e.g. `a[3]` in `a[3] = a[3] / 2` is
done once.

# Misc

- On some platforms, it is necessary to use `gmtime_s` with the
arguments reversed to get the current time. You can now define
`DOCTEST_CONFIG_USE_GMTIME_S` to build and run unit tests on those
platforms.

---------

Co-authored-by: Arseny Kapoulkine <[email protected]>
Co-authored-by: Vyacheslav Egorov <[email protected]>
Co-authored-by: Andy Friesen <[email protected]>
Co-authored-by: Lily Brown <[email protected]>
Co-authored-by: Aaron Weiss <[email protected]>
  • Loading branch information
6 people authored Sep 30, 2023
1 parent 16fbfe9 commit 1d0b449
Show file tree
Hide file tree
Showing 46 changed files with 1,220 additions and 298 deletions.
23 changes: 13 additions & 10 deletions Analysis/include/Luau/ConstraintGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ struct ConstraintGraphBuilder
std::function<void(const ModuleName&, const ScopePtr&)> prepareModuleScope, DcrLogger* logger, NotNull<DataFlowGraph> dfg,
std::vector<RequireCycle> requireCycles);

/**
* The entry point to the ConstraintGraphBuilder. This will construct a set
* of scopes, constraints, and free types that can be solved later.
* @param block the root block to generate constraints for.
*/
void visitModuleRoot(AstStatBlock* block);

private:
/**
* Fabricates a new free type belonging to a given scope.
* @param scope the scope the free type belongs to.
Expand Down Expand Up @@ -143,13 +151,6 @@ struct ConstraintGraphBuilder

void applyRefinements(const ScopePtr& scope, Location location, RefinementId refinement);

/**
* The entry point to the ConstraintGraphBuilder. This will construct a set
* of scopes, constraints, and free types that can be solved later.
* @param block the root block to generate constraints for.
*/
void visit(AstStatBlock* block);

ControlFlow visitBlockWithoutChildScope(const ScopePtr& scope, AstStatBlock* block);

ControlFlow visit(const ScopePtr& scope, AstStat* stat);
Expand All @@ -172,7 +173,8 @@ struct ConstraintGraphBuilder
ControlFlow visit(const ScopePtr& scope, AstStatError* error);

InferencePack checkPack(const ScopePtr& scope, AstArray<AstExpr*> exprs, const std::vector<std::optional<TypeId>>& expectedTypes = {});
InferencePack checkPack(const ScopePtr& scope, AstExpr* expr, const std::vector<std::optional<TypeId>>& expectedTypes = {});
InferencePack checkPack(
const ScopePtr& scope, AstExpr* expr, const std::vector<std::optional<TypeId>>& expectedTypes = {}, bool generalize = true);

InferencePack checkPack(const ScopePtr& scope, AstExprCall* call);

Expand All @@ -182,18 +184,19 @@ struct ConstraintGraphBuilder
* @param expr the expression to check.
* @param expectedType the type of the expression that is expected from its
* surrounding context. Used to implement bidirectional type checking.
* @param generalize If true, generalize any lambdas that are encountered.
* @return the type of the expression.
*/
Inference check(const ScopePtr& scope, AstExpr* expr, ValueContext context = ValueContext::RValue, std::optional<TypeId> expectedType = {},
bool forceSingleton = false);
bool forceSingleton = false, bool generalize = true);

Inference check(const ScopePtr& scope, AstExprConstantString* string, std::optional<TypeId> expectedType, bool forceSingleton);
Inference check(const ScopePtr& scope, AstExprConstantBool* bool_, std::optional<TypeId> expectedType, bool forceSingleton);
Inference check(const ScopePtr& scope, AstExprLocal* local, ValueContext context);
Inference check(const ScopePtr& scope, AstExprGlobal* global);
Inference check(const ScopePtr& scope, AstExprIndexName* indexName);
Inference check(const ScopePtr& scope, AstExprIndexExpr* indexExpr);
Inference check(const ScopePtr& scope, AstExprFunction* func, std::optional<TypeId> expectedType);
Inference check(const ScopePtr& scope, AstExprFunction* func, std::optional<TypeId> expectedType, bool generalize);
Inference check(const ScopePtr& scope, AstExprUnary* unary);
Inference check(const ScopePtr& scope, AstExprBinary* binary, std::optional<TypeId> expectedType);
Inference check(const ScopePtr& scope, AstExprIfElse* ifElse, std::optional<TypeId> expectedType);
Expand Down
15 changes: 15 additions & 0 deletions Analysis/include/Luau/NonStrictTypeChecker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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"

namespace Luau
{

struct BuiltinTypes;


void checkNonStrict(NotNull<BuiltinTypes> builtinTypes, Module* module);

} // namespace Luau
28 changes: 15 additions & 13 deletions Analysis/src/Clone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "Luau/TypePack.h"
#include "Luau/Unifiable.h"

LUAU_FASTFLAG(DebugLuauCopyBeforeNormalizing)
LUAU_FASTFLAG(DebugLuauReadWriteProperties)

LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution)
Expand Down Expand Up @@ -253,8 +252,10 @@ class TypeCloner2

void cloneChildren(FreeType* t)
{
// TODO: clone lower and upper bounds.
// TODO: In the new solver, we should ice.
if (t->lowerBound)
t->lowerBound = shallowClone(t->lowerBound);
if (t->upperBound)
t->upperBound = shallowClone(t->upperBound);
}

void cloneChildren(GenericType* t)
Expand Down Expand Up @@ -376,7 +377,11 @@ class TypeCloner2

void cloneChildren(TypeFamilyInstanceType* t)
{
// TODO: In the new solver, we should ice.
for (TypeId& ty : t->typeArguments)
ty = shallowClone(ty);

for (TypePackId& tp : t->packArguments)
tp = shallowClone(tp);
}

void cloneChildren(FreeTypePack* t)
Expand Down Expand Up @@ -416,7 +421,11 @@ class TypeCloner2

void cloneChildren(TypeFamilyInstanceTypePack* t)
{
// TODO: In the new solver, we should ice.
for (TypeId& ty : t->typeArguments)
ty = shallowClone(ty);

for (TypePackId& tp : t->packArguments)
tp = shallowClone(tp);
}
};

Expand Down Expand Up @@ -560,8 +569,6 @@ struct TypePackCloner
void operator()(const Unifiable::Bound<TypePackId>& t)
{
TypePackId cloned = clone(t.boundTo, dest, cloneState);
if (FFlag::DebugLuauCopyBeforeNormalizing)
cloned = dest.addTypePack(TypePackVar{BoundTypePack{cloned}});
seenTypePacks[typePackId] = cloned;
}

Expand Down Expand Up @@ -629,8 +636,6 @@ void TypeCloner::operator()(const GenericType& t)
void TypeCloner::operator()(const Unifiable::Bound<TypeId>& t)
{
TypeId boundTo = clone(t.boundTo, dest, cloneState);
if (FFlag::DebugLuauCopyBeforeNormalizing)
boundTo = dest.addType(BoundType{boundTo});
seenTypes[typeId] = boundTo;
}

Expand Down Expand Up @@ -701,7 +706,7 @@ void TypeCloner::operator()(const FunctionType& t)
void TypeCloner::operator()(const TableType& t)
{
// If table is now bound to another one, we ignore the content of the original
if (!FFlag::DebugLuauCopyBeforeNormalizing && t.boundTo)
if (t.boundTo)
{
TypeId boundTo = clone(*t.boundTo, dest, cloneState);
seenTypes[typeId] = boundTo;
Expand All @@ -718,9 +723,6 @@ void TypeCloner::operator()(const TableType& t)

ttv->level = TypeLevel{0, 0};

if (FFlag::DebugLuauCopyBeforeNormalizing && t.boundTo)
ttv->boundTo = clone(*t.boundTo, dest, cloneState);

for (const auto& [name, prop] : t.props)
ttv->props[name] = clone(prop, dest, cloneState);

Expand Down
Loading

0 comments on commit 1d0b449

Please sign in to comment.