Skip to content

Commit f9b1974

Browse files
committed
Merge tag '696' into wip_merge
2 parents 387c654 + 6169fa1 commit f9b1974

40 files changed

+1013
-418
lines changed

Analysis/include/Luau/BuiltinDefinitions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ TypeId getGlobalBinding(GlobalTypes& globals, const std::string& name);
8888
bool matchSetMetatable(const AstExprCall& call);
8989
bool matchTableFreeze(const AstExprCall& call);
9090
bool matchAssert(const AstExprCall& call);
91+
bool matchTypeOf(const AstExprCall& call);
9192

9293
// Returns `true` if the function should introduce typestate for its first argument.
9394
bool shouldTypestateForFirstArgument(const AstExprCall& call);

Analysis/include/Luau/ConstraintSolver.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,12 @@ struct ConstraintSolver
462462
void reproduceConstraints(NotNull<Scope> scope, const Location& location, const Substitution& subst);
463463

464464
TypeId simplifyIntersection(NotNull<Scope> scope, Location location, TypeId left, TypeId right);
465-
TypeId simplifyIntersection(NotNull<Scope> scope, Location location, std::set<TypeId> parts);
465+
466+
// Clip with LuauSimplifyIntersectionNoTreeSet
467+
TypeId simplifyIntersection_DEPRECATED(NotNull<Scope> scope, Location location, std::set<TypeId> parts);
468+
469+
TypeId simplifyIntersection(NotNull<Scope> scope, Location location, TypeIds parts);
470+
466471
TypeId simplifyUnion(NotNull<Scope> scope, Location location, TypeId left, TypeId right);
467472

468473
TypePackId anyifyModuleReturnTypePackGenerics(TypePackId tp);

Analysis/include/Luau/Simplify.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Luau/DenseHash.h"
66
#include "Luau/NotNull.h"
77
#include "Luau/TypeFwd.h"
8+
#include "Luau/TypeIds.h"
89

910
#include <optional>
1011
#include <set>
@@ -22,7 +23,11 @@ struct SimplifyResult
2223
};
2324

2425
SimplifyResult simplifyIntersection(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId left, TypeId right);
25-
SimplifyResult simplifyIntersection(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, std::set<TypeId> parts);
26+
27+
// Clip with LuauSimplifyIntersectionNoTreeSet
28+
SimplifyResult simplifyIntersection_DEPRECATED(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, std::set<TypeId> parts);
29+
30+
SimplifyResult simplifyIntersection(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeIds parts);
2631

2732
SimplifyResult simplifyUnion(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId left, TypeId right);
2833

Analysis/include/Luau/Subtyping.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ struct SubtypingEnvironment
168168
// TODO: Clip with LuauSubtypingGenericsDoesntUseVariance
169169
std::optional<TypeId> applyMappedGenerics_DEPRECATED(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId ty);
170170

171-
const TypeId* tryFindSubstitution(TypeId ty) const;
171+
// TODO: Clip with LuauTryFindSubstitutionReturnOptional
172+
const TypeId* tryFindSubstitution_DEPRECATED(TypeId ty) const;
173+
std::optional<TypeId> tryFindSubstitution(TypeId ty) const;
172174
// TODO: Clip with LuauSubtypingGenericsDoesntUseVariance
173175
const SubtypingResult* tryFindSubtypingResult(std::pair<TypeId, TypeId> subAndSuper) const;
174176

Analysis/include/Luau/TypeIds.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class TypeIds
4040
void retain(const TypeIds& tys);
4141
void clear();
4242

43+
void clearWithoutRealloc();
44+
4345
TypeId front() const;
4446
iterator begin();
4547
iterator end();

Analysis/src/BuiltinDefinitions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,18 @@ bool matchAssert(const AstExprCall& call)
18801880
return true;
18811881
}
18821882

