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 {...@@ -71,6 +71,7 @@ enum ConstCastResultId {
71 ConstCastResultIdPtrLens,71 ConstCastResultIdPtrLens,
72 ConstCastResultIdCV,72 ConstCastResultIdCV,
73 ConstCastResultIdPtrSentinel,73 ConstCastResultIdPtrSentinel,
74 ConstCastResultIdIntShorten,
74};75};
7576
76struct ConstCastOnly;77struct ConstCastOnly;
...@@ -97,6 +98,7 @@ struct ConstCastBadAllowsZero;...@@ -97,6 +98,7 @@ struct ConstCastBadAllowsZero;
97struct ConstCastBadNullTermArrays;98struct ConstCastBadNullTermArrays;
98struct ConstCastBadCV;99struct ConstCastBadCV;
99struct ConstCastPtrSentinel;100struct ConstCastPtrSentinel;
101struct ConstCastIntShorten;
100102
101struct ConstCastOnly {103struct ConstCastOnly {
102 ConstCastResultId id;104 ConstCastResultId id;
...@@ -117,6 +119,7 @@ struct ConstCastOnly {...@@ -117,6 +119,7 @@ struct ConstCastOnly {
117 ConstCastBadNullTermArrays *sentinel_arrays;119 ConstCastBadNullTermArrays *sentinel_arrays;
118 ConstCastBadCV *bad_cv;120 ConstCastBadCV *bad_cv;
119 ConstCastPtrSentinel *bad_ptr_sentinel;121 ConstCastPtrSentinel *bad_ptr_sentinel;
122 ConstCastIntShorten *int_shorten;
120 } data;123 } data;
121};124};
122125
...@@ -186,6 +189,11 @@ struct ConstCastPtrSentinel {...@@ -186,6 +189,11 @@ struct ConstCastPtrSentinel {
186 ZigType *actual_type;189 ZigType *actual_type;
187};190};
188191
192struct ConstCastIntShorten {
193 ZigType *wanted_type;
194 ZigType *actual_type;
195};
196
189static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);197static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
190static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,198static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval,
191 ResultLoc *result_loc);199 ResultLoc *result_loc);
...@@ -10213,6 +10221,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -10213,6 +10221,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
10213 return result;10221 return result;
10214 }10222 }
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
10216 result.id = ConstCastResultIdType;10232 result.id = ConstCastResultIdType;
10217 result.data.type_mismatch = allocate_nonzero<ConstCastTypeMismatch>(1);10233 result.data.type_mismatch = allocate_nonzero<ConstCastTypeMismatch>(1);
10218 result.data.type_mismatch->wanted_type = wanted_type;10234 result.data.type_mismatch->wanted_type = wanted_type;
...@@ -12733,6 +12749,17 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa...@@ -12733,6 +12749,17 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
12733 add_error_note(ira->codegen, parent_msg, source_node,12749 add_error_note(ira->codegen, parent_msg, source_node,
12734 buf_sprintf("calling convention mismatch"));12750 buf_sprintf("calling convention mismatch"));
12735 break;12751 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 }
12736 case ConstCastResultIdFnAlign: // TODO12763 case ConstCastResultIdFnAlign: // TODO
12737 case ConstCastResultIdFnVarArgs: // TODO12764 case ConstCastResultIdFnVarArgs: // TODO
12738 case ConstCastResultIdFnReturnType: // TODO12765 case ConstCastResultIdFnReturnType: // TODO
test/compile_errors.zig+1
...@@ -1648,6 +1648,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -1648,6 +1648,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1648 "tmp.zig:3:31: error: integer value 300 cannot be coerced to type 'u8'",1648 "tmp.zig:3:31: error: integer value 300 cannot be coerced to type 'u8'",
1649 "tmp.zig:7:22: error: integer value 300 cannot be coerced to type 'u8'",1649 "tmp.zig:7:22: error: integer value 300 cannot be coerced to type 'u8'",
1650 "tmp.zig:11:20: error: expected type 'u8', found 'u16'",1650 "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",
1651 );1652 );
16521653
1653 cases.add(1654 cases.add(