Add a typed node return to AddNode (#5123)

Building on #5120, make a variant of `AddNode` that returns typed nodes,
and replace `UnsafeMake` uses with it. This switches to templating in
import parsing so that we can get type validation.

With this change, `UnsafeMake` ends up used in three places: `Tree::As`,
`Tree::TryAs`, and `Context::AddNode`. That should mean that all typed
nodes are verified.
This commit is contained in:
Jon Ross-Perkins
2025-03-18 16:59:33 +00:00
committed by GitHub
parent a801a982bc
commit b555392cee
4 changed files with 43 additions and 37 deletions
+5 -9
View File
@@ -447,27 +447,23 @@ static auto ParsingInDeferredDefinitionScope(Context& context) -> bool {
auto Context::AddFunctionDefinitionStart(Lex::TokenIndex token, bool has_error)
-> void {
auto start_id = AddNode<NodeKind::FunctionDefinitionStart>(token, has_error);
if (ParsingInDeferredDefinitionScope(*this)) {
deferred_definition_stack_.push_back(tree_->deferred_definitions_.Add(
{.start_id = FunctionDefinitionStartId::UnsafeMake(
NodeId(tree_->node_impls_.size()))}));
deferred_definition_stack_.push_back(
tree_->deferred_definitions_.Add({.start_id = start_id}));
}
AddNode(NodeKind::FunctionDefinitionStart, token, has_error);
}
auto Context::AddFunctionDefinition(Lex::TokenIndex token, bool has_error)
-> void {
auto definition_id = AddNode<NodeKind::FunctionDefinition>(token, has_error);
if (ParsingInDeferredDefinitionScope(*this)) {
auto definition_index = deferred_definition_stack_.pop_back_val();
auto& definition = tree_->deferred_definitions_.Get(definition_index);
definition.definition_id =
FunctionDefinitionId::UnsafeMake(NodeId(tree_->node_impls_.size()));
definition.definition_id = definition_id;
definition.next_definition_index =
DeferredDefinitionIndex(tree_->deferred_definitions().size());
}
AddNode(NodeKind::FunctionDefinition, token, has_error);
}
auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {