luau/tests/TypeInfer.primitives.test.cpp

124 lines
2.9 KiB
C++
Raw Normal View History

2022-03-18 08:46:04 +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"
#include "Luau/Type.h"
#include "Luau/VisitType.h"
2022-03-18 08:46:04 +08:00
#include "Fixture.h"
Sync to upstream/release/600 (#1076) ### What's Changed - Improve readability of unions and intersections by limiting the number of elements of those types that can be presented on a single line (gated under `FFlag::LuauToStringSimpleCompositeTypesSingleLine`) - Adds a new option to the compiler `--record-stats` to record and output compilation statistics - `if...then...else` expressions are now optimized into `AND/OR` form when possible. ### VM - Add a new `buffer` type to Luau based on the [buffer RFC](https://github.com/Roblox/luau/pull/739) and additional C API functions to work with it; this release does not include the library. - Internal C API to work with string buffers has been updated to align with Lua version more closely ### Native Codegen - Added support for new X64 instruction (rev) and new A64 instruction (bswap) in the assembler - Simplified the way numerical loop condition is translated to IR ### New Type Solver - Operator inference now handled by type families - Created a new system called `Type Paths` to explain why subtyping tests fail in order to improve the quality of error messages. - Systematic changes to implement Data Flow analysis in the new solver (`Breadcrumb` removed and replaced with `RefinementKey`) --- Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> --------- Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com>
2023-10-21 09:10:30 +08:00
#include "DiffAsserts.h"
2022-03-18 08:46:04 +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);
Sync to upstream/release/600 (#1076) ### What's Changed - Improve readability of unions and intersections by limiting the number of elements of those types that can be presented on a single line (gated under `FFlag::LuauToStringSimpleCompositeTypesSingleLine`) - Adds a new option to the compiler `--record-stats` to record and output compilation statistics - `if...then...else` expressions are now optimized into `AND/OR` form when possible. ### VM - Add a new `buffer` type to Luau based on the [buffer RFC](https://github.com/Roblox/luau/pull/739) and additional C API functions to work with it; this release does not include the library. - Internal C API to work with string buffers has been updated to align with Lua version more closely ### Native Codegen - Added support for new X64 instruction (rev) and new A64 instruction (bswap) in the assembler - Simplified the way numerical loop condition is translated to IR ### New Type Solver - Operator inference now handled by type families - Created a new system called `Type Paths` to explain why subtyping tests fail in order to improve the quality of error messages. - Systematic changes to implement Data Flow analysis in the new solver (`Breadcrumb` removed and replaced with `RefinementKey`) --- Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> --------- Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com>
2023-10-21 09:10:30 +08:00
CHECK_EQ_DIFF(builtinTypes->numberType, requireType("t"));
2022-03-18 08:46:04 +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));
CHECK_EQ("*error-type*", toString(requireType("t")));
2022-03-18 08:46:04 +08:00
}
TEST_CASE_FIXTURE(Fixture, "string_method")
{
CheckResult result = check(R"(
local p = ("tacos"):len()
)");
LUAU_REQUIRE_NO_ERRORS(result);
CHECK_EQ(*requireType("p"), *builtinTypes->numberType);
2022-03-18 08:46:04 +08:00
}
TEST_CASE_FIXTURE(Fixture, "string_function_indirect")
{
CheckResult result = check(R"(
local s:string
local l = s.lower
local p = l(s)
)");
LUAU_REQUIRE_NO_ERRORS(result);
CHECK_EQ(*requireType("p"), *builtinTypes->stringType);
2022-03-18 08:46:04 +08:00
}
TEST_CASE_FIXTURE(Fixture, "check_methods_of_number")
2022-03-18 08:46:04 +08:00
{
CheckResult result = check(R"(
local x: number = 9999
function x:y(z: number)
local s: string = z
end
)");
2022-03-18 08:46:04 +08:00
LUAU_REQUIRE_ERROR_COUNT(2, result);
if (FFlag::LuauSolverV2)
{
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:46:04 +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());
}
Sync to upstream/release/628 (#1278) ### What's new? * Remove a case of unsound `table.move` optimization * Add Luau stack slot reservations that were missing in REPL (fixes #1273) ### New Type Solver * Assignments have been completely reworked to fix a case of cyclic constraint dependency * When indexing, if the fresh type's upper bound already contains a compatible indexer, do not add another upper bound * Distribute type arguments over all type families sans `eq`, `keyof`, `rawkeyof`, and other internal type families * Fix a case where `buffers` component weren't read in two places (fixes #1267) * Fix a case where things that constitutes a strong ref were slightly incorrect * Fix a case where constraint dependencies weren't setup wrt `for ... in` statement ### Native Codegen * Fix an optimization that splits TValue store only when its value and its tag are compatible * Implement a system to plug additional type information for custom host userdata types --- ### Internal Contributors Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> --------- Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Vighnesh <vvijay@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: David Cope <dcope@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-06-01 03:18:18 +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:46:04 +08:00
TEST_SUITE_END();