2021-10-29 16:25:12 -04:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2021-11-04 22:34:35 -04:00
|
|
|
#include "Luau/Scope.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
#include "Luau/TypeInfer.h"
|
2023-01-04 15:53:17 -05:00
|
|
|
#include "Luau/Type.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
#include "Fixture.h"
|
|
|
|
|
|
|
|
#include "doctest.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace Luau;
|
|
|
|
|
|
|
|
TEST_SUITE_BEGIN("NonstrictModeTests");
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_nullary_function")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function foo(x, y) end
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId fooType = requireType("foo");
|
|
|
|
REQUIRE(fooType);
|
|
|
|
|
2023-01-04 15:53:17 -05:00
|
|
|
const FunctionType* ftv = get<FunctionType>(fooType);
|
2021-10-29 16:25:12 -04:00
|
|
|
REQUIRE_MESSAGE(ftv != nullptr, "Expected a function, got " << toString(fooType));
|
|
|
|
|
|
|
|
auto args = flatten(ftv->argTypes).first;
|
|
|
|
REQUIRE_EQ(2, args.size());
|
|
|
|
REQUIRE_EQ("any", toString(args[0]));
|
|
|
|
REQUIRE_EQ("any", toString(args[1]));
|
|
|
|
|
2022-06-16 21:05:14 -04:00
|
|
|
auto rets = flatten(ftv->retTypes).first;
|
2021-10-29 16:25:12 -04:00
|
|
|
REQUIRE_EQ(0, rets.size());
|
|
|
|
}
|
|
|
|
|
2022-07-21 17:16:54 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "infer_the_maximum_number_of_values_the_function_could_return")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function getMinCardCountForWidth(width)
|
|
|
|
if width < 513 then
|
|
|
|
return 3
|
|
|
|
else
|
|
|
|
return 8, 'jellybeans'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
TypeId t = requireType("getMinCardCountForWidth");
|
|
|
|
REQUIRE(t);
|
|
|
|
|
2022-07-21 17:16:54 -04:00
|
|
|
REQUIRE_EQ("(any) -> (...any)", toString(t));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-07-21 17:16:54 -04:00
|
|
|
#if 0
|
|
|
|
// Maybe we want this?
|
2021-10-29 16:25:12 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "return_annotation_is_still_checked")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
function foo(x): number return 'hello' end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2022-07-21 17:16:54 -04:00
|
|
|
|
|
|
|
REQUIRE_NE(*typeChecker.anyType, *requireType("foo"));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
2022-07-21 17:16:54 -04:00
|
|
|
#endif
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "function_parameters_are_any")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function f(arg)
|
|
|
|
arg = 9
|
|
|
|
arg:concat(4)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "inconsistent_return_types_are_ok")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function f()
|
|
|
|
if 1 then
|
|
|
|
return 4
|
|
|
|
else
|
|
|
|
return 'hello'
|
|
|
|
end
|
|
|
|
return 'one', 'two'
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "locals_are_any_by_default")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local m = 55
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
|
|
|
CHECK_EQ(*typeChecker.anyType, *requireType("m"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "parameters_having_type_any_are_optional")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local function f(a, b)
|
|
|
|
return a
|
|
|
|
end
|
|
|
|
|
|
|
|
f(5)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "local_tables_are_not_any")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local T = {}
|
|
|
|
function T:method() end
|
|
|
|
function T.staticmethod() end
|
|
|
|
|
|
|
|
T.method()
|
|
|
|
T:staticmethod()
|
|
|
|
)");
|
|
|
|
|
2022-03-17 20:46:04 -04:00
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-03-17 20:46:04 -04:00
|
|
|
CHECK_EQ("This function does not take self. Did you mean to use a dot instead of a colon?", toString(result.errors[0]));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "offer_a_hint_if_you_use_a_dot_instead_of_a_colon")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local T = {}
|
2022-03-17 20:46:04 -04:00
|
|
|
function T:method(x: number) end
|
|
|
|
T.method(5)
|
2021-10-29 16:25:12 -04:00
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_ERROR_COUNT(1, result);
|
|
|
|
|
2022-03-17 20:46:04 -04:00
|
|
|
CHECK_EQ("This function must be called with self. Did you mean to use a colon instead of a dot?", toString(result.errors[0]));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "table_props_are_any")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local T = {}
|
|
|
|
T.foo = 55
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-04 15:53:17 -05:00
|
|
|
TableType* ttv = getMutable<TableType>(requireType("T"));
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
REQUIRE(ttv != nullptr);
|
|
|
|
|
2022-08-11 17:01:33 -04:00
|
|
|
REQUIRE(ttv->props.count("foo"));
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeId fooProp = ttv->props["foo"].type;
|
|
|
|
REQUIRE(fooProp != nullptr);
|
|
|
|
|
|
|
|
CHECK_EQ(*fooProp, *typeChecker.anyType);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "inline_table_props_are_also_any")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
local T = {
|
|
|
|
one = 1,
|
|
|
|
two = 'two',
|
|
|
|
three = function() return 3 end
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-04 15:53:17 -05:00
|
|
|
TableType* ttv = getMutable<TableType>(requireType("T"));
|
2021-10-29 16:25:12 -04:00
|
|
|
REQUIRE_MESSAGE(ttv, "Should be a table: " << toString(requireType("T")));
|
|
|
|
|
|
|
|
CHECK_EQ(*typeChecker.anyType, *ttv->props["one"].type);
|
|
|
|
CHECK_EQ(*typeChecker.anyType, *ttv->props["two"].type);
|
2023-02-24 16:49:38 -05:00
|
|
|
CHECK_MESSAGE(get<FunctionType>(follow(ttv->props["three"].type)), "Should be a function: " << *ttv->props["three"].type);
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 15:36:37 -04:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "for_in_iterator_variables_are_any")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function requires_a_table(arg: {}) end
|
|
|
|
function requires_a_number(arg: number) end
|
|
|
|
|
|
|
|
local T = {}
|
|
|
|
for a, b in pairs(T) do
|
|
|
|
requires_a_table(a)
|
|
|
|
requires_a_table(b)
|
|
|
|
requires_a_number(a)
|
|
|
|
requires_a_number(b)
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2022-05-13 15:36:37 -04:00
|
|
|
TEST_CASE_FIXTURE(BuiltinsFixture, "table_dot_insert_and_recursive_calls")
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
function populateListFromIds(list, normalizedData)
|
|
|
|
local newList = {}
|
|
|
|
|
|
|
|
for _, value in ipairs(list) do
|
|
|
|
if type(value) == "table" then
|
|
|
|
table.insert(newList, populateListFromIds(value, normalizedData))
|
|
|
|
else
|
|
|
|
table.insert(newList, normalizedData[value])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return newList
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "delay_function_does_not_require_its_argument_to_return_anything")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
|
|
|
|
function delay(ms: number?, cb: () -> ()): () end
|
|
|
|
|
|
|
|
delay(50, function() end)
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "inconsistent_module_return_types_are_ok")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
|
|
|
|
local FFlag: any
|
|
|
|
|
|
|
|
if FFlag.get('SomeFlag') then
|
|
|
|
return {foo='bar'}
|
|
|
|
else
|
|
|
|
return function(prop)
|
|
|
|
return 'bar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
|
2023-01-06 16:14:35 -05:00
|
|
|
REQUIRE_EQ("any", toString(getMainModule()->returnType));
|
2021-10-29 16:25:12 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 19:57:43 -04:00
|
|
|
TEST_CASE_FIXTURE(Fixture, "returning_insufficient_return_values")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
|
|
|
|
function foo(): (boolean, string?)
|
|
|
|
if true then
|
|
|
|
return true, "hello"
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_FIXTURE(Fixture, "returning_too_many_values")
|
|
|
|
{
|
|
|
|
CheckResult result = check(R"(
|
|
|
|
--!nonstrict
|
|
|
|
|
|
|
|
function foo(): boolean
|
|
|
|
if true then
|
|
|
|
return true, "hello"
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
|
|
|
|
LUAU_REQUIRE_NO_ERRORS(result);
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TEST_SUITE_END();
|