diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000..86b8f6f471 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: [andrewrk] +patreon: andrewrk diff --git a/CMakeLists.txt b/CMakeLists.txt index 985216ed7b..d77b6f09ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -630,9 +630,11 @@ set(ZIG_STD_FILES "os/windows/bits.zig" "os/windows/error.zig" "os/windows/kernel32.zig" + "os/windows/lang.zig" "os/windows/ntdll.zig" "os/windows/ole32.zig" "os/windows/shell32.zig" + "os/windows/sublang.zig" "os/zen.zig" "packed_int_array.zig" "pdb.zig" @@ -6066,6 +6068,8 @@ set(ZIG_LIBC_FILES "include/x86_64-linux-musl/bits/stat.h" "include/x86_64-linux-musl/bits/syscall.h" "include/x86_64-linux-musl/bits/user.h" + "include/wasm32-freestanding-musl/bits/alltypes.h" + "include/wasm32-freestanding-musl/errno.h" "musl/arch/aarch64/atomic_arch.h" "musl/arch/aarch64/bits/alltypes.h.in" "musl/arch/aarch64/bits/endian.h" @@ -6726,9 +6730,13 @@ add_custom_command( "-Doutput-dir=${CMAKE_BINARY_DIR}" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" DEPENDS + "${CMAKE_SOURCE_DIR}/src-self-hosted/dep_tokenizer.zig" "${CMAKE_SOURCE_DIR}/src-self-hosted/stage1.zig" "${CMAKE_SOURCE_DIR}/src-self-hosted/translate_c.zig" "${CMAKE_SOURCE_DIR}/build.zig" + "${CMAKE_SOURCE_DIR}/std/zig/parse.zig" + "${CMAKE_SOURCE_DIR}/std/zig/render.zig" + "${CMAKE_SOURCE_DIR}/std/zig/tokenizer.zig" ) add_custom_target(userland_target DEPENDS "${LIBUSERLAND}") add_executable(zig "${ZIG_MAIN_SRC}") diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..c19ef48829 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,91 @@ +## Contributing + +### Start a Project Using Zig + +One of the best ways you can contribute to Zig is to start using it for a +personal project. Here are some great examples: + + * [Oxid](https://github.com/dbandstra/oxid) - arcade style game + * [TM35-Metronome](https://github.com/TM35-Metronome) - tools for modifying and randomizing Pokémon games + * [trOS](https://github.com/sjdh02/trOS) - tiny aarch64 baremetal OS thingy + +Without fail, these projects lead to discovering bugs and helping flesh out use +cases, which lead to further design iterations of Zig. Importantly, each issue +found this way comes with real world motivations, so it is easy to explain +your reasoning behind proposals and feature requests. + +Ideally, such a project will help you to learn new skills and add something +to your personal portfolio at the same time. + +### Spread the Word + +Another way to contribute is to write about Zig, or speak about Zig at a +conference, or do either of those things for your project which uses Zig. +Here are some examples: + + * [Iterative Replacement of C with Zig](http://tiehuis.github.io/blog/zig1.html) + * [The Right Tool for the Right Job: Redis Modules & Zig](https://www.youtube.com/watch?v=eCHM8-_poZY) + +Zig is a brand new language, with no advertising budget. Word of mouth is the +only way people find out about the project, and the more people hear about it, +the more people will use it, and the better chance we have to take over the +world. + +### Finding Contributor Friendly Issues + +Please note that issues labeled +[Proposal](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aproposal) +but do not also have the +[Accepted](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aaccepted) +label are still under consideration, and efforts to implement such a proposal +have a high risk of being wasted. If you are interested in a proposal which is +still under consideration, please express your interest in the issue tracker, +providing extra insights and considerations that others have not yet expressed. +The most highly regarded argument in such a discussion is a real world use case. + +The issue label +[Contributor Friendly](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3A%22contributor+friendly%22) +exists to help contributors find issues that are "limited in scope and/or +knowledge of Zig internals." + +### Editing Source Code + +First, build the Stage 1 compiler as described in [the Building section](#building). + +When making changes to the standard library, be sure to edit the files in the +`std` directory and not the installed copy in the build directory. If you add a +new file to the standard library, you must also add the file path in +CMakeLists.txt. + +To test changes, do the following from the build directory: + +1. Run `make install` (on POSIX) or + `msbuild -p:Configuration=Release INSTALL.vcxproj` (on Windows). +2. `bin/zig build --build-file ../build.zig test` (on POSIX) or + `bin\zig.exe build --build-file ..\build.zig test` (on Windows). + +That runs the whole test suite, which does a lot of extra testing that you +likely won't always need, and can take upwards of 2 hours. This is what the +CI server runs when you make a pull request. + +To save time, you can add the `--help` option to the `zig build` command and +see what options are available. One of the most helpful ones is +`-Dskip-release`. Adding this option to the command in step 2 above will take +the time down from around 2 hours to about 6 minutes, and this is a good +enough amount of testing before making a pull request. + +Another example is choosing a different set of things to test. For example, +`test-std` instead of `test` will only run the standard library tests, and +not the other ones. Combining this suggestion with the previous one, you could +do this: + +`bin/zig build --build-file ../build.zig test-std -Dskip-release` (on POSIX) or +`bin\zig.exe build --build-file ..\build.zig test-std -Dskip-release` (on Windows). + +This will run only the standard library tests, in debug mode only, for all +targets (it will cross-compile the tests for non-native targets but not run +them). + +When making changes to the compiler source code, the most helpful test step to +run is `test-behavior`. When editing documentation it is `docs`. You can find +this information and more in the `--help` menu. diff --git a/README.md b/README.md index 4ca79232d8..92ad435f5b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Zig is an open-source programming language designed for **robustness**, * [Introduction](https://ziglang.org/#Introduction) * [Download & Documentation](https://ziglang.org/download) * [Community](https://github.com/ziglang/zig/wiki/Community) + * [Contributing](https://github.com/ziglang/zig/blob/master/CONTRIBUTING.md) ## Building from Source @@ -96,95 +97,3 @@ use stage 1. ``` ./stage2/bin/zig build --build-file ../build.zig install -Drelease-fast ``` - -## Contributing - -### Start a Project Using Zig - -One of the best ways you can contribute to Zig is to start using it for a -personal project. Here are some great examples: - - * [Oxid](https://github.com/dbandstra/oxid) - arcade style game - * [TM35-Metronome](https://github.com/TM35-Metronome) - tools for modifying and randomizing Pokémon games - * [trOS](https://github.com/sjdh02/trOS) - tiny aarch64 baremetal OS thingy - -Without fail, these projects lead to discovering bugs and helping flesh out use -cases, which lead to further design iterations of Zig. Importantly, each issue -found this way comes with real world motivations, so it is easy to explain -your reasoning behind proposals and feature requests. - -Ideally, such a project will help you to learn new skills and add something -to your personal portfolio at the same time. - -### Spread the Word - -Another way to contribute is to write about Zig, or speak about Zig at a -conference, or do either of those things for your project which uses Zig. -Here are some examples: - - * [Iterative Replacement of C with Zig](http://tiehuis.github.io/blog/zig1.html) - * [The Right Tool for the Right Job: Redis Modules & Zig](https://www.youtube.com/watch?v=eCHM8-_poZY) - -Zig is a brand new language, with no advertising budget. Word of mouth is the -only way people find out about the project, and the more people hear about it, -the more people will use it, and the better chance we have to take over the -world. - -### Finding Contributor Friendly Issues - -Please note that issues labeled -[Proposal](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aproposal) -but do not also have the -[Accepted](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3Aaccepted) -label are still under consideration, and efforts to implement such a proposal -have a high risk of being wasted. If you are interested in a proposal which is -still under consideration, please express your interest in the issue tracker, -providing extra insights and considerations that others have not yet expressed. -The most highly regarded argument in such a discussion is a real world use case. - -The issue label -[Contributor Friendly](https://github.com/ziglang/zig/issues?q=is%3Aissue+is%3Aopen+label%3A%22contributor+friendly%22) -exists to help contributors find issues that are "limited in scope and/or -knowledge of Zig internals." - -### Editing Source Code - -First, build the Stage 1 compiler as described in [the Building section](#building). - -When making changes to the standard library, be sure to edit the files in the -`std` directory and not the installed copy in the build directory. If you add a -new file to the standard library, you must also add the file path in -CMakeLists.txt. - -To test changes, do the following from the build directory: - -1. Run `make install` (on POSIX) or - `msbuild -p:Configuration=Release INSTALL.vcxproj` (on Windows). -2. `bin/zig build --build-file ../build.zig test` (on POSIX) or - `bin\zig.exe build --build-file ..\build.zig test` (on Windows). - -That runs the whole test suite, which does a lot of extra testing that you -likely won't always need, and can take upwards of 2 hours. This is what the -CI server runs when you make a pull request. - -To save time, you can add the `--help` option to the `zig build` command and -see what options are available. One of the most helpful ones is -`-Dskip-release`. Adding this option to the command in step 2 above will take -the time down from around 2 hours to about 6 minutes, and this is a good -enough amount of testing before making a pull request. - -Another example is choosing a different set of things to test. For example, -`test-std` instead of `test` will only run the standard library tests, and -not the other ones. Combining this suggestion with the previous one, you could -do this: - -`bin/zig build --build-file ../build.zig test-std -Dskip-release` (on POSIX) or -`bin\zig.exe build --build-file ..\build.zig test-std -Dskip-release` (on Windows). - -This will run only the standard library tests, in debug mode only, for all -targets (it will cross-compile the tests for non-native targets but not run -them). - -When making changes to the compiler source code, the most helpful test step to -run is `test-behavior`. When editing documentation it is `docs`. You can find -this information and more in the `--help` menu. diff --git a/ci/azure/linux_script b/ci/azure/linux_script index aaa75d6fce..20373f3a49 100755 --- a/ci/azure/linux_script +++ b/ci/azure/linux_script @@ -28,7 +28,7 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then docker run -i --mount type=bind,source="$ARTIFACTSDIR",target=/z ziglang/static-base:llvm8-1 -j2 $BUILD_SOURCEVERSION TARBALL="$(ls $ARTIFACTSDIR)" mv "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg" - s3cmd put -P "$ARTIFACTSDIR/$TARBALL" s3://ziglang.org/builds/ + s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$ARTIFACTSDIR/$TARBALL" s3://ziglang.org/builds/ SHASUM=$(sha256sum $ARTIFACTSDIR/$TARBALL | cut '-d ' -f1) BYTESIZE=$(wc -c < $ARTIFACTSDIR/$TARBALL) diff --git a/ci/azure/macos_script b/ci/azure/macos_script index 1b1d8741fa..7a7f1d1bce 100755 --- a/ci/azure/macos_script +++ b/ci/azure/macos_script @@ -6,7 +6,7 @@ set -e brew install s3cmd gcc@8 ZIGDIR="$(pwd)" -CACHE_BASENAME="llvm+clang-8.0.0-macos-x86_64-gcc8-release-static" +CACHE_BASENAME="llvm+clang-8.0.0-macos-x86_64-gcc8-release" PREFIX="$HOME/$CACHE_BASENAME" TMPDIR="$HOME/tmpz" JOBS="-j2" @@ -62,7 +62,7 @@ else cd $HOME tar cfJ "$CACHE_BASENAME.tar.xz" "$CACHE_BASENAME" cp "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg" - s3cmd put -P "$CACHE_BASENAME.tar.xz" "s3://ziglang.org/builds/$CACHE_BASENAME.tar.xz" + s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$CACHE_BASENAME.tar.xz" "s3://ziglang.org/builds/$CACHE_BASENAME.tar.xz" fi cd $ZIGDIR @@ -85,7 +85,7 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then tar cfJ "$TARBALL" "$DIRNAME" mv "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg" - s3cmd put -P "$TARBALL" s3://ziglang.org/builds/ + s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$TARBALL" s3://ziglang.org/builds/ SHASUM=$(shasum -a 256 $TARBALL | cut '-d ' -f1) BYTESIZE=$(wc -c < $TARBALL) diff --git a/ci/azure/pipelines.yml b/ci/azure/pipelines.yml index 37fce2a27a..8e6fcde636 100644 --- a/ci/azure/pipelines.yml +++ b/ci/azure/pipelines.yml @@ -1,7 +1,7 @@ jobs: - job: BuildMacOS pool: - vmImage: 'macOS 10.13' + vmImage: 'macOS 10.14' timeoutInMinutes: 360 diff --git a/ci/azure/update_download_page b/ci/azure/update_download_page index c1e816a309..c6a690e8e6 100755 --- a/ci/azure/update_download_page +++ b/ci/azure/update_download_page @@ -35,7 +35,7 @@ env "../$ZIG" run update-download-page.zig mv "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg" -s3cmd put -P "../$SRC_TARBALL" s3://ziglang.org/builds/ +s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "../$SRC_TARBALL" s3://ziglang.org/builds/ s3cmd put -P "../$LANGREF" s3://ziglang.org/documentation/master/index.html --add-header="Cache-Control: max-age=0, must-revalidate" s3cmd put -P www/download/index.html s3://ziglang.org/download/index.html --add-header="Cache-Control: max-age=0, must-revalidate" s3cmd put -P www/download/index.json s3://ziglang.org/download/index.json --add-header="Cache-Control: max-age=0, must-revalidate" diff --git a/ci/azure/windows_upload b/ci/azure/windows_upload index 8478be0e0b..266d1b57dd 100755 --- a/ci/azure/windows_upload +++ b/ci/azure/windows_upload @@ -19,7 +19,7 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then 7z a "$TARBALL" "$DIRNAME" mv "$DOWNLOADSECUREFILE_SECUREFILEPATH" "$HOME/.s3cfg" - s3cmd put -P "$TARBALL" s3://ziglang.org/builds/ + s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$TARBALL" s3://ziglang.org/builds/ SHASUM=$(sha256sum $TARBALL | cut '-d ' -f1) BYTESIZE=$(wc -c < $TARBALL) diff --git a/ci/srht/freebsd_script b/ci/srht/freebsd_script index 0ff66aaec5..9edf076c53 100755 --- a/ci/srht/freebsd_script +++ b/ci/srht/freebsd_script @@ -36,7 +36,7 @@ if [ -f ~/.s3cfg ]; then mv release "$DIRNAME" tar cfJ "$TARBALL" "$DIRNAME" - s3cmd put -P "$TARBALL" s3://ziglang.org/builds/ + s3cmd put -P --add-header="cache-control: public, max-age=31536000, immutable" "$TARBALL" s3://ziglang.org/builds/ SHASUM=$(shasum -a 256 $TARBALL | cut '-d ' -f1) BYTESIZE=$(wc -c < $TARBALL) diff --git a/doc/docgen.zig b/doc/docgen.zig index 998db54bbc..461ff5cac2 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -788,7 +788,7 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok std.zig.Token.Id.Keyword_try, std.zig.Token.Id.Keyword_union, std.zig.Token.Id.Keyword_unreachable, - std.zig.Token.Id.Keyword_use, + std.zig.Token.Id.Keyword_usingnamespace, std.zig.Token.Id.Keyword_var, std.zig.Token.Id.Keyword_volatile, std.zig.Token.Id.Keyword_allowzero, diff --git a/doc/langref.html.in b/doc/langref.html.in index 61db4ed598..ba093881ab 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2026,9 +2026,9 @@ fn foo(bytes: []u8) u32 { {#header_open|allowzero#}

This pointer attribute allows a pointer to have address zero. This is only ever needed on the - freestanding OS target, where the address zero is mappable. In this code example, if the pointer - did not have the {#syntax#}allowzero{#endsyntax#} attribute, this would be a - {#link|Pointer Cast Invalid Null#} panic: + freestanding OS target, where the address zero is mappable. If you want to represent null pointers, use + {#link|Optional Pointers#} instead. In this code example, if the pointer did not have the + {#syntax#}allowzero{#endsyntax#} attribute, this would be a {#link|Pointer Cast Invalid Null#} panic:

{#code_begin|test|allowzero#} const std = @import("std"); @@ -2263,6 +2263,28 @@ test "linked list" { } {#code_end#} + {#header_open|Default Field Values#} +

+ Each struct field may have an expression indicating the default field value. Such expressions + are executed at {#link|comptime#}, and allow the field to be omitted in a struct literal expression: +

+ {#code_begin|test#} +const Foo = struct { + a: i32 = 1234, + b: i32, +}; + +test "default struct initialization fields" { + const x = Foo{ + .b = 5, + }; + if (x.a + x.b != 1239) { + @compileError("it's even comptime known!"); + } +} + {#code_end#} + {#header_close#} + {#header_open|extern struct#}

An {#syntax#}extern struct{#endsyntax#} has in-memory layout guaranteed to match the C ABI for the target.

@@ -3271,6 +3293,7 @@ test "for else" { var items = []?i32 { 3, 4, null, 5 }; // For loops can also be used as expressions. + // Similar to while loops, when you break from a for loop, the else branch is not evaluated. var sum: i32 = 0; const result = for (items) |value| { if (value == null) { @@ -7545,7 +7568,7 @@ pub const TypeInfo = union(TypeId) { pub const Struct = struct { layout: ContainerLayout, fields: []StructField, - defs: []Definition, + decls: []Declaration, }; pub const Optional = struct { @@ -7573,7 +7596,7 @@ pub const TypeInfo = union(TypeId) { layout: ContainerLayout, tag_type: type, fields: []EnumField, - defs: []Definition, + decls: []Declaration, }; pub const UnionField = struct { @@ -7586,7 +7609,7 @@ pub const TypeInfo = union(TypeId) { layout: ContainerLayout, tag_type: ?type, fields: []UnionField, - defs: []Definition, + decls: []Declaration, }; pub const CallingConvention = enum { @@ -7622,7 +7645,7 @@ pub const TypeInfo = union(TypeId) { child: type, }; - pub const Definition = struct { + pub const Declaration = struct { name: []const u8, is_pub: bool, data: Data, @@ -7630,9 +7653,9 @@ pub const TypeInfo = union(TypeId) { pub const Data = union(enum) { Type: type, Var: type, - Fn: FnDef, + Fn: FnDecl, - pub const FnDef = struct { + pub const FnDecl = struct { fn_type: type, inline_type: Inline, calling_convention: CallingConvention, @@ -8466,6 +8489,8 @@ fn concat(allocator: *Allocator, a: []const u8, b: []const u8) ![]u8 {
  • Are you linking libc? In this case, {#syntax#}std.heap.c_allocator{#endsyntax#} is likely the right choice, at least for your main allocator.
  • +
  • Are you building for WebAssembly? In this case, {#syntax#}std.heap.wasm_allocator{#endsyntax#} is likely + the right choice for your main allocator as it uses WebAssembly's memory instructions.
  • Is the maximum number of bytes that you will need bounded by a number known at {#link|comptime#}? In this case, use {#syntax#}std.heap.FixedBufferAllocator{#endsyntax#} or @@ -8996,8 +9021,12 @@ all your base are belong to us {#header_close#} {#header_close#} {#header_open|WebAssembly#} +

    Zig supports building for WebAssembly out of the box. There is also a specialized {#syntax#}std.heap.wasm_allocator{#endsyntax#} + memory allocator for WebAssembly environments.

    {#header_open|Freestanding#} - {#code_begin|lib|wasm#} +

    For host environments like the web browser and nodejs, build as a library using the freestanding OS target. + Here's an example of running Zig code compiled to WebAssembly with nodejs.

    + {#code_begin|lib|math#} {#target_wasm#} extern fn print(i32) void; @@ -9006,7 +9035,22 @@ export fn add(a: i32, b: i32) void { } {#code_end#} {#header_close#} +

    test.js

    +
    const fs = require('fs');
    +const source = fs.readFileSync("./math.wasm");
    +const typedArray = new Uint8Array(source);
    +
    +WebAssembly.instantiate(typedArray, {
    +  env: {
    +    print: (result) => { console.log(`The result is ${result}`); }
    +  }}).then(result => {
    +  const add = result.instance.exports.add;
    +  add(1, 2);
    +});
    +
    $ node test.js
    +The result is 3
    {#header_open|WASI#} +

    Zig's support for WebAssembly System Interface (WASI) is under active development. Example of using the standard library and reading command line arguments:

    {#code_begin|exe|wasi#} {#target_wasi#} const std = @import("std"); @@ -9020,6 +9064,10 @@ pub fn main() !void { } } {#code_end#} +
    $ wasmer run wasi.wasm 123 hello
    +0: wasi.wasm
    +1: 123
    +2: hello
    {#header_close#} {#header_close#} {#header_open|Targets#} @@ -9260,6 +9308,7 @@ Available libcs: s390x-linux-musl sparc-linux-gnu sparcv9-linux-gnu + wasm32-freestanding-musl x86_64-linux-gnu x86_64-linux-gnux32 x86_64-linux-musl @@ -9892,7 +9941,7 @@ KEYWORD_try <- 'try' end_of_word KEYWORD_undefined <- 'undefined' end_of_word KEYWORD_union <- 'union' end_of_word KEYWORD_unreachable <- 'unreachable' end_of_word -KEYWORD_use <- 'use' end_of_word +KEYWORD_usingnamespace <- 'usingnamespace' end_of_word KEYWORD_var <- 'var' end_of_word KEYWORD_volatile <- 'volatile' end_of_word KEYWORD_while <- 'while' end_of_word @@ -9909,7 +9958,7 @@ keyword <- KEYWORD_align / KEYWORD_and / KEYWORD_allowzero / KEYWORD_anyerror / KEYWORD_stdcallcc / KEYWORD_struct / KEYWORD_suspend / KEYWORD_switch / KEYWORD_test / KEYWORD_threadlocal / KEYWORD_true / KEYWORD_try / KEYWORD_undefined / KEYWORD_union / KEYWORD_unreachable - / KEYWORD_use / KEYWORD_var / KEYWORD_volatile / KEYWORD_while + / KEYWORD_usingnamespace / KEYWORD_var / KEYWORD_volatile / KEYWORD_while {#header_close#} {#header_open|Zen#}