Check __call for class type vars

This commit is contained in:
JohnnyMorganz 2022-11-27 16:32:16 +00:00
parent 9f025a0d5f
commit 1e4a993f20

View file

@ -4135,11 +4135,17 @@ std::optional<WithPredicate<TypePackId>> TypeChecker::checkCallOverload(const Sc
std::vector<Location> metaArgLocations;
// Might be a callable table
// Might be a callable table or class
std::optional<TypeId> callTy = std::nullopt;
if (const MetatableTypeVar* mttv = get<MetatableTypeVar>(fn))
{
if (std::optional<TypeId> ty = getIndexTypeFromType(scope, mttv->metatable, "__call", expr.func->location, /* addErrors= */ false))
callTy = getIndexTypeFromType(scope, mttv->metatable, "__call", expr.func->location, /* addErrors= */ false);
} else if (const ClassTypeVar* ctv = get<ClassTypeVar>(fn); ctv && ctv->metatable)
{
callTy = getIndexTypeFromType(scope, *ctv->metatable, "__call", expr.func->location, /* addErrors= */ false);
}
if (callTy) {
// Construct arguments with 'self' added in front
TypePackId metaCallArgPack = addTypePack(TypePackVar(TypePack{args->head, args->tail}));
@ -4149,13 +4155,12 @@ std::optional<WithPredicate<TypePackId>> TypeChecker::checkCallOverload(const Sc
metaArgLocations = *argLocations;
metaArgLocations.insert(metaArgLocations.begin(), expr.func->location);
fn = instantiate(scope, *ty, expr.func->location);
fn = instantiate(scope, *callTy, expr.func->location);
argPack = metaCallArgPack;
args = metaCallArgs;
argLocations = &metaArgLocations;
}
}
const FunctionTypeVar* ftv = get<FunctionTypeVar>(fn);
if (!ftv)