Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Analysis/src/Subtyping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ LUAU_FASTFLAGVARIABLE(LuauDropUnionSubtypeReasoning)
LUAU_FASTFLAGVARIABLE(LuauDontBindOptionalGenericToNil)
LUAU_FASTFLAGVARIABLE(LuauImproveUniqueTableWidthSubtyping)
LUAU_FASTFLAG(LuauBidirectionalInferenceSimplifyTables)
LUAU_FASTFLAGVARIABLE(LuauNegationsFixSubtypePath)

namespace Luau
{
Expand Down Expand Up @@ -1836,6 +1837,7 @@ SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const Type
TypeId negatedTy = follow(superNegation->ty);

SubtypingResult result;
bool moved = false;

if (is<NeverType>(negatedTy))
{
Expand Down Expand Up @@ -1867,6 +1869,7 @@ SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const Type
{
NegationType negatedTmp{ty};
result.andAlso(isCovariantWith(env, subTy, &negatedTmp, scope));
moved = true;
}
}
}
Expand All @@ -1884,6 +1887,7 @@ SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const Type
{
NegationType negatedTmp{ty};
result.orElse(isCovariantWith(env, subTy, &negatedTmp, scope));
moved = true;
}
}
}
Expand Down Expand Up @@ -1933,7 +1937,10 @@ SubtypingResult Subtyping::isCovariantWith(SubtypingEnvironment& env, const Type
else
result = {false};

return result.withSuperComponent(TypePath::TypeField::Negated);
if (FFlag::LuauNegationsFixSubtypePath && moved)
return result;
else
return result.withSuperComponent(TypePath::TypeField::Negated);
}

SubtypingResult Subtyping::isCovariantWith(
Expand Down
21 changes: 20 additions & 1 deletion Analysis/src/TypeChecker2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LUAU_FASTFLAG(LuauTweakAccessViolationReporting)
LUAU_FASTFLAG(LuauReadOnlyIndexers)
LUAU_FASTFLAG(LuauImproveUniqueTableWidthSubtyping)
LUAU_FASTFLAG(LuauBidirectionalInferenceSimplifyTables)
LUAU_FASTFLAG(LuauNegationsFixSubtypePath)

LUAU_FASTFLAG(DebugLuauUserDefinedClasses)

Expand Down Expand Up @@ -3028,7 +3029,6 @@ Reasonings TypeChecker2::explainReasonings_(TID subTy, TID superTy, Location loc
continue;

std::optional<TypeOrPack> optSubLeaf = traverse(subTy, reasoning.subPath, builtinTypes, subtyping->arena);

std::optional<TypeOrPack> optSuperLeaf = traverse(superTy, reasoning.superPath, builtinTypes, subtyping->arena);

if (!optSubLeaf || !optSuperLeaf)
Expand Down Expand Up @@ -3064,6 +3064,25 @@ Reasonings TypeChecker2::explainReasonings_(TID subTy, TID superTy, Location loc
subLeafAsString = "()";

std::string superLeafAsString = toString(superLeaf);
if (FFlag::LuauNegationsFixSubtypePath && !reasoning.superPath.components.empty())
{
// If we don't do this, we get "`number` is not a subtype of `number`" etc in our error messages
if (const TypePath::TypeField* tf = get_if<TypePath::TypeField>(&reasoning.superPath.components.back()); tf && *tf == TypePath::TypeField::Negated)
{
Path clippedPath = reasoning.superPath.pop();
std::optional<TypeOrPack> optNegationLeaf = traverse(superTy, clippedPath, builtinTypes, subtyping->arena);

if (optNegationLeaf)
{
const TypeOrPack& negationLeaf = *optNegationLeaf;
auto negationLeafTy = get<TypeId>(negationLeaf);

if (negationLeafTy)
superLeafAsString = toString(negationLeaf);
}
}
}

// if the string is empty, it must be an empty type pack
if (superLeafAsString.empty())
superLeafAsString = "()";
Expand Down
36 changes: 36 additions & 0 deletions tests/TypeInfer.negations.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Luau/Common.h"
#include "ScopedFlags.h"

LUAU_FASTFLAG(LuauNegationsFixSubtypePath)

using namespace Luau;

namespace
Expand Down Expand Up @@ -77,4 +79,38 @@ end
LUAU_REQUIRE_NO_ERRORS(result);
}

TEST_CASE_FIXTURE(NegationFixture, "subtype_path_is_valid_for_unions")
{
ScopedFastFlag fixSubtypePath{FFlag::LuauNegationsFixSubtypePath, true};

CheckResult result = check(R"(
type T = Not<false?>
local x: T = false :: false
)");

LUAU_REQUIRE_ERROR_COUNT(1, result);
CHECK_EQ(
"Expected this to be '~(false?)', but got 'false'; \n"
"the negation `~(false?)`, and `false` is not a subtype of `~(false?)`",
toString(result.errors[0])
);
}

TEST_CASE_FIXTURE(NegationFixture, "subtype_path_is_valid_for_intersections")
{
ScopedFastFlag fixSubtypePath{FFlag::LuauNegationsFixSubtypePath, true};

CheckResult result = check(R"(
type T = Not<unknown & boolean>
local x: T = false
)");

LUAU_REQUIRE_ERROR_COUNT(1, result);
CHECK_EQ(
"Expected this to be '~(boolean & unknown)', but got 'boolean'; \n"
"the negation `~(boolean & unknown)`, and `boolean` is not a subtype of `~(boolean & unknown)`",
toString(result.errors[0])
);
}

TEST_SUITE_END();
Loading