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 <vector>
#include <optional> #include <optional>
LUAU_FASTFLAG(LuauSeparateTypechecks)
namespace Luau namespace Luau
{ {
@ -55,10 +57,19 @@ std::optional<ModuleName> pathExprToModuleName(const ModuleName& currentModuleNa
struct SourceNode struct SourceNode
{ {
bool isDirty(bool forAutocomplete) const
{
if (FFlag::LuauSeparateTypechecks)
return forAutocomplete ? dirtyAutocomplete : dirty;
else
return dirty;
}
ModuleName name; ModuleName name;
std::unordered_set<ModuleName> requires; std::unordered_set<ModuleName> requires;
std::vector<std::pair<ModuleName, Location>> requireLocations; std::vector<std::pair<ModuleName, Location>> requireLocations;
bool dirty = true; bool dirty = true;
bool dirtyAutocomplete = true;
}; };
struct FrontendOptions struct FrontendOptions
@ -71,12 +82,16 @@ struct FrontendOptions
// When true, we run typechecking twice, once in the regular mode, and once in strict mode // 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). // 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 struct CheckResult
{ {
std::vector<TypeError> errors; std::vector<TypeError> errors;
std::vector<ModuleName> timeoutHits;
}; };
struct FrontendModuleResolver : ModuleResolver struct FrontendModuleResolver : ModuleResolver
@ -123,7 +138,7 @@ struct Frontend
CheckResult check(const SourceModule& module); // OLD. TODO KILL CheckResult check(const SourceModule& module); // OLD. TODO KILL
LintResult lint(const SourceModule& module, std::optional<LintOptions> enabledLintWarnings = {}); 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); void markDirty(const ModuleName& name, std::vector<ModuleName>* markedDirty = nullptr);
/** Borrow a pointer into the SourceModule cache. /** Borrow a pointer into the SourceModule cache.
@ -147,10 +162,10 @@ struct Frontend
void applyBuiltinDefinitionToEnvironment(const std::string& environmentName, const std::string& definitionName); void applyBuiltinDefinitionToEnvironment(const std::string& environmentName, const std::string& definitionName);
private: 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); 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); static LintResult classifyLints(const std::vector<LintWarning>& warnings, const Config& config);

View file

@ -29,8 +29,8 @@ struct SourceModule
std::optional<std::string> environmentName; std::optional<std::string> environmentName;
bool cyclic = false; bool cyclic = false;
std::unique_ptr<Allocator> allocator; std::shared_ptr<Allocator> allocator;
std::unique_ptr<AstNameTable> names; std::shared_ptr<AstNameTable> names;
std::vector<ParseError> parseErrors; std::vector<ParseError> parseErrors;
AstStatBlock* root = nullptr; AstStatBlock* root = nullptr;
@ -48,6 +48,12 @@ struct SourceModule
bool isWithinComment(const SourceModule& sourceModule, Position pos); 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 struct TypeArena
{ {
TypedAllocator<TypeVar> typeVars; TypedAllocator<TypeVar> typeVars;
@ -77,20 +83,6 @@ struct TypeArena
void freeze(TypeArena& arena); void freeze(TypeArena& arena);
void unfreeze(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 struct Module
{ {
~Module(); ~Module();
@ -98,6 +90,10 @@ struct Module
TypeArena interfaceTypes; TypeArena interfaceTypes;
TypeArena internalTypes; 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 std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr}; DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
@ -109,6 +105,7 @@ struct Module
ErrorVec errors; ErrorVec errors;
Mode mode; Mode mode;
SourceCode::Type type; SourceCode::Type type;
bool timeout = false;
ScopePtr getModuleScope() const; ScopePtr getModuleScope() const;

View file

@ -124,6 +124,12 @@ struct HashBoolNamePair
size_t operator()(const std::pair<bool, Name>& pair) const; 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 // All TypeVars are retained via Environment::typeVars. All TypeIds
// within a program are borrowed pointers into this set. // within a program are borrowed pointers into this set.
struct TypeChecker struct TypeChecker
@ -413,6 +419,10 @@ public:
UnifierSharedState unifierState; UnifierSharedState unifierState;
std::vector<RequireCycle> requireCycles;
std::optional<double> finishTime;
public: public:
const TypeId nilType; const TypeId nilType;
const TypeId numberType; const TypeId numberType;

View file

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

View file

@ -2,45 +2,14 @@
#pragma once #pragma once
#include "Luau/Common.h" #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 <new>
#include <type_traits> #include <type_traits>
#include <initializer_list> #include <initializer_list>
#include <stddef.h> #include <stddef.h>
#endif
namespace Luau 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> template<typename... Ts>
class Variant 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> template<class Visitor, typename... Ts>
auto visit(Visitor&& vis, const Variant<Ts...>& var) 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>; 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>>...>, static_assert(std::conjunction_v<std::is_same<Result, std::invoke_result_t<Visitor, Ts>>...>,
"visitor result type must be consistent between alternatives"); "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> template<class Visitor, typename... Ts>
auto visit(Visitor&& vis, Variant<Ts...>& var) 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&>; 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&>>...>, static_assert(std::conjunction_v<std::is_same<Result, std::invoke_result_t<Visitor, Ts&>>...>,
"visitor result type must be consistent between alternatives"); "visitor result type must be consistent between alternatives");
@ -294,7 +267,6 @@ auto visit(Visitor&& vis, Variant<Ts...>& var)
return res; return res;
} }
} }
#endif
template<class> template<class>
inline constexpr bool always_false_v = false; inline constexpr bool always_false_v = false;

View file

@ -14,6 +14,7 @@
#include <utility> #include <utility>
LUAU_FASTFLAGVARIABLE(LuauIfElseExprFixCompletionIssue, false); LUAU_FASTFLAGVARIABLE(LuauIfElseExprFixCompletionIssue, false);
LUAU_FASTFLAGVARIABLE(LuauAutocompleteSingletonTypes, false);
LUAU_FASTFLAG(LuauSelfCallAutocompleteFix) LUAU_FASTFLAG(LuauSelfCallAutocompleteFix)
static const std::unordered_set<std::string> kStatementStartingKeywords = { static const std::unordered_set<std::string> kStatementStartingKeywords = {
@ -625,6 +626,31 @@ AutocompleteEntryMap autocompleteModuleTypes(const Module& module, Position posi
return result; 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) static bool canSuggestInferredType(ScopePtr scope, TypeId ty)
{ {
ty = follow(ty); ty = follow(ty);
@ -1309,6 +1335,26 @@ static void autocompleteExpression(const SourceModule& sourceModule, const Modul
scope = scope->parent; 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 correctForNil = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.nilType);
TypeCorrectKind correctForBoolean = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.booleanType); TypeCorrectKind correctForBoolean = checkTypeCorrectKind(module, typeArena, node, position, typeChecker.booleanType);
TypeCorrectKind correctForFunction = TypeCorrectKind correctForFunction =
@ -1321,6 +1367,7 @@ static void autocompleteExpression(const SourceModule& sourceModule, const Modul
result["not"] = {AutocompleteEntryKind::Keyword}; result["not"] = {AutocompleteEntryKind::Keyword};
result["function"] = {AutocompleteEntryKind::Keyword, std::nullopt, false, false, correctForFunction}; result["function"] = {AutocompleteEntryKind::Keyword, std::nullopt, false, false, correctForFunction};
} }
}
} }
static AutocompleteEntryMap autocompleteExpression(const SourceModule& sourceModule, const Module& module, const TypeChecker& typeChecker, 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>()) 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 (finder.ancestry.size() >= 2)
{ {
if (auto idxExpr = finder.ancestry.at(finder.ancestry.size() - 2)->as<AstExprIndexExpr>()) if (auto idxExpr = finder.ancestry.at(finder.ancestry.size() - 2)->as<AstExprIndexExpr>())
{ {
if (auto it = module->astTypes.find(idxExpr->expr)) 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>()) 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) 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. // FIXME: We can improve performance here by parsing without checking.
// The old type graph is probably fine. (famous last words!) // The old type graph is probably fine. (famous last words!)
// FIXME: We don't need to typecheck for script analysis here, just for autocomplete. // FIXME: We don't need to typecheck for script analysis here, just for autocomplete.
frontend.check(moduleName); frontend.check(moduleName);
}
const SourceModule* sourceModule = frontend.getSourceModule(moduleName); const SourceModule* sourceModule = frontend.getSourceModule(moduleName);
if (!sourceModule) if (!sourceModule)
return {}; return {};
TypeChecker& typeChecker = (frontend.options.typecheckTwice ? frontend.typeCheckerForAutocomplete : frontend.typeChecker); TypeChecker& typeChecker =
ModulePtr module = (frontend.options.typecheckTwice ? frontend.moduleResolverForAutocomplete.getModule(moduleName) (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)); : frontend.moduleResolver.getModule(moduleName));
if (!module) if (!module)
@ -1692,7 +1768,8 @@ OwningAutocompleteResult autocompleteSource(Frontend& frontend, std::string_view
sourceModule->mode = Mode::Strict; sourceModule->mode = Mode::Strict;
sourceModule->commentLocations = std::move(result.commentLocations); 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); 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 // 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/Error.h"
#include "Luau/Clone.h"
#include "Luau/Module.h" #include "Luau/Module.h"
#include "Luau/StringUtils.h" #include "Luau/StringUtils.h"
#include "Luau/ToString.h" #include "Luau/ToString.h"

View file

@ -2,6 +2,7 @@
#include "Luau/Frontend.h" #include "Luau/Frontend.h"
#include "Luau/Common.h" #include "Luau/Common.h"
#include "Luau/Clone.h"
#include "Luau/Config.h" #include "Luau/Config.h"
#include "Luau/FileResolver.h" #include "Luau/FileResolver.h"
#include "Luau/Parser.h" #include "Luau/Parser.h"
@ -16,8 +17,11 @@
#include <chrono> #include <chrono>
#include <stdexcept> #include <stdexcept>
LUAU_FASTFLAG(LuauCyclicModuleTypeSurface)
LUAU_FASTFLAG(LuauInferInNoCheckMode) LUAU_FASTFLAG(LuauInferInNoCheckMode)
LUAU_FASTFLAGVARIABLE(LuauKnowsTheDataModel3, false) LUAU_FASTFLAGVARIABLE(LuauKnowsTheDataModel3, false)
LUAU_FASTFLAGVARIABLE(LuauSeparateTypechecks, false)
LUAU_FASTINTVARIABLE(LuauAutocompleteCheckTimeoutMs, 0)
namespace Luau namespace Luau
{ {
@ -234,12 +238,6 @@ ErrorVec accumulateErrors(
return result; 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 // 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. // 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) // 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_SCOPE("Frontend::check", "Frontend");
LUAU_TIMETRACE_ARGUMENT("name", name.c_str()); LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
FrontendOptions frontendOptions = optionOverride.value_or(options);
CheckResult checkResult; CheckResult checkResult;
auto it = sourceNodes.find(name); auto it = sourceNodes.find(name);
if (it != sourceNodes.end() && !it->second.dirty) if (it != sourceNodes.end() && !it->second.isDirty(frontendOptions.forAutocomplete))
{ {
// No recheck required. // 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); auto it2 = moduleResolver.modules.find(name);
if (it2 == moduleResolver.modules.end() || it2->second == nullptr) if (it2 == moduleResolver.modules.end() || it2->second == nullptr)
throw std::runtime_error("Frontend::modules does not have data for " + name); throw std::runtime_error("Frontend::modules does not have data for " + name);
return CheckResult{accumulateErrors(sourceNodes, moduleResolver.modules, name)}; return CheckResult{accumulateErrors(sourceNodes, moduleResolver.modules, name)};
} }
}
std::vector<ModuleName> buildQueue; std::vector<ModuleName> buildQueue;
bool cycleDetected = parseGraph(buildQueue, checkResult, name); bool cycleDetected = parseGraph(buildQueue, checkResult, name, frontendOptions.forAutocomplete);
FrontendOptions frontendOptions = optionOverride.value_or(options);
// Keep track of which AST nodes we've reported cycles in // Keep track of which AST nodes we've reported cycles in
std::unordered_set<AstNode*> reportedCycles; std::unordered_set<AstNode*> reportedCycles;
double autocompleteTimeLimit = FInt::LuauAutocompleteCheckTimeoutMs / 1000.0;
for (const ModuleName& moduleName : buildQueue) for (const ModuleName& moduleName : buildQueue)
{ {
LUAU_ASSERT(sourceNodes.count(moduleName)); LUAU_ASSERT(sourceNodes.count(moduleName));
SourceNode& sourceNode = sourceNodes[moduleName]; SourceNode& sourceNode = sourceNodes[moduleName];
if (!sourceNode.dirty) if (!sourceNode.isDirty(frontendOptions.forAutocomplete))
continue; continue;
LUAU_ASSERT(sourceModules.count(moduleName)); 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 // This is used by the type checker to replace the resulting type of cyclic modules with any
sourceModule.cyclic = !requireCycles.empty(); 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); ModulePtr module = typeChecker.check(sourceModule, mode, environmentScope);
// If we're typechecking twice, we do so. // If we're typechecking twice, we do so.
// The second typecheck is always in strict mode with DM awareness // The second typecheck is always in strict mode with DM awareness
// to provide better typen information for IDE features. // 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); ModulePtr moduleForAutocomplete = typeCheckerForAutocomplete.check(sourceModule, Mode::Strict);
moduleResolverForAutocomplete.modules[moduleName] = moduleForAutocomplete; moduleResolverForAutocomplete.modules[moduleName] = moduleForAutocomplete;
} }
@ -467,7 +518,7 @@ CheckResult Frontend::check(const ModuleName& name, std::optional<FrontendOption
return checkResult; 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_SCOPE("Frontend::parseGraph", "Frontend");
LUAU_TIMETRACE_ARGUMENT("root", root.c_str()); LUAU_TIMETRACE_ARGUMENT("root", root.c_str());
@ -486,7 +537,7 @@ bool Frontend::parseGraph(std::vector<ModuleName>& buildQueue, CheckResult& chec
bool cyclic = false; bool cyclic = false;
{ {
auto [sourceNode, _] = getSourceNode(checkResult, root); auto [sourceNode, _] = getSourceNode(checkResult, root, forAutocomplete);
if (sourceNode) if (sourceNode)
stack.push_back(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 // 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 // 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. // 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; continue;
// note: this check is technically redundant *except* that getSourceNode has somewhat broken memoization // 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) if (sourceNode)
{ {
stack.push_back(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()); LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
CheckResult checkResult; CheckResult checkResult;
auto [_sourceNode, sourceModule] = getSourceNode(checkResult, name); auto [_sourceNode, sourceModule] = getSourceNode(checkResult, name, false);
if (!sourceModule) if (!sourceModule)
return LintResult{}; // FIXME: We really should do something a bit more obvious when a file is too broken to lint. 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); 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); 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) 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)) if (!moduleResolver.modules.count(name))
return; return;
}
std::unordered_map<ModuleName, std::vector<ModuleName>> reverseDeps; std::unordered_map<ModuleName, std::vector<ModuleName>> reverseDeps;
for (const auto& module : sourceNodes) for (const auto& module : sourceNodes)
@ -722,10 +781,21 @@ void Frontend::markDirty(const ModuleName& name, std::vector<ModuleName>* marked
if (markedDirty) if (markedDirty)
markedDirty->push_back(next); markedDirty->push_back(next);
if (FFlag::LuauSeparateTypechecks)
{
if (sourceNode.dirty && sourceNode.dirtyAutocomplete)
continue;
sourceNode.dirty = true;
sourceNode.dirtyAutocomplete = true;
}
else
{
if (sourceNode.dirty) if (sourceNode.dirty)
continue; continue;
sourceNode.dirty = true; sourceNode.dirty = true;
}
if (0 == reverseDeps.count(name)) if (0 == reverseDeps.count(name))
continue; 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. // 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_SCOPE("Frontend::getSourceNode", "Frontend");
LUAU_TIMETRACE_ARGUMENT("name", name.c_str()); LUAU_TIMETRACE_ARGUMENT("name", name.c_str());
auto it = sourceNodes.find(name); 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); auto moduleIt = sourceModules.find(name);
if (moduleIt != sourceModules.end()) if (moduleIt != sourceModules.end())
@ -801,7 +871,19 @@ std::pair<SourceNode*, SourceModule*> Frontend::getSourceNode(CheckResult& check
sourceNode.name = name; sourceNode.name = name;
sourceNode.requires.clear(); sourceNode.requires.clear();
sourceNode.requireLocations.clear(); sourceNode.requireLocations.clear();
if (FFlag::LuauSeparateTypechecks)
{
if (it == sourceNodes.end())
{
sourceNode.dirty = true; sourceNode.dirty = true;
sourceNode.dirtyAutocomplete = true;
}
}
else
{
sourceNode.dirty = true;
}
for (const auto& [moduleName, location] : requireTrace.requires) for (const auto& [moduleName, location] : requireTrace.requires)
sourceNode.requires.insert(moduleName); sourceNode.requires.insert(moduleName);

View file

@ -23,80 +23,45 @@ std::ostream& operator<<(std::ostream& stream, const AstName& name)
return stream << "<empty>"; 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) if (!err.typeFun.typeParams.empty() || !err.typeFun.typePackParams.empty())
{
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())
{ {
stream << "<"; stream << "<";
bool first = true; bool first = true;
for (auto param : error.typeFun.typeParams) for (auto param : err.typeFun.typeParams)
{ {
if (first) if (first)
first = false; first = false;
@ -106,7 +71,7 @@ std::ostream& operator<<(std::ostream& stream, const IncorrectGenericParameterCo
stream << toString(param.ty); stream << toString(param.ty);
} }
for (auto param : error.typeFun.typePackParams) for (auto param : err.typeFun.typePackParams)
{ {
if (first) if (first)
first = false; first = false;
@ -119,31 +84,20 @@ std::ostream& operator<<(std::ostream& stream, const IncorrectGenericParameterCo
stream << ">"; stream << ">";
} }
stream << ", typeFun = " << toString(error.typeFun.type) << ", actualCount = " << error.actualParameters << " }"; stream << ", typeFun = " << toString(err.typeFun.type) << ", actualCount = " << err.actualParameters << " }";
return stream; }
} else if constexpr (std::is_same_v<T, SyntaxError>)
stream << "SyntaxError { " << err.message << " }";
std::ostream& operator<<(std::ostream& stream, const SyntaxError& ge) else if constexpr (std::is_same_v<T, CodeTooComplex>)
{ stream << "CodeTooComplex {}";
return stream << "SyntaxError { " << ge.message << " }"; else if constexpr (std::is_same_v<T, UnificationTooComplex>)
} stream << "UnificationTooComplex {}";
else if constexpr (std::is_same_v<T, UnknownPropButFoundLikeProp>)
std::ostream& operator<<(std::ostream& stream, const CodeTooComplex&) {
{ stream << "UnknownPropButFoundLikeProp { key = '" << err.key << "', suggested = { ";
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 = { ";
bool first = true; bool first = true;
for (Name name : e.candidates) for (Name name : err.candidates)
{ {
if (first) if (first)
first = false; first = false;
@ -153,40 +107,22 @@ std::ostream& operator<<(std::ostream& stream, const UnknownPropButFoundLikeProp
stream << "'" << name << "'"; stream << "'" << name << "'";
} }
return stream << " }, table = " << toString(e.table) << " } "; stream << " }, table = " << toString(err.table) << " } ";
} }
else if constexpr (std::is_same_v<T, GenericError>)
std::ostream& operator<<(std::ostream& stream, const GenericError& ge) stream << "GenericError { " << err.message << " }";
{ else if constexpr (std::is_same_v<T, CannotCallNonFunction>)
return stream << "GenericError { " << ge.message << " }"; stream << "CannotCallNonFunction { " << toString(err.ty) << " }";
} else if constexpr (std::is_same_v<T, ExtraInformation>)
stream << "ExtraInformation { " << err.message << " }";
std::ostream& operator<<(std::ostream& stream, const CannotCallNonFunction& e) else if constexpr (std::is_same_v<T, DeprecatedApiUsed>)
{ stream << "DeprecatedApiUsed { " << err.symbol << ", useInstead = " << err.useInstead << " }";
return stream << "CannotCallNonFunction { " << toString(e.ty) << " }"; else if constexpr (std::is_same_v<T, ModuleHasCyclicDependency>)
} {
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 << "ModuleHasCyclicDependency {"; stream << "ModuleHasCyclicDependency {";
bool first = true; bool first = true;
for (const ModuleName& name : e.cycle) for (const ModuleName& name : err.cycle)
{ {
if (first) if (first)
first = false; first = false;
@ -196,20 +132,24 @@ std::ostream& operator<<(std::ostream& stream, const ModuleHasCyclicDependency&
stream << name; stream << name;
} }
return stream << "}"; stream << "}";
} }
else if constexpr (std::is_same_v<T, IllegalRequire>)
std::ostream& operator<<(std::ostream& stream, const IllegalRequire& e) stream << "IllegalRequire { " << err.moduleName << ", reason = " << err.reason << " }";
{ else if constexpr (std::is_same_v<T, FunctionExitsWithoutReturning>)
return stream << "IllegalRequire { " << e.moduleName << ", reason = " << e.reason << " }"; stream << "FunctionExitsWithoutReturning {" << toString(err.expectedReturnType) << "}";
} else if constexpr (std::is_same_v<T, DuplicateGenericParameter>)
stream << "DuplicateGenericParameter { " + err.parameterName + " }";
std::ostream& operator<<(std::ostream& stream, const MissingProperties& e) else if constexpr (std::is_same_v<T, CannotInferBinaryOperation>)
{ stream << "CannotInferBinaryOperation { op = " + toString(err.op) + ", suggested = '" +
stream << "MissingProperties { superType = '" << toString(e.superType) << "', subType = '" << toString(e.subType) << "', properties = { "; (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; bool first = true;
for (Name name : e.properties) for (Name name : err.properties)
{ {
if (first) if (first)
first = false; first = false;
@ -219,37 +159,18 @@ std::ostream& operator<<(std::ostream& stream, const MissingProperties& e)
stream << "'" << name << "'"; stream << "'" << name << "'";
} }
return stream << " }, context " << e.context << " } "; stream << " }, context " << err.context << " } ";
} }
else if constexpr (std::is_same_v<T, SwappedGenericTypeParameter>)
std::ostream& operator<<(std::ostream& stream, const DuplicateGenericParameter& error) stream << "SwappedGenericTypeParameter { name = '" + err.name + "', kind = " + std::to_string(err.kind) + " }";
{ else if constexpr (std::is_same_v<T, OptionalValueAccess>)
return stream << "DuplicateGenericParameter { " + error.parameterName + " }"; stream << "OptionalValueAccess { optional = '" + toString(err.optional) + "' }";
} else if constexpr (std::is_same_v<T, MissingUnionProperty>)
{
std::ostream& operator<<(std::ostream& stream, const CannotInferBinaryOperation& error) stream << "MissingUnionProperty { type = '" + toString(err.type) + "', missing = { ";
{
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 = { ";
bool first = true; bool first = true;
for (auto ty : error.missing) for (auto ty : err.missing)
{ {
if (first) if (first)
first = false; first = false;
@ -259,15 +180,28 @@ std::ostream& operator<<(std::ostream& stream, const MissingUnionProperty& error
stream << "'" << toString(ty) << "'"; 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; 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) std::ostream& operator<<(std::ostream& stream, const TableState& tv)
{ {
return stream << static_cast<std::underlying_type<TableState>::type>(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); 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 } // namespace Luau

View file

@ -2,6 +2,7 @@
#include "Luau/Module.h" #include "Luau/Module.h"
#include "Luau/Common.h" #include "Luau/Common.h"
#include "Luau/Clone.h"
#include "Luau/RecursionCounter.h" #include "Luau/RecursionCounter.h"
#include "Luau/Scope.h" #include "Luau/Scope.h"
#include "Luau/TypeInfer.h" #include "Luau/TypeInfer.h"
@ -12,7 +13,6 @@
#include <algorithm> #include <algorithm>
LUAU_FASTFLAGVARIABLE(DebugLuauFreezeArena, false) LUAU_FASTFLAGVARIABLE(DebugLuauFreezeArena, false)
LUAU_FASTINTVARIABLE(LuauTypeCloneRecursionLimit, 300)
LUAU_FASTFLAGVARIABLE(LuauCloneDeclaredGlobals, false) LUAU_FASTFLAGVARIABLE(LuauCloneDeclaredGlobals, false)
namespace Luau namespace Luau
@ -113,363 +113,6 @@ TypePackId TypeArena::addTypePack(TypePackVar tp)
return allocated; 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 ScopePtr Module::getModuleScope() const
{ {
LUAU_ASSERT(!scopes.empty()); LUAU_ASSERT(!scopes.empty());

View file

@ -7,6 +7,8 @@
#include <algorithm> #include <algorithm>
#include <stdexcept> #include <stdexcept>
LUAU_FASTFLAGVARIABLE(LuauTxnLogPreserveOwner, false)
namespace Luau namespace Luau
{ {
@ -78,11 +80,32 @@ void TxnLog::concat(TxnLog rhs)
void TxnLog::commit() 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) for (auto& [ty, rep] : typeVarChanges)
*asMutable(ty) = rep.get()->pending; *asMutable(ty) = rep.get()->pending;
for (auto& [tp, rep] : typePackChanges) for (auto& [tp, rep] : typePackChanges)
*asMutable(tp) = rep.get()->pending; *asMutable(tp) = rep.get()->pending;
}
clear(); clear();
} }

View file

@ -22,6 +22,9 @@ LUAU_FASTINTVARIABLE(LuauTypeInferRecursionLimit, 500)
LUAU_FASTINTVARIABLE(LuauTypeInferTypePackLoopLimit, 5000) LUAU_FASTINTVARIABLE(LuauTypeInferTypePackLoopLimit, 5000)
LUAU_FASTINTVARIABLE(LuauCheckRecursionLimit, 500) LUAU_FASTINTVARIABLE(LuauCheckRecursionLimit, 500)
LUAU_FASTFLAG(LuauKnowsTheDataModel3) LUAU_FASTFLAG(LuauKnowsTheDataModel3)
LUAU_FASTFLAG(LuauSeparateTypechecks)
LUAU_FASTFLAG(LuauAutocompleteSingletonTypes)
LUAU_FASTFLAGVARIABLE(LuauCyclicModuleTypeSurface, false)
LUAU_FASTFLAGVARIABLE(LuauEqConstraint, false) LUAU_FASTFLAGVARIABLE(LuauEqConstraint, false)
LUAU_FASTFLAGVARIABLE(LuauWeakEqConstraint, false) // Eventually removed as false. LUAU_FASTFLAGVARIABLE(LuauWeakEqConstraint, false) // Eventually removed as false.
LUAU_FASTFLAGVARIABLE(DebugLuauFreezeDuringUnification, false) LUAU_FASTFLAGVARIABLE(DebugLuauFreezeDuringUnification, false)
@ -35,7 +38,7 @@ LUAU_FASTFLAGVARIABLE(LuauExpectedTypesOfProperties, false)
LUAU_FASTFLAGVARIABLE(LuauErrorRecoveryType, false) LUAU_FASTFLAGVARIABLE(LuauErrorRecoveryType, false)
LUAU_FASTFLAGVARIABLE(LuauOnlyMutateInstantiatedTables, false) LUAU_FASTFLAGVARIABLE(LuauOnlyMutateInstantiatedTables, false)
LUAU_FASTFLAGVARIABLE(LuauPropertiesGetExpectedType, false) LUAU_FASTFLAGVARIABLE(LuauPropertiesGetExpectedType, false)
LUAU_FASTFLAGVARIABLE(LuauStatFunctionSimplify3, false) LUAU_FASTFLAGVARIABLE(LuauStatFunctionSimplify4, false)
LUAU_FASTFLAGVARIABLE(LuauUnsealedTableLiteral, false) LUAU_FASTFLAGVARIABLE(LuauUnsealedTableLiteral, false)
LUAU_FASTFLAG(LuauTypeMismatchModuleName) LUAU_FASTFLAG(LuauTypeMismatchModuleName)
LUAU_FASTFLAGVARIABLE(LuauTwoPassAliasDefinitionFix, false) LUAU_FASTFLAGVARIABLE(LuauTwoPassAliasDefinitionFix, false)
@ -46,6 +49,7 @@ LUAU_FASTFLAGVARIABLE(LuauDoNotTryToReduce, false)
LUAU_FASTFLAGVARIABLE(LuauDoNotAccidentallyDependOnPointerOrdering, false) LUAU_FASTFLAGVARIABLE(LuauDoNotAccidentallyDependOnPointerOrdering, false)
LUAU_FASTFLAGVARIABLE(LuauFixArgumentCountMismatchAmountWithGenericTypes, false) LUAU_FASTFLAGVARIABLE(LuauFixArgumentCountMismatchAmountWithGenericTypes, false)
LUAU_FASTFLAGVARIABLE(LuauFixIncorrectLineNumberDuplicateType, false) LUAU_FASTFLAGVARIABLE(LuauFixIncorrectLineNumberDuplicateType, false)
LUAU_FASTFLAGVARIABLE(LuauCheckImplicitNumbericKeys, false)
LUAU_FASTFLAG(LuauAnyInIsOptionalIsOptional) LUAU_FASTFLAG(LuauAnyInIsOptionalIsOptional)
LUAU_FASTFLAGVARIABLE(LuauDecoupleOperatorInferenceFromUnifiedTypeInference, false) LUAU_FASTFLAGVARIABLE(LuauDecoupleOperatorInferenceFromUnifiedTypeInference, false)
LUAU_FASTFLAGVARIABLE(LuauArgCountMismatchSaysAtLeastWhenVariadic, false) LUAU_FASTFLAGVARIABLE(LuauArgCountMismatchSaysAtLeastWhenVariadic, false)
@ -53,6 +57,11 @@ LUAU_FASTFLAGVARIABLE(LuauArgCountMismatchSaysAtLeastWhenVariadic, false)
namespace Luau namespace Luau
{ {
const char* TimeLimitError::what() const throw()
{
return "Typeinfer failed to complete in allotted time";
}
static bool typeCouldHaveMetatable(TypeId ty) static bool typeCouldHaveMetatable(TypeId ty)
{ {
return get<TableTypeVar>(follow(ty)) || get<ClassTypeVar>(follow(ty)) || get<MetatableTypeVar>(follow(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.reset(new Module());
currentModule->type = module.type; currentModule->type = module.type;
if (FFlag::LuauSeparateTypechecks)
{
currentModule->allocator = module.allocator;
currentModule->names = module.names;
}
iceHandler->moduleName = module.name; iceHandler->moduleName = module.name;
ScopePtr parentScope = environmentScope.value_or(globalScope); ScopePtr parentScope = environmentScope.value_or(globalScope);
@ -271,7 +286,21 @@ ModulePtr TypeChecker::check(const SourceModule& module, Mode mode, std::optiona
if (prepareModuleScope) if (prepareModuleScope)
prepareModuleScope(module.name, currentModule->getModuleScope()); prepareModuleScope(module.name, currentModule->getModuleScope());
if (FFlag::LuauSeparateTypechecks)
{
try
{
checkBlock(moduleScope, *module.root); checkBlock(moduleScope, *module.root);
}
catch (const TimeLimitError&)
{
currentModule->timeout = true;
}
}
else
{
checkBlock(moduleScope, *module.root);
}
if (get<FreeTypePack>(follow(moduleScope->returnType))) if (get<FreeTypePack>(follow(moduleScope->returnType)))
moduleScope->returnType = addTypePack(TypePack{{}, std::nullopt}); moduleScope->returnType = addTypePack(TypePack{{}, std::nullopt});
@ -366,6 +395,9 @@ void TypeChecker::check(const ScopePtr& scope, const AstStat& program)
} }
else else
ice("Unknown AstStat"); 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. // 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}; scope->bindings[name->local] = {anyIfNonstrict(quantify(funScope, ty, name->local->location)), name->local->location};
return; 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; TypeId exprTy = checkExpr(scope, *name->expr).type;
TableTypeVar* ttv = getMutableTableType(exprTy); 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 (!getIndexTypeFromType(scope, exprTy, name->index.value, name->indexLocation, false))
{
if (ttv || isTableIntersection(exprTy))
reportError(TypeError{function.location, CannotExtendTable{exprTy, CannotExtendTable::Property, name->index.value}}); reportError(TypeError{function.location, CannotExtendTable{exprTy, CannotExtendTable::Property, name->index.value}});
else
reportError(TypeError{function.location, OnlyTablesCanHaveMethods{exprTy}});
} }
ty = follow(ty); ty = follow(ty);
@ -1153,7 +1181,7 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
if (ttv && ttv->state != TableState::Sealed) if (ttv && ttv->state != TableState::Sealed)
ttv->props[name->index.value] = {follow(quantify(funScope, ty, name->indexLocation)), /* deprecated */ false, {}, name->indexLocation}; 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>()); 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) else if (function.func->self)
{ {
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify3); LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify4);
AstExprIndexName* indexName = function.name->as<AstExprIndexName>(); AstExprIndexName* indexName = function.name->as<AstExprIndexName>();
if (!indexName) if (!indexName)
@ -1202,7 +1230,7 @@ void TypeChecker::check(const ScopePtr& scope, TypeId ty, const ScopePtr& funSco
} }
else else
{ {
LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify3); LUAU_ASSERT(!FFlag::LuauStatFunctionSimplify4);
TypeId leftType = checkLValueBinding(scope, *function.name); TypeId leftType = checkLValueBinding(scope, *function.name);
@ -2030,7 +2058,11 @@ TypeId TypeChecker::checkExprTable(
indexer = expectedTable->indexer; indexer = expectedTable->indexer;
if (indexer) if (indexer)
{
if (FFlag::LuauCheckImplicitNumbericKeys)
unify(numberType, indexer->indexType, value->location);
unify(valueType, indexer->indexResultType, value->location); unify(valueType, indexer->indexResultType, value->location);
}
else else
indexer = TableIndexer{numberType, anyIfNonstrict(valueType)}; 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>()) else if (auto indexName = funName.as<AstExprIndexName>())
{ {
TypeId lhsType = checkExpr(scope, *indexName->expr).type; 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; return lhsType;
TableTypeVar* ttv = getMutableTableType(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 (!ttv)
{ {
if (!FFlag::LuauErrorRecoveryType && !isTableIntersection(lhsType)) if (!FFlag::LuauErrorRecoveryType && !isTableIntersection(lhsType))
@ -2997,22 +3043,6 @@ TypeId TypeChecker::checkFunctionName(const ScopePtr& scope, AstExpr& funName, T
return errorRecoveryType(scope); 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) if (lhsType->persistent || ttv->state == TableState::Sealed)
return errorRecoveryType(scope); return errorRecoveryType(scope);
} }
@ -3020,7 +3050,12 @@ TypeId TypeChecker::checkFunctionName(const ScopePtr& scope, AstExpr& funName, T
Name name = indexName->index.value; Name name = indexName->index.value;
if (ttv->props.count(name)) if (ttv->props.count(name))
{
if (FFlag::LuauStatFunctionSimplify4)
return ttv->props[name].type;
else
return errorRecoveryType(scope); return errorRecoveryType(scope);
}
Property& property = ttv->props[name]; Property& property = ttv->props[name];
@ -4155,6 +4190,20 @@ TypeId TypeChecker::checkRequire(const ScopePtr& scope, const ModuleInfo& module
return anyType; 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); ModulePtr module = resolver->getModule(moduleInfo.name);
if (!module) 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 // either the file does not exist or there's a cycle. If there's a cycle
// we will already have reported the error. // we will already have reported the error.
if (!resolver->moduleExists(moduleInfo.name) && !moduleInfo.optional) if (!resolver->moduleExists(moduleInfo.name) && !moduleInfo.optional)
{
if (FFlag::LuauCyclicModuleTypeSurface)
{
reportError(TypeError{location, UnknownRequire{humanReadableName}});
}
else
{ {
std::string reportedModulePath = resolver->getHumanReadableModuleName(moduleInfo.name); std::string reportedModulePath = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(TypeError{location, UnknownRequire{reportedModulePath}}); reportError(TypeError{location, UnknownRequire{reportedModulePath}});
} }
}
return errorRecoveryType(scope); return errorRecoveryType(scope);
} }
if (module->type != SourceCode::Module) 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); std::string humanReadableName = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(location, IllegalRequire{humanReadableName, "Module is not a ModuleScript. It cannot be required."}); reportError(location, IllegalRequire{humanReadableName, "Module is not a ModuleScript. It cannot be required."});
}
return errorRecoveryType(scope); return errorRecoveryType(scope);
} }
@ -4184,9 +4247,16 @@ TypeId TypeChecker::checkRequire(const ScopePtr& scope, const ModuleInfo& module
std::optional<TypeId> moduleType = first(modulePack); std::optional<TypeId> moduleType = first(modulePack);
if (!moduleType) 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); std::string humanReadableName = resolver->getHumanReadableModuleName(moduleInfo.name);
reportError(location, IllegalRequire{humanReadableName, "Module does not return exactly 1 value. It cannot be required."}); reportError(location, IllegalRequire{humanReadableName, "Module does not return exactly 1 value. It cannot be required."});
}
return errorRecoveryType(scope); return errorRecoveryType(scope);
} }
@ -4629,7 +4699,9 @@ TypeId TypeChecker::freshType(TypeLevel level)
TypeId TypeChecker::singletonType(bool value) 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}))); 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 stringType_{PrimitiveTypeVar{PrimitiveTypeVar::String}, /*persistent*/ true};
static TypeVar booleanType_{PrimitiveTypeVar{PrimitiveTypeVar::Boolean}, /*persistent*/ true}; static TypeVar booleanType_{PrimitiveTypeVar{PrimitiveTypeVar::Boolean}, /*persistent*/ true};
static TypeVar threadType_{PrimitiveTypeVar{PrimitiveTypeVar::Thread}, /*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 anyType_{AnyTypeVar{}};
static TypeVar errorType_{ErrorTypeVar{}}; static TypeVar errorType_{ErrorTypeVar{}};
static TypeVar optionalNumberType_{UnionTypeVar{{&numberType_, &nilType_}}}; static TypeVar optionalNumberType_{UnionTypeVar{{&numberType_, &nilType_}}};
@ -665,6 +667,8 @@ SingletonTypes::SingletonTypes()
, stringType(&stringType_) , stringType(&stringType_)
, booleanType(&booleanType_) , booleanType(&booleanType_)
, threadType(&threadType_) , threadType(&threadType_)
, trueType(&trueType_)
, falseType(&falseType_)
, anyType(&anyType_) , anyType(&anyType_)
, optionalNumberType(&optionalNumberType_) , optionalNumberType(&optionalNumberType_)
, anyTypePack(&anyTypePack_) , anyTypePack(&anyTypePack_)

View file

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

View file

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

View file

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

View file

@ -290,7 +290,8 @@ struct ConstantVisitor : AstVisitor
Constant la = analyze(expr->left); Constant la = analyze(expr->left);
Constant ra = analyze(expr->right); 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); foldBinary(result, expr->op, la, ra);
} }
else if (AstExprTypeAssertion* expr = node->as<AstExprTypeAssertion>()) 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/Autocomplete.h
Analysis/include/Luau/BuiltinDefinitions.h Analysis/include/Luau/BuiltinDefinitions.h
Analysis/include/Luau/Config.h Analysis/include/Luau/Config.h
Analysis/include/Luau/Clone.h
Analysis/include/Luau/Documentation.h Analysis/include/Luau/Documentation.h
Analysis/include/Luau/Error.h Analysis/include/Luau/Error.h
Analysis/include/Luau/FileResolver.h Analysis/include/Luau/FileResolver.h
@ -85,6 +86,7 @@ target_sources(Luau.Analysis PRIVATE
Analysis/src/Autocomplete.cpp Analysis/src/Autocomplete.cpp
Analysis/src/BuiltinDefinitions.cpp Analysis/src/BuiltinDefinitions.cpp
Analysis/src/Config.cpp Analysis/src/Config.cpp
Analysis/src/Clone.cpp
Analysis/src/Error.cpp Analysis/src/Error.cpp
Analysis/src/Frontend.cpp Analysis/src/Frontend.cpp
Analysis/src/IostreamHelpers.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_close(lua_State* L, StkId level);
LUAI_FUNC void luaF_freeproto(lua_State* L, Proto* f, struct lua_Page* page); 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); 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 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); 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; 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 nf = lua_objlen(L, 1);
int nt = lua_objlen(L, tt); int nt = lua_objlen(L, tt);

View file

@ -14,6 +14,7 @@
LUAU_FASTFLAG(LuauTraceTypesInNonstrictMode2) LUAU_FASTFLAG(LuauTraceTypesInNonstrictMode2)
LUAU_FASTFLAG(LuauSetMetatableDoesNotTimeTravel) LUAU_FASTFLAG(LuauSetMetatableDoesNotTimeTravel)
LUAU_FASTFLAG(LuauSeparateTypechecks)
using namespace Luau; using namespace Luau;
@ -25,6 +26,11 @@ static std::optional<AutocompleteEntryMap> nullCallback(std::string tag, std::op
template<class BaseType> template<class BaseType>
struct ACFixtureImpl : BaseType struct ACFixtureImpl : BaseType
{ {
ACFixtureImpl()
: Fixture(true, true)
{
}
AutocompleteResult autocomplete(unsigned row, unsigned column) AutocompleteResult autocomplete(unsigned row, unsigned column)
{ {
return Luau::autocomplete(this->frontend, "MainModule", Position{row, column}, nullCallback); return Luau::autocomplete(this->frontend, "MainModule", Position{row, column}, nullCallback);
@ -72,7 +78,25 @@ struct ACFixtureImpl : BaseType
} }
LUAU_ASSERT("Digit expected after @ symbol" && prevChar != '@'); 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 const Position& getPosition(char marker) const
@ -2496,7 +2520,7 @@ local t = {
CHECK(ac.entryMap.count("second")); CHECK(ac.entryMap.count("second"));
} }
TEST_CASE_FIXTURE(Fixture, "autocomplete_documentation_symbols") TEST_CASE_FIXTURE(ACFixture, "autocomplete_documentation_symbols")
{ {
loadDefinition(R"( loadDefinition(R"(
declare y: { declare y: {
@ -2504,13 +2528,11 @@ TEST_CASE_FIXTURE(Fixture, "autocomplete_documentation_symbols")
} }
)"); )");
fileResolver.source["Module/A"] = R"( check(R"(
local a = y. local a = y.@1
)"; )");
frontend.check("Module/A"); auto ac = autocomplete('1');
auto ac = autocomplete(frontend, "Module/A", Position{1, 21}, nullCallback);
REQUIRE(ac.entryMap.count("x")); REQUIRE(ac.entryMap.count("x"));
CHECK_EQ(ac.entryMap["x"].documentationSymbol, "@test/global/y.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")); 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") TEST_CASE_FIXTURE(ACFixture, "function_in_assignment_has_parentheses_2")
{ {
check(R"( 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") TEST_CASE("AndOrChainCodegen")
{ {
const char* source = R"( const char* source = R"(

View file

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

View file

@ -91,7 +91,7 @@ struct TestConfigResolver : ConfigResolver
struct Fixture struct Fixture
{ {
explicit Fixture(bool freeze = true); explicit Fixture(bool freeze = true, bool prepareAutocomplete = false);
~Fixture(); ~Fixture();
// Throws Luau::ParseErrors if the parse fails. // 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"); 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") TEST_CASE_FIXTURE(FrontendFixture, "dont_reparse_clean_file_when_linting")
{ {
fileResolver.source["Modules/A"] = R"( 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 // 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/Module.h"
#include "Luau/Scope.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}}); 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") 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()); 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(); TEST_SUITE_END();

View file

@ -1270,7 +1270,7 @@ caused by:
TEST_CASE_FIXTURE(Fixture, "function_decl_quantify_right_type") TEST_CASE_FIXTURE(Fixture, "function_decl_quantify_right_type")
{ {
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true}; ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
fileResolver.source["game/isAMagicMock"] = R"( fileResolver.source["game/isAMagicMock"] = R"(
--!nonstrict --!nonstrict
@ -1294,7 +1294,7 @@ end
TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite") TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite")
{ {
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true}; ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"( CheckResult result = check(R"(
function string.len(): number function string.len(): number
@ -1316,7 +1316,7 @@ print(string.len('hello'))
TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite_2") TEST_CASE_FIXTURE(Fixture, "function_decl_non_self_sealed_overwrite_2")
{ {
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true}; ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
ScopedFastFlag inferStatFunction{"LuauInferStatFunction", true}; ScopedFastFlag inferStatFunction{"LuauInferStatFunction", true};
CheckResult result = check(R"( CheckResult result = check(R"(
@ -1324,12 +1324,12 @@ local t: { f: ((x: number) -> number)? } = {}
function t.f(x) function t.f(x)
print(x + 5) print(x + 5)
return x .. "asd" return x .. "asd" -- 1st error: we know that return type is a number, not a string
end end
t.f = function(x) t.f = function(x)
print(x + 5) print(x + 5)
return x .. "asd" return x .. "asd" -- 2nd error: we know that return type is a number, not a string
end end
)"); )");
@ -1338,6 +1338,33 @@ end
CHECK_EQ(toString(result.errors[1]), R"(Type 'string' could not be converted into 'number')"); 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") TEST_CASE_FIXTURE(Fixture, "strict_mode_ok_with_missing_arguments")
{ {
ScopedFastFlag sff{"LuauAnyInIsOptionalIsOptional", true}; 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") TEST_CASE_FIXTURE(Fixture, "function_statement_sealed_table_assignment_through_indexer")
{ {
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify3", true}; ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"( CheckResult result = check(R"(
local t: {[string]: () -> number} = {} 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") TEST_CASE_FIXTURE(Fixture, "table_intersection_write_sealed_indirect")
{ {
ScopedFastFlag statFunctionSimplify{"LuauStatFunctionSimplify4", true};
CheckResult result = check(R"( CheckResult result = check(R"(
type X = { x: (number) -> number } type X = { x: (number) -> number }
type Y = { y: (string) -> string } 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 function xy:w(a:number) return a * 10 end
)"); )");
LUAU_REQUIRE_ERROR_COUNT(3, result); LUAU_REQUIRE_ERROR_COUNT(4, result);
CHECK_EQ(toString(result.errors[0]), "Cannot add property 'z' to table 'X & Y'"); CHECK_EQ(toString(result.errors[0]), R"(Type '(string, number) -> string' could not be converted into '(string) -> string'
CHECK_EQ(toString(result.errors[1]), "Cannot add property 'y' to table 'X & Y'"); caused by:
CHECK_EQ(toString(result.errors[2]), "Cannot add property 'w' to table 'X & Y'"); 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") TEST_CASE_FIXTURE(Fixture, "table_intersection_setmetatable")

View file

@ -95,6 +95,8 @@ end
)"); )");
LUAU_REQUIRE_ERROR_COUNT(2, result); 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(); 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); 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(); TEST_SUITE_END();

View file

@ -242,4 +242,30 @@ TEST_CASE_FIXTURE(TryUnifyFixture, "cli_50320_follow_in_any_unification")
state.tryUnify(&func, typeChecker.anyType); 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(); 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); 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(); TEST_SUITE_END();

View file

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