Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run `rustc` to check a program without generating any files

Tags:

rust

As cargo check shows, it's often useful to check if your program is well-formed without actually generating code (an often pretty time-consuming task). I want to check a single (library) Rust file with rustc directly (I cannot use Cargo!).

cargo check apparently works by calling this:

rustc --emit=metadata -Z no-codegen

This only emits metadata, a .rmeta file. Cargo actually needs that to check crates dependent on the checked crate. In my case I really don't need the metadata file.

I tried the following:

rustc --crate-type=lib --emit=
rustc --crate-type=lib --emit=nothing

But both didn't work. I use --crate-type=lib because my file doesn't have a main function. I need a platform-independent solution (I don't just want to use it on my machine, but use it in a public script).

How do I make rustc not write a single file?

like image 945
Lukas Kalbertodt Avatar asked Jul 23 '18 19:07

Lukas Kalbertodt


2 Answers

You can just skip the --emit flag.

The final command would then be: rustc -Z no-codegen rust.rs

like image 94
H2O Avatar answered Oct 03 '22 03:10

H2O


To quote my own GitHub comment about this very question, there are a few options for stable Rust:

  • rustc --emit=mir -o /dev/null seems to work in 1.18 and newer, writing nothing. (--emit=mir is the only helpful --emit option—the others try to create silly files like /dev/null.foo0.rcgu.o, except --emit=dep-info, which does no checking.)
  • rustc -C extra-filename=-tmp -C linker=true (i.e. use /bin/true as a “linker”) seems to work in all versions, writing some intermediate files but cleaning them up.
  • rustc --out-dir=<new empty temporary directory> is less clever and therefore perhaps less likely to break?

Note that linker errors, if any, will not be found by the first two options (nor by the nightly-only -Zno-codegen option).

like image 38
Anders Kaseorg Avatar answered Oct 03 '22 03:10

Anders Kaseorg