// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details #include "Luau/TxnLog.h" #include "Luau/ToString.h" #include "Luau/TypeArena.h" #include "Luau/TypePack.h" #include #include LUAU_FASTFLAG(LuauUnknownAndNeverType) namespace Luau { const std::string nullPendingResult = ""; std::string toString(PendingType* pending) { if (pending == nullptr) return nullPendingResult; return toString(pending->pending); } std::string dump(PendingType* pending) { if (pending == nullptr) { printf("%s\n", nullPendingResult.c_str()); return nullPendingResult; } ToStringOptions opts; opts.exhaustive = true; opts.functionTypeArguments = true; std::string result = toString(pending->pending, opts); printf("%s\n", result.c_str()); return result; } std::string toString(PendingTypePack* pending) { if (pending == nullptr) return nullPendingResult; return toString(pending->pending); } std::string dump(PendingTypePack* pending) { if (pending == nullptr) { printf("%s\n", nullPendingResult.c_str()); return nullPendingResult; } ToStringOptions opts; opts.exhaustive = true; opts.functionTypeArguments = true; std::string result = toString(pending->pending, opts); printf("%s\n", result.c_str()); return result; } static const TxnLog emptyLog; const TxnLog* TxnLog::empty() { return &emptyLog; } void TxnLog::concat(TxnLog rhs) { for (auto& [ty, rep] : rhs.typeVarChanges) typeVarChanges[ty] = std::move(rep); for (auto& [tp, rep] : rhs.typePackChanges) typePackChanges[tp] = std::move(rep); } void TxnLog::concatAsIntersections(TxnLog rhs, NotNull arena) { for (auto& [ty, rightRep] : rhs.typeVarChanges) { if (auto leftRep = typeVarChanges.find(ty)) { TypeId leftTy = arena->addType((*leftRep)->pending); TypeId rightTy = arena->addType(rightRep->pending); typeVarChanges[ty]->pending.ty = IntersectionTypeVar{{leftTy, rightTy}}; } else typeVarChanges[ty] = std::move(rightRep); } for (auto& [tp, rep] : rhs.typePackChanges) typePackChanges[tp] = std::move(rep); } void TxnLog::concatAsUnion(TxnLog rhs, NotNull arena) { for (auto& [ty, rightRep] : rhs.typeVarChanges) { if (auto leftRep = typeVarChanges.find(ty)) { TypeId leftTy = arena->addType((*leftRep)->pending); TypeId rightTy = arena->addType(rightRep->pending); typeVarChanges[ty]->pending.ty = UnionTypeVar{{leftTy, rightTy}}; } else typeVarChanges[ty] = std::move(rightRep); } for (auto& [tp, rep] : rhs.typePackChanges) typePackChanges[tp] = std::move(rep); } void TxnLog::commit() { for (auto& [ty, rep] : typeVarChanges) asMutable(ty)->reassign(rep.get()->pending); for (auto& [tp, rep] : typePackChanges) asMutable(tp)->reassign(rep.get()->pending); clear(); } void TxnLog::clear() { typeVarChanges.clear(); typePackChanges.clear(); } TxnLog TxnLog::inverse() { TxnLog inversed(sharedSeen); for (auto& [ty, _rep] : typeVarChanges) inversed.typeVarChanges[ty] = std::make_unique(*ty); for (auto& [tp, _rep] : typePackChanges) inversed.typePackChanges[tp] = std::make_unique(*tp); return inversed; } bool TxnLog::haveSeen(TypeId lhs, TypeId rhs) const { return haveSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } void TxnLog::pushSeen(TypeId lhs, TypeId rhs) { pushSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } void TxnLog::popSeen(TypeId lhs, TypeId rhs) { popSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } bool TxnLog::haveSeen(TypePackId lhs, TypePackId rhs) const { return haveSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } void TxnLog::pushSeen(TypePackId lhs, TypePackId rhs) { pushSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } void TxnLog::popSeen(TypePackId lhs, TypePackId rhs) { popSeen((TypeOrPackId)lhs, (TypeOrPackId)rhs); } bool TxnLog::haveSeen(TypeOrPackId lhs, TypeOrPackId rhs) const { const std::pair sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs); if (sharedSeen->end() != std::find(sharedSeen->begin(), sharedSeen->end(), sortedPair)) { return true; } return false; } void TxnLog::pushSeen(TypeOrPackId lhs, TypeOrPackId rhs) { const std::pair sortedPair = (lhs > rhs) ? std::make_pair(lhs, rhs) : std::make_pair(rhs, lhs); sharedSeen->push_back(sortedPair); } void TxnLog::popSeen(TypeOrPackId lhs, TypeOrPackId rhs) { const std::pair 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(!ty->persistent); // Explicitly don't look in ancestors. If we have discovered something new // about this type, we don't want to mutate the parent's state. auto& pending = typeVarChanges[ty]; if (!pending) { pending = std::make_unique(*ty); pending->pending.owningArena = nullptr; } return pending.get(); } PendingTypePack* TxnLog::queue(TypePackId tp) { LUAU_ASSERT(!tp->persistent); // Explicitly don't look in ancestors. If we have discovered something new // about this type, we don't want to mutate the parent's state. auto& pending = typePackChanges[tp]; if (!pending) { pending = std::make_unique(*tp); pending->pending.owningArena = nullptr; } return pending.get(); } PendingType* TxnLog::pending(TypeId ty) const { // This function will technically work if `this` is nullptr, but this // indicates a bug, so we explicitly assert. LUAU_ASSERT(static_cast(this) != nullptr); for (const TxnLog* current = this; current; current = current->parent) { if (auto it = current->typeVarChanges.find(ty)) return it->get(); } return nullptr; } PendingTypePack* TxnLog::pending(TypePackId tp) const { // This function will technically work if `this` is nullptr, but this // indicates a bug, so we explicitly assert. LUAU_ASSERT(static_cast(this) != nullptr); for (const TxnLog* current = this; current; current = current->parent) { if (auto it = current->typePackChanges.find(tp)) return it->get(); } return nullptr; } PendingType* TxnLog::replace(TypeId ty, TypeVar replacement) { PendingType* newTy = queue(ty); newTy->pending.reassign(replacement); return newTy; } PendingTypePack* TxnLog::replace(TypePackId tp, TypePackVar replacement) { PendingTypePack* newTp = queue(tp); newTp->pending.reassign(replacement); return newTp; } PendingType* TxnLog::bindTable(TypeId ty, std::optional newBoundTo) { LUAU_ASSERT(get(ty)); PendingType* newTy = queue(ty); if (TableTypeVar* ttv = Luau::getMutable(newTy)) ttv->boundTo = newBoundTo; return newTy; } PendingType* TxnLog::changeLevel(TypeId ty, TypeLevel newLevel) { LUAU_ASSERT(get(ty) || get(ty) || get(ty)); PendingType* newTy = queue(ty); if (FreeTypeVar* ftv = Luau::getMutable(newTy)) { ftv->level = newLevel; } else if (TableTypeVar* ttv = Luau::getMutable(newTy)) { LUAU_ASSERT(ttv->state == TableState::Free || ttv->state == TableState::Generic); ttv->level = newLevel; } else if (FunctionTypeVar* ftv = Luau::getMutable(newTy)) { ftv->level = newLevel; } return newTy; } PendingTypePack* TxnLog::changeLevel(TypePackId tp, TypeLevel newLevel) { LUAU_ASSERT(get(tp)); PendingTypePack* newTp = queue(tp); if (FreeTypePack* ftp = Luau::getMutable(newTp)) { ftp->level = newLevel; } return newTp; } PendingType* TxnLog::changeScope(TypeId ty, NotNull newScope) { LUAU_ASSERT(get(ty) || get(ty) || get(ty)); PendingType* newTy = queue(ty); if (FreeTypeVar* ftv = Luau::getMutable(newTy)) { ftv->scope = newScope; } else if (TableTypeVar* ttv = Luau::getMutable(newTy)) { LUAU_ASSERT(ttv->state == TableState::Free || ttv->state == TableState::Generic); ttv->scope = newScope; } else if (FunctionTypeVar* ftv = Luau::getMutable(newTy)) { ftv->scope = newScope; } return newTy; } PendingTypePack* TxnLog::changeScope(TypePackId tp, NotNull newScope) { LUAU_ASSERT(get(tp)); PendingTypePack* newTp = queue(tp); if (FreeTypePack* ftp = Luau::getMutable(newTp)) { ftp->scope = newScope; } return newTp; } PendingType* TxnLog::changeIndexer(TypeId ty, std::optional indexer) { LUAU_ASSERT(get(ty)); PendingType* newTy = queue(ty); if (TableTypeVar* ttv = Luau::getMutable(newTy)) { ttv->indexer = indexer; } return newTy; } std::optional TxnLog::getLevel(TypeId ty) const { if (FreeTypeVar* ftv = getMutable(ty)) return ftv->level; else if (TableTypeVar* ttv = getMutable(ty); ttv && (ttv->state == TableState::Free || ttv->state == TableState::Generic)) return ttv->level; else if (FunctionTypeVar* ftv = getMutable(ty)) return ftv->level; return std::nullopt; } TypeId TxnLog::follow(TypeId ty) const { return Luau::follow(ty, [this](TypeId ty) { PendingType* state = this->pending(ty); if (state == nullptr) return ty; // Ugly: Fabricate a TypeId that doesn't adhere to most of the invariants // that normally apply. This is safe because follow will only call get<> // on the returned pointer. return const_cast(&state->pending); }); } TypePackId TxnLog::follow(TypePackId tp) const { return Luau::follow(tp, [this](TypePackId tp) { PendingTypePack* state = this->pending(tp); if (state == nullptr) return tp; // Ugly: Fabricate a TypePackId that doesn't adhere to most of the // invariants that normally apply. This is safe because follow will // only call get<> on the returned pointer. return const_cast(&state->pending); }); } std::pair, std::vector> TxnLog::getChanges() const { std::pair, std::vector> result; for (const auto& [typeId, _newState] : typeVarChanges) result.first.push_back(typeId); for (const auto& [typePackId, _newState] : typePackChanges) result.second.push_back(typePackId); return result; } } // namespace Luau