#(Compile) errors on windows, not on mac

1 messages · Page 1 of 1 (latest)

mild remnant
#
const std = @import("std");
const stdin = std.io.getStdIn().reader();
//const stdout = std.io.getStdOut().writer();
pub fn main() !void {
    var gp = std.heap.GeneralPurposeAllocator(.{}){};
    //try stdout.writeAll("Enter max 6 characters: ");
    //try stdout.print("Enter max 6 characters: ", .{});
    std.debug.print("Enter max 6 characters: ", .{});
    const input: []const u8 = blk: {
        var buf_array = try std.ArrayList(u8).initCapacity(gp.allocator(), 16);
        errdefer buf_array.deinit();

        while (true) {
            try stdin.readUntilDelimiterArrayList(&buf_array, '\n', 1 << 16);
            if (buf_array.items.len <= 6) break;
            //try stdout.print("Too many characters!\nTry again: ", .{});
            std.debug.print("Too many characters!\nTry again: ", .{});
            continue;
        }
        break :blk buf_array.toOwnedSlice();
    };
    defer gp.allocator().free(input);
    //try stdout.print("You entered: {s}\n", .{input});
    std.debug.print("You entered: {s}\n", .{input});
}
#

Gives me following errors:

C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1760:20: error: unable to evaluate comptime expression
        .x86_64 => asm volatile (
                   ^~~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1773:15: note: called from here
    return teb().ProcessEnvironmentBlock;
           ~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:95:30: note: called from here
        return os.windows.peb().ProcessParameters.hStdInput;
               ~~~~~~~~~~~~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:109:33: note: called from here
        .handle = getStdInHandle(),
                  ~~~~~~~~~~~~~~^~
usrIn.zig:11:30: note: called from here
const stdin = std.io.getStdIn().reader();
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1760:20: error: unable to evaluate comptime expression
        .x86_64 => asm volatile (
                   ^~~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1773:15: note: called from here
    return teb().ProcessEnvironmentBlock;
           ~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:43:30: note: called from here
        return os.windows.peb().ProcessParameters.hStdOutput;
               ~~~~~~~~~~~~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:57:34: note: called from here
        .handle = getStdOutHandle(),
                  ~~~~~~~~~~~~~~~^~
usrIn.zig:12:32: note: called from here
const stdout = std.io.getStdOut().writer();
#

Is there some permission I need on windows for reading and writing to terminal?

sweet echo
#

getStdIn does actual work (on some platforms), so you can't store its result in a global (globals have to be comptime-known, it's like C rather than C++). So you just have to put those definitions inside your main function

mild remnant
#

That's good to know!

#

Next error I get, which I didn't get on mac is:

#
error: expected type '[]const u8', found 'error{OutOfMemory}![]u8'
            if (buf_array.items.len <= 6) break;

Doesn't change when I allocate more or less.

supple parrot
#
  1. It should be defer buf_array.deinit() as the docs for toOwnedSlice() mention its capacity may or may not be cleared and deinit() is still required to clean up its memory.

  2. The compile error is from toOwnedSlice() returning Allocator.Error![]const u8 while the input block expects only []const u8. You should unwrap the error with break :blk try buf_array.toOwnedSlice()

mild remnant
#

Right on with break :blk try!

mild remnant