Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DSLX:TS] Cannot index a parameteric-signedness value. #1837

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions xls/dslx/type_system/deduce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,14 @@ absl::StatusOr<std::unique_ptr<Type>> DeduceIndex(const Index* node,
ctx->file_table());
}

std::optional<BitsLikeProperties> bits_like = GetBitsLike(*lhs_type);
if (bits_like.has_value()) {
return TypeInferenceErrorStatus(
node->span(), lhs_type.get(),
"Bits-like value cannot be indexed, value to index is not an array.",
ctx->file_table());
}

auto* array_type = dynamic_cast<ArrayType*>(lhs_type.get());
if (array_type == nullptr) {
return TypeInferenceErrorStatus(node->span(), lhs_type.get(),
Expand Down
14 changes: 12 additions & 2 deletions xls/dslx/type_system/typecheck_module_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1961,18 +1961,28 @@ fn f() -> u32 {
}

TEST(TypecheckTest, Index) {
// Indexing a 4-element bit-constructed array with a constant index.
XLS_EXPECT_OK(Typecheck("fn f(x: uN[32][4]) -> u32 { x[u32:0] }"));

// Indexing a 5-element array with a dynamic index.
XLS_EXPECT_OK(Typecheck("fn f(x: u32[5], i: u8) -> u32 { x[i] }"));

// Indexing a bit value is not allowed, only arrays.
EXPECT_THAT(
Typecheck("fn f(x: u32, i: u8) -> u32 { x[i] }"),
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("not an array")));

// Use an array as an index.
// Indexing a parametric-signedness bit value is not allowed.
EXPECT_THAT(
Typecheck("fn f(x: xN[false][5], i: u8) -> u1 { x[i] }"),
StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("not an array")));

// Cannot use an array as an index.
EXPECT_THAT(Typecheck("fn f(x: u32[5], i: u8[5]) -> u32 { x[i] }"),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Index is not bits typed")));

// Use a signed number as an index.
// Cannot use a signed number as an index.
EXPECT_THAT(Typecheck("fn f(x: u32[5], i: s8) -> u32 { x[i] }"),
StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Index is not unsigned-bits typed")));
Expand Down