| author | |
| committer | |
| log | a12abc6d6c8b89a09befdcbd9019247ccc3bd641 |
| tree | 55d95c1c99ddebc36cb4877b52b28ea406c73e5b |
| parent | 9d85335de9f89213ed0c3f6a1ba4be1e02d186a1 |
| parent | 9d4561ef0082728bbba085e8a4689ccc93a37b27 |
| signature |
Stage2 namespacing fixes6 files changed, 103 insertions(+), 9 deletions(-)
lib/std/bounded_array.zig+7-7| ... | ... | @@ -15,16 +15,16 @@ const testing = std.testing; |
| 15 | 15 | /// var slice = a.slice(); // a slice of the 64-byte array |
| 16 | 16 | /// var a_clone = a; // creates a copy - the structure doesn't use any internal pointers |
| 17 | 17 | /// ``` |
| 18 | pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { | |
| 18 | pub fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type { | |
| 19 | 19 | return struct { |
| 20 | 20 | const Self = @This(); |
| 21 | buffer: [capacity]T = undefined, | |
| 21 | buffer: [buffer_capacity]T = undefined, | |
| 22 | 22 | len: usize = 0, |
| 23 | 23 | |
| 24 | 24 | /// Set the actual length of the slice. |
| 25 | 25 | /// Returns error.Overflow if it exceeds the length of the backing array. |
| 26 | 26 | pub fn init(len: usize) error{Overflow}!Self { |
| 27 | if (len > capacity) return error.Overflow; | |
| 27 | if (len > buffer_capacity) return error.Overflow; | |
| 28 | 28 | return Self{ .len = len }; |
| 29 | 29 | } |
| 30 | 30 | |
| ... | ... | @@ -41,7 +41,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 41 | 41 | /// Adjust the slice's length to `len`. |
| 42 | 42 | /// Does not initialize added items if any. |
| 43 | 43 | 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; | |
| 45 | 45 | self.len = len; |
| 46 | 46 | } |
| 47 | 47 | |
| ... | ... | @@ -69,7 +69,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 69 | 69 | |
| 70 | 70 | /// Check that the slice can hold at least `additional_count` items. |
| 71 | 71 | 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) { | |
| 73 | 73 | return error.Overflow; |
| 74 | 74 | } |
| 75 | 75 | } |
| ... | ... | @@ -83,7 +83,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 83 | 83 | /// Increase length by 1, returning pointer to the new item. |
| 84 | 84 | /// Asserts that there is space for the new item. |
| 85 | 85 | pub fn addOneAssumeCapacity(self: *Self) *T { |
| 86 | assert(self.len < capacity); | |
| 86 | assert(self.len < buffer_capacity); | |
| 87 | 87 | self.len += 1; |
| 88 | 88 | return &self.slice()[self.len - 1]; |
| 89 | 89 | } |
| ... | ... | @@ -236,7 +236,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type { |
| 236 | 236 | pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { |
| 237 | 237 | const old_len = self.len; |
| 238 | 238 | self.len += n; |
| 239 | assert(self.len <= capacity); | |
| 239 | assert(self.len <= buffer_capacity); | |
| 240 | 240 | mem.set(T, self.slice()[old_len..self.len], value); |
| 241 | 241 | } |
| 242 | 242 |
lib/std/debug.zig+2-2| ... | ... | @@ -1947,7 +1947,7 @@ noinline fn showMyTrace() usize { |
| 1947 | 1947 | /// For more advanced usage, see `ConfigurableTrace`. |
| 1948 | 1948 | pub const Trace = ConfigurableTrace(2, 4, builtin.mode == .Debug); |
| 1949 | 1949 | |
| 1950 | pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime enabled: bool) type { | |
| 1950 | pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime is_enabled: bool) type { | |
| 1951 | 1951 | return struct { |
| 1952 | 1952 | addrs: [actual_size][stack_frame_count]usize = undefined, |
| 1953 | 1953 | notes: [actual_size][]const u8 = undefined, |
| ... | ... | @@ -1956,7 +1956,7 @@ pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize |
| 1956 | 1956 | const actual_size = if (enabled) size else 0; |
| 1957 | 1957 | const Index = if (enabled) usize else u0; |
| 1958 | 1958 | |
| 1959 | pub const enabled = enabled; | |
| 1959 | pub const enabled = is_enabled; | |
| 1960 | 1960 | |
| 1961 | 1961 | pub const add = if (enabled) addNoInline else addNoOp; |
| 1962 | 1962 |
src/AstGen.zig+40| ... | ... | @@ -11751,6 +11751,46 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast. |
| 11751 | 11751 | error.OutOfMemory => return error.OutOfMemory, |
| 11752 | 11752 | } |
| 11753 | 11753 | } |
| 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 | }; | |
| 11754 | 11794 | gop.value_ptr.* = member_node; |
| 11755 | 11795 | } |
| 11756 | 11796 | return decl_count; |
src/Sema.zig+11| ... | ... | @@ -5384,6 +5384,17 @@ fn lookupInNamespace( |
| 5384 | 5384 | } |
| 5385 | 5385 | } |
| 5386 | 5386 | |
| 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 | ||
| 5387 | 5398 | switch (candidates.items.len) { |
| 5388 | 5399 | 0 => {}, |
| 5389 | 5400 | 1 => { |
test/behavior/basic.zig+21| ... | ... | @@ -1104,3 +1104,24 @@ test "namespace lookup ignores decl causing the lookup" { |
| 1104 | 1104 | }; |
| 1105 | 1105 | _ = S.foo(); |
| 1106 | 1106 | } |
| 1107 | ||
| 1108 | test "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 @@ |
| 1 | fn foo(a: usize) void { | |
| 2 | struct { | |
| 3 | const a = 1; | |
| 4 | }; | |
| 5 | } | |
| 6 | fn 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 |