authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-22 22:57:12-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-07-22 22:57:12-04:00
logc56b30f565966358abe8935e1f521f60660000b4
tree3c62522e9f0130c802e39bcdb87ce683e13fe9f2
parent18b8738069268cc913bbae9580d2d170618a2ae9
parent1799455e0587644c98cdba67bd10a59a2d45b116
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9378 from g-w1/loop-shadowing

astgen: errors for shadowing in captures

2 files changed, 80 insertions(+), 1 deletions(-)

src/AstGen.zig+10-1
......@@ -5009,6 +5009,7 @@ fn ifExpr(
50095009 const token_name_str = tree.tokenSlice(token_name_index);
50105010 if (mem.eql(u8, "_", token_name_str))
50115011 break :s &then_scope.base;
5012 try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index);
50125013 payload_val_scope = .{
50135014 .parent = &then_scope.base,
50145015 .gen_zir = &then_scope,
......@@ -5031,6 +5032,7 @@ fn ifExpr(
50315032 break :s &then_scope.base;
50325033 const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);
50335034 const ident_name = try astgen.identAsString(ident_token);
5035 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
50345036 payload_val_scope = .{
50355037 .parent = &then_scope.base,
50365038 .gen_zir = &then_scope,
......@@ -5072,6 +5074,7 @@ fn ifExpr(
50725074 const error_token_str = tree.tokenSlice(error_token);
50735075 if (mem.eql(u8, "_", error_token_str))
50745076 break :s &else_scope.base;
5077 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
50755078 payload_val_scope = .{
50765079 .parent = &else_scope.base,
50775080 .gen_zir = &else_scope,
......@@ -5265,7 +5268,9 @@ fn whileExpr(
52655268 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;
52665269 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
52675270 break :s &then_scope.base;
5268 const ident_name = try astgen.identAsString(payload_token + @boolToInt(payload_is_ref));
5271 const payload_name_loc = payload_token + @boolToInt(payload_is_ref);
5272 const ident_name = try astgen.identAsString(payload_name_loc);
5273 try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc);
52695274 payload_val_scope = .{
52705275 .parent = &then_scope.base,
52715276 .gen_zir = &then_scope,
......@@ -5288,6 +5293,7 @@ fn whileExpr(
52885293 const ident_name = try astgen.identAsString(ident_token);
52895294 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
52905295 break :s &then_scope.base;
5296 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
52915297 payload_val_scope = .{
52925298 .parent = &then_scope.base,
52935299 .gen_zir = &then_scope,
......@@ -5345,6 +5351,7 @@ fn whileExpr(
53455351 const ident_name = try astgen.identAsString(error_token);
53465352 if (mem.eql(u8, tree.tokenSlice(error_token), "_"))
53475353 break :s &else_scope.base;
5354 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
53485355 payload_val_scope = .{
53495356 .parent = &else_scope.base,
53505357 .gen_zir = &else_scope,
......@@ -5484,6 +5491,7 @@ fn forExpr(
54845491 const name_str_index = try astgen.identAsString(ident);
54855492 const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;
54865493 const payload_inst = try then_scope.addBin(tag, array_ptr, index);
5494 try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident);
54875495 payload_val_scope = .{
54885496 .parent = &then_scope.base,
54895497 .gen_zir = &then_scope,
......@@ -5507,6 +5515,7 @@ fn forExpr(
55075515 return astgen.failTok(index_token, "discard of index capture; omit it instead", .{});
55085516 }
55095517 const index_name = try astgen.identAsString(index_token);
5518 try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token);
55105519 index_scope = .{
55115520 .parent = payload_sub_scope,
55125521 .gen_zir = &then_scope,
test/cases.zig+70
......@@ -1065,6 +1065,76 @@ pub fn addCases(ctx: *TestContext) !void {
10651065 ":5:19: error: redeclaration of local constant 'c'",
10661066 ":4:19: note: previous declaration here",
10671067 });
1068 case.addError(
1069 \\pub fn main() void {
1070 \\ var i = 0;
1071 \\ for (n) |_, i| {
1072 \\ }
1073 \\}
1074 , &[_][]const u8{
1075 ":3:17: error: redeclaration of local variable 'i'",
1076 ":2:9: note: previous declaration here",
1077 });
1078 case.addError(
1079 \\pub fn main() void {
1080 \\ var i = 0;
1081 \\ for (n) |i| {
1082 \\ }
1083 \\}
1084 , &[_][]const u8{
1085 ":3:14: error: redeclaration of local variable 'i'",
1086 ":2:9: note: previous declaration here",
1087 });
1088 case.addError(
1089 \\pub fn main() void {
1090 \\ var i = 0;
1091 \\ while (n) |i| {
1092 \\ }
1093 \\}
1094 , &[_][]const u8{
1095 ":3:16: error: redeclaration of local variable 'i'",
1096 ":2:9: note: previous declaration here",
1097 });
1098 case.addError(
1099 \\pub fn main() void {
1100 \\ var i = 0;
1101 \\ while (n) |bruh| {
1102 \\ _ = bruh;
1103 \\ } else |i| {
1104 \\
1105 \\ }
1106 \\}
1107 , &[_][]const u8{
1108 ":5:13: error: redeclaration of local variable 'i'",
1109 ":2:9: note: previous declaration here",
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 });
10681138 }
10691139
10701140 {