mirror of
https://github.com/luau-lang/luau.git
synced 2025-04-17 02:13:55 +01:00
This doesn't contain all changes for 507 yet but we might want to do the Luau 0.507 release a bit earlier to end the year sooner. Changes: - Type ascription (::) now permits casts between related types in both directions, allowing to refine or loosen the type (RFC #56) - Fix type definition for tonumber to return number? since the input string isn't guaranteed to contain a valid number - Fix type refinements for field access via [] - Many stability fixes for type checker - Provide extra information in error messages for type mismatches in more cases - Improve performance of type checking for large unions when union members are string literals - Add coverage reporting support to Repl (--coverage command line argument) and lua_getcoverage C API - Work around code signing issues during Makefile builds on macOS - Improve performance of truthiness checks in some cases, particularly on Apple M1, resulting in 10-25% perf gains on qsort benchmark depending on the CPU/compiler - Fix support for little-endian systems; IBM s390x here we go!
79 lines
2.4 KiB
C++
79 lines
2.4 KiB
C++
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
#include "Luau/TypeUtils.h"
|
|
|
|
#include "Luau/Scope.h"
|
|
#include "Luau/ToString.h"
|
|
#include "Luau/TypeInfer.h"
|
|
|
|
namespace Luau
|
|
{
|
|
|
|
std::optional<TypeId> findMetatableEntry(ErrorVec& errors, const ScopePtr& globalScope, TypeId type, std::string entry, Location location)
|
|
{
|
|
type = follow(type);
|
|
|
|
std::optional<TypeId> metatable = getMetatable(type);
|
|
if (!metatable)
|
|
return std::nullopt;
|
|
|
|
TypeId unwrapped = follow(*metatable);
|
|
|
|
if (get<AnyTypeVar>(unwrapped))
|
|
return getSingletonTypes().anyType;
|
|
|
|
const TableTypeVar* mtt = getTableType(unwrapped);
|
|
if (!mtt)
|
|
{
|
|
errors.push_back(TypeError{location, GenericError{"Metatable was not a table."}});
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto it = mtt->props.find(entry);
|
|
if (it != mtt->props.end())
|
|
return it->second.type;
|
|
else
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<TypeId> findTablePropertyRespectingMeta(ErrorVec& errors, const ScopePtr& globalScope, TypeId ty, Name name, Location location)
|
|
{
|
|
if (get<AnyTypeVar>(ty))
|
|
return ty;
|
|
|
|
if (const TableTypeVar* tableType = getTableType(ty))
|
|
{
|
|
const auto& it = tableType->props.find(name);
|
|
if (it != tableType->props.end())
|
|
return it->second.type;
|
|
}
|
|
|
|
std::optional<TypeId> mtIndex = findMetatableEntry(errors, globalScope, ty, "__index", location);
|
|
while (mtIndex)
|
|
{
|
|
TypeId index = follow(*mtIndex);
|
|
if (const auto& itt = getTableType(index))
|
|
{
|
|
const auto& fit = itt->props.find(name);
|
|
if (fit != itt->props.end())
|
|
return fit->second.type;
|
|
}
|
|
else if (const auto& itf = get<FunctionTypeVar>(index))
|
|
{
|
|
std::optional<TypeId> r = first(follow(itf->retType));
|
|
if (!r)
|
|
return getSingletonTypes().nilType;
|
|
else
|
|
return *r;
|
|
}
|
|
else if (get<AnyTypeVar>(index))
|
|
return getSingletonTypes().anyType;
|
|
else
|
|
errors.push_back(TypeError{location, GenericError{"__index should either be a function or table. Got " + toString(index)}});
|
|
|
|
mtIndex = findMetatableEntry(errors, globalScope, *mtIndex, "__index", location);
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
} // namespace Luau
|