Skip to content

Commit d5648d2

Browse files
committed
remove implicit cast from T to *const T
closes #1465
1 parent 378d3e4 commit d5648d2

29 files changed

+112
-270
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn build(b: *Builder) !void {
121121
test_step.dependOn(docs_step);
122122
}
123123

124-
fn dependOnLib(lib_exe_obj: var, dep: *const LibraryDep) void {
124+
fn dependOnLib(lib_exe_obj: var, dep: LibraryDep) void {
125125
for (dep.libdirs.toSliceConst()) |lib_dir| {
126126
lib_exe_obj.addLibPath(lib_dir);
127127
}

doc/docgen.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const Tokenizer = struct.{
191191
line_end: usize,
192192
};
193193

194-
fn getTokenLocation(self: *Tokenizer, token: *const Token) Location {
194+
fn getTokenLocation(self: *Tokenizer, token: Token) Location {
195195
var loc = Location.{
196196
.line = 0,
197197
.column = 0,
@@ -216,7 +216,7 @@ const Tokenizer = struct.{
216216
}
217217
};
218218

219-
fn parseError(tokenizer: *Tokenizer, token: *const Token, comptime fmt: []const u8, args: ...) error {
219+
fn parseError(tokenizer: *Tokenizer, token: Token, comptime fmt: []const u8, args: ...) error {
220220
const loc = tokenizer.getTokenLocation(token);
221221
warn("{}:{}:{}: error: " ++ fmt ++ "\n", tokenizer.source_file_name, loc.line + 1, loc.column + 1, args);
222222
if (loc.line_start <= loc.line_end) {
@@ -239,7 +239,7 @@ fn parseError(tokenizer: *Tokenizer, token: *const Token, comptime fmt: []const
239239
return error.ParseError;
240240
}
241241

242-
fn assertToken(tokenizer: *Tokenizer, token: *const Token, id: Token.Id) !void {
242+
fn assertToken(tokenizer: *Tokenizer, token: Token, id: Token.Id) !void {
243243
if (token.id != id) {
244244
return parseError(tokenizer, token, "expected {}, found {}", @tagName(id), @tagName(token.id));
245245
}

doc/langref.html.in

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ const Vec3 = struct.{
19051905
};
19061906
}
19071907

1908-
pub fn dot(self: *const Vec3, other: *const Vec3) f32 {
1908+
pub fn dot(self: Vec3, other: Vec3) f32 {
19091909
return self.x * other.x + self.y * other.y + self.z * other.z;
19101910
}
19111911
};
@@ -2243,8 +2243,8 @@ const Variant = union(enum).{
22432243
Int: i32,
22442244
Bool: bool,
22452245

2246-
fn truthy(self: *const Variant) bool {
2247-
return switch (self.*) {
2246+
fn truthy(self: Variant) bool {
2247+
return switch (self) {
22482248
Variant.Int => |x_int| x_int != 0,
22492249
Variant.Bool => |x_bool| x_bool,
22502250
};
@@ -4040,9 +4040,6 @@ test "float widening" {
40404040
{#header_open|Implicit Cast: undefined#}
40414041
<p>TODO</p>
40424042
{#header_close#}
4043-
{#header_open|Implicit Cast: T to *const T#}
4044-
<p>TODO</p>
4045-
{#header_close#}
40464043
{#header_close#}
40474044

40484045
{#header_open|Explicit Casts#}

src-self-hosted/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
658658
const main_handle = try async<allocator> asyncFmtMainChecked(
659659
&result,
660660
&loop,
661-
flags,
661+
&flags,
662662
color,
663663
);
664664
defer cancel main_handle;

src/ir.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9758,46 +9758,6 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
97589758
return result;
97599759
}
97609760

9761-
static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_instr,
9762-
IrInstruction *value, ZigType *wanted_type)
9763-
{
9764-
if (instr_is_comptime(value)) {
9765-
ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
9766-
if (!val)
9767-
return ira->codegen->invalid_instruction;
9768-
9769-
IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
9770-
source_instr->scope, source_instr->source_node);
9771-
const_instruction->base.value.type = wanted_type;
9772-
const_instruction->base.value.special = ConstValSpecialStatic;
9773-
const_instruction->base.value.data.x_ptr.special = ConstPtrSpecialRef;
9774-
const_instruction->base.value.data.x_ptr.data.ref.pointee = val;
9775-
return &const_instruction->base;
9776-
}
9777-
9778-
if (value->id == IrInstructionIdLoadPtr) {
9779-
IrInstructionLoadPtr *load_ptr_inst = (IrInstructionLoadPtr *)value;
9780-
ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type,
9781-
load_ptr_inst->ptr->value.type, source_instr->source_node, false);
9782-
if (const_cast_result.id == ConstCastResultIdInvalid)
9783-
return ira->codegen->invalid_instruction;
9784-
if (const_cast_result.id == ConstCastResultIdOk)
9785-
return load_ptr_inst->ptr;
9786-
}
9787-
IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instr->scope,
9788-
source_instr->source_node, value, true, false);
9789-
new_instruction->value.type = wanted_type;
9790-
9791-
ZigType *child_type = wanted_type->data.pointer.child_type;
9792-
if (type_has_bits(child_type)) {
9793-
ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
9794-
assert(fn_entry);
9795-
fn_entry->alloca_list.append(new_instruction);
9796-
}
9797-
ir_add_alloca(ira, new_instruction, child_type);
9798-
return new_instruction;
9799-
}
9800-
98019761
static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, ZigType *wanted_type) {
98029762
assert(wanted_type->id == ZigTypeIdOptional);
98039763
assert(instr_is_comptime(value));
@@ -10929,14 +10889,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1092910889
return ir_analyze_undefined_to_anything(ira, source_instr, value, wanted_type);
1093010890
}
1093110891

10932-
// cast from something to const pointer of it
10933-
if (!type_requires_comptime(actual_type)) {
10934-
ZigType *const_ptr_actual = get_pointer_to_type(ira->codegen, actual_type, true);
10935-
if (types_match_const_cast_only(ira, wanted_type, const_ptr_actual, source_node, false).id == ConstCastResultIdOk) {
10936-
return ir_analyze_cast_ref(ira, source_instr, value, wanted_type);
10937-
}
10938-
}
10939-
1094010892
ErrorMsg *parent_msg = ir_add_error_node(ira, source_instr->source_node,
1094110893
buf_sprintf("expected type '%s', found '%s'",
1094210894
buf_ptr(&wanted_type->name),

std/buffer.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub const Buffer = struct.{
3232
}
3333

3434
/// Must deinitialize with deinit.
35-
pub fn initFromBuffer(buffer: *const Buffer) !Buffer {
35+
pub fn initFromBuffer(buffer: Buffer) !Buffer {
3636
return Buffer.init(buffer.list.allocator, buffer.toSliceConst());
3737
}
3838

@@ -148,7 +148,7 @@ test "simple Buffer" {
148148
assert(buf.eql("hello world"));
149149
assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));
150150

151-
var buf2 = try Buffer.initFromBuffer(&buf);
151+
var buf2 = try Buffer.initFromBuffer(buf);
152152
assert(buf.eql(buf2.toSliceConst()));
153153

154154
assert(buf.startsWith("hell"));

std/build.zig

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub const Builder = struct.{
3737
invalid_user_input: bool,
3838
zig_exe: []const u8,
3939
default_step: *Step,
40-
env_map: BufMap,
40+
env_map: *const BufMap,
4141
top_level_steps: ArrayList(*TopLevelStep),
4242
prefix: []const u8,
4343
search_prefixes: ArrayList([]const u8),
@@ -89,6 +89,8 @@ pub const Builder = struct.{
8989
};
9090

9191
pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
92+
const env_map = allocator.createOne(BufMap) catch unreachable;
93+
env_map.* = os.getEnvMap(allocator) catch unreachable;
9294
var self = Builder.{
9395
.zig_exe = zig_exe,
9496
.build_root = build_root,
@@ -110,7 +112,7 @@ pub const Builder = struct.{
110112
.available_options_list = ArrayList(AvailableOption).init(allocator),
111113
.top_level_steps = ArrayList(*TopLevelStep).init(allocator),
112114
.default_step = undefined,
113-
.env_map = os.getEnvMap(allocator) catch unreachable,
115+
.env_map = env_map,
114116
.prefix = undefined,
115117
.search_prefixes = ArrayList([]const u8).init(allocator),
116118
.lib_dir = undefined,
@@ -155,7 +157,7 @@ pub const Builder = struct.{
155157
return LibExeObjStep.createObject(self, name, root_src);
156158
}
157159

158-
pub fn addSharedLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
160+
pub fn addSharedLibrary(self: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep {
159161
return LibExeObjStep.createSharedLibrary(self, name, root_src, ver);
160162
}
161163

@@ -178,7 +180,7 @@ pub const Builder = struct.{
178180
return LibExeObjStep.createCStaticLibrary(self, name);
179181
}
180182

181-
pub fn addCSharedLibrary(self: *Builder, name: []const u8, ver: *const Version) *LibExeObjStep {
183+
pub fn addCSharedLibrary(self: *Builder, name: []const u8, ver: Version) *LibExeObjStep {
182184
return LibExeObjStep.createCSharedLibrary(self, name, ver);
183185
}
184186

@@ -541,7 +543,7 @@ pub const Builder = struct.{
541543
}
542544

543545
fn spawnChild(self: *Builder, argv: []const []const u8) !void {
544-
return self.spawnChildEnvMap(null, &self.env_map, argv);
546+
return self.spawnChildEnvMap(null, self.env_map, argv);
545547
}
546548

547549
fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
@@ -850,12 +852,12 @@ pub const LibExeObjStep = struct.{
850852
Obj,
851853
};
852854

853-
pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: *const Version) *LibExeObjStep {
855+
pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep {
854856
const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, false, ver)) catch unreachable;
855857
return self;
856858
}
857859

858-
pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: *const Version) *LibExeObjStep {
860+
pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: Version) *LibExeObjStep {
859861
const self = builder.allocator.create(initC(builder, name, Kind.Lib, version, false)) catch unreachable;
860862
return self;
861863
}
@@ -891,7 +893,7 @@ pub const LibExeObjStep = struct.{
891893
return self;
892894
}
893895

894-
fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: *const Version) LibExeObjStep {
896+
fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: Version) LibExeObjStep {
895897
var self = LibExeObjStep.{
896898
.no_rosegment = false,
897899
.strip = false,
@@ -909,7 +911,7 @@ pub const LibExeObjStep = struct.{
909911
.step = Step.init(name, builder.allocator, make),
910912
.output_path = null,
911913
.output_h_path = null,
912-
.version = ver.*,
914+
.version = ver,
913915
.out_filename = undefined,
914916
.out_h_filename = builder.fmt("{}.h", name),
915917
.major_only_filename = undefined,
@@ -933,13 +935,13 @@ pub const LibExeObjStep = struct.{
933935
return self;
934936
}
935937

936-
fn initC(builder: *Builder, name: []const u8, kind: Kind, version: *const Version, static: bool) LibExeObjStep {
938+
fn initC(builder: *Builder, name: []const u8, kind: Kind, version: Version, static: bool) LibExeObjStep {
937939
var self = LibExeObjStep.{
938940
.no_rosegment = false,
939941
.builder = builder,
940942
.name = name,
941943
.kind = kind,
942-
.version = version.*,
944+
.version = version,
943945
.static = static,
944946
.target = Target.Native,
945947
.cflags = ArrayList([]const u8).init(builder.allocator),

std/debug/index.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
976976
};
977977
}
978978

979-
fn printLineFromFile(out_stream: var, line_info: *const LineInfo) !void {
979+
fn printLineFromFile(out_stream: var, line_info: LineInfo) !void {
980980
var f = try os.File.openRead(line_info.file_name);
981981
defer f.close();
982982
// TODO fstat and make sure that the file has the correct size

std/event/net.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const posix = os.posix;
88
const Loop = std.event.Loop;
99

1010
pub const Server = struct.{
11-
handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const os.File) void,
11+
handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, os.File) void,
1212

1313
loop: *Loop,
1414
sockfd: ?i32,
@@ -40,7 +40,7 @@ pub const Server = struct.{
4040
pub fn listen(
4141
self: *Server,
4242
address: *const std.net.Address,
43-
handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, *const os.File) void,
43+
handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, os.File) void,
4444
) !void {
4545
self.handleRequestFn = handleRequestFn;
4646

@@ -82,7 +82,7 @@ pub const Server = struct.{
8282
continue;
8383
}
8484
var socket = os.File.openHandle(accepted_fd);
85-
_ = async<self.loop.allocator> self.handleRequestFn(self, accepted_addr, socket) catch |err| switch (err) {
85+
_ = async<self.loop.allocator> self.handleRequestFn(self, &accepted_addr, socket) catch |err| switch (err) {
8686
error.OutOfMemory => {
8787
socket.close();
8888
continue;
@@ -278,9 +278,9 @@ test "listen on a port, send bytes, receive bytes" {
278278
tcp_server: Server,
279279

280280
const Self = @This();
281-
async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: *const os.File) void {
281+
async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: os.File) void {
282282
const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
283-
var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/1592
283+
var socket = _socket; // TODO https://github.com/ziglang/zig/issues/1592
284284
defer socket.close();
285285
// TODO guarantee elision of this allocation
286286
const next_handler = async errorableHandler(self, _addr, socket) catch unreachable;
@@ -307,9 +307,9 @@ test "listen on a port, send bytes, receive bytes" {
307307
try loop.initSingleThreaded(std.debug.global_allocator);
308308
var server = MyServer.{ .tcp_server = Server.init(&loop) };
309309
defer server.tcp_server.deinit();
310-
try server.tcp_server.listen(addr, MyServer.handler);
310+
try server.tcp_server.listen(&addr, MyServer.handler);
311311

312-
const p = try async<std.debug.global_allocator> doAsyncTest(&loop, server.tcp_server.listen_address, &server.tcp_server);
312+
const p = try async<std.debug.global_allocator> doAsyncTest(&loop, &server.tcp_server.listen_address, &server.tcp_server);
313313
defer cancel p;
314314
loop.run();
315315
}

std/fmt/errol/index.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ fn tableLowerBound(k: u64) usize {
217217
/// @in: The HP number.
218218
/// @val: The double.
219219
/// &returns: The HP number.
220-
fn hpProd(in: *const HP, val: f64) HP {
220+
fn hpProd(in: HP, val: f64) HP {
221221
var hi: f64 = undefined;
222222
var lo: f64 = undefined;
223223
split(in.val, &hi, &lo);

0 commit comments

Comments
 (0)