Skip to content

Commit

Permalink
Type: set incomplete array length correctly for typeof-types
Browse files Browse the repository at this point in the history
Fixes #692
  • Loading branch information
ehaas committed Apr 25, 2024
1 parent aa52b23 commit 82de1f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1818,10 +1818,8 @@ fn initDeclarator(p: *Parser, decl_spec: *DeclSpec, attr_buf_top: usize) Error!?
var init_list_expr = try p.initializer(init_d.d.ty);
init_d.initializer = init_list_expr;
if (!init_list_expr.ty.isArray()) break :init;
if (init_d.d.ty.specifier == .incomplete_array) {
// Modifying .data is exceptionally allowed for .incomplete_array.
init_d.d.ty.data.array.len = init_list_expr.ty.arrayLen() orelse break :init;
init_d.d.ty.specifier = .array;
if (init_d.d.ty.is(.incomplete_array)) {
init_d.d.ty.setIncompleteArrayLen(init_list_expr.ty.arrayLen() orelse break :init);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/aro/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,24 @@ pub fn isArray(ty: Type) bool {
};
}


/// Must only be used to set the length of an incomplete array as determined by its initializer
pub fn setIncompleteArrayLen(ty: *Type, len: u64) void {
switch (ty.specifier) {
.incomplete_array => {
// Modifying .data is exceptionally allowed for .incomplete_array.
ty.data.array.len = len;
ty.specifier = .array;
},

.typeof_type => ty.data.sub_type.setIncompleteArrayLen(len),
.typeof_expr => ty.data.expr.ty.setIncompleteArrayLen(len),
.attributed => ty.data.attributed.base.setIncompleteArrayLen(len),

else => unreachable,
}
}

/// Whether the type is promoted if used as a variadic argument or as an argument to a function with no prototype
fn undergoesDefaultArgPromotion(ty: Type, comp: *const Compilation) bool {
return switch (ty.specifier) {
Expand Down
2 changes: 2 additions & 0 deletions test/cases/typeof incomplete array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
typeof(const int[]) arr1 = {1,2};
_Static_assert(sizeof(arr1) == sizeof(int[2]), "");

0 comments on commit 82de1f0

Please sign in to comment.