2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
#include "Luau/TypeChecker2.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "Luau/Ast.h"
|
|
|
|
#include "Luau/AstQuery.h"
|
|
|
|
#include "Luau/Clone.h"
|
2022-07-01 00:29:02 +01:00
|
|
|
#include "Luau/Instantiation.h"
|
2022-06-17 01:54:42 +01:00
|
|
|
#include "Luau/Normalize.h"
|
2022-07-01 00:29:02 +01:00
|
|
|
#include "Luau/TxnLog.h"
|
|
|
|
#include "Luau/TypeUtils.h"
|
2022-06-24 02:44:07 +01:00
|
|
|
#include "Luau/Unifier.h"
|
|
|
|
#include "Luau/ToString.h"
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct TypeChecker2 : public AstVisitor
|
|
|
|
{
|
|
|
|
const SourceModule* sourceModule;
|
|
|
|
Module* module;
|
|
|
|
InternalErrorReporter ice; // FIXME accept a pointer from Frontend
|
2022-07-01 00:29:02 +01:00
|
|
|
SingletonTypes& singletonTypes;
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
TypeChecker2(const SourceModule* sourceModule, Module* module)
|
|
|
|
: sourceModule(sourceModule)
|
|
|
|
, module(module)
|
2022-07-01 00:29:02 +01:00
|
|
|
, singletonTypes(getSingletonTypes())
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
using AstVisitor::visit;
|
|
|
|
|
|
|
|
TypePackId lookupPack(AstExpr* expr)
|
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
// If a type isn't in the type graph, it probably means that a recursion limit was exceeded.
|
|
|
|
// We'll just return anyType in these cases. Typechecking against any is very fast and this
|
|
|
|
// allows us not to think about this very much in the actual typechecking logic.
|
2022-06-17 01:54:42 +01:00
|
|
|
TypePackId* tp = module->astTypePacks.find(expr);
|
2022-07-01 00:29:02 +01:00
|
|
|
if (tp)
|
|
|
|
return follow(*tp);
|
|
|
|
else
|
|
|
|
return singletonTypes.anyTypePack;
|
2022-06-17 01:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TypeId lookupType(AstExpr* expr)
|
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
// If a type isn't in the type graph, it probably means that a recursion limit was exceeded.
|
|
|
|
// We'll just return anyType in these cases. Typechecking against any is very fast and this
|
|
|
|
// allows us not to think about this very much in the actual typechecking logic.
|
2022-06-17 01:54:42 +01:00
|
|
|
TypeId* ty = module->astTypes.find(expr);
|
2022-07-01 00:29:02 +01:00
|
|
|
if (ty)
|
|
|
|
return follow(*ty);
|
|
|
|
|
|
|
|
TypePackId* tp = module->astTypePacks.find(expr);
|
|
|
|
if (tp)
|
|
|
|
return flattenPack(*tp);
|
|
|
|
|
|
|
|
return singletonTypes.anyType;
|
2022-06-17 01:54:42 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
TypeId lookupAnnotation(AstType* annotation)
|
|
|
|
{
|
|
|
|
TypeId* ty = module->astResolvedTypes.find(annotation);
|
|
|
|
LUAU_ASSERT(ty);
|
|
|
|
return follow(*ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypePackId reconstructPack(AstArray<AstExpr*> exprs, TypeArena& arena)
|
|
|
|
{
|
|
|
|
std::vector<TypeId> head;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < exprs.size - 1; ++i)
|
|
|
|
{
|
|
|
|
head.push_back(lookupType(exprs.data[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
TypePackId tail = lookupPack(exprs.data[exprs.size - 1]);
|
|
|
|
return arena.addTypePack(TypePack{head, tail});
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope2* findInnermostScope(Location location)
|
|
|
|
{
|
|
|
|
Scope2* bestScope = module->getModuleScope2();
|
|
|
|
Location bestLocation = module->scope2s[0].first;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < module->scope2s.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& [scopeBounds, scope] = module->scope2s[i];
|
|
|
|
if (scopeBounds.encloses(location))
|
|
|
|
{
|
|
|
|
if (scopeBounds.begin > bestLocation.begin || scopeBounds.end < bestLocation.end)
|
|
|
|
{
|
|
|
|
bestScope = scope.get();
|
|
|
|
bestLocation = scopeBounds;
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 00:29:02 +01:00
|
|
|
else if (scopeBounds.begin > location.end)
|
2022-06-24 02:44:07 +01:00
|
|
|
{
|
|
|
|
// TODO: Is this sound? This relies on the fact that scopes are inserted
|
|
|
|
// into the scope list in the order that they appear in the AST.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(AstStatLocal* local) override
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < local->values.size; ++i)
|
|
|
|
{
|
|
|
|
AstExpr* value = local->values.data[i];
|
|
|
|
if (i == local->values.size - 1)
|
|
|
|
{
|
|
|
|
if (i < local->values.size)
|
|
|
|
{
|
|
|
|
TypePackId valueTypes = lookupPack(value);
|
|
|
|
auto it = begin(valueTypes);
|
|
|
|
for (size_t j = i; j < local->vars.size; ++j)
|
|
|
|
{
|
|
|
|
if (it == end(valueTypes))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
AstLocal* var = local->vars.data[i];
|
|
|
|
if (var->annotation)
|
|
|
|
{
|
|
|
|
TypeId varType = lookupAnnotation(var->annotation);
|
|
|
|
if (!isSubtype(*it, varType, ice))
|
|
|
|
{
|
|
|
|
reportError(TypeMismatch{varType, *it}, value->location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TypeId valueType = lookupType(value);
|
|
|
|
AstLocal* var = local->vars.data[i];
|
|
|
|
|
|
|
|
if (var->annotation)
|
|
|
|
{
|
|
|
|
TypeId varType = lookupAnnotation(var->annotation);
|
|
|
|
if (!isSubtype(varType, valueType, ice))
|
|
|
|
{
|
|
|
|
reportError(TypeMismatch{varType, valueType}, value->location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
bool visit(AstStatAssign* assign) override
|
|
|
|
{
|
|
|
|
size_t count = std::min(assign->vars.size, assign->values.size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
AstExpr* lhs = assign->vars.data[i];
|
2022-07-01 00:29:02 +01:00
|
|
|
TypeId lhsType = lookupType(lhs);
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
AstExpr* rhs = assign->values.data[i];
|
2022-07-01 00:29:02 +01:00
|
|
|
TypeId rhsType = lookupType(rhs);
|
2022-06-17 01:54:42 +01:00
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
if (!isSubtype(rhsType, lhsType, ice))
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
reportError(TypeMismatch{lhsType, rhsType}, rhs->location);
|
2022-06-17 01:54:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
bool visit(AstStatReturn* ret) override
|
|
|
|
{
|
|
|
|
Scope2* scope = findInnermostScope(ret->location);
|
|
|
|
TypePackId expectedRetType = scope->returnType;
|
|
|
|
|
|
|
|
TypeArena arena;
|
|
|
|
TypePackId actualRetType = reconstructPack(ret->list, arena);
|
|
|
|
|
|
|
|
UnifierSharedState sharedState{&ice};
|
|
|
|
Unifier u{&arena, Mode::Strict, ret->location, Covariant, sharedState};
|
|
|
|
u.anyIsTop = true;
|
|
|
|
|
|
|
|
u.tryUnify(actualRetType, expectedRetType);
|
|
|
|
const bool ok = u.errors.empty() && u.log.empty();
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
for (const TypeError& e : u.errors)
|
2022-07-01 00:29:02 +01:00
|
|
|
reportError(e);
|
2022-06-24 02:44:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
bool visit(AstExprCall* call) override
|
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
TypeArena arena;
|
|
|
|
Instantiation instantiation{TxnLog::empty(), &arena, TypeLevel{}};
|
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
TypePackId expectedRetType = lookupPack(call);
|
|
|
|
TypeId functionType = lookupType(call->func);
|
2022-07-01 00:29:02 +01:00
|
|
|
TypeId instantiatedFunctionType = instantiation.substitute(functionType).value_or(nullptr);
|
|
|
|
LUAU_ASSERT(functionType);
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
TypePack args;
|
|
|
|
for (const auto& arg : call->args)
|
|
|
|
{
|
|
|
|
TypeId argTy = module->astTypes[arg];
|
|
|
|
LUAU_ASSERT(argTy);
|
|
|
|
args.head.push_back(argTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypePackId argsTp = arena.addTypePack(args);
|
|
|
|
FunctionTypeVar ftv{argsTp, expectedRetType};
|
|
|
|
TypeId expectedType = arena.addType(ftv);
|
2022-07-01 00:29:02 +01:00
|
|
|
if (!isSubtype(expectedType, instantiatedFunctionType, ice))
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
|
|
|
unfreeze(module->interfaceTypes);
|
|
|
|
CloneState cloneState;
|
|
|
|
expectedType = clone(expectedType, module->interfaceTypes, cloneState);
|
|
|
|
freeze(module->interfaceTypes);
|
|
|
|
reportError(TypeMismatch{expectedType, functionType}, call->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
bool visit(AstExprFunction* fn) override
|
|
|
|
{
|
|
|
|
TypeId inferredFnTy = lookupType(fn);
|
|
|
|
const FunctionTypeVar* inferredFtv = get<FunctionTypeVar>(inferredFnTy);
|
|
|
|
LUAU_ASSERT(inferredFtv);
|
|
|
|
|
|
|
|
auto argIt = begin(inferredFtv->argTypes);
|
|
|
|
for (const auto& arg : fn->args)
|
|
|
|
{
|
|
|
|
if (argIt == end(inferredFtv->argTypes))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (arg->annotation)
|
|
|
|
{
|
|
|
|
TypeId inferredArgTy = *argIt;
|
|
|
|
TypeId annotatedArgTy = lookupAnnotation(arg->annotation);
|
|
|
|
|
|
|
|
if (!isSubtype(annotatedArgTy, inferredArgTy, ice))
|
|
|
|
{
|
|
|
|
reportError(TypeMismatch{annotatedArgTy, inferredArgTy}, arg->location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++argIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
bool visit(AstExprIndexName* indexName) override
|
|
|
|
{
|
|
|
|
TypeId leftType = lookupType(indexName->expr);
|
|
|
|
TypeId resultType = lookupType(indexName);
|
|
|
|
|
|
|
|
// leftType must have a property called indexName->index
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
std::optional<TypeId> t = findTablePropertyRespectingMeta(module->errors, leftType, indexName->index.value, indexName->location);
|
|
|
|
if (t)
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
if (!isSubtype(resultType, *t, ice))
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
2022-07-01 00:29:02 +01:00
|
|
|
reportError(TypeMismatch{resultType, *t}, indexName->location);
|
2022-06-17 01:54:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reportError(UnknownProperty{leftType, indexName->index.value}, indexName->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(AstExprConstantNumber* number) override
|
|
|
|
{
|
|
|
|
TypeId actualType = lookupType(number);
|
|
|
|
TypeId numberType = getSingletonTypes().numberType;
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
if (!isSubtype(numberType, actualType, ice))
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
|
|
|
reportError(TypeMismatch{actualType, numberType}, number->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(AstExprConstantString* string) override
|
|
|
|
{
|
|
|
|
TypeId actualType = lookupType(string);
|
|
|
|
TypeId stringType = getSingletonTypes().stringType;
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
if (!isSubtype(stringType, actualType, ice))
|
2022-06-17 01:54:42 +01:00
|
|
|
{
|
|
|
|
reportError(TypeMismatch{actualType, stringType}, string->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
/** Extract a TypeId for the first type of the provided pack.
|
|
|
|
*
|
|
|
|
* Note that this may require modifying some types. I hope this doesn't cause problems!
|
|
|
|
*/
|
|
|
|
TypeId flattenPack(TypePackId pack)
|
|
|
|
{
|
|
|
|
pack = follow(pack);
|
|
|
|
|
2022-07-14 23:39:35 +01:00
|
|
|
while (true)
|
2022-07-01 00:29:02 +01:00
|
|
|
{
|
2022-07-14 23:39:35 +01:00
|
|
|
auto tp = get<TypePack>(pack);
|
|
|
|
if (tp && tp->head.empty() && tp->tail)
|
2022-07-01 00:29:02 +01:00
|
|
|
pack = *tp->tail;
|
2022-07-14 23:39:35 +01:00
|
|
|
else
|
|
|
|
break;
|
2022-07-01 00:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto ty = first(pack))
|
|
|
|
return *ty;
|
|
|
|
else if (auto vtp = get<VariadicTypePack>(pack))
|
|
|
|
return vtp->ty;
|
|
|
|
else if (auto ftp = get<FreeTypePack>(pack))
|
|
|
|
{
|
|
|
|
TypeId result = module->internalTypes.addType(FreeTypeVar{ftp->scope});
|
|
|
|
TypePackId freeTail = module->internalTypes.addTypePack(FreeTypePack{ftp->scope});
|
|
|
|
|
|
|
|
TypePack& resultPack = asMutable(pack)->ty.emplace<TypePack>();
|
|
|
|
resultPack.head.assign(1, result);
|
|
|
|
resultPack.tail = freeTail;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (get<Unifiable::Error>(pack))
|
|
|
|
return singletonTypes.errorRecoveryType();
|
|
|
|
else
|
|
|
|
ice.ice("flattenPack got a weird pack!");
|
|
|
|
}
|
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
bool visit(AstType* ty) override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visit(AstTypeReference* ty) override
|
|
|
|
{
|
|
|
|
Scope2* scope = findInnermostScope(ty->location);
|
|
|
|
|
|
|
|
// TODO: Imported types
|
|
|
|
// TODO: Generic types
|
|
|
|
if (!scope->lookupTypeBinding(ty->name.value))
|
|
|
|
{
|
|
|
|
reportError(UnknownSymbol{ty->name.value, UnknownSymbol::Context::Type}, ty->location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
void reportError(TypeErrorData&& data, const Location& location)
|
|
|
|
{
|
|
|
|
module->errors.emplace_back(location, sourceModule->name, std::move(data));
|
|
|
|
}
|
2022-07-01 00:29:02 +01:00
|
|
|
|
|
|
|
void reportError(TypeError e)
|
|
|
|
{
|
|
|
|
module->errors.emplace_back(std::move(e));
|
|
|
|
}
|
2022-06-17 01:54:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void check(const SourceModule& sourceModule, Module* module)
|
|
|
|
{
|
|
|
|
TypeChecker2 typeChecker{&sourceModule, module};
|
|
|
|
|
|
|
|
sourceModule.root->visit(&typeChecker);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Luau
|