From 8621e3b5bd814005129b58469d93c2499e3d085e Mon Sep 17 00:00:00 2001 From: Sam Tebbs Date: Wed, 7 Aug 2019 11:51:12 +0100 Subject: Don't emit clang error if source or filename pointer is null --- src/ir.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 65a21a418d..2b096a3383 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -21312,12 +21312,15 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct } for (size_t i = 0; i < errors_len; i += 1) { Stage2ErrorMsg *clang_err = &errors_ptr[i]; - ErrorMsg *err_msg = err_msg_create_with_offset( - clang_err->filename_ptr ? - buf_create_from_mem(clang_err->filename_ptr, clang_err->filename_len) : buf_alloc(), - clang_err->line, clang_err->column, clang_err->offset, clang_err->source, - buf_create_from_mem(clang_err->msg_ptr, clang_err->msg_len)); - err_msg_add_note(parent_err_msg, err_msg); + // Clang can emit "too many errors, stopping now", in which case `source` and `filename_ptr` are null + if (clang_err->source && clang_err->filename_ptr) { + ErrorMsg *err_msg = err_msg_create_with_offset( + clang_err->filename_ptr ? + buf_create_from_mem(clang_err->filename_ptr, clang_err->filename_len) : buf_alloc(), + clang_err->line, clang_err->column, clang_err->offset, clang_err->source, + buf_create_from_mem(clang_err->msg_ptr, clang_err->msg_len)); + err_msg_add_note(parent_err_msg, err_msg); + } } return ira->codegen->invalid_instruction; -- cgit v1.2.3 From 1b83ee78a48a64bef28f12b7b2e263074f88b6b6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 11 Aug 2019 12:00:32 -0400 Subject: allow comptime_int to implicit cast to comptime_float --- src/ir.cpp | 3 +++ std/math.zig | 7 +++++++ test/compile_errors.zig | 8 -------- test/stage1/behavior/cast.zig | 7 ++++++- 4 files changed, 16 insertions(+), 9 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 2b096a3383..13348d28c4 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9713,6 +9713,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat); assert(const_val_is_int || const_val_is_float); + if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) { + return true; + } if (other_type->id == ZigTypeIdFloat) { if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) { return true; diff --git a/std/math.zig b/std/math.zig index e10c9329d9..e47021512e 100644 --- a/std/math.zig +++ b/std/math.zig @@ -305,6 +305,13 @@ test "math.min" { testing.expect(@typeOf(result) == i16); testing.expect(result == -200); } + { + const a = 10.34; + var b: f32 = 999.12; + var result = min(a, b); + testing.expect(@typeOf(result) == f32); + testing.expect(result == 10.34); + } } pub fn max(x: var, y: var) @typeOf(x + y) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index a4bc2a66f0..437e40900d 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3225,14 +3225,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:17: note: value 8 cannot fit into type u3", ); - cases.add( - "incompatible number literals", - \\const x = 2 == 2.0; - \\export fn entry() usize { return @sizeOf(@typeOf(x)); } - , - "tmp.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'", - ); - cases.add( "missing function call param", \\const Foo = struct { diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index c243f18088..04c7fa606f 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" { } test "peer type resolution: unreachable, error set, unreachable" { - const Error = error { + const Error = error{ FileDescriptorAlreadyPresentInSet, OperationCausesCircularLoop, FileDescriptorNotRegistered, @@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" { }; expect(transformed_err == error.SystemResources); } + +test "implicit cast comptime_int to comptime_float" { + comptime expect(comptime_float(10) == f32(10)); + expect(2 == 2.0); +} -- cgit v1.2.3 From b87686dfa094770e96da33fb23a7d011a168157c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 11 Aug 2019 13:43:44 -0400 Subject: fix enum with one member and custom tag type --- src/ir.cpp | 1 - test/stage1/behavior/enum.zig | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 13348d28c4..fde2b972f8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11938,7 +11938,6 @@ static IrInstruction *ir_analyze_enum_to_int(IrAnalyze *ira, IrInstruction *sour if (enum_type->data.enumeration.layout == ContainerLayoutAuto && enum_type->data.enumeration.src_field_count == 1) { - assert(tag_type == ira->codegen->builtin_types.entry_num_lit_int); IrInstruction *result = ir_const(ira, source_instr, tag_type); init_const_bigint(&result->value, tag_type, &enum_type->data.enumeration.fields[0].value); diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 51f4f0e196..d7d34aec88 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -982,3 +982,14 @@ test "enum literal casting to tagged union" { else => @panic("fail"), } } + +test "enum with one member and custom tag type" { + const E = enum(u2) { + One, + }; + expect(@enumToInt(E.One) == 0); + const E2 = enum(u2) { + One = 2, + }; + expect(@enumToInt(E2.One) == 2); +} -- cgit v1.2.3