Skip to content

Commit

Permalink
Merge 95bc87c into b8ef743
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Jun 9, 2022
2 parents b8ef743 + 95bc87c commit ab6068d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 21 deletions.
98 changes: 77 additions & 21 deletions Analysis/src/ToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ LUAU_FASTFLAG(LuauLowerBoundsCalculation)
* Fair warning: Setting this will break a lot of Luau unit tests.
*/
LUAU_FASTFLAGVARIABLE(DebugLuauVerboseTypeNames, false)
LUAU_FASTFLAGVARIABLE(LuauToStringTableBracesNewlines, false)

namespace Luau
{
Expand Down Expand Up @@ -283,7 +284,8 @@ struct TypeVarStringifier
}

Luau::visit(
[this, tv](auto&& t) {
[this, tv](auto&& t)
{
return (*this)(tv, t);
},
tv->ty);
Expand Down Expand Up @@ -557,22 +559,54 @@ struct TypeVarStringifier
{
case TableState::Sealed:
state.result.invalid = true;
openbrace = "{| ";
closedbrace = " |}";
if (FFlag::LuauToStringTableBracesNewlines)
{
openbrace = "{|";
closedbrace = "|}";
}
else
{
openbrace = "{| ";
closedbrace = " |}";
}
break;
case TableState::Unsealed:
openbrace = "{ ";
closedbrace = " }";
if (FFlag::LuauToStringTableBracesNewlines)
{
openbrace = "{";
closedbrace = "}";
}
else
{
openbrace = "{ ";
closedbrace = " }";
}
break;
case TableState::Free:
state.result.invalid = true;
openbrace = "{- ";
closedbrace = " -}";
if (FFlag::LuauToStringTableBracesNewlines)
{
openbrace = "{-";
closedbrace = "-}";
}
else
{
openbrace = "{- ";
closedbrace = " -}";
}
break;
case TableState::Generic:
state.result.invalid = true;
openbrace = "{+ ";
closedbrace = " +}";
if (FFlag::LuauToStringTableBracesNewlines)
{
openbrace = "{+";
closedbrace = "+}";
}
else
{
openbrace = "{+ ";
closedbrace = " +}";
}
break;
}

Expand All @@ -591,6 +625,8 @@ struct TypeVarStringifier
bool comma = false;
if (ttv.indexer)
{
if (FFlag::LuauToStringTableBracesNewlines)
state.newline();
state.emit("[");
stringify(ttv.indexer->indexType);
state.emit("]: ");
Expand All @@ -607,6 +643,10 @@ struct TypeVarStringifier
state.emit(",");
state.newline();
}
else if (FFlag::LuauToStringTableBracesNewlines)
{
state.newline();
}

size_t length = state.result.name.length() - oldLength;

Expand All @@ -633,6 +673,13 @@ struct TypeVarStringifier
}

state.dedent();
if (FFlag::LuauToStringTableBracesNewlines)
{
if (comma)
state.newline();
else
state.emit(" ");
}
state.emit(closedbrace);

state.unsee(&ttv);
Expand Down Expand Up @@ -833,7 +880,8 @@ struct TypePackStringifier
}

