2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "Luau/AstQuery.h"
|
2022-12-10 02:07:25 +08:00
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
2024-09-14 01:14:29 +08:00
|
|
|
#include "Luau/Common.h"
|
2022-12-10 02:07:25 +08:00
|
|
|
#include "Luau/Constraint.h"
|
2022-09-02 07:00:14 +08:00
|
|
|
#include "Luau/ModuleResolver.h"
|
|
|
|
#include "Luau/NotNull.h"
|
2022-02-18 08:41:20 +08:00
|
|
|
#include "Luau/Parser.h"
|
2023-01-04 01:33:19 +08:00
|
|
|
#include "Luau/Type.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/TypeAttach.h"
|
2023-09-08 07:24:03 +08:00
|
|
|
#include "Luau/TypeInfer.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/Transpiler.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2024-09-20 22:14:29 +08:00
|
|
|
#include <limits>
|
2021-10-30 04:25:12 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string_view>
|
2022-12-10 02:07:25 +08:00
|
|
|
#include <iostream>
|
2024-01-19 23:13:08 +08:00
|
|
|
#include <fstream>
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
static const char* mainModuleName = "MainModule";
|
|
|
|
|
2024-08-31 03:28:44 +08:00
|
|
|
LUAU_FASTFLAG(LuauSolverV2);
|
2024-01-19 23:13:08 +08:00
|
|
|
LUAU_FASTFLAG(DebugLuauLogSolverToJsonFile)
|
2024-09-20 22:14:29 +08:00
|
|
|
LUAU_DYNAMIC_FASTINT(LuauTypeSolverRelease)
|
2022-06-04 04:32:20 +08:00
|
|
|
|
2022-10-07 07:55:58 +08:00
|
|
|
extern std::optional<unsigned> randomSeed; // tests/main.cpp
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-12-10 02:07:25 +08:00
|
|
|
std::optional<ModuleInfo> TestFileResolver::resolveModuleInfo(const ModuleName& currentModuleName, const AstExpr& pathExpr)
|
|
|
|
{
|
|
|
|
if (auto name = pathExprToModuleName(currentModuleName, pathExpr))
|
|
|
|
return {{*name, false}};
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ModulePtr TestFileResolver::getModule(const ModuleName& moduleName) const
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestFileResolver::moduleExists(const ModuleName& moduleName) const
|
|
|
|
{
|
|
|
|
auto it = source.find(moduleName);
|
|
|
|
return (it != source.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<SourceCode> TestFileResolver::readSource(const ModuleName& name)
|
|
|
|
{
|
|
|
|
auto it = source.find(name);
|
|
|
|
if (it == source.end())
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
SourceCode::Type sourceType = SourceCode::Module;
|
|
|
|
|
|
|
|
auto it2 = sourceTypes.find(name);
|
|
|
|
if (it2 != sourceTypes.end())
|
|
|
|
sourceType = it2->second;
|
|
|
|
|
|
|
|
return SourceCode{it->second, sourceType};
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:07:18 +08:00
|
|
|
std::optional<ModuleInfo> TestFileResolver::resolveModule(const ModuleInfo* context, AstExpr* expr)
|
|
|
|
{
|
|
|
|
if (AstExprGlobal* g = expr->as<AstExprGlobal>())
|
|
|
|
{
|
|
|
|
if (g->name == "game")
|
|
|
|
return ModuleInfo{"game"};
|
|
|
|
if (g->name == "workspace")
|
|
|
|
return ModuleInfo{"workspace"};
|
|
|
|
if (g->name == "script")
|
|
|
|
return context ? std::optional<ModuleInfo>(*context) : std::nullopt;
|
|
|
|
}
|
|
|
|
else if (AstExprIndexName* i = expr->as<AstExprIndexName>(); i && context)
|
|
|
|
{
|
|
|
|
if (i->index == "Parent")
|
|
|
|
{
|
|
|
|
std::string_view view = context->name;
|
|
|
|
size_t lastSeparatorIndex = view.find_last_of('/');
|
|
|
|
|
|
|
|
if (lastSeparatorIndex == std::string_view::npos)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
return ModuleInfo{ModuleName(view.substr(0, lastSeparatorIndex)), context->optional};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ModuleInfo{context->name + '/' + i->index.value, context->optional};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (AstExprIndexExpr* i = expr->as<AstExprIndexExpr>(); i && context)
|
|
|
|
{
|
|
|
|
if (AstExprConstantString* index = i->index->as<AstExprConstantString>())
|
|
|
|
{
|
|
|
|
return ModuleInfo{context->name + '/' + std::string(index->value.data, index->value.size), context->optional};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (AstExprCall* call = expr->as<AstExprCall>(); call && call->self && call->args.size >= 1 && context)
|
|
|
|
{
|
|
|
|
if (AstExprConstantString* index = call->args.data[0]->as<AstExprConstantString>())
|
|
|
|
{
|
|
|
|
AstName func = call->func->as<AstExprIndexName>()->index;
|
|
|
|
|
|
|
|
if (func == "GetService" && context->name == "game")
|
|
|
|
return ModuleInfo{"game/" + std::string(index->value.data, index->value.size)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
std::string TestFileResolver::getHumanReadableModuleName(const ModuleName& name) const
|
|
|
|
{
|
2024-03-01 21:58:44 +08:00
|
|
|
// We have a handful of tests that need to distinguish between a canonical
|
|
|
|
// ModuleName and the human-readable version so we apply a simple transform
|
|
|
|
// here: We replace all slashes with dots.
|
|
|
|
std::string result = name;
|
|
|
|
for (size_t i = 0; i < result.size(); ++i)
|
|
|
|
{
|
|
|
|
if (result[i] == '/')
|
|
|
|
result[i] = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> TestFileResolver::getEnvironmentForModule(const ModuleName& name) const
|
|
|
|
{
|
|
|
|
auto it = environments.find(name);
|
|
|
|
if (it != environments.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-12-10 02:07:25 +08:00
|
|
|
const Config& TestConfigResolver::getConfig(const ModuleName& name) const
|
|
|
|
{
|
|
|
|
auto it = configFiles.find(name);
|
|
|
|
if (it != configFiles.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return defaultConfig;
|
|
|
|
}
|
|
|
|
|
2024-10-12 00:38:27 +08:00
|
|
|
Fixture::Fixture(bool prepareAutocomplete)
|
|
|
|
: frontend(
|
2024-08-02 07:25:12 +08:00
|
|
|
&fileResolver,
|
|
|
|
&configResolver,
|
|
|
|
{/* retainFullTypeGraphs= */ true, /* forAutocomplete */ false, /* runLintChecks */ false, /* randomConstraintResolutionSeed */ randomSeed}
|
|
|
|
)
|
2023-01-04 01:33:19 +08:00
|
|
|
, builtinTypes(frontend.builtinTypes)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
configResolver.defaultConfig.mode = Mode::Strict;
|
|
|
|
configResolver.defaultConfig.enabledLint.warningMask = ~0ull;
|
|
|
|
configResolver.defaultConfig.parseOptions.captureComments = true;
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
Luau::freeze(frontend.globals.globalTypes);
|
|
|
|
Luau::freeze(frontend.globalsForAutocomplete.globalTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
Luau::setPrintLine([](auto s) {});
|
2024-01-19 23:13:08 +08:00
|
|
|
|
|
|
|
if (FFlag::DebugLuauLogSolverToJsonFile)
|
|
|
|
{
|
2024-08-02 07:25:12 +08:00
|
|
|
frontend.writeJsonLog = [&](const Luau::ModuleName& moduleName, std::string log)
|
|
|
|
{
|
2024-01-19 23:13:08 +08:00
|
|
|
std::string path = moduleName + ".log.json";
|
|
|
|
size_t pos = moduleName.find_last_of('/');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
path = moduleName.substr(pos + 1);
|
|
|
|
|
|
|
|
std::ofstream os(path);
|
|
|
|
|
|
|
|
os << log << std::endl;
|
|
|
|
MESSAGE("Wrote JSON log to ", path);
|
|
|
|
};
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Fixture::~Fixture()
|
|
|
|
{
|
|
|
|
Luau::resetPrintLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
AstStatBlock* Fixture::parse(const std::string& source, const ParseOptions& parseOptions)
|
|
|
|
{
|
|
|
|
sourceModule.reset(new SourceModule);
|
|
|
|
|
|
|
|
ParseResult result = Parser::parse(source.c_str(), source.length(), *sourceModule->names, *sourceModule->allocator, parseOptions);
|
|
|
|
|
|
|
|
sourceModule->name = fromString(mainModuleName);
|
|
|
|
sourceModule->root = result.root;
|
|
|
|
sourceModule->mode = parseMode(result.hotcomments);
|
2022-02-18 08:41:20 +08:00
|
|
|
sourceModule->hotcomments = std::move(result.hotcomments);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
if (!result.errors.empty())
|
|
|
|
{
|
|
|
|
// if AST is available, check how lint and typecheck handle error nodes
|
|
|
|
if (result.root)
|
|
|
|
{
|
2024-08-31 03:28:44 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2023-02-17 22:53:37 +08:00
|
|
|
{
|
2023-10-14 03:38:31 +08:00
|
|
|
Mode mode = sourceModule->mode ? *sourceModule->mode : Mode::Strict;
|
2024-08-02 07:25:12 +08:00
|
|
|
ModulePtr module = Luau::check(
|
|
|
|
*sourceModule,
|
|
|
|
mode,
|
|
|
|
{},
|
|
|
|
builtinTypes,
|
|
|
|
NotNull{&ice},
|
|
|
|
NotNull{&moduleResolver},
|
|
|
|
NotNull{&fileResolver},
|
|
|
|
frontend.globals.globalScope,
|
|
|
|
/*prepareModuleScope*/ nullptr,
|
|
|
|
frontend.options,
|
|
|
|
{},
|
|
|
|
false,
|
|
|
|
{}
|
|
|
|
);
|
2023-04-14 20:05:27 +08:00
|
|
|
|
|
|
|
Luau::lint(sourceModule->root, *sourceModule->names, frontend.globals.globalScope, module.get(), sourceModule->hotcomments, {});
|
|
|
|
}
|
2023-02-17 22:53:37 +08:00
|
|
|
else
|
2023-03-17 22:59:30 +08:00
|
|
|
{
|
2023-04-14 20:05:27 +08:00
|
|
|
TypeChecker typeChecker(frontend.globals.globalScope, &moduleResolver, builtinTypes, &frontend.iceHandler);
|
|
|
|
ModulePtr module = typeChecker.check(*sourceModule, sourceModule->mode.value_or(Luau::Mode::Nonstrict), std::nullopt);
|
2023-03-17 22:59:30 +08:00
|
|
|
|
|
|
|
Luau::lint(sourceModule->root, *sourceModule->names, frontend.globals.globalScope, module.get(), sourceModule->hotcomments, {});
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
throw ParseErrors(result.errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.root;
|
|
|
|
}
|
|
|
|
|
2023-10-14 03:38:31 +08:00
|
|
|
CheckResult Fixture::check(Mode mode, const std::string& source)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2022-03-25 05:49:08 +08:00
|
|
|
ModuleName mm = fromString(mainModuleName);
|
2021-10-30 04:25:12 +08:00
|
|
|
configResolver.defaultConfig.mode = mode;
|
2022-03-25 05:49:08 +08:00
|
|
|
fileResolver.source[mm] = std::move(source);
|
|
|
|
frontend.markDirty(mm);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-03-25 05:49:08 +08:00
|
|
|
CheckResult result = frontend.check(mm);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckResult Fixture::check(const std::string& source)
|
|
|
|
{
|
2022-03-25 05:49:08 +08:00
|
|
|
return check(Mode::Strict, source);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LintResult Fixture::lint(const std::string& source, const std::optional<LintOptions>& lintOptions)
|
|
|
|
{
|
2023-03-17 22:59:30 +08:00
|
|
|
ModuleName mm = fromString(mainModuleName);
|
|
|
|
configResolver.defaultConfig.mode = Mode::Strict;
|
|
|
|
fileResolver.source[mm] = std::move(source);
|
|
|
|
frontend.markDirty(mm);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-03-17 22:59:30 +08:00
|
|
|
return lintModule(mm);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-03-17 22:59:30 +08:00
|
|
|
LintResult Fixture::lintModule(const ModuleName& moduleName, const std::optional<LintOptions>& lintOptions)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2023-03-17 22:59:30 +08:00
|
|
|
FrontendOptions options = frontend.options;
|
|
|
|
options.runLintChecks = true;
|
|
|
|
options.enabledLintWarnings = lintOptions;
|
|
|
|
|
|
|
|
CheckResult result = frontend.check(moduleName, options);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-03-17 22:59:30 +08:00
|
|
|
return result.lintResult;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ParseResult Fixture::parseEx(const std::string& source, const ParseOptions& options)
|
|
|
|
{
|
|
|
|
ParseResult result = tryParse(source, options);
|
|
|
|
if (!result.errors.empty())
|
|
|
|
throw ParseErrors(result.errors);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseResult Fixture::tryParse(const std::string& source, const ParseOptions& parseOptions)
|
|
|
|
{
|
|
|
|
ParseOptions options = parseOptions;
|
|
|
|
options.allowDeclarationSyntax = true;
|
|
|
|
|
|
|
|
sourceModule.reset(new SourceModule);
|
|
|
|
ParseResult result = Parser::parse(source.c_str(), source.length(), *sourceModule->names, *sourceModule->allocator, options);
|
|
|
|
sourceModule->root = result.root;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:06:31 +08:00
|
|
|
ParseResult Fixture::matchParseError(const std::string& source, const std::string& message, std::optional<Location> location)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
ParseOptions options;
|
|
|
|
options.allowDeclarationSyntax = true;
|
|
|
|
|
|
|
|
sourceModule.reset(new SourceModule);
|
|
|
|
ParseResult result = Parser::parse(source.c_str(), source.length(), *sourceModule->names, *sourceModule->allocator, options);
|
|
|
|
|
2022-07-01 07:29:02 +08:00
|
|
|
CHECK_MESSAGE(!result.errors.empty(), "Expected a parse error in '" << source << "'");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-07-01 07:29:02 +08:00
|
|
|
if (!result.errors.empty())
|
|
|
|
{
|
|
|
|
CHECK_EQ(result.errors.front().getMessage(), message);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-07-01 07:29:02 +08:00
|
|
|
if (location)
|
|
|
|
CHECK_EQ(result.errors.front().getLocation(), *location);
|
|
|
|
}
|
2022-01-15 00:06:31 +08:00
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseResult Fixture::matchParseErrorPrefix(const std::string& source, const std::string& prefix)
|
|
|
|
{
|
|
|
|
ParseOptions options;
|
|
|
|
options.allowDeclarationSyntax = true;
|
|
|
|
|
|
|
|
sourceModule.reset(new SourceModule);
|
|
|
|
ParseResult result = Parser::parse(source.c_str(), source.length(), *sourceModule->names, *sourceModule->allocator, options);
|
|
|
|
|
2022-07-01 07:29:02 +08:00
|
|
|
CHECK_MESSAGE(!result.errors.empty(), "Expected a parse error in '" << source << "'");
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-07-01 07:29:02 +08:00
|
|
|
if (!result.errors.empty())
|
|
|
|
{
|
|
|
|
const std::string& message = result.errors.front().getMessage();
|
|
|
|
CHECK_GE(message.length(), prefix.length());
|
|
|
|
CHECK_EQ(prefix, message.substr(0, prefix.size()));
|
|
|
|
}
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePtr Fixture::getMainModule()
|
|
|
|
{
|
|
|
|
return frontend.moduleResolver.getModule(fromString(mainModuleName));
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceModule* Fixture::getMainSourceModule()
|
|
|
|
{
|
2022-04-15 05:57:15 +08:00
|
|
|
return frontend.getSourceModule(fromString(mainModuleName));
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
std::optional<PrimitiveType::Type> Fixture::getPrimitiveType(TypeId ty)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
REQUIRE(ty != nullptr);
|
|
|
|
|
|
|
|
TypeId aType = follow(ty);
|
|
|
|
REQUIRE(aType != nullptr);
|
|
|
|
|
2023-01-04 01:33:19 +08:00
|
|
|
const PrimitiveType* pt = get<PrimitiveType>(aType);
|
2021-10-30 04:25:12 +08:00
|
|
|
if (pt != nullptr)
|
|
|
|
return pt->type;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Fixture::getType(const std::string& name)
|
|
|
|
{
|
|
|
|
ModulePtr module = getMainModule();
|
|
|
|
REQUIRE(module);
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
if (!module->hasModuleScope())
|
|
|
|
return std::nullopt;
|
|
|
|
|
2024-08-31 03:28:44 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2022-07-29 11:41:13 +08:00
|
|
|
return linearSearchForBinding(module->getModuleScope().get(), name.c_str());
|
2022-06-04 04:32:20 +08:00
|
|
|
else
|
|
|
|
return lookupName(module->getModuleScope(), name);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TypeId Fixture::requireType(const std::string& name)
|
|
|
|
{
|
|
|
|
std::optional<TypeId> ty = getType(name);
|
2022-04-15 05:57:15 +08:00
|
|
|
REQUIRE_MESSAGE(bool(ty), "Unable to requireType \"" << name << "\"");
|
2021-10-30 04:25:12 +08:00
|
|
|
return follow(*ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeId Fixture::requireType(const ModuleName& moduleName, const std::string& name)
|
|
|
|
{
|
|
|
|
ModulePtr module = frontend.moduleResolver.getModule(moduleName);
|
|
|
|
REQUIRE(module);
|
2023-01-07 00:07:19 +08:00
|
|
|
return requireType(module, name);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TypeId Fixture::requireType(const ModulePtr& module, const std::string& name)
|
|
|
|
{
|
2023-01-07 00:07:19 +08:00
|
|
|
if (!module->hasModuleScope())
|
|
|
|
FAIL("requireType: module scope data is not available");
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
return requireType(module->getModuleScope(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeId Fixture::requireType(const ScopePtr& scope, const std::string& name)
|
|
|
|
{
|
|
|
|
std::optional<TypeId> ty = lookupName(scope, name);
|
|
|
|
REQUIRE_MESSAGE(ty, "requireType: No type \"" << name << "\"");
|
|
|
|
return *ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Fixture::findTypeAtPosition(Position position)
|
|
|
|
{
|
|
|
|
ModulePtr module = getMainModule();
|
|
|
|
SourceModule* sourceModule = getMainSourceModule();
|
|
|
|
return Luau::findTypeAtPosition(*module, *sourceModule, position);
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:20:08 +08:00
|
|
|
std::optional<TypeId> Fixture::findExpectedTypeAtPosition(Position position)
|
|
|
|
{
|
|
|
|
ModulePtr module = getMainModule();
|
|
|
|
SourceModule* sourceModule = getMainSourceModule();
|
|
|
|
return Luau::findExpectedTypeAtPosition(*module, *sourceModule, position);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TypeId Fixture::requireTypeAtPosition(Position position)
|
|
|
|
{
|
|
|
|
auto ty = findTypeAtPosition(position);
|
|
|
|
REQUIRE_MESSAGE(ty, "requireTypeAtPosition: No type at position " << position);
|
|
|
|
return *ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Fixture::lookupType(const std::string& name)
|
|
|
|
{
|
2023-01-07 00:07:19 +08:00
|
|
|
ModulePtr module = getMainModule();
|
|
|
|
|
|
|
|
if (!module->hasModuleScope())
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
if (auto typeFun = module->getModuleScope()->lookupType(name))
|
2021-10-30 04:25:12 +08:00
|
|
|
return typeFun->type;
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> Fixture::lookupImportedType(const std::string& moduleAlias, const std::string& name)
|
|
|
|
{
|
2023-01-07 00:07:19 +08:00
|
|
|
ModulePtr module = getMainModule();
|
|
|
|
|
|
|
|
if (!module->hasModuleScope())
|
|
|
|
FAIL("lookupImportedType: module scope data is not available");
|
|
|
|
|
|
|
|
if (auto typeFun = module->getModuleScope()->lookupImportedType(moduleAlias, name))
|
2021-10-30 04:25:12 +08:00
|
|
|
return typeFun->type;
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
TypeId Fixture::requireTypeAlias(const std::string& name)
|
|
|
|
{
|
|
|
|
std::optional<TypeId> ty = lookupType(name);
|
|
|
|
REQUIRE(ty);
|
2023-11-18 02:15:31 +08:00
|
|
|
return follow(*ty);
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
2023-07-28 19:37:00 +08:00
|
|
|
TypeId Fixture::requireExportedType(const ModuleName& moduleName, const std::string& name)
|
|
|
|
{
|
|
|
|
ModulePtr module = frontend.moduleResolver.getModule(moduleName);
|
|
|
|
REQUIRE(module);
|
|
|
|
|
|
|
|
auto it = module->exportedTypeBindings.find(name);
|
|
|
|
REQUIRE(it != module->exportedTypeBindings.end());
|
|
|
|
|
|
|
|
return it->second.type;
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
std::string Fixture::decorateWithTypes(const std::string& code)
|
|
|
|
{
|
|
|
|
fileResolver.source[mainModuleName] = code;
|
|
|
|
|
|
|
|
Luau::CheckResult typeInfo = frontend.check(mainModuleName);
|
|
|
|
|
|
|
|
SourceModule* sourceModule = frontend.getSourceModule(mainModuleName);
|
|
|
|
attachTypeData(*sourceModule, *frontend.moduleResolver.getModule(mainModuleName));
|
|
|
|
|
|
|
|
return transpileWithTypes(*sourceModule->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::dumpErrors(std::ostream& os, const std::vector<TypeError>& errors)
|
|
|
|
{
|
|
|
|
for (const auto& error : errors)
|
|
|
|
{
|
|
|
|
os << std::endl;
|
|
|
|
os << "Error: " << error << std::endl;
|
|
|
|
|
|
|
|
std::string_view source = fileResolver.source[error.moduleName];
|
|
|
|
std::vector<std::string_view> lines = Luau::split(source, '\n');
|
|
|
|
|
|
|
|
if (error.location.begin.line >= lines.size())
|
|
|
|
{
|
|
|
|
os << "\tSource not available?" << std::endl;
|
2022-06-17 08:54:42 +08:00
|
|
|
continue;
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view theLine = lines[error.location.begin.line];
|
|
|
|
os << "Line:\t" << theLine << std::endl;
|
|
|
|
int startCol = error.location.begin.column;
|
|
|
|
int endCol = error.location.end.line == error.location.begin.line ? error.location.end.column : int(theLine.size());
|
|
|
|
|
|
|
|
os << '\t' << std::string(startCol, ' ') << std::string(std::max(1, endCol - startCol), '-') << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::registerTestTypes()
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
addGlobalBinding(frontend.globals, "game", builtinTypes->anyType, "@luau");
|
|
|
|
addGlobalBinding(frontend.globals, "workspace", builtinTypes->anyType, "@luau");
|
|
|
|
addGlobalBinding(frontend.globals, "script", builtinTypes->anyType, "@luau");
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::dumpErrors(const CheckResult& cr)
|
|
|
|
{
|
2022-08-12 04:42:54 +08:00
|
|
|
std::string error = getErrors(cr);
|
|
|
|
if (!error.empty())
|
|
|
|
MESSAGE(error);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::dumpErrors(const ModulePtr& module)
|
|
|
|
{
|
2022-08-12 04:42:54 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
dumpErrors(ss, module->errors);
|
|
|
|
if (!ss.str().empty())
|
|
|
|
MESSAGE(ss.str());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::dumpErrors(const Module& module)
|
|
|
|
{
|
2022-08-12 04:42:54 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
dumpErrors(ss, module.errors);
|
|
|
|
if (!ss.str().empty())
|
|
|
|
MESSAGE(ss.str());
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Fixture::getErrors(const CheckResult& cr)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
dumpErrors(ss, cr.errors);
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fixture::validateErrors(const std::vector<Luau::TypeError>& errors)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
|
|
|
// This helps us validate that error stringification doesn't crash, using both user-facing and internal test-only representation
|
|
|
|
// Also we exercise error comparison to make sure it's at least able to compare the error equal to itself
|
|
|
|
for (const Luau::TypeError& e : errors)
|
|
|
|
{
|
|
|
|
oss.clear();
|
|
|
|
oss << e;
|
|
|
|
toString(e);
|
|
|
|
// CHECK(e == e); TODO: this doesn't work due to union/intersection type vars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadDefinitionFileResult Fixture::loadDefinition(const std::string& source)
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
unfreeze(frontend.globals.globalTypes);
|
2023-04-08 03:56:27 +08:00
|
|
|
LoadDefinitionFileResult result =
|
|
|
|
frontend.loadDefinitionFile(frontend.globals, frontend.globals.globalScope, source, "@test", /* captureComments */ false);
|
2023-03-11 03:20:04 +08:00
|
|
|
freeze(frontend.globals.globalTypes);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2022-10-22 01:33:43 +08:00
|
|
|
if (result.module)
|
|
|
|
dumpErrors(result.module);
|
2021-10-30 04:25:12 +08:00
|
|
|
REQUIRE_MESSAGE(result.success, "loadDefinition: unable to load definition file");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-10-12 00:38:27 +08:00
|
|
|
BuiltinsFixture::BuiltinsFixture(bool prepareAutocomplete)
|
|
|
|
: Fixture(prepareAutocomplete)
|
2022-05-14 03:16:50 +08:00
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
Luau::unfreeze(frontend.globals.globalTypes);
|
|
|
|
Luau::unfreeze(frontend.globalsForAutocomplete.globalTypes);
|
2022-05-14 03:16:50 +08:00
|
|
|
|
2023-04-08 03:56:27 +08:00
|
|
|
registerBuiltinGlobals(frontend, frontend.globals);
|
2022-05-14 03:16:50 +08:00
|
|
|
if (prepareAutocomplete)
|
2023-04-08 03:56:27 +08:00
|
|
|
registerBuiltinGlobals(frontend, frontend.globalsForAutocomplete, /*typeCheckForAutocomplete*/ true);
|
2022-05-14 03:16:50 +08:00
|
|
|
registerTestTypes();
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
Luau::freeze(frontend.globals.globalTypes);
|
|
|
|
Luau::freeze(frontend.globalsForAutocomplete.globalTypes);
|
2022-05-14 03:16:50 +08:00
|
|
|
}
|
|
|
|
|
2024-10-05 00:42:22 +08:00
|
|
|
static std::vector<std::string_view> parsePathExpr(const AstExpr& pathExpr)
|
|
|
|
{
|
|
|
|
const AstExprIndexName* indexName = pathExpr.as<AstExprIndexName>();
|
|
|
|
if (!indexName)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::vector<std::string_view> segments{indexName->index.value};
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (AstExprIndexName* in = indexName->expr->as<AstExprIndexName>())
|
|
|
|
{
|
|
|
|
segments.push_back(in->index.value);
|
|
|
|
indexName = in;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (AstExprGlobal* indexNameAsGlobal = indexName->expr->as<AstExprGlobal>())
|
|
|
|
{
|
|
|
|
segments.push_back(indexNameAsGlobal->name.value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (AstExprLocal* indexNameAsLocal = indexName->expr->as<AstExprLocal>())
|
|
|
|
{
|
|
|
|
segments.push_back(indexNameAsLocal->local->name.value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::reverse(segments.begin(), segments.end());
|
|
|
|
return segments;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> pathExprToModuleName(const ModuleName& currentModuleName, const std::vector<std::string_view>& segments)
|
|
|
|
{
|
|
|
|
if (segments.empty())
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
std::vector<std::string_view> result;
|
|
|
|
|
|
|
|
auto it = segments.begin();
|
|
|
|
|
|
|
|
if (*it == "script" && !currentModuleName.empty())
|
|
|
|
{
|
|
|
|
result = split(currentModuleName, '/');
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != segments.end(); ++it)
|
|
|
|
{
|
|
|
|
if (result.size() > 1 && *it == "Parent")
|
|
|
|
result.pop_back();
|
|
|
|
else
|
|
|
|
result.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(result, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> pathExprToModuleName(const ModuleName& currentModuleName, const AstExpr& pathExpr)
|
|
|
|
{
|
|
|
|
std::vector<std::string_view> segments = parsePathExpr(pathExpr);
|
|
|
|
return pathExprToModuleName(currentModuleName, segments);
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
ModuleName fromString(std::string_view name)
|
|
|
|
{
|
|
|
|
return ModuleName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rep(const std::string& s, size_t n)
|
|
|
|
{
|
|
|
|
std::string r;
|
|
|
|
r.reserve(s.length() * n);
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
r += s;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInArena(TypeId t, const TypeArena& arena)
|
|
|
|
{
|
2023-01-04 01:33:19 +08:00
|
|
|
return arena.types.contains(t);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void dumpErrors(const ModulePtr& module)
|
|
|
|
{
|
|
|
|
for (const auto& error : module->errors)
|
|
|
|
std::cout << "Error: " << error << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dump(const std::string& name, TypeId ty)
|
|
|
|
{
|
|
|
|
std::cout << name << '\t' << toString(ty, {true}) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<TypeId> lookupName(ScopePtr scope, const std::string& name)
|
|
|
|
{
|
|
|
|
auto binding = scope->linearSearchForBinding(name);
|
|
|
|
if (binding)
|
|
|
|
return binding->typeId;
|
|
|
|
else
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-07-29 11:41:13 +08:00
|
|
|
std::optional<TypeId> linearSearchForBinding(Scope* scope, const char* name)
|
2022-06-04 04:32:20 +08:00
|
|
|
{
|
|
|
|
while (scope)
|
|
|
|
{
|
|
|
|
for (const auto& [n, ty] : scope->bindings)
|
|
|
|
{
|
|
|
|
if (n.astName() == name)
|
2022-07-29 11:41:13 +08:00
|
|
|
return ty.typeId;
|
2022-06-04 04:32:20 +08:00
|
|
|
}
|
|
|
|
|
2022-07-29 11:41:13 +08:00
|
|
|
scope = scope->parent.get();
|
2022-06-04 04:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-01-07 00:07:19 +08:00
|
|
|
void registerHiddenTypes(Frontend* frontend)
|
2022-10-28 06:22:49 +08:00
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
GlobalTypes& globals = frontend->globals;
|
|
|
|
|
|
|
|
unfreeze(globals.globalTypes);
|
|
|
|
|
|
|
|
TypeId t = globals.globalTypes.addType(GenericType{"T"});
|
2022-10-28 06:22:49 +08:00
|
|
|
GenericTypeDefinition genericT{t};
|
|
|
|
|
2023-04-08 03:56:27 +08:00
|
|
|
TypeId u = globals.globalTypes.addType(GenericType{"U"});
|
|
|
|
GenericTypeDefinition genericU{u};
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
ScopePtr globalScope = globals.globalScope;
|
|
|
|
globalScope->exportedTypeBindings["Not"] = TypeFun{{genericT}, globals.globalTypes.addType(NegationType{t})};
|
2023-04-08 03:56:27 +08:00
|
|
|
globalScope->exportedTypeBindings["Mt"] = TypeFun{{genericT, genericU}, globals.globalTypes.addType(MetatableType{t, u})};
|
2023-01-07 00:07:19 +08:00
|
|
|
globalScope->exportedTypeBindings["fun"] = TypeFun{{}, frontend->builtinTypes->functionType};
|
|
|
|
globalScope->exportedTypeBindings["cls"] = TypeFun{{}, frontend->builtinTypes->classType};
|
|
|
|
globalScope->exportedTypeBindings["err"] = TypeFun{{}, frontend->builtinTypes->errorType};
|
2023-01-28 05:28:45 +08:00
|
|
|
globalScope->exportedTypeBindings["tbl"] = TypeFun{{}, frontend->builtinTypes->tableType};
|
2023-03-11 03:20:04 +08:00
|
|
|
|
|
|
|
freeze(globals.globalTypes);
|
2023-01-07 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void createSomeClasses(Frontend* frontend)
|
|
|
|
{
|
2023-03-11 03:20:04 +08:00
|
|
|
GlobalTypes& globals = frontend->globals;
|
|
|
|
|
|
|
|
TypeArena& arena = globals.globalTypes;
|
2023-01-07 00:07:19 +08:00
|
|
|
unfreeze(arena);
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
ScopePtr moduleScope = globals.globalScope;
|
2023-01-07 00:07:19 +08:00
|
|
|
|
2024-07-20 01:21:40 +08:00
|
|
|
TypeId parentType = arena.addType(ClassType{"Parent", {}, frontend->builtinTypes->classType, std::nullopt, {}, nullptr, "Test", {}});
|
2023-01-07 00:07:19 +08:00
|
|
|
|
|
|
|
ClassType* parentClass = getMutable<ClassType>(parentType);
|
|
|
|
parentClass->props["method"] = {makeFunction(arena, parentType, {}, {})};
|
|
|
|
|
|
|
|
parentClass->props["virtual_method"] = {makeFunction(arena, parentType, {}, {})};
|
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
addGlobalBinding(globals, "Parent", {parentType});
|
2023-01-07 00:07:19 +08:00
|
|
|
moduleScope->exportedTypeBindings["Parent"] = TypeFun{{}, parentType};
|
|
|
|
|
2024-07-20 01:21:40 +08:00
|
|
|
TypeId childType = arena.addType(ClassType{"Child", {}, parentType, std::nullopt, {}, nullptr, "Test", {}});
|
2023-01-07 00:07:19 +08:00
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
addGlobalBinding(globals, "Child", {childType});
|
2023-01-07 00:07:19 +08:00
|
|
|
moduleScope->exportedTypeBindings["Child"] = TypeFun{{}, childType};
|
|
|
|
|
2024-07-20 01:21:40 +08:00
|
|
|
TypeId anotherChildType = arena.addType(ClassType{"AnotherChild", {}, parentType, std::nullopt, {}, nullptr, "Test", {}});
|
2023-01-20 20:02:39 +08:00
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
addGlobalBinding(globals, "AnotherChild", {anotherChildType});
|
2023-01-20 20:02:39 +08:00
|
|
|
moduleScope->exportedTypeBindings["AnotherChild"] = TypeFun{{}, anotherChildType};
|
|
|
|
|
2024-07-20 01:21:40 +08:00
|
|
|
TypeId unrelatedType = arena.addType(ClassType{"Unrelated", {}, frontend->builtinTypes->classType, std::nullopt, {}, nullptr, "Test", {}});
|
2023-01-07 00:07:19 +08:00
|
|
|
|
2023-03-11 03:20:04 +08:00
|
|
|
addGlobalBinding(globals, "Unrelated", {unrelatedType});
|
2023-01-07 00:07:19 +08:00
|
|
|
moduleScope->exportedTypeBindings["Unrelated"] = TypeFun{{}, unrelatedType};
|
|
|
|
|
|
|
|
for (const auto& [name, ty] : moduleScope->exportedTypeBindings)
|
|
|
|
persist(ty.type);
|
|
|
|
|
|
|
|
freeze(arena);
|
2022-10-28 06:22:49 +08:00
|
|
|
}
|
|
|
|
|
2022-06-04 04:32:20 +08:00
|
|
|
void dump(const std::vector<Constraint>& constraints)
|
|
|
|
{
|
|
|
|
ToStringOptions opts;
|
|
|
|
for (const auto& c : constraints)
|
|
|
|
printf("%s\n", toString(c, opts).c_str());
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
} // namespace Luau
|