{"action":"closed","issue":{"url":"https://api.github.com/repos/andrewrk/zig/issues/71","labels_url":"https://api.github.com/repos/andrewrk/zig/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/andrewrk/zig/issues/71/comments","events_url":"https://api.github.com/repos/andrewrk/zig/issues/71/events","html_url":"https://github.com/andrewrk/zig/issues/71","id":126994705,"number":71,"title":"lift constant structs and arrays out of functions","user":{"login":"andrewrk","id":106511,"avatar_url":"https://avatars.githubusercontent.com/u/106511?v=3","gravatar_id":"","url":"https://api.github.com/users/andrewrk","html_url":"https://github.com/andrewrk","followers_url":"https://api.github.com/users/andrewrk/followers","following_url":"https://api.github.com/users/andrewrk/following{/other_user}","gists_url":"https://api.github.com/users/andrewrk/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewrk/subscriptions","organizations_url":"https://api.github.com/users/andrewrk/orgs","repos_url":"https://api.github.com/users/andrewrk/repos","events_url":"https://api.github.com/users/andrewrk/events{/privacy}","received_events_url":"https://api.github.com/users/andrewrk/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/andrewrk/zig/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/andrewrk/zig/labels/optimization","name":"optimization","color":"5319e7"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-16T02:24:40Z","updated_at":"2016-01-23T03:38:50Z","closed_at":"2016-01-23T03:38:50Z","body":"```zig\r\nfn f() => {\r\n    const hex_mult = []u16{4096, 256, 16, 1};\r\n}\r\n```\r\n\r\nIn this snippet, the local variable `hex_mult` is constant, and all the elements are able to be constant expression evaluated. So we can lift the array out of the stack and make it a global constant at codegen.\r\n\r\nAnother example:\r\n\r\n```zig\r\nfn f(x: i32) => {\r\n    const hex_mult = []i32{x, 256, 16, 1};\r\n}\r\n```\r\n\r\nThis cannot be lifted since one of the elements is determined at runtime and cannot be constant expression evaluated.\r\n\r\nThis works for structs too:\r\n\r\n```zig\r\nstruct Point {\r\n    x: i32,\r\n    y: i32,\r\n}\r\nfn f() => {\r\n    const p = Point {\r\n        .x = 1,\r\n        .y = 2,\r\n    };\r\n}\r\n```\r\n\r\nThe struct is constant and all fields can be constant expression evaluated, so this can get lifted into a global constant."}}