Skip to content

Commit f739586

Browse files
authored
Merge branch 'luau-lang:master' into master
2 parents e0602bc + a2303a6 commit f739586

File tree

83 files changed

+2122
-1129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2122
-1129
lines changed

Analysis/include/Luau/Error.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ struct UserDefinedTypeFunctionError
455455
bool operator==(const UserDefinedTypeFunctionError& rhs) const;
456456
};
457457

458+
struct ReservedIdentifier
459+
{
460+
std::string name;
461+
462+
bool operator==(const ReservedIdentifier& rhs) const;
463+
};
464+
458465
using TypeErrorData = Variant<
459466
TypeMismatch,
460467
UnknownSymbol,
@@ -504,7 +511,8 @@ using TypeErrorData = Variant<
504511
UnexpectedTypeInSubtyping,
505512
UnexpectedTypePackInSubtyping,
506513
ExplicitFunctionAnnotationRecommended,
507-
UserDefinedTypeFunctionError>;
514+
UserDefinedTypeFunctionError,
515+
ReservedIdentifier>;
508516

509517
struct TypeErrorSummary
510518
{

Analysis/include/Luau/FileResolver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct SourceCode
2020
None,
2121
Module,
2222
Script,
23-
Local
23+
Local_DEPRECATED
2424
};
2525

2626
std::string source;

Analysis/include/Luau/Generalization.h

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,24 @@ struct GeneralizationParams
1616
Polarity polarity = Polarity::None;
1717
};
1818

