From c1af3605328d21f59ee8ceba3c7350193f0a2429 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 18 Sep 2018 16:32:40 -0400 Subject: [PATCH] add compile error for slice.*.len closes #1372 --- src/ir.cpp | 7 ++++++- test/compile_errors.zig | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ir.cpp b/src/ir.cpp index 350459db23..0143445976 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -14874,8 +14874,13 @@ static ZigType *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFi if (type_is_invalid(container_ptr->value.type)) return ira->codegen->builtin_types.entry_invalid; + if (container_ptr->value.type->id != ZigTypeIdPointer) { + ir_add_error_node(ira, field_ptr_instruction->base.source_node, + buf_sprintf("attempt to dereference non-pointer type '%s'", + buf_ptr(&container_ptr->value.type->name))); + return ira->codegen->builtin_types.entry_invalid; + } ZigType *container_type = container_ptr->value.type->data.pointer.child_type; - assert(container_ptr->value.type->id == ZigTypeIdPointer); Buf *field_name = field_ptr_instruction->field_name_buffer; if (!field_name) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index a1dd33fb43..0b1446b518 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,16 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "deref slice and get len field", + \\export fn entry() void { + \\ var a: []u8 = undefined; + \\ _ = a.*.len; + \\} + , + ".tmp_source.zig:3:12: error: attempt to dereference non-pointer type '[]u8'", + ); + cases.add( "@ptrCast a 0 bit type to a non- 0 bit type", \\export fn entry() bool {