authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-16 19:57:22-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-16 19:57:22-04:00
loga12abc6d6c8b89a09befdcbd9019247ccc3bd641
tree55d95c1c99ddebc36cb4877b52b28ea406c73e5b
parent9d85335de9f89213ed0c3f6a1ba4be1e02d186a1
parent9d4561ef0082728bbba085e8a4689ccc93a37b27
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12456 from Vexu/stage2

Stage2 namespacing fixes

6 files changed, 103 insertions(+), 9 deletions(-)

lib/std/bounded_array.zig+7-7
......@@ -15,16 +15,16 @@ const testing = std.testing;
1515/// var slice = a.slice(); // a slice of the 64-byte array
1616/// var a_clone = a; // creates a copy - the structure doesn't use any internal pointers
1717/// ```
18pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
18pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type {
1919 return struct {
2020 const Self = @This();
21 buffer: [capacity]T = undefined,
21 buffer: [buffer_capacity]T = undefined,
2222 len: usize = 0,
2323
2424 /// Set the actual length of the slice.
2525 /// Returns error.Overflow if it exceeds the length of the backing array.
2626 pub fn init(len: usize) error{Overflow}!Self {
27 if (len > capacity) return error.Overflow;
27 if (len > buffer_capacity) return error.Overflow;
2828 return Self{ .len = len };
2929 }
3030
......@@ -41,7 +41,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
4141 /// Adjust the slice's length to `len`.
4242 /// Does not initialize added items if any.
4343 pub fn resize(self: *Self, len: usize) error{Overflow}!void {
44 if (len > capacity) return error.Overflow;
44 if (len > buffer_capacity) return error.Overflow;
4545 self.len = len;
4646 }
4747
......@@ -69,7 +69,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
6969
7070 /// Check that the slice can hold at least `additional_count` items.
7171 pub fn ensureUnusedCapacity(self: Self, additional_count: usize) error{Overflow}!void {
72 if (self.len + additional_count > capacity) {
72 if (self.len + additional_count > buffer_capacity) {
7373 return error.Overflow;
7474 }
7575 }
......@@ -83,7 +83,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
8383 /// Increase length by 1, returning pointer to the new item.
8484 /// Asserts that there is space for the new item.
8585 pub fn addOneAssumeCapacity(self: *Self) *T {
86 assert(self.len < capacity);
86 assert(self.len < buffer_capacity);
8787 self.len += 1;
8888 return &self.slice()[self.len - 1];
8989 }
......@@ -236,7 +236,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
236236 pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void {
237237 const old_len = self.len;
238238 self.len += n;
239 assert(self.len <= capacity);
239 assert(self.len <= buffer_capacity);
240240 mem.set(T, self.slice()[old_len..self.len], value);
241241 }
242242
lib/std/debug.zig+2-2
......@@ -1947,7 +1947,7 @@ noinline fn showMyTrace() usize {
19471947/// For more advanced usage, see `ConfigurableTrace`.
19481948pub const Trace = ConfigurableTrace(2, 4, builtin.mode == .Debug);
19491949
1950pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime enabled: bool) type {
1950pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime is_enabled: bool) type {
19511951 return struct {
19521952 addrs: [actual_size][stack_frame_count]usize = undefined,
19531953 notes: [actual_size][]const u8 = undefined,
......@@ -1956,7 +1956,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize
19561956 const actual_size = if (enabled) size else 0;
19571957 const Index = if (enabled) usize else u0;
19581958
1959 pub const enabled = enabled;
1959 pub const enabled = is_enabled;
19601960
19611961 pub const add = if (enabled) addNoInline else addNoOp;
19621962
src/AstGen.zig+40
......@@ -11751,6 +11751,46 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
1175111751 error.OutOfMemory => return error.OutOfMemory,
1175211752 }
1175311753 }
11754
11755 // const index_name = try astgen.identAsString(index_token);
11756 var s = namespace.parent;
11757 while (true) switch (s.tag) {
11758 .local_val => {
11759 const local_val = s.cast(Scope.LocalVal).?;
11760 if (local_val.name == name_str_index) {
11761 return astgen.failTokNotes(name_token, "redeclaration of {s} '{s}'", .{
11762 @tagName(local_val.id_cat), token_bytes,
11763 }, &[_]u32{
11764 try astgen.errNoteTok(
11765 local_val.token_src,
11766 "previous declaration here",
11767 .{},
11768 ),
11769 });
11770 }
11771 s = local_val.parent;
11772 },
11773 .local_ptr => {
11774 const local_ptr = s.cast(Scope.LocalPtr).?;
11775 if (local_ptr.name == name_str_index) {
11776 return astgen.failTokNotes(name_token, "redeclaration of {s} '{s}'", .{
11777 @tagName(local_ptr.id_cat), token_bytes,
11778 }, &[_]u32{
11779 try astgen.errNoteTok(
11780 local_ptr.token_src,
11781 "previous declaration here",
11782 .{},
11783 ),
11784 });
11785 }
11786 s = local_ptr.parent;
11787 },
11788 .namespace => s = s.cast(Scope.Namespace).?.parent,
11789 .gen_zir => s = s.cast(GenZir).?.parent,
11790 .defer_normal, .defer_error => s = s.cast(Scope.Defer).?.parent,
11791 .defer_gen => s = s.cast(Scope.DeferGen).?.parent,
11792 .top => break,
11793 };
1175411794 gop.value_ptr.* = member_node;
1175511795 }
1175611796 return decl_count;
src/Sema.zig+11
......@@ -5384,6 +5384,17 @@ fn lookupInNamespace(
53845384 }
53855385 }
53865386
5387 {
5388 var i: usize = 0;
5389 while (i < candidates.items.len) {
5390 if (candidates.items[i] == sema.owner_decl_index) {
5391 _ = candidates.orderedRemove(i);
5392 } else {
5393 i += 1;
5394 }
5395 }
5396 }
5397
53875398 switch (candidates.items.len) {
53885399 0 => {},
53895400 1 => {
test/behavior/basic.zig+21
......@@ -1104,3 +1104,24 @@ test "namespace lookup ignores decl causing the lookup" {
11041104 };
11051105 _ = S.foo();
11061106}
1107
1108test "ambiguous reference error ignores current declaration" {
1109 const S = struct {
1110 const foo = 666;
1111
1112 const a = @This();
1113 const b = struct {
1114 const foo = a.foo;
1115 const bar = struct {
1116 bar: u32 = b.foo,
1117 };
1118
1119 comptime {
1120 _ = b.foo;
1121 }
1122 };
1123
1124 usingnamespace b;
1125 };
1126 try expect(S.b.foo == 666);
1127}
test/cases/compile_errors/decl_shadows_local.zig created+22
......@@ -0,0 +1,22 @@
1fn foo(a: usize) void {
2 struct {
3 const a = 1;
4 };
5}
6fn bar(a: usize) void {
7 struct {
8 const b = struct {
9 const a = 1;
10 };
11 };
12 _ = a;
13}
14
15// error
16// backend=stage2
17// target=native
18//
19// :3:15: error: redeclaration of function parameter 'a'
20// :1:8: note: previous declaration here
21// :9:19: error: redeclaration of function parameter 'a'
22// :6:8: note: previous declaration here