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
3 changes: 3 additions & 0 deletions Analysis/include/Luau/ConstraintSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ struct ConstraintSolver
DenseHashSet<TypeId> generalizedTypes_{nullptr};
const NotNull<DenseHashSet<TypeId>> generalizedTypes{&generalizedTypes_};

// The current Constraint that is being processed, can be nullptr.
const Constraint* currentConstraintRef;

// Recorded errors that take place within the solver.
ErrorVec errors;

Expand Down
277 changes: 276 additions & 1 deletion Analysis/src/ConstraintSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,258 @@ size_t HashInstantiationSignature::operator()(const InstantiationSignature& sign
return hash;
}


struct TypeFinder : TypeOnceVisitor
{
TypeFinder() : TypeOnceVisitor("TypeFinder", false)
{
}

TypeId tyToFind;
bool hasFoundType = false;

// SOLID Principle for telling that we found the type.
void makeFound(TypeId ty)
{
hasFoundType = true;
}

// Reset the boolean
// so we don't tell that we found a type we didn't actually find.
void reset()
{
hasFoundType = false;
}

bool visit(TypeId ty) override
{
ty = follow(ty);

if (ty == tyToFind)
makeFound(ty); // If we found the type

return true;
}

// Override for IntersectionTypes
bool visit(TypeId ty, const IntersectionType& iTy) override
{
for (TypeId part : iTy.parts)
{
part = follow(part);

if (part == tyToFind)
makeFound(part); // If we found the type
}

return visit(ty);
}

bool isMatch(TypeId ty)
{
reset();

if (ty == tyToFind)
makeFound(ty);
else
traverse(ty);

if (hasFoundType == true)
return true;
else
return false;
}
};

// A way to find a type, such as a dependency through Constraints
struct ConstraintTypeFinder
{
// TODO: Standardize Constraint Visitors?

TypeId tyToFind;
TypeFinder typeFinder{};

explicit ConstraintTypeFinder(TypeId tyToFind)
: tyToFind(tyToFind)
{
typeFinder.tyToFind = tyToFind;
}

bool visit(AssignIndexConstraint c)
{
if (typeFinder.isMatch(c.indexType))
return true;
if (typeFinder.isMatch(c.lhsType))
return true;
if (typeFinder.isMatch(c.propType))
return true;
if (typeFinder.isMatch(c.rhsType))
return true;

return false;
}
bool visit(AssignPropConstraint c)
{
if (typeFinder.isMatch(c.lhsType))
return true;
if (typeFinder.isMatch(c.propType))
return true;
if (typeFinder.isMatch(c.rhsType))
return true;

return false;
}
bool visit(EqualityConstraint c)
{
if (typeFinder.isMatch(c.assignmentType))
return true;
if (typeFinder.isMatch(c.resultType))
return true;

return false;
}
bool visit(FunctionCallConstraint c)
{
if (typeFinder.isMatch(c.fn))
return true;
// argPacks not included

return false;
}
bool visit(FunctionCheckConstraint c)
{
if (typeFinder.isMatch(c.fn))
return true;
// argsPacks not included

return false;
}
bool visit(GeneralizationConstraint c)
{
if (typeFinder.isMatch(c.generalizedType))
return true;
if (typeFinder.isMatch(c.sourceType))
return true;
for (auto ty : c.interiorTypes)
{
if (typeFinder.isMatch(ty))
return true;
}

return false;
}
bool visit(IterableConstraint c)
{
for (auto ty : c.variables)
{
if (typeFinder.isMatch(ty))
return true;
}

return false;
}
bool visit(NameConstraint c)
{
if (typeFinder.isMatch(c.namedType))
return true;

for (auto ty : c.typeParameters)
{
if (typeFinder.isMatch(ty))
return true;
}

return false;
}
bool visit(PackSubtypeConstraint c)
{
// ?
return false;
}
bool visit(PrimitiveTypeConstraint c)
{
if (typeFinder.isMatch(c.freeType))
return true;
if (typeFinder.isMatch(c.primitiveType))
return true;
// expectedType needed?

return false;
}
bool visit(ReduceConstraint c)
{
if (typeFinder.isMatch(c.ty))
return true;

return false;
}
bool visit(ReducePackConstraint c)
{
// ?
return false;
}
bool visit(SubtypeConstraint c)
{
if (typeFinder.isMatch(c.subType))
return true;
if (typeFinder.isMatch(c.superType))
return true;

return false;
}
bool visit(TypeAliasExpansionConstraint c)
{
if (typeFinder.isMatch(c.target))
return true;

return false;
}
bool visit(UnpackConstraint c)
{
// ?
return false;
}


// Returns true if it found the type.
bool traverseAndFind(ConstraintV cV)
{
if (auto c = get_if<AssignIndexConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<AssignPropConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<EqualityConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<FunctionCallConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<FunctionCheckConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<GeneralizationConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<IterableConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<NameConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<PackSubtypeConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<PrimitiveTypeConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<ReduceConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<ReducePackConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<SubtypeConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<TypeAliasExpansionConstraint>(&cV))
return visit(*c);
else if (auto c = get_if<UnpackConstraint>(&cV))
return visit(*c);

return false;
}
};


