From ae161863db544e2db20ba7705bf0ada7d3ce5c94 Mon Sep 17 00:00:00 2001 From: pfg Date: Tue, 6 Oct 2020 22:38:36 -0700 Subject: [PATCH] stage1: improve error messages for missing `try` statements --- src/stage1/ir.cpp | 8 ++++++-- test/compile_errors.zig | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index ebda9b88f6..2ec615f6b4 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -20102,7 +20102,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr, if (uncasted_value->value->type->id == ZigTypeIdErrorUnion || uncasted_value->value->type->id == ZigTypeIdErrorSet) { - ir_add_error(ira, source_instr, buf_sprintf("error is discarded")); + ir_add_error(ira, source_instr, buf_sprintf("error is discarded. consider using `try`, `catch`, or `if`")); return ira->codegen->invalid_inst_gen; } return ir_const_void(ira, source_instr); @@ -29492,7 +29492,11 @@ static IrInstGen *ir_analyze_instruction_check_statement_is_void(IrAnalyze *ira, return ira->codegen->invalid_inst_gen; if (statement_type->id != ZigTypeIdVoid && statement_type->id != ZigTypeIdUnreachable) { - ir_add_error(ira, &instruction->base.base, buf_sprintf("expression value is ignored")); + if(statement_type->id == ZigTypeIdErrorUnion || statement_type->id == ZigTypeIdErrorSet) { + ir_add_error(ira, &instruction->base.base, buf_sprintf("error is ignored. consider using `try`, `catch`, or `if`")); + }else{ + ir_add_error(ira, &instruction->base.base, buf_sprintf("expression value is ignored")); + } } return ir_const_void(ira, &instruction->base.base); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 816d569b4d..a733240d57 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2287,7 +2287,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return error.OutOfMemory; \\} , &[_][]const u8{ - "tmp.zig:2:12: error: error is discarded", + "tmp.zig:2:12: error: error is discarded. consider using `try`, `catch`, or `if`", }); cases.add("volatile on global assembly", @@ -2338,9 +2338,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return error.Bad; \\} , &[_][]const u8{ - "tmp.zig:2:24: error: expression value is ignored", - "tmp.zig:6:25: error: expression value is ignored", - "tmp.zig:10:25: error: expression value is ignored", + "tmp.zig:2:24: error: error is ignored. consider using `try`, `catch`, or `if`", + "tmp.zig:6:25: error: error is ignored. consider using `try`, `catch`, or `if`", + "tmp.zig:10:25: error: error is ignored. consider using `try`, `catch`, or `if`", }); cases.add("empty while loop body", @@ -6236,7 +6236,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} \\fn bar() anyerror!i32 { return 0; } , &[_][]const u8{ - "tmp.zig:2:14: error: expression value is ignored", + "tmp.zig:2:14: error: error is ignored. consider using `try`, `catch`, or `if`", }); cases.add("dereference an array",