Skip to content

Commit

Permalink
Sync to upstream/release/518 (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeux committed Mar 11, 2022
1 parent dbdf91f commit a44b790
Show file tree
Hide file tree
Showing 26 changed files with 1,124 additions and 2,155 deletions.
55 changes: 0 additions & 55 deletions Analysis/include/Luau/TxnLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,6 @@ namespace Luau

using TypeOrPackId = const void*;

// Log of where what TypeIds we are rebinding and what they used to be
// Remove with LuauUseCommitTxnLog
struct DEPRECATED_TxnLog
{
DEPRECATED_TxnLog()
: originalSeenSize(0)
, ownedSeen()
, sharedSeen(&ownedSeen)
{
}

explicit DEPRECATED_TxnLog(std::vector<std::pair<TypeOrPackId, TypeOrPackId>>* sharedSeen)
: originalSeenSize(sharedSeen->size())
, ownedSeen()
, sharedSeen(sharedSeen)
{
}

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

DEPRECATED_TxnLog(DEPRECATED_TxnLog&&) = default;
DEPRECATED_TxnLog& operator=(DEPRECATED_TxnLog&&) = default;

void operator()(TypeId a);
void operator()(TypePackId a);
void operator()(TableTypeVar* a);

void rollback();

void concat(DEPRECATED_TxnLog rhs);

bool haveSeen(TypeId lhs, TypeId rhs);
void pushSeen(TypeId lhs, TypeId rhs);
void popSeen(TypeId lhs, TypeId rhs);

bool haveSeen(TypePackId lhs, TypePackId rhs);
void pushSeen(TypePackId lhs, TypePackId rhs);
void popSeen(TypePackId lhs, TypePackId rhs);

private:
std::vector<std::pair<TypeId, TypeVar>> typeVarChanges;
std::vector<std::pair<TypePackId, TypePackVar>> typePackChanges;
std::vector<std::pair<TableTypeVar*, std::optional<TypeId>>> tableChanges;
size_t originalSeenSize;

bool haveSeen(TypeOrPackId lhs, TypeOrPackId rhs);
void pushSeen(TypeOrPackId lhs, TypeOrPackId rhs);
void popSeen(TypeOrPackId lhs, TypeOrPackId rhs);

public:
std::vector<std::pair<TypeOrPackId, TypeOrPackId>> ownedSeen; // used to avoid infinite recursion when types are cyclic
std::vector<std::pair<TypeOrPackId, TypeOrPackId>>* sharedSeen; // shared with all the descendent logs
};

// Pending state for a TypeVar. Generated by a TxnLog and committed via
// TxnLog::commit.
struct PendingType
Expand Down
1 change: 0 additions & 1 deletion Analysis/include/Luau/TypePack.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ struct TypePackIterator
const TypePack* tp = nullptr;
size_t currentIndex = 0;

// Only used if LuauUseCommittingTxnLog is true.
const TxnLog* log;
};

Expand Down
1 change: 0 additions & 1 deletion Analysis/include/Luau/Unifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ struct Unifier
TypeArena* const types;
Mode mode;

DEPRECATED_TxnLog DEPRECATED_log;
TxnLog log;
ErrorVec errors;
Location location;
Expand Down
56 changes: 10 additions & 46 deletions Analysis/src/Autocomplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include <unordered_set>
#include <utility>

LUAU_FASTFLAG(LuauUseCommittingTxnLog)
LUAU_FASTFLAGVARIABLE(LuauAutocompleteAvoidMutation, false);
LUAU_FASTFLAGVARIABLE(LuauMissingFollowACMetatables, false);
LUAU_FASTFLAGVARIABLE(LuauIfElseExprFixCompletionIssue, false);

