Sync to upstream/release/522 (#450)

This commit is contained in:
Arseny Kapoulkine 2022-04-07 14:29:01 -07:00 committed by GitHub
parent ffff25a9e5
commit de1381e3f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 1407 additions and 785 deletions

View file

@ -0,0 +1,25 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include "Luau/TypeVar.h"
#include <unordered_map>
namespace Luau
{
// Only exposed so they can be unit tested.
using SeenTypes = std::unordered_map<TypeId, TypeId>;
using SeenTypePacks = std::unordered_map<TypePackId, TypePackId>;
struct CloneState
{
int recursionCount = 0;
bool encounteredFreeType = false;
};
TypePackId clone(TypePackId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
TypeId clone(TypeId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
TypeFun clone(const TypeFun& typeFun, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
} // namespace Luau

View file

@ -12,6 +12,8 @@
#include <vector>
#include <optional>
LUAU_FASTFLAG(LuauSeparateTypechecks)
namespace Luau
{
@ -55,10 +57,19 @@ std::optional<ModuleName> pathExprToModuleName(const ModuleName& currentModuleNa
struct SourceNode
{
bool isDirty(bool forAutocomplete) const
{
if (FFlag::LuauSeparateTypechecks)
return forAutocomplete ? dirtyAutocomplete : dirty;
else
return dirty;
}
ModuleName name;
std::unordered_set<ModuleName> requires;
std::vector<std::pair<ModuleName, Location>> requireLocations;
bool dirty = true;
bool dirtyAutocomplete = true;
};
struct FrontendOptions
@ -71,12 +82,16 @@ struct FrontendOptions
// When true, we run typechecking twice, once in the regular mode, and once in strict mode
// in order to get more precise type information (e.g. for autocomplete).
bool typecheckTwice = false;
bool typecheckTwice_DEPRECATED = false;
// Run typechecking only in mode required for autocomplete (strict mode in order to get more precise type information)
bool forAutocomplete = false;
};
struct CheckResult
{
std::vector<TypeError> errors;
std::vector<ModuleName> timeoutHits;
};
struct FrontendModuleResolver : ModuleResolver
@ -123,7 +138,7 @@ struct Frontend
CheckResult check(const SourceModule& module); // OLD. TODO KILL
LintResult lint(const SourceModule& module, std::optional<LintOptions> enabledLintWarnings = {});
bool isDirty(const ModuleName& name) const;
bool isDirty(const ModuleName& name, bool forAutocomplete = false) const;
void markDirty(const ModuleName& name, std::vector<ModuleName>* markedDirty = nullptr);
/** Borrow a pointer into the SourceModule cache.
@ -147,10 +162,10 @@ struct Frontend
void applyBuiltinDefinitionToEnvironment(const std::string& environmentName, const std::string& definitionName);
private:
std::pair<SourceNode*, SourceModule*> getSourceNode(CheckResult& checkResult, const ModuleName& name);
std::pair<SourceNode*, SourceModule*> getSourceNode(CheckResult& checkResult, const ModuleName& name, bool forAutocomplete);
SourceModule parse(const ModuleName& name, std::string_view src, const ParseOptions& parseOptions);
bool parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& checkResult, const ModuleName& root);
bool parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& checkResult, const ModuleName& root, bool forAutocomplete);
static LintResult classifyLints(const std::vector<LintWarning>& warnings, const Config& config);

View file

@ -29,8 +29,8 @@ struct SourceModule
std::optional<std::string> environmentName;
bool cyclic = false;
std::unique_ptr<Allocator> allocator;
std::unique_ptr<AstNameTable> names;
std::shared_ptr<Allocator> allocator;
std::shared_ptr<AstNameTable> names;
std::vector<ParseError> parseErrors;
AstStatBlock* root = nullptr;
@ -48,6 +48,12 @@ struct SourceModule
bool isWithinComment(const SourceModule& sourceModule, Position pos);
struct RequireCycle
{
Location location;
std::vector<ModuleName> path; // one of the paths for a require() to go all the way back to the originating module
};
struct TypeArena
{
TypedAllocator<TypeVar> typeVars;
@ -77,20 +83,6 @@ struct TypeArena
void freeze(TypeArena& arena);
void unfreeze(TypeArena& arena);
// Only exposed so they can be unit tested.
using SeenTypes = std::unordered_map<TypeId, TypeId>;
using SeenTypePacks = std::unordered_map<TypePackId, TypePackId>;
struct CloneState
{
int recursionCount = 0;
bool encounteredFreeType = false;
};
TypePackId clone(TypePackId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
TypeId clone(TypeId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
TypeFun clone(const TypeFun& typeFun, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState);
struct Module
{
~Module();
@ -98,6 +90,10 @@ struct Module
TypeArena interfaceTypes;
TypeArena internalTypes;
// Scopes and AST types refer to parse data, so we need to keep that alive
std::shared_ptr<Allocator> allocator;
std::shared_ptr<AstNameTable> names;
std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
@ -109,6 +105,7 @@ struct Module
ErrorVec errors;
Mode mode;
SourceCode::Type type;
bool timeout = false;
ScopePtr getModuleScope() const;

View file

@ -124,6 +124,12 @@ struct HashBoolNamePair
size_t operator()(const std::pair<bool, Name>& pair) const;
};
class TimeLimitError : public std::exception
{
public:
virtual const char* what() const throw();
};
// All TypeVars are retained via Environment::typeVars. All TypeIds
// within a program are borrowed pointers into this set.
struct TypeChecker
@ -413,6 +419,10 @@ public:
UnifierSharedState unifierState;
std::vector<RequireCycle> requireCycles;
std::optional<double> finishTime;
public:
const TypeId nilType;
const TypeId numberType;

View file

@ -513,6 +513,8 @@ struct SingletonTypes
const TypeId stringType;
const TypeId booleanType;
const TypeId threadType;
const TypeId trueType;
const TypeId falseType;
const TypeId anyType;
const TypeId optionalNumberType;

View file

@ -2,45 +2,14 @@
#pragma once
#include "Luau/Common.h"
#ifndef LUAU_USE_STD_VARIANT
#define LUAU_USE_STD_VARIANT 0
#endif
#if LUAU_USE_STD_VARIANT
#include <variant>
#else
#include <new>
#include <type_traits>
#include <initializer_list>
#include <stddef.h>
#endif
namespace Luau
{
#if LUAU_USE_STD_VARIANT
template<typename... Ts>
using Variant = std::variant<Ts...>;
template<class Visitor, class Variant>
auto visit(Visitor&& vis, Variant&& var)
{
// This change resolves the ABI issues with std::variant on libc++; std::visit normally throws bad_variant_access
// but it requires an update to libc++.dylib which ships with macOS 10.14. To work around this, we assert on valueless
// variants since we will never generate them and call into a libc++ function that doesn't throw.
LUAU_ASSERT(!var.valueless_by_exception());
#ifdef __APPLE__
// See https://stackoverflow.com/a/53868971/503215
return std::__variant_detail::__visitation::__variant::__visit_value(vis, var);
#else
return std::visit(vis, var);
#endif
}
using std::get_if;
#else
template<typename... Ts>
class Variant
{
@ -248,6 +217,8 @@ static void fnVisitV(Visitor& vis, std::conditional_t<std::is_const_v<T>, const
template<class Visitor, typename... Ts>
auto visit(Visitor&& vis, const Variant<Ts...>& var)
{
static_assert(std::conjunction_v<std::is_invocable<Visitor, Ts>...>, "visitor must accept every alternative as an argument");
using Result = std::invoke_result_t<Visitor, typename Variant<Ts...>::first_alternative>;
static_assert(std::conjunction_v<std::is_same<Result, std::invoke_result_t<Visitor, Ts>>...>,
"visitor result type must be consistent between alternatives");
@ -273,6 +244,8 @@ auto visit(Visitor&& vis, const Variant<Ts...>& var)
template<class Visitor, typename... Ts>
auto visit(Visitor&& vis, Variant<Ts...>& var)
{
static_assert(std::conjunction_v<std::is_invocable<Visitor, Ts&>...>, "visitor must accept every alternative as an argument");
using Result = std::invoke_result_t<Visitor, typename Variant<Ts...>::first_alternative&>;
static_assert(std::conjunction_v<std::is_same<Result, std::invoke_result_t<Visitor, Ts&>>...>,
"visitor result type must be consistent between alternatives");
@ -294,7 +267,6 @@ auto visit(Visitor&& vis, Variant<Ts...>& var)
return res;
}
}
#endif
template<class>
inline constexpr bool always_false_v = false;

View file

@ -14,6 +14,7 @@
#include <utility>
LUAU_FASTFLAGVARIABLE(LuauIfElseExprFixCompletionIssue, false);
LUAU_FASTFLAGVARIABLE(LuauAutocompleteSingletonTypes, false);
LUAU_FASTFLAG(LuauSelfCallAutocompleteFix)
static const std::unordered_set<std::string> kStatementStartingKeywords = {
@ -625,6 +626,31 @@ AutocompleteEntryMap autocompleteModuleTypes(const Module& module, Position posi
return result;
}
static void autocompleteStringSingleton(TypeId ty, bool addQuotes, AutocompleteEntryMap& result)
{
auto formatKey = [addQuotes](const std::string& key) {
if (addQuotes)
return "\"" + escape(key) + "\"";
return escape(key);
};
ty = follow(ty);
if (auto ss = get<StringSingleton>(get<SingletonTypeVar>(ty)))
{
result[formatKey(ss->value)] = AutocompleteEntry{AutocompleteEntryKind::String, ty, false, false, TypeCorrectKind::Correct};
}
else if (auto uty = get<UnionTypeVar>(ty))
{
for (auto el : uty)
{
if (auto ss = get<StringSingleton>(get<SingletonTypeVar>(el)))
result[formatKey(ss->value)] = AutocompleteEntry{AutocompleteEntryKind::String, ty, false, false, TypeCorrectKind::Correct};
}
}
};
static bool canSuggestInferredType(ScopePtr scope, TypeId ty)
{
ty = follow(ty);
@ -1309,6 +1335,26 @@ static void autocompleteExpression(const SourceModule& sourceModule, const Modul
scope = scope->parent;
}
if (FFlag::LuauAutocompleteSingletonTypes)
{
TypeCorrectKind correctForNil = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.nilType);
TypeCorrectKind correctForTrue = checkTypeCorrectKind(module, typeArena, node, position, getSingletonTypes().trueType);
TypeCorrectKind correctForFalse = checkTypeCorrectKind(module, typeArena, node, position, getSingletonTypes().falseType);
TypeCorrectKind correctForFunction =
functionIsExpectedAt(module, node, position).value_or(false) ? TypeCorrectKind::Correct : TypeCorrectKind::None;
result["if"] = {AutocompleteEntryKind::Keyword, std::nullopt, false, false};
result["true"] = {AutocompleteEntryKind::Keyword, typeChecker.booleanType, false, false, correctForTrue};
result["false"] = {AutocompleteEntryKind::Keyword, typeChecker.booleanType, false, false, correctForFalse};
result["nil"] = {AutocompleteEntryKind::Keyword, typeChecker.nilType, false, false, correctForNil};
result["not"] = {AutocompleteEntryKind::Keyword};
result["function"] = {AutocompleteEntryKind::Keyword, std::nullopt, false, false, correctForFunction};
if (auto ty = findExpectedTypeAt(module, node, position))
autocompleteStringSingleton(*ty, true, result);
}
else
{
TypeCorrectKind correctForNil = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.nilType);
TypeCorrectKind correctForBoolean = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.booleanType);
TypeCorrectKind correctForFunction =
@ -1321,6 +1367,7 @@ static void autocompleteExpression(const SourceModule& sourceModule, const Modul
result["not"] = {AutocompleteEntryKind::Keyword};
result["function"] = {AutocompleteEntryKind::Keyword, std::nullopt, false, false, correctForFunction};
}
}
}
static AutocompleteEntryMap autocompleteExpression(const SourceModule& sourceModule, const Module& module, const TypeChecker& typeChecker,
@ -1625,17 +1672,33 @@ static AutocompleteResult autocomplete(const SourceModule& sourceModule, const M
}
else if (node->is<AstExprConstantString>())
{
AutocompleteEntryMap result;
if (FFlag::LuauAutocompleteSingletonTypes)
{
if (auto it = module->astExpectedTypes.find(node->asExpr()))
autocompleteStringSingleton(*it, false, result);
}
if (finder.ancestry.size() >= 2)
{
if (auto idxExpr = finder.ancestry.at(finder.ancestry.size() - 2)->as<AstExprIndexExpr>())
{
if (auto it = module->astTypes.find(idxExpr->expr))
autocompleteProps(*module, typeArena, follow(*it), PropIndexType::Point, finder.ancestry, result);
}
else if (auto binExpr = finder.ancestry.at(finder.ancestry.size() - 2)->as<AstExprBinary>();
binExpr && FFlag::LuauAutocompleteSingletonTypes)
{
return {autocompleteProps(*module, typeArena, follow(*it), PropIndexType::Point, finder.ancestry), finder.ancestry};
if (binExpr->op == AstExprBinary::CompareEq || binExpr->op == AstExprBinary::CompareNe)
{
if (auto it = module->astTypes.find(node == binExpr->left ? binExpr->right : binExpr->left))
autocompleteStringSingleton(*it, false, result);
}
}
}
return {};
return {result, finder.ancestry};
}
if (node->is<AstExprConstantNumber>())
@ -1653,17 +1716,30 @@ static AutocompleteResult autocomplete(const SourceModule& sourceModule, const M
AutocompleteResult autocomplete(Frontend& frontend, const ModuleName& moduleName, Position position, StringCompletionCallback callback)
{
if (FFlag::LuauSeparateTypechecks)
{
// FIXME: We can improve performance here by parsing without checking.
// The old type graph is probably fine. (famous last words!)
FrontendOptions opts;
opts.forAutocomplete = true;
frontend.check(moduleName, opts);
}
else
{
// FIXME: We can improve performance here by parsing without checking.
// The old type graph is probably fine. (famous last words!)
// FIXME: We don't need to typecheck for script analysis here, just for autocomplete.
frontend.check(moduleName);
}
const SourceModule* sourceModule = frontend.getSourceModule(moduleName);
if (!sourceModule)
return {};
TypeChecker& typeChecker = (frontend.options.typecheckTwice ? frontend.typeCheckerForAutocomplete : frontend.typeChecker);
ModulePtr module = (frontend.options.typecheckTwice ? frontend.moduleResolverForAutocomplete.getModule(moduleName)
TypeChecker& typeChecker =
(frontend.options.typecheckTwice_DEPRECATED || FFlag::LuauSeparateTypechecks ? frontend.typeCheckerForAutocomplete : frontend.typeChecker);
ModulePtr module =
(frontend.options.typecheckTwice_DEPRECATED || FFlag::LuauSeparateTypechecks ? frontend.moduleResolverForAutocomplete.getModule(moduleName)
: frontend.moduleResolver.getModule(moduleName));
if (!module)
@ -1692,7 +1768,8 @@ OwningAutocompleteResult autocompleteSource(Frontend& frontend, std::string_view
sourceModule->mode = Mode::Strict;
sourceModule->commentLocations = std::move(result.commentLocations);
TypeChecker& typeChecker = (frontend.options.typecheckTwice ? frontend.typeCheckerForAutocomplete : frontend.typeChecker);
TypeChecker& typeChecker =
(frontend.options.typecheckTwice_DEPRECATED || FFlag::LuauSeparateTypechecks ? frontend.typeCheckerForAutocomplete : frontend.typeChecker);
ModulePtr module = typeChecker.check(*sourceModule, Mode::Strict);

371
Analysis/src/Clone.cpp Normal file
View file

@ -0,0 +1,371 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Clone.h"
#include "Luau/Module.h"
#include "Luau/RecursionCounter.h"
#include "Luau/TypePack.h"
#include "Luau/Unifiable.h"
LUAU_FASTINTVARIABLE(LuauTypeCloneRecursionLimit, 300)
namespace Luau
{
namespace
{
struct TypePackCloner;
/*
* Both TypeCloner and TypePackCloner work by depositing the requested type variable into the appropriate 'seen' set.
* They do not return anything because their sole consumer (the deepClone function) already has a pointer into this storage.
*/
struct TypeCloner
{
TypeCloner(TypeArena& dest, TypeId typeId, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
: dest(dest)
, typeId(typeId)
, seenTypes(seenTypes)
, seenTypePacks(seenTypePacks)
, cloneState(cloneState)
{
}
TypeArena& dest;
TypeId typeId;
SeenTypes& seenTypes;
SeenTypePacks& seenTypePacks;
CloneState& cloneState;
template<typename T>
void defaultClone(const T& t);
void operator()(const Unifiable::Free& t);
void operator()(const Unifiable::Generic& t);
void operator()(const Unifiable::Bound<TypeId>& t);
void operator()(const Unifiable::Error& t);
void operator()(const PrimitiveTypeVar& t);
void operator()(const SingletonTypeVar& t);
void operator()(const FunctionTypeVar& t);
void operator()(const TableTypeVar& t);
void operator()(const MetatableTypeVar& t);
void operator()(const ClassTypeVar& t);
void operator()(const AnyTypeVar& t);
void operator()(const UnionTypeVar& t);
void operator()(const IntersectionTypeVar& t);
void operator()(const LazyTypeVar& t);
};
struct TypePackCloner
{
TypeArena& dest;
TypePackId typePackId;
SeenTypes& seenTypes;
SeenTypePacks& seenTypePacks;
CloneState& cloneState;
TypePackCloner(TypeArena& dest, TypePackId typePackId, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
: dest(dest)
, typePackId(typePackId)
, seenTypes(seenTypes)
, seenTypePacks(seenTypePacks)
, cloneState(cloneState)
{
}
template<typename T>
void defaultClone(const T& t)
{
TypePackId cloned = dest.addTypePack(TypePackVar{t});
seenTypePacks[typePackId] = cloned;
}
void operator()(const Unifiable::Free& t)
{
cloneState.encounteredFreeType = true;
TypePackId err = getSingletonTypes().errorRecoveryTypePack(getSingletonTypes().anyTypePack);
TypePackId cloned = dest.addTypePack(*err);
seenTypePacks[typePackId] = cloned;
}
void operator()(const Unifiable::Generic& t)
{
defaultClone(t);
}
void operator()(const Unifiable::Error& t)
{
defaultClone(t);
}
// While we are a-cloning, we can flatten out bound TypeVars and make things a bit tighter.
// We just need to be sure that we rewrite pointers both to the binder and the bindee to the same pointer.
void operator()(const Unifiable::Bound<TypePackId>& t)
{
TypePackId cloned = clone(t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypePacks[typePackId] = cloned;
}
void operator()(const VariadicTypePack& t)
{
TypePackId cloned = dest.addTypePack(TypePackVar{VariadicTypePack{clone(t.ty, dest, seenTypes, seenTypePacks, cloneState)}});
seenTypePacks[typePackId] = cloned;
}
void operator()(const TypePack& t)
{
TypePackId cloned = dest.addTypePack(TypePack{});
TypePack* destTp = getMutable<TypePack>(cloned);
LUAU_ASSERT(destTp != nullptr);
seenTypePacks[typePackId] = cloned;
for (TypeId ty : t.head)
destTp->head.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
if (t.tail)
destTp->tail = clone(*t.tail, dest, seenTypes, seenTypePacks, cloneState);
}
};
template<typename T>
void TypeCloner::defaultClone(const T& t)
{
TypeId cloned = dest.addType(t);
seenTypes[typeId] = cloned;
}
void TypeCloner::operator()(const Unifiable::Free& t)
{
cloneState.encounteredFreeType = true;
TypeId err = getSingletonTypes().errorRecoveryType(getSingletonTypes().anyType);
TypeId cloned = dest.addType(*err);
seenTypes[typeId] = cloned;
}
void TypeCloner::operator()(const Unifiable::Generic& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const Unifiable::Bound<TypeId>& t)
{
TypeId boundTo = clone(t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypes[typeId] = boundTo;
}
void TypeCloner::operator()(const Unifiable::Error& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const PrimitiveTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const SingletonTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const FunctionTypeVar& t)
{
TypeId result = dest.addType(FunctionTypeVar{TypeLevel{0, 0}, {}, {}, nullptr, nullptr, t.definition, t.hasSelf});
FunctionTypeVar* ftv = getMutable<FunctionTypeVar>(result);
LUAU_ASSERT(ftv != nullptr);
seenTypes[typeId] = result;
for (TypeId generic : t.generics)
ftv->generics.push_back(clone(generic, dest, seenTypes, seenTypePacks, cloneState));
for (TypePackId genericPack : t.genericPacks)
ftv->genericPacks.push_back(clone(genericPack, dest, seenTypes, seenTypePacks, cloneState));
ftv->tags = t.tags;
ftv->argTypes = clone(t.argTypes, dest, seenTypes, seenTypePacks, cloneState);
ftv->argNames = t.argNames;
ftv->retType = clone(t.retType, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const TableTypeVar& t)
{
// If table is now bound to another one, we ignore the content of the original
if (t.boundTo)
{
TypeId boundTo = clone(*t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypes[typeId] = boundTo;
return;
}
TypeId result = dest.addType(TableTypeVar{});
TableTypeVar* ttv = getMutable<TableTypeVar>(result);
LUAU_ASSERT(ttv != nullptr);
*ttv = t;
seenTypes[typeId] = result;
ttv->level = TypeLevel{0, 0};
for (const auto& [name, prop] : t.props)
ttv->props[name] = {clone(prop.type, dest, seenTypes, seenTypePacks, cloneState), prop.deprecated, {}, prop.location, prop.tags};
if (t.indexer)
ttv->indexer = TableIndexer{clone(t.indexer->indexType, dest, seenTypes, seenTypePacks, cloneState),
clone(t.indexer->indexResultType, dest, seenTypes, seenTypePacks, cloneState)};
for (TypeId& arg : ttv->instantiatedTypeParams)
arg = clone(arg, dest, seenTypes, seenTypePacks, cloneState);
for (TypePackId& arg : ttv->instantiatedTypePackParams)
arg = clone(arg, dest, seenTypes, seenTypePacks, cloneState);
if (ttv->state == TableState::Free)
{
cloneState.encounteredFreeType = true;
ttv->state = TableState::Sealed;
}
ttv->definitionModuleName = t.definitionModuleName;
ttv->methodDefinitionLocations = t.methodDefinitionLocations;
ttv->tags = t.tags;
}
void TypeCloner::operator()(const MetatableTypeVar& t)
{
TypeId result = dest.addType(MetatableTypeVar{});
MetatableTypeVar* mtv = getMutable<MetatableTypeVar>(result);
seenTypes[typeId] = result;
mtv->table = clone(t.table, dest, seenTypes, seenTypePacks, cloneState);
mtv->metatable = clone(t.metatable, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const ClassTypeVar& t)
{
TypeId result = dest.addType(ClassTypeVar{t.name, {}, std::nullopt, std::nullopt, t.tags, t.userData});
ClassTypeVar* ctv = getMutable<ClassTypeVar>(result);
seenTypes[typeId] = result;
for (const auto& [name, prop] : t.props)
ctv->props[name] = {clone(prop.type, dest, seenTypes, seenTypePacks, cloneState), prop.deprecated, {}, prop.location, prop.tags};
if (t.parent)
ctv->parent = clone(*t.parent, dest, seenTypes, seenTypePacks, cloneState);
if (t.metatable)
ctv->metatable = clone(*t.metatable, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const AnyTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const UnionTypeVar& t)
{
std::vector<TypeId> options;
options.reserve(t.options.size());
for (TypeId ty : t.options)
options.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
TypeId result = dest.addType(UnionTypeVar{std::move(options)});
seenTypes[typeId] = result;
}
void TypeCloner::operator()(const IntersectionTypeVar& t)
{
TypeId result = dest.addType(IntersectionTypeVar{});
seenTypes[typeId] = result;
IntersectionTypeVar* option = getMutable<IntersectionTypeVar>(result);
LUAU_ASSERT(option != nullptr);
for (TypeId ty : t.parts)
option->parts.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
}
void TypeCloner::operator()(const LazyTypeVar& t)
{
defaultClone(t);
}
} // anonymous namespace
TypePackId clone(TypePackId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
if (tp->persistent)
return tp;
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);
TypePackId& res = seenTypePacks[tp];
if (res == nullptr)
{
TypePackCloner cloner{dest, tp, seenTypes, seenTypePacks, cloneState};
Luau::visit(cloner, tp->ty); // Mutates the storage that 'res' points into.
}
return res;
}
TypeId clone(TypeId typeId, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
if (typeId->persistent)
return typeId;
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);
TypeId& res = seenTypes[typeId];
if (res == nullptr)
{
TypeCloner cloner{dest, typeId, seenTypes, seenTypePacks, cloneState};
Luau::visit(cloner, typeId->ty); // Mutates the storage that 'res' points into.
// Persistent types are not being cloned and we get the original type back which might be read-only
if (!res->persistent)
asMutable(res)->documentationSymbol = typeId->documentationSymbol;
}
return res;
}
TypeFun clone(const TypeFun& typeFun, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
TypeFun result;
for (auto param : typeFun.typeParams)
{
TypeId ty = clone(param.ty, dest, seenTypes, seenTypePacks, cloneState);
std::optional<TypeId> defaultValue;
if (param.defaultValue)
defaultValue = clone(*param.defaultValue, dest, seenTypes, seenTypePacks, cloneState);
result.typeParams.push_back({ty, defaultValue});
}
for (auto param : typeFun.typePackParams)
{
TypePackId tp = clone(param.tp, dest, seenTypes, seenTypePacks, cloneState);
std::optional<TypePackId> defaultValue;
if (param.defaultValue)
defaultValue = clone(*param.defaultValue, dest, seenTypes, seenTypePacks, cloneState);
result.typePackParams.push_back({tp, defaultValue});
}
result.type = clone(typeFun.type, dest, seenTypes, seenTypePacks, cloneState);
return result;
}
} // namespace Luau

View file

@ -1,6 +1,7 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Error.h"
#include "Luau/Clone.h"
#include "Luau/Module.h"
#include "Luau/StringUtils.h"
#include "Luau/ToString.h"

View file

@ -2,6 +2,7 @@
#include "Luau/Frontend.h"
#include "Luau/Common.h"
#include "Luau/Clone.h"
#include "Luau/Config.h"
#include "Luau/FileResolver.h"
#include "Luau/Parser.h"
@ -16,8 +17,11 @@
#include <chrono>
#include <stdexcept>
LUAU_FASTFLAG(LuauCyclicModuleTypeSurface)
LUAU_FASTFLAG(LuauInferInNoCheckMode)
LUAU_FASTFLAGVARIABLE(LuauKnowsTheDataModel3, false)
LUAU_FASTFLAGVARIABLE(LuauSeparateTypechecks, false)
LUAU_FASTINTVARIABLE(LuauAutocompleteCheckTimeoutMs, 0)
namespace Luau
{
@ -234,12 +238,6 @@ ErrorVec accumulateErrors(
return result;
}
struct RequireCycle
{
Location location;
std::vector<ModuleName> path; // one of the paths for a require() to go all the way back to the originating module
};
// Given a source node (start), find all requires that start a transitive dependency path that ends back at start
// For each such path, record the full path and the location of the require in the starting module.
// Note that this is O(V^2) for a fully connected graph and produces O(V) paths of length O(V)
@ -356,33 +354,55 @@ CheckResult Frontend::check(const ModuleName& name, std::optional<FrontendOption
LUAU_TIMETRACE_SCOPE("Frontend::check", "Frontend");
LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
FrontendOptions frontendOptions = optionOverride.value_or(options);
CheckResult checkResult;
auto it = sourceNodes.find(name);
if (it != sourceNodes.end() && !it->second.dirty)
if (it != sourceNodes.end() && !it->second.isDirty(frontendOptions.forAutocomplete))
{
// No recheck required.
if (FFlag::LuauSeparateTypechecks)
{
if (frontendOptions.forAutocomplete)
{
auto it2 = moduleResolverForAutocomplete.modules.find(name);
if (it2 == moduleResolverForAutocomplete.modules.end() || it2->second == nullptr)
throw std::runtime_error("Frontend::modules does not have data for " + name);
}
else
{
auto it2 = moduleResolver.modules.find(name);
if (it2 == moduleResolver.modules.end() || it2->second == nullptr)
throw std::runtime_error("Frontend::modules does not have data for " + name);
}
return CheckResult{accumulateErrors(
sourceNodes, frontendOptions.forAutocomplete ? moduleResolverForAutocomplete.modules : moduleResolver.modules, name)};
}
else
{
auto it2 = moduleResolver.modules.find(name);
if (it2 == moduleResolver.modules.end() || it2->second == nullptr)
throw std::runtime_error("Frontend::modules does not have data for " + name);
return CheckResult{accumulateErrors(sourceNodes, moduleResolver.modules, name)};
}
}
std::vector<ModuleName> buildQueue;
bool cycleDetected = parseGraph(buildQueue, checkResult, name);
FrontendOptions frontendOptions = optionOverride.value_or(options);
bool cycleDetected = parseGraph(buildQueue, checkResult, name, frontendOptions.forAutocomplete);
// Keep track of which AST nodes we've reported cycles in
std::unordered_set<AstNode*> reportedCycles;
double autocompleteTimeLimit = FInt::LuauAutocompleteCheckTimeoutMs / 1000.0;
for (const ModuleName& moduleName : buildQueue)
{
LUAU_ASSERT(sourceNodes.count(moduleName));
SourceNode& sourceNode = sourceNodes[moduleName];
if (!sourceNode.dirty)
if (!sourceNode.isDirty(frontendOptions.forAutocomplete))
continue;
LUAU_ASSERT(sourceModules.count(moduleName));
@ -408,13 +428,44 @@ CheckResult Frontend::check(const ModuleName& name, std::optional<FrontendOption
// This is used by the type checker to replace the resulting type of cyclic modules with any
sourceModule.cyclic = !requireCycles.empty();
if (FFlag::LuauSeparateTypechecks && frontendOptions.forAutocomplete)
{
// The autocomplete typecheck is always in strict mode with DM awareness
// to provide better type information for IDE features
if (FFlag::LuauCyclicModuleTypeSurface)
typeCheckerForAutocomplete.requireCycles = requireCycles;
if (autocompleteTimeLimit != 0.0)
typeCheckerForAutocomplete.finishTime = TimeTrace::getClock() + autocompleteTimeLimit;
else
typeCheckerForAutocomplete.finishTime = std::nullopt;
ModulePtr moduleForAutocomplete = typeCheckerForAutocomplete.check(sourceModule, Mode::Strict);
moduleResolverForAutocomplete.modules[moduleName] = moduleForAutocomplete;
if (moduleForAutocomplete->timeout)
checkResult.timeoutHits.push_back(moduleName);
stats.timeCheck += getTimestamp() - timestamp;
stats.filesStrict += 1;
sourceNode.dirtyAutocomplete = false;
continue;
}
if (FFlag::LuauCyclicModuleTypeSurface)
typeChecker.requireCycles = requireCycles;
ModulePtr module = typeChecker.check(sourceModule, mode, environmentScope);
// If we're typechecking twice, we do so.
// The second typecheck is always in strict mode with DM awareness
// to provide better typen information for IDE features.
if (frontendOptions.typecheckTwice)
if (!FFlag::LuauSeparateTypechecks && frontendOptions.typecheckTwice_DEPRECATED)
{
if (FFlag::LuauCyclicModuleTypeSurface)
typeCheckerForAutocomplete.requireCycles = requireCycles;
ModulePtr moduleForAutocomplete = typeCheckerForAutocomplete.check(sourceModule, Mode::Strict);
moduleResolverForAutocomplete.modules[moduleName] = moduleForAutocomplete;
}
@ -467,7 +518,7 @@ CheckResult Frontend::check(const ModuleName& name, std::optional<FrontendOption
return checkResult;
}
bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& checkResult, const ModuleName& root)
bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& checkResult, const ModuleName& root, bool forAutocomplete)
{
LUAU_TIMETRACE_SCOPE("Frontend::parseGraph", "Frontend");
LUAU_TIMETRACE_ARGUMENT("root", root.c_str());
@ -486,7 +537,7 @@ bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& chec
bool cyclic = false;
{
auto [sourceNode, _] = getSourceNode(checkResult, root);
auto [sourceNode, _] = getSourceNode(checkResult, root, forAutocomplete);
if (sourceNode)
stack.push_back(sourceNode);
}
@ -538,7 +589,7 @@ bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& chec
// this relies on the fact that markDirty marks reverse-dependencies dirty as well
// thus if a node is not dirty, all its transitive deps aren't dirty, which means that they won't ever need
// to be built, *and* can't form a cycle with any nodes we did process.
if (!it->second.dirty)
if (!it->second.isDirty(forAutocomplete))
continue;
// note: this check is technically redundant *except* that getSourceNode has somewhat broken memoization
@ -550,7 +601,7 @@ bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& chec
}
}
auto [sourceNode, _] = getSourceNode(checkResult, dep);
auto [sourceNode, _] = getSourceNode(checkResult, dep, forAutocomplete);
if (sourceNode)
{
stack.push_back(sourceNode);
@ -594,7 +645,7 @@ LintResult Frontend::lint(const ModuleName& name, std::optional<Luau::LintOption
LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
CheckResult checkResult;
auto [_sourceNode, sourceModule] = getSourceNode(checkResult, name);
auto [_sourceNode, sourceModule] = getSourceNode(checkResult, name, false);
if (!sourceModule)
return LintResult{}; // FIXME: We really should do something a bit more obvious when a file is too broken to lint.
@ -685,10 +736,10 @@ LintResult Frontend::lint(const SourceModule& module, std::optional<Luau::LintOp
return classifyLints(warnings, config);
}
bool Frontend::isDirty(const ModuleName& name) const
bool Frontend::isDirty(const ModuleName& name, bool forAutocomplete) const
{
auto it = sourceNodes.find(name);
return it == sourceNodes.end() || it->second.dirty;
return it == sourceNodes.end() || it->second.isDirty(forAutocomplete);
}
/*
@ -699,8 +750,16 @@ bool Frontend::isDirty(const ModuleName& name) const
*/
void Frontend::markDirty(const ModuleName& name, std::vector<ModuleName>* markedDirty)
{
if (FFlag::LuauSeparateTypechecks)
{
if (!moduleResolver.modules.count(name) && !moduleResolverForAutocomplete.modules.count(name))
return;
}
else
{
if (!moduleResolver.modules.count(name))
return;
}
std::unordered_map<ModuleName, std::vector<ModuleName>> reverseDeps;
for (const auto& module : sourceNodes)
@ -722,10 +781,21 @@ void Frontend::markDirty(const ModuleName& name, std::vector<ModuleName>* marked
if (markedDirty)
markedDirty->push_back(next);
if (FFlag::LuauSeparateTypechecks)
{
if (sourceNode.dirty && sourceNode.dirtyAutocomplete)
continue;
sourceNode.dirty = true;
sourceNode.dirtyAutocomplete = true;
}
else
{
if (sourceNode.dirty)
continue;
sourceNode.dirty = true;
}
if (0 == reverseDeps.count(name))
continue;
@ -752,13 +822,13 @@ const SourceModule* Frontend::getSourceModule(const ModuleName& moduleName) cons
}
// Read AST into sourceModules if necessary. Trace require()s. Report parse errors.
std::pair<SourceNode*, SourceModule*> Frontend::getSourceNode(CheckResult& checkResult, const ModuleName& name)
std::pair<SourceNode*, SourceModule*> Frontend::getSourceNode(CheckResult& checkResult, const ModuleName& name, bool forAutocomplete)
{
LUAU_TIMETRACE_SCOPE("Frontend::getSourceNode", "Frontend");
LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
auto it = sourceNodes.find(name);
if (it != sourceNodes.end() && !it->second.dirty)
if (it != sourceNodes.end() && !it->second.isDirty(forAutocomplete))
{
auto moduleIt = sourceModules.find(name);
if (moduleIt != sourceModules.end())
@ -801,7 +871,19 @@ std::pair<SourceNode*, SourceModule*> Frontend::getSourceNode(CheckResult& check
sourceNode.name = name;
sourceNode.requires.clear();
sourceNode.requireLocations.clear();
if (FFlag::LuauSeparateTypechecks)
{
if (it == sourceNodes.end())
{
sourceNode.dirty = true;
sourceNode.dirtyAutocomplete = true;
}
}
else
{
sourceNode.dirty = true;
}
for (const auto& [moduleName, location] : requireTrace.requires)
sourceNode.requires.insert(moduleName);

View file

@ -23,80 +23,45 @@ std::ostream& operator<<(std::ostream& stream, const AstName& name)
return stream << "<empty>";
}
std::ostream& operator<<(std::ostream& stream, const TypeMismatch& tm)
template<typename T>
static void errorToString(std::ostream& stream, const T& err)
{
return stream << "TypeMismatch { " << toString(tm.wantedType) << ", " << toString(tm.givenType) << " }";
}
if constexpr (false)
{
}
else if constexpr (std::is_same_v<T, TypeMismatch>)
stream << "TypeMismatch { " << toString(err.wantedType) << ", " << toString(err.givenType) << " }";
else if constexpr (std::is_same_v<T, UnknownSymbol>)
stream << "UnknownSymbol { " << err.name << " , context " << err.context << " }";
else if constexpr (std::is_same_v<T, UnknownProperty>)
stream << "UnknownProperty { " << toString(err.table) << ", key = " << err.key << " }";
else if constexpr (std::is_same_v<T, NotATable>)
stream << "NotATable { " << toString(err.ty) << " }";
else if constexpr (std::is_same_v<T, CannotExtendTable>)
stream << "CannotExtendTable { " << toString(err.tableType) << ", context " << err.context << ", prop \"" << err.prop << "\" }";
else if constexpr (std::is_same_v<T, OnlyTablesCanHaveMethods>)
stream << "OnlyTablesCanHaveMethods { " << toString(err.tableType) << " }";
else if constexpr (std::is_same_v<T, DuplicateTypeDefinition>)
stream << "DuplicateTypeDefinition { " << err.name << " }";
else if constexpr (std::is_same_v<T, CountMismatch>)
stream << "CountMismatch { expected " << err.expected << ", got " << err.actual << ", context " << err.context << " }";
else if constexpr (std::is_same_v<T, FunctionDoesNotTakeSelf>)
stream << "FunctionDoesNotTakeSelf { }";
else if constexpr (std::is_same_v<T, FunctionRequiresSelf>)
stream << "FunctionRequiresSelf { extraNils " << err.requiredExtraNils << " }";
else if constexpr (std::is_same_v<T, OccursCheckFailed>)
stream << "OccursCheckFailed { }";
else if constexpr (std::is_same_v<T, UnknownRequire>)
stream << "UnknownRequire { " << err.modulePath << " }";
else if constexpr (std::is_same_v<T, IncorrectGenericParameterCount>)
{
stream << "IncorrectGenericParameterCount { name = " << err.name;
std::ostream& operator<<(std::ostream& stream, const TypeError& error)
{
return stream << "TypeError { \"" << error.moduleName << "\", " << error.location << ", " << error.data << " }";
}
std::ostream& operator<<(std::ostream& stream, const UnknownSymbol& error)
{
return stream << "UnknownSymbol { " << error.name << " , context " << error.context << " }";
}
std::ostream& operator<<(std::ostream& stream, const UnknownProperty& error)
{
return stream << "UnknownProperty { " << toString(error.table) << ", key = " << error.key << " }";
}
std::ostream& operator<<(std::ostream& stream, const NotATable& ge)
{
return stream << "NotATable { " << toString(ge.ty) << " }";
}
std::ostream& operator<<(std::ostream& stream, const CannotExtendTable& error)
{
return stream << "CannotExtendTable { " << toString(error.tableType) << ", context " << error.context << ", prop \"" << error.prop << "\" }";
}
std::ostream& operator<<(std::ostream& stream, const OnlyTablesCanHaveMethods& error)
{
return stream << "OnlyTablesCanHaveMethods { " << toString(error.tableType) << " }";
}
std::ostream& operator<<(std::ostream& stream, const DuplicateTypeDefinition& error)
{
return stream << "DuplicateTypeDefinition { " << error.name << " }";
}
std::ostream& operator<<(std::ostream& stream, const CountMismatch& error)
{
return stream << "CountMismatch { expected " << error.expected << ", got " << error.actual << ", context " << error.context << " }";
}
std::ostream& operator<<(std::ostream& stream, const FunctionDoesNotTakeSelf&)
{
return stream << "FunctionDoesNotTakeSelf { }";
}
std::ostream& operator<<(std::ostream& stream, const FunctionRequiresSelf& error)
{
return stream << "FunctionRequiresSelf { extraNils " << error.requiredExtraNils << " }";
}
std::ostream& operator<<(std::ostream& stream, const OccursCheckFailed&)
{
return stream << "OccursCheckFailed { }";
}
std::ostream& operator<<(std::ostream& stream, const UnknownRequire& error)
{
return stream << "UnknownRequire { " << error.modulePath << " }";
}
std::ostream& operator<<(std::ostream& stream, const IncorrectGenericParameterCount& error)
{
stream << "IncorrectGenericParameterCount { name = " << error.name;
if (!error.typeFun.typeParams.empty() || !error.typeFun.typePackParams.empty())
if (!err.typeFun.typeParams.empty() || !err.typeFun.typePackParams.empty())
{
stream << "<";
bool first = true;
for (auto param : error.typeFun.typeParams)
for (auto param : err.typeFun.typeParams)
{
if (first)
first = false;
@ -106,7 +71,7 @@ std::ostream& operator<<(std::ostream& stream, const IncorrectGenericParameterCo
stream << toString(param.ty);
}
for (auto param : error.typeFun.typePackParams)
for (auto param : err.typeFun.typePackParams)
{
if (first)
first = false;
@ -119,31 +84,20 @@ std::ostream& operator<<(std::ostream& stream, const IncorrectGenericParameterCo
stream << ">";
}
stream << ", typeFun = " << toString(error.typeFun.type) << ", actualCount = " << error.actualParameters << " }";
return stream;
}
std::ostream& operator<<(std::ostream& stream, const SyntaxError& ge)
{
return stream << "SyntaxError { " << ge.message << " }";
}
std::ostream& operator<<(std::ostream& stream, const CodeTooComplex&)
{
return stream << "CodeTooComplex {}";
}
std::ostream& operator<<(std::ostream& stream, const UnificationTooComplex&)
{
return stream << "UnificationTooComplex {}";
}
std::ostream& operator<<(std::ostream& stream, const UnknownPropButFoundLikeProp& e)
{
stream << "UnknownPropButFoundLikeProp { key = '" << e.key << "', suggested = { ";
stream << ", typeFun = " << toString(err.typeFun.type) << ", actualCount = " << err.actualParameters << " }";
}
else if constexpr (std::is_same_v<T, SyntaxError>)
stream << "SyntaxError { " << err.message << " }";
else if constexpr (std::is_same_v<T, CodeTooComplex>)
stream << "CodeTooComplex {}";
else if constexpr (std::is_same_v<T, UnificationTooComplex>)
stream << "UnificationTooComplex {}";
else if constexpr (std::is_same_v<T, UnknownPropButFoundLikeProp>)
{
stream << "UnknownPropButFoundLikeProp { key = '" << err.key << "', suggested = { ";
bool first = true;
for (Name name : e.candidates)
for (Name name : err.candidates)
{
if (first)
first = false;
@ -153,40 +107,22 @@ std::ostream& operator<<(std::ostream& stream, const UnknownPropButFoundLikeProp
stream << "'" << name << "'";
}
return stream << " }, table = " << toString(e.table) << " } ";
}
std::ostream& operator<<(std::ostream& stream, const GenericError& ge)
{
return stream << "GenericError { " << ge.message << " }";
}
std::ostream& operator<<(std::ostream& stream, const CannotCallNonFunction& e)
{
return stream << "CannotCallNonFunction { " << toString(e.ty) << " }";
}
std::ostream& operator<<(std::ostream& stream, const FunctionExitsWithoutReturning& error)
{
return stream << "FunctionExitsWithoutReturning {" << toString(error.expectedReturnType) << "}";
}
std::ostream& operator<<(std::ostream& stream, const ExtraInformation& e)
{
return stream << "ExtraInformation { " << e.message << " }";
}
std::ostream& operator<<(std::ostream& stream, const DeprecatedApiUsed& e)
{
return stream << "DeprecatedApiUsed { " << e.symbol << ", useInstead = " << e.useInstead << " }";
}
std::ostream& operator<<(std::ostream& stream, const ModuleHasCyclicDependency& e)
{
stream << " }, table = " << toString(err.table) << " } ";
}
else if constexpr (std::is_same_v<T, GenericError>)
stream << "GenericError { " << err.message << " }";
else if constexpr (std::is_same_v<T, CannotCallNonFunction>)
stream << "CannotCallNonFunction { " << toString(err.ty) << " }";
else if constexpr (std::is_same_v<T, ExtraInformation>)
stream << "ExtraInformation { " << err.message << " }";
else if constexpr (std::is_same_v<T, DeprecatedApiUsed>)
stream << "DeprecatedApiUsed { " << err.symbol << ", useInstead = " << err.useInstead << " }";
else if constexpr (std::is_same_v<T, ModuleHasCyclicDependency>)
{
stream << "ModuleHasCyclicDependency {";
bool first = true;
for (const ModuleName& name : e.cycle)
for (const ModuleName& name : err.cycle)
{
if (first)
first = false;
@ -196,20 +132,24 @@ std::ostream& operator<<(std::ostream& stream, const ModuleHasCyclicDependency&
stream << name;
}
return stream << "}";
}
std::ostream& operator<<(std::ostream& stream, const IllegalRequire& e)
{
return stream << "IllegalRequire { " << e.moduleName << ", reason = " << e.reason << " }";
}
std::ostream& operator<<(std::ostream& stream, const MissingProperties& e)
{
stream << "MissingProperties { superType = '" << toString(e.superType) << "', subType = '" << toString(e.subType) << "', properties = { ";
stream << "}";
}
else if constexpr (std::is_same_v<T, IllegalRequire>)
stream << "IllegalRequire { " << err.moduleName << ", reason = " << err.reason << " }";
else if constexpr (std::is_same_v<T, FunctionExitsWithoutReturning>)
stream << "FunctionExitsWithoutReturning {" << toString(err.expectedReturnType) << "}";
else if constexpr (std::is_same_v<T, DuplicateGenericParameter>)
stream << "DuplicateGenericParameter { " + err.parameterName + " }";
else if constexpr (std::is_same_v<T, CannotInferBinaryOperation>)
stream << "CannotInferBinaryOperation { op = " + toString(err.op) + ", suggested = '" +
(err.suggestedToAnnotate ? *err.suggestedToAnnotate : "") + "', kind "
<< err.kind << "}";
else if constexpr (std::is_same_v<T, MissingProperties>)
{
stream << "MissingProperties { superType = '" << toString(err.superType) << "', subType = '" << toString(err.subType) << "', properties = { ";
bool first = true;
for (Name name : e.properties)
for (Name name : err.properties)
{
if (first)
first = false;
@ -219,37 +159,18 @@ std::ostream& operator<<(std::ostream& stream, const MissingProperties& e)
stream << "'" << name << "'";
}
return stream << " }, context " << e.context << " } ";
}
std::ostream& operator<<(std::ostream& stream, const DuplicateGenericParameter& error)
{
return stream << "DuplicateGenericParameter { " + error.parameterName + " }";
}
std::ostream& operator<<(std::ostream& stream, const CannotInferBinaryOperation& error)
{
return stream << "CannotInferBinaryOperation { op = " + toString(error.op) + ", suggested = '" +
(error.suggestedToAnnotate ? *error.suggestedToAnnotate : "") + "', kind "
<< error.kind << "}";
}
std::ostream& operator<<(std::ostream& stream, const SwappedGenericTypeParameter& error)
{
return stream << "SwappedGenericTypeParameter { name = '" + error.name + "', kind = " + std::to_string(error.kind) + " }";
}
std::ostream& operator<<(std::ostream& stream, const OptionalValueAccess& error)
{
return stream << "OptionalValueAccess { optional = '" + toString(error.optional) + "' }";
}
std::ostream& operator<<(std::ostream& stream, const MissingUnionProperty& error)
{
stream << "MissingUnionProperty { type = '" + toString(error.type) + "', missing = { ";
stream << " }, context " << err.context << " } ";
}
else if constexpr (std::is_same_v<T, SwappedGenericTypeParameter>)
stream << "SwappedGenericTypeParameter { name = '" + err.name + "', kind = " + std::to_string(err.kind) + " }";
else if constexpr (std::is_same_v<T, OptionalValueAccess>)
stream << "OptionalValueAccess { optional = '" + toString(err.optional) + "' }";
else if constexpr (std::is_same_v<T, MissingUnionProperty>)
{
stream << "MissingUnionProperty { type = '" + toString(err.type) + "', missing = { ";
bool first = true;
for (auto ty : error.missing)
for (auto ty : err.missing)
{
if (first)
first = false;
@ -259,15 +180,28 @@ std::ostream& operator<<(std::ostream& stream, const MissingUnionProperty& error
stream << "'" << toString(ty) << "'";
}
return stream << " }, key = '" + error.key + "' }";
stream << " }, key = '" + err.key + "' }";
}
else if constexpr (std::is_same_v<T, TypesAreUnrelated>)
stream << "TypesAreUnrelated { left = '" + toString(err.left) + "', right = '" + toString(err.right) + "' }";
else
static_assert(always_false_v<T>, "Non-exhaustive type switch");
}
std::ostream& operator<<(std::ostream& stream, const TypesAreUnrelated& error)
std::ostream& operator<<(std::ostream& stream, const TypeErrorData& data)
{
stream << "TypesAreUnrelated { left = '" + toString(error.left) + "', right = '" + toString(error.right) + "' }";
auto cb = [&](const auto& e) {
return errorToString(stream, e);
};
visit(cb, data);
return stream;
}
std::ostream& operator<<(std::ostream& stream, const TypeError& error)
{
return stream << "TypeError { \"" << error.moduleName << "\", " << error.location << ", " << error.data << " }";
}
std::ostream& operator<<(std::ostream& stream, const TableState& tv)
{
return stream << static_cast<std::underlying_type<TableState>::type>(tv);
@ -283,15 +217,4 @@ std::ostream& operator<<(std::ostream& stream, const TypePackVar& tv)
return stream << toString(tv);
}
std::ostream& operator<<(std::ostream& lhs, const TypeErrorData& ted)
{
Luau::visit(
[&](const auto& a) {
lhs << a;
},
ted);
return lhs;
}
} // namespace Luau

View file

@ -2,6 +2,7 @@
#include "Luau/Module.h"
#include "Luau/Common.h"
#include "Luau/Clone.h"
#include "Luau/RecursionCounter.h"
#include "Luau/Scope.h"
#include "Luau/TypeInfer.h"
@ -12,7 +13,6 @@
#include <algorithm>
LUAU_FASTFLAGVARIABLE(DebugLuauFreezeArena, false)
LUAU_FASTINTVARIABLE(LuauTypeCloneRecursionLimit, 300)
LUAU_FASTFLAGVARIABLE(LuauCloneDeclaredGlobals, false)
namespace Luau
@ -113,363 +113,6 @@ TypePackId TypeArena::addTypePack(TypePackVar tp)
return allocated;
}
namespace
{
struct TypePackCloner;
/*
* Both TypeCloner and TypePackCloner work by depositing the requested type variable into the appropriate 'seen' set.
* They do not return anything because their sole consumer (the deepClone function) already has a pointer into this storage.
*/
struct TypeCloner
{
TypeCloner(TypeArena& dest, TypeId typeId, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
: dest(dest)
, typeId(typeId)
, seenTypes(seenTypes)
, seenTypePacks(seenTypePacks)
, cloneState(cloneState)
{
}
TypeArena& dest;
TypeId typeId;
SeenTypes& seenTypes;
SeenTypePacks& seenTypePacks;
CloneState& cloneState;
template<typename T>
void defaultClone(const T& t);
void operator()(const Unifiable::Free& t);
void operator()(const Unifiable::Generic& t);
void operator()(const Unifiable::Bound<TypeId>& t);
void operator()(const Unifiable::Error& t);
void operator()(const PrimitiveTypeVar& t);
void operator()(const SingletonTypeVar& t);
void operator()(const FunctionTypeVar& t);
void operator()(const TableTypeVar& t);
void operator()(const MetatableTypeVar& t);
void operator()(const ClassTypeVar& t);
void operator()(const AnyTypeVar& t);
void operator()(const UnionTypeVar& t);
void operator()(const IntersectionTypeVar& t);
void operator()(const LazyTypeVar& t);
};
struct TypePackCloner
{
TypeArena& dest;
TypePackId typePackId;
SeenTypes& seenTypes;
SeenTypePacks& seenTypePacks;
CloneState& cloneState;
TypePackCloner(TypeArena& dest, TypePackId typePackId, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
: dest(dest)
, typePackId(typePackId)
, seenTypes(seenTypes)
, seenTypePacks(seenTypePacks)
, cloneState(cloneState)
{
}
template<typename T>
void defaultClone(const T& t)
{
TypePackId cloned = dest.addTypePack(TypePackVar{t});
seenTypePacks[typePackId] = cloned;
}
void operator()(const Unifiable::Free& t)
{
cloneState.encounteredFreeType = true;
TypePackId err = getSingletonTypes().errorRecoveryTypePack(getSingletonTypes().anyTypePack);
TypePackId cloned = dest.addTypePack(*err);
seenTypePacks[typePackId] = cloned;
}
void operator()(const Unifiable::Generic& t)
{
defaultClone(t);
}
void operator()(const Unifiable::Error& t)
{
defaultClone(t);
}
// While we are a-cloning, we can flatten out bound TypeVars and make things a bit tighter.
// We just need to be sure that we rewrite pointers both to the binder and the bindee to the same pointer.
void operator()(const Unifiable::Bound<TypePackId>& t)
{
TypePackId cloned = clone(t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypePacks[typePackId] = cloned;
}
void operator()(const VariadicTypePack& t)
{
TypePackId cloned = dest.addTypePack(TypePackVar{VariadicTypePack{clone(t.ty, dest, seenTypes, seenTypePacks, cloneState)}});
seenTypePacks[typePackId] = cloned;
}
void operator()(const TypePack& t)
{
TypePackId cloned = dest.addTypePack(TypePack{});
TypePack* destTp = getMutable<TypePack>(cloned);
LUAU_ASSERT(destTp != nullptr);
seenTypePacks[typePackId] = cloned;
for (TypeId ty : t.head)
destTp->head.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
if (t.tail)
destTp->tail = clone(*t.tail, dest, seenTypes, seenTypePacks, cloneState);
}
};
template<typename T>
void TypeCloner::defaultClone(const T& t)
{
TypeId cloned = dest.addType(t);
seenTypes[typeId] = cloned;
}
void TypeCloner::operator()(const Unifiable::Free& t)
{
cloneState.encounteredFreeType = true;
TypeId err = getSingletonTypes().errorRecoveryType(getSingletonTypes().anyType);
TypeId cloned = dest.addType(*err);
seenTypes[typeId] = cloned;
}
void TypeCloner::operator()(const Unifiable::Generic& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const Unifiable::Bound<TypeId>& t)
{
TypeId boundTo = clone(t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypes[typeId] = boundTo;
}
void TypeCloner::operator()(const Unifiable::Error& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const PrimitiveTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const SingletonTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const FunctionTypeVar& t)
{
TypeId result = dest.addType(FunctionTypeVar{TypeLevel{0, 0}, {}, {}, nullptr, nullptr, t.definition, t.hasSelf});
FunctionTypeVar* ftv = getMutable<FunctionTypeVar>(result);
LUAU_ASSERT(ftv != nullptr);
seenTypes[typeId] = result;
for (TypeId generic : t.generics)
ftv->generics.push_back(clone(generic, dest, seenTypes, seenTypePacks, cloneState));
for (TypePackId genericPack : t.genericPacks)
ftv->genericPacks.push_back(clone(genericPack, dest, seenTypes, seenTypePacks, cloneState));
ftv->tags = t.tags;
ftv->argTypes = clone(t.argTypes, dest, seenTypes, seenTypePacks, cloneState);
ftv->argNames = t.argNames;
ftv->retType = clone(t.retType, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const TableTypeVar& t)
{
// If table is now bound to another one, we ignore the content of the original
if (t.boundTo)
{
TypeId boundTo = clone(*t.boundTo, dest, seenTypes, seenTypePacks, cloneState);
seenTypes[typeId] = boundTo;
return;
}
TypeId result = dest.addType(TableTypeVar{});
TableTypeVar* ttv = getMutable<TableTypeVar>(result);
LUAU_ASSERT(ttv != nullptr);
*ttv = t;
seenTypes[typeId] = result;
ttv->level = TypeLevel{0, 0};
for (const auto& [name, prop] : t.props)
ttv->props[name] = {clone(prop.type, dest, seenTypes, seenTypePacks, cloneState), prop.deprecated, {}, prop.location, prop.tags};
if (t.indexer)
ttv->indexer = TableIndexer{clone(t.indexer->indexType, dest, seenTypes, seenTypePacks, cloneState),
clone(t.indexer->indexResultType, dest, seenTypes, seenTypePacks, cloneState)};
for (TypeId& arg : ttv->instantiatedTypeParams)
arg = clone(arg, dest, seenTypes, seenTypePacks, cloneState);
for (TypePackId& arg : ttv->instantiatedTypePackParams)
arg = clone(arg, dest, seenTypes, seenTypePacks, cloneState);
if (ttv->state == TableState::Free)
{
cloneState.encounteredFreeType = true;
ttv->state = TableState::Sealed;
}
ttv->definitionModuleName = t.definitionModuleName;
ttv->methodDefinitionLocations = t.methodDefinitionLocations;
ttv->tags = t.tags;
}
void TypeCloner::operator()(const MetatableTypeVar& t)
{
TypeId result = dest.addType(MetatableTypeVar{});
MetatableTypeVar* mtv = getMutable<MetatableTypeVar>(result);
seenTypes[typeId] = result;
mtv->table = clone(t.table, dest, seenTypes, seenTypePacks, cloneState);
mtv->metatable = clone(t.metatable, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const ClassTypeVar& t)
{
TypeId result = dest.addType(ClassTypeVar{t.name, {}, std::nullopt, std::nullopt, t.tags, t.userData});
ClassTypeVar* ctv = getMutable<ClassTypeVar>(result);
seenTypes[typeId] = result;
for (const auto& [name, prop] : t.props)
ctv->props[name] = {clone(prop.type, dest, seenTypes, seenTypePacks, cloneState), prop.deprecated, {}, prop.location, prop.tags};
if (t.parent)
ctv->parent = clone(*t.parent, dest, seenTypes, seenTypePacks, cloneState);
if (t.metatable)
ctv->metatable = clone(*t.metatable, dest, seenTypes, seenTypePacks, cloneState);
}
void TypeCloner::operator()(const AnyTypeVar& t)
{
defaultClone(t);
}
void TypeCloner::operator()(const UnionTypeVar& t)
{
std::vector<TypeId> options;
options.reserve(t.options.size());
for (TypeId ty : t.options)
options.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
TypeId result = dest.addType(UnionTypeVar{std::move(options)});
seenTypes[typeId] = result;
}
void TypeCloner::operator()(const IntersectionTypeVar& t)
{
TypeId result = dest.addType(IntersectionTypeVar{});
seenTypes[typeId] = result;
IntersectionTypeVar* option = getMutable<IntersectionTypeVar>(result);
LUAU_ASSERT(option != nullptr);
for (TypeId ty : t.parts)
option->parts.push_back(clone(ty, dest, seenTypes, seenTypePacks, cloneState));
}
void TypeCloner::operator()(const LazyTypeVar& t)
{
defaultClone(t);
}
} // anonymous namespace
TypePackId clone(TypePackId tp, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
if (tp->persistent)
return tp;
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);
TypePackId& res = seenTypePacks[tp];
if (res == nullptr)
{
TypePackCloner cloner{dest, tp, seenTypes, seenTypePacks, cloneState};
Luau::visit(cloner, tp->ty); // Mutates the storage that 'res' points into.
}
return res;
}
TypeId clone(TypeId typeId, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
if (typeId->persistent)
return typeId;
RecursionLimiter _ra(&cloneState.recursionCount, FInt::LuauTypeCloneRecursionLimit);
TypeId& res = seenTypes[typeId];
if (res == nullptr)
{
TypeCloner cloner{dest, typeId, seenTypes, seenTypePacks, cloneState};
Luau::visit(cloner, typeId->ty); // Mutates the storage that 'res' points into.
// Persistent types are not being cloned and we get the original type back which might be read-only
if (!res->persistent)
asMutable(res)->documentationSymbol = typeId->documentationSymbol;
}
return res;
}
TypeFun clone(const TypeFun& typeFun, TypeArena& dest, SeenTypes& seenTypes, SeenTypePacks& seenTypePacks, CloneState& cloneState)
{
TypeFun result;
for (auto param : typeFun.typeParams)
{
TypeId ty = clone(param.ty, dest, seenTypes, seenTypePacks, cloneState);
std::optional<TypeId> defaultValue;
if (param.defaultValue)
defaultValue = clone(*param.defaultValue, dest, seenTypes, seenTypePacks, cloneState);
result.typeParams.push_back({ty, defaultValue});
}
for (auto param : typeFun.typePackParams)
{
TypePackId tp = clone(param.tp, dest, seenTypes, seenTypePacks, cloneState);
std::optional<TypePackId> defaultValue;
if (param.defaultValue)
defaultValue = clone(*param.defaultValue, dest, seenTypes, seenTypePacks, cloneState);
result.typePackParams.push_back({tp, defaultValue});
}
result.type = clone(typeFun.type, dest, seenTypes, seenTypePacks, cloneState);
return result;
}
ScopePtr Module::getModuleScope() const
{
LUAU_ASSERT(!scopes.empty());

View file

@ -7,6 +7,8 @@
#include <algorithm>
#include <stdexcept>
LUAU_FASTFLAGVARIABLE(LuauTxnLogPreserveOwner, false)
namespace Luau
{
@ -78,11 +80,32 @@ void TxnLog::concat(TxnLog rhs)
void TxnLog::commit()
{
if (FFlag::LuauTxnLogPreserveOwner)
{
for (auto& [ty, rep] : typeVarChanges)
{
TypeArena* owningArena = ty->owningArena;
TypeVar* mtv = asMutable(ty);
*mtv = rep.get()->pending;
mtv->owningArena = owningArena;
}
for (auto& [tp, rep] : typePackChanges)
{
TypeArena* owningArena = tp->owningArena;
TypePackVar* mpv = asMutable(tp);
*mpv = rep.get()->pending;
mpv->owningArena = owningArena;
}
}
else
{
for (auto& [ty, rep] : typeVarChanges)
*asMutable(ty) = rep.get()->pending;
for (auto& [tp, rep] : typePackChanges)
*asMutable(tp) = rep.get()->pending;
}
clear();
}

View file

@ -22,6 +22,9 @@ LUAU_FASTINTVARIABLE(LuauTypeInferRecursionLimit, 500)
LUAU_FASTINTVARIABLE(LuauTypeInferTypePackLoopLimit, 5000)
LUAU_FASTINTVARIABLE(LuauCheckRecursionLimit, 500)
LUAU_FASTFLAG(LuauKnowsTheDataModel3)
LUAU_FASTFLAG(LuauSeparateTypechecks)
LUAU_FASTFLAG(LuauAutocompleteSingletonTypes)
LUAU_FASTFLAGVARIABLE(LuauCyclicModuleTypeSurface, false)
LUAU_FASTFLAGVARIABLE(LuauEqConstraint, false)
LUAU_FASTFLAGVARIABLE(LuauWeakEqConstraint, false) // Eventually removed as false.
LUAU_FASTFLAGVARIABLE(DebugLuauFreezeDuringUnification, false)
@ -35,7 +38,7 @@ LUAU_FASTFLAGVARIABLE(LuauExpectedTypesOfProperties, false)
LUAU_FASTFLAGVARIABLE(LuauErrorRecoveryType, false)
LUAU_FASTFLAGVARIABLE(LuauOnlyMutateInstantiatedTables, false)
LUAU_FASTFLAGVARIABLE(LuauPropertiesGetExpectedType, false)
LUAU_FASTFLAGVARIABLE(LuauStatFunctionSimplify3, false)
LUAU_FASTFLAGVARIABLE(LuauStatFunctionSimplify4, false)
LUAU_FASTFLAGVARIABLE(LuauUnsealedTableLiteral, false)
LUAU_FASTFLAG(LuauTypeMismatchModuleName)
LUAU_FASTFLAGVARIABLE(LuauTwoPassAliasDefinitionFix, false)
@ -46,6 +49,7 @@ LUAU_FASTFLAGVARIABLE(LuauDoNotTryToReduce, false)
LUAU_FASTFLAGVARIABLE(LuauDoNotAccidentallyDependOnPointerOrdering, false)
LUAU_FASTFLAGVARIABLE(LuauFixArgumentCountMismatchAmountWithGenericTypes, false)
LUAU_FASTFLAGVARIABLE(LuauFixIncorrectLineNumberDuplicateType, false)
LUAU_FASTFLAGVARIABLE(LuauCheckImplicitNumbericKeys, false)
LUAU_FASTFLAG(LuauAnyInIsOptionalIsOptional)
LUAU_FASTFLAGVARIABLE(LuauDecoupleOperatorInferenceFromUnifiedTypeInference, false)
LUAU_FASTFLAGVARIABLE(LuauArgCountMismatchSaysAtLeastWhenVariadic, false)
@ -53,6 +57,11 @@ LUAU_FASTFLAGVARIABLE(LuauArgCountMismatchSaysAtLeastWhenVariadic, false)
namespace Luau
{
const char* TimeLimitError::what() const throw()
{
return "Typeinfer failed to complete in allotted time";
}
static bool typeCouldHaveMetatable(TypeId ty)
{
return get<TableTypeVar>(follow(ty)) || get<ClassTypeVar>(follow(ty)) || get<MetatableTypeVar>(follow(ty));
@ -251,6 +260,12 @@ ModulePtr TypeChecker::check(const SourceModule& module, Mode mode, std::optiona
currentModule.reset(new Module());
currentModule->type = module.type;
if (FFlag::LuauSeparateTypechecks)
{
currentModule->allocator = module.allocator;
currentModule->names = module.names;
}
iceHandler->moduleName = module.name;
ScopePtr parentScope = environmentScope.value_or(globalScope);
@ -271,7 +286,21 @@ ModulePtr TypeChecker::check(const SourceModule& module, Mode mode, std::optiona
if (prepareModuleScope)
prepareModuleScope(module.name, currentModule->getModuleScope());
if (FFlag::LuauSeparateTypechecks)
{
try
{
checkBlock(moduleScope, *module.root);
}
catch (const TimeLimitError&)
{
currentModule->timeout = true;
}
}
else
{
checkBlock(moduleScope, *module.root);
}
if (get<FreeTypePack>(follow(moduleScope->returnType)))
moduleScope->returnType = addTypePack(TypePack{{}, std::nullopt});
@ -366,6 +395,9 @@ void TypeChecker::check(const ScopePtr& scope, const AstStat& program)
}
else
ice("Unknown AstStat");
if (FFlag::LuauSeparateTypechecks && finishTime && TimeTrace::getClock() > *finishTime)
throw TimeLimitError();
}
// This particular overload is for do...end. If you need to not increase the scope level, use checkBlock directly.
@ -1115,21 +1147,17 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
scope->bindings[name->local] = {anyIfNonstrict(quantify(funScope, ty, name->local->location)), name->local->location};
return;
}
else if (auto name = function.name->as<AstExprIndexName>(); name && FFlag::LuauStatFunctionSimplify3)
else if (auto name = function.name->as<AstExprIndexName>(); name && FFlag::LuauStatFunctionSimplify4)
{
TypeId exprTy = checkExpr(scope, *name->expr).type;
TableTypeVar* ttv = getMutableTableType(exprTy);
if (!ttv)
{
if (isTableIntersection(exprTy))
reportError(TypeError{function.location, CannotExtendTable{exprTy, CannotExtendTable::Property, name->index.value}});
else if (!get<ErrorTypeVar>(exprTy) && !get<AnyTypeVar>(exprTy))
reportError(TypeError{function.location, OnlyTablesCanHaveMethods{exprTy}});
}
else if (ttv->state == TableState::Sealed)
{
if (!getIndexTypeFromType(scope, exprTy, name->index.value, name->indexLocation, false))
{
if (ttv || isTableIntersection(exprTy))
reportError(TypeError{function.location, CannotExtendTable{exprTy, CannotExtendTable::Property, name->index.value}});
else
reportError(TypeError{function.location, OnlyTablesCanHaveMethods{exprTy}});
}
ty = follow(ty);
@ -1153,7 +1181,7 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
if (ttv && ttv->state != TableState::Sealed)
ttv->props[name->index.value] = {follow(quantify(funScope, ty, name->indexLocation)), /* deprecated */ false, {}, name->indexLocation};
}
else if (FFlag::LuauStatFunctionSimplify3)
else if (FFlag::LuauStatFunctionSimplify4)
{
LUAU_ASSERT(function.name->is<AstExprError>());
@ -1163,7 +1191,7 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
}
else if (function.func->self)
{
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify3);
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify4);
AstExprIndexName* indexName = function.name->as<AstExprIndexName>();
if (!indexName)
@ -1202,7 +1230,7 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
}
else
{
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify3);
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify4);
TypeId leftType = checkLValueBinding(scope, *function.name);
@ -2030,7 +2058,11 @@ TypeId TypeChecker::checkExprTable(
indexer = expectedTable->indexer;
if (indexer)
{
if (FFlag::LuauCheckImplicitNumbericKeys)
unify(numberType, indexer->indexType, value->location);
unify(valueType, indexer->indexResultType, value->location);
}
else
indexer = TableIndexer{numberType, anyIfNonstrict(valueType)};
}
@ -2984,10 +3016,24 @@ TypeId TypeChecker::checkFunctionName(const ScopePtr& scope, AstExpr& funName, T
else if (auto indexName = funName.as<AstExprIndexName>())
{
TypeId lhsType = checkExpr(scope, *indexName->expr).type;
if (get<ErrorTypeVar>(lhsType) || get<AnyTypeVar>(lhsType))
if (!FFlag::LuauStatFunctionSimplify4 && (get<ErrorTypeVar>(lhsType) || get<AnyTypeVar>(lhsType)))
return lhsType;
TableTypeVar* ttv = getMutableTableType(lhsType);
if (FFlag::LuauStatFunctionSimplify4)
{
if (!ttv || ttv->state == TableState::Sealed)
{
if (auto ty = getIndexTypeFromType(scope, lhsType, indexName->index.value, indexName->indexLocation, false))
return *ty;
return errorRecoveryType(scope);
}
}
else
{
if (!ttv)
{
if (!FFlag::LuauErrorRecoveryType && !isTableIntersection(lhsType))
@ -2997,22 +3043,6 @@ TypeId TypeChecker::checkFunctionName(const ScopePtr& scope, AstExpr& funName, T
return errorRecoveryType(scope);
}
if (FFlag::LuauStatFunctionSimplify3)
{
if (lhsType->persistent)
return errorRecoveryType(scope);
// Cannot extend sealed table, but we dont report an error here because it will be reported during AstStatFunction check
if (ttv->state == TableState::Sealed)
{
if (ttv->indexer && isPrim(ttv->indexer->indexType, PrimitiveTypeVar::String))
return ttv->indexer->indexResultType;
else
return errorRecoveryType(scope);
}
}
else
{
if (lhsType->persistent || ttv->state == TableState::Sealed)
return errorRecoveryType(scope);
}
@ -3020,7 +3050,12 @@ TypeId TypeChecker::checkFunctionName(const ScopePtr& scope, AstExpr& funName, T
Name name = indexName->index.value;
if (ttv->props.count(name))
{
if (FFlag::LuauStatFunctionSimplify4)
return ttv->props[name].type;
else
return errorRecoveryType(scope);
}
Property& property = ttv->props[name];
@ -4155,6 +4190,20 @@ TypeId TypeChecker::checkRequire(const ScopePtr& scope, const ModuleInfo& module
return anyType;
}
// Types of requires that transitively refer to current module have to be replaced with 'any'
std::string humanReadableName;
if (FFlag::LuauCyclicModuleTypeSurface)
{
humanReadableName = resolver->getHumanReadableModuleName(moduleInfo.name);
for (const auto& [location, path] : requireCycles)
{
if (!path.empty() && path.front() == humanReadableName)
return anyType;
}
}
ModulePtr module = resolver->getModule(moduleInfo.name);
if (!module)
{
@ -4162,18 +4211,32 @@ TypeId TypeChecker::checkRequire(const ScopePtr& scope, const ModuleInfo& module
// either the file does not exist or there's a cycle. If there's a cycle
// we will already have reported the error.
if (!resolver->moduleExists(moduleInfo.name) && !moduleInfo.optional)
{
if (FFlag::LuauCyclicModuleTypeSurface)
{
reportError(TypeError{location, UnknownRequire{humanReadableName}});
}
else
{
std::string reportedModulePath = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(TypeError{location, UnknownRequire{reportedModulePath}});
}
}
return errorRecoveryType(scope);
}
if (module->type != SourceCode::Module)
{
if (FFlag::LuauCyclicModuleTypeSurface)
{
reportError(location, IllegalRequire{humanReadableName, "Module is not a ModuleScript. It cannot be required."});
}
else
{
std::string humanReadableName = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(location, IllegalRequire{humanReadableName, "Module is not a ModuleScript. It cannot be required."});
}
return errorRecoveryType(scope);
}
@ -4184,9 +4247,16 @@ TypeId TypeChecker::checkRequire(const ScopePtr& scope, const ModuleInfo& module
std::optional<TypeId> moduleType = first(modulePack);
if (!moduleType)
{
if (FFlag::LuauCyclicModuleTypeSurface)
{
reportError(location, IllegalRequire{humanReadableName, "Module does not return exactly 1 value. It cannot be required."});
}
else
{
std::string humanReadableName = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(location, IllegalRequire{humanReadableName, "Module does not return exactly 1 value. It cannot be required."});
}
return errorRecoveryType(scope);
}
@ -4629,7 +4699,9 @@ TypeId TypeChecker::freshType(TypeLevel level)
TypeId TypeChecker::singletonType(bool value)
{
// TODO: cache singleton types
if (FFlag::LuauAutocompleteSingletonTypes)
return value ? getSingletonTypes().trueType : getSingletonTypes().falseType;
return currentModule->internalTypes.addType(TypeVar(SingletonTypeVar(BooleanSingleton{value})));
}

View file

@ -652,6 +652,8 @@ static TypeVar numberType_{PrimitiveTypeVar{PrimitiveTypeVar::Number}, /*persist
static TypeVar stringType_{PrimitiveTypeVar{PrimitiveTypeVar::String}, /*persistent*/ true};
static TypeVar booleanType_{PrimitiveTypeVar{PrimitiveTypeVar::Boolean}, /*persistent*/ true};
static TypeVar threadType_{PrimitiveTypeVar{PrimitiveTypeVar::Thread}, /*persistent*/ true};
static TypeVar trueType_{SingletonTypeVar{BooleanSingleton{true}}, /*persistent*/ true};
static TypeVar falseType_{SingletonTypeVar{BooleanSingleton{false}}, /*persistent*/ true};
static TypeVar anyType_{AnyTypeVar{}};
static TypeVar errorType_{ErrorTypeVar{}};
static TypeVar optionalNumberType_{UnionTypeVar{{&numberType_, &nilType_}}};
@ -665,6 +667,8 @@ SingletonTypes::SingletonTypes()
, stringType(&stringType_)
, booleanType(&booleanType_)
, threadType(&threadType_)
, trueType(&trueType_)
, falseType(&falseType_)
, anyType(&anyType_)
, optionalNumberType(&optionalNumberType_)
, anyTypePack(&anyTypePack_)

View file

@ -9,14 +9,21 @@
LUAU_FASTFLAG(DebugLuauTimeTracing)
namespace Luau
{
namespace TimeTrace
{
double getClock();
uint32_t getClockMicroseconds();
} // namespace TimeTrace
} // namespace Luau
#if defined(LUAU_ENABLE_TIME_TRACE)
namespace Luau
{
namespace TimeTrace
{
uint32_t getClockMicroseconds();
struct Token
{
const char* name;

View file

@ -10,6 +10,7 @@
// See docs/SyntaxChanges.md for an explanation.
LUAU_FASTINTVARIABLE(LuauRecursionLimit, 1000)
LUAU_FASTINTVARIABLE(LuauParseErrorLimit, 100)
LUAU_FASTFLAGVARIABLE(LuauParseRecoverUnexpectedPack, false)
namespace Luau
{
@ -1420,6 +1421,11 @@ AstType* Parser::parseTypeAnnotation(TempVector<AstType*>& parts, const Location
parts.push_back(parseSimpleTypeAnnotation(/* allowPack= */ false).type);
isIntersection = true;
}
else if (FFlag::LuauParseRecoverUnexpectedPack && c == Lexeme::Dot3)
{
report(lexer.current().location, "Unexpected '...' after type annotation");
nextLexeme();
}
else
break;
}
@ -1536,6 +1542,11 @@ AstTypeOrPack Parser::parseSimpleTypeAnnotation(bool allowPack)
prefix = name.name;
name = parseIndexName("field name", pointPosition);
}
else if (FFlag::LuauParseRecoverUnexpectedPack && lexer.current().type == Lexeme::Dot3)
{
report(lexer.current().location, "Unexpected '...' after type name; type pack is not allowed in this context");
nextLexeme();
}
else if (name.name == "typeof")
{
Lexeme typeofBegin = lexer.current();

View file

@ -26,9 +26,6 @@
#include <time.h>
LUAU_FASTFLAGVARIABLE(DebugLuauTimeTracing, false)
#if defined(LUAU_ENABLE_TIME_TRACE)
namespace Luau
{
namespace TimeTrace
@ -67,6 +64,14 @@ static double getClockTimestamp()
#endif
}
double getClock()
{
static double period = getClockPeriod();
static double start = getClockTimestamp();
return (getClockTimestamp() - start) * period;
}
uint32_t getClockMicroseconds()
{
static double period = getClockPeriod() * 1e6;
@ -74,7 +79,15 @@ uint32_t getClockMicroseconds()
return uint32_t((getClockTimestamp() - start) * period);
}
} // namespace TimeTrace
} // namespace Luau
#if defined(LUAU_ENABLE_TIME_TRACE)
namespace Luau
{
namespace TimeTrace
{
struct GlobalContext
{
GlobalContext() = default;

View file

@ -290,7 +290,8 @@ struct ConstantVisitor : AstVisitor
Constant la = analyze(expr->left);
Constant ra = analyze(expr->right);
if (la.type != Constant::Type_Unknown && ra.type != Constant::Type_Unknown)
// note: ra doesn't need to be constant to fold and/or
if (la.type != Constant::Type_Unknown)
foldBinary(result, expr->op, la, ra);
}
else if (AstExprTypeAssertion* expr = node->as<AstExprTypeAssertion>())

View file

@ -47,6 +47,7 @@ target_sources(Luau.Analysis PRIVATE
Analysis/include/Luau/Autocomplete.h
Analysis/include/Luau/BuiltinDefinitions.h
Analysis/include/Luau/Config.h
Analysis/include/Luau/Clone.h
Analysis/include/Luau/Documentation.h
Analysis/include/Luau/Error.h
Analysis/include/Luau/FileResolver.h
@ -85,6 +86,7 @@ target_sources(Luau.Analysis PRIVATE
Analysis/src/Autocomplete.cpp
Analysis/src/BuiltinDefinitions.cpp
Analysis/src/Config.cpp
Analysis/src/Clone.cpp
Analysis/src/Error.cpp
Analysis/src/Frontend.cpp
Analysis/src/IostreamHelpers.cpp

View file

@ -14,6 +14,6 @@ LUAI_FUNC UpVal* luaF_findupval(lua_State* L, StkId level);
LUAI_FUNC void luaF_close(lua_State* L, StkId level);
LUAI_FUNC void luaF_freeproto(lua_State* L, Proto* f, struct lua_Page* page);
LUAI_FUNC void luaF_freeclosure(lua_State* L, Closure* c, struct lua_Page* page);
void luaF_unlinkupval(UpVal* uv);
LUAI_FUNC void luaF_unlinkupval(UpVal* uv);
LUAI_FUNC void luaF_freeupval(lua_State* L, UpVal* uv, struct lua_Page* page);
LUAI_FUNC const LocVar* luaF_getlocal(const Proto* func, int local_number, int pc);

View file

@ -201,7 +201,7 @@ static int tmove(lua_State* L)
void (*telemetrycb)(lua_State* L, int f, int e, int t, int nf, int nt) = lua_table_move_telemetry;
if (DFFlag::LuauTableMoveTelemetry2 && telemetrycb)
if (DFFlag::LuauTableMoveTelemetry2 && telemetrycb && e >= f)
{
int nf = lua_objlen(L, 1);
int nt = lua_objlen(L, tt);

View file

@ -14,6 +14,7 @@
LUAU_FASTFLAG(LuauTraceTypesInNonstrictMode2)
LUAU_FASTFLAG(LuauSetMetatableDoesNotTimeTravel)
LUAU_FASTFLAG(LuauSeparateTypechecks)
using namespace Luau;
@ -25,6 +26,11 @@ static std::optional<AutocompleteEntryMap> nullCallback(std::string tag, std::op
template<class BaseType>
struct ACFixtureImpl : BaseType
{
ACFixtureImpl()
: Fixture(true, true)
{
}
AutocompleteResult autocomplete(unsigned row, unsigned column)
{
return Luau::autocomplete(this->frontend, "MainModule", Position{row, column}, nullCallback);
@ -72,7 +78,25 @@ struct ACFixtureImpl : BaseType
}
LUAU_ASSERT("Digit expected after @ symbol" && prevChar != '@');
return Fixture::check(filteredSource);
return BaseType::check(filteredSource);
}
LoadDefinitionFileResult loadDefinition(const std::string& source)
{
if (FFlag::LuauSeparateTypechecks)
{
TypeChecker& typeChecker = this->frontend.typeCheckerForAutocomplete;
unfreeze(typeChecker.globalTypes);
LoadDefinitionFileResult result = loadDefinitionFile(typeChecker, typeChecker.globalScope, source, "@test");
freeze(typeChecker.globalTypes);
REQUIRE_MESSAGE(result.success, "loadDefinition: unable to load definition file");
return result;
}
else
{
return BaseType::loadDefinition(source);
}
}
const Position& getPosition(char marker) const
@ -2496,7 +2520,7 @@ local t = {
CHECK(ac.entryMap.count("second"));
}
TEST_CASE_FIXTURE(Fixture, "autocomplete_documentation_symbols")
TEST_CASE_FIXTURE(ACFixture, "autocomplete_documentation_symbols")
{
loadDefinition(R"(
declare y: {
@ -2504,13 +2528,11 @@ TEST_CASE_FIXTURE(Fixture, "autocomplete_documentation_symbols")
}
)");
fileResolver.source["Module/A"] = R"(
local a = y.
)";
check(R"(
local a = y.@1
)");
frontend.check("Module/A");
auto ac = autocomplete(frontend, "Module/A", Position{1, 21}, nullCallback);
auto ac = autocomplete('1');
REQUIRE(ac.entryMap.count("x"));
CHECK_EQ(ac.entryMap["x"].documentationSymbol, "@test/global/y.x");
@ -2736,6 +2758,107 @@ TEST_CASE_FIXTURE(ACFixture, "autocomplete_on_string_singletons")
CHECK(ac.entryMap.count("format"));
}
TEST_CASE_FIXTURE(ACFixture, "autocomplete_string_singletons")
{
ScopedFastFlag luauAutocompleteSingletonTypes{"LuauAutocompleteSingletonTypes", true};
ScopedFastFlag luauExpectedTypesOfProperties{"LuauExpectedTypesOfProperties", true};
check(R"(
type tag = "cat" | "dog"
local function f(a: tag) end
f("@1")
f(@2)
local x: tag = "@3"
)");
auto ac = autocomplete('1');
CHECK(ac.entryMap.count("cat"));
CHECK(ac.entryMap.count("dog"));
ac = autocomplete('2');
CHECK(ac.entryMap.count("\"cat\""));
CHECK(ac.entryMap.count("\"dog\""));
ac = autocomplete('3');
CHECK(ac.entryMap.count("cat"));
CHECK(ac.entryMap.count("dog"));
check(R"(
type tagged = {tag:"cat", fieldx:number} | {tag:"dog", fieldy:number}
local x: tagged = {tag="@4"}
)");
ac = autocomplete('4');
CHECK(ac.entryMap.count("cat"));
CHECK(ac.entryMap.count("dog"));
}
TEST_CASE_FIXTURE(ACFixture, "autocomplete_string_singleton_equality")
{
ScopedFastFlag luauAutocompleteSingletonTypes{"LuauAutocompleteSingletonTypes", true};
check(R"(
type tagged = {tag:"cat", fieldx:number} | {tag:"dog", fieldy:number}
local x: tagged = {tag="cat", fieldx=2}
if x.tag == "@1" or "@2" ~= x.tag then end
)");
auto ac = autocomplete('1');
CHECK(ac.entryMap.count("cat"));
CHECK(ac.entryMap.count("dog"));
ac = autocomplete('2');
CHECK(ac.entryMap.count("cat"));
CHECK(ac.entryMap.count("dog"));
// CLI-48823: assignment to x.tag should also autocomplete, but union l-values are not supported yet
}
TEST_CASE_FIXTURE(ACFixture, "autocomplete_boolean_singleton")
{
ScopedFastFlag luauAutocompleteSingletonTypes{"LuauAutocompleteSingletonTypes", true};
check(R"(
local function f(x: true) end
f(@1)
)");
auto ac = autocomplete('1');
REQUIRE(ac.entryMap.count("true"));
CHECK(ac.entryMap["true"].typeCorrect == TypeCorrectKind::Correct);
REQUIRE(ac.entryMap.count("false"));
CHECK(ac.entryMap["false"].typeCorrect == TypeCorrectKind::None);
}
TEST_CASE_FIXTURE(ACFixture, "autocomplete_string_singleton_escape")
{
ScopedFastFlag luauAutocompleteSingletonTypes{"LuauAutocompleteSingletonTypes", true};
check(R"(
type tag = "strange\t\"cat\"" | 'nice\t"dog"'
local function f(x: tag) end
f(@1)
f("@2")
)");
auto ac = autocomplete('1');
CHECK(ac.entryMap.count("\"strange\\t\\\"cat\\\"\""));
CHECK(ac.entryMap.count("\"nice\\t\\\"dog\\\"\""));
ac = autocomplete('2');
CHECK(ac.entryMap.count("strange\\t\\\"cat\\\""));
CHECK(ac.entryMap.count("nice\\t\\\"dog\\\""));
}
TEST_CASE_FIXTURE(ACFixture, "function_in_assignment_has_parentheses_2")
{
check(R"(

View file

@ -1074,6 +1074,39 @@ RETURN R1 1
)");
}
TEST_CASE("AndOrFoldLeft")
{
// constant folding and/or expression is possible even if just the left hand is constant
CHECK_EQ("\n" + compileFunction0("local a = false if a and b then b() end"), R"(
RETURN R0 0
)");
CHECK_EQ("\n" + compileFunction0("local a = true if a or b then b() end"), R"(
GETIMPORT R0 1
CALL R0 0 0
RETURN R0 0
)");
// however, if right hand side is constant we can't constant fold the entire expression
// (note that we don't need to evaluate the right hand side, but we do need a branch)
CHECK_EQ("\n" + compileFunction0("local a = false if b and a then b() end"), R"(
GETIMPORT R0 1
JUMPIFNOT R0 +4
RETURN R0 0
GETIMPORT R0 1
CALL R0 0 0
RETURN R0 0
)");
CHECK_EQ("\n" + compileFunction0("local a = true if b or a then b() end"), R"(
GETIMPORT R0 1
JUMPIF R0 +0
GETIMPORT R0 1
CALL R0 0 0
RETURN R0 0
)");
}
TEST_CASE("AndOrChainCodegen")
{
const char* source = R"(

View file

@ -83,7 +83,7 @@ std::optional<std::string> TestFileResolver::getEnvironmentForModule(const Modul
return std::nullopt;
}
Fixture::Fixture(bool freeze)
Fixture::Fixture(bool freeze, bool prepareAutocomplete)
: sff_DebugLuauFreezeArena("DebugLuauFreezeArena", freeze)
, frontend(&fileResolver, &configResolver, {/* retainFullTypeGraphs= */ true})
, typeChecker(frontend.typeChecker)
@ -93,8 +93,11 @@ Fixture::Fixture(bool freeze)
configResolver.defaultConfig.parseOptions.captureComments = true;
registerBuiltinTypes(frontend.typeChecker);
if (prepareAutocomplete)
registerBuiltinTypes(frontend.typeCheckerForAutocomplete);
registerTestTypes();
Luau::freeze(frontend.typeChecker.globalTypes);
Luau::freeze(frontend.typeCheckerForAutocomplete.globalTypes);
Luau::setPrintLine([](auto s) {});
}

View file

@ -91,7 +91,7 @@ struct TestConfigResolver : ConfigResolver
struct Fixture
{
explicit Fixture(bool freeze = true);
explicit Fixture(bool freeze = true, bool prepareAutocomplete = false);
~Fixture();
// Throws Luau::ParseErrors if the parse fails.

View file

@ -384,6 +384,70 @@ TEST_CASE_FIXTURE(FrontendFixture, "cycle_error_paths")
CHECK_EQ(ce2->cycle[1], "game/Gui/Modules/A");
}
TEST_CASE_FIXTURE(FrontendFixture, "cycle_incremental_type_surface")
{
ScopedFastFlag luauCyclicModuleTypeSurface{"LuauCyclicModuleTypeSurface", true};
fileResolver.source["game/A"] = R"(
return {hello = 2}
)";
CheckResult result = frontend.check("game/A");
LUAU_REQUIRE_NO_ERRORS(result);
fileResolver.source["game/A"] = R"(
local me = require(game.A)
return {hello = 2}
)";
frontend.markDirty("game/A");
result = frontend.check("game/A");
LUAU_REQUIRE_ERRORS(result);
auto ty = requireType("game/A", "me");
CHECK_EQ(toString(ty), "any");
}
TEST_CASE_FIXTURE(FrontendFixture, "cycle_incremental_type_surface_longer")
{
ScopedFastFlag luauCyclicModuleTypeSurface{"LuauCyclicModuleTypeSurface", true};
fileResolver.source["game/A"] = R"(
return {mod_a = 2}
)";
CheckResult result = frontend.check("game/A");
LUAU_REQUIRE_NO_ERRORS(result);
fileResolver.source["game/B"] = R"(
local me = require(game.A)
return {mod_b = 4}
)";
result = frontend.check("game/B");
LUAU_REQUIRE_NO_ERRORS(result);
fileResolver.source["game/A"] = R"(
local me = require(game.B)
return {mod_a_prime = 3}
)";
frontend.markDirty("game/A");
frontend.markDirty("game/B");
result = frontend.check("game/A");
LUAU_REQUIRE_ERRORS(result);
TypeId tyA = requireType("game/A", "me");
CHECK_EQ(toString(tyA), "any");
result = frontend.check("game/B");
LUAU_REQUIRE_ERRORS(result);
TypeId tyB = requireType("game/B", "me");
CHECK_EQ(toString(tyB), "any");
}
TEST_CASE_FIXTURE(FrontendFixture, "dont_reparse_clean_file_when_linting")
{
fileResolver.source["Modules/A"] = R"(

View file

@ -1,4 +1,5 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Clone.h"
#include "Luau/Module.h"
#include "Luau/Scope.h"

View file

@ -2022,6 +2022,15 @@ TEST_CASE_FIXTURE(Fixture, "parse_type_alias_default_type_errors")
matchParseError("type Y<T... = (string) -> number> = {}", "Expected type pack after '=', got type", Location{{0, 14}, {0, 32}});
}
TEST_CASE_FIXTURE(Fixture, "parse_type_pack_errors")
{
ScopedFastFlag luauParseRecoverUnexpectedPack{"LuauParseRecoverUnexpectedPack", true};
matchParseError("type Y<T...> = {a: T..., b: number}", "Unexpected '...' after type name; type pack is not allowed in this context",
Location{{0, 20}, {0, 23}});
matchParseError("type Y<T...> = {a: (number | string)...", "Unexpected '...' after type annotation", Location{{0, 36}, {0, 39}});
}
TEST_CASE_FIXTURE(Fixture, "parse_if_else_expression")
{
{
@ -2590,4 +2599,16 @@ type Y<T..., U = T...> = (T...) -> U...
CHECK_EQ(1, result.errors.size());
}
TEST_CASE_FIXTURE(Fixture, "recover_unexpected_type_pack")
{
ScopedFastFlag luauParseRecoverUnexpectedPack{"LuauParseRecoverUnexpectedPack", true};
ParseResult result = tryParse(R"(
type X<T...> = { a: T..., b: number }
type Y<T> = { a: T..., b: number }
type Z<T> = { a: string | T..., b: number }
)");
REQUIRE_EQ(3, result.errors.size());
}
TEST_SUITE_END();

View file

@ -1270,7 +1270,7 @@ caused by:
TEST_CASE_FIXTURE(Fixture, "function_decl_quantify_right_type")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true};
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
fileResolver.source["game/isAMagicMock"] = R"(
--!nonstrict
@ -1294,7 +1294,7 @@ end
TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true};
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"(
function string.len(): number
@ -1316,7 +1316,7 @@ print(string.len('hello'))
TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite_2")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true};
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
ScopedFastFlag inferStatFunction{"LuauInferStatFunction", true};
CheckResult result = check(R"(
@ -1324,12 +1324,12 @@ local t: { f: ((x: number) -> number)? } = {}
function t.f(x)
print(x + 5)
return x .. "asd"
return x .. "asd" -- 1st error: we know that return type is a number, not a string
end
t.f = function(x)
print(x + 5)
return x .. "asd"
return x .. "asd" -- 2nd error: we know that return type is a number, not a string
end
)");
@ -1338,6 +1338,33 @@ end
CHECK_EQ(toString(result.errors[1]), R"(Type 'string' could not be converted into 'number')");
}
TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_unsealed_overwrite")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
ScopedFastFlag inferStatFunction{"LuauInferStatFunction", true};
CheckResult result = check(R"(
local t = { f = nil :: ((x: number) -> number)? }
function t.f(x: string): string -- 1st error: new function value type is incompatible
return x .. "asd"
end
t.f = function(x)
print(x + 5)
return x .. "asd" -- 2nd error: we know that return type is a number, not a string
end
)");
LUAU_REQUIRE_ERROR_COUNT(2, result);
CHECK_EQ(toString(result.errors[0]), R"(Type '(string) -> string' could not be converted into '((number) -> number)?'
caused by:
None of the union options are compatible. For example: Type '(string) -> string' could not be converted into '(number) -> number'
caused by:
Argument #1 type is not compatible. Type 'number' could not be converted into 'string')");
CHECK_EQ(toString(result.errors[1]), R"(Type 'string' could not be converted into 'number')");
}
TEST_CASE_FIXTURE(Fixture, "strict_mode_ok_with_missing_arguments")
{
ScopedFastFlag sff{"LuauAnyInIsOptionalIsOptional", true};
@ -1352,7 +1379,7 @@ TEST_CASE_FIXTURE(Fixture, "strict_mode_ok_with_missing_arguments")
TEST_CASE_FIXTURE(Fixture, "function_statement_sealed_table_assignment_through_indexer")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true};
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"(
local t: {[string]: () -> number} = {}

View file

@ -311,6 +311,8 @@ TEST_CASE_FIXTURE(Fixture, "table_intersection_write_sealed")
TEST_CASE_FIXTURE(Fixture, "table_intersection_write_sealed_indirect")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"(
type X = { x: (number) -> number }
type Y = { y: (string) -> string }
@ -326,10 +328,39 @@ TEST_CASE_FIXTURE(Fixture, "table_intersection_write_sealed_indirect")
function xy:w(a:number) return a * 10 end
)");
LUAU_REQUIRE_ERROR_COUNT(3, result);
CHECK_EQ(toString(result.errors[0]), "Cannot add property 'z' to table 'X & Y'");
CHECK_EQ(toString(result.errors[1]), "Cannot add property 'y' to table 'X & Y'");
CHECK_EQ(toString(result.errors[2]), "Cannot add property 'w' to table 'X & Y'");
LUAU_REQUIRE_ERROR_COUNT(4, result);
CHECK_EQ(toString(result.errors[0]), R"(Type '(string, number) -> string' could not be converted into '(string) -> string'
caused by:
Argument count mismatch. Function expects 2 arguments, but only 1 is specified)");
CHECK_EQ(toString(result.errors[1]), "Cannot add property 'z' to table 'X & Y'");
CHECK_EQ(toString(result.errors[2]), "Type 'number' could not be converted into 'string'");
CHECK_EQ(toString(result.errors[3]), "Cannot add property 'w' to table 'X & Y'");
}
TEST_CASE_FIXTURE(Fixture, "table_write_sealed_indirect")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
// After normalization, previous 'table_intersection_write_sealed_indirect' is identical to this one
CheckResult result = check(R"(
type XY = { x: (number) -> number, y: (string) -> string }
local xy : XY = {
x = function(a: number) return -a end,
y = function(a: string) return a .. "b" end
}
function xy.z(a:number) return a * 10 end
function xy:y(a:number) return a * 10 end
function xy:w(a:number) return a * 10 end
)");
LUAU_REQUIRE_ERROR_COUNT(4, result);
CHECK_EQ(toString(result.errors[0]), R"(Type '(string, number) -> string' could not be converted into '(string) -> string'
caused by:
Argument count mismatch. Function expects 2 arguments, but only 1 is specified)");
CHECK_EQ(toString(result.errors[1]), "Cannot add property 'z' to table 'XY'");
CHECK_EQ(toString(result.errors[2]), "Type 'number' could not be converted into 'string'");
CHECK_EQ(toString(result.errors[3]), "Cannot add property 'w' to table 'XY'");
}
TEST_CASE_FIXTURE(Fixture, "table_intersection_setmetatable")

View file

@ -95,6 +95,8 @@ end
)");
LUAU_REQUIRE_ERROR_COUNT(2, result);
CHECK_EQ(toString(result.errors[0]), "Cannot add method to non-table type 'number'");
CHECK_EQ(toString(result.errors[1]), "Type 'number' could not be converted into 'string'");
}
TEST_SUITE_END();

View file

@ -2922,4 +2922,19 @@ TEST_CASE_FIXTURE(Fixture, "inferred_properties_of_a_table_should_start_with_the
LUAU_REQUIRE_NO_ERRORS(result);
}
TEST_CASE_FIXTURE(Fixture, "mixed_tables_with_implicit_numbered_keys")
{
ScopedFastFlag sff{"LuauCheckImplicitNumbericKeys", true};
CheckResult result = check(R"(
local t: { [string]: number } = { 5, 6, 7 }
)");
LUAU_REQUIRE_ERROR_COUNT(3, result);
CHECK_EQ("Type 'number' could not be converted into 'string'", toString(result.errors[0]));
CHECK_EQ("Type 'number' could not be converted into 'string'", toString(result.errors[1]));
CHECK_EQ("Type 'number' could not be converted into 'string'", toString(result.errors[2]));
}
TEST_SUITE_END();

View file

@ -242,4 +242,30 @@ TEST_CASE_FIXTURE(TryUnifyFixture, "cli_50320_follow_in_any_unification")
state.tryUnify(&func, typeChecker.anyType);
}
TEST_CASE_FIXTURE(TryUnifyFixture, "txnlog_preserves_type_owner")
{
ScopedFastFlag luauTxnLogPreserveOwner{"LuauTxnLogPreserveOwner", true};
TypeId a = arena.addType(TypeVar{FreeTypeVar{TypeLevel{}}});
TypeId b = typeChecker.numberType;
state.tryUnify(a, b);
state.log.commit();
CHECK_EQ(a->owningArena, &arena);
}
TEST_CASE_FIXTURE(TryUnifyFixture, "txnlog_preserves_pack_owner")
{
ScopedFastFlag luauTxnLogPreserveOwner{"LuauTxnLogPreserveOwner", true};
TypePackId a = arena.addTypePack(TypePackVar{FreeTypePack{TypeLevel{}}});
TypePackId b = typeChecker.anyTypePack;
state.tryUnify(a, b);
state.log.commit();
CHECK_EQ(a->owningArena, &arena);
}
TEST_SUITE_END();

View file

@ -513,4 +513,29 @@ TEST_CASE_FIXTURE(Fixture, "dont_allow_cyclic_unions_to_be_inferred")
LUAU_REQUIRE_NO_ERRORS(result);
}
TEST_CASE_FIXTURE(Fixture, "table_union_write_indirect")
{
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"(
type A = { x: number, y: (number) -> string } | { z: number, y: (number) -> string }
local a:A = nil
function a.y(x)
return tostring(x * 2)
end
function a.y(x: string): number
return tonumber(x) or 0
end
)");
LUAU_REQUIRE_ERROR_COUNT(1, result);
// NOTE: union normalization will improve this message
CHECK_EQ(toString(result.errors[0]),
R"(Type '(string) -> number' could not be converted into '((number) -> string) | ((number) -> string)'; none of the union options are compatible)");
}
TEST_SUITE_END();

View file

@ -37,7 +37,7 @@ def getType(target, typeName):
return ty
def luau_variant_summary(valobj, internal_dict, options):
type_id = valobj.GetChildMemberWithName("typeid").GetValueAsUnsigned()
type_id = valobj.GetChildMemberWithName("typeId").GetValueAsUnsigned()
storage = valobj.GetChildMemberWithName("storage")
params = templateParams(valobj.GetType().GetCanonicalType().GetName())
stored_type = params[type_id]
@ -89,7 +89,7 @@ class LuauVariantSyntheticChildrenProvider:
return None
def update(self):
self.type_index = self.valobj.GetChildMemberWithName("typeid").GetValueAsSigned()
self.type_index = self.valobj.GetChildMemberWithName("typeId").GetValueAsSigned()
self.type_params = templateParams(self.valobj.GetType().GetCanonicalType().GetName())
if len(self.type_params) > self.type_index: