luau/tests/InferPolarity.test.cpp
Vighnesh-V 8863bfc950
Sync to upstream/release/686 (#1948)
## General
This week has been spent mostly on fixing bugs in incremental
autocomplete as well as making the new Type Solver more stable.

- Fixes a bug where registered "require" aliases were case-sensitive
instead of case-insensitive.
### New Type Solver
- Adjust literal sub typing logic to account for unreduced type
functions
- Implement a number of subtyping stack utilization improvements
- Emit a single error if an internal type escapes a module's interface
- Checked function errors in the New Non Strict warn about incorrect
argument use with one-indexed positions, e.g. `argument #1 was used
incorrectly` instead of `argument #0 was used incorrectly`.
- Improvements to type function reduction that let us progress further
while reducing
- Augment the generalization system to not emit duplicate constraints.
- Fix a bug where we didn't seal tables in modules that failed to
complete typechecking.

### Fragment Autocomplete
- Provide richer autocomplete suggestions inside of for loops
- Provide richer autocomplete suggestions inside of interpolated string
expressions
- Improve the quality of error messages when typing out interpolated
strings.

### Compiler
- Fixes REX encoding of extended byte registers for the x86 assembly
code generation.
- Fixes for table shape constant data encoding

---
Co-authored-by: Andy Friesen <afriesen@roblox.com>
Co-authored-by: Ariel Weiss <aaronweiss@roblox.com>
Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com>
Co-authored-by: Sora Kanosue <skanosue@roblox.com>
Co-authored-by: Varun Saini <vsaini@roblox.com>
Co-authored-by: Vighnesh Vijay <vvijay@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2025-08-08 10:18:16 -07:00

93 lines
2.8 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)
LUAU_FASTFLAG(LuauTrackFreeInteriorTypePacks)
TEST_SUITE_BEGIN("InferPolarity");
TEST_CASE_FIXTURE(Fixture, "T where T = { m: <a>(a) -> T }")
{
ScopedFastFlag sff[] = {
{FFlag::LuauEagerGeneralization4, true},
{FFlag::LuauTrackFreeInteriorTypePacks, 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::LuauTrackFreeInteriorTypePacks, 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();