Skip to content

Commit e87c628

Browse files
richmckeevercopybara-github
authored andcommitted
Avoid inadvertent circumventing of the check for under-qualified array ellipsis.
Fixes #3212 PiperOrigin-RevId: 820820986
1 parent 259d031 commit e87c628

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

xls/dslx/type_system_v2/typecheck_module_v2_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,29 @@ TEST(TypecheckV2Test, GlobalArrayConstantWithEllipsisAndNoElementsFails) {
11241124
"without an element to repeat")));
11251125
}
11261126

1127+
TEST(TypecheckV2Test, EllipsisArrayWithNoDeclaredSizeFails) {
1128+
EXPECT_THAT(
1129+
R"(
1130+
fn bar(x: u32[5]) -> u32 {
1131+
x[4]
1132+
}
1133+
1134+
fn foo() -> u32 {
1135+
let a = u32:1;
1136+
let b = u32:2;
1137+
let y = [a, b, a, ...];
1138+
bar(y)
1139+
}
1140+
1141+
#[test]
1142+
fn foo_test() {
1143+
assert_eq(foo(), u32:1);
1144+
}
1145+
)",
1146+
TypecheckFails(HasSubstr(
1147+
"Array has ellipsis (`...`) but does not have a type annotation.")));
1148+
}
1149+
11271150
TEST(TypecheckV2Test, GlobalArrayConstantEmptyWithoutAnnotationFails) {
11281151
EXPECT_THAT(
11291152
"const X = [];",

xls/dslx/type_system_v2/unify_type_annotations.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
namespace xls::dslx {
4949
namespace {
5050

51-
5251
bool operator==(const SignednessAndSize& x, const SignednessAndSize& y) {
5352
return x.flag == y.flag && x.is_signed == y.is_signed && x.size == y.size;
5453
}
@@ -71,6 +70,11 @@ std::optional<const TypeAnnotation*> GetSliceContainerSize(
7170
return std::nullopt;
7271
}
7372

73+
bool IsArrayTypeWithMinSize(const TypeAnnotation* annotation) {
74+
return annotation->annotation_kind() == TypeAnnotationKind::kArray &&
75+
down_cast<const ArrayTypeAnnotation*>(annotation)->dim_is_min();
76+
}
77+
7478
absl::Status MinSizeLargerThanStandardSizeError(
7579
const TypeAnnotation* min_annotation, const SignednessAndSize& min,
7680
const SignednessAndSize& standard, const FileTable& file_table) {
@@ -121,7 +125,7 @@ class Unifier {
121125
if (annotations.empty()) {
122126
return module_.Make<AnyTypeAnnotation>(/*multiple=*/true);
123127
}
124-
if (annotations.size() == 1 &&
128+
if (annotations.size() == 1 && !IsArrayTypeWithMinSize(annotations[0]) &&
125129
!table_.GetAnnotationFlag(annotations[0])
126130
.HasFlag(TypeInferenceFlag::kFormalMemberType)) {
127131
XLS_ASSIGN_OR_RETURN(std::optional<StructOrProcRef> struct_or_proc_ref,

0 commit comments

Comments
 (0)