Add compiler error when negating invalid type

This commit is contained in:
Timon Kruiper 2019-09-05 18:43:54 +02:00 committed by Andrew Kelley
parent 4a5bc89862
commit ca70ca7e26
2 changed files with 22 additions and 0 deletions

View File

@ -16565,6 +16565,15 @@ static IrInstruction *ir_analyze_negation(IrAnalyze *ira, IrInstructionUnOp *ins
if (type_is_invalid(expr_type))
return ira->codegen->invalid_instruction;
if (!(expr_type->id == ZigTypeIdInt || expr_type->id == ZigTypeIdComptimeInt ||
expr_type->id == ZigTypeIdFloat || expr_type->id == ZigTypeIdComptimeFloat ||
expr_type->id == ZigTypeIdVector))
{
ir_add_error(ira, &instruction->base,
buf_sprintf("negation of type '%s'", buf_ptr(&expr_type->name)));
return ira->codegen->invalid_instruction;
}
bool is_wrap_op = (instruction->op_id == IrUnOpNegationWrap);
ZigType *scalar_type = (expr_type->id == ZigTypeIdVector) ? expr_type->data.vector.elem_type : expr_type;

View File

@ -2,6 +2,19 @@ const tests = @import("tests.zig");
const builtin = @import("builtin");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"attempt to negate a non-integer, non-float or non-vector type",
\\fn foo() anyerror!u32 {
\\ return 1;
\\}
\\
\\export fn entry() void {
\\ const x = -foo();
\\}
,
"tmp.zig:6:15: error: negation of type 'anyerror!u32'",
);
cases.add(
"attempt to create 17 bit float type",
\\const builtin = @import("builtin");