authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-07-14 18:17:55-04:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-07-14 18:17:55-04:00
log1799455e0587644c98cdba67bd10a59a2d45b116
tree6539087c26b1984a0fc3ed391efdb1955d79bf01
parent759d1d9aeffbe1c5eaf7d5675b6d1c165757cdf1

astgen: errors for shadowing in if captures


2 files changed, 30 insertions(+), 0 deletions(-)

src/AstGen.zig+3
...@@ -5008,6 +5008,7 @@ fn ifExpr(...@@ -5008,6 +5008,7 @@ fn ifExpr(
5008 const token_name_str = tree.tokenSlice(token_name_index);5008 const token_name_str = tree.tokenSlice(token_name_index);
5009 if (mem.eql(u8, "_", token_name_str))5009 if (mem.eql(u8, "_", token_name_str))
5010 break :s &then_scope.base;5010 break :s &then_scope.base;
5011 try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index);
5011 payload_val_scope = .{5012 payload_val_scope = .{
5012 .parent = &then_scope.base,5013 .parent = &then_scope.base,
5013 .gen_zir = &then_scope,5014 .gen_zir = &then_scope,
...@@ -5030,6 +5031,7 @@ fn ifExpr(...@@ -5030,6 +5031,7 @@ fn ifExpr(
5030 break :s &then_scope.base;5031 break :s &then_scope.base;
5031 const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);5032 const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);
5032 const ident_name = try astgen.identAsString(ident_token);5033 const ident_name = try astgen.identAsString(ident_token);
5034 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
5033 payload_val_scope = .{5035 payload_val_scope = .{
5034 .parent = &then_scope.base,5036 .parent = &then_scope.base,
5035 .gen_zir = &then_scope,5037 .gen_zir = &then_scope,
...@@ -5071,6 +5073,7 @@ fn ifExpr(...@@ -5071,6 +5073,7 @@ fn ifExpr(
5071 const error_token_str = tree.tokenSlice(error_token);5073 const error_token_str = tree.tokenSlice(error_token);
5072 if (mem.eql(u8, "_", error_token_str))5074 if (mem.eql(u8, "_", error_token_str))
5073 break :s &else_scope.base;5075 break :s &else_scope.base;
5076 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
5074 payload_val_scope = .{5077 payload_val_scope = .{
5075 .parent = &else_scope.base,5078 .parent = &else_scope.base,
5076 .gen_zir = &else_scope,5079 .gen_zir = &else_scope,
test/cases.zig+27
...@@ -1108,6 +1108,33 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1108,6 +1108,33 @@ pub fn addCases(ctx: *TestContext) !void {
1108 ":5:13: error: redeclaration of local variable 'i'",1108 ":5:13: error: redeclaration of local variable 'i'",
1109 ":2:9: note: previous declaration here",1109 ":2:9: note: previous declaration here",
1110 });1110 });
1111 case.addError(
1112 \\pub fn main() void {
1113 \\ var i = 0;
1114 \\ if (true) |i| {}
1115 \\}
1116 , &[_][]const u8{
1117 ":3:16: error: redeclaration of local variable 'i'",
1118 ":2:9: note: previous declaration here",
1119 });
1120 case.addError(
1121 \\pub fn main() void {
1122 \\ var i = 0;
1123 \\ if (true) |i| {} else |e| {}
1124 \\}
1125 , &[_][]const u8{
1126 ":3:16: error: redeclaration of local variable 'i'",
1127 ":2:9: note: previous declaration here",
1128 });
1129 case.addError(
1130 \\pub fn main() void {
1131 \\ var i = 0;
1132 \\ if (true) |_| {} else |i| {}
1133 \\}
1134 , &[_][]const u8{
1135 ":3:28: error: redeclaration of local variable 'i'",
1136 ":2:9: note: previous declaration here",
1137 });
1111 }1138 }
11121139
1113 {1140 {