Skip to content

Commit cbfe4b4

Browse files
committed
add implicit cast from [0]T to %[]T
closes #347 also add std.os.path.relative
1 parent d04d3ec commit cbfe4b4

File tree

4 files changed

+159
-6
lines changed

4 files changed

+159
-6
lines changed

src/ir.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5963,7 +5963,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
59635963
return ImplicitCastMatchResultYes;
59645964
}
59655965

5966-
// implicit conversion from error child type to error type
5966+
// implicit T to %T
59675967
if (expected_type->id == TypeTableEntryIdErrorUnion &&
59685968
ir_types_match_with_implicit_cast(ira, expected_type->data.error.child_type, actual_type, value))
59695969
{
@@ -6012,7 +6012,7 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
60126012
return ImplicitCastMatchResultYes;
60136013
}
60146014

6015-
// implicit array to slice conversion
6015+
// implicit [N]T to []const T
60166016
if (expected_type->id == TypeTableEntryIdStruct &&
60176017
expected_type->data.structure.is_slice &&
60186018
actual_type->id == TypeTableEntryIdArray)
@@ -6027,6 +6027,21 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
60276027
}
60286028
}
60296029

6030+
//// implicit [N]T to %[]const T
6031+
//if (expected_type->id == TypeTableEntryIdErrorUnion &&
6032+
// is_slice(expected_type->data.error.child_type) &&
6033+
// actual_type->id == TypeTableEntryIdArray)
6034+
//{
6035+
// TypeTableEntry *ptr_type =
6036+
// expected_type->data.error.child_type->data.structure.fields[slice_ptr_index].type_entry;
6037+
// assert(ptr_type->id == TypeTableEntryIdPointer);
6038+
// if ((ptr_type->data.pointer.is_const || actual_type->data.array.len == 0) &&
6039+
// types_match_const_cast_only(ptr_type->data.pointer.child_type, actual_type->data.array.child_type))
6040+
// {
6041+
// return ImplicitCastMatchResultYes;
6042+
// }
6043+
//}
6044+
60306045
// implicit [N]T to &const []const N
60316046
if (expected_type->id == TypeTableEntryIdPointer &&
60326047
expected_type->data.pointer.is_const &&
@@ -6799,6 +6814,8 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
67996814
IrInstruction *array, TypeTableEntry *wanted_type)
68006815
{
68016816
assert(is_slice(wanted_type));
6817+
// In this function we honor the const-ness of wanted_type, because
6818+
// we may be casting [0]T to []const T which is perfectly valid.
68026819

68036820
TypeTableEntry *array_type = array->value.type;
68046821
assert(array_type->id == TypeTableEntryIdArray);
@@ -6807,6 +6824,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
68076824
IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
68086825
source_instr->source_node, wanted_type);
68096826
init_const_slice(ira->codegen, &result->value, &array->value, 0, array_type->data.array.len, true);
6827+
result->value.type = wanted_type;
68106828
return result;
68116829
}
68126830

@@ -6822,8 +6840,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
68226840

68236841
IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,
68246842
source_instr->source_node, array_ptr, start, end, false);
6825-
TypeTableEntry *child_type = array_type->data.array.child_type;
6826-
result->value.type = get_slice_type(ira->codegen, child_type, true);
6843+
result->value.type = wanted_type;
68276844
ir_add_alloca(ira, result, result->value.type);
68286845

68296846
return result;
@@ -7200,6 +7217,29 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
72007217
}
72017218
}
72027219

