Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
Replace assertion when child type not found with graceful handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Apr 5, 2022
1 parent 337515b commit 82a4d82
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ struct CliConfigResolver : Luau::ConfigResolver
}
};

Luau::TypeId makeInstanceType(Luau::TypeArena& typeArena, const Luau::ScopePtr& globalScope, SourceNode& node)
std::optional<Luau::TypeId> makeInstanceType(Luau::TypeArena& typeArena, const Luau::ScopePtr& globalScope, SourceNode& node)
{
std::optional<Luau::TypeFun> baseType;
if (node.className.has_value())
Expand All @@ -340,7 +340,13 @@ Luau::TypeId makeInstanceType(Luau::TypeArena& typeArena, const Luau::ScopePtr&
{
baseType = globalScope->lookupType("Instance");
}
LUAU_ASSERT(baseType); // TODO: is this ensured??
// If we reach this stage, we couldn't find the class name nor the "Instance" type
// This most likely means a valid definitions file was not provided
if (!baseType.has_value())
{
return std::nullopt;
}

auto typeId = baseType.value().type;

if (node.children.size() > 0)
Expand All @@ -349,8 +355,12 @@ Luau::TypeId makeInstanceType(Luau::TypeArena& typeArena, const Luau::ScopePtr&
Luau::TableTypeVar children{Luau::TableState::Sealed, globalScope->level};
for (const auto& child : node.children)
{
auto childProperty = Luau::makeProperty(makeInstanceType(typeArena, globalScope, *child.second), "@luau/instance");
children.props[child.first] = childProperty;
auto childType = makeInstanceType(typeArena, globalScope, *child.second);
if (childType.has_value())
{
auto childProperty = Luau::makeProperty(childType.value(), "@luau/instance");
children.props[child.first] = childProperty;
}
}
Luau::TypeId childId = typeArena.addType(children);
typeId = Luau::makeIntersection(typeArena, {typeId, childId});
Expand Down Expand Up @@ -471,9 +481,13 @@ int main(int argc, char** argv)
// Extend the props to include the children
for (const auto& child : (*services.second).children)
{
ctv->props[child.first] = makeProperty(
makeInstanceType(frontend.typeChecker.globalTypes, frontend.typeChecker.globalScope, *(child.second)),
"@luau/serviceChild");
auto childType =
makeInstanceType(frontend.typeChecker.globalTypes, frontend.typeChecker.globalScope, *(child.second));
if (childType.has_value())
{

ctv->props[child.first] = makeProperty(childType.value(), "@luau/serviceChild");
}
}
}
}
Expand Down Expand Up @@ -502,8 +516,10 @@ int main(int argc, char** argv)
auto typeArena = scope->returnType->owningArena;
LUAU_ASSERT(typeArena);
auto ty = makeInstanceType(*typeArena, scope, node.value());
if (!ty.has_value())
return;

scope->bindings[Luau::AstName("script")] = Luau::Binding{ty, Luau::Location{}, {}, {}, std::nullopt};
scope->bindings[Luau::AstName("script")] = Luau::Binding{ty.value(), Luau::Location{}, {}, {}, std::nullopt};
};

Luau::freeze(frontend.typeChecker.globalTypes);
Expand Down

0 comments on commit 82a4d82

Please sign in to comment.