authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-26 19:50:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-26 19:50:52-05:00
log8ecd6c4d8c021f7778b4959bdf75204dfd2d1946
tree09f82cbd9d7698657537ac48d32dc4f362377b92
parenta6ef83cccfd1c586346c374d0f011e29d465da0e
signaturelock-open Commit is signed but in an unrecognized format.

add compiler note for bad int coercion

closes #3724

2 files changed, 28 insertions(+), 0 deletions(-)

src/ir.cpp+27
......@@ -71,6 +71,7 @@ enum ConstCastResultId {
7171 ConstCastResultIdPtrLens,
7272 ConstCastResultIdCV,
7373 ConstCastResultIdPtrSentinel,
74 ConstCastResultIdIntShorten,
7475};
7576
7677struct ConstCastOnly;
......@@ -97,6 +98,7 @@ struct ConstCastBadAllowsZero;
9798struct ConstCastBadNullTermArrays;
9899struct ConstCastBadCV;
99100struct ConstCastPtrSentinel;
101struct ConstCastIntShorten;
100102
101103struct ConstCastOnly {
102104 ConstCastResultId id;
......@@ -117,6 +119,7 @@ struct ConstCastOnly {
117119 ConstCastBadNullTermArrays *sentinel_arrays;
118120 ConstCastBadCV *bad_cv;
119121 ConstCastPtrSentinel *bad_ptr_sentinel;
122 ConstCastIntShorten *int_shorten;
120123 } data;
121124};
122125
......@@ -186,6 +189,11 @@ struct ConstCastPtrSentinel {
186189 ZigType *actual_type;
187190};
188191
192struct ConstCastIntShorten {
193 ZigType *wanted_type;
194 ZigType *actual_type;
195};
196
189197static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
190198static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
191199 ResultLoc *result_loc);
......@@ -10213,6 +10221,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
1021310221 return result;
1021410222 }
1021510223
10224 if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) {
10225 result.id = ConstCastResultIdIntShorten;
10226 result.data.int_shorten = allocate_nonzero<ConstCastIntShorten>(1);
10227 result.data.int_shorten->wanted_type = wanted_type;
10228 result.data.int_shorten->actual_type = actual_type;
10229 return result;
10230 }
10231
1021610232 result.id = ConstCastResultIdType;
1021710233 result.data.type_mismatch = allocate_nonzero<ConstCastTypeMismatch>(1);
1021810234 result.data.type_mismatch->wanted_type = wanted_type;
......@@ -12733,6 +12749,17 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1273312749 add_error_note(ira->codegen, parent_msg, source_node,
1273412750 buf_sprintf("calling convention mismatch"));
1273512751 break;
12752 case ConstCastResultIdIntShorten: {
12753 ZigType *wanted_type = cast_result->data.int_shorten->wanted_type;
12754 ZigType *actual_type = cast_result->data.int_shorten->actual_type;
12755 const char *wanted_signed = wanted_type->data.integral.is_signed ? "signed" : "unsigned";
12756 const char *actual_signed = wanted_type->data.integral.is_signed ? "signed" : "unsigned";
12757 add_error_note(ira->codegen, parent_msg, source_node,
12758 buf_sprintf("%s %" PRIu32 "-bit int cannot represent all possible %s %" PRIu32 "-bit values",
12759 wanted_signed, wanted_type->data.integral.bit_count,
12760 actual_signed, actual_type->data.integral.bit_count));
12761 break;
12762 }
1273612763 case ConstCastResultIdFnAlign: // TODO
1273712764 case ConstCastResultIdFnVarArgs: // TODO
1273812765 case ConstCastResultIdFnReturnType: // TODO
test/compile_errors.zig+1
......@@ -1648,6 +1648,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
16481648 "tmp.zig:3:31: error: integer value 300 cannot be coerced to type 'u8'",
16491649 "tmp.zig:7:22: error: integer value 300 cannot be coerced to type 'u8'",
16501650 "tmp.zig:11:20: error: expected type 'u8', found 'u16'",
1651 "tmp.zig:11:20: note: unsigned 8-bit int cannot represent all possible unsigned 16-bit values",
16511652 );
16521653
16531654 cases.add(