Skip to content

Commit

Permalink
Fix negation type 'inner' method in user-defined type functions (#1582)
Browse files Browse the repository at this point in the history
Fixes #1580
  • Loading branch information
vegorov-rbx authored Dec 20, 2024
1 parent 8f94786 commit 9a102e2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Analysis/src/TypeFunctionRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <vector>

LUAU_DYNAMIC_FASTINT(LuauTypeFunctionSerdeIterationLimit)
LUAU_FASTFLAGVARIABLE(LuauUserTypeFunFixInner)
LUAU_FASTFLAGVARIABLE(LuauUserTypeFunPrintToError)
LUAU_FASTFLAGVARIABLE(LuauUserTypeFunFixNoReadWrite)
LUAU_FASTFLAGVARIABLE(LuauUserTypeFunThreadBuffer)
Expand Down Expand Up @@ -413,10 +414,21 @@ static int getNegatedValue(lua_State* L)
luaL_error(L, "type.inner: expected 1 argument, but got %d", argumentCount);

TypeFunctionTypeId self = getTypeUserData(L, 1);
if (auto tfnt = get<TypeFunctionNegationType>(self); !tfnt)
allocTypeUserData(L, tfnt->type->type);

if (FFlag::LuauUserTypeFunFixInner)
{
if (auto tfnt = get<TypeFunctionNegationType>(self); tfnt)
allocTypeUserData(L, tfnt->type->type);
else
luaL_error(L, "type.inner: cannot call inner method on non-negation type: `%s` type", getTag(L, self).c_str());
}
else
luaL_error(L, "type.inner: cannot call inner method on non-negation type: `%s` type", getTag(L, self).c_str());
{
if (auto tfnt = get<TypeFunctionNegationType>(self); !tfnt)
allocTypeUserData(L, tfnt->type->type);
else
luaL_error(L, "type.inner: cannot call inner method on non-negation type: `%s` type", getTag(L, self).c_str());
}

return 1;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/TypeFunction.user.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using namespace Luau;

LUAU_FASTFLAG(LuauSolverV2)
LUAU_FASTFLAG(LuauUserTypeFunFixNoReadWrite)
LUAU_FASTFLAG(LuauUserTypeFunFixInner)
LUAU_FASTFLAG(LuauUserTypeFunPrintToError)
LUAU_FASTFLAG(LuauUserTypeFunExportedAndLocal)
LUAU_FASTFLAG(LuauUserDefinedTypeFunParseExport)
Expand Down Expand Up @@ -475,6 +476,28 @@ TEST_CASE_FIXTURE(BuiltinsFixture, "udtf_negation_methods_work")
CHECK(toString(tpm->givenTp) == "~string");
}

TEST_CASE_FIXTURE(ClassFixture, "udtf_negation_inner")
{
ScopedFastFlag newSolver{FFlag::LuauSolverV2, true};
ScopedFastFlag luauUserTypeFunFixInner{FFlag::LuauUserTypeFunFixInner, true};

CheckResult result = check(R"(
type function pass(t)
return types.negationof(t):inner()
end
type function fail(t)
return t:inner()
end
local function ok(idx: pass<number>): number return idx end
local function notok(idx: fail<number>): never return idx end
)");

LUAU_REQUIRE_ERROR_COUNT(4, result);
CHECK(toString(result.errors[0]) == R"('fail' type function errored at runtime: [string "fail"]:7: type.inner: cannot call inner method on non-negation type: `number` type)");
}

TEST_CASE_FIXTURE(BuiltinsFixture, "udtf_table_serialization_works")
{
ScopedFastFlag newSolver{FFlag::LuauSolverV2, true};
Expand Down

0 comments on commit 9a102e2

Please sign in to comment.