From 8ecd6c4d8c021f7778b4959bdf75204dfd2d1946 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 26 Nov 2019 19:50:52 -0500 Subject: [PATCH] add compiler note for bad int coercion closes #3724 --- src/ir.cpp | 27 +++++++++++++++++++++++++++ test/compile_errors.zig | 1 + 2 files changed, 28 insertions(+) diff --git a/src/ir.cpp b/src/ir.cpp index db135092e..18d7698c7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -71,6 +71,7 @@ enum ConstCastResultId { ConstCastResultIdPtrLens, ConstCastResultIdCV, ConstCastResultIdPtrSentinel, + ConstCastResultIdIntShorten, }; struct ConstCastOnly; @@ -97,6 +98,7 @@ struct ConstCastBadAllowsZero; struct ConstCastBadNullTermArrays; struct ConstCastBadCV; struct ConstCastPtrSentinel; +struct ConstCastIntShorten; struct ConstCastOnly { ConstCastResultId id; @@ -117,6 +119,7 @@ struct ConstCastOnly { ConstCastBadNullTermArrays *sentinel_arrays; ConstCastBadCV *bad_cv; ConstCastPtrSentinel *bad_ptr_sentinel; + ConstCastIntShorten *int_shorten; } data; }; @@ -186,6 +189,11 @@ struct ConstCastPtrSentinel { ZigType *actual_type; }; +struct ConstCastIntShorten { + ZigType *wanted_type; + ZigType *actual_type; +}; + static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope); static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval, ResultLoc *result_loc); @@ -10213,6 +10221,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted return result; } + if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) { + result.id = ConstCastResultIdIntShorten; + result.data.int_shorten = allocate_nonzero(1); + result.data.int_shorten->wanted_type = wanted_type; + result.data.int_shorten->actual_type = actual_type; + return result; + } + result.id = ConstCastResultIdType; result.data.type_mismatch = allocate_nonzero(1); result.data.type_mismatch->wanted_type = wanted_type; @@ -12733,6 +12749,17 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa add_error_note(ira->codegen, parent_msg, source_node, buf_sprintf("calling convention mismatch")); break; + case ConstCastResultIdIntShorten: { + ZigType *wanted_type = cast_result->data.int_shorten->wanted_type; + ZigType *actual_type = cast_result->data.int_shorten->actual_type; + const char *wanted_signed = wanted_type->data.integral.is_signed ? "signed" : "unsigned"; + const char *actual_signed = wanted_type->data.integral.is_signed ? "signed" : "unsigned"; + add_error_note(ira->codegen, parent_msg, source_node, + buf_sprintf("%s %" PRIu32 "-bit int cannot represent all possible %s %" PRIu32 "-bit values", + wanted_signed, wanted_type->data.integral.bit_count, + actual_signed, actual_type->data.integral.bit_count)); + break; + } case ConstCastResultIdFnAlign: // TODO case ConstCastResultIdFnVarArgs: // TODO case ConstCastResultIdFnReturnType: // TODO diff --git a/test/compile_errors.zig b/test/compile_errors.zig index c1e2d579a..f2600d2d7 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1648,6 +1648,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:31: error: integer value 300 cannot be coerced to type 'u8'", "tmp.zig:7:22: error: integer value 300 cannot be coerced to type 'u8'", "tmp.zig:11:20: error: expected type 'u8', found 'u16'", + "tmp.zig:11:20: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values", ); cases.add(