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
52 changes: 52 additions & 0 deletions Analysis/src/ConstraintGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ LUAU_FASTFLAG(DebugLuauStringSingletonBasedOnQuotes)
LUAU_FASTFLAGVARIABLE(LuauInstantiateResolvedTypeFunctions)
LUAU_FASTFLAGVARIABLE(LuauPushTypeConstraint)
LUAU_FASTFLAGVARIABLE(LuauNumericUnaryOpsDontProduceNegationRefinements)
LUAU_FASTFLAGVARIABLE(LuauFixPrepopulateGlobalOnSameGlobal)

namespace Luau
{
Expand Down Expand Up @@ -4369,6 +4370,7 @@ struct GlobalPrepopulator : AstVisitor
const NotNull<Scope> globalScope;
const NotNull<TypeArena> arena;
const NotNull<const DataFlowGraph> dfg;
DenseHashSet<const Def*> disallowedDefIds{nullptr};

GlobalPrepopulator(NotNull<Scope> globalScope, NotNull<TypeArena> arena, NotNull<const DataFlowGraph> dfg)
: globalScope(globalScope)
Expand All @@ -4379,6 +4381,21 @@ struct GlobalPrepopulator : AstVisitor

bool visit(AstExprGlobal* global) override
{
if (FFlag::LuauFixPrepopulateGlobalOnSameGlobal)
{
if (auto ty = globalScope->lookup(global->name))
{
DefId def = dfg->getDef(global);
// Skip if disallowed
if (disallowedDefIds.contains(def.get()))
return true;

globalScope->lvalueTypes[def] = *ty;
}

return true;
}

if (auto ty = globalScope->lookup(global->name))
{
DefId def = dfg->getDef(global);
Expand All @@ -4390,6 +4407,41 @@ struct GlobalPrepopulator : AstVisitor

bool visit(AstStatAssign* assign) override
{
if (FFlag::LuauFixPrepopulateGlobalOnSameGlobal)
{
for (size_t i = 0; i < assign->vars.size; i++)
{
auto lhsExpr = assign->vars.data[i];
auto rhsExpr = i < assign->values.size ? assign->values.data[i] : nullptr;

if (lhsExpr)
{
if (rhsExpr)
if (auto lhs = lhsExpr->as<AstExprGlobal>())
if (auto rhs = rhsExpr->as<AstExprGlobal>())
{
// `a = a` case
if (lhs->name == rhs->name)
{
DefId def = dfg->getDef(rhs);
disallowedDefIds.insert(def.get());
}
}

if (const AstExprGlobal* g = lhsExpr->as<AstExprGlobal>())
{
if (!globalScope->lookup(g->name))
globalScope->globalsToWarn.insert(g->name.value);

TypeId bt = arena->addType(BlockedType{});
globalScope->bindings[g->name] = Binding{bt, g->location};
}
}
}

return true;
}

for (const Luau::AstExpr* expr : assign->vars)
{
if (const AstExprGlobal* g = expr->as<AstExprGlobal>())
Expand Down
17 changes: 17 additions & 0 deletions tests/TypeInfer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ LUAU_FASTFLAG(LuauReturnMappedGenericPacksFromSubtyping2)
LUAU_FASTFLAG(LuauMissingFollowMappedGenericPacks)
LUAU_FASTFLAG(LuauOccursCheckInCommit)
LUAU_FASTFLAG(LuauParametrizedAttributeSyntax)
LUAU_FASTFLAG(LuauFixPrepopulateGlobalOnSameGlobal)

using namespace Luau;

Expand Down Expand Up @@ -2516,6 +2517,22 @@ TEST_CASE_FIXTURE(Fixture, "if_then_else_two_errors")
CHECK_EQ("number", toString(err2->givenType));
}

TEST_CASE_FIXTURE(BuiltinsFixture, "prepopulate_globals_global_on_same_global_name_assign")
{
ScopedFastFlag sff[]{
{FFlag::LuauSolverV2, true},
{FFlag::LuauFixPrepopulateGlobalOnSameGlobal, true}
};

CheckResult result = check(R"(
--!strict
a = a
function a:test() end
)");

LUAU_CHECK_NO_ERROR(result, ConstraintSolvingIncompleteError);
}

TEST_CASE_FIXTURE(Fixture, "standalone_constraint_solving_incomplete_is_hidden")
{
ScopedFastFlag sffs[] = {
Expand Down
Loading