// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details #include "Luau/BuiltinDefinitions.h" #include "Luau/Frontend.h" #include "Luau/Symbol.h" #include "Luau/Common.h" #include "Luau/ToString.h" #include /** FIXME: Many of these type definitions are not quite completely accurate. * * Some of them require richer generics than we have. For instance, we do not yet have a way to talk * about a function that takes any number of values, but where each value must have some specific type. */ namespace Luau { static std::optional> magicFunctionSelect( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult); static std::optional> magicFunctionSetMetaTable( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult); static std::optional> magicFunctionAssert( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult); static std::optional> magicFunctionPack( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult); static std::optional> magicFunctionRequire( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult); TypeId makeUnion(TypeArena& arena, std::vector&& types) { return arena.addType(UnionTypeVar{std::move(types)}); } TypeId makeIntersection(TypeArena& arena, std::vector&& types) { return arena.addType(IntersectionTypeVar{std::move(types)}); } TypeId makeOption(TypeChecker& typeChecker, TypeArena& arena, TypeId t) { return makeUnion(arena, {typeChecker.nilType, t}); } TypeId makeFunction( TypeArena& arena, std::optional selfType, std::initializer_list paramTypes, std::initializer_list retTypes) { return makeFunction(arena, selfType, {}, {}, paramTypes, {}, retTypes); } TypeId makeFunction(TypeArena& arena, std::optional selfType, std::initializer_list generics, std::initializer_list genericPacks, std::initializer_list paramTypes, std::initializer_list retTypes) { return makeFunction(arena, selfType, generics, genericPacks, paramTypes, {}, retTypes); } TypeId makeFunction(TypeArena& arena, std::optional selfType, std::initializer_list paramTypes, std::initializer_list paramNames, std::initializer_list retTypes) { return makeFunction(arena, selfType, {}, {}, paramTypes, paramNames, retTypes); } TypeId makeFunction(TypeArena& arena, std::optional selfType, std::initializer_list generics, std::initializer_list genericPacks, std::initializer_list paramTypes, std::initializer_list paramNames, std::initializer_list retTypes) { std::vector params; if (selfType) params.push_back(*selfType); for (auto&& p : paramTypes) params.push_back(p); TypePackId paramPack = arena.addTypePack(std::move(params)); TypePackId retPack = arena.addTypePack(std::vector(retTypes)); FunctionTypeVar ftv{generics, genericPacks, paramPack, retPack, {}, selfType.has_value()}; if (selfType) ftv.argNames.push_back(Luau::FunctionArgument{"self", {}}); if (paramNames.size() != 0) { for (auto&& p : paramNames) ftv.argNames.push_back(Luau::FunctionArgument{std::move(p), {}}); } else if (selfType) { // If argument names were not provided, but we have already added a name for 'self' argument, we have to fill remaining slots as well for (size_t i = 0; i < paramTypes.size(); i++) ftv.argNames.push_back(std::nullopt); } return arena.addType(std::move(ftv)); } void attachMagicFunction(TypeId ty, MagicFunction fn) { if (auto ftv = getMutable(ty)) ftv->magicFunction = fn; else LUAU_ASSERT(!"Got a non functional type"); } Property makeProperty(TypeId ty, std::optional documentationSymbol) { return { /* type */ ty, /* deprecated */ false, /* deprecatedSuggestion */ {}, /* location */ std::nullopt, /* tags */ {}, documentationSymbol, }; } void addGlobalBinding(TypeChecker& typeChecker, const std::string& name, TypeId ty, const std::string& packageName) { addGlobalBinding(typeChecker, typeChecker.globalScope, name, ty, packageName); } void addGlobalBinding(TypeChecker& typeChecker, const std::string& name, Binding binding) { addGlobalBinding(typeChecker, typeChecker.globalScope, name, binding); } void addGlobalBinding(TypeChecker& typeChecker, const ScopePtr& scope, const std::string& name, TypeId ty, const std::string& packageName) { std::string documentationSymbol = packageName + "/global/" + name; addGlobalBinding(typeChecker, scope, name, Binding{ty, Location{}, {}, {}, documentationSymbol}); } void addGlobalBinding(TypeChecker& typeChecker, const ScopePtr& scope, const std::string& name, Binding binding) { scope->bindings[typeChecker.globalNames.names->getOrAdd(name.c_str())] = binding; } TypeId getGlobalBinding(TypeChecker& typeChecker, const std::string& name) { auto t = tryGetGlobalBinding(typeChecker, name); LUAU_ASSERT(t.has_value()); return t->typeId; } std::optional tryGetGlobalBinding(TypeChecker& typeChecker, const std::string& name) { AstName astName = typeChecker.globalNames.names->getOrAdd(name.c_str()); auto it = typeChecker.globalScope->bindings.find(astName); if (it != typeChecker.globalScope->bindings.end()) return it->second; return std::nullopt; } Binding* tryGetGlobalBindingRef(TypeChecker& typeChecker, const std::string& name) { AstName astName = typeChecker.globalNames.names->get(name.c_str()); if (astName == AstName()) return nullptr; auto it = typeChecker.globalScope->bindings.find(astName); if (it != typeChecker.globalScope->bindings.end()) return &it->second; return nullptr; } void assignPropDocumentationSymbols(TableTypeVar::Props& props, const std::string& baseName) { for (auto& [name, prop] : props) { prop.documentationSymbol = baseName + "." + name; } } void registerBuiltinTypes(TypeChecker& typeChecker) { LUAU_ASSERT(!typeChecker.globalTypes.typeVars.isFrozen()); LUAU_ASSERT(!typeChecker.globalTypes.typePacks.isFrozen()); TypeId numberType = typeChecker.numberType; TypeId booleanType = typeChecker.booleanType; TypeId nilType = typeChecker.nilType; TypeArena& arena = typeChecker.globalTypes; TypePackId oneNumberPack = arena.addTypePack({numberType}); TypePackId oneBooleanPack = arena.addTypePack({booleanType}); TypePackId numberVariadicList = arena.addTypePack(TypePackVar{VariadicTypePack{numberType}}); TypePackId listOfAtLeastOneNumber = arena.addTypePack(TypePack{{numberType}, numberVariadicList}); TypeId listOfAtLeastOneNumberToNumberType = arena.addType(FunctionTypeVar{ listOfAtLeastOneNumber, oneNumberPack, }); TypeId listOfAtLeastZeroNumbersToNumberType = arena.addType(FunctionTypeVar{numberVariadicList, oneNumberPack}); LoadDefinitionFileResult loadResult = Luau::loadDefinitionFile(typeChecker, typeChecker.globalScope, getBuiltinDefinitionSource(), "@luau"); LUAU_ASSERT(loadResult.success); TypeId mathLibType = getGlobalBinding(typeChecker, "math"); if (TableTypeVar* ttv = getMutable(mathLibType)) { ttv->props["min"] = makeProperty(listOfAtLeastOneNumberToNumberType, "@luau/global/math.min"); ttv->props["max"] = makeProperty(listOfAtLeastOneNumberToNumberType, "@luau/global/math.max"); } TypeId bit32LibType = getGlobalBinding(typeChecker, "bit32"); if (TableTypeVar* ttv = getMutable(bit32LibType)) { ttv->props["band"] = makeProperty(listOfAtLeastZeroNumbersToNumberType, "@luau/global/bit32.band"); ttv->props["bor"] = makeProperty(listOfAtLeastZeroNumbersToNumberType, "@luau/global/bit32.bor"); ttv->props["bxor"] = makeProperty(listOfAtLeastZeroNumbersToNumberType, "@luau/global/bit32.bxor"); ttv->props["btest"] = makeProperty(arena.addType(FunctionTypeVar{listOfAtLeastOneNumber, oneBooleanPack}), "@luau/global/bit32.btest"); } TypeId genericK = arena.addType(GenericTypeVar{"K"}); TypeId genericV = arena.addType(GenericTypeVar{"V"}); TypeId mapOfKtoV = arena.addType(TableTypeVar{{}, TableIndexer(genericK, genericV), typeChecker.globalScope->level, TableState::Generic}); std::optional stringMetatableTy = getMetatable(getSingletonTypes().stringType); LUAU_ASSERT(stringMetatableTy); const TableTypeVar* stringMetatableTable = get(follow(*stringMetatableTy)); LUAU_ASSERT(stringMetatableTable); auto it = stringMetatableTable->props.find("__index"); LUAU_ASSERT(it != stringMetatableTable->props.end()); addGlobalBinding(typeChecker, "string", it->second.type, "@luau"); // next(t: Table, i: K | nil) -> (K, V) TypePackId nextArgsTypePack = arena.addTypePack(TypePack{{mapOfKtoV, makeOption(typeChecker, arena, genericK)}}); addGlobalBinding(typeChecker, "next", arena.addType(FunctionTypeVar{{genericK, genericV}, {}, nextArgsTypePack, arena.addTypePack(TypePack{{genericK, genericV}})}), "@luau"); TypePackId pairsArgsTypePack = arena.addTypePack({mapOfKtoV}); TypeId pairsNext = arena.addType(FunctionTypeVar{nextArgsTypePack, arena.addTypePack(TypePack{{genericK, genericV}})}); TypePackId pairsReturnTypePack = arena.addTypePack(TypePack{{pairsNext, mapOfKtoV, nilType}}); // NOTE we are missing 'i: K | nil' argument in the first return types' argument. // pairs(t: Table) -> ((Table) -> (K, V), Table, nil) addGlobalBinding(typeChecker, "pairs", arena.addType(FunctionTypeVar{{genericK, genericV}, {}, pairsArgsTypePack, pairsReturnTypePack}), "@luau"); TypeId genericMT = arena.addType(GenericTypeVar{"MT"}); TableTypeVar tab{TableState::Generic, typeChecker.globalScope->level}; TypeId tabTy = arena.addType(tab); TypeId tableMetaMT = arena.addType(MetatableTypeVar{tabTy, genericMT}); addGlobalBinding(typeChecker, "getmetatable", makeFunction(arena, std::nullopt, {genericMT}, {}, {tableMetaMT}, {genericMT}), "@luau"); // setmetatable({ @metatable MT }, MT) -> { @metatable MT } // clang-format off addGlobalBinding(typeChecker, "setmetatable", arena.addType( FunctionTypeVar{ {genericMT}, {}, arena.addTypePack(TypePack{{tableMetaMT, genericMT}}), arena.addTypePack(TypePack{{tableMetaMT}}) } ), "@luau" ); // clang-format on for (const auto& pair : typeChecker.globalScope->bindings) { persist(pair.second.typeId); if (TableTypeVar* ttv = getMutable(pair.second.typeId)) { if (!ttv->name) ttv->name = toString(pair.first); } } attachMagicFunction(getGlobalBinding(typeChecker, "assert"), magicFunctionAssert); attachMagicFunction(getGlobalBinding(typeChecker, "setmetatable"), magicFunctionSetMetaTable); attachMagicFunction(getGlobalBinding(typeChecker, "select"), magicFunctionSelect); auto tableLib = getMutable(getGlobalBinding(typeChecker, "table")); attachMagicFunction(tableLib->props["pack"].type, magicFunctionPack); attachMagicFunction(getGlobalBinding(typeChecker, "require"), magicFunctionRequire); } static std::optional> magicFunctionSelect( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult) { auto [paramPack, _predicates] = exprResult; (void)scope; if (expr.args.size <= 0) { typechecker.reportError(TypeError{expr.location, GenericError{"select should take 1 or more arguments"}}); return std::nullopt; } AstExpr* arg1 = expr.args.data[0]; if (AstExprConstantNumber* num = arg1->as()) { const auto& [v, tail] = flatten(paramPack); int offset = int(num->value); if (offset > 0) { if (size_t(offset) < v.size()) { std::vector result(v.begin() + offset, v.end()); return ExprResult{typechecker.currentModule->internalTypes.addTypePack(TypePack{std::move(result), tail})}; } else if (tail) return ExprResult{*tail}; } typechecker.reportError(TypeError{arg1->location, GenericError{"bad argument #1 to select (index out of range)"}}); } else if (AstExprConstantString* str = arg1->as()) { if (str->value.size == 1 && str->value.data[0] == '#') return ExprResult{typechecker.currentModule->internalTypes.addTypePack({typechecker.numberType})}; } return std::nullopt; } static std::optional> magicFunctionSetMetaTable( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult) { auto [paramPack, _predicates] = exprResult; TypeArena& arena = typechecker.currentModule->internalTypes; std::vector expectedArgs = typechecker.unTypePack(scope, paramPack, 2, expr.location); TypeId target = follow(expectedArgs[0]); TypeId mt = follow(expectedArgs[1]); if (const auto& tab = get(target)) { if (target->persistent) { typechecker.reportError(TypeError{expr.location, CannotExtendTable{target, CannotExtendTable::Metatable}}); } else { typechecker.tablify(mt); const TableTypeVar* mtTtv = get(mt); MetatableTypeVar mtv{target, mt}; if ((tab->name || tab->syntheticName) && (mtTtv && (mtTtv->name || mtTtv->syntheticName))) { std::string tableName = tab->name ? *tab->name : *tab->syntheticName; std::string metatableName = mtTtv->name ? *mtTtv->name : *mtTtv->syntheticName; if (tableName == metatableName) mtv.syntheticName = tableName; else mtv.syntheticName = "{ @metatable: " + metatableName + ", " + tableName + " }"; } TypeId mtTy = arena.addType(mtv); AstExpr* targetExpr = expr.args.data[0]; if (AstExprLocal* targetLocal = targetExpr->as()) { const Name targetName(targetLocal->local->name.value); scope->bindings[targetLocal->local] = Binding{mtTy, expr.location}; } return ExprResult{arena.addTypePack({mtTy})}; } } else if (get(target) || get(target) || isTableIntersection(target)) { } else { typechecker.reportError(TypeError{expr.location, GenericError{"setmetatable should take a table"}}); } return ExprResult{arena.addTypePack({target})}; } static std::optional> magicFunctionAssert( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult) { auto [paramPack, predicates] = exprResult; if (expr.args.size < 1) return ExprResult{paramPack}; typechecker.reportErrors(typechecker.resolve(predicates, scope, true)); return ExprResult{paramPack}; } static std::optional> magicFunctionPack( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult) { auto [paramPack, _predicates] = exprResult; TypeArena& arena = typechecker.currentModule->internalTypes; const auto& [paramTypes, paramTail] = flatten(paramPack); std::vector options; options.reserve(paramTypes.size()); for (auto type : paramTypes) options.push_back(type); if (paramTail) { if (const VariadicTypePack* vtp = get(*paramTail)) options.push_back(vtp->ty); } options = typechecker.reduceUnion(options); // table.pack() -> {| n: number, [number]: nil |} // table.pack(1) -> {| n: number, [number]: number |} // table.pack(1, "foo") -> {| n: number, [number]: number | string |} TypeId result = nullptr; if (options.empty()) result = typechecker.nilType; else if (options.size() == 1) result = options[0]; else result = arena.addType(UnionTypeVar{std::move(options)}); TypeId packedTable = arena.addType( TableTypeVar{{{"n", {typechecker.numberType}}}, TableIndexer(typechecker.numberType, result), scope->level, TableState::Sealed}); return ExprResult{arena.addTypePack({packedTable})}; } static bool checkRequirePath(TypeChecker& typechecker, AstExpr* expr) { // require(foo.parent.bar) will technically work, but it depends on legacy goop that // Luau does not and could not support without a bunch of work. It's deprecated anyway, so // we'll warn here if we see it. bool good = true; AstExprIndexName* indexExpr = expr->as(); while (indexExpr) { if (indexExpr->index == "parent") { typechecker.reportError(indexExpr->indexLocation, DeprecatedApiUsed{"parent", "Parent"}); good = false; } indexExpr = indexExpr->expr->as(); } return good; } static std::optional> magicFunctionRequire( TypeChecker& typechecker, const ScopePtr& scope, const AstExprCall& expr, ExprResult exprResult) { TypeArena& arena = typechecker.currentModule->internalTypes; if (expr.args.size != 1) { typechecker.reportError(TypeError{expr.location, GenericError{"require takes 1 argument"}}); return std::nullopt; } if (!checkRequirePath(typechecker, expr.args.data[0])) return std::nullopt; if (auto moduleInfo = typechecker.resolver->resolveModuleInfo(typechecker.currentModuleName, expr)) return ExprResult{arena.addTypePack({typechecker.checkRequire(scope, *moduleInfo, expr.location)})}; return std::nullopt; } } // namespace Luau