7220+
// explicit cast from [N]T to %[]const T
7221+
if (wanted_type->id == TypeTableEntryIdErrorUnion &&
7222+
is_slice(wanted_type->data.error.child_type) &&
7223+
actual_type->id == TypeTableEntryIdArray)
7224+
{
7225+
TypeTableEntry *ptr_type =
7226+
wanted_type->data.error.child_type->data.structure.fields[slice_ptr_index].type_entry;
7227+
assert(ptr_type->id == TypeTableEntryIdPointer);
7228+
if ((ptr_type->data.pointer.is_const || actual_type->data.array.len == 0) &&
7229+
types_match_const_cast_only(ptr_type->data.pointer.child_type, actual_type->data.array.child_type))
7230+
{
7231+
IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_type->data.error.child_type, value);
7232+
if (type_is_invalid(cast1->value.type))
7233+
return ira->codegen->invalid_instruction;
7234+
7235+
IrInstruction *cast2 = ir_analyze_cast(ira, source_instr, wanted_type, cast1);
7236+
if (type_is_invalid(cast2->value.type))
7237+
return ira->codegen->invalid_instruction;
7238+
7239+
return cast2;
7240+
}
7241+
}
7242+
72037243
// explicit cast from pure error to error union type
72047244
if (wanted_type->id == TypeTableEntryIdErrorUnion &&
72057245
actual_type->id == TypeTableEntryIdPureError)

std/mem.zig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ error NoMem;
1010
pub const Allocator = struct {
1111
allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
1212
/// Note that old_mem may be a slice of length 0, in which case reallocFn
13-
/// should simply call allocFn
13+
/// should simply call allocFn.
1414
reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
15+
/// Note that mem may be a slice of length 0, in which case freeFn
16+
/// should do nothing.
1517
freeFn: fn (self: &Allocator, mem: []u8),
1618

1719
/// Aborts the program if an allocation fails.
@@ -228,6 +230,10 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) -> bool {
228230
return eql(u8, a, b);
229231
}
230232

233+
/// Returns an iterator that iterates over the slices of ::s that are not
234+
/// the byte ::c.
235+
/// split(" abc def ghi ")
236+
/// Will return slices for "abc", "def", "ghi", null, in that order.
231237
pub fn split(s: []const u8, c: u8) -> SplitIterator {
232238
SplitIterator {
233239
.index = 0,
@@ -236,6 +242,14 @@ pub fn split(s: []const u8, c: u8) -> SplitIterator {
236242
}
237243
}
238244

245+
test "mem.split" {
246+
var it = split(" abc def ghi ", ' ');
247+
assert(eql(u8, ??it.next(), "abc"));
248+
assert(eql(u8, ??it.next(), "def"));
249+
assert(eql(u8, ??it.next(), "ghi"));
250+
assert(it.next() == null);
251+
}
252+
239253
pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) -> bool {
240254
return if (needle.len > haystack.len) false else eql(T, haystack[0...needle.len], needle);
241255
}
@@ -259,6 +273,14 @@ const SplitIterator = struct {
259273

260274
return self.s[start...end];
261275
}
276+
277+
/// Returns a slice of the remaining bytes. Does not affect iterator state.
278+
pub fn rest(self: &const SplitIterator) -> []const u8 {
279+
// move to beginning of token
280+
var index: usize = self.index;
281+
while (index < self.s.len and self.s[index] == self.c; index += 1) {}
282+
return self.s[index...];
283+
}
262284
};
263285

