authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-16 15:13:06+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-17 20:28:43+02:00
log14f03fbd1602dae8fcaa5da42f5769422d1c4a7a
tree5efe70f435efc79416b4b8a7a25fbabb0aa4419f
parent24646b8b5d026a75deb5b3eace0e9142f0719da0

AstGen: reset source cursor before generating pointer attributes

These attributes can appear in any order but AstGen expects the source cursor to be incremented in a monotonically increasing order. Closes #14332

2 files changed, 31 insertions(+), 4 deletions(-)

src/AstGen.zig+21-4
...@@ -3341,6 +3341,9 @@ fn ptrType(...@@ -3341,6 +3341,9 @@ fn ptrType(
3341 return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});3341 return gz.astgen.failTok(ptr_info.allowzero_token.?, "C pointers always allow address zero", .{});
3342 }3342 }
33433343
3344 const source_offset = gz.astgen.source_offset;
3345 const source_line = gz.astgen.source_line;
3346 const source_column = gz.astgen.source_column;
3344 const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);3347 const elem_type = try typeExpr(gz, scope, ptr_info.ast.child_type);
33453348
3346 var sentinel_ref: Zir.Inst.Ref = .none;3349 var sentinel_ref: Zir.Inst.Ref = .none;
...@@ -3351,17 +3354,31 @@ fn ptrType(...@@ -3351,17 +3354,31 @@ fn ptrType(
3351 var trailing_count: u32 = 0;3354 var trailing_count: u32 = 0;
33523355
3353 if (ptr_info.ast.sentinel != 0) {3356 if (ptr_info.ast.sentinel != 0) {
3357 // These attributes can appear in any order and they all come before the
3358 // element type so we need to reset the source cursor before generating them.
3359 gz.astgen.source_offset = source_offset;
3360 gz.astgen.source_line = source_line;
3361 gz.astgen.source_column = source_column;
3362
3354 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);3363 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);
3355 trailing_count += 1;3364 trailing_count += 1;
3356 }3365 }
3357 if (ptr_info.ast.align_node != 0) {
3358 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);
3359 trailing_count += 1;
3360 }
3361 if (ptr_info.ast.addrspace_node != 0) {3366 if (ptr_info.ast.addrspace_node != 0) {
3367 gz.astgen.source_offset = source_offset;
3368 gz.astgen.source_line = source_line;
3369 gz.astgen.source_column = source_column;
3370
3362 addrspace_ref = try expr(gz, scope, .{ .rl = .{ .ty = .address_space_type } }, ptr_info.ast.addrspace_node);3371 addrspace_ref = try expr(gz, scope, .{ .rl = .{ .ty = .address_space_type } }, ptr_info.ast.addrspace_node);
3363 trailing_count += 1;3372 trailing_count += 1;
3364 }3373 }
3374 if (ptr_info.ast.align_node != 0) {
3375 gz.astgen.source_offset = source_offset;
3376 gz.astgen.source_line = source_line;
3377 gz.astgen.source_column = source_column;
3378
3379 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);
3380 trailing_count += 1;
3381 }
3365 if (ptr_info.ast.bit_range_start != 0) {3382 if (ptr_info.ast.bit_range_start != 0) {
3366 assert(ptr_info.ast.bit_range_end != 0);3383 assert(ptr_info.ast.bit_range_end != 0);
3367 bit_start_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start);3384 bit_start_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start);
test/behavior/pointers.zig+10
...@@ -522,3 +522,13 @@ test "ptrToInt on a generic function" {...@@ -522,3 +522,13 @@ test "ptrToInt on a generic function" {
522 };522 };
523 try S.doTheTest(&S.generic);523 try S.doTheTest(&S.generic);
524}524}
525
526test "pointer alignment and element type include call expression" {
527 const S = struct {
528 fn T() type {
529 return struct { _: i32 };
530 }
531 const P = *align(@alignOf(T())) [@sizeOf(T())]u8;
532 };
533 try expect(@alignOf(S.P) > 0);
534}