19+
template<typename TID>
20+
struct GeneralizationResult
21+
{
22+
std::optional<TID> result;
23+
24+
// True if the provided type was replaced with a generic.
25+
bool wasReplacedByGeneric = false;
26+
27+
bool resourceLimitsExceeded = false;
28+
29+
explicit operator bool() const
30+
{
31+
return bool(result);
32+
}
33+
};
34+
1935
// Replace a single free type by its bounds according to the polarity provided.
20-
std::optional<TypeId> generalizeType(
36+
GeneralizationResult<TypeId> generalizeType(
2137
NotNull<TypeArena> arena,
2238
NotNull<BuiltinTypes> builtinTypes,
2339
NotNull<Scope> scope,
@@ -26,7 +42,7 @@ std::optional<TypeId> generalizeType(
2642
);
2743

2844
// Generalize one type pack
29-
std::optional<TypePackId> generalizeTypePack(
45+
GeneralizationResult<TypePackId> generalizeTypePack(
3046
NotNull<TypeArena> arena,
3147
NotNull<BuiltinTypes> builtinTypes,
3248
NotNull<Scope> scope,
@@ -36,11 +52,31 @@ std::optional<TypePackId> generalizeTypePack(
3652

3753
void sealTable(NotNull<Scope> scope, TypeId ty);
3854

55+
/** Attempt to generalize a type.
56+
*
57+
* If generalizationTarget is set, then only that type will be replaced by its
58+
* bounds. The way this is intended to be used is that ty is some function that
59+
* is not fully generalized, and generalizationTarget is a type within its
60+
* signature. There should be no further constraints that could affect the
61+
* bounds of generalizationTarget.
62+
*
63+
* Returns nullopt if generalization failed due to resources limits.
64+
*/
3965
std::optional<TypeId> generalize(
66+
NotNull<TypeArena> arena,
67+
NotNull<BuiltinTypes> builtinTypes,
68+
NotNull<Scope> scope,
69+
NotNull<DenseHashSet<TypeId>> cachedTypes,
70+
TypeId ty,
71+
std::optional<TypeId> generalizationTarget = {}
72+
);
73+
74+
void pruneUnnecessaryGenerics(
4075
NotNull<TypeArena> arena,
4176
NotNull<BuiltinTypes> builtinTypes,
4277
NotNull<Scope> scope,
4378
NotNull<DenseHashSet<TypeId>> cachedTypes,
4479
TypeId ty
4580
);
46-
}
81+
82+
} // namespace Luau

Analysis/include/Luau/Simplify.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ SimplifyResult simplifyIntersection(NotNull<BuiltinTypes> builtinTypes, NotNull<
2424

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

27+
SimplifyResult simplifyIntersectWithTruthy(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId target);
28+
SimplifyResult simplifyIntersectWithFalsy(NotNull<BuiltinTypes> builtinTypes, NotNull<TypeArena> arena, TypeId target);
29+
2730
enum class Relation
2831
{
2932
Disjoint, // No A is a B or vice versa

Analysis/include/Luau/Substitution.h

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct TarjanNode
8686
struct Tarjan
8787
{
8888
Tarjan();
89+
virtual ~Tarjan() = default;
8990

9091
// Vertices (types and type packs) are indexed, using pre-order traversal.
9192
DenseHashMap<TypeId, int> typeToIndex{nullptr};
@@ -121,7 +122,7 @@ struct Tarjan
121122
void visitChildren(TypePackId tp, int index);
122123

123124
void visitChild(TypeId ty);
124-
void visitChild(TypePackId ty);
125+
void visitChild(TypePackId tp);
125126

126127
template<typename Ty>
127128
void visitChild(std::optional<Ty> ty)
@@ -132,7 +133,7 @@ struct Tarjan
132133

133134
// Visit the root vertex.
134135
TarjanResult visitRoot(TypeId ty);
135-
TarjanResult visitRoot(TypePackId ty);
136+
TarjanResult visitRoot(TypePackId tp);
136137

137138
// Used to reuse the object for a new operation
138139
void clearTarjan(const TxnLog* log);
@@ -150,26 +151,12 @@ struct Tarjan
150151
void visitSCC(int index);
151152

152153
// Each subclass can decide to ignore some nodes.
153-
virtual bool ignoreChildren(TypeId ty)
154-
{
155-
return false;
156-
}
157-
158-
virtual bool ignoreChildren(TypePackId ty)
159-
{
160-
return false;
161-
}
154+
virtual bool ignoreChildren(TypeId ty);
155+
virtual bool ignoreChildren(TypePackId ty);
162156

163157
// Some subclasses might ignore children visit, but not other actions like replacing the children
164-
virtual bool ignoreChildrenVisit(TypeId ty)
165-
{
166-
return ignoreChildren(ty);
167-
}
168-
169-
virtual bool ignoreChildrenVisit(TypePackId ty)
170-
{
171-
return ignoreChildren(ty);
172-
}
158+
virtual bool ignoreChildrenVisit(TypeId ty);
159+
virtual bool ignoreChildrenVisit(TypePackId ty);
173160

174161
// Subclasses should say which vertices are dirty,
175162
// and what to do with dirty vertices.
@@ -184,6 +171,7 @@ struct Tarjan
184171
struct Substitution : Tarjan
185172
{
186173
protected:
174+
explicit Substitution(TypeArena* arena);
187175
Substitution(const TxnLog* log_, TypeArena* arena);
188176

189177
/*
@@ -232,28 +220,23 @@ struct Substitution : Tarjan
232220
virtual TypeId clean(TypeId ty) = 0;
233221
virtual TypePackId clean(TypePackId tp) = 0;
234222

223+
protected:
235224
// Helper functions to create new types (used by subclasses)
236225
template<typename T>
237-
TypeId addType(const T& tv)
226+
TypeId addType(T tv)
238227
{
239-
return arena->addType(tv);
228+
return arena->addType(std::move(tv));
240229
}
241230

242231
template<typename T>
243-
TypePackId addTypePack(const T& tp)
232+
TypePackId addTypePack(T tp)
244233
{
245-
return arena->addTypePack(TypePackVar{tp});
234+
return arena->addTypePack(TypePackVar{std::move(tp)});
246235
}
247236

248237
private:
249238
template<typename Ty>
250-
std::optional<Ty> replace(std::optional<Ty> ty)
251-
{
252-
if (ty)
253-
return replace(*ty);
254-
else
255-
return std::nullopt;
256-
}
239+
std::optional<Ty> replace(std::optional<Ty> ty);
257240
};
258241

259242
} // namespace Luau

Analysis/include/Luau/TypeFunction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ struct TypeFunction
155155

156156
/// The reducer function for the type function.
157157
ReducerFunction<TypeId> reducer;
158+
159+
/// If true, this type function can reduce even if it is parameterized on a generic.
160+
bool canReduceGenerics = false;
158161
};
159162

160163
/// Represents a type function that may be applied to map a series of types and
@@ -167,6 +170,9 @@ struct TypePackFunction
167170

168171
/// The reducer function for the type pack function.
169172
ReducerFunction<TypePackId> reducer;
173+
174+
/// If true, this type function can reduce even if it is parameterized on a generic.
175+
bool canReduceGenerics = false;
170176
};
171177

172178
struct FunctionGraphReductionResult

Analysis/include/Luau/TypeInfer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct TypeChecker
130130
const PredicateVec& predicates = {}
131131
);
132132
WithPredicate<TypeId> checkExpr(const ScopePtr& scope, const AstExprBinary& expr, std::optional<TypeId> expectedType = std::nullopt);
133+
WithPredicate<TypeId> checkExpr_DEPRECATED(const ScopePtr& scope, const AstExprBinary& expr, std::optional<TypeId> expectedType = std::nullopt);
133134
WithPredicate<TypeId> checkExpr(const ScopePtr& scope, const AstExprTypeAssertion& expr);
134135
WithPredicate<TypeId> checkExpr(const ScopePtr& scope, const AstExprError& expr);
135136
WithPredicate<TypeId> checkExpr(const ScopePtr& scope, const AstExprIfElse& expr, std::optional<TypeId> expectedType = std::nullopt);

Analysis/src/AutocompleteCore.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ LUAU_FASTFLAGVARIABLE(LuauAutocompleteRefactorsForIncrementalAutocomplete)
3131

3232
LUAU_FASTFLAGVARIABLE(LuauAutocompleteUsesModuleForTypeCompatibility)
3333
LUAU_FASTFLAGVARIABLE(LuauAutocompleteUnionCopyPreviousSeen)
34+
LUAU_FASTFLAGVARIABLE(LuauAutocompleteMissingFollows)
3435

3536
static const std::unordered_set<std::string> kStatementStartingKeywords =
3637
{"while", "if", "local", "repeat", "function", "do", "for", "return", "break", "continue", "type", "export"};
@@ -83,6 +84,8 @@ static ParenthesesRecommendation getParenRecommendationForIntersect(const Inters
8384
ParenthesesRecommendation rec = ParenthesesRecommendation::None;
8485
for (Luau::TypeId partId : intersect->parts)
8586
{
87+
if (FFlag::LuauAutocompleteMissingFollows)
88+
partId = follow(partId);
8689
if (auto partFunc = Luau::get<FunctionType>(partId))
8790
{
8891
rec = std::max(rec, getParenRecommendationForFunc(partFunc, nodes));
@@ -1623,6 +1626,8 @@ static std::optional<AutocompleteEntryMap> autocompleteStringParams(
16231626
{
16241627
for (TypeId part : intersect->parts)
16251628
{
1629+
if (FFlag::LuauAutocompleteMissingFollows)
1630+
part = follow(part);
16261631
if (auto candidateFunctionType = Luau::get<FunctionType>(part))
16271632
{
16281633
if (std::optional<AutocompleteEntryMap> ret = performCallback(candidateFunctionType))

Analysis/src/BuiltinDefinitions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131

3232
LUAU_FASTFLAG(LuauSolverV2)
33-
LUAU_FASTFLAG(LuauNonReentrantGeneralization)
33+
LUAU_FASTFLAG(LuauNonReentrantGeneralization2)
3434
LUAU_FASTFLAGVARIABLE(LuauTableCloneClonesType3)
3535
LUAU_FASTFLAG(LuauTrackInteriorFreeTypesOnScope)
3636
LUAU_FASTFLAGVARIABLE(LuauFollowTableFreeze)
@@ -314,8 +314,8 @@ void registerBuiltinGlobals(Frontend& frontend, GlobalTypes& globals, bool typeC
314314

315315
TypeArena& arena = globals.globalTypes;
316316
NotNull<BuiltinTypes> builtinTypes = globals.builtinTypes;
317-
Scope* globalScope = nullptr; // NotNull<Scope> when removing FFlag::LuauNonReentrantGeneralization
318-
if (FFlag::LuauNonReentrantGeneralization)
317+
Scope* globalScope = nullptr; // NotNull<Scope> when removing FFlag::LuauNonReentrantGeneralization2
318+
if (FFlag::LuauNonReentrantGeneralization2)
319319
globalScope = globals.globalScope.get();
320320

321321
if (FFlag::LuauSolverV2)
@@ -1614,7 +1614,7 @@ bool MagicFreeze::infer(const MagicFunctionCallContext& context)
16141614
if (resultTy && !get<BlockedType>(resultTy))
16151615
{
16161616
// If there's an existing result type but it's _not_ blocked, then
1617-
// we aren't type stating this builtin and should fall back to
1617+
// we aren't type stating this builtin and should fall back to
16181618
// regular inference.
16191619
return false;
16201620
}

0 commit comments

Comments
 (0)