ChanServ changed the topic of #zig to: zig programming language | https://ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
<fengb> How about we target Lua and pipe the results into fengari
<daurnimator> go for it :P
<oats> zig. on. javascript
<fengb> Actually... that’d be a pretty cool idea
<fengb> Be like Typescript but without needing backward support
<waleee-cl> seems kind of done a bunch of times now
<fengb> But Zig is better
nephele_ has joined #zig
curtisf has joined #zig
nephele has quit [Ping timeout: 252 seconds]
nephele_ is now known as nephele
fraktor has joined #zig
<fraktor> I'm trying to link to glibc, but when I add `b.linkLibC()` to my build.zig files, my build fails. Running the `build-exe` command directly yields "illegal hardware instruction." I'm using zig-bootstrap from the master branch; what should I do to fix this?
marijnfs_ has joined #zig
marijnfs has quit [Ping timeout: 246 seconds]
marler8997 has quit [Ping timeout: 272 seconds]
daurnimator has quit [Ping timeout: 265 seconds]
<fraktor> What if I'm not invoking any C code, but just trying to include the C library?
<fraktor> (I will be invoking C code, but I've got the default hello world program for now).
<andrewrk> are there any more error details you can share?
benjif has quit [Ping timeout: 260 seconds]
daurnimator has joined #zig
<fraktor> https://imgur.com/Pv5BZAE Is this helpful? Here's my build.zig file: https://0paste.com/66983
<andrewrk> yeah that's helpful. it looks like the zig compiler itself crashed
<andrewrk> the next step to troubleshooting this would be to get a debug build of the compiler and get a stack trace
<fraktor> Yeah, it's when I try to build, not when I try to run.
dddddd has quit [Ping timeout: 256 seconds]
<fraktor> How would I get a debug build? Do I have to rebuild zig?
<andrewrk> illegal instruction hints that perhaps the zig you are using was cross compiled with different cpu feature flags than are available on your CPU
<andrewrk> yes you would have to rebuild zig with -DCMAKE_BUILD_TYPE=Debug rather than -DCMAKE_BUILD_TYPE=Release. that option is in the `build` script of the bootstrap repo
<andrewrk> did you bychance compile for the native target and then copy the resulting binary to a different computer?
<fraktor> I did not.
<fengb> What CPU target do we use for the nightly builds?
<andrewrk> baseline
<fraktor> Should I build for baseline instead of native?
<andrewrk> native is correct if you're intending to use it natively
<curtisf> Could someone explain this "error: cannot assign to constant" error to me? https://gist.github.com/CurtisFenner/577c28741a0d65b3ce501f495e8b6bed I'm trying to go from `@noInlineCall` to the new `@call` syntax
<fraktor> That is the plan. That only determines the architecture for the compiler itself, that is I can still cross-compile, right?
<andrewrk> curtisf, as a workaround, maybe try extracting that arg to a const. e.g. const workaround: std.builtin.CallOptions = .{.modifier = .never_inline };
<andrewrk> sorry, that looks like a really annoying bug
<curtisf> hmm extracting doesn't seem to work
<curtisf> what is the error trying to say exactly?
aerona has joined #zig
<andrewrk> curtisf, oh, hm, maybe it's simply pointing at the wrong ast node
<andrewrk> curtisf, double check that @This()._parseField is not a function pointer
<curtisf> it looks like it's just a regular method definition
<andrewrk> would be easier to debug with a debugger and a breakpoint set on add_node_error
<curtisf> Is that still not really possible on windows?
<andrewrk> unfortunately it still requires a debug llvm/clang/lld build
<curtisf> oh shoot, is #2727 still a problem? I was trying to fix some weird looking code but one of them has a todo mentioning it
<curtisf> I guess comptime parser combinators are a real stress test of the Zig compiler 😅
<andrewrk> that's probably true. the good news is that the design of self-hosted is coming along and it's rock solid so far
<andrewrk> we'll see if it survives implementing all the language features :)
blueberrypie7 has quit [Quit: leaving]
aerona has quit [Quit: Leaving]
<curtisf> OK, so how do I get the tag-enum-value associated with a tagged-union? Before I had `@TagType(@TypeOf(un))(un)`, but that doesn't work anymore
xackus has joined #zig
xackus_ has quit [Ping timeout: 272 seconds]
aerona has joined #zig
<curtisf> ah thanks, didn't realize that's what that was
blueberrypie has joined #zig
fraktor has quit [Ping timeout: 260 seconds]
fraktor has joined #zig
<fraktor> Doing another build because I can't to 8 jobs with only 16G of ram
<fraktor> s/can't to/can't do/
<curtisf> ah, pulling the argument out did actually work, I missed the `comptime`
curtisf has quit [Remote host closed the connection]
st4ll1 has joined #zig
drp has joined #zig
waleee-cl has quit [Quit: Connection closed for inactivity]
slurpie has joined #zig
joey152 has quit [Remote host closed the connection]
drp has quit [Read error: Connection reset by peer]
frmdstryr has quit [Ping timeout: 246 seconds]
metaleap has joined #zig
aerona has quit [Remote host closed the connection]
<scientes> is it possible to use non-natural alignment on a slice type?
<scientes> or do I need to use a pointer?
fraktor has quit [Ping timeout: 260 seconds]
<scientes> is there a convenience function for this?
<scientes> mem.copy(u8, strsCur, entries[i].key[0..entries[i].key.len + 1]);
<scientes> strsCur = strsCur[entries[i].key.len + 1..];
<scientes> uggh, also annoying you can't have a zero-length slice pointing to the end of another slice
daurnimator has quit [Ping timeout: 244 seconds]
cole-h has quit [Quit: Goodbye]
daurnimator has joined #zig
ifreund has joined #zig
pystub has joined #zig
frett27_ has joined #zig
frett27 has quit [Ping timeout: 260 seconds]
constptr has joined #zig
waleee-cl has joined #zig
Patrice_ has joined #zig
frett27_ has quit [Ping timeout: 246 seconds]
st4ll1 has quit [Quit: WeeChat 2.8]
marijnfs has joined #zig
<marijnfs> did the pub qualifier become more strict?
shakesoda has quit [Quit: Connection closed for inactivity]
_Vi has quit [Ping timeout: 244 seconds]
benjif has joined #zig
waleee-cl has quit [Quit: Connection closed for inactivity]
slurpie has quit [Ping timeout: 246 seconds]
marijnfs has quit [Ping timeout: 258 seconds]
metabulation has quit [Ping timeout: 258 seconds]
_Vi has joined #zig
aerona has joined #zig
st4ll1 has joined #zig
aerona has quit [Quit: Leaving]
decentpenguin has joined #zig
wootehfoot has joined #zig
cole-h has joined #zig
return0e has quit [Remote host closed the connection]
st4ll1 has quit [Quit: WeeChat 2.8]
dddddd has joined #zig
wootehfoot has quit [Ping timeout: 240 seconds]
Akuli has joined #zig
waleee-cl has joined #zig
donaldallen has joined #zig
<donaldallen> Are there any options for zig fmt, such as maximum line width? I have an enum with a long list of variants and zig fmt insists on putting them on one extremely long line.
<ifreund> donaldallen: no, but if you throw a trailing comma in there zig fmt will wrap
<donaldallen> Thanks -- I'll try it.
<ifreund> (this goes for everything, not just enums)
<donaldallen> Works great. Just what I wanted. Thanks again.
<ifreund> no problem!
<andrewrk> donaldallen, zig fmt also respects line breaks after binary operators
<ifreund> and if you want your ternary's to wrap (those aren't binary are they?
<ifreund> hmm, guess that would be ternaries
<andrewrk> what ternary?
<ifreund> i mean if/else without using a block
<ifreund> if (true) 1 else 0;
<ifreund> ternary definitely isn't the right word cause you can do just `if (true) return;` or `if (thing1) 1 else if (thing2) 2 else 3;`
<ifreund> but zig fmt will respect your line breaks
<donaldallen> ifelse without a block doesn't work as I would like, e.g., a function call in the else clause doesn't get indented properly (function name and args all at same indentation level).
<donaldallen> Wrap it in brackets and it works properly.
return0e has joined #zig
<donaldallen> But I just noticed an assignment statement in an if that was unbracketed and the rhs of the assignment is a function call and that got indented correctly.
<donaldallen> "was unbracketed" -> "is unbracketed"
st4ll1 has joined #zig
INSANU has joined #zig
<INSANU> can you guys tell me if there is any company using this language?
gdh has joined #zig
<gdh> andrewrk, or pixelherodev, there are no constructors in Zig, are there?
<ifreund> gdh: no
<gdh> Thanks.
gdh has left #zig ["Leaving"]
<demizer> Hi everyone, is it possible to use file.write into an ArrayList or a buffer of some kind so I can test the output? Haven't been able to find an example of this in the stdlib.
<tdeo> you can call .outStream() on an ArrayList(u8)
<tdeo> which gets you an OutStream, not a file, but it should help
<demizer> Thanks for the response. The function I want to test takes a std.File as a parameter. So I need something that satisfies that type. I might just have to write and read a file to test this function unfortunately unless there is some other way.
<tdeo> linux has memfd_create if you want a File without actually opening one, but it's probably better to just use a temp one
<demizer> thank you!
<oats> speaking of out streams, do you pretty much have to use 'var' for a function argument that takes one? Looks like you'd need a fairly complicated function signature otherwise...
<andrewrk> oats, yes
<oats> and 'var' means type inference based on how it's used in the function body, yeah?
<andrewrk> it's less than ideal, there are open issues for it, the most relevant one is probably https://github.com/ziglang/zig/issues/130
<oats> ah yes
<oats> traits would be <3
<oats> I get that it's a tricky thing to figure out though
<oats> semi-related, why do you still have to specify a max size when reading from an InStream to an ArrayList, or with an allocator?
fraktor has joined #zig
<fraktor> If I use `defer` in a for loop, when does that get run?
<oats> I think at the end of each loop?
<fraktor> Hmm. That's a little frustrating. Is there a way of making those statements run when the function exits?
<fraktor> Assuming that I have multiple return points that is.
<tdeo> why would you want that?
<fraktor> I'm using SDL, which means I don't have control over allocation. I have a list of textures that I'm loading up, and I want to free them when I'm done with them.
<fraktor> Oh wait; I can just do a second loop and defer the entirety of that loop.
joey152 has joined #zig
dputtick has joined #zig
slurpie has joined #zig
aerona has joined #zig
shakesoda has joined #zig
INSANU has quit [Ping timeout: 256 seconds]
decentpenguin has quit [Quit: decentpenguin]
<fraktor> I'm attempting to use a C library that accepts an enum as a parameter, but the definition of the enum is a c_int for some reason. Here's a code snippet and the error message: https://0paste.com/67149
<ifreund> fraktor: that paste isn't loading for me, but you should try using an enum literal `.EnumName`
<fraktor> ifreund: What I have is a c_int, and I need to turn it into an enum.
<fraktor> By the way the service seems to have gone down for a sec, but it just loaded again for me
<ifreund> fraktor: ah yep, loaded for me
<ifreund> try removing the c
<ifreund> just .SDL_FLIP_HORIZONTAL
<fraktor> c is the namespace that it lives in.
<ifreund> did you try it?
<fraktor> ...I did. I don't understand what just happened.
<ifreund> so as far as I understand it zig translates two separate things for c enums
<ifreund> 1. the c.SDL_WHATEVER which is a c_int
<fengb> translate-c provides 2 copies of the enum definition: one as a c_int because C likes to use enums as ints, and one as an actual enum
<fengb> Oh I’m slow again
<ifreund> and 2. an enum corresponding to it, which will be c.enum_typename
<ifreund> and since this is zig you can infer the enum type by starting it with a dot
<fraktor> I didn't know about that second feature. Excuse me while I go and remove some extraneous typing
jwmerrill has joined #zig
INSANU has joined #zig
<ifreund> fraktor: fyi, you can inspect the zig code translate_c is generating if you're curious.
<ifreund> it will put a c_import.zig somewhere in your zig-cache
<st4ll1> so .SDL_FLIP_HORIZONTAL is the same as c.enum_unnamed_93.SDL_FLIP_HORIZONTAL? or how does is work?
<ifreund> st4ll1: yeah that's right
<fraktor> I'm now trying to store .SDL_FLIP_NONE into a variable, but I'm getting the error "cannot store runtime value in type '(enum literal)'"
<st4ll1> i knew it worked but i did not know why :D
<jwmerrill> I've been working on a (now mostly complete) Zig implementation of the Lox language from Crafting Interpreters. I implemented the garbage collector following the Allocator protocol, but I'm wondering if someone more knowledgeable can double check whether I'm correctly obeying the required invariants. Code: https://github.com/jwmerrill/zig-lox/blob/22ba159cef52b67af3b3ed9a37581cd76de949fc/src/memory.zig#L32-L49
<oats> jwmerrill: shit, me too!
<oats> right now I'm trying to come up with an elegant way to handle expression evaluation
<oats> mine's kind of a mess right now
<jwmerrill> oats: cool!
<ifreund> fraktor: it will probably work if you do const myvar: c.enum_name = .SDL_FLIP_NONE;
<oats> crafting interpreters is really nice
<jwmerrill> Here's the Zig source comment saying what a reallocFn must do: https://github.com/ziglang/zig/blob/cf750a58d517c03b38509d9d3189c7b7af41820d/lib/std/mem.zig#L19-L39
<jwmerrill> I'm a little bit worried that running the GC from inside reallocFn (which may end up recursively calling reallocFn and/or shrink) may violate the contract.
<fraktor> So I need to do c.enum_unnamed_93? What if that number changes?
<ifreund> i believe how enum literals work is that they are of a special enum literal type and may coerce into enum types which use that name
<jwmerrill> But tests are passing, so that's good.
<jwmerrill> And the Zig implementation looks very similar to the C implementation of reallocate from the book, so that also seems promising https://github.com/munificent/craftinginterpreters/blob/3dc7cc2030b26dc747d339cde4aa31dad1189b7b/c/memory.c#L24-L49
<ifreund> fraktor: yeah, it's a little nasty, though these numbers have been made to use a namespace-local counter instead of a global one on master
racoon has quit [Quit: ZNC 1.7.5 - https://znc.in]
<ifreund> if you're sticking with 0.6.0 like me you may be interested in my hack https://github.com/ifreund/river/blob/master/src/c.zig#L55
dottedmag has quit [Quit: QUIT]
Barabas has joined #zig
<Barabas> Good evening everyone
<ifreund> hello
dottedmag has joined #zig
<Barabas> I was writing a simple command line program. On windows. And to my surprise when a user presses enter it doesn't end the line with \n but with \r
<Barabas> Is this normal? Is it the same on Linux (probably not)?
<ifreund> no, \r is not a thing on linux
<ifreund> (and I have no idea how windows works sorry)
<oats> jwmerrill: how did you decide to use c_allocator?
<Barabas> So Linux will have \n at the end of a user input line, right?
<jwmerrill> I started of using fixed buffer allocator, but was getting OOM on some benchmarks from the book repo.
* oats is using the ArenaAllocator around the page_allocator and de-initing after each evaluation
<jwmerrill> I think c_allocator is still the only general purpose allocator available.
<oats> jwmerrill: I like how you've limited where you pass around the allocator
<oats> I've basically threaded mine throughout the whole thing :<
<nephele> windows line endings and unix line endings are different, because history :D
<jwmerrill> Once you have a GC, it doesn't seem like there's a good time to deinit a whole arena except when the program actually completes.
<Barabas> I know, but in windows files it's \n\r, right?
<fraktor> I believe it's \r\n
<Barabas> Oooh... then maybe that's what it does and I got confused because it didn't show me the \n
ur5us has joined #zig
<oats> jwmerrill: it's been weirdly fun trying to translate these OOP concepts that I'm only vaguely familiar with
<Barabas> ew OOP :P
<oats> and actually, since you kind of have proper algebraic types in Zig, implementing the AST was much simpler than the java version
mikdusan has quit [Read error: Connection reset by peer]
<oats> almost haskelly
<jwmerrill> I found implementing the OOP section to be a bit of a grind because I don't like using those features much myself. But it was still educational in hindsight.
mikdusan has joined #zig
<oats> makes sense :)
* oats goes and makes a AbstractInterpreterASTFactoryBeanAdapter
<jwmerrill> I only did the bytecode interpreter, which doesn't have an AST. But algebraic data types are awesome for ASTs.
<oats> oh, this is the bytecode VM!
<oats> jesus, how did I not realize lol
<oats> I just thought this was way further along in the tree-walking interpreter lol
<jwmerrill> Haha, that's probably why it looks very different :-)
<oats> wait, does it end up being a kind of JIT bytecode?
<oats> you still have lox scanning code
<jwmerrill> You compile to bytecode, which means an array of u8 where every byte represents an operator or an operand. Then the vm interprets those operands one by one at runtime.
<oats> oh that sounds fun
<jwmerrill> A JIT would be if you generated machine code to run at runtime, but this never generates machine code. It just does the operations that they bytecode says to do.
<jwmerrill> Yeah, it's really neat. I've done a few tree-walking interpreters before, but never a bytecode interpreter. I had heard a lot of the concepts, but actually doing it was very educational.
<oats> I wonder how a lox tree-walker would compare to the bytecode vm, both written in zig
<oats> without the java overhead :P
<oats> er, for performance
<fengb> Hmm, I should take a look at that book too
<oats> fengb: it's real good
<jwmerrill> +1
<oats> the guy's a really great writer
<Barabas> What's this book?
<fengb> He is. I really liked Game Programming Patterns too
<jwmerrill> Crafting Interpreters
<Barabas> Oh that guy, yeah his GPP was nice.
<fengb> I'm from a different world. I've only written bytecode interpreters :P
<Barabas> I took a look at... wren I think it was back then.
<jwmerrill> oats: in a way, you're setting yourself a harder challenge by doing the AST interpreter in Zig, because you have to think about how to manage the memory for all those nodes. Java and its garbage collector take care of that for you.
<oats> yeah, it's been a bit of a challenge
<jwmerrill> The bytecode interpreter does less allocating, so it's actually easier to deal with a language with manual memory management (like C or Zig).
<oats> lots and lots of 'try'ing
<oats> thankful it exists though lol
<jwmerrill> Yeah, 'try' shows up 191 times in my compiler.zig.
<jwmerrill> I've been wondering if I should be doing something to cut down on that, but I think it's just what you're supposed to do.
<oats> I think the only language where you could get rid of the 'try' noise and still have safe code is haskell :P
<Barabas> I guess most of the tries are for memory allocation?
<oats> sorry, I really like haskell, I'll refrain from bringing it up too much :P
<oats> Barabas: yeh
<jwmerrill> Yes, 99.9% of them are in case you hit OutOfMemory while allocating.
aerona has quit [Quit: Leaving]
<Barabas> You can of course try moving the memory allocation up, but that means you have to somehow know how much memory you will be using (at most) further down.
<Barabas> I'm reading the book now :D
<jwmerrill> Yeah, that sounds smart for a lot of applications, but seems impossible to predict in an implementation of a dynamic language.
<jwmerrill> You don't know what programs someone else will write.
<jwmerrill> One thing that's nice about having to write "try" for everything that allocates is that I know that my garbage collector won't run on any lines that don't have "try" on them.
<jwmerrill> Helps with a few cases where you have to temporarily violate a GC invariant, and then make sure it's fixed up again before the GC can have a chance to run.
<Barabas> Yeah I have no idea what your code looks like ^^
<jwmerrill> You can find out if you want, but it's a lot to read if you haven't already read the book. https://github.com/jwmerrill/zig-lox
<Barabas> I'm curious, so I'll take a look :)
aerona has joined #zig
<Barabas> Oh, it doesn't look that bad. Just the compiler has a lot of try.
<jwmerrill> Yeah, it really isn't a problem. Mildly repetetive to type, but I don't mind this kind of repetition because it's easy and represents something important.
drewr has joined #zig
<oats> I also often want to construct an error message string from multiple parts deep down in the call stack, and I need an allocator for that :(
<oats> doesn't feel super great, I should probably try to come up with a better pattern
<oats> I wish you could do a tagged-union-like thing with error values
<oats> like attach a struct to your error value with more info
<jwmerrill> I also wonder a little bit why you can't attach extra data to errors. Possible it's a performance consideration?
<jwmerrill> You could do this by returning a datastructure that might have "errors" without actually using Zig's built in error system. Then you can do whatever you want.
<oats> yeah
<oats> or when you're doing the oopy thing of an object with methods, keep a field with an optional error struct
kevsmith has quit [Remote host closed the connection]
<oats> hrrrrrmmmm
<oats> This channel is now known as #zig-langdev
Akuli has quit [Quit: Leaving]
<ifreund> see https://github.com/ziglang/zig/issues/2647 for some discussion on the topic of error data
<jwmerrill> oats: if you start feeling too bogged down with these kinds of issues doing the AST interpreter, you should think about jumping ahead to the bytecode interpreter. The design is a better match for Zig's capabilities, IMO.
<jwmerrill> I'm sure all the difficulties with the AST approach are solvable, though, if you're enjoying the challenge.
<jwmerrill> ifreund: thanks, interesting read
<oats> jwmerrill: just gotta give it a bit of extra thunk :)
<oats> but yeah, I'm mostly just having fun
constptr has quit [Quit: Connection closed for inactivity]
<oats> I really like the union(error) solution
<oats> that seems like a nice simple way of doing things
foobles has joined #zig
<foobles> andrewrk so I am trying to incorporate your idea for 1332, but I don't see how to use ir_create_basic_block_gen
<foobles> from what I can see, it does nothing to change the ira's current_basic_block
<foobles> could I do this manually?
ifreund has quit [Ping timeout: 272 seconds]
nycex has quit [Remote host closed the connection]
<oats> what happens if you try to free a slice of memory that did not come from the allocator?
nycex has joined #zig
<oats> ie, a stack-allocated slice
<tdeo> undefined behaviour
<tdeo> i don't think there's safety for this yet
nycex- has joined #zig
nycex has quit [Ping timeout: 240 seconds]
metaleap has quit [Remote host closed the connection]
metaleap has joined #zig
metaleap has quit [Remote host closed the connection]
metaleap has joined #zig
return0e has quit [Remote host closed the connection]
INSANU has quit [Read error: Connection reset by peer]
<dputtick> Does anybody know of a way to unwrap an optional + error union in one statement? Something like `const arg = arg_it.next(allocator) orelse catch 8 // default value`, except orelse catch is illegal syntax.
<ikskuh> use parens
<ikskuh> (x catch y) orelse z;
return0e has joined #zig
<dputtick> ah excellent, ty
<dputtick> hm, i can't get it to work with the opposite order: (x orelse y) catch z. the return type of x is ?std.process.NextError![]u8, and I'm trying for something like the nice catch syntax for a default value if there are no command line args
<dputtick> similar to this example https://github.com/ziglang/zig/blob/master/test/cli.zig#L20 except setting a default rather than throwing
wootehfoot has joined #zig
Patrice_ has quit [Ping timeout: 246 seconds]
pystub has quit [Ping timeout: 272 seconds]
<Barabas> dputtick use a block with a return value instead of returning the error as in the example. See https://ziglang.org/documentation/master/#blocks for how to return values from blocks (using break)
plumm has quit [Read error: Connection reset by peer]
plumm has joined #zig
nycex- is now known as nycex
<dputtick> thanks Barabas! works nicely, quite elegant
wootehfoot has quit [Ping timeout: 256 seconds]
<Barabas> you're welcome :)
metaleap has quit [Quit: Leaving]
dddddd has quit [Ping timeout: 260 seconds]
waleee-cl has quit [Quit: Connection closed for inactivity]
marijnfs has joined #zig
marijnfs has quit [Ping timeout: 272 seconds]
marijnfs has joined #zig
dingenskirchen has quit [Quit: dingenskirchen]
dingenskirchen has joined #zig