zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit ca70ca7e26aaae3425dad3a2b179f544bacf45e3 (tree)
parent 4a5bc89862aca6f1870cbaa7d398ab2eed3022c3
Author: Timon Kruiper <timonkruiper@gmail.com>
Date:   Thu,  5 Sep 2019 18:43:54 +0200

Add compiler error when negating invalid type

Diffstat:
Msrc/ir.cpp | 9+++++++++
Mtest/compile_errors.zig | 13+++++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -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; diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -3,6 +3,19 @@ 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"); \\comptime {