Luau::visit(
[this, tp](auto&& t) {
[this, tp](auto&& t)
{
return (*this)(tp, t);
},
tp->ty);
Expand Down Expand Up @@ -964,9 +1012,11 @@ static void assignCycleNames(const std::set<TypeId>& cycles, const std::set<Type
if (auto ttv = get<TableTypeVar>(follow(cycleTy)); !exhaustive && ttv && (ttv->syntheticName || ttv->name))
{
// If we have a cycle type in type parameters, assign a cycle name for this named table
if (std::find_if(ttv->instantiatedTypeParams.begin(), ttv->instantiatedTypeParams.end(), [&](auto&& el) {
return cycles.count(follow(el));
}) != ttv->instantiatedTypeParams.end())
if (std::find_if(ttv->instantiatedTypeParams.begin(), ttv->instantiatedTypeParams.end(),
[&](auto&& el)
{
return cycles.count(follow(el));
}) != ttv->instantiatedTypeParams.end())
cycleNames[cycleTy] = ttv->name ? *ttv->name : *ttv->syntheticName;

continue;
Expand Down Expand Up @@ -1062,9 +1112,11 @@ ToStringResult toStringDetailed(TypeId ty, const ToStringOptions& opts)
state.exhaustive = true;

std::vector<std::pair<TypeId, std::string>> sortedCycleNames{state.cycleNames.begin(), state.cycleNames.end()};
std::sort(sortedCycleNames.begin(), sortedCycleNames.end(), [](const auto& a, const auto& b) {
return a.second < b.second;
});
std::sort(sortedCycleNames.begin(), sortedCycleNames.end(),
[](const auto& a, const auto& b)
{
return a.second < b.second;
});

bool semi = false;
for (const auto& [cycleTy, name] : sortedCycleNames)
Expand All @@ -1075,7 +1127,8 @@ ToStringResult toStringDetailed(TypeId ty, const ToStringOptions& opts)
state.emit(name);
state.emit(" = ");
Luau::visit(
[&tvs, cycleTy = cycleTy](auto&& t) {
[&tvs, cycleTy = cycleTy](auto&& t)
{
return tvs(cycleTy, t);
},
cycleTy->ty);
Expand Down Expand Up @@ -1132,9 +1185,11 @@ ToStringResult toStringDetailed(TypePackId tp, const ToStringOptions& opts)
state.exhaustive = true;

std::vector<std::pair<TypeId, std::string>> sortedCycleNames{state.cycleNames.begin(), state.cycleNames.end()};
std::sort(sortedCycleNames.begin(), sortedCycleNames.end(), [](const auto& a, const auto& b) {
return a.second < b.second;
});
std::sort(sortedCycleNames.begin(), sortedCycleNames.end(),
[](const auto& a, const auto& b)
{
return a.second < b.second;
});

bool semi = false;
for (const auto& [cycleTy, name] : sortedCycleNames)
Expand All @@ -1145,7 +1200,8 @@ ToStringResult toStringDetailed(TypePackId tp, const ToStringOptions& opts)
state.emit(name);
state.emit(" = ");
Luau::visit(
[&tvs, cycleTy = cycleTy](auto t) {
[&tvs, cycleTy = cycleTy](auto t)
{
return tvs(cycleTy, t);
},
cycleTy->ty);
Expand Down
36 changes: 36 additions & 0 deletions tests/ToString.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ TEST_CASE_FIXTURE(Fixture, "named_table")
CHECK_EQ("TheTable", toString(&table));
}

TEST_CASE_FIXTURE(Fixture, "empty_table")
{
ScopedFastFlag LuauToStringTableBracesNewlines("LuauToStringTableBracesNewlines", true);
CheckResult result = check(R"(
local a: {}
)");

CHECK_EQ("{| |}", toString(requireType("a")));

// Should stay the same with useLineBreaks enabled
ToStringOptions opts;
opts.useLineBreaks = true;
CHECK_EQ("{| |}", toString(requireType("a"), opts));
}

TEST_CASE_FIXTURE(Fixture, "table_respects_use_line_break")
{
ScopedFastFlag LuauToStringTableBracesNewlines("LuauToStringTableBracesNewlines", true);
CheckResult result = check(R"(
local a: { prop: string, anotherProp: number, thirdProp: boolean }
)");

ToStringOptions opts;
opts.useLineBreaks = true;
opts.indent = true;

//clang-format off
CHECK_EQ("{|\n"
" anotherProp: number,\n"
" prop: string,\n"
" thirdProp: boolean\n"
"|}",
toString(requireType("a"), opts));
//clang-format on
}

TEST_CASE_FIXTURE(BuiltinsFixture, "exhaustive_toString_of_cyclic_table")
{
CheckResult result = check(R"(
Expand Down

0 comments on commit ab6068d

Please sign in to comment.