| ... | ... | @@ -2385,7 +2385,7 @@ fn varDecl( |
| 2385 | 2385 | } |
| 2386 | 2386 | const ident_name = try astgen.identAsString(name_token); |
| 2387 | 2387 | |
| 2388 | | try astgen.detectLocalShadowing(scope, ident_name, name_token); |
| 2388 | try astgen.detectLocalShadowing(scope, ident_name, name_token, ident_name_raw); |
| 2389 | 2389 | |
| 2390 | 2390 | if (var_decl.ast.init_node == 0) { |
| 2391 | 2391 | return astgen.failNode(node, "variables must be initialized", .{}); |
| ... | ... | @@ -2952,12 +2952,13 @@ fn fnDecl( |
| 2952 | 2952 | } else false; |
| 2953 | 2953 | |
| 2954 | 2954 | const param_name: u32 = if (param.name_token) |name_token| blk: { |
| 2955 | | if (mem.eql(u8, "_", tree.tokenSlice(name_token))) |
| 2955 | const name_bytes = tree.tokenSlice(name_token); |
| 2956 | if (mem.eql(u8, "_", name_bytes)) |
| 2956 | 2957 | break :blk 0; |
| 2957 | 2958 | |
| 2958 | 2959 | const param_name = try astgen.identAsString(name_token); |
| 2959 | 2960 | if (!is_extern) { |
| 2960 | | try astgen.detectLocalShadowing(params_scope, param_name, name_token); |
| 2961 | try astgen.detectLocalShadowing(params_scope, param_name, name_token, name_bytes); |
| 2961 | 2962 | } |
| 2962 | 2963 | break :blk param_name; |
| 2963 | 2964 | } else if (!is_extern) { |
| ... | ... | @@ -5035,7 +5036,7 @@ fn ifExpr( |
| 5035 | 5036 | const token_name_str = tree.tokenSlice(token_name_index); |
| 5036 | 5037 | if (mem.eql(u8, "_", token_name_str)) |
| 5037 | 5038 | break :s &then_scope.base; |
| 5038 | | try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index); |
| 5039 | try astgen.detectLocalShadowing(&then_scope.base, ident_name, token_name_index, token_name_str); |
| 5039 | 5040 | payload_val_scope = .{ |
| 5040 | 5041 | .parent = &then_scope.base, |
| 5041 | 5042 | .gen_zir = &then_scope, |
| ... | ... | @@ -5054,11 +5055,12 @@ fn ifExpr( |
| 5054 | 5055 | .optional_payload_unsafe_ptr |
| 5055 | 5056 | else |
| 5056 | 5057 | .optional_payload_unsafe; |
| 5057 | | if (mem.eql(u8, "_", tree.tokenSlice(ident_token))) |
| 5058 | const ident_bytes = tree.tokenSlice(ident_token); |
| 5059 | if (mem.eql(u8, "_", ident_bytes)) |
| 5058 | 5060 | break :s &then_scope.base; |
| 5059 | 5061 | const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); |
| 5060 | 5062 | const ident_name = try astgen.identAsString(ident_token); |
| 5061 | | try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token); |
| 5063 | try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes); |
| 5062 | 5064 | payload_val_scope = .{ |
| 5063 | 5065 | .parent = &then_scope.base, |
| 5064 | 5066 | .gen_zir = &then_scope, |
| ... | ... | @@ -5100,7 +5102,7 @@ fn ifExpr( |
| 5100 | 5102 | const error_token_str = tree.tokenSlice(error_token); |
| 5101 | 5103 | if (mem.eql(u8, "_", error_token_str)) |
| 5102 | 5104 | break :s &else_scope.base; |
| 5103 | | try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token); |
| 5105 | try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token, error_token_str); |
| 5104 | 5106 | payload_val_scope = .{ |
| 5105 | 5107 | .parent = &else_scope.base, |
| 5106 | 5108 | .gen_zir = &else_scope, |
| ... | ... | @@ -5291,11 +5293,12 @@ fn whileExpr( |
| 5291 | 5293 | .err_union_payload_unsafe; |
| 5292 | 5294 | const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); |
| 5293 | 5295 | const ident_token = if (payload_is_ref) payload_token + 1 else payload_token; |
| 5294 | | if (mem.eql(u8, "_", tree.tokenSlice(ident_token))) |
| 5296 | const ident_bytes = tree.tokenSlice(ident_token); |
| 5297 | if (mem.eql(u8, "_", ident_bytes)) |
| 5295 | 5298 | break :s &then_scope.base; |
| 5296 | 5299 | const payload_name_loc = payload_token + @boolToInt(payload_is_ref); |
| 5297 | 5300 | const ident_name = try astgen.identAsString(payload_name_loc); |
| 5298 | | try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc); |
| 5301 | try astgen.detectLocalShadowing(&then_scope.base, ident_name, payload_name_loc, ident_bytes); |
| 5299 | 5302 | payload_val_scope = .{ |
| 5300 | 5303 | .parent = &then_scope.base, |
| 5301 | 5304 | .gen_zir = &then_scope, |
| ... | ... | @@ -5316,9 +5319,10 @@ fn whileExpr( |
| 5316 | 5319 | .optional_payload_unsafe; |
| 5317 | 5320 | const payload_inst = try then_scope.addUnNode(tag, cond.inst, node); |
| 5318 | 5321 | const ident_name = try astgen.identAsString(ident_token); |
| 5319 | | if (mem.eql(u8, "_", tree.tokenSlice(ident_token))) |
| 5322 | const ident_bytes = tree.tokenSlice(ident_token); |
| 5323 | if (mem.eql(u8, "_", ident_bytes)) |
| 5320 | 5324 | break :s &then_scope.base; |
| 5321 | | try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token); |
| 5325 | try astgen.detectLocalShadowing(&then_scope.base, ident_name, ident_token, ident_bytes); |
| 5322 | 5326 | payload_val_scope = .{ |
| 5323 | 5327 | .parent = &then_scope.base, |
| 5324 | 5328 | .gen_zir = &then_scope, |
| ... | ... | @@ -5374,9 +5378,10 @@ fn whileExpr( |
| 5374 | 5378 | .err_union_code; |
| 5375 | 5379 | const payload_inst = try else_scope.addUnNode(tag, cond.inst, node); |
| 5376 | 5380 | const ident_name = try astgen.identAsString(error_token); |
| 5377 | | if (mem.eql(u8, tree.tokenSlice(error_token), "_")) |
| 5381 | const ident_bytes = tree.tokenSlice(error_token); |
| 5382 | if (mem.eql(u8, ident_bytes, "_")) |
| 5378 | 5383 | break :s &else_scope.base; |
| 5379 | | try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token); |
| 5384 | try astgen.detectLocalShadowing(&else_scope.base, ident_name, error_token, ident_bytes); |
| 5380 | 5385 | payload_val_scope = .{ |
| 5381 | 5386 | .parent = &else_scope.base, |
| 5382 | 5387 | .gen_zir = &else_scope, |
| ... | ... | @@ -5523,7 +5528,7 @@ fn forExpr( |
| 5523 | 5528 | const name_str_index = try astgen.identAsString(ident); |
| 5524 | 5529 | const tag: Zir.Inst.Tag = if (is_ptr) .elem_ptr else .elem_val; |
| 5525 | 5530 | const payload_inst = try then_scope.addBin(tag, array_ptr, index); |
| 5526 | | try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident); |
| 5531 | try astgen.detectLocalShadowing(&then_scope.base, name_str_index, ident, value_name); |
| 5527 | 5532 | payload_val_scope = .{ |
| 5528 | 5533 | .parent = &then_scope.base, |
| 5529 | 5534 | .gen_zir = &then_scope, |
| ... | ... | @@ -5543,11 +5548,12 @@ fn forExpr( |
| 5543 | 5548 | ident + 2 |
| 5544 | 5549 | else |
| 5545 | 5550 | break :blk payload_sub_scope; |
| 5546 | | if (mem.eql(u8, tree.tokenSlice(index_token), "_")) { |
| 5551 | const token_bytes = tree.tokenSlice(index_token); |
| 5552 | if (mem.eql(u8, token_bytes, "_")) { |
| 5547 | 5553 | return astgen.failTok(index_token, "discard of index capture; omit it instead", .{}); |
| 5548 | 5554 | } |
| 5549 | 5555 | const index_name = try astgen.identAsString(index_token); |
| 5550 | | try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token); |
| 5556 | try astgen.detectLocalShadowing(payload_sub_scope, index_name, index_token, token_bytes); |
| 5551 | 5557 | index_scope = .{ |
| 5552 | 5558 | .parent = payload_sub_scope, |
| 5553 | 5559 | .gen_zir = &then_scope, |
| ... | ... | @@ -10059,7 +10065,7 @@ fn declareNewName( |
| 10059 | 10065 | const ns = scope.cast(Scope.Namespace).?; |
| 10060 | 10066 | const gop = try ns.decls.getOrPut(gpa, name_index); |
| 10061 | 10067 | if (gop.found_existing) { |
| 10062 | | const name = try gpa.dupe(u8, mem.spanZ(astgen.nullTerminatedString(name_index))); |
| 10068 | const name = try gpa.dupe(u8, mem.span(astgen.nullTerminatedString(name_index))); |
| 10063 | 10069 | defer gpa.free(name); |
| 10064 | 10070 | return astgen.failNodeNotes(node, "redeclaration of '{s}'", .{ |
| 10065 | 10071 | name, |
| ... | ... | @@ -10094,13 +10100,17 @@ fn detectLocalShadowing( |
| 10094 | 10100 | scope: *Scope, |
| 10095 | 10101 | ident_name: u32, |
| 10096 | 10102 | name_token: ast.TokenIndex, |
| 10103 | token_bytes: []const u8, |
| 10097 | 10104 | ) !void { |
| 10098 | 10105 | const gpa = astgen.gpa; |
| 10099 | | const name_slice = mem.spanZ(astgen.nullTerminatedString(ident_name)); |
| 10100 | | if (isPrimitive(name_slice)) { |
| 10101 | | const name = try gpa.dupe(u8, name_slice); |
| 10102 | | defer gpa.free(name); |
| 10103 | | return astgen.failTok(name_token, "local shadows primitive '{s}'", .{name}); |
| 10106 | if (token_bytes[0] != '@' and isPrimitive(token_bytes)) { |
| 10107 | return astgen.failTokNotes(name_token, "name shadows primitive '{s}'", .{ |
| 10108 | token_bytes, |
| 10109 | }, &[_]u32{ |
| 10110 | try astgen.errNoteTok(name_token, "consider using @\"{s}\" to disambiguate", .{ |
| 10111 | token_bytes, |
| 10112 | }), |
| 10113 | }); |
| 10104 | 10114 | } |
| 10105 | 10115 | |
| 10106 | 10116 | var s = scope; |
| ... | ... | @@ -10108,6 +10118,7 @@ fn detectLocalShadowing( |
| 10108 | 10118 | .local_val => { |
| 10109 | 10119 | const local_val = s.cast(Scope.LocalVal).?; |
| 10110 | 10120 | if (local_val.name == ident_name) { |
| 10121 | const name_slice = mem.span(astgen.nullTerminatedString(ident_name)); |
| 10111 | 10122 | const name = try gpa.dupe(u8, name_slice); |
| 10112 | 10123 | defer gpa.free(name); |
| 10113 | 10124 | return astgen.failTokNotes(name_token, "redeclaration of {s} '{s}'", .{ |
| ... | ... | @@ -10125,6 +10136,7 @@ fn detectLocalShadowing( |
| 10125 | 10136 | .local_ptr => { |
| 10126 | 10137 | const local_ptr = s.cast(Scope.LocalPtr).?; |
| 10127 | 10138 | if (local_ptr.name == ident_name) { |
| 10139 | const name_slice = mem.span(astgen.nullTerminatedString(ident_name)); |
| 10128 | 10140 | const name = try gpa.dupe(u8, name_slice); |
| 10129 | 10141 | defer gpa.free(name); |
| 10130 | 10142 | return astgen.failTokNotes(name_token, "redeclaration of {s} '{s}'", .{ |
| ... | ... | @@ -10145,6 +10157,7 @@ fn detectLocalShadowing( |
| 10145 | 10157 | s = ns.parent; |
| 10146 | 10158 | continue; |
| 10147 | 10159 | }; |
| 10160 | const name_slice = mem.span(astgen.nullTerminatedString(ident_name)); |
| 10148 | 10161 | const name = try gpa.dupe(u8, name_slice); |
| 10149 | 10162 | defer gpa.free(name); |
| 10150 | 10163 | return astgen.failTokNotes(name_token, "local shadows declaration of '{s}'", .{ |