authorgravatar for pfg@pfg.pwpfg <pfg@pfg.pw> 2020-10-06 22:38:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-07 03:50:11-04:00
logae161863db544e2db20ba7705bf0ada7d3ce5c94
treebe0b211bad4d896ff107dab38ef398bc00d4dabe
parentf2d374e8465042fa5cb6bf2be7b9b086948f3a94

stage1: improve error messages for missing `try` statements


2 files changed, 11 insertions(+), 7 deletions(-)

src/stage1/ir.cpp+6-2
......@@ -20102,7 +20102,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,
2010220102 if (uncasted_value->value->type->id == ZigTypeIdErrorUnion ||
2010320103 uncasted_value->value->type->id == ZigTypeIdErrorSet)
2010420104 {
20105 ir_add_error(ira, source_instr, buf_sprintf("error is discarded"));
20105 ir_add_error(ira, source_instr, buf_sprintf("error is discarded. consider using `try`, `catch`, or `if`"));
2010620106 return ira->codegen->invalid_inst_gen;
2010720107 }
2010820108 return ir_const_void(ira, source_instr);
......@@ -29492,7 +29492,11 @@ static IrInstGen *ir_analyze_instruction_check_statement_is_void(IrAnalyze *ira,
2949229492 return ira->codegen->invalid_inst_gen;
2949329493
2949429494 if (statement_type->id != ZigTypeIdVoid && statement_type->id != ZigTypeIdUnreachable) {
29495 ir_add_error(ira, &instruction->base.base, buf_sprintf("expression value is ignored"));
29495 if(statement_type->id == ZigTypeIdErrorUnion || statement_type->id == ZigTypeIdErrorSet) {
29496 ir_add_error(ira, &instruction->base.base, buf_sprintf("error is ignored. consider using `try`, `catch`, or `if`"));
29497 }else{
29498 ir_add_error(ira, &instruction->base.base, buf_sprintf("expression value is ignored"));
29499 }
2949629500 }
2949729501
2949829502 return ir_const_void(ira, &instruction->base.base);
test/compile_errors.zig+5-5
......@@ -2287,7 +2287,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
22872287 \\ return error.OutOfMemory;
22882288 \\}
22892289 , &[_][]const u8{
2290 "tmp.zig:2:12: error: error is discarded",
2290 "tmp.zig:2:12: error: error is discarded. consider using `try`, `catch`, or `if`",
22912291 });
22922292
22932293 cases.add("volatile on global assembly",
......@@ -2338,9 +2338,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
23382338 \\ return error.Bad;
23392339 \\}
23402340 , &[_][]const u8{
2341 "tmp.zig:2:24: error: expression value is ignored",
2342 "tmp.zig:6:25: error: expression value is ignored",
2343 "tmp.zig:10:25: error: expression value is ignored",
2341 "tmp.zig:2:24: error: error is ignored. consider using `try`, `catch`, or `if`",
2342 "tmp.zig:6:25: error: error is ignored. consider using `try`, `catch`, or `if`",
2343 "tmp.zig:10:25: error: error is ignored. consider using `try`, `catch`, or `if`",
23442344 });
23452345
23462346 cases.add("empty while loop body",
......@@ -6236,7 +6236,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
62366236 \\}
62376237 \\fn bar() anyerror!i32 { return 0; }
62386238 , &[_][]const u8{
6239 "tmp.zig:2:14: error: expression value is ignored",
6239 "tmp.zig:2:14: error: error is ignored. consider using `try`, `catch`, or `if`",
62406240 });
62416241
62426242 cases.add("dereference an array",