Skip to content

Commit

Permalink
Merge b78b3b9 into 6467c85
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Jul 3, 2022
2 parents 6467c85 + b78b3b9 commit 4170def
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Analysis/src/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ CheckResult Frontend::check(const ModuleName& name, std::optional<FrontendOption
module->astTypes.clear();
module->astExpectedTypes.clear();
module->astOriginalCallTypes.clear();
module->astResolvedTypes.clear();
module->astResolvedTypePacks.clear();
module->scopes.resize(1);
}

Expand Down
52 changes: 29 additions & 23 deletions Analysis/src/TypeInfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4899,7 +4899,7 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation
if (lit->parameters.size != 1 || !lit->parameters.data[0].type)
{
reportError(TypeError{annotation.location, GenericError{"_luau_print requires one generic parameter"}});
return errorRecoveryType(anyType);
return currentModule->astResolvedTypes[&annotation] = errorRecoveryType(anyType);
}

ToStringOptions opts;
Expand All @@ -4909,7 +4909,7 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation

TypeId param = resolveType(scope, *lit->parameters.data[0].type);
luauPrintLine(format("_luau_print\t%s\t|\t%s", toString(param, opts).c_str(), toString(lit->location).c_str()));
return param;
return currentModule->astResolvedTypes[&annotation] = param;
}

else
Expand All @@ -4918,7 +4918,7 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation
if (!tf)
{
if (lit->name == kParseNameError)
return errorRecoveryType(scope);
return currentModule->astResolvedTypes[&annotation] = errorRecoveryType(scope);

std::string typeName;
if (lit->prefix)
Expand All @@ -4930,11 +4930,11 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation
else
reportError(TypeError{annotation.location, UnknownSymbol{typeName, UnknownSymbol::Type}});

return errorRecoveryType(scope);
return currentModule->astResolvedTypes[&annotation] = errorRecoveryType(scope);
}

if (lit->parameters.size == 0 && tf->typeParams.empty() && tf->typePackParams.empty())
return tf->type;
return currentModule->astResolvedTypes[&annotation] = tf->type;

bool parameterCountErrorReported = false;
bool hasDefaultTypes = std::any_of(tf->typeParams.begin(), tf->typeParams.end(), [](auto&& el) {
Expand Down Expand Up @@ -5085,9 +5085,9 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation
// If the generic parameters and the type arguments are the same, we are about to
// perform an identity substitution, which we can just short-circuit.
if (sameTys && sameTps)
return tf->type;
return currentModule->astResolvedTypes[&annotation] = tf->type;

return instantiateTypeFun(scope, *tf, typeParams, typePackParams, annotation.location);
return currentModule->astResolvedTypes[&annotation] = instantiateTypeFun(scope, *tf, typeParams, typePackParams, annotation.location);
}
else if (const auto& table = annotation.as<AstTypeTable>())
{
Expand All @@ -5102,7 +5102,7 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation

TableTypeVar ttv{props, tableIndexer, scope->level, TableState::Sealed};
ttv.definitionModuleName = currentModuleName;
return addType(std::move(ttv));
return currentModule->astResolvedTypes[&annotation] = addType(std::move(ttv));
}
else if (const auto& func = annotation.as<AstTypeFunction>())
{
Expand Down Expand Up @@ -5139,43 +5139,43 @@ TypeId TypeChecker::resolveType(const ScopePtr& scope, const AstType& annotation
ftv->argNames.push_back(std::nullopt);
}

return fnType;
return currentModule->astResolvedTypes[&annotation] = fnType;
}
else if (auto typeOf = annotation.as<AstTypeTypeof>())
{
TypeId ty = checkExpr(scope, *typeOf->expr).type;
return ty;
return currentModule->astResolvedTypes[&annotation] = ty;
}
else if (const auto& un = annotation.as<AstTypeUnion>())
{
std::vector<TypeId> types;
for (AstType* ann : un->types)
types.push_back(resolveType(scope, *ann));

return addType(UnionTypeVar{types});
return currentModule->astResolvedTypes[&annotation] = addType(UnionTypeVar{types});
}
else if (const auto& un = annotation.as<AstTypeIntersection>())
{
std::vector<TypeId> types;
for (AstType* ann : un->types)
types.push_back(resolveType(scope, *ann));

return addType(IntersectionTypeVar{types});
return currentModule->astResolvedTypes[&annotation] = addType(IntersectionTypeVar{types});
}
else if (const auto& tsb = annotation.as<AstTypeSingletonBool>())
{
return singletonType(tsb->value);
return currentModule->astResolvedTypes[&annotation] = singletonType(tsb->value);
}
else if (const auto& tss = annotation.as<AstTypeSingletonString>())
{
return singletonType(std::string(tss->value.data, tss->value.size));
return currentModule->astResolvedTypes[&annotation] = singletonType(std::string(tss->value.data, tss->value.size));
}
else if (annotation.is<AstTypeError>())
return errorRecoveryType(scope);
return currentModule->astResolvedTypes[&annotation] = errorRecoveryType(scope);
else
{
reportError(TypeError{annotation.location, GenericError{"Unknown type annotation?"}});
return errorRecoveryType(scope);
return currentModule->astResolvedTypes[&annotation] = errorRecoveryType(scope);
}
}

