2022-03-18 08:06:25 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
|
|
|
|
#include "Luau/AstQuery.h"
|
|
|
|
#include "Luau/BuiltinDefinitions.h"
|
|
|
|
#include "Luau/Scope.h"
|
|
|
|
#include "Luau/TypeInfer.h"
|
2023-01-04 01:33:19 +08:00
|
|
|
#include "Luau/Type.h"
|
|
|
|
#include "Luau/VisitType.h"
|
2022-03-18 08:06:25 +08:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
2023-10-21 04:36:26 +08:00
|
|
|
#include "DiffAsserts.h"
|
2022-03-18 08:06:25 +08:00
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("TypeInferPrimitives");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "cannot_call_primitives")
|
|
|
|
{
|
|
|
|
CheckResult result = check("local foo = 5 foo()");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
REQUIRE(get<CannotCallNonFunction>(result.errors[0]) != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_length")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local s = "Hello, World!"
|
|
|
|
local t = #s
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-10-21 04:36:26 +08:00
|
|
|
CHECK_EQ_DIFF(builtinTypes->numberType, requireType("t"));
|
2022-03-18 08:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_index")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local s = "Hello, World!"
|
|
|
|
local t = s[4]
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
|
|
|
NotATable* nat = get<NotATable>(result.errors[0]);
|
|
|
|
REQUIRE(nat);
|
|
|
|
CHECK_EQ("string", toString(nat->ty));
|
|
|
|
|
2022-11-05 01:02:37 +08:00
|
|
|
CHECK_EQ("*error-type*", toString(requireType("t")));
|
2022-03-18 08:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_method")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local p = ("tacos"):len()
|
|
|
|
)");
|
|
|
|
|
2022-09-02 07:00:14 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-03-11 03:20:04 +08:00
|
|
|
CHECK_EQ(*requireType("p"), *builtinTypes->numberType);
|
2022-03-18 08:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_function_indirect")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local s:string
|
|
|
|
local l = s.lower
|
|
|
|
local p = l(s)
|
|
|
|
)");
|
|
|
|
|
2022-09-02 07:00:14 +08:00
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
2023-03-11 03:20:04 +08:00
|
|
|
CHECK_EQ(*requireType("p"), *builtinTypes->stringType);
|
2022-03-18 08:06:25 +08:00
|
|
|
}
|
|
|
|
|
2024-08-17 00:48:02 +08:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "check_methods_of_number")
|
2022-03-18 08:06:25 +08:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
2024-08-17 00:48:02 +08:00
|
|
|
local x: number = 9999
|
|
|
|
function x:y(z: number)
|
|
|
|
local s: string = z
|
|
|
|
end
|
|
|
|
)");
|
2022-03-18 08:06:25 +08:00
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(2, result);
|
2024-08-17 00:48:02 +08:00
|
|
|
|
2024-08-31 03:28:44 +08:00
|
|
|
if (FFlag::LuauSolverV2)
|
2024-08-17 00:48:02 +08:00
|
|
|
{
|
|
|
|
CHECK("Expected type table, got 'number' instead" == toString(result.errors[0]));
|
|
|
|
CHECK("Type 'number' could not be converted into 'string'" == toString(result.errors[1]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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'");
|
|
|
|
}
|
2022-03-18 08:06:25 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 05:44:50 +08:00
|
|
|
TEST_CASE("singleton_types")
|
|
|
|
{
|
|
|
|
BuiltinsFixture a;
|
|
|
|
|
|
|
|
{
|
|
|
|
BuiltinsFixture b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that Frontend 'a' environment wasn't modified by 'b'
|
|
|
|
CheckResult result = a.check("local s: string = 'hello' local t = s:lower()");
|
|
|
|
|
|
|
|
CHECK(result.errors.empty());
|
|
|
|
}
|
|
|
|
|
2024-06-01 01:46:33 +08:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "property_of_buffers")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
local b = buffer.create(100)
|
|
|
|
print(b.foo)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
}
|
|
|
|
|
2022-03-18 08:06:25 +08:00
|
|
|
TEST_SUITE_END();
|