264286
test "testStringEquality" {

std/os/path.zig

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ const assert = debug.assert;
33
const mem = @import("../mem.zig");
44
const Allocator = mem.Allocator;
55
const os = @import("index.zig");
6+
const math = @import("../math.zig");
67

7-
pub const sep = '/';
8+
pub const sep = switch (@compileVar("os")) {
9+
Os.windows => '\\',
10+
else => '/',
11+
};
12+
pub const delimiter = switch (@compileVar("os")) {
13+
Os.windows => ';',
14+
else => ':',
15+
};
816

917
/// Naively combines a series of paths with the native path seperator.
1018
/// Allocates memory for the result, which must be freed by the caller.
@@ -134,6 +142,7 @@ test "os.path.resolve" {
134142
assert(mem.eql(u8, testResolve("/a/b", "c", "//d", "e///"), "/d/e"));
135143
assert(mem.eql(u8, testResolve("/a/b/c", "..", "../"), "/a"));
136144
assert(mem.eql(u8, testResolve("/", "..", ".."), "/"));
145+
assert(mem.eql(u8, testResolve("/a/b/c/"), "/a/b/c"));
137146
}
138147
fn testResolve(args: ...) -> []u8 {
139148
return %%resolve(&debug.global_allocator, args);
@@ -175,3 +184,71 @@ test "os.path.dirname" {
175184
fn testDirname(input: []const u8, expected_output: []const u8) {
176185
assert(mem.eql(u8, dirname(input), expected_output));
177186
}
187+
188+
/// Returns the relative path from ::from to ::to. If ::from and ::to each
189+
/// resolve to the same path (after calling ::resolve on each), a zero-length
190+
/// string is returned.
191+
pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 {
192+
const resolved_from = %return resolve(allocator, from);
193+
defer allocator.free(resolved_from);
194+
195+
const resolved_to = %return resolve(allocator, to);
196+
defer allocator.free(resolved_to);
197+
198+
var from_it = mem.split(resolved_from, '/');
199+
var to_it = mem.split(resolved_to, '/');
200+
while (true) {
201+
const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
202+
const to_rest = to_it.rest();
203+
test(to_it.next()) |to_component| {
204+
if (mem.eql(u8, from_component, to_component))
205+
continue;
206+
}
207+
var up_count: usize = 1;
208+
while (true) {
209+
_ = from_it.next() ?? break;
210+
up_count += 1;
211+
}
212+
const up_index_end = up_count * "../".len;
213+
const result = %return allocator.alloc(u8, up_index_end + to_rest.len);
214+
%defer allocator.free(result);
215+
216+
var result_index: usize = 0;
217+
while (result_index < up_index_end) {
218+
result[result_index] = '.';
219+
result_index += 1;
220+
result[result_index] = '.';
221+
result_index += 1;
222+
result[result_index] = '/';
223+
result_index += 1;
224+
}
225+
if (to_rest.len == 0) {
226+
// shave off the trailing slash
227+
return result[0...result_index - 1];
228+
}
229+
230+
mem.copy(u8, result[result_index...], to_rest);
231+
return result;
232+
}
233+
234+
return []u8{};
235+
}
236+
237+
test "os.path.relative" {
238+
testRelative("/var/lib", "/var", "..");
239+
testRelative("/var/lib", "/bin", "../../bin");
240+
testRelative("/var/lib", "/var/lib", "");
241+
testRelative("/var/lib", "/var/apache", "../apache");
242+
testRelative("/var/", "/var/lib", "lib");
243+
testRelative("/", "/var/lib", "var/lib");
244+
testRelative("/foo/test", "/foo/test/bar/package.json", "bar/package.json");
245+
testRelative("/Users/a/web/b/test/mails", "/Users/a/web/b", "../..");
246+
testRelative("/foo/bar/baz-quux", "/foo/bar/baz", "../baz");
247+
testRelative("/foo/bar/baz", "/foo/bar/baz-quux", "../baz-quux");
248+
testRelative("/baz-quux", "/baz", "../baz");
249+
testRelative("/baz", "/baz-quux", "../baz-quux");
250+
}
251+
fn testRelative(from: []const u8, to: []const u8, expected_output: []const u8) {
252+
const result = %%relative(&debug.global_allocator, from, to);
253+
assert(mem.eql(u8, result, expected_output));
254+
}

test/cases/cast.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,17 @@ test "implicitly cast from [N]T to ?[]const T" {
159159
fn castToMaybeSlice() -> ?[]const u8 {
160160
return "hi";
161161
}
162+
163+
164+
test "implicitly cast from [0]T to %[]T" {
165+
testCastZeroArrayToErrSliceMut();
166+
comptime testCastZeroArrayToErrSliceMut();
167+
}
168+
169+
fn testCastZeroArrayToErrSliceMut() {
170+
assert((%%gimmeErrOrSlice()).len == 0);
171+
}
172+
173+
fn gimmeErrOrSlice() -> %[]u8 {
174+
return []u8{};
175+
}

0 commit comments

Comments
 (0)