Skip to content

Commit 9d4561e

Browse files
committed
AstGen: detect declarations shadowing locals
Closes #9355
1 parent c17793b commit 9d4561e

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

lib/std/bounded_array.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/// ```
18-
pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
18+
pub 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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ noinline fn showMyTrace() usize {
19471947
/// For more advanced usage, see `ConfigurableTrace`.
19481948
pub const Trace = ConfigurableTrace(2, 4, builtin.mode == .Debug);
19491949

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 {
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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)