luau/tests/InferPolarity.test.cpp
Andy Friesen 713ee2ff8b
Sync to upstream/release/678 (#1878)
# What's Changed?

We've been hard at work fixing bugs in the new type solver and getting
it ready to go!

## Native Codegen

* Specialized Luau Codegen instruction for fetching an import.
As a reminder, an import is an expression like `global.thing` and covers
stuff like libraries without fastcalls `coroutine.resume`) and atomic
extern libraries.

## New Type Solver

* Fix an issue that prevented eager generalization from working properly
with OO styled code.
* Avoid copying uninitialized memory in Luau attribute parsing
* Improve type inference of unsealed tables.
    * This fixes https://github.com/luau-lang/luau/issues/1838
    * and https://github.com/luau-lang/luau/issues/1859
* Infer potential singleton string keys in autocomplete when the
expected index type is a union type.
* Avoid creating cyclic types when reducing types of the form `t1 where
t1 = refine<T, t1, Y>`
* The type cloner now does the same thing for the new and old solvers.
* Properly infer polarity (aka variance) for divergent table properties.
(ie tables whose read type and write type are not the same)
* Crash fixes.

---------

Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com>
Co-authored-by: Varun Saini <61795485+vrn-sn@users.noreply.github.com>
Co-authored-by: Alexander Youngblood <ayoungblood@roblox.com>
Co-authored-by: Menarul Alam <malam@roblox.com>
Co-authored-by: Aviral Goel <agoel@roblox.com>
Co-authored-by: Vighnesh <vvijay@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
Co-authored-by: Ariel Weiss <aaronweiss@roblox.com>
2025-06-13 09:36:35 -07:00

88 lines
2.7 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 "Luau/InferPolarity.h"
#include "Luau/Polarity.h"
#include "Luau/Type.h"
#include "Luau/TypeArena.h"
using namespace Luau;
LUAU_FASTFLAG(LuauEagerGeneralization4);
LUAU_FASTFLAG(LuauInferPolarityOfReadWriteProperties)
TEST_SUITE_BEGIN("InferPolarity");
TEST_CASE_FIXTURE(Fixture, "T where T = { m: <a>(a) -> T }")
{
ScopedFastFlag sff{FFlag::LuauEagerGeneralization4, true};
TypeArena arena;
ScopePtr globalScope = std::make_shared<Scope>(getBuiltins()->anyTypePack);
TypeId tType = arena.addType(BlockedType{});
TypeId aType = arena.addType(GenericType{globalScope.get(), "a"});
TypeId mType = arena.addType(FunctionType{
TypeLevel{},
/* generics */ {aType},
/* genericPacks */ {},
/* argPack */ arena.addTypePack({aType}),
/* retPack */ arena.addTypePack({tType})
});
emplaceType<TableType>(
asMutable(tType),
TableType{
TableType::Props{{"m", Property::rw(mType)}},
/* indexer */ std::nullopt,
TypeLevel{},
globalScope.get(),
TableState::Sealed
}
);
inferGenericPolarities(NotNull{&arena}, NotNull{globalScope.get()}, tType);
const GenericType* aGeneric = get<GenericType>(aType);
REQUIRE(aGeneric);
CHECK(aGeneric->polarity == Polarity::Negative);
}
TEST_CASE_FIXTURE(Fixture, "<a, b>({ read x: a, write x: b }) -> ()")
{
ScopedFastFlag sffs[] = {
{FFlag::LuauEagerGeneralization4, true},
{FFlag::LuauInferPolarityOfReadWriteProperties, true},
};
TypeArena arena;
ScopePtr globalScope = std::make_shared<Scope>(getBuiltins()->anyTypePack);
TypeId aType = arena.addType(GenericType{globalScope.get(), "a"});
TypeId bType = arena.addType(GenericType{globalScope.get(), "b"});
TableType ttv;
ttv.state = TableState::Sealed;
ttv.props["x"] = Property::create({aType}, {bType});
TypeId mType = arena.addType(FunctionType{
TypeLevel{},
/* generics */ {aType, bType},
/* genericPacks */ {},
/* argPack */ arena.addTypePack({arena.addType(std::move(ttv))}),
/* retPack */ builtinTypes->emptyTypePack,
});
inferGenericPolarities(NotNull{&arena}, NotNull{globalScope.get()}, mType);
const GenericType* aGeneric = get<GenericType>(aType);
REQUIRE(aGeneric);
CHECK(aGeneric->polarity == Polarity::Negative);
const GenericType* bGeneric = get<GenericType>(bType);
REQUIRE(bGeneric);
CHECK(bGeneric->polarity == Polarity::Positive);
}
TEST_SUITE_END();