experimental alternative rustc
zustc is a compiler for a subset of Rust built for fast incremental builds. zust-analyzer is a lightweight LSP server.
zustc has its own front end: lexer, parser and type inference. It generates code with Cranelift (the default) or minclif, its own small x86-64 backend. It is tuned for the edit-one-thing-and-rebuild loop of development, and tries to minimize:
zustc can compile itself.
zust-analyzer is an LSP server for editors. It answers definition, references, hover, completion, rename and more with a light analysis over the token stream and a table of definitions. It works on ordinary Rust workspaces, and also indexes std and dependency crates.
Status: experimental.
- zustc targets Linux x86-64 and does not support all of Rust (supported language).
--target wasm32-unknown-unknownwrites a wasm module (experimental: tests/wasm/README.md).- Security regression tests from past Rust CVEs and soundness bugs: tests/security/README.md. What zustc does not catch yet, and what else is left: TODO.md.
- zustc's checks (types, borrow checking, region errors against signatures) are partial, so it is not a replacement for rustc for checking correctness.
- zust-analyzer implements only part of what rust-analyzer does.
cc (used to build the link line)rustup component add rust-srcgit clone https://github.com/mizchi/zust.git
cd zust
cargo install --path .
cargo install --path crates/analyzer
cargo install --path crates/zago
This puts zustc, zust-analyzer and zago in ~/.cargo/bin. You can also run cargo build --release --workspace and use
the binaries in target/release/ without installing.
zustc hello.rs -o hello
./hello
-o, the output goes next to the input with the extension removed (hello).hello.cache: the cachehello.o: the object filehello.link: the saved link linemod x; reads x.rs or x/mod.rs.
zustc check hello.rs
This runs up to type checking (no code generation) and prints all diagnostics. It also checks unreachable functions
and every requested instance of generic functions. When a round of the check has at least 32 functions per worker to
check, they are split across at most -j N forked processes (N is the number of CPUs by default); the diagnostics are
the same at any -j.
Pass a file that lists one crate per line. Each line is lib|bin <name> <root file> [dependencies..], with paths
relative to that file:
lib util util/src/lib.rs
lib geom geom/src/lib.rs util
bin app app/src/main.rs geom util
zustc build workspace.txt -o target
Words after the dependencies set more of the crate:
features=a,b: its features;cfg=name / cfg=name=value: other cfgs (what a build script's rustc-cfg gives);env=KEY=VALUE: the values env! / option_env! see (CARGO_PKG_*, OUT_DIR);exe=NAME: a bin's executable name;extern=ALIAS=NAME: the dependency NAME under the name ALIAS in this crate (a renamed dependency, rustc's
--extern ALIAS=..). Every module of the crate sees it; NAME keeps its own name everywhere else.In a value, %, # and white space are written %XX. A name env! does not find there is looked up in zustc's
own environment, as rustc does.
Each crate gets its own object file and cache, and they are linked at the end. With
--target wasm32-unknown-unknown, the crates' functions and data are put into one wasm module per bin.
Changing the body of a function in a crate that others depend on does not recompile the dependents; zustc only relinks.
See tests/workspace/ for an example.
zustc does not read Cargo.toml: zago (next section) does.
zago builds a Cargo package with zustc: it reads Cargo.toml and Cargo.lock, takes crates.io sources from
$CARGO_HOME/registry/src (where cargo fetch puts them), turns on features as cargo does, and builds the graph as a
zustc workspace (docs/zago.md).
zago run
build, run, check, test, metadata (the JSON of cargo metadata), clean, with cargo's flags
for them. zago check checks every crate a build compiles and lists each one's errors, writing no objects.
zago test runs each test in a process of its own, all at once, as cargo-nextest does.target/zust/<profile>/. With --target wasm32-unknown-unknown, each bin is a wasm module in
target/zust/wasm32-unknown-unknown/<profile>/, and zago run runs it with
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER, as cargo does.rustc-env and
OUT_DIR reach the crates. A proc-macro dependency is built by rustc and run by the toolchain's proc-macro server
(rust-analyzer-proc-macro-srv): its attributes, derives and function-lBorrow check: a by-value argument of a type parameter's type is taken to flow into the result (false errors in a serde format written by hand and in a poll loop)
https://github.com/mizchi/zust/issues/894
Labels: None
A constant rvalue the region check takes as promoted but the lowering leaves in the frame: &(7u32 as u64), &u32::MAX, &[1 + 1, 8] returned as &'static dangle
https://github.com/mizchi/zust/issues/852
Labels: None
An empty Vec's pointer is null (std: dangling and non-null): NonNull::new(v.as_mut_ptr()) is None, bytes' BytesMut::new() panics
https://github.com/mizchi/zust/issues/848
Labels: None
KPI check: the prelude selection's per-name scans of the source add up (ceiling raised in #821)
https://github.com/mizchi/zust/issues/824
Labels: None
Closure calls and captures that still differ from rustc after #821: a call through &closure, Box<closure> calls and bounds, move capture of one field
https://github.com/mizchi/zust/issues/823
Labels: None
A static made by an associated const fn is written as its arguments in field order: zeros for a body with loops, no error
https://github.com/mizchi/zust/issues/788
Labels: None
Method probe: a default of impl Tr for &S comes before S's &mut self method on a place of type S
https://github.com/mizchi/zust/issues/787
Labels: None
Track Tokio default and full-feature support
https://github.com/mizchi/zust/issues/743
Labels: bug, crate-coverage
English | 日本語
This document covers how to change and extend zust (zustc and zust-analyzer). See README.md for usage and docs/architecture.md for how it works inside.
Steps for a fresh clone, in order. After them, every command in this document works.
Install the tools (Linux x86-64):
rustup component add rust-src) for
zust-analyzer's std definitionscc (it drives the link line) and a linker. zustc picks wild > mold > ld.lld > ld. The benchmark numbers and the
CI size budget are measured with wild 0.11.0 (cargo install --locked wild-linker --version 0.11.0, or a release
binary)bench/ and scripts/)tests/compare.sh checks memory errors and leaks with it, and bench/kpi_track.py counts instructions
with cachegrindbench/lsp_parity.py, bench/ide_golden_ra.py)bench/vs_blaze.py)Clone with the submodules. cratebench/ and bench/deps_bench.py build real crates from them:
git clone --recurse-submodules https://github.com/mizchi/zust.git
In an existing clone, run git submodule update --init cratebench/vendor.
Build the two release binaries, target/release/zustc and target/release/zust-analyzer. Most tests and scripts
use these:
cargo build --release --workspace
Fetch rustc's UI tests. This is a sparse clone of tests/ui and tests/auxiliary only, about 170 MB, into
out/rust-tests:
python3 bench/rust_ui.py --fetch
A checkout fetched before tests/auxiliary was added needs it added once. The C helpers that compiletest links
into every test (rust_test_helpers.c) are in it:
git -C out/rust-tests sparse-checkout add tests/auxiliary
Enable the pre-commit hook. It keeps the English / Japanese documents in agreement:
git config core.hooksPath .githooks
Check that everything passes before changing anything:
cargo test --release --workspace
tests/compare.sh
python3 bench/rust_ui.py --baseline bench/rust_ui_passing.txt --check
Take the KPIs once without writing them, to see this machine's numbers. Instruction counts do not depend on the machine's load, so they can be compared with the log:
python3 bench/kpi_track.py "baseline" --dry-run
| path | contents |
|---|---|
src/ | the zustc CLI (src/main.rs) and re-exports of the crates below |
crates/base → syntax → sema → typeck → codegen → core | the compiler layers (each depends on the ones before it) |
crates/minclif / crates/clif / crates/ir | our own x86-64 backend / the Cranelift backend / the instruction-stream types |
crates/ide | editor analysis (symbols, definition, references, hover, completion). Not used by the compiler |
crates/analyzer | the LSP server zust-analyzer |
examples/ | inputs compiled by zustc (not Cargo examples) |
tests/ | zustc tests (comparisons with rustc, caching, self-hosting, and more) |
benches/ / bench/ | criterion benchmarks / benchmark and regression-monitoring scripts |
cratebench/ | a benchmark that builds small real-world crates |
docs/ | the language subset, the design, and work reports (English; Japanese versions in docs/ja/) |
What each crate contains is described in detail in docs/architecture.md.
cargo test --release --workspace
This runs all the tests. With cargo-nextest the same tests run in about two thirds of the time
(each test in its own process, the test binaries side by side; 71s -> 50s on 4 cores). It does not run doc tests,
so CI runs those with cargo test --doc:
cargo nextest run --release --workspace
.config/nextest.toml holds its settings (the ci profile: no fail-fast, a hung test stopped after 5 minutes). A
test that builds and runs many programs spreads them over the cores itself (tests/common/par.rs), so cargo test
is not slow either.
The tests cover:
tests/budget.rs)If you only changed the LSP side, this is enough:
cargo test --release -p zust-ide -p zust-analyzer
tests/compare.sh
Builds examples/*.rs with both zustc and rustc and checks that the output and exit code match. It compares both a
build without a cache and a rebuild from the cache. If valgrind is available, memory errors and definite leaks also
fail the run.
tests/probe.sh
Compares small programs that use common features (tests/probe/*.rs) with rustc and prints how many pass. When you
add a language feature, add a probe here.
tests/selfhost.sh
Compiles parts of zustc's own source (lexer, prelude, link, macros, parse, type inference, and more) with zustc and checks that the output is the same as the rustc-compiled version.
tests/selfhost_compiler.sh
Compiles the whole compiler with both zustc and rustc and checks that the two resulting compilers produce the same
.o files. It also compiles the compiler again with the zustc-bu