authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-07-14 18:03:15-04:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-07-14 18:03:15-04:00
log759d1d9aeffbe1c5eaf7d5675b6d1c165757cdf1
tree8e35f1531d3967705a23af6453929b75d9d34d95
parent132b18e2b39feca3b90b1b13df2b4649b1661fd5

astgen: errors for shadowing in loop captures


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

src/AstGen.zig+7-1
...@@ -5264,7 +5264,9 @@ fn whileExpr(...@@ -5264,7 +5264,9 @@ fn whileExpr(
5264 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;5264 const ident_token = if (payload_is_ref) payload_token + 1 else payload_token;
5265 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))5265 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
5266 break :s &then_scope.base;5266 break :s &then_scope.base;
5267 const ident_name = try astgen.identAsString(payload_token + @boolToInt(payload_is_ref));5267 const payload_name_loc = payload_token + @boolToInt(payload_is_ref);
5268 const ident_name = try astgen.identAsString(payload_name_loc);
5269 try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc);
5268 payload_val_scope = .{5270 payload_val_scope = .{
5269 .parent = &then_scope.base,5271 .parent = &then_scope.base,
5270 .gen_zir = &then_scope,5272 .gen_zir = &then_scope,
...@@ -5287,6 +5289,7 @@ fn whileExpr(...@@ -5287,6 +5289,7 @@ fn whileExpr(
5287 const ident_name = try astgen.identAsString(ident_token);5289 const ident_name = try astgen.identAsString(ident_token);
5288 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))5290 if (mem.eql(u8, "_", tree.tokenSlice(ident_token)))
5289 break :s &then_scope.base;5291 break :s &then_scope.base;
5292 try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token);
5290 payload_val_scope = .{5293 payload_val_scope = .{
5291 .parent = &then_scope.base,5294 .parent = &then_scope.base,
5292 .gen_zir = &then_scope,5295 .gen_zir = &then_scope,
...@@ -5344,6 +5347,7 @@ fn whileExpr(...@@ -5344,6 +5347,7 @@ fn whileExpr(
5344 const ident_name = try astgen.identAsString(error_token);5347 const ident_name = try astgen.identAsString(error_token);
5345 if (mem.eql(u8, tree.tokenSlice(error_token), "_"))5348 if (mem.eql(u8, tree.tokenSlice(error_token), "_"))
5346 break :s &else_scope.base;5349 break :s &else_scope.base;
5350 try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token);
5347 payload_val_scope = .{5351 payload_val_scope = .{
5348 .parent = &else_scope.base,5352 .parent = &else_scope.base,
5349 .gen_zir = &else_scope,5353 .gen_zir = &else_scope,
...@@ -5483,6 +5487,7 @@ fn forExpr(...@@ -5483,6 +5487,7 @@ fn forExpr(
5483 const name_str_index = try astgen.identAsString(ident);5487 const name_str_index = try astgen.identAsString(ident);
5484 const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;5488 const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val;
5485 const payload_inst = try then_scope.addBin(tag, array_ptr, index);5489 const payload_inst = try then_scope.addBin(tag, array_ptr, index);
5490 try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident);
5486 payload_val_scope = .{5491 payload_val_scope = .{
5487 .parent = &then_scope.base,5492 .parent = &then_scope.base,
5488 .gen_zir = &then_scope,5493 .gen_zir = &then_scope,
...@@ -5506,6 +5511,7 @@ fn forExpr(...@@ -5506,6 +5511,7 @@ fn forExpr(
5506 return astgen.failTok(index_token, "discard of index capture; omit it instead", .{});5511 return astgen.failTok(index_token, "discard of index capture; omit it instead", .{});
5507 }5512 }
5508 const index_name = try astgen.identAsString(index_token);5513 const index_name = try astgen.identAsString(index_token);
5514 try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token);
5509 index_scope = .{5515 index_scope = .{
5510 .parent = payload_sub_scope,5516 .parent = payload_sub_scope,
5511 .gen_zir = &then_scope,5517 .gen_zir = &then_scope,
test/cases.zig+43
...@@ -1065,6 +1065,49 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1065,6 +1065,49 @@ 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 });
1068 }1111 }
10691112
1070 {1113 {