luau/tests/TypeInfer.tryUnify.test.cpp

400 lines
12 KiB
C++
Raw Normal View History

// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2023-02-24 10:24:22 -08:00
#include "Luau/Common.h"
#include "Luau/Scope.h"
2023-02-24 10:24:22 -08:00
#include "Luau/Symbol.h"
#include "Luau/TypeInfer.h"
2023-01-03 19:33:19 +02:00
#include "Luau/Type.h"
#include "Fixture.h"
#include "doctest.h"
using namespace Luau;
2024-08-30 12:28:44 -07:00
LUAU_FASTFLAG(LuauSolverV2);
2024-06-28 17:07:35 -07:00
LUAU_FASTFLAG(LuauUnifierRecursionOnRestart);
2023-02-24 10:24:22 -08:00
struct TryUnifyFixture : Fixture
{
2023-11-03 12:47:28 -07:00
// Cannot use `TryUnifyFixture` under DCR.
2024-11-01 09:47:10 -07:00
DOES_NOT_PASS_NEW_SOLVER_GUARD();
2023-11-03 12:47:28 -07:00
TypeArena arena;
ScopePtr globalScope{new Scope{arena.addTypePack({TypeId{}})}};
InternalErrorReporter iceHandler;
UnifierSharedState unifierState{&iceHandler};
2023-01-03 19:33:19 +02:00
Normalizer normalizer{&arena, builtinTypes, NotNull{&unifierState}};
2023-05-25 23:46:51 +03:00
Unifier state{NotNull{&normalizer}, NotNull{globalScope.get()}, Location{}, Variance::Covariant};
};
TEST_SUITE_BEGIN("TryUnifyTests");
TEST_CASE_FIXTURE(TryUnifyFixture, "primitives_unify")
{
2023-01-03 19:33:19 +02:00
Type numberOne{TypeVariant{PrimitiveType{PrimitiveType::Number}}};
2024-10-25 09:46:08 -07:00
Type numberTwo = numberOne.clone();
2022-01-06 14:10:07 -08:00
state.tryUnify(&numberTwo, &numberOne);
2023-03-17 16:59:30 +02:00
CHECK(!state.failure);
CHECK(state.errors.empty());
}
TEST_CASE_FIXTURE(TryUnifyFixture, "compatible_functions_are_unified")
{
2025-02-07 13:04:46 -08:00
Type functionOne{TypeVariant{
FunctionType(arena.addTypePack({arena.freshType(builtinTypes, globalScope->level)}), arena.addTypePack({builtinTypes->numberType}))
2024-08-01 16:25:12 -07:00
}};
2025-02-07 13:04:46 -08:00
Type functionTwo{TypeVariant{FunctionType(
arena.addTypePack({arena.freshType(builtinTypes, globalScope->level)}), arena.addTypePack({arena.freshType(builtinTypes, globalScope->level)})
)}};
2022-01-06 14:10:07 -08:00
state.tryUnify(&functionTwo, &functionOne);
2023-03-17 16:59:30 +02:00
CHECK(!state.failure);
CHECK(state.errors.empty());
2022-03-11 08:31:18 -08:00
state.log.commit();
2022-01-06 14:10:07 -08:00
CHECK_EQ(functionOne, functionTwo);
}
TEST_CASE_FIXTURE(TryUnifyFixture, "incompatible_functions_are_preserved")
{
2025-02-07 13:04:46 -08:00
TypePackVar argPackOne{TypePack{{arena.freshType(builtinTypes, globalScope->level)}, std::nullopt}};
Type functionOne{TypeVariant{
FunctionType(arena.addTypePack({arena.freshType(builtinTypes, globalScope->level)}), arena.addTypePack({builtinTypes->numberType}))
2024-08-01 16:25:12 -07:00
}};
2024-10-25 09:46:08 -07:00
Type functionOneSaved = functionOne.clone();
2025-02-07 13:04:46 -08:00
TypePackVar argPackTwo{TypePack{{arena.freshType(builtinTypes, globalScope->level)}, std::nullopt}};
Type functionTwo{TypeVariant{
FunctionType(arena.addTypePack({arena.freshType(builtinTypes, globalScope->level)}), arena.addTypePack({builtinTypes->stringType}))
2024-08-01 16:25:12 -07:00
}};
2024-10-25 09:46:08 -07:00
Type functionTwoSaved = functionTwo.clone();
2022-01-06 14:10:07 -08:00
state.tryUnify(&functionTwo, &functionOne);
2023-03-17 16:59:30 +02:00
CHECK(state.failure);
CHECK(!state.errors.empty());
CHECK_EQ(functionOne, functionOneSaved);
CHECK_EQ(functionTwo, functionTwoSaved);
}
TEST_CASE_FIXTURE(TryUnifyFixture, "tables_can_be_unified")
{
2023-01-03 19:33:19 +02:00
Type tableOne{TypeVariant{
2025-02-07 13:04:46 -08:00
TableType{{{"foo", {arena.freshType(builtinTypes, globalScope->level)}}}, std::nullopt, globalScope->level, TableState::Unsealed},
}};
2023-01-03 19:33:19 +02:00
Type tableTwo{TypeVariant{
2025-02-07 13:04:46 -08:00
TableType{{{"foo", {arena.freshType(builtinTypes, globalScope->level)}}}, std::nullopt, globalScope->level, TableState::Unsealed},
}};
2023-04-28 14:55:55 +03:00
CHECK_NE(*getMutable<TableType>(&tableOne)->props["foo"].type(), *getMutable<TableType>(&tableTwo)->props["foo"].type());
2022-01-06 14:10:07 -08:00
state.tryUnify(&tableTwo, &tableOne);
2023-03-17 16:59:30 +02:00
CHECK(!state.failure);
CHECK(state.errors.empty());
2022-03-11 08:31:18 -08:00
state.log.commit();
2022-01-06 14:10:07 -08:00
2023-04-28 14:55:55 +03:00
CHECK_EQ(*getMutable<TableType>(&tableOne)->props["foo"].type(), *getMutable<TableType>(&tableTwo)->props["foo"].type());
}
TEST_CASE_FIXTURE(TryUnifyFixture, "incompatible_tables_are_preserved")
{
2023-01-03 19:33:19 +02:00
Type tableOne{TypeVariant{
2024-08-01 16:25:12 -07:00
TableType{
2025-02-07 13:04:46 -08:00
{{"foo", {arena.freshType(builtinTypes, globalScope->level)}}, {"bar", {builtinTypes->numberType}}},
2024-08-01 16:25:12 -07:00
std::nullopt,
globalScope->level,
TableState::Unsealed
},
}};
2023-01-03 19:33:19 +02:00
Type tableTwo{TypeVariant{
2024-08-01 16:25:12 -07:00
TableType{
2025-02-07 13:04:46 -08:00
{{"foo", {arena.freshType(builtinTypes, globalScope->level)}}, {"bar", {builtinTypes->stringType}}},
2024-08-01 16:25:12 -07:00
std::nullopt,
globalScope->level,
TableState::Unsealed
},
}};
2023-04-28 14:55:55 +03:00
CHECK_NE(*getMutable<TableType>(&tableOne)->props["foo"].type(), *getMutable<TableType>(&tableTwo)->props["foo"].type());
2022-01-06 14:10:07 -08:00
state.tryUnify(&tableTwo, &tableOne);
2023-03-17 16:59:30 +02:00
CHECK(state.failure);
CHECK_EQ(1, state.errors.size());
2023-04-28 14:55:55 +03:00
CHECK_NE(*getMutable<TableType>(&tableOne)->props["foo"].type(), *getMutable<TableType>(&tableTwo)->props["foo"].type());
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "uninhabited_intersection_sub_never")
2022-12-02 12:46:05 +02:00
{
CheckResult result = check(R"(
function f(arg : string & number) : never
return arg
end
)");
LUAU_REQUIRE_NO_ERRORS(result);
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "uninhabited_intersection_sub_anything")
2022-12-02 12:46:05 +02:00
{
CheckResult result = check(R"(
function f(arg : string & number) : boolean
return arg
end
)");
LUAU_REQUIRE_NO_ERRORS(result);
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "uninhabited_table_sub_never")
2022-12-02 12:46:05 +02:00
{
2024-11-01 09:47:10 -07:00
DOES_NOT_PASS_NEW_SOLVER_GUARD();
2024-08-16 09:48:02 -07:00
2022-12-02 12:46:05 +02:00
CheckResult result = check(R"(
function f(arg : { prop : string & number }) : never
return arg
end
)");
LUAU_REQUIRE_NO_ERRORS(result);
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "uninhabited_table_sub_anything")
2022-12-02 12:46:05 +02:00
{
2024-11-01 09:47:10 -07:00
DOES_NOT_PASS_NEW_SOLVER_GUARD();
2024-08-16 09:48:02 -07:00
2022-12-02 12:46:05 +02:00
CheckResult result = check(R"(
function f(arg : { prop : string & number }) : boolean
return arg
end
)");
LUAU_REQUIRE_NO_ERRORS(result);
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "members_of_failed_typepack_unification_are_unified_with_errorType")
{
2024-11-01 09:47:10 -07:00
DOES_NOT_PASS_NEW_SOLVER_GUARD();
2024-08-16 09:48:02 -07:00
CheckResult result = check(R"(
function f(arg: number) end
local a
local b
f(a, b)
)");
LUAU_REQUIRE_ERROR_COUNT(1, result);
2023-06-02 11:17:31 -07:00
CHECK_EQ("number", toString(requireType("a")));
2022-11-04 10:02:37 -07:00
CHECK_EQ("*error-type*", toString(requireType("b")));
2021-11-18 14:21:07 -08:00
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "result_of_failed_typepack_unification_is_constrained")
2021-11-18 14:21:07 -08:00
{
2024-11-01 09:47:10 -07:00
DOES_NOT_PASS_NEW_SOLVER_GUARD();
2024-08-16 09:48:02 -07:00
2021-11-18 14:21:07 -08:00
CheckResult result = check(R"(
function f(arg: number) return arg end
local a
local b
local c = f(a, b)
)");
LUAU_REQUIRE_ERROR_COUNT(1, result);
2023-06-02 11:17:31 -07:00
CHECK_EQ("number", toString(requireType("a")));
2022-11-04 10:02:37 -07:00
CHECK_EQ("*error-type*", toString(requireType("b")));
2021-11-18 14:21:07 -08:00
CHECK_EQ("number", toString(requireType("c")));
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "typepack_unification_should_trim_free_tails")
{
CheckResult result = check(R"(
--!strict
local function f(v: number)
if v % 2 == 0 then
return true
end
end
return function()
return (f(1))
end
)");
LUAU_REQUIRE_ERROR_COUNT(1, result);
2022-03-04 08:19:20 -08:00
CHECK_EQ("(number) -> boolean", toString(requireType("f")));
}
TEST_CASE_FIXTURE(TryUnifyFixture, "variadic_type_pack_unification")
{
2023-03-10 11:20:04 -08:00
TypePackVar testPack{TypePack{{builtinTypes->numberType, builtinTypes->stringType}, std::nullopt}};
TypePackVar variadicPack{VariadicTypePack{builtinTypes->numberType}};
2022-01-06 14:10:07 -08:00
state.tryUnify(&testPack, &variadicPack);
2023-03-17 16:59:30 +02:00
CHECK(state.failure);
CHECK(!state.errors.empty());
}
TEST_CASE_FIXTURE(TryUnifyFixture, "variadic_tails_respect_progress")
{
2023-03-10 11:20:04 -08:00
TypePackVar variadicPack{VariadicTypePack{builtinTypes->booleanType}};
TypePackVar a{TypePack{{builtinTypes->numberType, builtinTypes->stringType, builtinTypes->booleanType, builtinTypes->booleanType}}};
TypePackVar b{TypePack{{builtinTypes->numberType, builtinTypes->stringType}, &variadicPack}};
2022-01-06 14:10:07 -08:00
state.tryUnify(&b, &a);
2023-03-17 16:59:30 +02:00
CHECK(!state.failure);
CHECK(state.errors.empty());
}
2023-11-03 12:47:28 -07:00
TEST_CASE_FIXTURE(Fixture, "variadics_should_use_reversed_properly")
{
CheckResult result = check(R"(
--!strict
local function f<T>(...: T): ...T
return ...
end
local x: string = f(1)
)");
LUAU_REQUIRE_ERROR_COUNT(1, result);
TypeMismatch* tm = get<TypeMismatch>(result.errors[0]);
REQUIRE(tm);
CHECK_EQ(toString(tm->givenType), "number");
CHECK_EQ(toString(tm->wantedType), "string");
}
2022-05-13 12:16:50 -07:00
TEST_CASE_FIXTURE(BuiltinsFixture, "cli_41095_concat_log_in_sealed_table_unification")
{
CheckResult result = check(R"(
--!strict
table.insert()
)");
LUAU_REQUIRE_ERROR_COUNT(2, result);
CHECK_EQ(toString(result.errors[0]), "No overload for function accepts 0 arguments.");
2024-08-30 12:28:44 -07:00
if (FFlag::LuauSolverV2)
2023-02-24 10:24:22 -08:00
CHECK_EQ(toString(result.errors[1]), "Available overloads: <V>({V}, V) -> (); and <V>({V}, number, V) -> ()");
else
CHECK_EQ(toString(result.errors[1]), "Available overloads: ({a}, a) -> (); and ({a}, number, a) -> ()");
}
2022-01-06 14:10:07 -08:00
TEST_CASE_FIXTURE(TryUnifyFixture, "free_tail_is_grown_properly")
{
2023-03-10 11:20:04 -08:00
TypePackId threeNumbers =
arena.addTypePack(TypePack{{builtinTypes->numberType, builtinTypes->numberType, builtinTypes->numberType}, std::nullopt});
TypePackId numberAndFreeTail = arena.addTypePack(TypePack{{builtinTypes->numberType}, arena.addTypePack(TypePackVar{FreeTypePack{TypeLevel{}}})});
2022-01-06 14:10:07 -08:00
2023-03-17 16:59:30 +02:00
CHECK(state.canUnify(numberAndFreeTail, threeNumbers).empty());
2022-01-06 14:10:07 -08:00
}
2022-02-03 15:09:37 -08:00
TEST_CASE_FIXTURE(TryUnifyFixture, "recursive_metatable_getmatchtag")
{
2025-02-07 13:04:46 -08:00
Type redirect{FreeType{TypeLevel{}, builtinTypes->neverType, builtinTypes->unknownType}};
2023-01-03 19:33:19 +02:00
Type table{TableType{}};
Type metatable{MetatableType{&redirect, &table}};
redirect = BoundType{&metatable}; // Now we have a metatable that is recursive on the table type
2023-03-10 11:20:04 -08:00
Type variant{UnionType{{&metatable, builtinTypes->numberType}}};
2022-02-03 15:09:37 -08:00
state.tryUnify(&metatable, &variant);
}
2022-02-11 10:43:14 -08:00
TEST_CASE_FIXTURE(TryUnifyFixture, "cli_50320_follow_in_any_unification")
{
TypePackVar free{FreeTypePack{TypeLevel{}}};
TypePackVar target{TypePack{}};
2023-01-03 19:33:19 +02:00
Type func{FunctionType{&free, &free}};
2022-02-11 10:43:14 -08:00
state.tryUnify(&free, &target);
// Shouldn't assert or error.
2023-03-10 11:20:04 -08:00
state.tryUnify(&func, builtinTypes->anyType);
2022-02-11 10:43:14 -08:00
}
2022-04-07 13:53:47 -07:00
TEST_CASE_FIXTURE(TryUnifyFixture, "txnlog_preserves_type_owner")
{
2025-02-07 13:04:46 -08:00
TypeId a = arena.freshType(builtinTypes, TypeLevel{});
2023-03-10 11:20:04 -08:00
TypeId b = builtinTypes->numberType;
2022-04-07 13:53:47 -07:00
state.tryUnify(a, b);
state.log.commit();
CHECK_EQ(a->owningArena, &arena);
}
TEST_CASE_FIXTURE(TryUnifyFixture, "txnlog_preserves_pack_owner")
{
TypePackId a = arena.addTypePack(TypePackVar{FreeTypePack{TypeLevel{}}});
2023-03-10 11:20:04 -08:00
TypePackId b = builtinTypes->anyTypePack;
2022-04-07 13:53:47 -07:00
state.tryUnify(a, b);
state.log.commit();
CHECK_EQ(a->owningArena, &arena);
}
2022-12-02 12:46:05 +02:00
TEST_CASE_FIXTURE(TryUnifyFixture, "fuzz_tail_unification_issue")
{
2023-03-10 11:20:04 -08:00
TypePackVar variadicAny{VariadicTypePack{builtinTypes->anyType}};
TypePackVar packTmp{TypePack{{builtinTypes->anyType}, &variadicAny}};
TypePackVar packSub{TypePack{{builtinTypes->anyType, builtinTypes->anyType}, &packTmp}};
2022-12-02 12:46:05 +02:00
2025-02-07 13:04:46 -08:00
Type freeTy{FreeType{TypeLevel{}, builtinTypes->neverType, builtinTypes->unknownType}};
2022-12-02 12:46:05 +02:00
TypePackVar freeTp{FreeTypePack{TypeLevel{}}};
TypePackVar packSuper{TypePack{{&freeTy}, &freeTp}};
state.tryUnify(&packSub, &packSuper);
}
2023-01-20 14:02:39 +02:00
TEST_CASE_FIXTURE(BuiltinsFixture, "fuzz_unify_any_should_check_log")
{
CheckResult result = check(R"(
repeat
_._,_ = nil
until _
local l0:(any)&(typeof(_)),l0:(any)|(any) = _,_
)");
LUAU_REQUIRE_ERRORS(result);
}
2024-06-28 17:07:35 -07:00
TEST_CASE_FIXTURE(BuiltinsFixture, "table_unification_full_restart_recursion")
{
ScopedFastFlag luauUnifierRecursionOnRestart{FFlag::LuauUnifierRecursionOnRestart, true};
CheckResult result = check(R"(
local A, B, C, D
E = function(a, b)
local mt = getmetatable(b)
if mt.tm:bar(A) == nil and mt.tm:bar(B) == nil then end
if mt.foo == true then D(b, 3) end
mt.foo:call(false, b)
end
A = function(a, b)
local mt = getmetatable(b)
if mt.foo == true then D(b, 3) end
C(mt, 3)
end
B = function(a, b)
local mt = getmetatable(b)
if mt.foo == true then D(b, 3) end
C(mt, 3)
end
)");
LUAU_REQUIRE_ERRORS(result);
}
TEST_SUITE_END();