Skip to content

Take advantage of mem.spanZ accepting null #4880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2020
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
2 changes: 1 addition & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ pub const DebugInfo = struct {
if (context.address >= seg_start and context.address < seg_end) {
// Android libc uses NULL instead of an empty string to mark the
// main program
context.name = if (info.dlpi_name) |dlpi_name| mem.spanZ(dlpi_name) else "";
context.name = mem.spanZ(info.dlpi_name) orelse "";
context.base_address = info.dlpi_addr;
// Stop the iteration
return error.Found;
Expand Down
22 changes: 16 additions & 6 deletions lib/std/mem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,14 @@ test "Span" {
/// When there is both a sentinel and an array length or slice length, the
/// length value is used instead of the sentinel.
pub fn span(ptr: var) Span(@TypeOf(ptr)) {
const Result = Span(@TypeOf(ptr));
if (@typeInfo(@TypeOf(ptr)) == .Optional and ptr == null) {
return null;
if (@typeInfo(@TypeOf(ptr)) == .Optional) {
if (ptr) |non_null| {
return span(non_null);
} else {
return null;
}
}
const Result = Span(@TypeOf(ptr));
const l = len(ptr);
if (@typeInfo(Result).Pointer.sentinel) |s| {
return ptr[0..l :s];
Expand All @@ -576,16 +580,21 @@ test "span" {
const ptr = @as([*:3]u16, array[0..2 :3]);
testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 }));
testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
testing.expectEqual(@as(?[:0]u16, null), span(@as(?[*:0]u16, null)));
}

/// Same as `span`, except when there is both a sentinel and an array
/// length or slice length, scans the memory for the sentinel value
/// rather than using the length.
pub fn spanZ(ptr: var) Span(@TypeOf(ptr)) {
const Result = Span(@TypeOf(ptr));
if (@typeInfo(@TypeOf(ptr)) == .Optional and ptr == null) {
return null;
if (@typeInfo(@TypeOf(ptr)) == .Optional) {
if (ptr) |non_null| {
return spanZ(non_null);
} else {
return null;
}
}
const Result = Span(@TypeOf(ptr));
const l = lenZ(ptr);
if (@typeInfo(Result).Pointer.sentinel) |s| {
return ptr[0..l :s];
Expand All @@ -599,6 +608,7 @@ test "spanZ" {
const ptr = @as([*:3]u16, array[0..2 :3]);
testing.expect(eql(u16, spanZ(ptr), &[_]u16{ 1, 2 }));
testing.expect(eql(u16, spanZ(&array), &[_]u16{ 1, 2, 3, 4, 5 }));
testing.expectEqual(@as(?[:0]u16, null), spanZ(@as(?[*:0]u16, null)));
}

/// Takes a pointer to an array, an array, a sentinel-terminated pointer,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ pub fn execvpe_expandArg0(
mem.set(?[*:0]u8, argv_buf, null);
defer {
for (argv_buf) |arg| {
const arg_buf = if (arg) |ptr| mem.spanZ(ptr) else break;
const arg_buf = mem.spanZ(arg) orelse break;
allocator.free(arg_buf);
}
allocator.free(argv_buf);
Expand Down
7 changes: 3 additions & 4 deletions src-self-hosted/stage2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -689,12 +689,11 @@ fn stage2CrossTarget(
mcpu_oz: ?[*:0]const u8,
dynamic_linker_oz: ?[*:0]const u8,
) !CrossTarget {
const zig_triple = if (zig_triple_oz) |zig_triple_z| mem.spanZ(zig_triple_z) else "native";
const mcpu = if (mcpu_oz) |mcpu_z| mem.spanZ(mcpu_z) else null;
const dynamic_linker = if (dynamic_linker_oz) |dl_z| mem.spanZ(dl_z) else null;
const mcpu = mem.spanZ(mcpu_oz);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun bug: if I inlined this into the following expression I got:

broken LLVM module found: Call parameter type does not match function signature!
  %67 = getelementptr inbounds %std.zig.cross_target.ParseOptions, %std.zig.cross_target.ParseOptions* %10, i32 0, i32 1, !dbg !20878
 %"?[:0]const u8"*  call fastcc void @std.mem.spanZ.176(%"?[]const u8"* sret %67, i8* %68), !dbg !20880

This is a bug in the Zig compiler.

const dynamic_linker = mem.spanZ(dynamic_linker_oz);
var diags: CrossTarget.ParseOptions.Diagnostics = .{};
const target: CrossTarget = CrossTarget.parse(.{
.arch_os_abi = zig_triple,
.arch_os_abi = mem.spanZ(zig_triple_oz) orelse "native",
.cpu_features = mcpu,
.dynamic_linker = dynamic_linker,
.diagnostics = &diags,
Expand Down