Skip to content

Commit eb66b07

Browse files
NL02copybara-github
authored andcommitted
[Explicit State Access] Return the type as the initialized state in init
Currently it returns the same type as Proc Next field which is an empty tuple. PiperOrigin-RevId: 862298516
1 parent 4cdc72d commit eb66b07

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

xls/dslx/frontend/parser.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,12 +3522,19 @@ absl::StatusOr<ModuleMember> Parser::ParseProcLike(const Pos& start_pos,
35223522

35233523
// Just as with proc member decls, we need the init fn to have its own return
35243524
// type, to avoid parent/child relationship violations.
3525-
XLS_ASSIGN_OR_RETURN(auto* init_return_type,
3526-
CloneNode(proc_like_body.next->return_type(),
3527-
&PreserveTypeDefinitionsReplacer));
3525+
TypeAnnotation* type_to_clone;
3526+
if (module_->attributes().contains(ModuleAttribute::kExplicitStateAccess)) {
3527+
XLS_RET_CHECK(!proc_like_body.next->params().empty())
3528+
<< "Proc 'next' function unexpectedly had no parameters.";
3529+
type_to_clone = proc_like_body.next->params()[0]->type_annotation();
3530+
} else {
3531+
type_to_clone = proc_like_body.next->return_type();
3532+
}
3533+
XLS_ASSIGN_OR_RETURN(
3534+
TypeAnnotation * init_return_type,
3535+
CloneNode(type_to_clone, &PreserveTypeDefinitionsReplacer));
35283536
init_return_type->SetParentage();
3529-
proc_like_body.init->set_return_type(
3530-
down_cast<TypeAnnotation*>(init_return_type));
3537+
proc_like_body.init->set_return_type(init_return_type);
35313538
proc_like_body.init->SetParentage();
35323539

35333540
auto* proc_like = module_->Make<T>(span, body_span, name_def,

xls/dslx/frontend/parser_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3495,6 +3495,26 @@ proc simple {
34953495
EXPECT_EQ(next.return_type()->ToString(), "()");
34963496
}
34973497

3498+
TEST_F(ParserTest, ExplicitStateAccessProcInitReturnsSameTypeAsSelf) {
3499+
constexpr std::string_view kProgram = R"(#![feature(explicit_state_access)]
3500+
proc simple {
3501+
config() {
3502+
()
3503+
}
3504+
init {
3505+
u32:0
3506+
}
3507+
next(state: u32) {
3508+
}
3509+
})";
3510+
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Module> module, Parse(kProgram));
3511+
XLS_ASSERT_OK_AND_ASSIGN(Proc * proc,
3512+
module->GetMemberOrError<Proc>("simple"));
3513+
const Function& init = proc->init();
3514+
ASSERT_NE(init.return_type(), nullptr);
3515+
EXPECT_EQ(init.return_type()->ToString(), "u32");
3516+
}
3517+
34983518
// Verifies that we can walk backwards through a tree. In this case, from the
34993519
// terminal node to the defining expr.
35003520
TEST(ParserBackrefTest, CanFindDefiner) {

0 commit comments

Comments
 (0)