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) {
404404 }
405405
406406 // pthreads don't support exit status, ignore value
407 _ = status;
408407 return default_value;
409408 },
410409 .ErrorUnion => |info| {
lib/std/heap/general_purpose_allocator.zig-2
......@@ -582,8 +582,6 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
582582 old_align: u29,
583583 ret_addr: usize,
584584 ) void {
585 _ = old_align;
586
587585 const entry = self.large_allocations.getEntry(@ptrToInt(old_mem.ptr)) orelse {
588586 if (config.safety) {
589587 @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 {
171171}
172172
173173pub fn approxEq(comptime T: type, x: T, y: T, tolerance: T) bool {
174 _ = T;
175174 _ = x;
176175 _ = y;
177176 _ = tolerance;
src/AstGen.zig+36-17
......@@ -2685,11 +2685,7 @@ fn genDefers(
26852685 }
26862686}
26872687
2688fn checkUsed(
2689 gz: *GenZir,
2690 outer_scope: *Scope,
2691 inner_scope: *Scope,
2692) InnerError!void {
2688fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void {
26932689 const astgen = gz.astgen;
26942690
26952691 var scope = inner_scope;
......@@ -2698,15 +2694,23 @@ fn checkUsed(
26982694 .gen_zir => scope = scope.cast(GenZir).?.parent,
26992695 .local_val => {
27002696 const s = scope.cast(Scope.LocalVal).?;
2701 if (!s.used) {
2697 if (s.used == 0 and s.discarded == 0) {
27022698 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 });
27032703 }
27042704 scope = s.parent;
27052705 },
27062706 .local_ptr => {
27072707 const s = scope.cast(Scope.LocalPtr).?;
2708 if (!s.used) {
2708 if (s.used == 0 and s.discarded == 0) {
27092709 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 });
27102714 }
27112715 scope = s.parent;
27122716 },
......@@ -6848,11 +6852,10 @@ fn localVarRef(
68486852 scope: *Scope,
68496853 rl: ResultLoc,
68506854 ident: Ast.Node.Index,
6851 ident_token: Ast.Node.Index,
6855 ident_token: Ast.TokenIndex,
68526856) InnerError!Zir.Inst.Ref {
68536857 const astgen = gz.astgen;
68546858 const gpa = astgen.gpa;
6855
68566859 const name_str_index = try astgen.identAsString(ident_token);
68576860 var s = scope;
68586861 var found_already: ?Ast.Node.Index = null; // we have found a decl with the same name already
......@@ -6865,7 +6868,11 @@ fn localVarRef(
68656868 if (local_val.name == name_str_index) {
68666869 // Locals cannot shadow anything, so we do not need to look for ambiguous
68676870 // 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
68706877 const value_inst = try tunnelThroughClosure(
68716878 gz,
......@@ -6884,7 +6891,11 @@ fn localVarRef(
68846891 .local_ptr => {
68856892 const local_ptr = s.cast(Scope.LocalPtr).?;
68866893 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
68896900 // Can't close over a runtime variable
68906901 if (num_namespaces_out != 0 and !local_ptr.maybe_comptime) {
......@@ -7519,7 +7530,7 @@ fn builtinCall(
75197530 .local_val => {
75207531 const local_val = s.cast(Scope.LocalVal).?;
75217532 if (local_val.name == decl_name) {
7522 local_val.used = true;
7533 local_val.used = ident_token;
75237534 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
75247535 .operand = local_val.inst,
75257536 .options = try comptimeExpr(gz, scope, .{ .coerced_ty = .export_options_type }, params[1]),
......@@ -7533,7 +7544,7 @@ fn builtinCall(
75337544 if (local_ptr.name == decl_name) {
75347545 if (!local_ptr.maybe_comptime)
75357546 return astgen.failNode(params[0], "unable to export runtime-known value", .{});
7536 local_ptr.used = true;
7547 local_ptr.used = ident_token;
75377548 const loaded = try gz.addUnNode(.load, local_ptr.ptr, node);
75387549 _ = try gz.addPlNode(.export_value, node, Zir.Inst.ExportValue{
75397550 .operand = loaded,
......@@ -10065,11 +10076,15 @@ const Scope = struct {
1006510076 inst: Zir.Inst.Ref,
1006610077 /// Source location of the corresponding variable declaration.
1006710078 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,
1006810085 /// String table index.
1006910086 name: u32,
1007010087 id_cat: IdCat,
10071 /// Track whether the name has been referenced.
10072 used: bool = false,
1007310088 };
1007410089
1007510090 /// This could be a `const` or `var` local. It has a pointer instead of a value.
......@@ -10084,14 +10099,18 @@ const Scope = struct {
1008410099 ptr: Zir.Inst.Ref,
1008510100 /// Source location of the corresponding variable declaration.
1008610101 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,
1008710108 /// String table index.
1008810109 name: u32,
1008910110 id_cat: IdCat,
1009010111 /// true means we find out during Sema whether the value is comptime.
1009110112 /// false means it is already known at AstGen the value is runtime-known.
1009210113 maybe_comptime: bool,
10093 /// Track whether the name has been referenced.
10094 used: bool = false,
1009510114 };
1009610115
1009710116 const Defer = struct {
test/behavior/bugs/11165.zig-2
......@@ -14,7 +14,6 @@ test "bytes" {
1414 .a = undefined,
1515 .c = "12345".*, // this caused problems
1616 };
17 _ = s_1;
1817
1918 var u_2 = U{ .s = s_1 };
2019 _ = u_2;
......@@ -35,7 +34,6 @@ test "aggregate" {
3534 .a = undefined,
3635 .c = c, // this caused problems
3736 };
38 _ = s_1;
3937
4038 var u_2 = U{ .s = s_1 };
4139 _ = u_2;
test/behavior/type.zig-2
......@@ -486,7 +486,6 @@ test "Type.Union from Type.Enum" {
486486 .decls = &.{},
487487 },
488488 });
489 _ = T;
490489 _ = @typeInfo(T).Union;
491490}
492491
......@@ -505,7 +504,6 @@ test "Type.Union from regular enum" {
505504 .decls = &.{},
506505 },
507506 });
508 _ = T;
509507 _ = @typeInfo(T).Union;
510508}
511509
test/behavior/type_info.zig+2-6
......@@ -425,12 +425,8 @@ fn generic2(comptime T: type, param: T, param2: u8) void {
425425 _ = param;
426426 _ = param2;
427427}
428fn generic3(param: anytype) @TypeOf(param) {
429 _ = param;
430}
431fn generic4(comptime param: anytype) @TypeOf(param) {
432 _ = param;
433}
428fn generic3(param: anytype) @TypeOf(param) {}
429fn generic4(comptime param: anytype) @TypeOf(param) {}
434430
435431test "typeInfo with comptime parameter in struct fn def" {
436432 const S = struct {