Skip to content

Commit

Permalink
Error when slicing a struct with an inline array #1488.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Sep 27, 2024
1 parent 4cdea86 commit a99e4b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
- Regression when compile time accessing a union field not last assigned to.
- Safer seed of rand() for WASM without libc.
- Bad error message aliasing an ident with a path. #1481.
- Error when slicing a struct with an inline array #1488.

### Stdlib changes
- Additional init functions for hashmap.
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/sema_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3357,13 +3357,16 @@ static inline bool sema_expr_analyse_slice(SemaContext *context, Expr *expr, Che
}
Expr *current_expr = subscripted;
Type *inner_type = sema_subscript_find_indexable_type_recursively(&type, &current_expr);

if (type == type_voidptr) inner_type = type_char;

if (!inner_type || !type_is_valid_for_array(inner_type))
{
RETURN_SEMA_ERROR(subscripted, "Cannot index %s.", type_quoted_error_string(subscripted->type));
}
if (current_expr != subscripted)
{
expr->slice_expr.expr = exprid(current_expr);
}
// Retain the original type when doing distinct slices.
Type *result_type = type_get_slice(inner_type);
Type *original_type_canonical = original_type->canonical;
Expand Down
34 changes: 34 additions & 0 deletions test/test_suite/slices/slice_inline.c3t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// #target: macos-x64
module foo;
struct Data{
inline char[32] data;
}
fn void Data.not_working(self) {
self[:self.data.len];
}

fn void Data.working(self) {
self.data[:self.data.len];
}
fn void! main(String[] args) {
Data d;
d.not_working();
d.working();
}

/* #expect: foo.ll

define void @foo.Data.not_working(ptr byval(%Data) align 8 %0) #0 {
entry:
%1 = insertvalue %"char[]" undef, ptr %0, 0
%2 = insertvalue %"char[]" %1, i64 32, 1
ret void
}
; Function Attrs: nounwind uwtable
define void @foo.Data.working(ptr byval(%Data) align 8 %0) #0 {
entry:
%1 = insertvalue %"char[]" undef, ptr %0, 0
%2 = insertvalue %"char[]" %1, i64 32, 1
ret void
}

0 comments on commit a99e4b6

Please sign in to comment.