From cf7de64f1a136c9208284362db6cc1d3c81f1111 Mon Sep 17 00:00:00 2001 From: pfg Date: Tue, 13 Oct 2020 22:29:41 -0700 Subject: [PATCH] stage1: improve error for missing a number type on a runtime var --- src/stage1/ir.cpp | 5 ++++- test/compile_errors.zig | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 738ef3c65d..2ed6675930 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -18804,9 +18804,12 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV case ReqCompTimeYes: var_class_requires_const = true; if (!var->gen_is_const && !is_comptime_var) { - ir_add_error_node(ira, source_node, + ErrorMsg *msg = ir_add_error_node(ira, source_node, buf_sprintf("variable of type '%s' must be const or comptime", buf_ptr(&result_type->name))); + if(result_type->id == ZigTypeIdComptimeInt || result_type -> id == ZigTypeIdComptimeFloat) { + add_error_note(ira->codegen, msg, source_node, buf_sprintf("to modify this variable at runtime, it must be given an explicit fixed-size number type")); + } result_type = ira->codegen->builtin_types.entry_invalid; } break; diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 4d1f86b5a8..48775c3bcb 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -6990,7 +6990,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:4: error: variable of type '*const comptime_int' must be const or comptime", "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime", "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime", + "tmp.zig:8:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type", "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime", + "tmp.zig:11:4: note: to modify this variable at runtime, it must be given an explicit fixed-size number type", "tmp.zig:14:4: error: variable of type '(null)' must be const or comptime", "tmp.zig:17:4: error: variable of type 'Opaque' not allowed", "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",