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,...@@ -20102,7 +20102,7 @@ static IrInstGen *ir_analyze_store_ptr(IrAnalyze *ira, IrInst* source_instr,
20102 if (uncasted_value->value->type->id == ZigTypeIdErrorUnion ||20102 if (uncasted_value->value->type->id == ZigTypeIdErrorUnion ||
20103 uncasted_value->value->type->id == ZigTypeIdErrorSet)20103 uncasted_value->value->type->id == ZigTypeIdErrorSet)
20104 {20104 {
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`"));
20106 return ira->codegen->invalid_inst_gen;20106 return ira->codegen->invalid_inst_gen;
20107 }20107 }
20108 return ir_const_void(ira, source_instr);20108 return ir_const_void(ira, source_instr);
...@@ -29492,7 +29492,11 @@ static IrInstGen *ir_analyze_instruction_check_statement_is_void(IrAnalyze *ira,...@@ -29492,7 +29492,11 @@ static IrInstGen *ir_analyze_instruction_check_statement_is_void(IrAnalyze *ira,
29492 return ira->codegen->invalid_inst_gen;29492 return ira->codegen->invalid_inst_gen;
2949329493
29494 if (statement_type->id != ZigTypeIdVoid && statement_type->id != ZigTypeIdUnreachable) {29494 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 }
29496 }29500 }
2949729501
29498 return ir_const_void(ira, &instruction->base.base);29502 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 {...@@ -2287,7 +2287,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2287 \\ return error.OutOfMemory;2287 \\ return error.OutOfMemory;
2288 \\}2288 \\}
2289 , &[_][]const u8{2289 , &[_][]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`",
2291 });2291 });
22922292
2293 cases.add("volatile on global assembly",2293 cases.add("volatile on global assembly",
...@@ -2338,9 +2338,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -2338,9 +2338,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2338 \\ return error.Bad;2338 \\ return error.Bad;
2339 \\}2339 \\}
2340 , &[_][]const u8{2340 , &[_][]const u8{
2341 "tmp.zig:2:24: 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: expression value is ignored",2342 "tmp.zig:6:25: error: error is ignored. consider using `try`, `catch`, or `if`",
2343 "tmp.zig:10:25: error: expression value is ignored",2343 "tmp.zig:10:25: error: error is ignored. consider using `try`, `catch`, or `if`",
2344 });2344 });
23452345
2346 cases.add("empty while loop body",2346 cases.add("empty while loop body",
...@@ -6236,7 +6236,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -6236,7 +6236,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
6236 \\}6236 \\}
6237 \\fn bar() anyerror!i32 { return 0; }6237 \\fn bar() anyerror!i32 { return 0; }
6238 , &[_][]const u8{6238 , &[_][]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`",
6240 });6240 });
62416241
6242 cases.add("dereference an array",6242 cases.add("dereference an array",