Closed
Description
I encountered a weird behavior when using cfg(test)
and cfg(not(test))
.
I tried this code:
fn does_not_work() -> usize {
#[cfg(not(test))]
0
#[cfg(test)]
1
}
Results in:
Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `#`
--> src/lib.rs:6:5
|
4 | 0
| - expected one of `.`, `;`, `?`, `}`, or an operator
5 |
6 | #[cfg(test)]
| ^ unexpected token
error: aborting due to previous error
error: could not compile `playground`.
To learn more, run the command again with --verbose.
Workaround
fn does_work() -> usize {
#[cfg(not(test))]
return 0; // `return` including a semicolon
#[cfg(test)]
1
}
Meta
Rust stable version 1.47.0
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f7f7eb30327396c209b0f358b1a3072b
Metadata
Metadata
Assignees
Labels
Area: Attributes (`#[…]`, `#![…]`)Area: Messages for errors, warnings, and lintsArea: The lexing & parsing of Rust source code to an ASTCategory: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.Relevant to the compiler team, which will review and decide on the PR/issue.