luau/Analysis/include/Luau/OverloadResolution.h

128 lines
3.7 KiB
C
Raw Normal View History

2024-02-02 18:20:03 +00: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"
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
#include "Luau/EqSatSimplification.h"
#include "Luau/Error.h"
2024-02-02 18:20:03 +00:00
#include "Luau/InsertionOrderedMap.h"
#include "Luau/Location.h"
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
#include "Luau/NotNull.h"
2024-02-02 18:20:03 +00:00
#include "Luau/Subtyping.h"
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
#include "Luau/TypeFwd.h"
2024-02-02 18:20:03 +00:00
namespace Luau
{
struct BuiltinTypes;
struct TypeArena;
struct Scope;
struct InternalErrorReporter;
struct TypeCheckLimits;
struct Subtyping;
class Normalizer;
struct OverloadResolver
{
enum Analysis
{
Ok,
TypeIsNotAFunction,
ArityMismatch,
OverloadIsNonviable, // Arguments were incompatible with the overloads parameters but were otherwise compatible by arity
};
2024-08-02 00:25:12 +01:00
OverloadResolver(
NotNull<BuiltinTypes> builtinTypes,
NotNull<TypeArena> arena,
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
NotNull<Simplifier> simplifier,
2024-08-02 00:25:12 +01:00
NotNull<Normalizer> normalizer,
2024-09-27 18:11:46 +01:00
NotNull<TypeFunctionRuntime> typeFunctionRuntime,
2024-08-02 00:25:12 +01:00
NotNull<Scope> scope,
NotNull<InternalErrorReporter> reporter,
NotNull<TypeCheckLimits> limits,
Location callLocation
);
2024-02-02 18:20:03 +00:00
NotNull<BuiltinTypes> builtinTypes;
NotNull<TypeArena> arena;
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
NotNull<Simplifier> simplifier;
2024-02-02 18:20:03 +00:00
NotNull<Normalizer> normalizer;
2024-09-27 18:11:46 +01:00
NotNull<TypeFunctionRuntime> typeFunctionRuntime;
2024-02-02 18:20:03 +00:00
NotNull<Scope> scope;
NotNull<InternalErrorReporter> ice;
NotNull<TypeCheckLimits> limits;
Subtyping subtyping;
Location callLoc;
// Resolver results
std::vector<TypeId> ok;
std::vector<TypeId> nonFunctions;
std::vector<std::pair<TypeId, ErrorVec>> arityMismatches;
std::vector<std::pair<TypeId, ErrorVec>> nonviableOverloads;
InsertionOrderedMap<TypeId, std::pair<OverloadResolver::Analysis, size_t>> resolution;
std::pair<OverloadResolver::Analysis, TypeId> selectOverload(TypeId ty, TypePackId args);
void resolve(TypeId fnTy, const TypePack* args, AstExpr* selfExpr, const std::vector<AstExpr*>* argExprs);
private:
std::optional<ErrorVec> testIsSubtype(const Location& location, TypeId subTy, TypeId superTy);
std::optional<ErrorVec> testIsSubtype(const Location& location, TypePackId subTy, TypePackId superTy);
std::pair<Analysis, ErrorVec> checkOverload(
2024-08-02 00:25:12 +01:00
TypeId fnTy,
const TypePack* args,
AstExpr* fnLoc,
const std::vector<AstExpr*>* argExprs,
bool callMetamethodOk = true
);
2024-02-02 18:20:03 +00:00
static bool isLiteral(AstExpr* expr);
LUAU_NOINLINE
std::pair<Analysis, ErrorVec> checkOverload_(
2024-08-02 00:25:12 +01:00
TypeId fnTy,
const FunctionType* fn,
const TypePack* args,
AstExpr* fnExpr,
const std::vector<AstExpr*>* argExprs
);
2024-02-02 18:20:03 +00:00
size_t indexof(Analysis analysis);
void add(Analysis analysis, TypeId ty, ErrorVec&& errors);
};
2024-03-08 23:57:12 +00:00
struct SolveResult
{
2024-04-19 22:04:30 +01:00
enum OverloadCallResult
{
2024-03-08 23:57:12 +00:00
Ok,
CodeTooComplex,
OccursCheckFailed,
NoMatchingOverload,
};
OverloadCallResult result;
std::optional<TypePackId> typePackId; // nullopt if result != Ok
TypeId overloadToUse = nullptr;
TypeId inferredTy = nullptr;
DenseHashMap<TypeId, std::vector<TypeId>> expandedFreeTypes{nullptr};
};
2024-07-19 18:21:40 +01:00
// Helper utility, presently used for binary operator type functions.
2024-03-08 23:57:12 +00:00
//
// Given a function and a set of arguments, select a suitable overload.
2024-08-02 00:25:12 +01:00
SolveResult solveFunctionCall(
NotNull<TypeArena> arena,
NotNull<BuiltinTypes> builtinTypes,
Sync to upstream/release/655 * General - Fix the benchmark require wrapper function to work in Lua - Fix memory leak in the new Luau C API test * New Solver - Luau: type functions should be able to signal whether or not irreducibility is due to an error - Do not generate extra expansion constraint for uninvoked user-defined type functions - Print in a user-defined type function should be reported as an error instead of logging to stdout - Many e-graphs bugfixes and performance improvements - Many general bugfixes and improvements to the new solver as a whole - Fixed issue with Luau used-defined type functions not having all environments initialized - Infer types of globals under new type solver * Fragment Autocomplete - Miscellaneous fixes to make interop with the old solver better * Runtime - Support disabling specific Luau built-in functions from being fast-called or constant-evaluated - Added constant folding for vector arithmetic - Added constant propagation and type inference for Vector3 globals ---------------------------------------------------------- 9 contributors: Co-authored-by: Aaron Weiss <aaronweiss@roblox.com> Co-authored-by: Andy Friesen <afriesen@roblox.com> Co-authored-by: Aviral Goel <agoel@roblox.com> Co-authored-by: Daniel Angel <danielangel@roblox.com> Co-authored-by: Jonathan Kelaty <jkelaty@roblox.com> Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com> Co-authored-by: Varun Saini <vsaini@roblox.com> Co-authored-by: Vighnesh Vijay <vvijay@roblox.com> Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2024-12-13 18:57:30 +00:00
NotNull<Simplifier> simplifier,
2024-08-02 00:25:12 +01:00
NotNull<Normalizer> normalizer,
2024-09-27 18:11:46 +01:00
NotNull<TypeFunctionRuntime> typeFunctionRuntime,
2024-08-02 00:25:12 +01:00
NotNull<InternalErrorReporter> iceReporter,
NotNull<TypeCheckLimits> limits,
NotNull<Scope> scope,
const Location& location,
TypeId fn,
TypePackId argsPack
);
2024-03-08 23:57:12 +00:00
2024-02-02 18:20:03 +00:00
} // namespace Luau