Skip to content

Commit 350979b

Browse files
NL02copybara-github
authored andcommitted
[Explicit State Access] Error if attempting to read or write without feature flag
PiperOrigin-RevId: 868884341
1 parent 9aff70d commit 350979b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

xls/dslx/frontend/parser.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,16 @@ absl::StatusOr<Expr*> Parser::BuildMacroOrInvocation(
28842884
parametrics);
28852885
}
28862886

2887+
if (name == "read" || name == "write") {
2888+
if (!module_->attributes().contains(
2889+
ModuleAttribute::kExplicitStateAccess)) {
2890+
return ParseErrorStatus(
2891+
span,
2892+
absl::StrFormat(
2893+
"%s() requires #![feature(explicit_state_access)]", name));
2894+
}
2895+
}
2896+
28872897
if (name == "zero!") {
28882898
if (parametrics.size() != 1) {
28892899
return ParseErrorStatus(

xls/dslx/frontend/parser_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,26 @@ TEST_F(ParserTest, ParseExplicitStateAccessAttribute) {
34983498
testing::ElementsAre(ModuleAttribute::kExplicitStateAccess));
34993499
}
35003500

3501+
TEST_F(ParserTest, ParseExplicitStateAccessNoFeatureFlag) {
3502+
constexpr std::string_view kProgram = R"(
3503+
proc foo {
3504+
config() { () }
3505+
init { u32: 10 }
3506+
next(state: u32) {
3507+
read(state);
3508+
write(state, state + 1);
3509+
state
3510+
}
3511+
}
3512+
)";
3513+
Scanner s{file_table_, Fileno(0), std::string(kProgram)};
3514+
Parser parser{"test", &s};
3515+
EXPECT_THAT(parser.ParseModule(),
3516+
StatusIs(absl::StatusCode::kInvalidArgument,
3517+
HasSubstr("read() requires "
3518+
"#![feature(explicit_state_access)]")));
3519+
}
3520+
35013521
TEST_F(ParserTest, ExplicitStateAccessProcNextReturnsEmptyTuple) {
35023522
constexpr std::string_view kProgram = R"(#![feature(explicit_state_access)]
35033523
proc simple {

0 commit comments

Comments
 (0)