references.excludeTests to exclude #[cfg(test)]-gated refs#20901
Draft
krobelus wants to merge 4 commits intorust-lang:masterfrom
Draft
references.excludeTests to exclude #[cfg(test)]-gated refs#20901krobelus wants to merge 4 commits intorust-lang:masterfrom
krobelus wants to merge 4 commits intorust-lang:masterfrom
Conversation
It's even inlined though that probably only makes a difference in cross-crate calls.
Helps a following commit.
For the next commit. While at it, handle #[cfg(all(test,foo))]. Also take the attribute by reference since we don't need to consume it. Not sure if that's the convention here.
The references.excludeTests setting fails to exclude references
from test code in various scenarios. For example when using
textDocument/references on "foo" in
pub fn foo() {}
#[cfg(test)]
mod tests {
use crate::foo; // Should be excluded
}
same when the test module lives in a different file:
#[cfg(test)]
mod tests;
Try to fix that for most real-world cases.
TODO: the unit test does not actually test this because whereas
full rust-analyzer finds references in code that's disabled due to
#[cfg(test)] directives, the test logic doesn't; "ModuleData::children"
is always empty. Would appreciate help with that.
changelog fix
Closes rust-lang#18573
Comment on lines
+1430
to
+1431
| // N.B. possible false negatives here. | ||
| Invalid | Atom(CfgAtom::KeyValue { .. }) | Any(_) | Not(_) => false, |
There was a problem hiding this comment.
Maybe something similar to CfgExpr::fold can be used for more complex cases, e.g.:
diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs
index 41a2ea8fe0..ad3af99fb9 100644
--- a/crates/ide-db/src/search.rs
+++ b/crates/ide-db/src/search.rs
@@ -1421,17 +1421,26 @@
/// Returns true if the given attributes enable code only in test configurations.
pub fn has_cfg_test(attrs: &AttrsWithOwner) -> bool {
- fn is_cfg_test(cfg_expr: &CfgExpr) -> bool {
+ fn is_cfg_test(cfg_expr: &CfgExpr) -> Option<bool> {
use CfgExpr::*;
use cfg::CfgAtom;
match cfg_expr {
- Atom(CfgAtom::Flag(flag)) => *flag == sym::test,
- All(exprs) => exprs.iter().any(is_cfg_test),
- // N.B. possible false negatives here.
- Invalid | Atom(CfgAtom::KeyValue { .. }) | Any(_) | Not(_) => false,
+ Invalid => None,
+ Atom(CfgAtom::Flag(flag)) => Some(*flag == sym::test),
+ Atom(CfgAtom::KeyValue { .. }) => Some(false),
+ All(exprs) => {
+ exprs.iter().try_fold(false, |acc, expr| is_cfg_test(expr).map(|res| acc || res))
+ }
+ Any(exprs) => {
+ exprs.iter().try_fold(true, |acc, expr| is_cfg_test(expr).map(|res| acc && res))
+ }
+ Not(expr) => is_cfg_test(expr).map(|is_test| !is_test),
}
}
- attrs.cfgs().any(|cfg_expr| is_cfg_test(&cfg_expr))
+ attrs
+ .cfgs()
+ .try_fold(false, |acc, cfg_expr| is_cfg_test(&cfg_expr).map(|is_test| acc || is_test))
+ .unwrap_or(false)
}
/// Returns true if this reference's enclosing module is declared conditional on `cfg(test)`.I noticed that expressions annotated with #[cfg(test)] are not handled by this PR, and it seems that the approach used for other ast types does not work for ast::Expr. Does the ast::HasAttrs trait correspond to the types which can be annotated with #[cfg(test)]? If so, maybe the implementation can be based around that. It might be unnecessary to handle some of these types, since I don't know why anyone would put #[cfg(test)] on something like an expression.
Collaborator
|
☔ The latest upstream changes (possibly #21804) made this pull request unmergeable. Please resolve the merge conflicts. |
Contributor
|
@krobelus Are you still interested in this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The references.excludeTests setting fails to exclude references from test code in various scenarios.
For example when using textDocument/references on "foo" in
same when the test module lives in a different file:
Try to fix that for most real-world cases.
TODO: the unit test does not actually test this because whereas
full rust-analyzer finds references in code that's disabled due to
#[cfg(test)] directives, the test logic doesn't; "ModuleData::children"
is always empty. Would appreciate help with that.
changelog fix
Closes #18573