Skip to content

Commit 062f1ba

Browse files
Merge pull request #1689 from xlsynth:cdleary/2024-11-01-ellipsis-requires-type-context
PiperOrigin-RevId: 693812992
2 parents 103c768 + d026260 commit 062f1ba

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

xls/dslx/type_system/deduce_expr.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ absl::StatusOr<std::unique_ptr<Type>> DeduceArray(const Array* node,
139139
member_types[0]->CloneToUnique(), member_types_dim);
140140

141141
if (node->type_annotation() == nullptr) {
142+
if (node->has_ellipsis()) {
143+
const Span array_obrack_span(node->span().start(), node->span().start());
144+
return TypeInferenceErrorStatus(
145+
array_obrack_span, inferred.get(),
146+
absl::StrFormat(
147+
"Array has ellipsis (`...`) but does not have a type annotation; "
148+
"please add a type annotation to indicate how many elements to "
149+
"expand to; for example: `%s[N]:%s`",
150+
inferred->element_type().ToString(), node->ToString()),
151+
ctx->file_table());
152+
}
153+
142154
return inferred;
143155
}
144156

xls/dslx/type_system/typecheck_module_test.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,18 @@ TEST(TypecheckErrorTest, ArrayEllipsisNoTrailingElement) {
20272027
"repeat; please add at least one element")));
20282028
}
20292029

2030+
TEST(TypecheckErrorTest, ArrayEllipsisNoLeadingTypeAnnotation) {
2031+
EXPECT_THAT(
2032+
Typecheck(R"(fn main() -> u8[2] {
2033+
let x: u8[2] = [u8:0, ...];
2034+
x
2035+
})"),
2036+
StatusIs(absl::StatusCode::kInvalidArgument,
2037+
HasSubstr("does not have a type annotation; please add a type "
2038+
"annotation to indicate how many elements to expand "
2039+
"to; for example: `uN[8][N]:[u8:0, ...]`")));
2040+
}
2041+
20302042
TEST(TypecheckTest, BadArrayAddition) {
20312043
EXPECT_THAT(Typecheck(R"(
20322044
fn f(a: bits[32][4], b: bits[32][4]) -> bits[32][4] {

0 commit comments

Comments
 (0)