1010#include " Luau/Common.h"
1111#include " Luau/FileResolver.h"
1212#include " Luau/Frontend.h"
13+ #include " Luau/TimeTrace.h"
1314#include " Luau/ToString.h"
1415#include " Luau/Subtyping.h"
1516#include " Luau/TypeInfer.h"
@@ -24,7 +25,8 @@ LUAU_FASTINT(LuauTypeInferIterationLimit)
2425LUAU_FASTINT(LuauTypeInferRecursionLimit)
2526
2627LUAU_FASTFLAGVARIABLE(LuauAutocompleteRefactorsForIncrementalAutocomplete)
27- LUAU_FASTFLAGVARIABLE(LuauAutocompleteUseLimits)
28+
29+ LUAU_FASTFLAGVARIABLE(LuauAutocompleteUsesModuleForTypeCompatibility)
2830
2931static const std::unordered_set<std::string> kStatementStartingKeywords =
3032 {" while" , " if" , " local" , " repeat" , " function" , " do" , " for" , " return" , " break" , " continue" , " type" , " export" };
@@ -146,44 +148,91 @@ static std::optional<TypeId> findExpectedTypeAt(const Module& module, AstNode* n
146148 return *it;
147149}
148150
149- static bool checkTypeMatch (TypeId subTy, TypeId superTy, NotNull<Scope> scope, TypeArena* typeArena, NotNull<BuiltinTypes> builtinTypes)
151+ static bool checkTypeMatch (
152+ const Module& module ,
153+ TypeId subTy,
154+ TypeId superTy,
155+ NotNull<Scope> scope,
156+ TypeArena* typeArena,
157+ NotNull<BuiltinTypes> builtinTypes
158+ )
150159{
151160 InternalErrorReporter iceReporter;
152161 UnifierSharedState unifierState (&iceReporter);
153162 SimplifierPtr simplifier = newSimplifier (NotNull{typeArena}, builtinTypes);
154163 Normalizer normalizer{typeArena, builtinTypes, NotNull{&unifierState}};
155-
156- if (FFlag::LuauSolverV2)
164+ if (FFlag::LuauAutocompleteUsesModuleForTypeCompatibility)
157165 {
158- TypeCheckLimits limits;
159- TypeFunctionRuntime typeFunctionRuntime{
160- NotNull{&iceReporter}, NotNull{&limits}
161- }; // TODO: maybe subtyping checks should not invoke user-defined type function runtime
166+ if (module .checkedInNewSolver )
167+ {
168+ TypeCheckLimits limits;
169+ TypeFunctionRuntime typeFunctionRuntime{
170+ NotNull{&iceReporter}, NotNull{&limits}
171+ }; // TODO: maybe subtyping checks should not invoke user-defined type function runtime
172+
173+ unifierState.counters .recursionLimit = FInt::LuauTypeInferRecursionLimit;
174+ unifierState.counters .iterationLimit = FInt::LuauTypeInferIterationLimit;
162175
163- unifierState.counters .recursionLimit = FInt::LuauTypeInferRecursionLimit;
164- unifierState.counters .iterationLimit = FInt::LuauTypeInferIterationLimit;
176+ Subtyping subtyping{
177+ builtinTypes,
178+ NotNull{typeArena},
179+ NotNull{simplifier.get ()},
180+ NotNull{&normalizer},
181+ NotNull{&typeFunctionRuntime},
182+ NotNull{&iceReporter}
183+ };
165184
166- Subtyping subtyping{
167- builtinTypes, NotNull{typeArena}, NotNull{simplifier.get ()}, NotNull{&normalizer}, NotNull{&typeFunctionRuntime}, NotNull{&iceReporter}
168- };
185+ return subtyping.isSubtype (subTy, superTy, scope).isSubtype ;
186+ }
187+ else
188+ {
189+ Unifier unifier (NotNull<Normalizer>{&normalizer}, scope, Location (), Variance::Covariant);
190+
191+ // Cost of normalization can be too high for autocomplete response time requirements
192+ unifier.normalize = false ;
193+ unifier.checkInhabited = false ;
169194
170- return subtyping.isSubtype (subTy, superTy, scope).isSubtype ;
195+ unifierState.counters .recursionLimit = FInt::LuauTypeInferRecursionLimit;
196+ unifierState.counters .iterationLimit = FInt::LuauTypeInferIterationLimit;
197+
198+ return unifier.canUnify (subTy, superTy).empty ();
199+ }
171200 }
172201 else
173202 {
174- Unifier unifier (NotNull<Normalizer>{&normalizer}, scope, Location (), Variance::Covariant);
203+ if (FFlag::LuauSolverV2)
204+ {
205+ TypeCheckLimits limits;
206+ TypeFunctionRuntime typeFunctionRuntime{
207+ NotNull{&iceReporter}, NotNull{&limits}
208+ }; // TODO: maybe subtyping checks should not invoke user-defined type function runtime
175209
176- // Cost of normalization can be too high for autocomplete response time requirements
177- unifier.normalize = false ;
178- unifier.checkInhabited = false ;
210+ unifierState.counters .recursionLimit = FInt::LuauTypeInferRecursionLimit;
211+ unifierState.counters .iterationLimit = FInt::LuauTypeInferIterationLimit;
179212
180- if (FFlag::LuauAutocompleteUseLimits)
213+ Subtyping subtyping{
214+ builtinTypes,
215+ NotNull{typeArena},
216+ NotNull{simplifier.get ()},
217+ NotNull{&normalizer},
218+ NotNull{&typeFunctionRuntime},
219+ NotNull{&iceReporter}
220+ };
221+
222+ return subtyping.isSubtype (subTy, superTy, scope).isSubtype ;
223+ }
224+ else
181225 {
226+ Unifier unifier (NotNull<Normalizer>{&normalizer}, scope, Location (), Variance::Covariant);
227+
228+ // Cost of normalization can be too high for autocomplete response time requirements
229+ unifier.normalize = false ;
230+ unifier.checkInhabited = false ;
182231 unifierState.counters .recursionLimit = FInt::LuauTypeInferRecursionLimit;
183232 unifierState.counters .iterationLimit = FInt::LuauTypeInferIterationLimit;
184- }
185233
186- return unifier.canUnify (subTy, superTy).empty ();
234+ return unifier.canUnify (subTy, superTy).empty ();
235+ }
187236 }
188237}
189238
@@ -209,10 +258,10 @@ static TypeCorrectKind checkTypeCorrectKind(
209258
210259 TypeId expectedType = follow (*typeAtPosition);
211260
212- auto checkFunctionType = [typeArena, builtinTypes, moduleScope, &expectedType](const FunctionType* ftv)
261+ auto checkFunctionType = [typeArena, builtinTypes, moduleScope, &expectedType, & module ](const FunctionType* ftv)
213262 {
214263 if (std::optional<TypeId> firstRetTy = first (ftv->retTypes ))
215- return checkTypeMatch (*firstRetTy, expectedType, moduleScope, typeArena, builtinTypes);
264+ return checkTypeMatch (module , *firstRetTy, expectedType, moduleScope, typeArena, builtinTypes);
216265
217266 return false ;
218267 };
@@ -235,7 +284,7 @@ static TypeCorrectKind checkTypeCorrectKind(
235284 }
236285 }
237286
238- return checkTypeMatch (ty, expectedType, moduleScope, typeArena, builtinTypes) ? TypeCorrectKind::Correct : TypeCorrectKind::None;
287+ return checkTypeMatch (module , ty, expectedType, moduleScope, typeArena, builtinTypes) ? TypeCorrectKind::Correct : TypeCorrectKind::None;
239288}
240289
241290enum class PropIndexType
@@ -286,7 +335,7 @@ static void autocompleteProps(
286335 // When called with '.', but declared with 'self', it is considered invalid if first argument is compatible
287336 if (std::optional<TypeId> firstArgTy = first (ftv->argTypes ))
288337 {
289- if (checkTypeMatch (rootTy, *firstArgTy, NotNull{module .getModuleScope ().get ()}, typeArena, builtinTypes))
338+ if (checkTypeMatch (module , rootTy, *firstArgTy, NotNull{module .getModuleScope ().get ()}, typeArena, builtinTypes))
290339 return calledWithSelf;
291340 }
292341
@@ -1714,6 +1763,7 @@ AutocompleteResult autocomplete_(
17141763 StringCompletionCallback callback
17151764)
17161765{
1766+ LUAU_TIMETRACE_SCOPE (" Luau::autocomplete_" , " AutocompleteCore" );
17171767 AstNode* node = ancestry.back ();
17181768
17191769 AstExprConstantNil dummy{Location{}};
0 commit comments