authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-13 13:01:42-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-13 13:01:42-04:00
logc25ce5bba0d00283ad4de9077dba5a8d5255b619
treecc4bdb8332acdde7b23bffacfc42bffaa6d35273
parentbe944870298e4694e8ccb3f8c65a0bd152e26dad
parentb2e94de3585e0a1242e3a5d6c8fd331da9f2d4f5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12820 from ziglang/pointless-discard-error

introduce compile error for pointless discards

22 files changed, 157 insertions(+), 140 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 {
src/Autodoc.zig-4
......@@ -2506,7 +2506,6 @@ fn walkInstruction(
25062506 try self.srcLocInfo(file, sn, parent_src)
25072507 else
25082508 parent_src;
2509 _ = src_info;
25102509
25112510 const decls_len = if (small.has_decls_len) blk: {
25122511 const decls_len = file.zir.extra[extra_index];
......@@ -2627,7 +2626,6 @@ fn walkInstruction(
26272626 extra_index += 1;
26282627 break :blk fields_len;
26292628 } else 0;
2630 _ = fields_len;
26312629
26322630 const decls_len = if (small.has_decls_len) blk: {
26332631 const decls_len = file.zir.extra[extra_index];
......@@ -2759,7 +2757,6 @@ fn walkInstruction(
27592757 extra_index += 1;
27602758 break :blk fields_len;
27612759 } else 0;
2762 _ = fields_len;
27632760
27642761 const decls_len = if (small.has_decls_len) blk: {
27652762 const decls_len = file.zir.extra[extra_index];
......@@ -2901,7 +2898,6 @@ fn walkInstruction(
29012898 extra_index += 1;
29022899 break :blk fields_len;
29032900 } else 0;
2904 _ = fields_len;
29052901
29062902 const decls_len = if (small.has_decls_len) blk: {
29072903 const decls_len = file.zir.extra[extra_index];
src/Sema.zig+11-15
......@@ -17575,30 +17575,30 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1757517575 const operand_coerced = try sema.coerce(block, Type.usize, operand_res, operand_src);
1757617576
1757717577 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
17578 const type_res = try sema.resolveType(block, src, extra.lhs);
17579 try sema.checkPtrType(block, type_src, type_res);
17580 try sema.resolveTypeLayout(block, src, type_res.elemType2());
17581 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
17578 const ptr_ty = try sema.resolveType(block, src, extra.lhs);
17579 const elem_ty = ptr_ty.elemType2();
17580 try sema.checkPtrType(block, type_src, ptr_ty);
1758217581 const target = sema.mod.getTarget();
17582 const ptr_align = try ptr_ty.ptrAlignmentAdvanced(target, sema.kit(block, src));
1758317583
1758417584 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
1758517585 const addr = val.toUnsignedInt(target);
17586 if (!type_res.isAllowzeroPtr() and addr == 0)
17587 return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{type_res.fmt(sema.mod)});
17586 if (!ptr_ty.isAllowzeroPtr() and addr == 0)
17587 return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(sema.mod)});
1758817588 if (addr != 0 and addr % ptr_align != 0)
17589 return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{type_res.fmt(sema.mod)});
17589 return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(sema.mod)});
1759017590
1759117591 const val_payload = try sema.arena.create(Value.Payload.U64);
1759217592 val_payload.* = .{
1759317593 .base = .{ .tag = .int_u64 },
1759417594 .data = addr,
1759517595 };
17596 return sema.addConstant(type_res, Value.initPayload(&val_payload.base));
17596 return sema.addConstant(ptr_ty, Value.initPayload(&val_payload.base));
1759717597 }
1759817598
1759917599 try sema.requireRuntimeBlock(block, src, operand_src);
17600 if (block.wantSafety() and try sema.typeHasRuntimeBits(block, sema.src, type_res.elemType2())) {
17601 if (!type_res.isAllowzeroPtr()) {
17600 if (block.wantSafety() and try sema.typeHasRuntimeBits(block, sema.src, elem_ty)) {
17601 if (!ptr_ty.isAllowzeroPtr()) {
1760217602 const is_non_zero = try block.addBinOp(.cmp_neq, operand_coerced, .zero_usize);
1760317603 try sema.addSafetyCheck(block, is_non_zero, .cast_to_null);
1760417604 }
......@@ -17618,7 +17618,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1761817618 try sema.addSafetyCheck(block, is_aligned, .incorrect_alignment);
1761917619 }
1762017620 }
17621 return block.addBitCast(type_res, operand_coerced);
17621 return block.addBitCast(ptr_ty, operand_coerced);
1762217622}
1762317623
1762417624fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
......@@ -19670,8 +19670,6 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1967019670 if (try sema.resolveDefinedValue(block, src_src, src_ptr)) |src_ptr_val| {
1967119671 if (!src_ptr_val.isComptimeMutablePtr()) break :rs src_src;
1967219672 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
19673 _ = dest_ptr_val;
19674 _ = src_ptr_val;
1967519673 _ = len_val;
1967619674 return sema.fail(block, src, "TODO: Sema.zirMemcpy at comptime", .{});
1967719675 } else break :rs len_src;
......@@ -19713,7 +19711,6 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1971319711 if (!ptr_val.isComptimeMutablePtr()) break :rs dest_src;
1971419712 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
1971519713 if (try sema.resolveMaybeUndefVal(block, value_src, value)) |val| {
19716 _ = ptr_val;
1971719714 _ = len_val;
1971819715 _ = val;
1971919716 return sema.fail(block, src, "TODO: Sema.zirMemset at comptime", .{});
......@@ -19941,7 +19938,6 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1994119938 if (val.tag() == .generic_poison) {
1994219939 break :blk FuncLinkSection{ .generic = {} };
1994319940 }
19944 _ = val;
1994519941 return sema.fail(block, section_src, "TODO implement linksection on functions", .{});
1994619942 } else if (extra.data.bits.has_section_ref) blk: {
1994719943 const section_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
src/arch/aarch64/CodeGen.zig-3
......@@ -2581,7 +2581,6 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
25812581}
25822582
25832583fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
2584 _ = inst;
25852584 const result: MCValue = if (self.liveness.isUnused(inst))
25862585 .dead
25872586 else
......@@ -3614,7 +3613,6 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
36143613 const ptr = try self.resolveInst(un_op);
36153614 const ptr_ty = self.air.typeOf(un_op);
36163615 const ret_ty = self.fn_type.fnReturnType();
3617 _ = ret_ty;
36183616
36193617 switch (self.ret_mcv) {
36203618 .none => {},
......@@ -5099,7 +5097,6 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) Inne
50995097 } else {
51005098 return self.fail("TODO codegen non-ELF const Decl pointer", .{});
51015099 }
5102 _ = tv;
51035100}
51045101
51055102fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue {
src/arch/arm/CodeGen.zig-4
......@@ -2111,7 +2111,6 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
21112111}
21122112
21132113fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
2114 _ = inst;
21152114 const result: MCValue = if (self.liveness.isUnused(inst))
21162115 .dead
21172116 else
......@@ -3353,7 +3352,6 @@ fn divFloat(
33533352) InnerError!MCValue {
33543353 _ = lhs_bind;
33553354 _ = rhs_bind;
3356 _ = lhs_ty;
33573355 _ = rhs_ty;
33583356 _ = maybe_inst;
33593357
......@@ -3420,7 +3418,6 @@ fn divExact(
34203418) InnerError!MCValue {
34213419 _ = lhs_bind;
34223420 _ = rhs_bind;
3423 _ = lhs_ty;
34243421 _ = rhs_ty;
34253422 _ = maybe_inst;
34263423
......@@ -3506,7 +3503,6 @@ fn modulo(
35063503) InnerError!MCValue {
35073504 _ = lhs_bind;
35083505 _ = rhs_bind;
3509 _ = lhs_ty;
35103506 _ = rhs_ty;
35113507 _ = maybe_inst;
35123508
src/arch/riscv64/CodeGen.zig-3
......@@ -1316,7 +1316,6 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
13161316}
13171317
13181318fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
1319 _ = inst;
13201319 const result: MCValue = if (self.liveness.isUnused(inst))
13211320 .dead
13221321 else
......@@ -1598,7 +1597,6 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void {
15981597 return self.structFieldPtr(ty_op.operand, ty_op.ty, index);
15991598}
16001599fn structFieldPtr(self: *Self, operand: Air.Inst.Ref, ty: Air.Inst.Ref, index: u32) !void {
1601 _ = self;
16021600 _ = operand;
16031601 _ = ty;
16041602 _ = index;
......@@ -1615,7 +1613,6 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
16151613}
16161614
16171615fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void {
1618 _ = self;
16191616 _ = inst;
16201617 return self.fail("TODO implement codegen airFieldParentPtr", .{});
16211618}
src/arch/sparc64/CodeGen.zig-2
......@@ -2084,9 +2084,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void {
20842084}
20852085
20862086fn airSwitch(self: *Self, inst: Air.Inst.Index) !void {
2087 _ = self;
20882087 _ = inst;
2089
20902088 return self.fail("TODO implement switch for {}", .{self.target.cpu.arch});
20912089}
20922090
src/arch/x86_64/CodeGen.zig-2
......@@ -1960,7 +1960,6 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void {
19601960}
19611961
19621962fn airErrReturnTrace(self: *Self, inst: Air.Inst.Index) !void {
1963 _ = inst;
19641963 const result: MCValue = if (self.liveness.isUnused(inst))
19651964 .dead
19661965 else
......@@ -6590,7 +6589,6 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) !void {
65906589fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void {
65916590 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
65926591 const extra = self.air.extraData(Air.Block, ty_pl.payload);
6593 _ = ty_pl;
65946592 _ = extra;
65956593 return self.fail("TODO implement x86 airCmpxchg", .{});
65966594 // return self.finishAir(inst, result, .{ extra.ptr, extra.expected_value, extra.new_value });
src/link/Plan9.zig-1
......@@ -622,7 +622,6 @@ pub fn updateDeclExports(
622622) !void {
623623 try self.seeDecl(decl_index);
624624 // we do all the things in flush
625 _ = self;
626625 _ = module;
627626 _ = exports;
628627}
src/print_air.zig-1
......@@ -324,7 +324,6 @@ const Writer = struct {
324324 fn writeNoOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
325325 _ = w;
326326 _ = inst;
327 _ = s;
328327 // no-op, no argument to write
329328 }
330329
src/translate_c/ast.zig+21-8
......@@ -1550,14 +1550,27 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
15501550 .main_token = try c.addToken(.identifier, "_"),
15511551 .data = undefined,
15521552 });
1553 return c.addNode(.{
1554 .tag = .assign,
1555 .main_token = try c.addToken(.equal, "="),
1556 .data = .{
1557 .lhs = lhs,
1558 .rhs = try renderNode(c, payload.value),
1559 },
1560 });
1553 const main_token = try c.addToken(.equal, "=");
1554 if (payload.value.tag() == .identifier) {
1555 // Render as `_ = @TypeOf(foo);` to avoid tripping "pointless discard" error.
1556 return c.addNode(.{
1557 .tag = .assign,
1558 .main_token = main_token,
1559 .data = .{
1560 .lhs = lhs,
1561 .rhs = try renderBuiltinCall(c, "@TypeOf", &.{payload.value}),
1562 },
1563 });
1564 } else {
1565 return c.addNode(.{
1566 .tag = .assign,
1567 .main_token = main_token,
1568 .data = .{
1569 .lhs = lhs,
1570 .rhs = try renderNode(c, payload.value),
1571 },
1572 });
1573 }
15611574 },
15621575 .@"while" => {
15631576 const payload = node.castTag(.@"while").?.data;
src/type.zig+18-7
......@@ -2715,8 +2715,12 @@ pub const Type = extern union {
27152715 }
27162716
27172717 /// Returns 0 if the pointer is naturally aligned and the element type is 0-bit.
2718 pub fn ptrAlignment(self: Type, target: Target) u32 {
2719 switch (self.tag()) {
2718 pub fn ptrAlignment(ty: Type, target: Target) u32 {
2719 return ptrAlignmentAdvanced(ty, target, null) catch unreachable;
2720 }
2721
2722 pub fn ptrAlignmentAdvanced(ty: Type, target: Target, sema_kit: ?Module.WipAnalysis) !u32 {
2723 switch (ty.tag()) {
27202724 .single_const_pointer,
27212725 .single_mut_pointer,
27222726 .many_const_pointer,
......@@ -2728,8 +2732,12 @@ pub const Type = extern union {
27282732 .optional_single_const_pointer,
27292733 .optional_single_mut_pointer,
27302734 => {
2731 const child_type = self.cast(Payload.ElemType).?.data;
2732 return child_type.abiAlignment(target);
2735 const child_type = ty.cast(Payload.ElemType).?.data;
2736 if (sema_kit) |sk| {
2737 const res = try child_type.abiAlignmentAdvanced(target, .{ .sema_kit = sk });
2738 return res.scalar;
2739 }
2740 return (child_type.abiAlignmentAdvanced(target, .eager) catch unreachable).scalar;
27332741 },
27342742
27352743 .manyptr_u8,
......@@ -2740,14 +2748,17 @@ pub const Type = extern union {
27402748 => return 1,
27412749
27422750 .pointer => {
2743 const ptr_info = self.castTag(.pointer).?.data;
2751 const ptr_info = ty.castTag(.pointer).?.data;
27442752 if (ptr_info.@"align" != 0) {
27452753 return ptr_info.@"align";
2754 } else if (sema_kit) |sk| {
2755 const res = try ptr_info.pointee_type.abiAlignmentAdvanced(target, .{ .sema_kit = sk });
2756 return res.scalar;
27462757 } else {
2747 return ptr_info.pointee_type.abiAlignment(target);
2758 return (ptr_info.pointee_type.abiAlignmentAdvanced(target, .eager) catch unreachable).scalar;
27482759 }
27492760 },
2750 .optional => return self.castTag(.optional).?.data.ptrAlignment(target),
2761 .optional => return ty.castTag(.optional).?.data.ptrAlignmentAdvanced(target, sema_kit),
27512762
27522763 else => unreachable,
27532764 }
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 {
test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig-1
......@@ -6,7 +6,6 @@ export fn entry() void {
66}
77
88fn makeLlamas(count: usize) [count]u8 {
9 _ = count;
109}
1110
1211// error
test/cases/compile_errors/pointless discard.zig created+12
......@@ -0,0 +1,12 @@
1export fn foo() void {
2 var x: i32 = 1234;
3 x += 1;
4 _ = x;
5}
6
7// error
8// backend=stage2
9// target=native
10//
11// :4:9: error: pointless discard of local variable
12// :3:5: note: used here
test/cases/type_of.0.zig-1
......@@ -1,6 +1,5 @@
11pub fn main() void {
22 var x: usize = 0;
3 _ = x;
43 const z = @TypeOf(x, @as(u128, 5));
54 assert(z == u128);
65}
test/translate_c.zig+57-57
......@@ -116,10 +116,10 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
116116 \\pub export fn foo() void {
117117 \\ while (true) if (true) {
118118 \\ var a: c_int = 1;
119 \\ _ = a;
119 \\ _ = @TypeOf(a);
120120 \\ } else {
121121 \\ var b: c_int = 2;
122 \\ _ = b;
122 \\ _ = @TypeOf(b);
123123 \\ };
124124 \\ if (true) if (true) {};
125125 \\}
......@@ -192,7 +192,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
192192 \\ .B = 0,
193193 \\ .C = 0,
194194 \\ };
195 \\ _ = a;
195 \\ _ = @TypeOf(a);
196196 \\ {
197197 \\ const struct_Foo_1 = extern struct {
198198 \\ A: c_int,
......@@ -204,7 +204,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
204204 \\ .B = 0,
205205 \\ .C = 0,
206206 \\ };
207 \\ _ = a_2;
207 \\ _ = @TypeOf(a_2);
208208 \\ }
209209 \\}
210210 });
......@@ -233,24 +233,24 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
233233 \\ B: c_int,
234234 \\ C: c_int,
235235 \\ };
236 \\ _ = union_unnamed_1;
236 \\ _ = @TypeOf(union_unnamed_1);
237237 \\ const Foo = union_unnamed_1;
238238 \\ var a: Foo = Foo{
239239 \\ .A = @as(c_int, 0),
240240 \\ };
241 \\ _ = a;
241 \\ _ = @TypeOf(a);
242242 \\ {
243243 \\ const union_unnamed_2 = extern union {
244244 \\ A: c_int,
245245 \\ B: c_int,
246246 \\ C: c_int,
247247 \\ };
248 \\ _ = union_unnamed_2;
248 \\ _ = @TypeOf(union_unnamed_2);
249249 \\ const Foo_1 = union_unnamed_2;
250250 \\ var a_2: Foo_1 = Foo_1{
251251 \\ .A = @as(c_int, 0),
252252 \\ };
253 \\ _ = a_2;
253 \\ _ = @TypeOf(a_2);
254254 \\ }
255255 \\}
256256 });
......@@ -318,7 +318,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
318318 \\ const bar_1 = struct {
319319 \\ threadlocal var static: c_int = 2;
320320 \\ };
321 \\ _ = bar_1;
321 \\ _ = @TypeOf(bar_1);
322322 \\ return 0;
323323 \\}
324324 });
......@@ -337,7 +337,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
337337 \\}
338338 \\pub export fn bar() c_int {
339339 \\ var a: c_int = 2;
340 \\ _ = a;
340 \\ _ = @TypeOf(a);
341341 \\ return 0;
342342 \\}
343343 \\pub export fn baz() c_int {
......@@ -352,7 +352,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
352352 , &[_][]const u8{
353353 \\pub export fn main() void {
354354 \\ var a: c_int = @bitCast(c_int, @truncate(c_uint, @alignOf(c_int)));
355 \\ _ = a;
355 \\ _ = @TypeOf(a);
356356 \\}
357357 });
358358
......@@ -500,7 +500,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
500500 \\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
501501 , &[_][]const u8{
502502 \\pub const foo = blk: {
503 \\ _ = foo;
503 \\ _ = @TypeOf(foo);
504504 \\ break :blk bar;
505505 \\};
506506 ,
......@@ -724,7 +724,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
724724 \\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
725725 \\ var opaque_1 = arg_opaque_1;
726726 \\ var cast: ?*struct_opaque_2 = @ptrCast(?*struct_opaque_2, opaque_1);
727 \\ _ = cast;
727 \\ _ = @TypeOf(cast);
728728 \\}
729729 });
730730
......@@ -761,7 +761,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
761761 \\pub export fn my_fn() align(128) void {}
762762 \\pub export fn other_fn() void {
763763 \\ var ARR: [16]u8 align(16) = undefined;
764 \\ _ = ARR;
764 \\ _ = @TypeOf(ARR);
765765 \\}
766766 });
767767 }
......@@ -798,17 +798,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
798798 , &[_][]const u8{
799799 \\pub export fn foo() void {
800800 \\ var a: c_int = undefined;
801 \\ _ = a;
801 \\ _ = @TypeOf(a);
802802 \\ var b: u8 = 123;
803 \\ _ = b;
803 \\ _ = @TypeOf(b);
804804 \\ const c: c_int = undefined;
805 \\ _ = c;
805 \\ _ = @TypeOf(c);
806806 \\ const d: c_uint = @bitCast(c_uint, @as(c_int, 440));
807 \\ _ = d;
807 \\ _ = @TypeOf(d);
808808 \\ var e: c_int = 10;
809 \\ _ = e;
809 \\ _ = @TypeOf(e);
810810 \\ var f: c_uint = 10;
811 \\ _ = f;
811 \\ _ = @TypeOf(f);
812812 \\}
813813 });
814814
......@@ -867,7 +867,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
867867 \\ const v2 = struct {
868868 \\ const static: [5:0]u8 = "2.2.2".*;
869869 \\ };
870 \\ _ = v2;
870 \\ _ = @TypeOf(v2);
871871 \\}
872872 });
873873
......@@ -911,7 +911,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
911911 \\pub export fn bar() void {
912912 \\ var func_ptr: ?*anyopaque = @ptrCast(?*anyopaque, foo);
913913 \\ var typed_func_ptr: ?*const fn () callconv(.C) void = @intToPtr(?*const fn () callconv(.C) void, @intCast(c_ulong, @ptrToInt(func_ptr)));
914 \\ _ = typed_func_ptr;
914 \\ _ = @TypeOf(typed_func_ptr);
915915 \\}
916916 });
917917 }
......@@ -1353,7 +1353,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13531353 , &[_][]const u8{
13541354 \\pub export fn foo() void {
13551355 \\ var a: c_int = undefined;
1356 \\ _ = a;
1356 \\ _ = @TypeOf(a);
13571357 \\}
13581358 });
13591359
......@@ -1524,23 +1524,23 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
15241524 \\ var p: ?*anyopaque = undefined;
15251525 \\ {
15261526 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment([*c]u8), p));
1527 \\ _ = to_char;
1527 \\ _ = @TypeOf(to_char);
15281528 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment([*c]c_short), p));
1529 \\ _ = to_short;
1529 \\ _ = @TypeOf(to_short);
15301530 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment([*c]c_int), p));
1531 \\ _ = to_int;
1531 \\ _ = @TypeOf(to_int);
15321532 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment([*c]c_longlong), p));
1533 \\ _ = to_longlong;
1533 \\ _ = @TypeOf(to_longlong);
15341534 \\ }
15351535 \\ {
15361536 \\ var to_char: [*c]u8 = @ptrCast([*c]u8, @alignCast(@import("std").meta.alignment([*c]u8), p));
1537 \\ _ = to_char;
1537 \\ _ = @TypeOf(to_char);
15381538 \\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(@import("std").meta.alignment([*c]c_short), p));
1539 \\ _ = to_short;
1539 \\ _ = @TypeOf(to_short);
15401540 \\ var to_int: [*c]c_int = @ptrCast([*c]c_int, @alignCast(@import("std").meta.alignment([*c]c_int), p));
1541 \\ _ = to_int;
1541 \\ _ = @TypeOf(to_int);
15421542 \\ var to_longlong: [*c]c_longlong = @ptrCast([*c]c_longlong, @alignCast(@import("std").meta.alignment([*c]c_longlong), p));
1543 \\ _ = to_longlong;
1543 \\ _ = @TypeOf(to_longlong);
15441544 \\ }
15451545 \\}
15461546 });
......@@ -1786,11 +1786,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17861786 \\ var arr: [10]u8 = [1]u8{
17871787 \\ 1,
17881788 \\ } ++ [1]u8{0} ** 9;
1789 \\ _ = arr;
1789 \\ _ = @TypeOf(arr);
17901790 \\ var arr1: [10][*c]u8 = [1][*c]u8{
17911791 \\ null,
17921792 \\ } ++ [1][*c]u8{null} ** 9;
1793 \\ _ = arr1;
1793 \\ _ = @TypeOf(arr1);
17941794 \\}
17951795 });
17961796
......@@ -2038,16 +2038,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
20382038 \\pub var c: c_int = 4;
20392039 \\pub export fn foo(arg_c_1: u8) void {
20402040 \\ var c_1 = arg_c_1;
2041 \\ _ = c_1;
2041 \\ _ = @TypeOf(c_1);
20422042 \\ var a_2: c_int = undefined;
20432043 \\ var b_3: u8 = 123;
20442044 \\ b_3 = @bitCast(u8, @truncate(i8, a_2));
20452045 \\ {
20462046 \\ var d: c_int = 5;
2047 \\ _ = d;
2047 \\ _ = @TypeOf(d);
20482048 \\ }
20492049 \\ var d: c_uint = @bitCast(c_uint, @as(c_int, 440));
2050 \\ _ = d;
2050 \\ _ = @TypeOf(d);
20512051 \\}
20522052 });
20532053
......@@ -2146,7 +2146,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21462146 \\ {
21472147 \\ var i: c_int = 2;
21482148 \\ var b: c_int = 4;
2149 \\ _ = b;
2149 \\ _ = @TypeOf(b);
21502150 \\ while ((i + @as(c_int, 2)) != 0) : (i = 2) {
21512151 \\ var a: c_int = 2;
21522152 \\ _ = blk: {
......@@ -2159,7 +2159,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
21592159 \\ }
21602160 \\ }
21612161 \\ var i: u8 = 2;
2162 \\ _ = i;
2162 \\ _ = @TypeOf(i);
21632163 \\}
21642164 });
21652165
......@@ -2396,27 +2396,27 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23962396 , &[_][]const u8{
23972397 \\pub export fn escapes() [*c]const u8 {
23982398 \\ var a: u8 = '\'';
2399 \\ _ = a;
2399 \\ _ = @TypeOf(a);
24002400 \\ var b: u8 = '\\';
2401 \\ _ = b;
2401 \\ _ = @TypeOf(b);
24022402 \\ var c: u8 = '\x07';
2403 \\ _ = c;
2403 \\ _ = @TypeOf(c);
24042404 \\ var d: u8 = '\x08';
2405 \\ _ = d;
2405 \\ _ = @TypeOf(d);
24062406 \\ var e: u8 = '\x0c';
2407 \\ _ = e;
2407 \\ _ = @TypeOf(e);
24082408 \\ var f: u8 = '\n';
2409 \\ _ = f;
2409 \\ _ = @TypeOf(f);
24102410 \\ var g: u8 = '\r';
2411 \\ _ = g;
2411 \\ _ = @TypeOf(g);
24122412 \\ var h: u8 = '\t';
2413 \\ _ = h;
2413 \\ _ = @TypeOf(h);
24142414 \\ var i: u8 = '\x0b';
2415 \\ _ = i;
2415 \\ _ = @TypeOf(i);
24162416 \\ var j: u8 = '\x00';
2417 \\ _ = j;
2417 \\ _ = @TypeOf(j);
24182418 \\ var k: u8 = '"';
2419 \\ _ = k;
2419 \\ _ = @TypeOf(k);
24202420 \\ return "'\\\x07\x08\x0c\n\r\t\x0b\x00\"";
24212421 \\}
24222422 });
......@@ -2612,7 +2612,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
26122612 \\pub export fn foo() c_int {
26132613 \\ return blk: {
26142614 \\ var a: c_int = 1;
2615 \\ _ = a;
2615 \\ _ = @TypeOf(a);
26162616 \\ break :blk a;
26172617 \\ };
26182618 \\}
......@@ -2716,7 +2716,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27162716 \\int bar(void) { return 0; }
27172717 , &[_][]const u8{
27182718 \\pub inline fn CALL(arg: anytype) @TypeOf(bar()) {
2719 \\ _ = arg;
2719 \\ _ = @TypeOf(arg);
27202720 \\ return bar();
27212721 \\}
27222722 });
......@@ -2775,14 +2775,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
27752775 \\pub export fn foo() void {
27762776 \\ if (true) {
27772777 \\ var a: c_int = 2;
2778 \\ _ = a;
2778 \\ _ = @TypeOf(a);
27792779 \\ }
27802780 \\ if ((blk: {
27812781 \\ _ = @as(c_int, 2);
27822782 \\ break :blk @as(c_int, 5);
27832783 \\ }) != 0) {
27842784 \\ var a: c_int = 2;
2785 \\ _ = a;
2785 \\ _ = @TypeOf(a);
27862786 \\ }
27872787 \\}
27882788 });
......@@ -3285,7 +3285,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
32853285 \\#define a 2
32863286 , &[_][]const u8{
32873287 \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*anyopaque, baz))) {
3288 \\ _ = bar;
3288 \\ _ = @TypeOf(bar);
32893289 \\ return baz(@import("std").zig.c_translation.cast(?*anyopaque, baz));
32903290 \\}
32913291 ,
......@@ -3425,7 +3425,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
34253425 , &[_][]const u8{
34263426 \\pub export fn foo(arg_a: [*c]c_int) void {
34273427 \\ var a = arg_a;
3428 \\ _ = a;
3428 \\ _ = @TypeOf(a);
34293429 \\}
34303430 \\pub export fn bar(arg_a: [*c]const c_int) void {
34313431 \\ var a = arg_a;
......@@ -3785,12 +3785,12 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
37853785 \\pub export fn bar(arg_x: c_int, arg_y: c_int) c_int {
37863786 \\ var x = arg_x;
37873787 \\ var y = arg_y;
3788 \\ _ = y;
3788 \\ _ = @TypeOf(y);
37893789 \\ return x;
37903790 \\}
37913791 ,
37923792 \\pub inline fn FOO(A: anytype, B: anytype) @TypeOf(A) {
3793 \\ _ = B;
3793 \\ _ = @TypeOf(B);
37943794 \\ return A;
37953795 \\}
37963796 });