Skip to content

Commit e0602bc

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

File tree

95 files changed

+3488
-1270
lines changed

Some content is hidden

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

95 files changed

+3488
-1270
lines changed

Analysis/include/Luau/ConstraintGenerator.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
#include "Luau/ModuleResolver.h"
1212
#include "Luau/Normalize.h"
1313
#include "Luau/NotNull.h"
14+
#include "Luau/Polarity.h"
1415
#include "Luau/Refinement.h"
1516
#include "Luau/Symbol.h"
1617
#include "Luau/TypeFwd.h"
1718
#include "Luau/TypeUtils.h"
18-
#include "Luau/Variant.h"
1919

2020
#include <memory>
2121
#include <vector>
22-
#include <unordered_map>
2322

2423
namespace Luau
2524
{
@@ -162,19 +161,26 @@ struct ConstraintGenerator
162161
void visitFragmentRoot(const ScopePtr& resumeScope, AstStatBlock* block);
163162

164163
private:
165-
std::vector<std::vector<TypeId>> interiorTypes;
164+
struct InteriorFreeTypes
165+
{
166+
std::vector<TypeId> types;
167+
std::vector<TypePackId> typePacks;
168+
};
169+
170+
std::vector<std::vector<TypeId>> DEPRECATED_interiorTypes;
171+
std::vector<InteriorFreeTypes> interiorFreeTypes;
166172

167173
/**
168174
* Fabricates a new free type belonging to a given scope.
169175
* @param scope the scope the free type belongs to.
170176
*/
171-
TypeId freshType(const ScopePtr& scope);
177+
TypeId freshType(const ScopePtr& scope, Polarity polarity = Polarity::Unknown);
172178

173179
/**
174180
* Fabricates a new free type pack belonging to a given scope.
175181
* @param scope the scope the free type pack belongs to.
176182
*/
177-
TypePackId freshTypePack(const ScopePtr& scope);
183+
TypePackId freshTypePack(const ScopePtr& scope, Polarity polarity = Polarity::Unknown);
178184

179185
/**
180186
* Allocate a new TypePack with the given head and tail.
@@ -295,7 +301,7 @@ struct ConstraintGenerator
295301
);
296302

297303
Inference check(const ScopePtr& scope, AstExprConstantString* string, std::optional<TypeId> expectedType, bool forceSingleton);
298-
Inference check(const ScopePtr& scope, AstExprConstantBool* bool_, std::optional<TypeId> expectedType, bool forceSingleton);
304+
Inference check(const ScopePtr& scope, AstExprConstantBool* boolExpr, std::optional<TypeId> expectedType, bool forceSingleton);
299305
Inference check(const ScopePtr& scope, AstExprLocal* local);
300306
Inference check(const ScopePtr& scope, AstExprGlobal* global);
301307
Inference checkIndexName(const ScopePtr& scope, const RefinementKey* key, AstExpr* indexee, const std::string& index, Location indexLocation);
@@ -371,6 +377,11 @@ struct ConstraintGenerator
371377
**/
372378
TypeId resolveType(const ScopePtr& scope, AstType* ty, bool inTypeArguments, bool replaceErrorWithFresh = false);
373379

380+
// resolveType() is recursive, but we only want to invoke
381+
// inferGenericPolarities() once at the very end. We thus isolate the
382+
// recursive part of the algorithm to this internal helper.
383+
TypeId resolveType_(const ScopePtr& scope, AstType* ty, bool inTypeArguments, bool replaceErrorWithFresh = false);
384+
374385
/**
375386
* Resolves a type pack from its AST annotation.
376387
* @param scope the scope that the type annotation appears within.
@@ -380,6 +391,9 @@ struct ConstraintGenerator
380391
**/
381392
TypePackId resolveTypePack(const ScopePtr& scope, AstTypePack* tp, bool inTypeArguments, bool replaceErrorWithFresh = false);
382393

394+
// Inner hepler for resolveTypePack
395+
TypePackId resolveTypePack_(const ScopePtr& scope, AstTypePack* tp, bool inTypeArguments, bool replaceErrorWithFresh = false);
396+
383397
/**
384398
* Resolves a type pack from its AST annotation.
385399
* @param scope the scope that the type annotation appears within.
@@ -418,7 +432,7 @@ struct ConstraintGenerator
418432
**/
419433
std::vector<std::pair<Name, GenericTypePackDefinition>> createGenericPacks(
420434
const ScopePtr& scope,
421-
AstArray<AstGenericTypePack*> packs,
435+
AstArray<AstGenericTypePack*> generics,
422436
bool useCache = false,
423437
bool addTypes = true
424438
);

Analysis/include/Luau/DataFlowGraph.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ struct DataFlowGraphBuilder
129129

130130
/// A stack of scopes used by the visitor to see where we are.
131131
ScopeStack scopeStack;
132-
133-
DfgScope* currentScope();
132+
NotNull<DfgScope> currentScope();
133+
DfgScope* currentScope_DEPRECATED();
134134

135135
struct FunctionCapture
136136
{
@@ -148,8 +148,8 @@ struct DataFlowGraphBuilder
148148
void joinBindings(DfgScope* p, const DfgScope& a, const DfgScope& b);
149149
void joinProps(DfgScope* p, const DfgScope& a, const DfgScope& b);
150150

151-
DefId lookup(Symbol symbol);
152-
DefId lookup(DefId def, const std::string& key);
151+
DefId lookup(Symbol symbol, Location location);
152+
DefId lookup(DefId def, const std::string& key, Location location);
153153

154154
ControlFlow visit(AstStatBlock* b);
155155
ControlFlow visitBlockWithoutChildScope(AstStatBlock* b);

Analysis/include/Luau/Def.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "Luau/NotNull.h"
55
#include "Luau/TypedAllocator.h"
66
#include "Luau/Variant.h"
7-
7+
#include "Luau/Location.h"
8+
#include "Luau/Symbol.h"
89
#include <string>
910
#include <optional>
1011

@@ -13,6 +14,7 @@ namespace Luau
1314

1415
struct Def;
1516
using DefId = NotNull<const Def>;
17+
struct AstLocal;
1618

1719
/**
1820
* A cell is a "single-object" value.
@@ -64,6 +66,8 @@ struct Def
6466
using V = Variant<struct Cell, struct Phi>;
6567

6668
V v;
69+
Symbol name;
70+
Location location;
6771
};
6872

6973
template<typename T>
@@ -79,7 +83,7 @@ struct DefArena
7983
{
8084
TypedAllocator<Def> allocator;
8185

82-
DefId freshCell(bool subscripted = false);
86+
DefId freshCell(Symbol sym, Location location, bool subscripted = false);
8387
DefId phi(DefId a, DefId b);
8488
DefId phi(const std::vector<DefId>& defs);
8589
};

Analysis/include/Luau/FragmentAutocomplete.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct FragmentAutocompleteResult
7575
{
7676
ModulePtr incrementalModule;
7777
Scope* freshScope;
78-
TypeArena arenaForAutocomplete;
78+
TypeArena arenaForAutocomplete_DEPRECATED;
7979
AutocompleteResult acResults;
8080
};
8181

Analysis/include/Luau/Generalization.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,39 @@
88
namespace Luau
99
{
1010

11+
template<typename TID>
12+
struct GeneralizationParams
13+
{
14+
bool foundOutsideFunctions = false;
15+
size_t useCount = 0;
16+
Polarity polarity = Polarity::None;
17+
};
18+
19+
// Replace a single free type by its bounds according to the polarity provided.
20+
std::optional<TypeId> generalizeType(
21+
NotNull<TypeArena> arena,
22+
NotNull<BuiltinTypes> builtinTypes,
23+
NotNull<Scope> scope,
24+
TypeId freeTy,
25+
const GeneralizationParams<TypeId>& params
26+
);
27+
28+
// Generalize one type pack
29+
std::optional<TypePackId> generalizeTypePack(
30+
NotNull<TypeArena> arena,
31+
NotNull<BuiltinTypes> builtinTypes,
32+
NotNull<Scope> scope,
33+
TypePackId tp,
34+
const GeneralizationParams<TypePackId>& params
35+
);
36+
37+
void sealTable(NotNull<Scope> scope, TypeId ty);
38+
1139
std::optional<TypeId> generalize(
1240
NotNull<TypeArena> arena,
1341
NotNull<BuiltinTypes> builtinTypes,
1442
NotNull<Scope> scope,
1543
NotNull<DenseHashSet<TypeId>> cachedTypes,
1644
TypeId ty
1745
);
18-
1946
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2+
#pragma once
3+
4+
#include "Luau/NotNull.h"
5+
#include "Luau/TypeFwd.h"
6+
7+
namespace Luau
8+
{
9+
10+
struct Scope;
11+
struct TypeArena;
12+
13+
void inferGenericPolarities(NotNull<TypeArena> arena, NotNull<Scope> scope, TypeId ty);
14+
void inferGenericPolarities(NotNull<TypeArena> arena, NotNull<Scope> scope, TypePackId tp);
15+
16+
} // namespace Luau

Analysis/include/Luau/InsertionOrderedMap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ struct InsertionOrderedMap
6767
return &pairs.at(it->second).second;
6868
}
6969

70+
V& operator[](const K& k)
71+
{
72+
auto it = indices.find(k);
73+
if (it == indices.end())
74+
{
75+
pairs.push_back(std::make_pair(k, V()));
76+
indices[k] = pairs.size() - 1;
77+
return pairs.back().second;
78+
}
79+
else
80+
return pairs.at(it->second).second;
81+
}
82+
7083
const_iterator begin() const
7184
{
7285
return pairs.begin();

Analysis/include/Luau/Polarity.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2+
#pragma once
3+
4+
#include <cstdint>
5+
6+
namespace Luau
7+
{
8+
9+
enum struct Polarity : uint8_t
10+
{
11+
None = 0b000,
12+
Positive = 0b001,
13+
Negative = 0b010,
14+
Mixed = 0b011,
15+
Unknown = 0b100,
16+
};
17+
18+
inline Polarity operator|(Polarity lhs, Polarity rhs)
19+
{
20+
return Polarity(uint8_t(lhs) | uint8_t(rhs));
21+
}
22+
23+
inline Polarity& operator|=(Polarity& lhs, Polarity rhs)
24+
{
25+
lhs = lhs | rhs;
26+
return lhs;
27+
}
28+
29+
inline Polarity operator&(Polarity lhs, Polarity rhs)
30+
{
31+
return Polarity(uint8_t(lhs) & uint8_t(rhs));
32+
}
33+
34+
inline Polarity& operator&=(Polarity& lhs, Polarity rhs)
35+
{
36+
lhs = lhs & rhs;
37+
return lhs;
38+
}
39+
40+
inline bool isPositive(Polarity p)
41+
{
42+
return bool(p & Polarity::Positive);
43+
}
44+
45+
inline bool isNegative(Polarity p)
46+
{
47+
return bool(p & Polarity::Negative);
48+
}
49+
50+
inline bool isKnown(Polarity p)
51+
{
52+
return p != Polarity::Unknown;
53+
}
54+
55+
inline Polarity invert(Polarity p)
56+
{
57+
switch (p)
58+
{
59+
case Polarity::Positive:
60+
return Polarity::Negative;
61+
case Polarity::Negative:
62+
return Polarity::Positive;
63+
default:
64+
return p;
65+
}
66+
}
67+
68+
} // namespace Luau

Analysis/include/Luau/Scope.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct Scope
4040
// All the children of this scope.
4141
std::vector<NotNull<Scope>> children;
4242
std::unordered_map<Symbol, Binding> bindings;
43-
TypePackId returnType;
43+
TypePackId returnType = nullptr;
4444
std::optional<TypePackId> varargPack;
4545

4646
TypeLevel level;
@@ -100,6 +100,7 @@ struct Scope
100100
std::unordered_map<Name, TypePackId> typeAliasTypePackParameters;
101101

102102
std::optional<std::vector<TypeId>> interiorFreeTypes;
103+
std::optional<std::vector<TypePackId>> interiorFreeTypePacks;
103104
};
104105

105106
// Returns true iff the left scope encloses the right scope. A Scope* equal to

Analysis/include/Luau/Type.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
#include "Luau/Ast.h"
77
#include "Luau/Common.h"
8-
#include "Luau/Refinement.h"
98
#include "Luau/DenseHash.h"
109
#include "Luau/NotNull.h"
10+
#include "Luau/Polarity.h"
1111
#include "Luau/Predicate.h"
12+
#include "Luau/Refinement.h"
1213
#include "Luau/Unifiable.h"
1314
#include "Luau/Variant.h"
1415
#include "Luau/VecDeque.h"
@@ -37,15 +38,6 @@ struct Constraint;
3738
struct Subtyping;
3839
struct TypeChecker2;
3940

40-
enum struct Polarity : uint8_t
41-
{
42-
None = 0b000,
43-
Positive = 0b001,
44-
Negative = 0b010,
45-
Mixed = 0b011,
46-
Unknown = 0b100,
47-
};
48-
4941
/**
5042
* There are three kinds of type variables:
5143
* - `Free` variables are metavariables, which stand for unconstrained types.
@@ -80,7 +72,7 @@ struct FreeType
8072
// New constructors
8173
explicit FreeType(TypeLevel level, TypeId lowerBound, TypeId upperBound);
8274
// This one got promoted to explicit
83-
explicit FreeType(Scope* scope, TypeId lowerBound, TypeId upperBound);
75+
explicit FreeType(Scope* scope, TypeId lowerBound, TypeId upperBound, Polarity polarity = Polarity::Unknown);
8476
explicit FreeType(Scope* scope, TypeLevel level, TypeId lowerBound, TypeId upperBound);
8577
// Old constructors
8678
explicit FreeType(TypeLevel level);
@@ -99,6 +91,8 @@ struct FreeType
9991
// Only used under local type inference
10092
TypeId lowerBound = nullptr;
10193
TypeId upperBound = nullptr;
94+
95+
Polarity polarity = Polarity::Unknown;
10296
};
10397

10498
struct GenericType
@@ -107,8 +101,8 @@ struct GenericType
107101
GenericType();
108102

109103
explicit GenericType(TypeLevel level);
110-
explicit GenericType(const Name& name);
111-
explicit GenericType(Scope* scope);
104+
explicit GenericType(const Name& name, Polarity polarity = Polarity::Unknown);
105+
explicit GenericType(Scope* scope, Polarity polarity = Polarity::Unknown);
112106

113107
GenericType(TypeLevel level, const Name& name);
114108
GenericType(Scope* scope, const Name& name);
@@ -118,6 +112,8 @@ struct GenericType
118112
Scope* scope = nullptr;
119113
Name name;
120114
bool explicitName = false;
115+
116+
Polarity polarity = Polarity::Unknown;
121117
};
122118

123119
// When an equality constraint is found, it is then "bound" to that type,
@@ -1206,7 +1202,7 @@ struct TypeIterator
12061202
}
12071203
};
12081204

1209-
TypeId freshType(NotNull<TypeArena> arena, NotNull<BuiltinTypes> builtinTypes, Scope* scope);
1205+
TypeId freshType(NotNull<TypeArena> arena, NotNull<BuiltinTypes> builtinTypes, Scope* scope, Polarity polarity = Polarity::Unknown);
12101206

12111207
using TypeIdPredicate = std::function<std::optional<TypeId>(TypeId)>;
12121208
std::vector<TypeId> filterMap(TypeId type, TypeIdPredicate predicate);

0 commit comments

Comments
 (0)