static const std::unordered_set<std::string> kStatementStartingKeywords = {
Expand Down Expand Up @@ -240,28 +237,9 @@ static TypeCorrectKind checkTypeCorrectKind(const Module& module, TypeArena* typ
UnifierSharedState unifierState(&iceReporter);
Unifier unifier(typeArena, Mode::Strict, Location(), Variance::Covariant, unifierState);

if (FFlag::LuauAutocompleteAvoidMutation && !FFlag::LuauUseCommittingTxnLog)
{
SeenTypes seenTypes;
SeenTypePacks seenTypePacks;
CloneState cloneState;
superTy = clone(superTy, *typeArena, seenTypes, seenTypePacks, cloneState);
subTy = clone(subTy, *typeArena, seenTypes, seenTypePacks, cloneState);

auto errors = unifier.canUnify(subTy, superTy);
return errors.empty();
}
else
{
unifier.tryUnify(subTy, superTy);

bool ok = unifier.errors.empty();

if (!FFlag::LuauUseCommittingTxnLog)
unifier.DEPRECATED_log.rollback();

return ok;
}
unifier.tryUnify(subTy, superTy);
bool ok = unifier.errors.empty();
return ok;
};

auto typeAtPosition = findExpectedTypeAt(module, node, position);
Expand Down Expand Up @@ -403,28 +381,14 @@ static void autocompleteProps(const Module& module, TypeArena* typeArena, TypeId
auto indexIt = mtable->props.find("__index");
if (indexIt != mtable->props.end())
{
if (FFlag::LuauMissingFollowACMetatables)
TypeId followed = follow(indexIt->second.type);
if (get<TableTypeVar>(followed) || get<MetatableTypeVar>(followed))
autocompleteProps(module, typeArena, followed, indexType, nodes, result, seen);
else if (auto indexFunction = get<FunctionTypeVar>(followed))
{
TypeId followed = follow(indexIt->second.type);
if (get<TableTypeVar>(followed) || get<MetatableTypeVar>(followed))
autocompleteProps(module, typeArena, followed, indexType, nodes, result, seen);
else if (auto indexFunction = get<FunctionTypeVar>(followed))
{
std::optional<TypeId> indexFunctionResult = first(indexFunction->retType);
if (indexFunctionResult)
autocompleteProps(module, typeArena, *indexFunctionResult, indexType, nodes, result, seen);
}
}
else
{
if (get<TableTypeVar>(indexIt->second.type) || get<MetatableTypeVar>(indexIt->second.type))
autocompleteProps(module, typeArena, indexIt->second.type, indexType, nodes, result, seen);
else if (auto indexFunction = get<FunctionTypeVar>(indexIt->second.type))
{
std::optional<TypeId> indexFunctionResult = first(indexFunction->retType);
if (indexFunctionResult)
autocompleteProps(module, typeArena, *indexFunctionResult, indexType, nodes, result, seen);
}
std::optional<TypeId> indexFunctionResult = first(indexFunction->retType);
if (indexFunctionResult)
autocompleteProps(module, typeArena, *indexFunctionResult, indexType, nodes, result, seen);
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions Analysis/src/BuiltinDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

LUAU_FASTFLAG(LuauAssertStripsFalsyTypes)
LUAU_FASTFLAGVARIABLE(LuauTableCloneType, false)
LUAU_FASTFLAGVARIABLE(LuauSetMetaTableArgsCheck, false)

/** FIXME: Many of these type definitions are not quite completely accurate.
*
Expand Down Expand Up @@ -376,11 +377,19 @@ static std::optional<ExprResult<TypePackId>> magicFunctionSetMetaTable(

TypeId mtTy = arena.addType(mtv);

AstExpr* targetExpr = expr.args.data[0];
if (AstExprLocal* targetLocal = targetExpr->as<AstExprLocal>())
if (FFlag::LuauSetMetaTableArgsCheck && expr.args.size < 1)
{
const Name targetName(targetLocal->local->name.value);
scope->bindings[targetLocal->local] = Binding{mtTy, expr.location};
return ExprResult<TypePackId>{};
}

if (!FFlag::LuauSetMetaTableArgsCheck || !expr.self)
{
AstExpr* targetExpr = expr.args.data[0];
if (AstExprLocal* targetLocal = targetExpr->as<AstExprLocal>())
{
const Name targetName(targetLocal->local->name.value);
scope->bindings[targetLocal->local] = Binding{mtTy, expr.location};
}
}

return ExprResult<TypePackId>{arena.addTypePack({mtTy})};
Expand Down
117 changes: 0 additions & 117 deletions Analysis/src/TxnLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,110 +7,9 @@
#include <algorithm>
#include <stdexcept>

LUAU_FASTFLAGVARIABLE(LuauUseCommittingTxnLog, false)

namespace Luau
{

void DEPRECATED_TxnLog::operator()(TypeId a)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
typeVarChanges.emplace_back(a, *a);
}

void DEPRECATED_TxnLog::operator()(TypePackId a)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
typePackChanges.emplace_back(a, *a);
}

void DEPRECATED_TxnLog::operator()(TableTypeVar* a)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
tableChanges.emplace_back(a, a->boundTo);
}

void DEPRECATED_TxnLog::rollback()
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
for (auto it = typeVarChanges.rbegin(); it != typeVarChanges.rend(); ++it)
std::swap(*asMutable(it->first), it->second);

for (auto it = typePackChanges.rbegin(); it != typePackChanges.rend(); ++it)
std::swap(*asMutable(it->first), it->second);

for (auto it = tableChanges.rbegin(); it != tableChanges.rend(); ++it)
std::swap(it->first->boundTo, it->second);

LUAU_ASSERT(originalSeenSize <= sharedSeen->size());
sharedSeen->resize(originalSeenSize);
}

void DEPRECATED_TxnLog::concat(DEPRECATED_TxnLog rhs)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
typeVarChanges.insert(typeVarChanges.end(), rhs.typeVarChanges.begin(), rhs.typeVarChanges.end());
rhs.typeVarChanges.clear();

typePackChanges.insert(typePackChanges.end(), rhs.typePackChanges.begin(), rhs.typePackChanges.end());
rhs.typePackChanges.clear();

tableChanges.insert(tableChanges.end(), rhs.tableChanges.begin(), rhs.tableChanges.end());
rhs.tableChanges.clear();
}

bool DEPRECATED_TxnLog::haveSeen(TypeId lhs, TypeId rhs)
{
return haveSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

void DEPRECATED_TxnLog::pushSeen(TypeId lhs, TypeId rhs)
{
pushSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

void DEPRECATED_TxnLog::popSeen(TypeId lhs, TypeId rhs)
{
popSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

bool DEPRECATED_TxnLog::haveSeen(TypePackId lhs, TypePackId rhs)
{
return haveSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

void DEPRECATED_TxnLog::pushSeen(TypePackId lhs, TypePackId rhs)
{
pushSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

void DEPRECATED_TxnLog::popSeen(TypePackId lhs, TypePackId rhs)
{
popSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs);
}

bool DEPRECATED_TxnLog::haveSeen(TypeOrPackId lhs, TypeOrPackId rhs)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
return (sharedSeen->end() != std::find(sharedSeen->begin(), sharedSeen->end(), sortedPair));
}

void DEPRECATED_TxnLog::pushSeen(TypeOrPackId lhs, TypeOrPackId rhs)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
sharedSeen->push_back(sortedPair);
}

void DEPRECATED_TxnLog::popSeen(TypeOrPackId lhs, TypeOrPackId rhs)
{
LUAU_ASSERT(!FFlag::LuauUseCommittingTxnLog);
const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
LUAU_ASSERT(sortedPair == sharedSeen->back());
sharedSeen->pop_back();
}

const std::string nullPendingResult = "<nullptr>";

std::string toString(PendingType* pending)
Expand Down Expand Up @@ -170,8 +69,6 @@ const TxnLog* TxnLog::empty()

void TxnLog::concat(TxnLog rhs)
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

for (auto& [ty, rep] : rhs.typeVarChanges)
typeVarChanges[ty] = std::move(rep);

Expand All @@ -181,8 +78,6 @@ void TxnLog::concat(TxnLog rhs)

void TxnLog::commit()
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

for (auto& [ty, rep] : typeVarChanges)
*asMutable(ty) = rep.get()->pending;

Expand All @@ -194,16 +89,12 @@ void TxnLog::commit()

void TxnLog::clear()
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

typeVarChanges.clear();
typePackChanges.clear();
}

TxnLog TxnLog::inverse()
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

TxnLog inversed(sharedSeen);

for (auto& [ty, _rep] : typeVarChanges)
Expand Down Expand Up @@ -247,8 +138,6 @@ void TxnLog::popSeen(TypePackId lhs, TypePackId rhs)

bool TxnLog::haveSeen(TypeOrPackId lhs, TypeOrPackId rhs) const
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
if (sharedSeen->end() != std::find(sharedSeen->begin(), sharedSeen->end(), sortedPair))
{
Expand All @@ -265,24 +154,19 @@ bool TxnLog::haveSeen(TypeOrPackId lhs, TypeOrPackId rhs) const

void TxnLog::pushSeen(TypeOrPackId lhs, TypeOrPackId rhs)
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
sharedSeen->push_back(sortedPair);
}

void TxnLog::popSeen(TypeOrPackId lhs, TypeOrPackId rhs)
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);

const std::pair<TypeOrPackId, TypeOrPackId> sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs);
LUAU_ASSERT(sortedPair == sharedSeen->back());
sharedSeen->pop_back();
}

PendingType* TxnLog::queue(TypeId ty)
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);
LUAU_ASSERT(!ty->persistent);

// Explicitly don't look in ancestors. If we have discovered something new
Expand All @@ -296,7 +180,6 @@ PendingType* TxnLog::queue(TypeId ty)

PendingTypePack* TxnLog::queue(TypePackId tp)
{
LUAU_ASSERT(FFlag::LuauUseCommittingTxnLog);
LUAU_ASSERT(!tp->persistent);

// Explicitly don't look in ancestors. If we have discovered something new
Expand Down
Loading

0 comments on commit a44b790

Please sign in to comment.