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
signaturebadge-question-mark 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(...@@ -5009,6 +5009,7 @@ fn ifExpr(
5009 const token_name_str = tree.tokenSlice(token_name_index);5009 const token_name_str = tree.tokenSlice(token_name_index);
5010 if (mem.eql(u8, "_", token_name_str))5010 if (mem.eql(u8, "_", token_name_str))
5011 break :s &then_scope.base;5011 break :s &then_scope.base;
5012 try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index);
5012 payload_val_scope = .{5013 payload_val_scope = .{
5013 .parent = &then_scope.base,5014 .parent = &then_scope.base,
5014 .gen_zir = &then_scope,5015 .gen_zir = &then_scope,
...@@ -5031,6 +5032,7 @@ fn ifExpr(...@@ -5031,6 +5032,7 @@ fn ifExpr(
5031 break :s &then_scope.base;5032 break :s &then_scope.base;
5032 const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);5033 const payload_inst = try then_scope.addUnNode(tag, cond.inst, node);
5033 const ident_name = try astgen.identAsString(ident_token);5034 const ident_name = try astgen.identAsString(ident_token);
5035 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
5034 payload_val_scope = .{5036 payload_val_scope = .{
5035 .parent = &then_scope.base,5037 .parent = &then_scope.base,
5036 .gen_zir = &then_scope,5038 .gen_zir = &then_scope,
...@@ -5072,6 +5074,7 @@ fn ifExpr(...@@ -5072,6 +5074,7 @@ fn ifExpr(
5072 const error_token_str = tree.tokenSlice(error_token);5074 const error_token_str = tree.tokenSlice(error_token);
5073 if (mem.eql(u8, "_", error_token_str))5075 if (mem.eql(u8, "_", error_token_str))
5074 break :s &else_scope.base;5076 break :s &else_scope.base;
5077 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
5075 payload_val_scope = .{5078 payload_val_scope = .{
5076 .parent = &else_scope.base,5079 .parent = &else_scope.base,
5077 .gen_zir = &else_scope,5080 .gen_zir = &else_scope,
...@@ -5265,7 +5268,9 @@ fn whileExpr(...@@ -5265,7 +5268,9 @@ fn whileExpr(
5265 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;5268 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;
5266 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))5269 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
5267 break :s &then_scope.base;5270 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);
5269 payload_val_scope = .{5274 payload_val_scope = .{
5270 .parent = &then_scope.base,5275 .parent = &then_scope.base,
5271 .gen_zir = &then_scope,5276 .gen_zir = &then_scope,
...@@ -5288,6 +5293,7 @@ fn whileExpr(...@@ -5288,6 +5293,7 @@ fn whileExpr(
5288 const ident_name = try astgen.identAsString(ident_token);5293 const ident_name = try astgen.identAsString(ident_token);
5289 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))5294 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
5290 break :s &then_scope.base;5295 break :s &then_scope.base;
5296 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
5291 payload_val_scope = .{5297 payload_val_scope = .{
5292 .parent = &then_scope.base,5298 .parent = &then_scope.base,
5293 .gen_zir = &then_scope,5299 .gen_zir = &then_scope,
...@@ -5345,6 +5351,7 @@ fn whileExpr(...@@ -5345,6 +5351,7 @@ fn whileExpr(
5345 const ident_name = try astgen.identAsString(error_token);5351 const ident_name = try astgen.identAsString(error_token);
5346 if (mem.eql(u8, tree.tokenSlice(error_token), "_"))5352 if (mem.eql(u8, tree.tokenSlice(error_token), "_"))
5347 break :s &else_scope.base;5353 break :s &else_scope.base;
5354 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
5348 payload_val_scope = .{5355 payload_val_scope = .{
5349 .parent = &else_scope.base,5356 .parent = &else_scope.base,
5350 .gen_zir = &else_scope,5357 .gen_zir = &else_scope,
...@@ -5484,6 +5491,7 @@ fn forExpr(...@@ -5484,6 +5491,7 @@ fn forExpr(
5484 const name_str_index = try astgen.identAsString(ident);5491 const name_str_index = try astgen.identAsString(ident);
5485 const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;5492 const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;
5486 const payload_inst = try then_scope.addBin(tag, array_ptr, index);5493 const payload_inst = try then_scope.addBin(tag, array_ptr, index);
5494 try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident);
5487 payload_val_scope = .{5495 payload_val_scope = .{
5488 .parent = &then_scope.base,5496 .parent = &then_scope.base,
5489 .gen_zir = &then_scope,5497 .gen_zir = &then_scope,
...@@ -5507,6 +5515,7 @@ fn forExpr(...@@ -5507,6 +5515,7 @@ fn forExpr(
5507 return astgen.failTok(index_token, "discard of index capture; omit it instead", .{});5515 return astgen.failTok(index_token, "discard of index capture; omit it instead", .{});
5508 }5516 }
5509 const index_name = try astgen.identAsString(index_token);5517 const index_name = try astgen.identAsString(index_token);
5518 try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token);
5510 index_scope = .{5519 index_scope = .{
5511 .parent = payload_sub_scope,5520 .parent = payload_sub_scope,
5512 .gen_zir = &then_scope,5521 .gen_zir = &then_scope,
test/cases.zig+70
...@@ -1065,6 +1065,76 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1065,6 +1065,76 @@ pub fn addCases(ctx: *TestContext) !void {
1065 ":5:19: error: redeclaration of local constant 'c'",1065 ":5:19: error: redeclaration of local constant 'c'",
1066 ":4:19: note: previous declaration here",1066 ":4:19: note: previous declaration here",
1067 });1067 });
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 });
1068 }1138 }
10691139
1070 {1140 {