Expand All @@ -5200,9 +5200,10 @@ TypePackId TypeChecker::resolveTypePack(const ScopePtr& scope, const AstTypeList

TypePackId TypeChecker::resolveTypePack(const ScopePtr& scope, const AstTypePack& annotation)
{
TypePackId result;
if (const AstTypePackVariadic* variadic = annotation.as<AstTypePackVariadic>())
{
return addTypePack(TypePackVar{VariadicTypePack{resolveType(scope, *variadic->variadicType)}});
result = addTypePack(TypePackVar{VariadicTypePack{resolveType(scope, *variadic->variadicType)}});
}
else if (const AstTypePackGeneric* generic = annotation.as<AstTypePackGeneric>())
{
Expand All @@ -5216,10 +5217,12 @@ TypePackId TypeChecker::resolveTypePack(const ScopePtr& scope, const AstTypePack
else
reportError(TypeError{generic->location, UnknownSymbol{genericName, UnknownSymbol::Type}});

return errorRecoveryTypePack(scope);
result = errorRecoveryTypePack(scope);
}
else
{
result = *genericTy;
}

return *genericTy;
}
else if (const AstTypePackExplicit* explicitTp = annotation.as<AstTypePackExplicit>())
{
Expand All @@ -5229,14 +5232,17 @@ TypePackId TypeChecker::resolveTypePack(const ScopePtr& scope, const AstTypePack
types.push_back(resolveType(scope, *type));

if (auto tailType = explicitTp->typeList.tailType)
return addTypePack(types, resolveTypePack(scope, *tailType));

return addTypePack(types);
result = addTypePack(types, resolveTypePack(scope, *tailType));
else
result = addTypePack(types);
}
else
{
ice("Unknown AstTypePack kind");
}

currentModule->astResolvedTypePacks[&annotation] = result;
return result;
}

bool ApplyTypeFunction::isDirty(TypeId ty)
Expand Down
2 changes: 2 additions & 0 deletions tests/Frontend.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ TEST_CASE_FIXTURE(FrontendFixture, "discard_type_graphs")
CHECK_EQ(0, module->internalTypes.typeVars.size());
CHECK_EQ(0, module->internalTypes.typePacks.size());
CHECK_EQ(0, module->astTypes.size());
CHECK_EQ(0, module->astResolvedTypes.size());
CHECK_EQ(0, module->astResolvedTypePacks.size());
}

TEST_CASE_FIXTURE(FrontendFixture, "it_should_be_safe_to_stringify_errors_when_full_type_graph_is_discarded")
Expand Down

0 comments on commit 4170def

Please sign in to comment.