From 7f3c04924492389f319e46f14c29fd0faa3d30c7 Mon Sep 17 00:00:00 2001 From: TFilteredC Date: Sun, 22 Jan 2023 23:25:54 +0000 Subject: [PATCH] Different approach Now to fix the tests so that it likes number | string instead of just string --- Analysis/src/Type.cpp | 45 ++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/Analysis/src/Type.cpp b/Analysis/src/Type.cpp index d50ac60f..2d73d011 100644 --- a/Analysis/src/Type.cpp +++ b/Analysis/src/Type.cpp @@ -50,14 +50,17 @@ static bool dcrMagicFunctionFind(MagicFunctionCallContext context); TypeId follow(TypeId t) { - return follow(t, [](TypeId t) { - return t; - }); + return follow(t, + [](TypeId t) + { + return t; + }); } TypeId follow(TypeId t, std::function mapper) { - auto advance = [&mapper](TypeId ty) -> std::optional { + auto advance = [&mapper](TypeId ty) -> std::optional + { if (auto btv = get>(mapper(ty))) return btv->boundTo; else if (auto ttv = get(mapper(ty))) @@ -66,7 +69,8 @@ TypeId follow(TypeId t, std::function mapper) return std::nullopt; }; - auto force = [&mapper](TypeId ty) { + auto force = [&mapper](TypeId ty) + { if (auto ltv = get_if(&mapper(ty)->ty)) { TypeId res = ltv->thunk(); @@ -237,7 +241,8 @@ bool isOverloadedFunction(TypeId ty) if (!get(follow(ty))) return false; - auto isFunction = [](TypeId part) -> bool { + auto isFunction = [](TypeId part) -> bool + { return get(part); }; @@ -1076,11 +1081,11 @@ IntersectionTypeIterator end(const IntersectionType* itv) return IntersectionTypeIterator{}; } -static std::vector parseFormatString(NotNull builtinTypes, const char* data, size_t size) +static std::vector parseFormatString(NotNull builtinTypes, const char* data, size_t size) { const char* options = "cdiouxXeEfgGqs*"; - std::vector result; + std::vector result; for (size_t i = 0; i < size; ++i) { @@ -1099,13 +1104,17 @@ static std::vector parseFormatString(NotNull builtinTyp break; if (data[i] == 'q' || data[i] == 's') - result.push_back(UnionType {{builtinTypes->stringType, builtinTypes->numberType}}); + { + // there has to be a better way than this help :( + TypeId unionFormatId = (new TypeArena)->addType(UnionType{{builtinTypes->stringType, builtinTypes->numberType}}); + result.push_back(unionFormatId); + } else if (data[i] == '*') - result.push_back(UnionType {{builtinTypes->unknownType}}); + result.push_back(builtinTypes->unknownType); else if (strchr(options, data[i])) - result.push_back(UnionType {{builtinTypes->numberType}}); + result.push_back(builtinTypes->numberType); else - result.push_back(UnionType {{builtinTypes->errorRecoveryType(builtinTypes->anyType)}}); + result.push_back(builtinTypes->errorRecoveryType(builtinTypes->anyType)); } } @@ -1134,7 +1143,7 @@ std::optional> magicFunctionFormat( if (!fmt) return std::nullopt; - std::vector expected = parseFormatString(typechecker.builtinTypes, fmt->value.data, fmt->value.size); + std::vector expected = parseFormatString(typechecker.builtinTypes, fmt->value.data, fmt->value.size); const auto& [params, tail] = flatten(paramPack); size_t paramOffset = 1; @@ -1144,10 +1153,7 @@ std::optional> magicFunctionFormat( for (size_t i = 0; i < expected.size() && i + paramOffset < params.size(); ++i) { Location location = expr.args.data[std::min(i + dataOffset, expr.args.size - 1)]->location; - - // use arena to flatten union type - const TypeId formatTypes = arena.addType(expected[i]); - typechecker.unify(params[i + paramOffset], formatTypes, scope, location); + typechecker.unify(params[i + paramOffset], expected[i], scope, location); } // if we know the argument count or if we have too many arguments for sure, we can issue an error @@ -1179,7 +1185,7 @@ static bool dcrMagicFunctionFormat(MagicFunctionCallContext context) if (!fmt) return false; - std::vector expected = parseFormatString(context.solver->builtinTypes, fmt->value.data, fmt->value.size); + std::vector expected = parseFormatString(context.solver->builtinTypes, fmt->value.data, fmt->value.size); const auto& [params, tail] = flatten(context.arguments); size_t paramOffset = 1; @@ -1187,8 +1193,7 @@ static bool dcrMagicFunctionFormat(MagicFunctionCallContext context) // unify the prefix one argument at a time for (size_t i = 0; i < expected.size() && i + paramOffset < params.size(); ++i) { - const TypeId formatTypes = arena->addType(expected[i]); - context.solver->unify(params[i + paramOffset], formatTypes, context.solver->rootScope); + context.solver->unify(params[i + paramOffset], expected[i], context.solver->rootScope); } // if we know the argument count or if we have too many arguments for sure, we can issue an error