Skip to content

Commit eedb4e3

Browse files
Merge pull request #1700 from xlsynth:cdleary/2024-11-06-slice-nullptr
PiperOrigin-RevId: 694662285
2 parents e630038 + 2cad5e2 commit eedb4e3

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

xls/dslx/frontend/ast_cloner.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,16 @@ class AstCloner : public AstNodeVisitor {
634634

635635
absl::Status HandleSlice(const Slice* n) override {
636636
XLS_RETURN_IF_ERROR(VisitChildren(n));
637-
old_to_new_[n] = module_->Make<Slice>(
638-
n->GetSpan().value(), down_cast<Expr*>(old_to_new_.at(n->start())),
639-
down_cast<Expr*>(old_to_new_.at(n->limit())));
637+
638+
Expr* new_start = n->start() == nullptr
639+
? nullptr
640+
: down_cast<Expr*>(old_to_new_.at(n->start()));
641+
Expr* new_limit = n->limit() == nullptr
642+
? nullptr
643+
: down_cast<Expr*>(old_to_new_.at(n->limit()));
644+
645+
old_to_new_[n] =
646+
module_->Make<Slice>(n->GetSpan().value(), new_start, new_limit);
640647
return absl::OkStatus();
641648
}
642649

xls/dslx/frontend/ast_cloner_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ TEST(AstClonerTest, TypeAlias) {
571571
XLS_ASSERT_OK(VerifyClone(type_alias, clone, file_table));
572572
}
573573

574+
// This is an interesting case because the start and limit of the slice are
575+
// nullptr.
576+
TEST(AstClonerTest, SliceWithNullptrs) {
577+
constexpr std::string_view kProgram =
578+
R"(const MOL = u32:42; const MOL2 = MOL[:];)";
579+
580+
FileTable file_table;
581+
XLS_ASSERT_OK_AND_ASSIGN(auto module, ParseModule(kProgram, "fake_path.x",
582+
"the_module", file_table));
583+
XLS_ASSERT_OK_AND_ASSIGN(ConstantDef * top_member,
584+
module->GetMemberOrError<ConstantDef>("MOL2"));
585+
XLS_ASSERT_OK_AND_ASSIGN(AstNode * clone, CloneAst(top_member));
586+
EXPECT_EQ("const MOL2 = MOL[:];", clone->ToString());
587+
XLS_ASSERT_OK(VerifyClone(top_member, clone, file_table));
588+
}
589+
574590
TEST(AstClonerTest, PreserveTypeDefinitionsReplacer) {
575591
constexpr std::string_view kProgram =
576592
R"(

xls/dslx/type_system/typecheck_module_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,20 @@ proc Bar {
468468
XLS_EXPECT_OK(Typecheck(kProgram));
469469
}
470470

471+
// Note: this previously caused a fatal error as the slice in this position is
472+
// handled in the AstCloner.
473+
TEST(TypecheckErrorTest, ProcWithSliceOfNumber) {
474+
constexpr std::string_view kProgram = R"(
475+
proc o {
476+
h: chan<u2[0[:]]> in;
477+
config(h: chan<u3> out) {}
478+
}
479+
)";
480+
EXPECT_THAT(Typecheck(kProgram),
481+
StatusIs(absl::StatusCode::kInvalidArgument,
482+
HasSubstr("Procs must define `init`")));
483+
}
484+
471485
TEST(TypecheckTest, ProcWithImplAsProcMemberInTuple) {
472486
constexpr std::string_view kProgram = R"(
473487
proc Foo {

0 commit comments

Comments
 (0)