diff --git a/releasenotes.md b/releasenotes.md index 2d257e553..f144962f5 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -62,6 +62,7 @@ - Fix unexpected display of macro definition when passing a poisoned expression #2305. - `@links` on macros would not be added to calling functions. - Fix `Formatter.print` returning incorrect size. +- A distinct type based on an array would yield .len == 0 ### Stdlib changes - Improve contract for readline. #2280 diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 2456f5c2d..3887facf0 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4862,16 +4862,20 @@ static inline bool sema_create_const_len(Expr *expr, Type *type, Type *flat) ASSERT_SPAN(expr, flat == type_flatten(flat) && "Should be flattened already."); size_t len; - if (type->type_kind == TYPE_CONST_ENUM) goto ENUMS; + if (type->type_kind == TYPE_CONST_ENUM) + { + len = vec_size(type->decl->enums.values); + expr_rewrite_const_int(expr, type_usz, len); + return true; + } switch (flat->type_kind) { case TYPE_ARRAY: case TYPE_VECTOR: - len = type->array.len; + len = flat->array.len; break; case TYPE_ENUM: -ENUMS: - len = vec_size(type->decl->enums.values); + len = vec_size(flat->decl->enums.values); break; case TYPE_INFERRED_ARRAY: case TYPE_FLEXIBLE_ARRAY: diff --git a/test/test_suite/compile_time_introspection/len_distinct.c3t b/test/test_suite/compile_time_introspection/len_distinct.c3t new file mode 100644 index 000000000..588d4635c --- /dev/null +++ b/test/test_suite/compile_time_introspection/len_distinct.c3t @@ -0,0 +1,6 @@ +typedef Foo = int[32]; +fn int main() +{ + $assert(Foo.len == 32); + return 0; +} \ No newline at end of file