Search code examples
rustconditional-compilation

Why does invalid code annotated by `#[cfg(test)]` still cause the build to fail?


Running cargo build will succeed for the following code:

#[cfg(test)]
mod tests {
    #[test]
    fn func() { 
        let x = 1;
        sss
    }
}

but will fail for this code:

#[cfg(test)]
mod tests {
    #[test]
    fn func() {
        sss
        let x = 1;
    }
}
error: expected `;`, found keyword `let`
 --> src/lib.rs:5:12
  |
5 |         sss
  |            ^ help: add `;` here
6 |         let x = 1;
  |         --- unexpected token

A section of the Rust Book on Test Organization says:

The #[cfg(test)] annotation on the tests module tells Rust to compile and run the test code only when you run cargo test, not when you run cargo build.

So why does Rust still compile mod tests that is annotated with #[cfg(test)]?


Solution

  • Code that is not compiled with cfg must still be syntactically valid (i.e. parse sucessfully), but nothing more than that.

    In the first code, let x = 1; is a normal variable declaration statement, and sss is a trailing expression returning the value of sss from the function. sss is not defined, and thus of course this code is not valid, but it is syntactically valid.

    In the second snippet, however, you have sss - that is an expression, and must have a trailing semicolon, but it doesn't. This code is not syntactically valid, and the fact that it is cfg-gated does not matter.