Skip to content

Commit 2cad5e2

Browse files
committed
[DSLX:FE] Fix slice cloning.
1 parent 062f1ba commit 2cad5e2

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
@@ -633,9 +633,16 @@ class AstCloner : public AstNodeVisitor {
633633

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

xls/dslx/frontend/ast_cloner_test.cc

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

514+
// This is an interesting case because the start and limit of the slice are
515+
// nullptr.
516+
TEST(AstClonerTest, SliceWithNullptrs) {
517+
constexpr std::string_view kProgram =
518+
R"(const MOL = u32:42; const MOL2 = MOL[:];)";
519+
520+
FileTable file_table;
521+
XLS_ASSERT_OK_AND_ASSIGN(auto module, ParseModule(kProgram, "fake_path.x",
522+
"the_module", file_table));
523+
XLS_ASSERT_OK_AND_ASSIGN(ConstantDef * top_member,
524+
module->GetMemberOrError<ConstantDef>("MOL2"));
525+
XLS_ASSERT_OK_AND_ASSIGN(AstNode * clone, CloneAst(top_member));
526+
EXPECT_EQ("const MOL2 = MOL[:];", clone->ToString());
527+
XLS_ASSERT_OK(VerifyClone(top_member, clone, file_table));
528+
}
529+
514530
TEST(AstClonerTest, PreserveTypeDefinitionsReplacer) {
515531
constexpr std::string_view kProgram =
516532
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)