Skip to content

Commit

Permalink
ptrCast builtin now gives an error for removing const qualifier
Browse files Browse the repository at this point in the history
closes #384
  • Loading branch information
andrewrk committed Mar 6, 2018
1 parent 46e258c commit 07e47c0
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/analyze.cpp
Expand Up @@ -3753,6 +3753,19 @@ uint32_t get_ptr_align(TypeTableEntry *type) {
}
}

bool get_ptr_const(TypeTableEntry *type) {
TypeTableEntry *ptr_type = get_codegen_ptr_type(type);
if (ptr_type->id == TypeTableEntryIdPointer) {
return ptr_type->data.pointer.is_const;
} else if (ptr_type->id == TypeTableEntryIdFn) {
return true;
} else if (ptr_type->id == TypeTableEntryIdPromise) {
return true;
} else {
zig_unreachable();
}
}

AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index) {
if (fn_entry->param_source_nodes)
return fn_entry->param_source_nodes[index];
Expand Down
1 change: 1 addition & 0 deletions src/analyze.hpp
Expand Up @@ -55,6 +55,7 @@ bool type_is_codegen_pointer(TypeTableEntry *type);

TypeTableEntry *get_codegen_ptr_type(TypeTableEntry *type);
uint32_t get_ptr_align(TypeTableEntry *type);
bool get_ptr_const(TypeTableEntry *type);
TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry);
TypeTableEntry *container_ref_type(TypeTableEntry *type_entry);
bool type_is_complete(TypeTableEntry *type_entry);
Expand Down
5 changes: 5 additions & 0 deletions src/ir.cpp
Expand Up @@ -16816,6 +16816,11 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc
return ira->codegen->builtin_types.entry_invalid;
}

if (get_ptr_const(src_type) && !get_ptr_const(dest_type)) {
ir_add_error(ira, &instruction->base, buf_sprintf("cast discards const qualifier"));
return ira->codegen->builtin_types.entry_invalid;
}

if (instr_is_comptime(ptr)) {
ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk);
if (!val)
Expand Down
4 changes: 1 addition & 3 deletions std/buf_map.zig
Expand Up @@ -62,9 +62,7 @@ pub const BufMap = struct {
}

fn free(self: &BufMap, value: []const u8) void {
// remove the const
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
self.hash_map.allocator.free(mut_value);
self.hash_map.allocator.free(value);
}

fn copy(self: &BufMap, value: []const u8) ![]const u8 {
Expand Down
4 changes: 1 addition & 3 deletions std/buf_set.zig
Expand Up @@ -50,9 +50,7 @@ pub const BufSet = struct {
}

fn free(self: &BufSet, value: []const u8) void {
// remove the const
const mut_value = @ptrCast(&u8, value.ptr)[0..value.len];
self.hash_map.allocator.free(mut_value);
self.hash_map.allocator.free(value);
}

fn copy(self: &BufSet, value: []const u8) ![]const u8 {
Expand Down
2 changes: 1 addition & 1 deletion std/os/index.zig
Expand Up @@ -1634,7 +1634,7 @@ pub fn argsFree(allocator: &mem.Allocator, args_alloc: []const []u8) void {
for (args_alloc) |arg| {
total_bytes += @sizeOf([]u8) + arg.len;
}
const unaligned_allocated_buf = @ptrCast(&u8, args_alloc.ptr)[0..total_bytes];
const unaligned_allocated_buf = @ptrCast(&const u8, args_alloc.ptr)[0..total_bytes];
const aligned_allocated_buf = @alignCast(@alignOf([]u8), unaligned_allocated_buf);
return allocator.free(aligned_allocated_buf);
}
Expand Down
4 changes: 2 additions & 2 deletions std/special/compiler_rt/udivmod.zig
Expand Up @@ -11,8 +11,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
const SignedDoubleInt = @IntType(true, DoubleInt.bit_count);
const Log2SingleInt = @import("../../math/index.zig").Log2Int(SingleInt);

const n = *@ptrCast(&[2]SingleInt, &a); // TODO issue #421
const d = *@ptrCast(&[2]SingleInt, &b); // TODO issue #421
const n = *@ptrCast(&const [2]SingleInt, &a); // TODO issue #421
const d = *@ptrCast(&const [2]SingleInt, &b); // TODO issue #421
var q: [2]SingleInt = undefined;
var r: [2]SingleInt = undefined;
var sr: c_uint = undefined;
Expand Down
2 changes: 1 addition & 1 deletion test/cases/cast.zig
Expand Up @@ -16,7 +16,7 @@ test "integer literal to pointer cast" {
test "pointer reinterpret const float to int" {
const float: f64 = 5.99999999999994648725e-01;
const float_ptr = &float;
const int_ptr = @ptrCast(&i32, float_ptr);
const int_ptr = @ptrCast(&const i32, float_ptr);
const int_val = *int_ptr;
assert(int_val == 858993411);
}
Expand Down
2 changes: 1 addition & 1 deletion test/cases/misc.zig
Expand Up @@ -261,7 +261,7 @@ test "generic malloc free" {
const a = memAlloc(u8, 10) catch unreachable;
memFree(u8, a);
}
const some_mem : [100]u8 = undefined;
var some_mem : [100]u8 = undefined;
fn memAlloc(comptime T: type, n: usize) error![]T {
return @ptrCast(&T, &some_mem[0])[0..n];
}
Expand Down
8 changes: 8 additions & 0 deletions test/compile_errors.zig
@@ -1,6 +1,14 @@
const tests = @import("tests.zig");

pub fn addCases(cases: &tests.CompileErrorContext) void {
cases.add("@ptrCast discards const qualifier",
\\export fn entry() void {
\\ const x: i32 = 1234;
\\ const y = @ptrCast(&i32, &x);
\\}
,
".tmp_source.zig:3:15: error: cast discards const qualifier");

cases.add("comptime slice of undefined pointer non-zero len",
\\export fn entry() void {
\\ const slice = (&i32)(undefined)[0..1];
Expand Down

0 comments on commit 07e47c0

Please sign in to comment.