Skip to content

Commit 5e0779f

Browse files
joonyoo181aatxealexmccordandyfriesenVighnesh-V
authored
Sync to upstream/release/636 (#1346)
# What's Changed? - Telemetry support for usage of any type in old/new solver - Bug fixes and flag removals with the new solver ## New Solver - Fixed constraint ordering bug to infer types more accurately - Improved inferring a call to `setmetatable()` ## VM - Restored global metatable lookup for `typeof` on lightuserdata to fix unintentional API change (Fixes #1335) --- ### Internal Contributors Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Dibri Nsofor <dnsofor@roblox.com> Co-authored-by: Jeremy Yoo <jyoo@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com> --------- Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Alexander McCord <amccord@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Vighnesh <vvijay@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: David Cope <dcope@roblox.com> Co-authored-by: Lily Brown <lbrown@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
1 parent a80abdb commit 5e0779f

40 files changed

+2281
-676
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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/Config.h"
5+
#include "Luau/ModuleResolver.h"
6+
#include "Luau/Scope.h"
7+
#include "Luau/Variant.h"
8+
#include "Luau/Normalize.h"
9+
#include "Luau/TypePack.h"
10+
#include "Luau/TypeArena.h"
11+
12+
#include <mutex>
13+
#include <string>
14+
#include <vector>
15+
#include <optional>
16+
17+
namespace Luau
18+
{
19+
20+
class AstStat;
21+
class ParseError;
22+
struct TypeError;
23+
struct LintWarning;
24+
struct GlobalTypes;
25+
struct ModuleResolver;
26+
struct ParseResult;
27+
struct DcrLogger;
28+
29+
struct TelemetryTypePair
30+
{
31+
std::string annotatedType;
32+
std::string inferredType;
33+
};
34+
35+
struct AnyTypeSummary
36+
{
37+
TypeArena arena;
38+
39+
DenseHashSet<TypeId> seenTypeFamilyInstances{nullptr};
40+
41+
int recursionCount = 0;
42+
43+
std::string root;
44+
int strictCount = 0;
45+
46+
DenseHashMap<const void*, bool> seen{nullptr};
47+
48+
AnyTypeSummary();
49+
50+
void traverse(Module* module, AstStat* src, NotNull<BuiltinTypes> builtinTypes);
51+
52+
std::pair<bool, TypeId> checkForAnyCast(Scope* scope, AstExprTypeAssertion* expr);
53+
54+
// Todo: errors resolved by anys
55+
void reportError(Location location, TypeErrorData err);
56+
57+
bool containsAny(TypePackId typ);
58+
bool containsAny(TypeId typ);
59+
60+
bool isAnyCast(Scope* scope, AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
61+
bool isAnyCall(Scope* scope, AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
62+
63+
bool hasVariadicAnys(Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
64+
bool hasArgAnys(Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
65+
bool hasAnyReturns(Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
66+
67+
TypeId checkForFamilyInhabitance(TypeId instance, Location location);
68+
TypeId lookupType(AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
69+
TypePackId reconstructTypePack(AstArray<AstExpr*> exprs, Module* module, NotNull<BuiltinTypes> builtinTypes);
70+
71+
DenseHashSet<TypeId> seenTypeFunctionInstances{nullptr};
72+
TypeId lookupAnnotation(AstType* annotation, Module* module, NotNull<BuiltinTypes> builtintypes);
73+
std::optional<TypePackId> lookupPackAnnotation(AstTypePack* annotation, Module* module);
74+
TypeId checkForTypeFunctionInhabitance(TypeId instance, Location location);
75+
76+
enum Pattern : uint64_t
77+
{
78+
Casts,
79+
FuncArg,
80+
FuncRet,
81+
FuncApp,
82+
VarAnnot,
83+
VarAny,
84+
TableProp,
85+
Alias,
86+
Assign
87+
};
88+
89+
struct TypeInfo
90+
{
91+
Pattern code;
92+
std::string node;
93+
TelemetryTypePair type;
94+
std::string debug;
95+
96+
explicit TypeInfo(Pattern code, std::string node, TelemetryTypePair type);
97+
};
98+
99+
std::vector<TypeInfo> typeInfo;
100+
101+
/**
102+
* Fabricates a scope that is a child of another scope.
103+
* @param node the lexical node that the scope belongs to.
104+
* @param parent the parent scope of the new scope. Must not be null.
105+
*/
106+
Scope* childScope(AstNode* node, const Scope* parent);
107+
108+
Scope* findInnerMostScope(Location location, Module* module);
109+
110+
void visit(Scope* scope, AstStat* stat, Module* module, NotNull<BuiltinTypes> builtinTypes);
111+
void visit(Scope* scope, AstStatBlock* block, Module* module, NotNull<BuiltinTypes> builtinTypes);
112+
void visit(Scope* scope, AstStatIf* ifStatement, Module* module, NotNull<BuiltinTypes> builtinTypes);
113+
void visit(Scope* scope, AstStatWhile* while_, Module* module, NotNull<BuiltinTypes> builtinTypes);
114+
void visit(Scope* scope, AstStatRepeat* repeat, Module* module, NotNull<BuiltinTypes> builtinTypes);
115+
void visit(Scope* scope, AstStatReturn* ret, Module* module, NotNull<BuiltinTypes> builtinTypes);
116+
void visit(Scope* scope, AstStatLocal* local, Module* module, NotNull<BuiltinTypes> builtinTypes);
117+
void visit(Scope* scope, AstStatFor* for_, Module* module, NotNull<BuiltinTypes> builtinTypes);
118+
void visit(Scope* scope, AstStatForIn* forIn, Module* module, NotNull<BuiltinTypes> builtinTypes);
119+
void visit(Scope* scope, AstStatAssign* assign, Module* module, NotNull<BuiltinTypes> builtinTypes);
120+
void visit(Scope* scope, AstStatCompoundAssign* assign, Module* module, NotNull<BuiltinTypes> builtinTypes);
121+
void visit(Scope* scope, AstStatFunction* function, Module* module, NotNull<BuiltinTypes> builtinTypes);
122+
void visit(Scope* scope, AstStatLocalFunction* function, Module* module, NotNull<BuiltinTypes> builtinTypes);
123+
void visit(Scope* scope, AstStatTypeAlias* alias, Module* module, NotNull<BuiltinTypes> builtinTypes);
124+
void visit(Scope* scope, AstStatExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
125+
void visit(Scope* scope, AstStatDeclareGlobal* declareGlobal, Module* module, NotNull<BuiltinTypes> builtinTypes);
126+
void visit(Scope* scope, AstStatDeclareClass* declareClass, Module* module, NotNull<BuiltinTypes> builtinTypes);
127+
void visit(Scope* scope, AstStatDeclareFunction* declareFunction, Module* module, NotNull<BuiltinTypes> builtinTypes);
128+
void visit(Scope* scope, AstStatError* error, Module* module, NotNull<BuiltinTypes> builtinTypes);
129+
};
130+
131+
} // namespace Luau

Analysis/include/Luau/AstQuery.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ struct ExprOrLocal
6161
AstLocal* local = nullptr;
6262
};
6363

64+
struct FindFullAncestry final : public AstVisitor
65+
{
66+
std::vector<AstNode*> nodes;
67+
Position pos;
68+
Position documentEnd;
69+
bool includeTypes = false;
70+
71+
explicit FindFullAncestry(Position pos, Position documentEnd, bool includeTypes = false);
72+
73+
bool visit(AstType* type) override;
74+
75+
bool visit(AstStatFunction* node) override;
76+
77+
bool visit(AstNode* node) override;
78+
};
79+
6480
std::vector<AstNode*> findAncestryAtPositionForAutocomplete(const SourceModule& source, Position pos);
6581
std::vector<AstNode*> findAncestryAtPositionForAutocomplete(AstStatBlock* root, Position pos);
6682
std::vector<AstNode*> findAstAncestryOfPosition(const SourceModule& source, Position pos, bool includeTypes = false);

Analysis/include/Luau/Frontend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Luau/Scope.h"
1010
#include "Luau/TypeCheckLimits.h"
1111
#include "Luau/Variant.h"
12+
#include "Luau/AnyTypeSummary.h"
1213

1314
#include <mutex>
1415
#include <string>
@@ -31,6 +32,7 @@ struct ParseResult;
3132
struct HotComment;
3233
struct BuildQueueItem;
3334
struct FrontendCancellationToken;
35+
struct AnyTypeSummary;
3436

3537
struct LoadDefinitionFileResult
3638
{

Analysis/include/Luau/Module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Luau/ParseResult.h"
99
#include "Luau/Scope.h"
1010
#include "Luau/TypeArena.h"
11+
#include "Luau/AnyTypeSummary.h"
1112

1213
#include <memory>
1314
#include <vector>
@@ -18,6 +19,7 @@ namespace Luau
1819
{
1920

2021
struct Module;
22+
struct AnyTypeSummary;
2123

2224
using ScopePtr = std::shared_ptr<struct Scope>;
2325
using ModulePtr = std::shared_ptr<Module>;
@@ -71,6 +73,10 @@ struct Module
7173
TypeArena interfaceTypes;
7274
TypeArena internalTypes;
7375

76+
// Summary of Ast Nodes that either contain
77+
// user annotated anys or typechecker inferred anys
78+
AnyTypeSummary ats{};
79+
7480
// Scopes and AST types refer to parse data, so we need to keep that alive
7581
std::shared_ptr<Allocator> allocator;
7682
std::shared_ptr<AstNameTable> names;

Analysis/include/Luau/Normalize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TypeIds
5959
const_iterator begin() const;
6060
const_iterator end() const;
6161
iterator erase(const_iterator it);
62+
void erase(TypeId ty);
6263

6364
size_t size() const;
6465
bool empty() const;

0 commit comments

Comments
 (0)