authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-06 16:37:03-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-06 16:37:03-05:00
log07e47c058c480914d45ec0b1f9c61b76f59e6299
treeebbe6e0a1e2982de491708e3f538cad12a0eddda
parent46e258c9f76cee5b37042913c34b3a1a07cce0a6

ptrCast builtin now gives an error for removing const qualifier

closes #384

10 files changed, 34 insertions(+), 11 deletions(-)

src/analyze.cpp+13
......@@ -3753,6 +3753,19 @@ uint32_t get_ptr_align(TypeTableEntry *type) {
37533753 }
37543754}
37553755
3756bool get_ptr_const(TypeTableEntry *type) {
3757 TypeTableEntry *ptr_type = get_codegen_ptr_type(type);
3758 if (ptr_type->id == TypeTableEntryIdPointer) {
3759 return ptr_type->data.pointer.is_const;
3760 } else if (ptr_type->id == TypeTableEntryIdFn) {
3761 return true;
3762 } else if (ptr_type->id == TypeTableEntryIdPromise) {
3763 return true;
3764 } else {
3765 zig_unreachable();
3766 }
3767}
3768
37563769AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index) {
37573770 if (fn_entry->param_source_nodes)
37583771 return fn_entry->param_source_nodes[index];
src/analyze.hpp+1
......@@ -55,6 +55,7 @@ bool type_is_codegen_pointer(TypeTableEntry *type);
5555
5656TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type);
5757uint32_t get_ptr_align(TypeTableEntry *type);
58bool get_ptr_const(TypeTableEntry *type);
5859TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);
5960TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);
6061bool type_is_complete(TypeTableEntry *type_entry);
src/ir.cpp+5
......@@ -16816,6 +16816,11 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc
1681616816 return ira->codegen->builtin_types.entry_invalid;
1681716817 }
1681816818
16819 if (get_ptr_const(src_type) && !get_ptr_const(dest_type)) {
16820 ir_add_error(ira, &instruction->base, buf_sprintf("cast discards const qualifier"));
16821 return ira->codegen->builtin_types.entry_invalid;
16822 }
16823
1681916824 if (instr_is_comptime(ptr)) {
1682016825 ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk);
1682116826 if (!val)
std/buf_map.zig+1-3
......@@ -62,9 +62,7 @@ pub const BufMap = struct {
6262 }
6363
6464 fn free(self: &BufMap, value: []const u8) void {
65 // remove the const
66 const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
67 self.hash_map.allocator.free(mut_value);
65 self.hash_map.allocator.free(value);
6866 }
6967
7068 fn copy(self: &BufMap, value: []const u8) ![]const u8 {
std/buf_set.zig+1-3
......@@ -50,9 +50,7 @@ pub const BufSet = struct {
5050 }
5151
5252 fn free(self: &BufSet, value: []const u8) void {
53 // remove the const
54 const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
55 self.hash_map.allocator.free(mut_value);
53 self.hash_map.allocator.free(value);
5654 }
5755
5856 fn copy(self: &BufSet, value: []const u8) ![]const u8 {
std/os/index.zig+1-1
......@@ -1634,7 +1634,7 @@ pub fn argsFree(allocator: &mem.Allocator, args_alloc: []const []u8) void {
16341634 for (args_alloc) |arg| {
16351635 total_bytes += @sizeOf([]u8) + arg.len;
16361636 }
1637 const unaligned_allocated_buf = @ptrCast(&u8, args_alloc.ptr)[0..total_bytes];
1637 const unaligned_allocated_buf = @ptrCast(&const u8, args_alloc.ptr)[0..total_bytes];
16381638 const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf);
16391639 return allocator.free(aligned_allocated_buf);
16401640}
std/special/compiler_rt/udivmod.zig+2-2
......@@ -11,8 +11,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
1111 const SignedDoubleInt = @IntType(true, DoubleInt.bit_count);
1212 const Log2SingleInt = @import("../../math/index.zig").Log2Int(SingleInt);
1313
14 const n = *@ptrCast(&[2]SingleInt, &a); // TODO issue #421
15 const d = *@ptrCast(&[2]SingleInt, &b); // TODO issue #421
14 const n = *@ptrCast(&const [2]SingleInt, &a); // TODO issue #421
15 const d = *@ptrCast(&const [2]SingleInt, &b); // TODO issue #421
1616 var q: [2]SingleInt = undefined;
1717 var r: [2]SingleInt = undefined;
1818 var sr: c_uint = undefined;
test/cases/cast.zig+1-1
......@@ -16,7 +16,7 @@ test "integer literal to pointer cast" {
1616test "pointer reinterpret const float to int" {
1717 const float: f64 = 5.99999999999994648725e-01;
1818 const float_ptr = &float;
19 const int_ptr = @ptrCast(&i32, float_ptr);
19 const int_ptr = @ptrCast(&const i32, float_ptr);
2020 const int_val = *int_ptr;
2121 assert(int_val == 858993411);
2222}
test/cases/misc.zig+1-1
......@@ -261,7 +261,7 @@ test "generic malloc free" {
261261 const a = memAlloc(u8, 10) catch unreachable;
262262 memFree(u8, a);
263263}
264const some_mem : [100]u8 = undefined;
264var some_mem : [100]u8 = undefined;
265265fn memAlloc(comptime T: type, n: usize) error![]T {
266266 return @ptrCast(&T, &some_mem[0])[0..n];
267267}
test/compile_errors.zig+8
......@@ -1,6 +1,14 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: &tests.CompileErrorContext) void {
4 cases.add("@ptrCast discards const qualifier",
5 \\export fn entry() void {
6 \\ const x: i32 = 1234;
7 \\ const y = @ptrCast(&i32, &x);
8 \\}
9 ,
10 ".tmp_source.zig:3:15: error: cast discards const qualifier");
11
412 cases.add("comptime slice of undefined pointer non-zero len",
513 \\export fn entry() void {
614 \\ const slice = (&i32)(undefined)[0..1];