1883+
bool matchTypeOf(const AstExprCall& call)
1884+
{
1885+
if (call.args.size != 1)
1886+
return false;
1887+
1888+
const AstExprGlobal* funcAsGlobal = call.func->as<AstExprGlobal>();
1889+
if (!funcAsGlobal || (funcAsGlobal->name != "typeof" && funcAsGlobal->name != "type"))
1890+
return false;
1891+
1892+
return true;
1893+
}
1894+
18831895
bool shouldTypestateForFirstArgument(const AstExprCall& call)
18841896
{
18851897
// TODO: magic function for setmetatable and assert and then add them

Analysis/src/BuiltinTypeFunctions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ LUAU_FASTFLAGVARIABLE(LuauRefineNoRefineAlways)
2626
LUAU_FASTFLAGVARIABLE(LuauRefineDistributesOverUnions)
2727
LUAU_FASTFLAG(LuauEGFixGenericsList)
2828
LUAU_FASTFLAG(LuauExplicitSkipBoundTypes)
29-
LUAU_FASTFLAG(LuauRawGetHandlesNil)
3029
LUAU_FASTFLAG(LuauNoMoreComparisonTypeFunctions)
3130
LUAU_FASTFLAGVARIABLE(LuauBuiltinTypeFunctionsArentGlobal)
3231
LUAU_FASTFLAG(LuauPassBindableGenericsByReference)
@@ -2326,7 +2325,7 @@ TypeFunctionReductionResult<TypeId> indexFunctionImpl(
23262325
for (TypeId ty : *typesToFind)
23272326
if (!tblIndexInto(ty, *tablesIter, properties, ctx, isRaw))
23282327
{
2329-
if (FFlag::LuauRawGetHandlesNil && isRaw)
2328+
if (isRaw)
23302329
properties.insert(ctx->builtins->nilType);
23312330
else
23322331
return {std::nullopt, Reduction::Erroneous, {}, {}};

Analysis/src/ConstraintGenerator.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ LUAU_FASTFLAGVARIABLE(LuauNoMoreComparisonTypeFunctions)
5454
LUAU_FASTFLAG(LuauNoOrderingTypeFunctions)
5555
LUAU_FASTFLAGVARIABLE(LuauDontReferenceScopePtrFromHashTable)
5656
LUAU_FASTFLAG(LuauBuiltinTypeFunctionsArentGlobal)
57+
LUAU_FASTFLAGVARIABLE(LuauMetatableAvoidSingletonUnion)
58+
LUAU_FASTFLAGVARIABLE(LuauAddRefinementToAssertions)
5759

5860
namespace Luau
5961
{
@@ -710,7 +712,7 @@ void ConstraintGenerator::applyRefinements(const ScopePtr& scope, Location locat
710712
discriminants.clear();
711713
return resultType;
712714
}
713-
715+
714716
};
715717

716718
for (auto& [def, partition] : refinements)
@@ -2471,9 +2473,19 @@ InferencePack ConstraintGenerator::checkPack(const ScopePtr& scope, AstExprCall*
24712473
{
24722474
expectedType = expectedTypesForCall[i];
24732475
}
2474-
auto [ty, refinement] = check(scope, arg, expectedType, /*forceSingleton*/ false, /*generalize*/ false);
2475-
args.push_back(ty);
2476-
argumentRefinements.push_back(refinement);
2476+
if (FFlag::LuauAddRefinementToAssertions && i == 0 && matchAssert(*call))
2477+
{
2478+
InConditionalContext flipper{&typeContext};
2479+
auto [ty, refinement] = check(scope, arg, expectedType, /*forceSingleton*/ false, /*generalize*/ false);
2480+
args.push_back(ty);
2481+
argumentRefinements.push_back(refinement);
2482+
}
2483+
else
2484+
{
2485+
auto [ty, refinement] = check(scope, arg, expectedType, /*forceSingleton*/ false, /*generalize*/ false);
2486+
args.push_back(ty);
2487+
argumentRefinements.push_back(refinement);
2488+
}
24772489
}
24782490
else
24792491
{
@@ -2535,13 +2547,26 @@ InferencePack ConstraintGenerator::checkPack(const ScopePtr& scope, AstExprCall*
25352547

25362548
if (isTableUnion(target))
25372549
{
2538-
const UnionType* targetUnion = get<UnionType>(target);
2539-
std::vector<TypeId> newParts;
2550+
if (FFlag::LuauMetatableAvoidSingletonUnion)
2551+
{
2552+
const UnionType* targetUnion = get<UnionType>(target);
2553+
UnionBuilder ub{arena, builtinTypes};
25402554

2541-
for (TypeId ty : targetUnion)
2542-
newParts.push_back(arena->addType(MetatableType{ty, mt}));
2555+
for (TypeId ty : targetUnion)
2556+
ub.add(arena->addType(MetatableType{ty, mt}));
25432557

2544-
resultTy = arena->addType(UnionType{std::move(newParts)});
2558+
resultTy = ub.build();
2559+
}
2560+
else
2561+
{
2562+
const UnionType* targetUnion = get<UnionType>(target);
2563+
std::vector<TypeId> newParts;
2564+
2565+
for (TypeId ty : targetUnion)
2566+
newParts.push_back(arena->addType(MetatableType{ty, mt}));
2567+
2568+
resultTy = arena->addType(UnionType{std::move(newParts)});
2569+
}
25452570
}
25462571
else
25472572
resultTy = arena->addType(MetatableType{target, mt});

Analysis/src/ConstraintSolver.cpp

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ LUAU_FASTFLAG(LuauPushTypeConstraint2)
5252
LUAU_FASTFLAGVARIABLE(LuauScopedSeenSetInLookupTableProp)
5353
LUAU_FASTFLAGVARIABLE(LuauIterableBindNotUnify)
5454
LUAU_FASTFLAGVARIABLE(LuauAvoidOverloadSelectionForFunctionType)
55+
LUAU_FASTFLAG(LuauSimplifyIntersectionNoTreeSet)
5556

5657
namespace Luau
5758
{
@@ -2412,46 +2413,95 @@ bool ConstraintSolver::tryDispatch(const AssignIndexConstraint& c, NotNull<const
24122413

24132414
if (auto lhsIntersection = getMutable<IntersectionType>(lhsType))
24142415
{
2415-
std::set<TypeId> parts;
24162416

2417-
for (TypeId t : lhsIntersection)
2417+
if (FFlag::LuauSimplifyIntersectionNoTreeSet)
24182418
{
2419-
if (auto tbl = getMutable<TableType>(follow(t)))
2419+
2420+
TypeIds parts;
2421+
2422+
for (TypeId t : lhsIntersection)
24202423
{
2421-
if (tbl->indexer)
2424+
if (auto tbl = getMutable<TableType>(follow(t)))
24222425
{
2423-
unify(constraint, indexType, tbl->indexer->indexType);
2424-
parts.insert(tbl->indexer->indexResultType);
2425-
}
2426+
if (tbl->indexer)
2427+
{
2428+
unify(constraint, indexType, tbl->indexer->indexType);
2429+
parts.insert(tbl->indexer->indexResultType);
2430+
}
24262431

2427-
if (tbl->state == TableState::Unsealed || tbl->state == TableState::Free)
2432+
if (tbl->state == TableState::Unsealed || tbl->state == TableState::Free)
2433+
{
2434+
tbl->indexer = TableIndexer{indexType, rhsType};
2435+
parts.insert(rhsType);
2436+
}
2437+
}
2438+
else if (auto cls = get<ExternType>(follow(t)))
24282439
{
2429-
tbl->indexer = TableIndexer{indexType, rhsType};
2430-
parts.insert(rhsType);
2440+
while (true)
2441+
{
2442+
if (cls->indexer)
2443+
{
2444+
unify(constraint, indexType, cls->indexer->indexType);
2445+
parts.insert(cls->indexer->indexResultType);
2446+
break;
2447+
}
2448+
2449+
if (cls->parent)
2450+
cls = get<ExternType>(cls->parent);
2451+
else
2452+
break;
2453+
}
24312454
}
24322455
}
2433-
else if (auto cls = get<ExternType>(follow(t)))
2456+
2457+
TypeId res = simplifyIntersection(constraint->scope, constraint->location, std::move(parts));
2458+
2459+
unify(constraint, rhsType, res);
2460+
}
2461+
else
2462+
{
2463+
2464+
std::set<TypeId> parts;
2465+
2466+
for (TypeId t : lhsIntersection)
24342467
{
2435-
while (true)
2468+
if (auto tbl = getMutable<TableType>(follow(t)))
24362469
{
2437-
if (cls->indexer)
2470+
if (tbl->indexer)
24382471
{
2439-
unify(constraint, indexType, cls->indexer->indexType);
2440-
parts.insert(cls->indexer->indexResultType);
2441-
break;
2472+
unify(constraint, indexType, tbl->indexer->indexType);
2473+
parts.insert(tbl->indexer->indexResultType);
24422474
}
24432475

2444-
if (cls->parent)
2445-
cls = get<ExternType>(cls->parent);
2446-
else
2447-
break;
2476+
if (tbl->state == TableState::Unsealed || tbl->state == TableState::Free)
2477+
{
2478+
tbl->indexer = TableIndexer{indexType, rhsType};
2479+
parts.insert(rhsType);
2480+
}
2481+
}
2482+
else if (auto cls = get<ExternType>(follow(t)))
2483+
{
2484+
while (true)
2485+
{
2486+
if (cls->indexer)
2487+
{
2488+
unify(constraint, indexType, cls->indexer->indexType);
2489+
parts.insert(cls->indexer->indexResultType);
2490+
break;
2491+
}
2492+
2493+
if (cls->parent)
2494+
cls = get<ExternType>(cls->parent);
2495+
else
2496+
break;
2497+
}
24482498
}
24492499
}
2450-
}
24512500

2452-
TypeId res = simplifyIntersection(constraint->scope, constraint->location, std::move(parts));
2501+
TypeId res = simplifyIntersection_DEPRECATED(constraint->scope, constraint->location, std::move(parts));
24532502

2454-
unify(constraint, rhsType, res);
2503+
unify(constraint, rhsType, res);
2504+
}
24552505
}
24562506

24572507
// Other types do not support index assignment.
@@ -3805,11 +3855,11 @@ TypeId ConstraintSolver::simplifyIntersection(NotNull<Scope> scope, Location loc
38053855
return ::Luau::simplifyIntersection(builtinTypes, arena, left, right).result;
38063856
}
38073857

3808-
TypeId ConstraintSolver::simplifyIntersection(NotNull<Scope> scope, Location location, std::set<TypeId> parts)
3858+
TypeId ConstraintSolver::simplifyIntersection(NotNull<Scope> scope, Location location, TypeIds parts)
38093859
{
38103860
if (FFlag::DebugLuauEqSatSimplification)
38113861
{
3812-
TypeId ty = arena->addType(IntersectionType{std::vector(parts.begin(), parts.end())});
3862+
TypeId ty = arena->addType(IntersectionType{parts.take()});
38133863

38143864
std::optional<EqSatSimplificationResult> res = eqSatSimplify(simplifier, ty);
38153865
if (!res)
@@ -3824,6 +3874,25 @@ TypeId ConstraintSolver::simplifyIntersection(NotNull<Scope> scope, Location loc
38243874
return ::Luau::simplifyIntersection(builtinTypes, arena, std::move(parts)).result;
38253875
}
38263876

3877+
TypeId ConstraintSolver::simplifyIntersection_DEPRECATED(NotNull<Scope> scope, Location location, std::set<TypeId> parts)
3878+
{
3879+
if (FFlag::DebugLuauEqSatSimplification)
3880+
{
3881+
TypeId ty = arena->addType(IntersectionType{std::vector(parts.begin(), parts.end())});
3882+
3883+
std::optional<EqSatSimplificationResult> res = eqSatSimplify(simplifier, ty);
3884+
if (!res)
3885+
return ty;
3886+
3887+
for (TypeId ty : res->newTypeFunctions)
3888+
pushConstraint(scope, location, ReduceConstraint{ty});
3889+
3890+
return res->result;
3891+
}
3892+
else
3893+
return ::Luau::simplifyIntersection_DEPRECATED(builtinTypes, arena, std::move(parts)).result;
3894+
}
3895+
38273896
TypeId ConstraintSolver::simplifyUnion(NotNull<Scope> scope, Location location, TypeId left, TypeId right)
38283897
{
38293898
if (FFlag::DebugLuauEqSatSimplification)

0 commit comments

Comments
 (0)