mirror of
https://github.com/luau-lang/luau.git
synced 2024-11-15 14:25:44 +08:00
76f67e0733
Type checker/autocomplete: * `Luau::autocomplete` no longer performs typechecking internally, make sure to run `Frontend::check` before performing autocomplete requests * Autocomplete string suggestions without "" are now only suggested inside the "" * Autocomplete suggestions now include `function (anonymous autofilled)` key with a full suggestion for the function expression (with arguments included) stored in `AutocompleteEntry::insertText` * `AutocompleteEntry::indexedWithSelf` is provided for function call suggestions made with `:` * Cyclic modules now see each other type exports as `any` to prevent memory use-after-free (similar to module return type) Runtime: * Updated inline/loop unroll cost model to better handle assignments (Fixes https://github.com/Roblox/luau/issues/978) * `math.noise` speed was improved by ~30% * `table.concat` speed was improved by ~5-7% * `tonumber` and `tostring` now have fastcall paths that execute ~1.5x and ~2.5x faster respectively (fixes #777) * Fixed crash in `luaL_typename` when index refers to a non-existing value * Fixed potential out of memory scenario when using `string.sub` or `string.char` in a loop * Fixed behavior of some fastcall builtins when called without arguments under -O2 to match original functions * Support for native code execution in VM is now enabled by default (note: native code still has to be generated explicitly) * `Codegen::compile` now accepts `CodeGen_OnlyNativeModules` flag. When set, only modules that have a `--!native` hot-comment at the top will be compiled to native code In our new typechecker: * Generic type packs are no longer considered to be variadic during unification * Timeout and cancellation now works in new solver * Fixed false positive errors around 'table' and 'function' type refinements * Table literals now use covariant unification rules. This is sound since literal has no type specified and has no aliases * Fixed issues with blocked types escaping the constraint solver * Fixed more places where error messages that should've been suppressed were still reported * Fixed errors when iterating over a top table type In our native code generation (jit): * 'DebugLuauAbortingChecks' flag is now supported on A64 * LOP_NEWCLOSURE has been translated to IR
342 lines
8.2 KiB
C++
342 lines
8.2 KiB
C++
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Fixture.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
using namespace Luau;
|
|
|
|
LUAU_FASTFLAG(DebugLuauDeferredConstraintResolution);
|
|
|
|
TEST_SUITE_BEGIN("TypeInferUnknownNever");
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_subtype_and_unknown_supertype")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: string)
|
|
local foo: unknown = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unknown_subtype_and_string_supertype")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: unknown)
|
|
local foo: string = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unknown_is_reflexive")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: unknown)
|
|
local foo: unknown = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "string_subtype_and_never_supertype")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: string)
|
|
local foo: never = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "never_subtype_and_string_supertype")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: never)
|
|
local foo: string = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "never_is_reflexive")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(x: never)
|
|
local foo: never = x
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unknown_is_optional_because_it_too_encompasses_nil")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: {x: unknown} = {}
|
|
)");
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "table_with_prop_of_type_never_is_uninhabitable")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: {x: never} = {}
|
|
)");
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "table_with_prop_of_type_never_is_also_reflexive")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: {x: never} = {x = 5 :: never}
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "array_like_table_of_never_is_inhabitable")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: {never} = {}
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_packs_containing_never_is_itself_uninhabitable")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f() return "foo", 5 :: never end
|
|
|
|
local x, y, z = f()
|
|
)");
|
|
|
|
if (FFlag::DebugLuauDeferredConstraintResolution)
|
|
{
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
CHECK_EQ("Function only returns 2 values, but 3 are required here", toString(result.errors[0]));
|
|
|
|
CHECK_EQ("string", toString(requireType("x")));
|
|
CHECK_EQ("never", toString(requireType("y")));
|
|
CHECK_EQ("*error-type*", toString(requireType("z")));
|
|
}
|
|
else
|
|
{
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("never", toString(requireType("x")));
|
|
CHECK_EQ("never", toString(requireType("y")));
|
|
CHECK_EQ("never", toString(requireType("z")));
|
|
}
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "type_packs_containing_never_is_itself_uninhabitable2")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(): (string, never) return "", 5 :: never end
|
|
local function g(): (never, string) return 5 :: never, "" end
|
|
|
|
local x1, x2 = f()
|
|
local y1, y2 = g()
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
if (FFlag::DebugLuauDeferredConstraintResolution)
|
|
{
|
|
CHECK_EQ("string", toString(requireType("x1")));
|
|
CHECK_EQ("never", toString(requireType("x2")));
|
|
CHECK_EQ("never", toString(requireType("y1")));
|
|
CHECK_EQ("string", toString(requireType("y2")));
|
|
}
|
|
else
|
|
{
|
|
CHECK_EQ("never", toString(requireType("x1")));
|
|
CHECK_EQ("never", toString(requireType("x2")));
|
|
CHECK_EQ("never", toString(requireType("y1")));
|
|
CHECK_EQ("never", toString(requireType("y2")));
|
|
}
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "index_on_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local x: never = 5 :: never
|
|
local z = x.y
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("never", toString(requireType("z")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "call_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local f: never = 5 :: never
|
|
local x, y, z = f()
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("never", toString(requireType("x")));
|
|
CHECK_EQ("never", toString(requireType("y")));
|
|
CHECK_EQ("never", toString(requireType("z")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "assign_to_local_which_is_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: never
|
|
t = 3
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "assign_to_global_which_is_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
--!nonstrict
|
|
t = 5 :: never
|
|
t = ""
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "assign_to_prop_which_is_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: never
|
|
t.x = 5
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "assign_to_subscript_which_is_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local t: never
|
|
t[5] = 7
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "for_loop_over_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
for i, v in (5 :: never) do
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "pick_never_from_variadic_type_pack")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function f(...: never)
|
|
local x, y = (...)
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "index_on_union_of_tables_for_properties_that_is_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
type Disjoint = {foo: never, bar: unknown, tag: "ok"} | {foo: never, baz: unknown, tag: "err"}
|
|
local disjoint: Disjoint = {foo = 5 :: never, bar = true, tag = "ok"}
|
|
local foo = disjoint.foo
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("never", toString(requireType("foo")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "index_on_union_of_tables_for_properties_that_is_sorta_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
type Disjoint = {foo: string, bar: unknown, tag: "ok"} | {foo: never, baz: unknown, tag: "err"}
|
|
local disjoint: Disjoint = {foo = 5 :: never, bar = true, tag = "ok"}
|
|
local foo = disjoint.foo
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("string", toString(requireType("foo")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "unary_minus_of_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local x = -(5 :: never)
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("never", toString(requireType("x")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "length_of_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local x = #({} :: never)
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("number", toString(requireType("x")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "dont_unify_operands_if_one_of_the_operand_is_never_in_any_ordering_operators")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function ord(x: nil, y)
|
|
return x ~= nil and x > y
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
CHECK_EQ("<a>(nil, a) -> boolean", toString(requireType("ord")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "math_operators_and_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function mul(x: nil, y)
|
|
return x ~= nil and x * y -- infers boolean | never, which is normalized into boolean
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
CHECK_EQ("<a>(nil, a) -> boolean", toString(requireType("mul")));
|
|
}
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "compare_never")
|
|
{
|
|
CheckResult result = check(R"(
|
|
local function cmp(x: nil, y: number)
|
|
return x ~= nil and x > y and x < y -- infers boolean | never, which is normalized into boolean
|
|
end
|
|
)");
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
CHECK_EQ("(nil, number) -> boolean", toString(requireType("cmp")));
|
|
}
|
|
|
|
TEST_SUITE_END();
|