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

0.1.1 bug: printf doesnt handle u2 .. u7 #546

PavelVozenilek opened this Issue Oct 19, 2017 · 1 comment


None yet
4 participants

PavelVozenilek commented Oct 19, 2017

This tiny program fails:

const io = @import("std").io;

pub fn main() -> %void {
  var x : u3 = 1;
  %%io.stdout.printf("{}\n", x);

with quite confusing error:

O:\zig0.1.1>zig build-exe b.zig
O:\zig0.1.1\lib\zig\std\fmt\index.zig:322:11: error: expected type 'u3', found 'u8'
        a /= base;
O:\zig0.1.1\lib\zig\std\fmt\index.zig:282:33: note: called from here
        return formatIntUnsigned(value, base, uppercase, width, context, output);
O:\zig0.1.1\lib\zig\std\fmt\index.zig:180:29: note: called from here
            return formatInt(value, 10, false, 0, context, output);
O:\zig0.1.1\lib\zig\std\fmt\index.zig:61:37: note: called from here
                    if (!formatValue(args[next_arg], context, output))
O:\zig0.1.1\lib\zig\std\io.zig:154:23: note: called from here
        _ = fmt.format(&context, printOutput, format, args);
O:\zig0.1.1\lib\zig\std\io.zig:144:27: note: called from here
        %return self.print(format, args);
O:\zig0.1.1\b.zig:5:21: note: called from here
  %%io.stdout.printf("{}\n", x);

In addition, when I change x variable type to i2 ... i7, compiler errors still keeps talking about unsigneds.

Types like u8, u16, i8 etc work as expected.

@tiehuis tiehuis added the bug label Oct 19, 2017


This comment has been minimized.


dimenus commented Oct 19, 2017

Why is the error confusing, it even gives you a stack trace? You can look at the source yourself since this is user level code....

fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
    context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool

So base is defined as a u8. As the error message states, you can't divide a u3 by a u8. How would you fix this?

//handle u2 - u7
 var a = if(@sizeOf(@typeOf(value)) == 1) u8(value) else value;

@andrewrk andrewrk modified the milestone: 0.2.0 Oct 19, 2017

@andrewrk andrewrk closed this in 9b91c76 Oct 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment