astgen: errors for shadowing in if captures

This commit is contained in:
Jacob G-W 2021-07-14 18:17:55 -04:00
parent 759d1d9aef
commit 1799455e05
2 changed files with 30 additions and 0 deletions

View File

@ -5008,6 +5008,7 @@ fn ifExpr(
const token_name_str = tree.tokenSlice(token_name_index); const token_name_str = tree.tokenSlice(token_name_index);
if (mem.eql(u8, "_", token_name_str)) if (mem.eql(u8, "_", token_name_str))
break :s &then_scope.base; break :s &then_scope.base;
try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index);
payload_val_scope = .{ payload_val_scope = .{
.parent = &then_scope.base, .parent = &then_scope.base,
.gen_zir = &then_scope, .gen_zir = &then_scope,
@ -5030,6 +5031,7 @@ fn ifExpr(
break :s &then_scope.base; break :s &then_scope.base;
const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);
const ident_name = try astgen.identAsString(ident_token); const ident_name = try astgen.identAsString(ident_token);
try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
payload_val_scope = .{ payload_val_scope = .{
.parent = &then_scope.base, .parent = &then_scope.base,
.gen_zir = &then_scope, .gen_zir = &then_scope,
@ -5071,6 +5073,7 @@ fn ifExpr(
const error_token_str = tree.tokenSlice(error_token); const error_token_str = tree.tokenSlice(error_token);
if (mem.eql(u8, "_", error_token_str)) if (mem.eql(u8, "_", error_token_str))
break :s &else_scope.base; break :s &else_scope.base;
try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
payload_val_scope = .{ payload_val_scope = .{
.parent = &else_scope.base, .parent = &else_scope.base,
.gen_zir = &else_scope, .gen_zir = &else_scope,

View File

@ -1108,6 +1108,33 @@ pub fn addCases(ctx: *TestContext) !void {
":5:13: error: redeclaration of local variable 'i'", ":5:13: error: redeclaration of local variable 'i'",
":2:9: note: previous declaration here", ":2:9: note: previous declaration here",
}); });
case.addError(
\\pub fn main() void {
\\ var i = 0;
\\ if (true) |i| {}
\\}
, &[_][]const u8{
":3:16: error: redeclaration of local variable 'i'",
":2:9: note: previous declaration here",
});
case.addError(
\\pub fn main() void {
\\ var i = 0;
\\ if (true) |i| {} else |e| {}
\\}
, &[_][]const u8{
":3:16: error: redeclaration of local variable 'i'",
":2:9: note: previous declaration here",
});
case.addError(
\\pub fn main() void {
\\ var i = 0;
\\ if (true) |_| {} else |i| {}
\\}
, &[_][]const u8{
":3:28: error: redeclaration of local variable 'i'",
":2:9: note: previous declaration here",
});
} }
{ {