Skip to content

Commit 34835bb

Browse files
authored
Merge pull request #13010 from Vexu/stage2-fixes
fix stack trace line numbers
2 parents f6312e4 + 2a4e89e commit 34835bb

13 files changed

+93
-24
lines changed

src/AstGen.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,13 +4233,10 @@ fn structDeclInner(
42334233
// are in scope, so that field types, alignments, and default value expressions
42344234
// can refer to decls within the struct itself.
42354235
astgen.advanceSourceCursorToNode(node);
4236-
// If `node == 0` then this is the root struct and all the declarations should
4237-
// be relative to the beginning of the file.
4238-
const decl_line = if (node == 0) 0 else astgen.source_line;
42394236
var block_scope: GenZir = .{
42404237
.parent = &namespace.base,
42414238
.decl_node_index = node,
4242-
.decl_line = decl_line,
4239+
.decl_line = gz.decl_line,
42434240
.astgen = astgen,
42444241
.force_comptime = true,
42454242
.instructions = gz.instructions,
@@ -4439,7 +4436,7 @@ fn unionDeclInner(
44394436
var block_scope: GenZir = .{
44404437
.parent = &namespace.base,
44414438
.decl_node_index = node,
4442-
.decl_line = astgen.source_line,
4439+
.decl_line = gz.decl_line,
44434440
.astgen = astgen,
44444441
.force_comptime = true,
44454442
.instructions = gz.instructions,
@@ -4722,7 +4719,7 @@ fn containerDecl(
47224719
var block_scope: GenZir = .{
47234720
.parent = &namespace.base,
47244721
.decl_node_index = node,
4725-
.decl_line = astgen.source_line,
4722+
.decl_line = gz.decl_line,
47264723
.astgen = astgen,
47274724
.force_comptime = true,
47284725
.instructions = gz.instructions,
@@ -4827,7 +4824,7 @@ fn containerDecl(
48274824
var block_scope: GenZir = .{
48284825
.parent = &namespace.base,
48294826
.decl_node_index = node,
4830-
.decl_line = astgen.source_line,
4827+
.decl_line = gz.decl_line,
48314828
.astgen = astgen,
48324829
.force_comptime = true,
48334830
.instructions = gz.instructions,

src/Module.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,6 +4435,7 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
44354435
new_decl.alive = true; // This Decl corresponds to a File and is therefore always alive.
44364436
new_decl.analysis = .in_progress;
44374437
new_decl.generation = mod.generation;
4438+
new_decl.name_fully_qualified = true;
44384439

44394440
if (file.status == .success_zir) {
44404441
assert(file.zir_loaded);

src/Sema.zig

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,11 @@ fn failWithBadMemberAccess(
43534353
.Enum => "enum",
43544354
else => unreachable,
43554355
};
4356+
if (sema.mod.declIsRoot(agg_ty.getOwnerDecl())) {
4357+
return sema.fail(block, field_src, "root struct of file '{}' has no member named '{s}'", .{
4358+
agg_ty.fmt(sema.mod), field_name,
4359+
});
4360+
}
43564361
const msg = msg: {
43574362
const msg = try sema.errMsg(block, field_src, "{s} '{}' has no member named '{s}'", .{
43584363
kw_name, agg_ty.fmt(sema.mod), field_name,
@@ -21664,14 +21669,17 @@ fn fieldPtr(
2166421669
else
2166521670
object_ptr;
2166621671

21672+
const attr_ptr_ty = if (is_pointer_to) object_ty else object_ptr_ty;
21673+
2166721674
if (mem.eql(u8, field_name, "ptr")) {
2166821675
const buf = try sema.arena.create(Type.SlicePtrFieldTypeBuffer);
2166921676
const slice_ptr_ty = inner_ty.slicePtrFieldType(buf);
2167021677

2167121678
const result_ty = try Type.ptr(sema.arena, sema.mod, .{
2167221679
.pointee_type = slice_ptr_ty,
21673-
.mutable = object_ptr_ty.ptrIsMutable(),
21674-
.@"addrspace" = object_ptr_ty.ptrAddressSpace(),
21680+
.mutable = attr_ptr_ty.ptrIsMutable(),
21681+
.@"volatile" = attr_ptr_ty.isVolatilePtr(),
21682+
.@"addrspace" = attr_ptr_ty.ptrAddressSpace(),
2167521683
});
2167621684

2167721685
if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| {
@@ -21690,8 +21698,9 @@ fn fieldPtr(
2169021698
} else if (mem.eql(u8, field_name, "len")) {
2169121699
const result_ty = try Type.ptr(sema.arena, sema.mod, .{
2169221700
.pointee_type = Type.usize,
21693-
.mutable = object_ptr_ty.ptrIsMutable(),
21694-
.@"addrspace" = object_ptr_ty.ptrAddressSpace(),
21701+
.mutable = attr_ptr_ty.ptrIsMutable(),
21702+
.@"volatile" = attr_ptr_ty.isVolatilePtr(),
21703+
.@"addrspace" = attr_ptr_ty.ptrAddressSpace(),
2169521704
});
2169621705

2169721706
if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| {

src/type.zig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,12 +3458,21 @@ pub const Type = extern union {
34583458
else => {},
34593459
}
34603460

3461+
const payload_size = switch (try child_type.abiSizeAdvanced(target, strat)) {
3462+
.scalar => |elem_size| elem_size,
3463+
.val => switch (strat) {
3464+
.sema_kit => unreachable,
3465+
.eager => unreachable,
3466+
.lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) },
3467+
},
3468+
};
3469+
34613470
// Optional types are represented as a struct with the child type as the first
34623471
// field and a boolean as the second. Since the child type's abi alignment is
34633472
// guaranteed to be >= that of bool's (1 byte) the added size is exactly equal
34643473
// to the child type's ABI alignment.
34653474
return AbiSizeAdvanced{
3466-
.scalar = child_type.abiAlignment(target) + child_type.abiSize(target),
3475+
.scalar = child_type.abiAlignment(target) + payload_size,
34673476
};
34683477
},
34693478

@@ -3478,7 +3487,14 @@ pub const Type = extern union {
34783487
}
34793488
const code_align = abiAlignment(Type.anyerror, target);
34803489
const payload_align = abiAlignment(data.payload, target);
3481-
const payload_size = abiSize(data.payload, target);
3490+
const payload_size = switch (try data.payload.abiSizeAdvanced(target, strat)) {
3491+
.scalar => |elem_size| elem_size,
3492+
.val => switch (strat) {
3493+
.sema_kit => unreachable,
3494+
.eager => unreachable,
3495+
.lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) },
3496+
},
3497+
};
34823498

34833499
var size: u64 = 0;
34843500
if (code_align > payload_align) {

test/behavior.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ test {
9898
_ = @import("behavior/bugs/12911.zig");
9999
_ = @import("behavior/bugs/12928.zig");
100100
_ = @import("behavior/bugs/12945.zig");
101+
_ = @import("behavior/bugs/12984.zig");
101102
_ = @import("behavior/byteswap.zig");
102103
_ = @import("behavior/byval_arg_var.zig");
103104
_ = @import("behavior/call.zig");

test/behavior/bugs/12984.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
const builtin = @import("builtin");
3+
4+
pub fn DeleagateWithContext(comptime Function: type) type {
5+
const ArgArgs = std.meta.ArgsTuple(Function);
6+
return struct {
7+
t: ArgArgs,
8+
};
9+
}
10+
11+
pub const OnConfirm = DeleagateWithContext(fn (bool) void);
12+
pub const CustomDraw = DeleagateWithContext(fn (?OnConfirm) void);
13+
14+
test "simple test" {
15+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
16+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
17+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
18+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
19+
20+
var c: CustomDraw = undefined;
21+
_ = c;
22+
}

test/behavior/slice.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,31 @@ test "slice len modification at comptime" {
684684
try expect(items[1] == 1);
685685
}
686686
}
687+
688+
test "slice field ptr const" {
689+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
690+
691+
const const_slice: []const u8 = "string";
692+
693+
const const_ptr_const_slice = &const_slice;
694+
try expectEqual(*const []const u8, @TypeOf(&const_ptr_const_slice.*));
695+
try expectEqual(*const [*]const u8, @TypeOf(&const_ptr_const_slice.ptr));
696+
697+
var var_ptr_const_slice = &const_slice;
698+
try expectEqual(*const []const u8, @TypeOf(&var_ptr_const_slice.*));
699+
try expectEqual(*const [*]const u8, @TypeOf(&var_ptr_const_slice.ptr));
700+
}
701+
702+
test "slice field ptr var" {
703+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
704+
705+
var var_slice: []const u8 = "string";
706+
707+
var var_ptr_var_slice = &var_slice;
708+
try expectEqual(*[]const u8, @TypeOf(&var_ptr_var_slice.*));
709+
try expectEqual(*[*]const u8, @TypeOf(&var_ptr_var_slice.ptr));
710+
711+
const const_ptr_var_slice = &var_slice;
712+
try expectEqual(*[]const u8, @TypeOf(&const_ptr_var_slice.*));
713+
try expectEqual(*[*]const u8, @TypeOf(&const_ptr_var_slice.ptr));
714+
}

test/cases/aarch64-macos/hello_world_with_updates.0.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
// output_mode=Exe
33
// target=aarch64-macos
44
//
5-
// :109:9: error: struct 'tmp.tmp' has no member named 'main'
6-
// :7:1: note: struct declared here
5+
// :109:9: error: root struct of file 'tmp' has no member named 'main'

test/cases/compile_errors/bogus_compile_var.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ export fn entry() usize { return @sizeOf(@TypeOf(x)); }
55
// backend=stage2
66
// target=native
77
//
8-
// :1:29: error: struct 'builtin.builtin' has no member named 'bogus'
9-
// :1:1: note: struct declared here
8+
// :1:29: error: root struct of file 'builtin' has no member named 'bogus'

test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export fn entry() void {
77
// backend=stage2
88
// target=native
99
//
10-
// :2:27: error: expected type 'u32', found 'tmp.tmp'
10+
// :2:27: error: expected type 'u32', found 'tmp'
1111
// :1:1: note: struct declared here

test/cases/x86_64-linux/hello_world_with_updates.0.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
// output_mode=Exe
33
// target=x86_64-linux
44
//
5-
// :109:9: error: struct 'tmp.tmp' has no member named 'main'
6-
// :7:1: note: struct declared here
5+
// :109:9: error: root struct of file 'tmp' has no member named 'main'

test/cases/x86_64-macos/hello_world_with_updates.0.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
// output_mode=Exe
33
// target=x86_64-macos
44
//
5-
// :109:9: error: struct 'tmp.tmp' has no member named 'main'
6-
// :7:1: note: struct declared here
5+
// :109:9: error: root struct of file 'tmp' has no member named 'main'

test/cases/x86_64-windows/hello_world_with_updates.0.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
// output_mode=Exe
33
// target=x86_64-windows
44
//
5-
// :130:9: error: struct 'tmp.tmp' has no member named 'main'
6-
// :7:1: note: struct declared here
5+
// :130:9: error: root struct of file 'tmp' has no member named 'main'

0 commit comments

Comments
 (0)