Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ var some_integers: [100]i32 = undefined;

test "modify an array" {
for (some_integers) |*item, i| {
item.* = i32(i);
item.* = @intCast(i32, i);
}
assert(some_integers[10] == 10);
assert(some_integers[99] == 99);
Expand Down Expand Up @@ -1397,8 +1397,8 @@ var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (initial_value) |*pt, i| {
pt.* = Point{
.x = i32(i),
.y = i32(i) * 2,
.x = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
};
}
break :init initial_value;
Expand Down Expand Up @@ -2410,7 +2410,7 @@ test "for basics" {
var sum2: i32 = 0;
for (items) |value, i| {
assert(@typeOf(i) == usize);
sum2 += i32(i);
sum2 += @intCast(i32, i);
}
assert(sum2 == 10);
}
Expand Down Expand Up @@ -5730,7 +5730,7 @@ comptime {
{#code_begin|test_err|attempt to cast negative value to unsigned integer#}
comptime {
const value: i32 = -1;
const unsigned = u32(value);
const unsigned = @intCast(u32, value);
}
{#code_end#}
<p>At runtime crashes with the message <code>attempt to cast negative value to unsigned integer</code> and a stack trace.</p>
Expand All @@ -5744,7 +5744,7 @@ comptime {
{#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#}
comptime {
const spartan_count: u16 = 300;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
}
{#code_end#}
<p>At runtime crashes with the message <code>integer cast truncated bits</code> and a stack trace.</p>
Expand Down
2 changes: 1 addition & 1 deletion example/hello_world/hello_libc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const c = @cImport({
const msg = c"Hello, world!\n";

export fn main(argc: c_int, argv: **u8) c_int {
if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;

return 0;
}
36 changes: 36 additions & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,10 @@ enum BuiltinFnId {
BuiltinFnIdMod,
BuiltinFnIdSqrt,
BuiltinFnIdTruncate,
BuiltinFnIdIntCast,
BuiltinFnIdFloatCast,
BuiltinFnIdIntToFloat,
BuiltinFnIdFloatToInt,
BuiltinFnIdIntType,
BuiltinFnIdSetCold,
BuiltinFnIdSetRuntimeSafety,
Expand Down Expand Up @@ -2040,6 +2044,10 @@ enum IrInstructionId {
IrInstructionIdCmpxchg,
IrInstructionIdFence,
IrInstructionIdTruncate,
IrInstructionIdIntCast,
IrInstructionIdFloatCast,
IrInstructionIdIntToFloat,
IrInstructionIdFloatToInt,
IrInstructionIdIntType,
IrInstructionIdBoolNot,
IrInstructionIdMemset,
Expand Down Expand Up @@ -2632,6 +2640,34 @@ struct IrInstructionTruncate {
IrInstruction *target;
};

struct IrInstructionIntCast {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionFloatCast {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionIntToFloat {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionFloatToInt {
IrInstruction base;

IrInstruction *dest_type;
IrInstruction *target;
};

struct IrInstructionIntType {
IrInstruction base;

Expand Down
8 changes: 8 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdPromiseResultType:
case IrInstructionIdAwaitBookkeeping:
case IrInstructionIdAddImplicitReturnType:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
zig_unreachable();

case IrInstructionIdReturn:
Expand Down Expand Up @@ -6310,6 +6314,10 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6);
create_builtin_fn(g, BuiltinFnIdFence, "fence", 1);
create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2);
create_builtin_fn(g, BuiltinFnIdIntCast, "intCast", 2);
create_builtin_fn(g, BuiltinFnIdFloatCast, "floatCast", 2);
create_builtin_fn(g, BuiltinFnIdIntToFloat, "intToFloat", 2);
create_builtin_fn(g, BuiltinFnIdFloatToInt, "floatToInt", 2);
create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1);
create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
Expand Down
Loading