mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 11:21:05 +01:00
Add a test and check-support for positional params with a return type (#4899)
Functions with positional parameters omit any implicit or explicit parameter lists. This causes them to not have a pattern block, which crashes if there is a return type that needs to add to the pattern block. Add a test covering this and handle it by having the ReturnTypeId handler peek at the node stack and conditionally add the missing pattern block. To do so it looks to see if the previous node is a `IdentifierNameNotBeforeParams` which implies it was not expecting a pattern (since there are no params) and thus the pattern block was not added to the stack. Note that lambdas also allow functions to omit an identifier, which will need a pattern block on the stack for implicit parameters, explicit parameters or a return type, without seeing any IdentifierName-like parse nodes. To handle this, we will need to look for additional nodes in the future and add the missing pattern block to the stack - possibly for the FunctionInitializer, but the parse support needs to be created for lambdas first.
This commit is contained in:
@@ -43,6 +43,20 @@ auto HandleParseNode(Context& context, Parse::ReturnTypeId node_id) -> bool {
|
||||
// Propagate the type expression.
|
||||
auto [type_node_id, type_inst_id] = context.node_stack().PopExprWithNodeId();
|
||||
auto type_id = ExprAsType(context, type_node_id, type_inst_id).type_id;
|
||||
|
||||
// If the previous node was `IdentifierNameBeforeParams`, then it would have
|
||||
// caused these entries to be pushed to the pattern stacks. But it's possible
|
||||
// to have a fn declaration without any parameters, in which case we find
|
||||
// `IdentifierNameNotBeforeParams` on the node stack. Then these entries are
|
||||
// not on the pattern stacks yet. They are only needed in that case if we have
|
||||
// a return type, which we now know that we do.
|
||||
if (context.node_stack().PeekNodeKind() ==
|
||||
Parse::NodeKind::IdentifierNameNotBeforeParams) {
|
||||
context.pattern_block_stack().Push();
|
||||
context.full_pattern_stack().PushFullPattern(
|
||||
FullPatternStack::Kind::ExplicitParamList);
|
||||
}
|
||||
|
||||
auto return_slot_pattern_id =
|
||||
context.AddPatternInst<SemIR::ReturnSlotPattern>(
|
||||
node_id, {.type_id = type_id, .type_inst_id = type_inst_id});
|
||||
|
||||
Reference in New Issue
Block a user