struct InstantiationQueuer : TypeOnceVisitor
{
ConstraintSolver* solver;
Expand All @@ -299,7 +551,27 @@ struct InstantiationQueuer : TypeOnceVisitor

bool visit(TypeId ty, const TypeFunctionInstanceType&) override
{
solver->pushConstraint(scope, location, ReduceConstraint{ty});
auto newConstraint = solver->pushConstraint(scope, location, ReduceConstraint{ty});

if (solver->currentConstraintRef)
{
// Block the things that depend on the Pending Expansion that is being expanded
// On the resolution of the new Reduction Constraint
if (auto currentC = get_if<TypeAliasExpansionConstraint>(&solver->currentConstraintRef->c))
{
auto cFinder = ConstraintTypeFinder(currentC->target);

for (auto constraint : solver->constraints)
{
if (solver->currentConstraintRef == constraint || newConstraint == constraint)
continue;

if (cFinder.traverseAndFind(constraint.get()->c))
solver->block(static_cast<NotNull<const Constraint>>(newConstraint), constraint);
}
}
}

return true;
}

Expand Down Expand Up @@ -467,6 +739,9 @@ void ConstraintSolver::run()
snapshot = logger->prepareStepSnapshot(rootScope, c, force, unsolvedConstraints);
}

// Set current Constraint
currentConstraintRef = c.get();

if (FFlag::DebugLuauAssertOnForcedConstraint)
LUAU_ASSERT(!force);

Expand Down
29 changes: 29 additions & 0 deletions tests/TypeFunction.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,35 @@ TEST_CASE_FIXTURE(BuiltinsFixture, "index_wait_for_pending_no_crash")
// Should not crash!
}

TEST_CASE_FIXTURE(BuiltinsFixture, "typefunc_dependency_issue1")
{
if (!FFlag::LuauSolverV2) { return; }

CheckResult check1 = check(R"(
local tbl_A = {} :: typeof({entry1 = 1})
tbl_A.entry1b = "hi"

type tbl_B = {entry2: nil}
local tbl_C = nil :: getmetatable< typeof( setmetatable({}, {entry3b="abc2"}) ) >
tbl_C.entry3 = "abc"
tbl_C.entry4 = "hi2"

local test = nil :: typeof(tbl_C)
local tbl_ABC = nil :: typeof(tbl_A) & tbl_B & typeof(tbl_C)

local indexesABC = nil :: keyof<typeof(tbl_ABC)>
)");

auto test1 = requireType("indexesABC");

if (auto ty = follow(test1)->ty.get_if<UnionType>())
{
CHECK_EQ(ty->options.size(), 6);
}
else
LUAU_ASSERT(false); // It should be a UnionType
}

TEST_CASE_FIXTURE(BuiltinsFixture, "index_type_function_works_w_array")
{
if (!FFlag::LuauSolverV2)
Expand Down
Loading