{"action":"created","issue":{"url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/1","repository_url":"https://api.github.com/repos/winksaville/zig-ux-semantics","labels_url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/1/comments","events_url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/1/events","html_url":"https://github.com/winksaville/zig-ux-semantics/issues/1","id":361757107,"node_id":"MDU6SXNzdWUzNjE3NTcxMDc=","number":1,"title":"discussion","user":{"login":"thejoshwolfe","id":87436,"node_id":"MDQ6VXNlcjg3NDM2","avatar_url":"https://avatars2.githubusercontent.com/u/87436?v=4","gravatar_id":"","url":"https://api.github.com/users/thejoshwolfe","html_url":"https://github.com/thejoshwolfe","followers_url":"https://api.github.com/users/thejoshwolfe/followers","following_url":"https://api.github.com/users/thejoshwolfe/following{/other_user}","gists_url":"https://api.github.com/users/thejoshwolfe/gists{/gist_id}","starred_url":"https://api.github.com/users/thejoshwolfe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thejoshwolfe/subscriptions","organizations_url":"https://api.github.com/users/thejoshwolfe/orgs","repos_url":"https://api.github.com/users/thejoshwolfe/repos","events_url":"https://api.github.com/users/thejoshwolfe/events{/privacy}","received_events_url":"https://api.github.com/users/thejoshwolfe/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-09-19T13:46:00Z","updated_at":"2018-09-20T09:18:07Z","closed_at":null,"author_association":"NONE","body":"```zig\r\n//    var val: u0 = undefined;\r\n//    switch (val) {\r\n//        0 => assert(val == 0),\r\n//    }\r\n```\r\nthis should work. please open a bug report.\r\n\r\n```zig\r\n//    var val: u1 = undefined;\r\n//    switch (val) {\r\n//        0 => assert(val == 0),\r\n//        1 => assert(val == 1),\r\n//    }\r\n```\r\n\r\nthis is undefined behavior. u1 is supposed to only hold the values 0 and 1, but in reality there's a bigger type allocated to hold it, probably a u8. wjen you initialize it to undefined, you must not read the value until you properly initialize it. valgrid would call your code a bug as well.\r\n\r\nthe compiler could implicitly put a bitmask on all reads from the u1 so that `undefined & 1` would be either 0 or 1, but those bitmasks are not supposed to be necessary if the value was initialized properly.\r\n\r\nif the u1 were part of a packed struct that was at least the size of a usize and all bits were accounted for in the struct, and the struct was initialized to undefined, then i think your example would behave at runtime the way you're expecting. it's still officially undefined behavior, but a u1 in a packed struct usually must have implicit bitmasks for access.\r\n\r\n```zig\r\n    var val: u0 = 0;\r\n    assert(val == 0);\r\n\r\n    // val = 1;         // Expected an error, got error: integer value 1 cannot be implicitly casted to type 'u0'.\r\n                      // It seems possible this could be done via some\r\n                      // unsafe cast or call to a external function,\r\n    assert(val == 0); // but if it did the result read back must always 0.\r\n```\r\n\r\nit sounds like you want `@truncate()` to happen implicitly, like it does in C. Try `val = @truncate(u0, 1);`."},"comment":{"url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/comments/423107490","html_url":"https://github.com/winksaville/zig-ux-semantics/issues/1#issuecomment-423107490","issue_url":"https://api.github.com/repos/winksaville/zig-ux-semantics/issues/1","id":423107490,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMzEwNzQ5MA==","user":{"login":"winksaville","id":1024284,"node_id":"MDQ6VXNlcjEwMjQyODQ=","avatar_url":"https://avatars3.githubusercontent.com/u/1024284?v=4","gravatar_id":"","url":"https://api.github.com/users/winksaville","html_url":"https://github.com/winksaville","followers_url":"https://api.github.com/users/winksaville/followers","following_url":"https://api.github.com/users/winksaville/following{/other_user}","gists_url":"https://api.github.com/users/winksaville/gists{/gist_id}","starred_url":"https://api.github.com/users/winksaville/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/winksaville/subscriptions","organizations_url":"https://api.github.com/users/winksaville/orgs","repos_url":"https://api.github.com/users/winksaville/repos","events_url":"https://api.github.com/users/winksaville/events{/privacy}","received_events_url":"https://api.github.com/users/winksaville/received_events","type":"User","site_admin":false},"created_at":"2018-09-20T09:18:07Z","updated_at":"2018-09-20T09:18:07Z","author_association":"OWNER","body":"@thejoshwolfe, thanks for the explanation, I've changed my tests to never read an undefined variable before its written and now it works for all build types!!!!\r\n\r\nOf course I had to do more work then necessary because u0 isn't a full citizen of TypeId.Int. I still wanted to test \"random\" data so I created `fn rand() u64` and that works fine with u1:\r\n```\r\ntest \"undefined.u1\" {\r\n    var i: u8 = 32;\r\n    while (i > 0) : (i -= 1) {\r\n        var val: u1 = undefined;\r\n        val = @truncate(u1, rand());\r\n        switch (val) {\r\n            0 => assert(val == 0),\r\n            1 => assert(val == 1),\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nBut a similar u0 version fails because `@truncate(u0, rand())` doesn't work.\r\n```\r\ntest \"undefined.u0.truncate.u0.rand.FAILS\" {\r\n    var i: u8 = 32;\r\n    while (i > 0) : (i -= 1) {\r\n        var val: u0 = undefined;\r\n        val = @truncate(u0, rand());\r\n        assert(val == 0);\r\n    }\r\n}\r\n```\r\n\r\nBut if I use a literal, `@truncate(u0, 123)` (which isn't the test I wanted) it works.\r\n```\r\ntest \"undefined.u0.truncate.u0.literal.OK\" {\r\n    var i: u8 = 32;\r\n    while (i > 0) : (i -= 1) {\r\n        var val: u0 = undefined;\r\n        val = @truncate(u0, 123);\r\n        assert(val == 0);\r\n    }\r\n}\r\n```\r\nNot only doesn't `@truncate(u0, rand())` work, but `prng.random.scalar(u0)` doesn't work either. I consider both of these bugs caused by u0 not being a full citizen of TypeId.Int."}}
{"actor":{"display_login":"winksaville"}}