authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-11 21:35:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-12 18:13:24-07:00
logf16855b9d70c747423ee81f27d619694856d365b
treedf242ab0fcbb17c0dcc0c640112749642b848383
parente323cf1264f390911dcc2efea71d46be1d631d92

remove pointless discards


7 files changed, 38 insertions(+), 31 deletions(-)

lib/std/Thread.zig-1
...@@ -404,7 +404,6 @@ fn callFn(comptime f: anytype, args: anytype) switch (Impl) {...@@ -404,7 +404,6 @@ fn callFn(comptime f: anytype, args: anytype) switch (Impl) {
404 }404 }
405405
406 // pthreads don't support exit status, ignore value406 // pthreads don't support exit status, ignore value
407 _ = status;
408 return default_value;407 return default_value;
409 },408 },
410 .ErrorUnion => |info| {409 .ErrorUnion => |info| {
lib/std/heap/general_purpose_allocator.zig-2
...@@ -582,8 +582,6 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -582,8 +582,6 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
582 old_align: u29,582 old_align: u29,
583 ret_addr: usize,583 ret_addr: usize,
584 ) void {584 ) void {
585 _ = old_align;
586
587 const entry = self.large_allocations.getEntry(@ptrToInt(old_mem.ptr)) orelse {585 const entry = self.large_allocations.getEntry(@ptrToInt(old_mem.ptr)) orelse {
588 if (config.safety) {586 if (config.safety) {
589 @panic("Invalid free");587 @panic("Invalid free");
lib/std/math.zig-1
...@@ -171,7 +171,6 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {...@@ -171,7 +171,6 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
171}171}
172172
173pub fn approxEq(comptime T: type, x: T, y: T, tolerance: T) bool {173pub fn approxEq(comptime T: type, x: T, y: T, tolerance: T) bool {
174 _ = T;
175 _ = x;174 _ = x;
176 _ = y;175 _ = y;
177 _ = tolerance;176 _ = tolerance;
src/AstGen.zig+36-17
...@@ -2685,11 +2685,7 @@ fn genDefers(...@@ -2685,11 +2685,7 @@ fn genDefers(
2685 }2685 }
2686}2686}
26872687
2688fn checkUsed(2688fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void {
2689 gz: *GenZir,
2690 outer_scope: *Scope,
2691 inner_scope: *Scope,
2692) InnerError!void {
2693 const astgen = gz.astgen;2689 const astgen = gz.astgen;
26942690
2695 var scope = inner_scope;2691 var scope = inner_scope;
...@@ -2698,15 +2694,23 @@ fn checkUsed(...@@ -2698,15 +2694,23 @@ fn checkUsed(
2698 .gen_zir => scope = scope.cast(GenZir).?.parent,2694 .gen_zir => scope = scope.cast(GenZir).?.parent,
2699 .local_val => {2695 .local_val => {
2700 const s = scope.cast(Scope.LocalVal).?;2696 const s = scope.cast(Scope.LocalVal).?;
2701 if (!s.used) {2697 if (s.used == 0 and s.discarded == 0) {
2702 try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});2698 try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});
2699 } else if (s.used != 0 and s.discarded != 0) {
2700 try astgen.appendErrorTokNotes(s.discarded, "pointless discard of {s}", .{@tagName(s.id_cat)}, &[_]u32{
2701 try gz.astgen.errNoteTok(s.used, "used here", .{}),
2702 });
2703 }2703 }
2704 scope = s.parent;2704 scope = s.parent;
2705 },2705 },
2706 .local_ptr => {2706 .local_ptr => {
2707 const s = scope.cast(Scope.LocalPtr).?;2707 const s = scope.cast(Scope.LocalPtr).?;
2708 if (!s.used) {2708 if (s.used == 0 and s.discarded == 0) {
2709 try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});2709 try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});
2710 } else if (s.used != 0 and s.discarded != 0) {
2711 try astgen.appendErrorTokNotes(s.discarded, "pointless discard of {s}", .{@tagName(s.id_cat)}, &[_]u32{
2712 try gz.astgen.errNoteTok(s.used, "used here", .{}),
2713 });
2710 }2714 }
2711 scope = s.parent;2715 scope = s.parent;
2712 },2716 },
...@@ -6848,11 +6852,10 @@ fn localVarRef(...@@ -6848,11 +6852,10 @@ fn localVarRef(
6848 scope: *Scope,6852 scope: *Scope,
6849 rl: ResultLoc,6853 rl: ResultLoc,
6850 ident: Ast.Node.Index,6854 ident: Ast.Node.Index,
6851 ident_token: Ast.Node.Index,6855 ident_token: Ast.TokenIndex,
6852) InnerError!Zir.Inst.Ref {6856) InnerError!Zir.Inst.Ref {
6853 const astgen = gz.astgen;6857 const astgen = gz.astgen;
6854 const gpa = astgen.gpa;6858 const gpa = astgen.gpa;
6855
6856 const name_str_index = try astgen.identAsString(ident_token);6859 const name_str_index = try astgen.identAsString(ident_token);
6857 var s = scope;6860 var s = scope;
6858 var found_already: ?Ast.Node.Index = null; // we have found a decl with the same name already6861 var found_already: ?Ast.Node.Index = null; // we have found a decl with the same name already
...@@ -6865,7 +6868,11 @@ fn localVarRef(...@@ -6865,7 +6868,11 @@ fn localVarRef(
6865 if (local_val.name == name_str_index) {6868 if (local_val.name == name_str_index) {
6866 // Locals cannot shadow anything, so we do not need to look for ambiguous6869 // Locals cannot shadow anything, so we do not need to look for ambiguous
6867 // references in this case.6870 // references in this case.
6868 local_val.used = true;6871 if (rl == .discard) {
6872 local_val.discarded = ident_token;
6873 } else {
6874 local_val.used = ident_token;
6875 }
68696876
6870 const value_inst = try tunnelThroughClosure(6877 const value_inst = try tunnelThroughClosure(
6871 gz,6878 gz,
...@@ -6884,7 +6891,11 @@ fn localVarRef(...@@ -6884,7 +6891,11 @@ fn localVarRef(
6884 .local_ptr => {6891 .local_ptr => {
6885 const local_ptr = s.cast(Scope.LocalPtr).?;6892 const local_ptr = s.cast(Scope.LocalPtr).?;
6886 if (local_ptr.name == name_str_index) {6893 if (local_ptr.name == name_str_index) {
6887 local_ptr.used = true;6894 if (rl == .discard) {
6895 local_ptr.discarded = ident_token;
6896 } else {
6897 local_ptr.used = ident_token;
6898 }
68886899
6889 // Can't close over a runtime variable6900 // Can't close over a runtime variable
6890 if (num_namespaces_out != 0 and !local_ptr.maybe_comptime) {6901 if (num_namespaces_out != 0 and !local_ptr.maybe_comptime) {
...@@ -7519,7 +7530,7 @@ fn builtinCall(...@@ -7519,7 +7530,7 @@ fn builtinCall(
7519 .local_val => {7530 .local_val => {
7520 const local_val = s.cast(Scope.LocalVal).?;7531 const local_val = s.cast(Scope.LocalVal).?;
7521 if (local_val.name == decl_name) {7532 if (local_val.name == decl_name) {
7522 local_val.used = true;7533 local_val.used = ident_token;
7523 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{7534 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
7524 .operand = local_val.inst,7535 .operand = local_val.inst,
7525 .options = try comptimeExpr(gz, scope, .{ .coerced_ty = .export_options_type }, params[1]),7536 .options = try comptimeExpr(gz, scope, .{ .coerced_ty = .export_options_type }, params[1]),
...@@ -7533,7 +7544,7 @@ fn builtinCall(...@@ -7533,7 +7544,7 @@ fn builtinCall(
7533 if (local_ptr.name == decl_name) {7544 if (local_ptr.name == decl_name) {
7534 if (!local_ptr.maybe_comptime)7545 if (!local_ptr.maybe_comptime)
7535 return astgen.failNode(params[0], "unable to export runtime-known value", .{});7546 return astgen.failNode(params[0], "unable to export runtime-known value", .{});
7536 local_ptr.used = true;7547 local_ptr.used = ident_token;
7537 const loaded = try gz.addUnNode(.load, local_ptr.ptr, node);7548 const loaded = try gz.addUnNode(.load, local_ptr.ptr, node);
7538 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{7549 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
7539 .operand = loaded,7550 .operand = loaded,
...@@ -10065,11 +10076,15 @@ const Scope = struct {...@@ -10065,11 +10076,15 @@ const Scope = struct {
10065 inst: Zir.Inst.Ref,10076 inst: Zir.Inst.Ref,
10066 /// Source location of the corresponding variable declaration.10077 /// Source location of the corresponding variable declaration.
10067 token_src: Ast.TokenIndex,10078 token_src: Ast.TokenIndex,
10079 /// Track the first identifer where it is referenced.
10080 /// 0 means never referenced.
10081 used: Ast.TokenIndex = 0,
10082 /// Track the identifier where it is discarded, like this `_ = foo;`.
10083 /// 0 means never discarded.
10084 discarded: Ast.TokenIndex = 0,
10068 /// String table index.10085 /// String table index.
10069 name: u32,10086 name: u32,
10070 id_cat: IdCat,10087 id_cat: IdCat,
10071 /// Track whether the name has been referenced.
10072 used: bool = false,
10073 };10088 };
1007410089
10075 /// This could be a `const` or `var` local. It has a pointer instead of a value.10090 /// This could be a `const` or `var` local. It has a pointer instead of a value.
...@@ -10084,14 +10099,18 @@ const Scope = struct {...@@ -10084,14 +10099,18 @@ const Scope = struct {
10084 ptr: Zir.Inst.Ref,10099 ptr: Zir.Inst.Ref,
10085 /// Source location of the corresponding variable declaration.10100 /// Source location of the corresponding variable declaration.
10086 token_src: Ast.TokenIndex,10101 token_src: Ast.TokenIndex,
10102 /// Track the first identifer where it is referenced.
10103 /// 0 means never referenced.
10104 used: Ast.TokenIndex = 0,
10105 /// Track the identifier where it is discarded, like this `_ = foo;`.
10106 /// 0 means never discarded.
10107 discarded: Ast.TokenIndex = 0,
10087 /// String table index.10108 /// String table index.
10088 name: u32,10109 name: u32,
10089 id_cat: IdCat,10110 id_cat: IdCat,
10090 /// true means we find out during Sema whether the value is comptime.10111 /// true means we find out during Sema whether the value is comptime.
10091 /// false means it is already known at AstGen the value is runtime-known.10112 /// false means it is already known at AstGen the value is runtime-known.
10092 maybe_comptime: bool,10113 maybe_comptime: bool,
10093 /// Track whether the name has been referenced.
10094 used: bool = false,
10095 };10114 };
1009610115
10097 const Defer = struct {10116 const Defer = struct {
test/behavior/bugs/11165.zig-2
...@@ -14,7 +14,6 @@ test "bytes" {...@@ -14,7 +14,6 @@ test "bytes" {
14 .a = undefined,14 .a = undefined,
15 .c = "12345".*, // this caused problems15 .c = "12345".*, // this caused problems
16 };16 };
17 _ = s_1;
1817
19 var u_2 = U{ .s = s_1 };18 var u_2 = U{ .s = s_1 };
20 _ = u_2;19 _ = u_2;
...@@ -35,7 +34,6 @@ test "aggregate" {...@@ -35,7 +34,6 @@ test "aggregate" {
35 .a = undefined,34 .a = undefined,
36 .c = c, // this caused problems35 .c = c, // this caused problems
37 };36 };
38 _ = s_1;
3937
40 var u_2 = U{ .s = s_1 };38 var u_2 = U{ .s = s_1 };
41 _ = u_2;39 _ = u_2;
test/behavior/type.zig-2
...@@ -486,7 +486,6 @@ test "Type.Union from Type.Enum" {...@@ -486,7 +486,6 @@ test "Type.Union from Type.Enum" {
486 .decls = &.{},486 .decls = &.{},
487 },487 },
488 });488 });
489 _ = T;
490 _ = @typeInfo(T).Union;489 _ = @typeInfo(T).Union;
491}490}
492491
...@@ -505,7 +504,6 @@ test "Type.Union from regular enum" {...@@ -505,7 +504,6 @@ test "Type.Union from regular enum" {
505 .decls = &.{},504 .decls = &.{},
506 },505 },
507 });506 });
508 _ = T;
509 _ = @typeInfo(T).Union;507 _ = @typeInfo(T).Union;
510}508}
511509
test/behavior/type_info.zig+2-6
...@@ -425,12 +425,8 @@ fn generic2(comptime T: type, param: T, param2: u8) void {...@@ -425,12 +425,8 @@ fn generic2(comptime T: type, param: T, param2: u8) void {
425 _ = param;425 _ = param;
426 _ = param2;426 _ = param2;
427}427}
428fn generic3(param: anytype) @TypeOf(param) {428fn generic3(param: anytype) @TypeOf(param) {}
429 _ = param;429fn generic4(comptime param: anytype) @TypeOf(param) {}
430}
431fn generic4(comptime param: anytype) @TypeOf(param) {
432 _ = param;
433}
434430
435test "typeInfo with comptime parameter in struct fn def" {431test "typeInfo with comptime parameter in struct fn def" {
436 const S = struct {432 const S = struct {