2022-06-03 23:15:45 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Ast.h"
|
2022-06-17 02:05:14 +01:00
|
|
|
#include "Luau/Constraint.h"
|
2023-03-17 19:20:37 +00:00
|
|
|
#include "Luau/ControlFlow.h"
|
2022-12-02 18:09:59 +00:00
|
|
|
#include "Luau/DataFlowGraph.h"
|
2023-10-21 02:10:30 +01:00
|
|
|
#include "Luau/InsertionOrderedMap.h"
|
2022-06-03 23:15:45 +01:00
|
|
|
#include "Luau/Module.h"
|
2022-09-02 00:14:03 +01:00
|
|
|
#include "Luau/ModuleResolver.h"
|
2023-10-21 02:10:30 +01:00
|
|
|
#include "Luau/Normalize.h"
|
2022-06-17 02:05:14 +01:00
|
|
|
#include "Luau/NotNull.h"
|
2023-03-17 19:20:37 +00:00
|
|
|
#include "Luau/Refinement.h"
|
2022-06-03 23:15:45 +01:00
|
|
|
#include "Luau/Symbol.h"
|
2023-10-21 02:10:30 +01:00
|
|
|
#include "Luau/TypeFwd.h"
|
2023-04-28 20:55:13 +01:00
|
|
|
#include "Luau/TypeUtils.h"
|
2022-06-03 23:15:45 +01:00
|
|
|
#include "Luau/Variant.h"
|
2023-10-21 02:10:30 +01:00
|
|
|
#include "Luau/Normalize.h"
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2022-06-03 23:15:45 +01:00
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-07-29 05:24:07 +01:00
|
|
|
struct Scope;
|
|
|
|
using ScopePtr = std::shared_ptr<Scope>;
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2022-09-08 23:14:25 +01:00
|
|
|
struct DcrLogger;
|
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
struct Inference
|
|
|
|
{
|
|
|
|
TypeId ty = nullptr;
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
RefinementId refinement = nullptr;
|
2022-10-28 11:37:29 +01:00
|
|
|
|
|
|
|
Inference() = default;
|
|
|
|
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
explicit Inference(TypeId ty, RefinementId refinement = nullptr)
|
2022-10-28 11:37:29 +01:00
|
|
|
: ty(ty)
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
, refinement(refinement)
|
2022-10-28 11:37:29 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InferencePack
|
|
|
|
{
|
|
|
|
TypePackId tp = nullptr;
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
std::vector<RefinementId> refinements;
|
2022-10-28 11:37:29 +01:00
|
|
|
|
|
|
|
InferencePack() = default;
|
|
|
|
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
explicit InferencePack(TypePackId tp, const std::vector<RefinementId>& refinements = {})
|
2022-10-28 11:37:29 +01:00
|
|
|
: tp(tp)
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
, refinements(refinements)
|
2022-10-28 11:37:29 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-03 23:45:04 +00:00
|
|
|
struct ConstraintGenerator
|
2022-06-03 23:15:45 +01:00
|
|
|
{
|
|
|
|
// A list of all the scopes in the module. This vector holds ownership of the
|
|
|
|
// scope pointers; the scopes themselves borrow pointers to other scopes to
|
|
|
|
// define the scope hierarchy.
|
2022-07-29 05:24:07 +01:00
|
|
|
std::vector<std::pair<Location, ScopePtr>> scopes;
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2022-08-11 22:01:33 +01:00
|
|
|
ModulePtr module;
|
2023-01-04 20:53:17 +00:00
|
|
|
NotNull<BuiltinTypes> builtinTypes;
|
2022-07-01 00:52:43 +01:00
|
|
|
const NotNull<TypeArena> arena;
|
2022-06-03 23:15:45 +01:00
|
|
|
// The root scope of the module we're generating constraints for.
|
2023-11-03 23:45:04 +00:00
|
|
|
// This is null when the CG is initially constructed.
|
2022-07-29 05:24:07 +01:00
|
|
|
Scope* rootScope;
|
2022-11-10 22:53:13 +00:00
|
|
|
|
2024-03-15 23:37:39 +00:00
|
|
|
TypeContext typeContext = TypeContext::Default;
|
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
struct InferredBinding
|
|
|
|
{
|
|
|
|
Scope* scope;
|
|
|
|
Location location;
|
|
|
|
TypeIds types;
|
|
|
|
};
|
|
|
|
|
2023-11-17 18:46:18 +00:00
|
|
|
// Some locals have multiple type states. We wish for Scope::bindings to
|
|
|
|
// map each local name onto the union of every type that the local can have
|
|
|
|
// over its lifetime, so we use this map to accumulate the set of types it
|
|
|
|
// might have.
|
|
|
|
//
|
|
|
|
// See the functions recordInferredBinding and fillInInferredBindings.
|
|
|
|
DenseHashMap<Symbol, InferredBinding> inferredBindings{{}};
|
2023-10-13 21:20:12 +01:00
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
// Constraints that go straight to the solver.
|
|
|
|
std::vector<ConstraintPtr> constraints;
|
|
|
|
|
|
|
|
// Constraints that do not go to the solver right away. Other constraints
|
|
|
|
// will enqueue them during solving.
|
|
|
|
std::vector<ConstraintPtr> unqueuedConstraints;
|
|
|
|
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
// The private scope of type aliases for which the type parameters belong to.
|
2022-08-04 23:35:33 +01:00
|
|
|
DenseHashMap<const AstStatTypeAlias*, ScopePtr> astTypeAliasDefiningScopes{nullptr};
|
2022-12-09 19:57:01 +00:00
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
NotNull<const DataFlowGraph> dfg;
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
RefinementArena refinementArena;
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2022-07-01 00:52:43 +01:00
|
|
|
int recursionCount = 0;
|
|
|
|
|
|
|
|
// It is pretty uncommon for constraint generation to itself produce errors, but it can happen.
|
|
|
|
std::vector<TypeError> errors;
|
|
|
|
|
2023-08-04 20:18:54 +01:00
|
|
|
// Needed to be able to enable error-suppression preservation for immediate refinements.
|
|
|
|
NotNull<Normalizer> normalizer;
|
2022-09-02 00:14:03 +01:00
|
|
|
// Needed to resolve modules to make 'require' import types properly.
|
|
|
|
NotNull<ModuleResolver> moduleResolver;
|
2022-07-01 00:52:43 +01:00
|
|
|
// Occasionally constraint generation needs to produce an ICE.
|
|
|
|
const NotNull<InternalErrorReporter> ice;
|
|
|
|
|
2022-08-11 22:01:33 +01:00
|
|
|
ScopePtr globalScope;
|
2023-04-28 20:55:13 +01:00
|
|
|
|
|
|
|
std::function<void(const ModuleName&, const ScopePtr&)> prepareModuleScope;
|
2023-07-28 16:13:53 +01:00
|
|
|
std::vector<RequireCycle> requireCycles;
|
2023-04-28 20:55:13 +01:00
|
|
|
|
2024-06-14 21:21:20 +01:00
|
|
|
DenseHashMap<TypeId, TypeIds> localTypes{nullptr};
|
2024-06-07 18:51:12 +01:00
|
|
|
|
2022-09-08 23:14:25 +01:00
|
|
|
DcrLogger* logger;
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
ConstraintGenerator(ModulePtr module, NotNull<Normalizer> normalizer, NotNull<ModuleResolver> moduleResolver, NotNull<BuiltinTypes> builtinTypes,
|
|
|
|
NotNull<InternalErrorReporter> ice, const ScopePtr& globalScope, std::function<void(const ModuleName&, const ScopePtr&)> prepareModuleScope,
|
|
|
|
DcrLogger* logger, NotNull<DataFlowGraph> dfg, std::vector<RequireCycle> requireCycles);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2023-09-30 02:13:05 +01:00
|
|
|
/**
|
2023-11-03 23:45:04 +00:00
|
|
|
* The entry point to the ConstraintGenerator. This will construct a set
|
2023-09-30 02:13:05 +01:00
|
|
|
* of scopes, constraints, and free types that can be solved later.
|
|
|
|
* @param block the root block to generate constraints for.
|
|
|
|
*/
|
|
|
|
void visitModuleRoot(AstStatBlock* block);
|
|
|
|
|
|
|
|
private:
|
2024-02-23 20:08:34 +00:00
|
|
|
std::vector<std::vector<TypeId>> interiorTypes;
|
|
|
|
|
2022-06-03 23:15:45 +01:00
|
|
|
/**
|
|
|
|
* Fabricates a new free type belonging to a given scope.
|
2022-07-01 00:52:43 +01:00
|
|
|
* @param scope the scope the free type belongs to.
|
2022-06-03 23:15:45 +01:00
|
|
|
*/
|
2022-07-29 05:24:07 +01:00
|
|
|
TypeId freshType(const ScopePtr& scope);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fabricates a new free type pack belonging to a given scope.
|
2022-07-01 00:52:43 +01:00
|
|
|
* @param scope the scope the free type pack belongs to.
|
2022-06-03 23:15:45 +01:00
|
|
|
*/
|
2022-07-29 05:24:07 +01:00
|
|
|
TypePackId freshTypePack(const ScopePtr& scope);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2024-02-02 21:32:42 +00:00
|
|
|
/**
|
|
|
|
* Allocate a new TypePack with the given head and tail.
|
|
|
|
*
|
|
|
|
* Avoids allocating 0-length type packs:
|
|
|
|
*
|
|
|
|
* If the head is non-empty, allocate and return a type pack with the given
|
|
|
|
* head and tail.
|
|
|
|
* If the head is empty and tail is non-empty, return *tail.
|
|
|
|
* If both the head and tail are empty, return an empty type pack.
|
|
|
|
*/
|
|
|
|
TypePackId addTypePack(std::vector<TypeId> head, std::optional<TypePackId> tail);
|
|
|
|
|
2022-06-03 23:15:45 +01:00
|
|
|
/**
|
|
|
|
* Fabricates a scope that is a child of another scope.
|
2022-08-18 22:32:08 +01:00
|
|
|
* @param node the lexical node that the scope belongs to.
|
2022-06-03 23:15:45 +01:00
|
|
|
* @param parent the parent scope of the new scope. Must not be null.
|
|
|
|
*/
|
2022-08-18 22:32:08 +01:00
|
|
|
ScopePtr childScope(AstNode* node, const ScopePtr& parent);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2024-04-25 23:26:09 +01:00
|
|
|
std::optional<TypeId> lookup(const ScopePtr& scope, Location location, DefId def, bool prototype = true);
|
2023-11-10 21:10:07 +00:00
|
|
|
|
2022-06-03 23:15:45 +01:00
|
|
|
/**
|
|
|
|
* Adds a new constraint with no dependencies to a given scope.
|
2022-07-01 00:52:43 +01:00
|
|
|
* @param scope the scope to add the constraint to.
|
2022-06-03 23:15:45 +01:00
|
|
|
* @param cv the constraint variant to add.
|
2022-10-21 18:54:01 +01:00
|
|
|
* @return the pointer to the inserted constraint
|
2022-06-03 23:15:45 +01:00
|
|
|
*/
|
2022-10-21 18:54:01 +01:00
|
|
|
NotNull<Constraint> addConstraint(const ScopePtr& scope, const Location& location, ConstraintV cv);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a constraint to a given scope.
|
|
|
|
* @param scope the scope to add the constraint to. Must not be null.
|
|
|
|
* @param c the constraint to add.
|
2022-10-21 18:54:01 +01:00
|
|
|
* @return the pointer to the inserted constraint
|
2022-06-03 23:15:45 +01:00
|
|
|
*/
|
2022-10-21 18:54:01 +01:00
|
|
|
NotNull<Constraint> addConstraint(const ScopePtr& scope, std::unique_ptr<Constraint> c);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2023-10-21 02:10:30 +01:00
|
|
|
struct RefinementPartition
|
|
|
|
{
|
|
|
|
// Types that we want to intersect against the type of the expression.
|
|
|
|
std::vector<TypeId> discriminantTypes;
|
|
|
|
|
|
|
|
// Sometimes the type we're discriminating against is implicitly nil.
|
|
|
|
bool shouldAppendNilType = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
using RefinementContext = InsertionOrderedMap<DefId, RefinementPartition>;
|
2024-02-16 02:04:39 +00:00
|
|
|
void unionRefinements(const ScopePtr& scope, Location location, const RefinementContext& lhs, const RefinementContext& rhs,
|
|
|
|
RefinementContext& dest, std::vector<ConstraintV>* constraints);
|
|
|
|
void computeRefinement(const ScopePtr& scope, Location location, RefinementId refinement, RefinementContext* refis, bool sense, bool eq,
|
|
|
|
std::vector<ConstraintV>* constraints);
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
void applyRefinements(const ScopePtr& scope, Location location, RefinementId refinement);
|
2022-11-04 17:33:22 +00:00
|
|
|
|
2023-03-17 19:20:37 +00:00
|
|
|
ControlFlow visitBlockWithoutChildScope(const ScopePtr& scope, AstStatBlock* block);
|
|
|
|
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStat* stat);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatBlock* block);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatLocal* local);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatFor* for_);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatForIn* forIn);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatWhile* while_);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatRepeat* repeat);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatLocalFunction* function);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatFunction* function);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatReturn* ret);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatAssign* assign);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatCompoundAssign* assign);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatIf* ifStatement);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatTypeAlias* alias);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatDeclareGlobal* declareGlobal);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatDeclareClass* declareClass);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatDeclareFunction* declareFunction);
|
|
|
|
ControlFlow visit(const ScopePtr& scope, AstStatError* error);
|
2022-06-17 02:05:14 +01:00
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
InferencePack checkPack(const ScopePtr& scope, AstArray<AstExpr*> exprs, const std::vector<std::optional<TypeId>>& expectedTypes = {});
|
2023-09-30 02:13:05 +01:00
|
|
|
InferencePack checkPack(
|
|
|
|
const ScopePtr& scope, AstExpr* expr, const std::vector<std::optional<TypeId>>& expectedTypes = {}, bool generalize = true);
|
2022-10-28 11:37:29 +01:00
|
|
|
|
2023-02-17 23:41:51 +00:00
|
|
|
InferencePack checkPack(const ScopePtr& scope, AstExprCall* call);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2022-06-17 02:05:14 +01:00
|
|
|
/**
|
|
|
|
* Checks an expression that is expected to evaluate to one type.
|
|
|
|
* @param scope the scope the expression is contained within.
|
|
|
|
* @param expr the expression to check.
|
2022-09-23 20:17:25 +01:00
|
|
|
* @param expectedType the type of the expression that is expected from its
|
|
|
|
* surrounding context. Used to implement bidirectional type checking.
|
2023-09-30 02:13:05 +01:00
|
|
|
* @param generalize If true, generalize any lambdas that are encountered.
|
2022-06-17 02:05:14 +01:00
|
|
|
* @return the type of the expression.
|
|
|
|
*/
|
2023-10-06 20:02:32 +01:00
|
|
|
Inference check(
|
|
|
|
const ScopePtr& scope, AstExpr* expr, std::optional<TypeId> expectedType = {}, bool forceSingleton = false, bool generalize = true);
|
2022-10-28 11:37:29 +01:00
|
|
|
|
2022-11-04 17:33:22 +00:00
|
|
|
Inference check(const ScopePtr& scope, AstExprConstantString* string, std::optional<TypeId> expectedType, bool forceSingleton);
|
|
|
|
Inference check(const ScopePtr& scope, AstExprConstantBool* bool_, std::optional<TypeId> expectedType, bool forceSingleton);
|
2023-10-06 20:02:32 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprLocal* local);
|
2022-10-28 11:37:29 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprGlobal* global);
|
2024-04-25 23:26:09 +01:00
|
|
|
Inference checkIndexName(const ScopePtr& scope, const RefinementKey* key, AstExpr* indexee, const std::string& index, Location indexLocation);
|
2022-10-28 11:37:29 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprIndexName* indexName);
|
|
|
|
Inference check(const ScopePtr& scope, AstExprIndexExpr* indexExpr);
|
2023-09-30 02:13:05 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprFunction* func, std::optional<TypeId> expectedType, bool generalize);
|
2022-10-28 11:37:29 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprUnary* unary);
|
|
|
|
Inference check(const ScopePtr& scope, AstExprBinary* binary, std::optional<TypeId> expectedType);
|
|
|
|
Inference check(const ScopePtr& scope, AstExprIfElse* ifElse, std::optional<TypeId> expectedType);
|
|
|
|
Inference check(const ScopePtr& scope, AstExprTypeAssertion* typeAssert);
|
2023-01-27 22:28:31 +00:00
|
|
|
Inference check(const ScopePtr& scope, AstExprInterpString* interpString);
|
2022-10-28 11:37:29 +01:00
|
|
|
Inference check(const ScopePtr& scope, AstExprTable* expr, std::optional<TypeId> expectedType);
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-03 19:26:13 +00:00
|
|
|
std::tuple<TypeId, TypeId, RefinementId> checkBinary(const ScopePtr& scope, AstExprBinary* binary, std::optional<TypeId> expectedType);
|
2022-10-21 18:54:01 +01:00
|
|
|
|
2024-05-31 20:18:18 +01:00
|
|
|
void visitLValue(const ScopePtr& scope, AstExpr* expr, TypeId rhsType);
|
|
|
|
void visitLValue(const ScopePtr& scope, AstExprLocal* local, TypeId rhsType);
|
|
|
|
void visitLValue(const ScopePtr& scope, AstExprGlobal* global, TypeId rhsType);
|
|
|
|
void visitLValue(const ScopePtr& scope, AstExprIndexName* indexName, TypeId rhsType);
|
|
|
|
void visitLValue(const ScopePtr& scope, AstExprIndexExpr* indexExpr, TypeId rhsType);
|
2022-07-01 00:52:43 +01:00
|
|
|
|
|
|
|
struct FunctionSignature
|
|
|
|
{
|
|
|
|
// The type of the function.
|
|
|
|
TypeId signature;
|
|
|
|
// The scope that encompasses the function's signature. May be nullptr
|
|
|
|
// if there was no need for a signature scope (the function has no
|
|
|
|
// generics).
|
2022-07-29 05:24:07 +01:00
|
|
|
ScopePtr signatureScope;
|
2022-07-01 00:52:43 +01:00
|
|
|
// The scope that encompasses the function's body. Is a child scope of
|
|
|
|
// signatureScope, if present.
|
2022-07-29 05:24:07 +01:00
|
|
|
ScopePtr bodyScope;
|
2022-07-01 00:52:43 +01:00
|
|
|
};
|
|
|
|
|
2023-05-19 20:37:30 +01:00
|
|
|
FunctionSignature checkFunctionSignature(
|
|
|
|
const ScopePtr& parent, AstExprFunction* fn, std::optional<TypeId> expectedType = {}, std::optional<Location> originalName = {});
|
2022-06-17 02:05:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the body of a function expression.
|
|
|
|
* @param scope the interior scope of the body of the function.
|
|
|
|
* @param fn the function expression to check.
|
|
|
|
*/
|
2022-07-29 05:24:07 +01:00
|
|
|
void checkFunctionBody(const ScopePtr& scope, AstExprFunction* fn);
|
2022-06-24 02:56:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a type from its AST annotation.
|
|
|
|
* @param scope the scope that the type annotation appears within.
|
|
|
|
* @param ty the AST annotation to resolve.
|
2023-01-20 20:27:03 +00:00
|
|
|
* @param inTypeArguments whether we are resolving a type that's contained within type arguments, `<...>`.
|
2022-06-24 02:56:00 +01:00
|
|
|
* @return the type of the AST annotation.
|
|
|
|
**/
|
2023-03-03 20:21:14 +00:00
|
|
|
TypeId resolveType(const ScopePtr& scope, AstType* ty, bool inTypeArguments, bool replaceErrorWithFresh = false);
|
2022-06-24 02:56:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a type pack from its AST annotation.
|
|
|
|
* @param scope the scope that the type annotation appears within.
|
|
|
|
* @param tp the AST annotation to resolve.
|
2023-01-20 20:27:03 +00:00
|
|
|
* @param inTypeArguments whether we are resolving a type that's contained within type arguments, `<...>`.
|
2022-06-24 02:56:00 +01:00
|
|
|
* @return the type pack of the AST annotation.
|
|
|
|
**/
|
2023-03-03 20:21:14 +00:00
|
|
|
TypePackId resolveTypePack(const ScopePtr& scope, AstTypePack* tp, bool inTypeArguments, bool replaceErrorWithFresh = false);
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2023-01-20 20:27:03 +00:00
|
|
|
/**
|
|
|
|
* Resolves a type pack from its AST annotation.
|
|
|
|
* @param scope the scope that the type annotation appears within.
|
|
|
|
* @param list the AST annotation to resolve.
|
|
|
|
* @param inTypeArguments whether we are resolving a type that's contained within type arguments, `<...>`.
|
|
|
|
* @return the type pack of the AST annotation.
|
|
|
|
**/
|
2023-03-03 20:21:14 +00:00
|
|
|
TypePackId resolveTypePack(const ScopePtr& scope, const AstTypeList& list, bool inTypeArguments, bool replaceErrorWithFresh = false);
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2023-02-24 21:49:38 +00:00
|
|
|
/**
|
|
|
|
* Creates generic types given a list of AST definitions, resolving default
|
|
|
|
* types as required.
|
|
|
|
* @param scope the scope that the generics should belong to.
|
|
|
|
* @param generics the AST generics to create types for.
|
|
|
|
* @param useCache whether to use the generic type cache for the given
|
|
|
|
* scope.
|
|
|
|
* @param addTypes whether to add the types to the scope's
|
|
|
|
* privateTypeBindings map.
|
|
|
|
**/
|
2023-02-17 23:41:51 +00:00
|
|
|
std::vector<std::pair<Name, GenericTypeDefinition>> createGenerics(
|
2023-02-24 21:49:38 +00:00
|
|
|
const ScopePtr& scope, AstArray<AstGenericType> generics, bool useCache = false, bool addTypes = true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates generic type packs given a list of AST definitions, resolving
|
|
|
|
* default type packs as required.
|
|
|
|
* @param scope the scope that the generic packs should belong to.
|
|
|
|
* @param generics the AST generics to create type packs for.
|
|
|
|
* @param useCache whether to use the generic type pack cache for the given
|
|
|
|
* scope.
|
|
|
|
* @param addTypes whether to add the types to the scope's
|
|
|
|
* privateTypePackBindings map.
|
|
|
|
**/
|
2023-02-17 23:41:51 +00:00
|
|
|
std::vector<std::pair<Name, GenericTypePackDefinition>> createGenericPacks(
|
2023-02-24 21:49:38 +00:00
|
|
|
const ScopePtr& scope, AstArray<AstGenericTypePack> packs, bool useCache = false, bool addTypes = true);
|
2022-06-24 02:56:00 +01:00
|
|
|
|
2022-10-28 11:37:29 +01:00
|
|
|
Inference flattenPack(const ScopePtr& scope, Location location, InferencePack pack);
|
2022-07-01 00:52:43 +01:00
|
|
|
|
|
|
|
void reportError(Location location, TypeErrorData err);
|
|
|
|
void reportCodeTooComplex(Location location);
|
|
|
|
|
2024-07-12 18:03:36 +01:00
|
|
|
// make a union type function of these two types
|
2024-01-27 03:20:56 +00:00
|
|
|
TypeId makeUnion(const ScopePtr& scope, Location location, TypeId lhs, TypeId rhs);
|
2024-07-12 18:03:36 +01:00
|
|
|
// make an intersect type function of these two types
|
2024-01-27 03:20:56 +00:00
|
|
|
TypeId makeIntersect(const ScopePtr& scope, Location location, TypeId lhs, TypeId rhs);
|
|
|
|
|
2022-07-01 00:52:43 +01:00
|
|
|
/** Scan the program for global definitions.
|
|
|
|
*
|
2023-11-03 23:45:04 +00:00
|
|
|
* ConstraintGenerator needs to differentiate between globals and accesses to undefined symbols. Doing this "for
|
2022-07-01 00:52:43 +01:00
|
|
|
* real" in a general way is going to be pretty hard, so we are choosing not to tackle that yet. For now, we do an
|
|
|
|
* initial scan of the AST and note what globals are defined.
|
|
|
|
*/
|
2022-07-29 05:24:07 +01:00
|
|
|
void prepopulateGlobalScope(const ScopePtr& globalScope, AstStatBlock* program);
|
2023-03-03 20:21:14 +00:00
|
|
|
|
2024-06-07 18:51:12 +01:00
|
|
|
bool recordPropertyAssignment(TypeId ty);
|
|
|
|
|
2023-11-17 18:46:18 +00:00
|
|
|
// Record the fact that a particular local has a particular type in at least
|
|
|
|
// one of its states.
|
|
|
|
void recordInferredBinding(AstLocal* local, TypeId ty);
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
void fillInInferredBindings(const ScopePtr& globalScope, AstStatBlock* block);
|
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
/** Given a function type annotation, return a vector describing the expected types of the calls to the function
|
|
|
|
* For example, calling a function with annotation ((number) -> string & ((string) -> number))
|
|
|
|
* yields a vector of size 1, with value: [number | string]
|
|
|
|
*/
|
|
|
|
std::vector<std::optional<TypeId>> getExpectedCallTypesForFunctionOverloads(const TypeId fnType);
|
2024-03-30 23:14:44 +00:00
|
|
|
|
2024-07-12 18:03:36 +01:00
|
|
|
TypeId createTypeFunctionInstance(
|
|
|
|
const TypeFunction& family, std::vector<TypeId> typeArguments, std::vector<TypePackId> packArguments, const ScopePtr& scope, Location location);
|
2022-06-03 23:15:45 +01:00
|
|
|
};
|
|
|
|
|
2022-11-10 22:53:13 +00:00
|
|
|
/** Borrow a vector of pointers from a vector of owning pointers to constraints.
|
2022-06-17 02:05:14 +01:00
|
|
|
*/
|
2022-11-10 22:53:13 +00:00
|
|
|
std::vector<NotNull<Constraint>> borrowConstraints(const std::vector<ConstraintPtr>& constraints);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2023-03-03 20:21:14 +00:00
|
|
|
|
2022-06-03 23:15:45 +01:00
|
|
|
} // namespace Luau
|