diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/cfg/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/cfg/src/cfg_expr.rs | 113 | ||||
-rw-r--r-- | crates/cfg/src/dnf.rs | 320 | ||||
-rw-r--r-- | crates/cfg/src/lib.rs | 130 | ||||
-rw-r--r-- | crates/cfg/src/tests.rs | 193 | ||||
-rw-r--r-- | crates/hir_def/src/attr.rs | 14 | ||||
-rw-r--r-- | crates/hir_def/src/diagnostics.rs | 17 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 16 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 29 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 22 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 4 | ||||
-rw-r--r-- | crates/ide/src/runnables.rs | 84 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cargo_target_spec.rs | 10 | ||||
-rw-r--r-- | crates/rust-analyzer/src/to_proto.rs | 2 |
15 files changed, 816 insertions, 140 deletions
diff --git a/Cargo.lock b/Cargo.lock index e819297bd..8bafc3895 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -162,6 +162,7 @@ checksum = "ed67cbde08356238e75fc4656be4749481eeffb09e19f320a25237d5221c985d" | |||
162 | name = "cfg" | 162 | name = "cfg" |
163 | version = "0.0.0" | 163 | version = "0.0.0" |
164 | dependencies = [ | 164 | dependencies = [ |
165 | "expect-test", | ||
165 | "mbe", | 166 | "mbe", |
166 | "rustc-hash", | 167 | "rustc-hash", |
167 | "syntax", | 168 | "syntax", |
diff --git a/crates/cfg/Cargo.toml b/crates/cfg/Cargo.toml index a6785ee8e..c68e391c1 100644 --- a/crates/cfg/Cargo.toml +++ b/crates/cfg/Cargo.toml | |||
@@ -17,3 +17,4 @@ tt = { path = "../tt", version = "0.0.0" } | |||
17 | [dev-dependencies] | 17 | [dev-dependencies] |
18 | mbe = { path = "../mbe" } | 18 | mbe = { path = "../mbe" } |
19 | syntax = { path = "../syntax" } | 19 | syntax = { path = "../syntax" } |
20 | expect-test = "1.0" | ||
diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs index 336fe25bc..42327f1e1 100644 --- a/crates/cfg/src/cfg_expr.rs +++ b/crates/cfg/src/cfg_expr.rs | |||
@@ -2,30 +2,77 @@ | |||
2 | //! | 2 | //! |
3 | //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation | 3 | //! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation |
4 | 4 | ||
5 | use std::slice::Iter as SliceIter; | 5 | use std::{fmt, slice::Iter as SliceIter}; |
6 | 6 | ||
7 | use tt::SmolStr; | 7 | use tt::SmolStr; |
8 | 8 | ||
9 | /// A simple configuration value passed in from the outside. | ||
10 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | ||
11 | pub enum CfgAtom { | ||
12 | /// eg. `#[cfg(test)]` | ||
13 | Flag(SmolStr), | ||
14 | /// eg. `#[cfg(target_os = "linux")]` | ||
15 | /// | ||
16 | /// Note that a key can have multiple values that are all considered "active" at the same time. | ||
17 | /// For example, `#[cfg(target_feature = "sse")]` and `#[cfg(target_feature = "sse2")]`. | ||
18 | KeyValue { key: SmolStr, value: SmolStr }, | ||
19 | } | ||
20 | |||
21 | impl CfgAtom { | ||
22 | /// Returns `true` when the atom comes from the target specification. | ||
23 | /// | ||
24 | /// If this returns `true`, then changing this atom requires changing the compilation target. If | ||
25 | /// it returns `false`, the atom might come from a build script or the build system. | ||
26 | pub fn is_target_defined(&self) -> bool { | ||
27 | match self { | ||
28 | CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"), | ||
29 | CfgAtom::KeyValue { key, value: _ } => matches!( | ||
30 | &**key, | ||
31 | "target_arch" | ||
32 | | "target_os" | ||
33 | | "target_env" | ||
34 | | "target_family" | ||
35 | | "target_endian" | ||
36 | | "target_pointer_width" | ||
37 | | "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature` | ||
38 | ), | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl fmt::Display for CfgAtom { | ||
44 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
45 | match self { | ||
46 | CfgAtom::Flag(name) => write!(f, "{}", name), | ||
47 | CfgAtom::KeyValue { key, value } => write!(f, "{} = {:?}", key, value), | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
9 | #[derive(Debug, Clone, PartialEq, Eq)] | 52 | #[derive(Debug, Clone, PartialEq, Eq)] |
10 | pub enum CfgExpr { | 53 | pub enum CfgExpr { |
11 | Invalid, | 54 | Invalid, |
12 | Atom(SmolStr), | 55 | Atom(CfgAtom), |
13 | KeyValue { key: SmolStr, value: SmolStr }, | ||
14 | All(Vec<CfgExpr>), | 56 | All(Vec<CfgExpr>), |
15 | Any(Vec<CfgExpr>), | 57 | Any(Vec<CfgExpr>), |
16 | Not(Box<CfgExpr>), | 58 | Not(Box<CfgExpr>), |
17 | } | 59 | } |
18 | 60 | ||
61 | impl From<CfgAtom> for CfgExpr { | ||
62 | fn from(atom: CfgAtom) -> Self { | ||
63 | CfgExpr::Atom(atom) | ||
64 | } | ||
65 | } | ||
66 | |||
19 | impl CfgExpr { | 67 | impl CfgExpr { |
20 | pub fn parse(tt: &tt::Subtree) -> CfgExpr { | 68 | pub fn parse(tt: &tt::Subtree) -> CfgExpr { |
21 | next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) | 69 | next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) |
22 | } | 70 | } |
23 | /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. | 71 | /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. |
24 | pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> { | 72 | pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> { |
25 | match self { | 73 | match self { |
26 | CfgExpr::Invalid => None, | 74 | CfgExpr::Invalid => None, |
27 | CfgExpr::Atom(name) => Some(query(name, None)), | 75 | CfgExpr::Atom(atom) => Some(query(atom)), |
28 | CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))), | ||
29 | CfgExpr::All(preds) => { | 76 | CfgExpr::All(preds) => { |
30 | preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) | 77 | preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) |
31 | } | 78 | } |
@@ -54,7 +101,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> { | |||
54 | // FIXME: escape? raw string? | 101 | // FIXME: escape? raw string? |
55 | let value = | 102 | let value = |
56 | SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); | 103 | SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); |
57 | CfgExpr::KeyValue { key: name, value } | 104 | CfgAtom::KeyValue { key: name, value }.into() |
58 | } | 105 | } |
59 | _ => return Some(CfgExpr::Invalid), | 106 | _ => return Some(CfgExpr::Invalid), |
60 | } | 107 | } |
@@ -70,7 +117,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> { | |||
70 | _ => CfgExpr::Invalid, | 117 | _ => CfgExpr::Invalid, |
71 | } | 118 | } |
72 | } | 119 | } |
73 | _ => CfgExpr::Atom(name), | 120 | _ => CfgAtom::Flag(name).into(), |
74 | }; | 121 | }; |
75 | 122 | ||
76 | // Eat comma separator | 123 | // Eat comma separator |
@@ -81,53 +128,3 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> { | |||
81 | } | 128 | } |
82 | Some(ret) | 129 | Some(ret) |
83 | } | 130 | } |
84 | |||
85 | #[cfg(test)] | ||
86 | mod tests { | ||
87 | use super::*; | ||
88 | |||
89 | use mbe::ast_to_token_tree; | ||
90 | use syntax::ast::{self, AstNode}; | ||
91 | |||
92 | fn assert_parse_result(input: &str, expected: CfgExpr) { | ||
93 | let (tt, _) = { | ||
94 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
95 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
96 | ast_to_token_tree(&tt).unwrap() | ||
97 | }; | ||
98 | let cfg = CfgExpr::parse(&tt); | ||
99 | assert_eq!(cfg, expected); | ||
100 | } | ||
101 | |||
102 | #[test] | ||
103 | fn test_cfg_expr_parser() { | ||
104 | assert_parse_result("#![cfg(foo)]", CfgExpr::Atom("foo".into())); | ||
105 | assert_parse_result("#![cfg(foo,)]", CfgExpr::Atom("foo".into())); | ||
106 | assert_parse_result( | ||
107 | "#![cfg(not(foo))]", | ||
108 | CfgExpr::Not(Box::new(CfgExpr::Atom("foo".into()))), | ||
109 | ); | ||
110 | assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); | ||
111 | |||
112 | // Only take the first | ||
113 | assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgExpr::Atom("foo".into())); | ||
114 | |||
115 | assert_parse_result( | ||
116 | r#"#![cfg(all(foo, bar = "baz"))]"#, | ||
117 | CfgExpr::All(vec![ | ||
118 | CfgExpr::Atom("foo".into()), | ||
119 | CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, | ||
120 | ]), | ||
121 | ); | ||
122 | |||
123 | assert_parse_result( | ||
124 | r#"#![cfg(any(not(), all(), , bar = "baz",))]"#, | ||
125 | CfgExpr::Any(vec![ | ||
126 | CfgExpr::Not(Box::new(CfgExpr::Invalid)), | ||
127 | CfgExpr::All(vec![]), | ||
128 | CfgExpr::Invalid, | ||
129 | CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, | ||
130 | ]), | ||
131 | ); | ||
132 | } | ||
133 | } | ||
diff --git a/crates/cfg/src/dnf.rs b/crates/cfg/src/dnf.rs new file mode 100644 index 000000000..580c9a9a2 --- /dev/null +++ b/crates/cfg/src/dnf.rs | |||
@@ -0,0 +1,320 @@ | |||
1 | //! Disjunctive Normal Form construction. | ||
2 | //! | ||
3 | //! Algorithm from <https://www.cs.drexel.edu/~jjohnson/2015-16/fall/CS270/Lectures/3/dnf.pdf>, | ||
4 | //! which would have been much easier to read if it used pattern matching. It's also missing the | ||
5 | //! entire "distribute ANDs over ORs" part, which is not trivial. Oh well. | ||
6 | //! | ||
7 | //! This is currently both messy and inefficient. Feel free to improve, there are unit tests. | ||
8 | |||
9 | use std::fmt; | ||
10 | |||
11 | use rustc_hash::FxHashSet; | ||
12 | |||
13 | use crate::{CfgAtom, CfgDiff, CfgExpr, CfgOptions, InactiveReason}; | ||
14 | |||
15 | /// A `#[cfg]` directive in Disjunctive Normal Form (DNF). | ||
16 | pub struct DnfExpr { | ||
17 | conjunctions: Vec<Conjunction>, | ||
18 | } | ||
19 | |||
20 | struct Conjunction { | ||
21 | literals: Vec<Literal>, | ||
22 | } | ||
23 | |||
24 | struct Literal { | ||
25 | negate: bool, | ||
26 | var: Option<CfgAtom>, // None = Invalid | ||
27 | } | ||
28 | |||
29 | impl DnfExpr { | ||
30 | pub fn new(expr: CfgExpr) -> Self { | ||
31 | let builder = Builder { expr: DnfExpr { conjunctions: Vec::new() } }; | ||
32 | |||
33 | builder.lower(expr.clone()) | ||
34 | } | ||
35 | |||
36 | /// Computes a list of present or absent atoms in `opts` that cause this expression to evaluate | ||
37 | /// to `false`. | ||
38 | /// | ||
39 | /// Note that flipping a subset of these atoms might be sufficient to make the whole expression | ||
40 | /// evaluate to `true`. For that, see `compute_enable_hints`. | ||
41 | /// | ||
42 | /// Returns `None` when `self` is already true, or contains errors. | ||
43 | pub fn why_inactive(&self, opts: &CfgOptions) -> Option<InactiveReason> { | ||
44 | let mut res = InactiveReason { enabled: Vec::new(), disabled: Vec::new() }; | ||
45 | |||
46 | for conj in &self.conjunctions { | ||
47 | let mut conj_is_true = true; | ||
48 | for lit in &conj.literals { | ||
49 | let atom = lit.var.as_ref()?; | ||
50 | let enabled = opts.enabled.contains(atom); | ||
51 | if lit.negate == enabled { | ||
52 | // Literal is false, but needs to be true for this conjunction. | ||
53 | conj_is_true = false; | ||
54 | |||
55 | if enabled { | ||
56 | res.enabled.push(atom.clone()); | ||
57 | } else { | ||
58 | res.disabled.push(atom.clone()); | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | if conj_is_true { | ||
64 | // This expression is not actually inactive. | ||
65 | return None; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | res.enabled.sort_unstable(); | ||
70 | res.enabled.dedup(); | ||
71 | res.disabled.sort_unstable(); | ||
72 | res.disabled.dedup(); | ||
73 | Some(res) | ||
74 | } | ||
75 | |||
76 | /// Returns `CfgDiff` objects that would enable this directive if applied to `opts`. | ||
77 | pub fn compute_enable_hints<'a>( | ||
78 | &'a self, | ||
79 | opts: &'a CfgOptions, | ||
80 | ) -> impl Iterator<Item = CfgDiff> + 'a { | ||
81 | // A cfg is enabled if any of `self.conjunctions` evaluate to `true`. | ||
82 | |||
83 | self.conjunctions.iter().filter_map(move |conj| { | ||
84 | let mut enable = FxHashSet::default(); | ||
85 | let mut disable = FxHashSet::default(); | ||
86 | for lit in &conj.literals { | ||
87 | let atom = lit.var.as_ref()?; | ||
88 | let enabled = opts.enabled.contains(atom); | ||
89 | if lit.negate && enabled { | ||
90 | disable.insert(atom.clone()); | ||
91 | } | ||
92 | if !lit.negate && !enabled { | ||
93 | enable.insert(atom.clone()); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | // Check that this actually makes `conj` true. | ||
98 | for lit in &conj.literals { | ||
99 | let atom = lit.var.as_ref()?; | ||
100 | let enabled = enable.contains(atom) | ||
101 | || (opts.enabled.contains(atom) && !disable.contains(atom)); | ||
102 | if enabled == lit.negate { | ||
103 | return None; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | if enable.is_empty() && disable.is_empty() { | ||
108 | return None; | ||
109 | } | ||
110 | |||
111 | let mut diff = CfgDiff { | ||
112 | enable: enable.into_iter().collect(), | ||
113 | disable: disable.into_iter().collect(), | ||
114 | }; | ||
115 | |||
116 | // Undo the FxHashMap randomization for consistent output. | ||
117 | diff.enable.sort_unstable(); | ||
118 | diff.disable.sort_unstable(); | ||
119 | |||
120 | Some(diff) | ||
121 | }) | ||
122 | } | ||
123 | } | ||
124 | |||
125 | impl fmt::Display for DnfExpr { | ||
126 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
127 | if self.conjunctions.len() != 1 { | ||
128 | write!(f, "any(")?; | ||
129 | } | ||
130 | for (i, conj) in self.conjunctions.iter().enumerate() { | ||
131 | if i != 0 { | ||
132 | f.write_str(", ")?; | ||
133 | } | ||
134 | |||
135 | write!(f, "{}", conj)?; | ||
136 | } | ||
137 | if self.conjunctions.len() != 1 { | ||
138 | write!(f, ")")?; | ||
139 | } | ||
140 | |||
141 | Ok(()) | ||
142 | } | ||
143 | } | ||
144 | |||
145 | impl Conjunction { | ||
146 | fn new(parts: Vec<CfgExpr>) -> Self { | ||
147 | let mut literals = Vec::new(); | ||
148 | for part in parts { | ||
149 | match part { | ||
150 | CfgExpr::Invalid | CfgExpr::Atom(_) | CfgExpr::Not(_) => { | ||
151 | literals.push(Literal::new(part)); | ||
152 | } | ||
153 | CfgExpr::All(conj) => { | ||
154 | // Flatten. | ||
155 | literals.extend(Conjunction::new(conj).literals); | ||
156 | } | ||
157 | CfgExpr::Any(_) => unreachable!("disjunction in conjunction"), | ||
158 | } | ||
159 | } | ||
160 | |||
161 | Self { literals } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | impl fmt::Display for Conjunction { | ||
166 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
167 | if self.literals.len() != 1 { | ||
168 | write!(f, "all(")?; | ||
169 | } | ||
170 | for (i, lit) in self.literals.iter().enumerate() { | ||
171 | if i != 0 { | ||
172 | f.write_str(", ")?; | ||
173 | } | ||
174 | |||
175 | write!(f, "{}", lit)?; | ||
176 | } | ||
177 | if self.literals.len() != 1 { | ||
178 | write!(f, ")")?; | ||
179 | } | ||
180 | |||
181 | Ok(()) | ||
182 | } | ||
183 | } | ||
184 | |||
185 | impl Literal { | ||
186 | fn new(expr: CfgExpr) -> Self { | ||
187 | match expr { | ||
188 | CfgExpr::Invalid => Self { negate: false, var: None }, | ||
189 | CfgExpr::Atom(atom) => Self { negate: false, var: Some(atom) }, | ||
190 | CfgExpr::Not(expr) => match *expr { | ||
191 | CfgExpr::Invalid => Self { negate: true, var: None }, | ||
192 | CfgExpr::Atom(atom) => Self { negate: true, var: Some(atom) }, | ||
193 | _ => unreachable!("non-atom {:?}", expr), | ||
194 | }, | ||
195 | CfgExpr::Any(_) | CfgExpr::All(_) => unreachable!("non-literal {:?}", expr), | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | |||
200 | impl fmt::Display for Literal { | ||
201 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
202 | if self.negate { | ||
203 | write!(f, "not(")?; | ||
204 | } | ||
205 | |||
206 | match &self.var { | ||
207 | Some(var) => write!(f, "{}", var)?, | ||
208 | None => f.write_str("<invalid>")?, | ||
209 | } | ||
210 | |||
211 | if self.negate { | ||
212 | write!(f, ")")?; | ||
213 | } | ||
214 | |||
215 | Ok(()) | ||
216 | } | ||
217 | } | ||
218 | |||
219 | struct Builder { | ||
220 | expr: DnfExpr, | ||
221 | } | ||
222 | |||
223 | impl Builder { | ||
224 | fn lower(mut self, expr: CfgExpr) -> DnfExpr { | ||
225 | let expr = make_nnf(expr); | ||
226 | let expr = make_dnf(expr); | ||
227 | |||
228 | match expr { | ||
229 | CfgExpr::Invalid | CfgExpr::Atom(_) | CfgExpr::Not(_) => { | ||
230 | self.expr.conjunctions.push(Conjunction::new(vec![expr])); | ||
231 | } | ||
232 | CfgExpr::All(conj) => { | ||
233 | self.expr.conjunctions.push(Conjunction::new(conj)); | ||
234 | } | ||
235 | CfgExpr::Any(mut disj) => { | ||
236 | disj.reverse(); | ||
237 | while let Some(conj) = disj.pop() { | ||
238 | match conj { | ||
239 | CfgExpr::Invalid | CfgExpr::Atom(_) | CfgExpr::All(_) | CfgExpr::Not(_) => { | ||
240 | self.expr.conjunctions.push(Conjunction::new(vec![conj])); | ||
241 | } | ||
242 | CfgExpr::Any(inner_disj) => { | ||
243 | // Flatten. | ||
244 | disj.extend(inner_disj.into_iter().rev()); | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | } | ||
249 | } | ||
250 | |||
251 | self.expr | ||
252 | } | ||
253 | } | ||
254 | |||
255 | fn make_dnf(expr: CfgExpr) -> CfgExpr { | ||
256 | match expr { | ||
257 | CfgExpr::Invalid | CfgExpr::Atom(_) | CfgExpr::Not(_) => expr, | ||
258 | CfgExpr::Any(e) => CfgExpr::Any(e.into_iter().map(|expr| make_dnf(expr)).collect()), | ||
259 | CfgExpr::All(e) => { | ||
260 | let e = e.into_iter().map(|expr| make_nnf(expr)).collect::<Vec<_>>(); | ||
261 | |||
262 | CfgExpr::Any(distribute_conj(&e)) | ||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | /// Turns a conjunction of expressions into a disjunction of expressions. | ||
268 | fn distribute_conj(conj: &[CfgExpr]) -> Vec<CfgExpr> { | ||
269 | fn go(out: &mut Vec<CfgExpr>, with: &mut Vec<CfgExpr>, rest: &[CfgExpr]) { | ||
270 | match rest { | ||
271 | [head, tail @ ..] => match head { | ||
272 | CfgExpr::Any(disj) => { | ||
273 | for part in disj { | ||
274 | with.push(part.clone()); | ||
275 | go(out, with, tail); | ||
276 | with.pop(); | ||
277 | } | ||
278 | } | ||
279 | _ => { | ||
280 | with.push(head.clone()); | ||
281 | go(out, with, tail); | ||
282 | with.pop(); | ||
283 | } | ||
284 | }, | ||
285 | _ => { | ||
286 | // Turn accumulated parts into a new conjunction. | ||
287 | out.push(CfgExpr::All(with.clone())); | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | |||
292 | let mut out = Vec::new(); | ||
293 | let mut with = Vec::new(); | ||
294 | |||
295 | go(&mut out, &mut with, conj); | ||
296 | |||
297 | out | ||
298 | } | ||
299 | |||
300 | fn make_nnf(expr: CfgExpr) -> CfgExpr { | ||
301 | match expr { | ||
302 | CfgExpr::Invalid | CfgExpr::Atom(_) => expr, | ||
303 | CfgExpr::Any(expr) => CfgExpr::Any(expr.into_iter().map(|expr| make_nnf(expr)).collect()), | ||
304 | CfgExpr::All(expr) => CfgExpr::All(expr.into_iter().map(|expr| make_nnf(expr)).collect()), | ||
305 | CfgExpr::Not(operand) => match *operand { | ||
306 | CfgExpr::Invalid | CfgExpr::Atom(_) => CfgExpr::Not(operand.clone()), // Original negated expr | ||
307 | CfgExpr::Not(expr) => { | ||
308 | // Remove double negation. | ||
309 | make_nnf(*expr) | ||
310 | } | ||
311 | // Convert negated conjunction/disjunction using DeMorgan's Law. | ||
312 | CfgExpr::Any(inner) => CfgExpr::All( | ||
313 | inner.into_iter().map(|expr| make_nnf(CfgExpr::Not(Box::new(expr)))).collect(), | ||
314 | ), | ||
315 | CfgExpr::All(inner) => CfgExpr::Any( | ||
316 | inner.into_iter().map(|expr| make_nnf(CfgExpr::Not(Box::new(expr)))).collect(), | ||
317 | ), | ||
318 | }, | ||
319 | } | ||
320 | } | ||
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index a9d50e698..d0e08cf5f 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs | |||
@@ -1,11 +1,17 @@ | |||
1 | //! cfg defines conditional compiling options, `cfg` attibute parser and evaluator | 1 | //! cfg defines conditional compiling options, `cfg` attibute parser and evaluator |
2 | 2 | ||
3 | mod cfg_expr; | 3 | mod cfg_expr; |
4 | mod dnf; | ||
5 | #[cfg(test)] | ||
6 | mod tests; | ||
7 | |||
8 | use std::fmt; | ||
4 | 9 | ||
5 | use rustc_hash::FxHashSet; | 10 | use rustc_hash::FxHashSet; |
6 | use tt::SmolStr; | 11 | use tt::SmolStr; |
7 | 12 | ||
8 | pub use cfg_expr::CfgExpr; | 13 | pub use cfg_expr::{CfgAtom, CfgExpr}; |
14 | pub use dnf::DnfExpr; | ||
9 | 15 | ||
10 | /// Configuration options used for conditional compilition on items with `cfg` attributes. | 16 | /// Configuration options used for conditional compilition on items with `cfg` attributes. |
11 | /// We have two kind of options in different namespaces: atomic options like `unix`, and | 17 | /// We have two kind of options in different namespaces: atomic options like `unix`, and |
@@ -19,33 +25,131 @@ pub use cfg_expr::CfgExpr; | |||
19 | /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options | 25 | /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options |
20 | #[derive(Debug, Clone, PartialEq, Eq, Default)] | 26 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
21 | pub struct CfgOptions { | 27 | pub struct CfgOptions { |
22 | atoms: FxHashSet<SmolStr>, | 28 | enabled: FxHashSet<CfgAtom>, |
23 | key_values: FxHashSet<(SmolStr, SmolStr)>, | ||
24 | } | 29 | } |
25 | 30 | ||
26 | impl CfgOptions { | 31 | impl CfgOptions { |
27 | pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { | 32 | pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { |
28 | cfg.fold(&|key, value| match value { | 33 | cfg.fold(&|atom| self.enabled.contains(atom)) |
29 | None => self.atoms.contains(key), | ||
30 | Some(value) => self.key_values.contains(&(key.clone(), value.clone())), | ||
31 | }) | ||
32 | } | 34 | } |
33 | 35 | ||
34 | pub fn insert_atom(&mut self, key: SmolStr) { | 36 | pub fn insert_atom(&mut self, key: SmolStr) { |
35 | self.atoms.insert(key); | 37 | self.enabled.insert(CfgAtom::Flag(key)); |
36 | } | 38 | } |
37 | 39 | ||
38 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { | 40 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { |
39 | self.key_values.insert((key, value)); | 41 | self.enabled.insert(CfgAtom::KeyValue { key, value }); |
40 | } | 42 | } |
41 | 43 | ||
42 | pub fn append(&mut self, other: &CfgOptions) { | 44 | pub fn append(&mut self, other: &CfgOptions) { |
43 | for atom in &other.atoms { | 45 | for atom in &other.enabled { |
44 | self.atoms.insert(atom.clone()); | 46 | self.enabled.insert(atom.clone()); |
47 | } | ||
48 | } | ||
49 | |||
50 | pub fn apply_diff(&mut self, diff: CfgDiff) { | ||
51 | for atom in diff.enable { | ||
52 | self.enabled.insert(atom); | ||
45 | } | 53 | } |
46 | 54 | ||
47 | for (key, value) in &other.key_values { | 55 | for atom in diff.disable { |
48 | self.key_values.insert((key.clone(), value.clone())); | 56 | self.enabled.remove(&atom); |
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | pub struct CfgDiff { | ||
62 | // Invariants: No duplicates, no atom that's both in `enable` and `disable`. | ||
63 | enable: Vec<CfgAtom>, | ||
64 | disable: Vec<CfgAtom>, | ||
65 | } | ||
66 | |||
67 | impl CfgDiff { | ||
68 | /// Returns the total number of atoms changed by this diff. | ||
69 | pub fn len(&self) -> usize { | ||
70 | self.enable.len() + self.disable.len() | ||
71 | } | ||
72 | } | ||
73 | |||
74 | impl fmt::Display for CfgDiff { | ||
75 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
76 | if !self.enable.is_empty() { | ||
77 | f.write_str("enable ")?; | ||
78 | for (i, atom) in self.enable.iter().enumerate() { | ||
79 | let sep = match i { | ||
80 | 0 => "", | ||
81 | _ if i == self.enable.len() - 1 => " and ", | ||
82 | _ => ", ", | ||
83 | }; | ||
84 | f.write_str(sep)?; | ||
85 | |||
86 | write!(f, "{}", atom)?; | ||
87 | } | ||
88 | |||
89 | if !self.disable.is_empty() { | ||
90 | f.write_str("; ")?; | ||
91 | } | ||
49 | } | 92 | } |
93 | |||
94 | if !self.disable.is_empty() { | ||
95 | f.write_str("disable ")?; | ||
96 | for (i, atom) in self.disable.iter().enumerate() { | ||
97 | let sep = match i { | ||
98 | 0 => "", | ||
99 | _ if i == self.enable.len() - 1 => " and ", | ||
100 | _ => ", ", | ||
101 | }; | ||
102 | f.write_str(sep)?; | ||
103 | |||
104 | write!(f, "{}", atom)?; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | Ok(()) | ||
109 | } | ||
110 | } | ||
111 | |||
112 | pub struct InactiveReason { | ||
113 | enabled: Vec<CfgAtom>, | ||
114 | disabled: Vec<CfgAtom>, | ||
115 | } | ||
116 | |||
117 | impl fmt::Display for InactiveReason { | ||
118 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
119 | if !self.enabled.is_empty() { | ||
120 | for (i, atom) in self.enabled.iter().enumerate() { | ||
121 | let sep = match i { | ||
122 | 0 => "", | ||
123 | _ if i == self.enabled.len() - 1 => " and ", | ||
124 | _ => ", ", | ||
125 | }; | ||
126 | f.write_str(sep)?; | ||
127 | |||
128 | write!(f, "{}", atom)?; | ||
129 | } | ||
130 | let is_are = if self.enabled.len() == 1 { "is" } else { "are" }; | ||
131 | write!(f, " {} enabled", is_are)?; | ||
132 | |||
133 | if !self.disabled.is_empty() { | ||
134 | f.write_str(" and ")?; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | if !self.disabled.is_empty() { | ||
139 | for (i, atom) in self.disabled.iter().enumerate() { | ||
140 | let sep = match i { | ||
141 | 0 => "", | ||
142 | _ if i == self.disabled.len() - 1 => " and ", | ||
143 | _ => ", ", | ||
144 | }; | ||
145 | f.write_str(sep)?; | ||
146 | |||
147 | write!(f, "{}", atom)?; | ||
148 | } | ||
149 | let is_are = if self.disabled.len() == 1 { "is" } else { "are" }; | ||
150 | write!(f, " {} disabled", is_are)?; | ||
151 | } | ||
152 | |||
153 | Ok(()) | ||
50 | } | 154 | } |
51 | } | 155 | } |
diff --git a/crates/cfg/src/tests.rs b/crates/cfg/src/tests.rs new file mode 100644 index 000000000..bd0f9ec48 --- /dev/null +++ b/crates/cfg/src/tests.rs | |||
@@ -0,0 +1,193 @@ | |||
1 | use expect_test::{expect, Expect}; | ||
2 | use mbe::ast_to_token_tree; | ||
3 | use syntax::{ast, AstNode}; | ||
4 | |||
5 | use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr}; | ||
6 | |||
7 | fn assert_parse_result(input: &str, expected: CfgExpr) { | ||
8 | let (tt, _) = { | ||
9 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
10 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
11 | ast_to_token_tree(&tt).unwrap() | ||
12 | }; | ||
13 | let cfg = CfgExpr::parse(&tt); | ||
14 | assert_eq!(cfg, expected); | ||
15 | } | ||
16 | |||
17 | fn check_dnf(input: &str, expect: Expect) { | ||
18 | let (tt, _) = { | ||
19 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
20 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
21 | ast_to_token_tree(&tt).unwrap() | ||
22 | }; | ||
23 | let cfg = CfgExpr::parse(&tt); | ||
24 | let actual = format!("#![cfg({})]", DnfExpr::new(cfg)); | ||
25 | expect.assert_eq(&actual); | ||
26 | } | ||
27 | |||
28 | fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) { | ||
29 | let (tt, _) = { | ||
30 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
31 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
32 | ast_to_token_tree(&tt).unwrap() | ||
33 | }; | ||
34 | let cfg = CfgExpr::parse(&tt); | ||
35 | let dnf = DnfExpr::new(cfg); | ||
36 | let why_inactive = dnf.why_inactive(opts).unwrap().to_string(); | ||
37 | expect.assert_eq(&why_inactive); | ||
38 | } | ||
39 | |||
40 | #[track_caller] | ||
41 | fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) { | ||
42 | let (tt, _) = { | ||
43 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
44 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
45 | ast_to_token_tree(&tt).unwrap() | ||
46 | }; | ||
47 | let cfg = CfgExpr::parse(&tt); | ||
48 | let dnf = DnfExpr::new(cfg); | ||
49 | let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>(); | ||
50 | assert_eq!(hints, expected_hints); | ||
51 | } | ||
52 | |||
53 | #[test] | ||
54 | fn test_cfg_expr_parser() { | ||
55 | assert_parse_result("#![cfg(foo)]", CfgAtom::Flag("foo".into()).into()); | ||
56 | assert_parse_result("#![cfg(foo,)]", CfgAtom::Flag("foo".into()).into()); | ||
57 | assert_parse_result( | ||
58 | "#![cfg(not(foo))]", | ||
59 | CfgExpr::Not(Box::new(CfgAtom::Flag("foo".into()).into())), | ||
60 | ); | ||
61 | assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); | ||
62 | |||
63 | // Only take the first | ||
64 | assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgAtom::Flag("foo".into()).into()); | ||
65 | |||
66 | assert_parse_result( | ||
67 | r#"#![cfg(all(foo, bar = "baz"))]"#, | ||
68 | CfgExpr::All(vec![ | ||
69 | CfgAtom::Flag("foo".into()).into(), | ||
70 | CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), | ||
71 | ]), | ||
72 | ); | ||
73 | |||
74 | assert_parse_result( | ||
75 | r#"#![cfg(any(not(), all(), , bar = "baz",))]"#, | ||
76 | CfgExpr::Any(vec![ | ||
77 | CfgExpr::Not(Box::new(CfgExpr::Invalid)), | ||
78 | CfgExpr::All(vec![]), | ||
79 | CfgExpr::Invalid, | ||
80 | CfgAtom::KeyValue { key: "bar".into(), value: "baz".into() }.into(), | ||
81 | ]), | ||
82 | ); | ||
83 | } | ||
84 | |||
85 | #[test] | ||
86 | fn smoke() { | ||
87 | check_dnf("#![cfg(test)]", expect![[r#"#![cfg(test)]"#]]); | ||
88 | check_dnf("#![cfg(not(test))]", expect![[r#"#![cfg(not(test))]"#]]); | ||
89 | check_dnf("#![cfg(not(not(test)))]", expect![[r#"#![cfg(test)]"#]]); | ||
90 | |||
91 | check_dnf("#![cfg(all(a, b))]", expect![[r#"#![cfg(all(a, b))]"#]]); | ||
92 | check_dnf("#![cfg(any(a, b))]", expect![[r#"#![cfg(any(a, b))]"#]]); | ||
93 | |||
94 | check_dnf("#![cfg(not(a))]", expect![[r#"#![cfg(not(a))]"#]]); | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn distribute() { | ||
99 | check_dnf("#![cfg(all(any(a, b), c))]", expect![[r#"#![cfg(any(all(a, c), all(b, c)))]"#]]); | ||
100 | check_dnf("#![cfg(all(c, any(a, b)))]", expect![[r#"#![cfg(any(all(c, a), all(c, b)))]"#]]); | ||
101 | check_dnf( | ||
102 | "#![cfg(all(any(a, b), any(c, d)))]", | ||
103 | expect![[r#"#![cfg(any(all(a, c), all(a, d), all(b, c), all(b, d)))]"#]], | ||
104 | ); | ||
105 | |||
106 | check_dnf( | ||
107 | "#![cfg(all(any(a, b, c), any(d, e, f), g))]", | ||
108 | expect![[ | ||
109 | r#"#![cfg(any(all(a, d, g), all(a, e, g), all(a, f, g), all(b, d, g), all(b, e, g), all(b, f, g), all(c, d, g), all(c, e, g), all(c, f, g)))]"# | ||
110 | ]], | ||
111 | ); | ||
112 | } | ||
113 | |||
114 | #[test] | ||
115 | fn demorgan() { | ||
116 | check_dnf("#![cfg(not(all(a, b)))]", expect![[r#"#![cfg(any(not(a), not(b)))]"#]]); | ||
117 | check_dnf("#![cfg(not(any(a, b)))]", expect![[r#"#![cfg(all(not(a), not(b)))]"#]]); | ||
118 | |||
119 | check_dnf("#![cfg(not(all(not(a), b)))]", expect![[r#"#![cfg(any(a, not(b)))]"#]]); | ||
120 | check_dnf("#![cfg(not(any(a, not(b))))]", expect![[r#"#![cfg(all(not(a), b))]"#]]); | ||
121 | } | ||
122 | |||
123 | #[test] | ||
124 | fn nested() { | ||
125 | check_dnf("#![cfg(all(any(a), not(all(any(b)))))]", expect![[r#"#![cfg(all(a, not(b)))]"#]]); | ||
126 | |||
127 | check_dnf("#![cfg(any(any(a, b)))]", expect![[r#"#![cfg(any(a, b))]"#]]); | ||
128 | check_dnf("#![cfg(not(any(any(a, b))))]", expect![[r#"#![cfg(all(not(a), not(b)))]"#]]); | ||
129 | check_dnf("#![cfg(all(all(a, b)))]", expect![[r#"#![cfg(all(a, b))]"#]]); | ||
130 | check_dnf("#![cfg(not(all(all(a, b))))]", expect![[r#"#![cfg(any(not(a), not(b)))]"#]]); | ||
131 | } | ||
132 | |||
133 | #[test] | ||
134 | fn hints() { | ||
135 | let mut opts = CfgOptions::default(); | ||
136 | |||
137 | check_enable_hints("#![cfg(test)]", &opts, &["enable test"]); | ||
138 | check_enable_hints("#![cfg(not(test))]", &opts, &[]); | ||
139 | |||
140 | check_enable_hints("#![cfg(any(a, b))]", &opts, &["enable a", "enable b"]); | ||
141 | check_enable_hints("#![cfg(any(b, a))]", &opts, &["enable b", "enable a"]); | ||
142 | |||
143 | check_enable_hints("#![cfg(all(a, b))]", &opts, &["enable a and b"]); | ||
144 | |||
145 | opts.insert_atom("test".into()); | ||
146 | |||
147 | check_enable_hints("#![cfg(test)]", &opts, &[]); | ||
148 | check_enable_hints("#![cfg(not(test))]", &opts, &["disable test"]); | ||
149 | } | ||
150 | |||
151 | /// Tests that we don't suggest hints for cfgs that express an inconsistent formula. | ||
152 | #[test] | ||
153 | fn hints_impossible() { | ||
154 | let mut opts = CfgOptions::default(); | ||
155 | |||
156 | check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]); | ||
157 | |||
158 | opts.insert_atom("test".into()); | ||
159 | |||
160 | check_enable_hints("#![cfg(all(test, not(test)))]", &opts, &[]); | ||
161 | } | ||
162 | |||
163 | #[test] | ||
164 | fn why_inactive() { | ||
165 | let mut opts = CfgOptions::default(); | ||
166 | opts.insert_atom("test".into()); | ||
167 | opts.insert_atom("test2".into()); | ||
168 | |||
169 | check_why_inactive("#![cfg(a)]", &opts, expect![["a is disabled"]]); | ||
170 | check_why_inactive("#![cfg(not(test))]", &opts, expect![["test is enabled"]]); | ||
171 | |||
172 | check_why_inactive( | ||
173 | "#![cfg(all(not(test), not(test2)))]", | ||
174 | &opts, | ||
175 | expect![["test and test2 are enabled"]], | ||
176 | ); | ||
177 | check_why_inactive("#![cfg(all(a, b))]", &opts, expect![["a and b are disabled"]]); | ||
178 | check_why_inactive( | ||
179 | "#![cfg(all(not(test), a))]", | ||
180 | &opts, | ||
181 | expect![["test is enabled and a is disabled"]], | ||
182 | ); | ||
183 | check_why_inactive( | ||
184 | "#![cfg(all(not(test), test2, a))]", | ||
185 | &opts, | ||
186 | expect![["test is enabled and a is disabled"]], | ||
187 | ); | ||
188 | check_why_inactive( | ||
189 | "#![cfg(all(not(test), not(test2), a))]", | ||
190 | &opts, | ||
191 | expect![["test and test2 are enabled and a is disabled"]], | ||
192 | ); | ||
193 | } | ||
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index dea552a60..b2ce7ca3c 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs | |||
@@ -125,12 +125,20 @@ impl Attrs { | |||
125 | AttrQuery { attrs: self, key } | 125 | AttrQuery { attrs: self, key } |
126 | } | 126 | } |
127 | 127 | ||
128 | pub fn cfg(&self) -> impl Iterator<Item = CfgExpr> + '_ { | 128 | pub fn cfg(&self) -> Option<CfgExpr> { |
129 | // FIXME: handle cfg_attr :-) | 129 | // FIXME: handle cfg_attr :-) |
130 | self.by_key("cfg").tt_values().map(CfgExpr::parse) | 130 | let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse).collect::<Vec<_>>(); |
131 | match cfgs.len() { | ||
132 | 0 => None, | ||
133 | 1 => Some(cfgs.pop().unwrap()), | ||
134 | _ => Some(CfgExpr::All(cfgs)), | ||
135 | } | ||
131 | } | 136 | } |
132 | pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { | 137 | pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool { |
133 | self.cfg().all(|cfg| cfg_options.check(&cfg) != Some(false)) | 138 | match self.cfg() { |
139 | None => true, | ||
140 | Some(cfg) => cfg_options.check(&cfg) != Some(false), | ||
141 | } | ||
134 | } | 142 | } |
135 | } | 143 | } |
136 | 144 | ||
diff --git a/crates/hir_def/src/diagnostics.rs b/crates/hir_def/src/diagnostics.rs index c9c08b01f..532496b62 100644 --- a/crates/hir_def/src/diagnostics.rs +++ b/crates/hir_def/src/diagnostics.rs | |||
@@ -1,11 +1,12 @@ | |||
1 | //! Diagnostics produced by `hir_def`. | 1 | //! Diagnostics produced by `hir_def`. |
2 | 2 | ||
3 | use std::any::Any; | 3 | use std::any::Any; |
4 | use stdx::format_to; | ||
4 | 5 | ||
6 | use cfg::{CfgExpr, CfgOptions, DnfExpr}; | ||
5 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; | 7 | use hir_expand::diagnostics::{Diagnostic, DiagnosticCode}; |
6 | use syntax::{ast, AstPtr, SyntaxNodePtr}; | ||
7 | |||
8 | use hir_expand::{HirFileId, InFile}; | 8 | use hir_expand::{HirFileId, InFile}; |
9 | use syntax::{ast, AstPtr, SyntaxNodePtr}; | ||
9 | 10 | ||
10 | // Diagnostic: unresolved-module | 11 | // Diagnostic: unresolved-module |
11 | // | 12 | // |
@@ -94,6 +95,8 @@ impl Diagnostic for UnresolvedImport { | |||
94 | pub struct InactiveCode { | 95 | pub struct InactiveCode { |
95 | pub file: HirFileId, | 96 | pub file: HirFileId, |
96 | pub node: SyntaxNodePtr, | 97 | pub node: SyntaxNodePtr, |
98 | pub cfg: CfgExpr, | ||
99 | pub opts: CfgOptions, | ||
97 | } | 100 | } |
98 | 101 | ||
99 | impl Diagnostic for InactiveCode { | 102 | impl Diagnostic for InactiveCode { |
@@ -101,8 +104,14 @@ impl Diagnostic for InactiveCode { | |||
101 | DiagnosticCode("inactive-code") | 104 | DiagnosticCode("inactive-code") |
102 | } | 105 | } |
103 | fn message(&self) -> String { | 106 | fn message(&self) -> String { |
104 | // FIXME: say *why* it is configured out | 107 | let inactive = DnfExpr::new(self.cfg.clone()).why_inactive(&self.opts); |
105 | "code is inactive due to #[cfg] directives".to_string() | 108 | let mut buf = "code is inactive due to #[cfg] directives".to_string(); |
109 | |||
110 | if let Some(inactive) = inactive { | ||
111 | format_to!(buf, ": {}", inactive); | ||
112 | } | ||
113 | |||
114 | buf | ||
106 | } | 115 | } |
107 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | 116 | fn display_source(&self) -> InFile<SyntaxNodePtr> { |
108 | InFile::new(self.file, self.node.clone()) | 117 | InFile::new(self.file, self.node.clone()) |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 01a28aeeb..eb41d324e 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -283,6 +283,7 @@ pub enum ModuleSource { | |||
283 | } | 283 | } |
284 | 284 | ||
285 | mod diagnostics { | 285 | mod diagnostics { |
286 | use cfg::{CfgExpr, CfgOptions}; | ||
286 | use hir_expand::diagnostics::DiagnosticSink; | 287 | use hir_expand::diagnostics::DiagnosticSink; |
287 | use hir_expand::hygiene::Hygiene; | 288 | use hir_expand::hygiene::Hygiene; |
288 | use hir_expand::InFile; | 289 | use hir_expand::InFile; |
@@ -299,7 +300,7 @@ mod diagnostics { | |||
299 | 300 | ||
300 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, | 301 | UnresolvedImport { ast: AstId<ast::Use>, index: usize }, |
301 | 302 | ||
302 | UnconfiguredCode { ast: InFile<SyntaxNodePtr> }, | 303 | UnconfiguredCode { ast: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions }, |
303 | } | 304 | } |
304 | 305 | ||
305 | #[derive(Debug, PartialEq, Eq)] | 306 | #[derive(Debug, PartialEq, Eq)] |
@@ -341,8 +342,10 @@ mod diagnostics { | |||
341 | pub(super) fn unconfigured_code( | 342 | pub(super) fn unconfigured_code( |
342 | container: LocalModuleId, | 343 | container: LocalModuleId, |
343 | ast: InFile<SyntaxNodePtr>, | 344 | ast: InFile<SyntaxNodePtr>, |
345 | cfg: CfgExpr, | ||
346 | opts: CfgOptions, | ||
344 | ) -> Self { | 347 | ) -> Self { |
345 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast } } | 348 | Self { in_module: container, kind: DiagnosticKind::UnconfiguredCode { ast, cfg, opts } } |
346 | } | 349 | } |
347 | 350 | ||
348 | pub(super) fn add_to( | 351 | pub(super) fn add_to( |
@@ -395,8 +398,13 @@ mod diagnostics { | |||
395 | } | 398 | } |
396 | } | 399 | } |
397 | 400 | ||
398 | DiagnosticKind::UnconfiguredCode { ast } => { | 401 | DiagnosticKind::UnconfiguredCode { ast, cfg, opts } => { |
399 | sink.push(InactiveCode { file: ast.file_id, node: ast.value.clone() }); | 402 | sink.push(InactiveCode { |
403 | file: ast.file_id, | ||
404 | node: ast.value.clone(), | ||
405 | cfg: cfg.clone(), | ||
406 | opts: opts.clone(), | ||
407 | }); | ||
400 | } | 408 | } |
401 | } | 409 | } |
402 | } | 410 | } |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index bff8edb62..f30172d90 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | use std::iter; | 6 | use std::iter; |
7 | 7 | ||
8 | use base_db::{CrateId, FileId, ProcMacroId}; | 8 | use base_db::{CrateId, FileId, ProcMacroId}; |
9 | use cfg::CfgOptions; | 9 | use cfg::{CfgExpr, CfgOptions}; |
10 | use hir_expand::InFile; | 10 | use hir_expand::InFile; |
11 | use hir_expand::{ | 11 | use hir_expand::{ |
12 | ast_id_map::FileAstId, | 12 | ast_id_map::FileAstId, |
@@ -900,7 +900,8 @@ impl ModCollector<'_, '_> { | |||
900 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting | 900 | // `#[macro_use] extern crate` is hoisted to imports macros before collecting |
901 | // any other items. | 901 | // any other items. |
902 | for item in items { | 902 | for item in items { |
903 | if self.is_cfg_enabled(self.item_tree.attrs((*item).into())) { | 903 | let attrs = self.item_tree.attrs((*item).into()); |
904 | if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { | ||
904 | if let ModItem::ExternCrate(id) = item { | 905 | if let ModItem::ExternCrate(id) = item { |
905 | let import = self.item_tree[*id].clone(); | 906 | let import = self.item_tree[*id].clone(); |
906 | if import.is_macro_use { | 907 | if import.is_macro_use { |
@@ -912,9 +913,11 @@ impl ModCollector<'_, '_> { | |||
912 | 913 | ||
913 | for &item in items { | 914 | for &item in items { |
914 | let attrs = self.item_tree.attrs(item.into()); | 915 | let attrs = self.item_tree.attrs(item.into()); |
915 | if !self.is_cfg_enabled(attrs) { | 916 | if let Some(cfg) = attrs.cfg() { |
916 | self.emit_unconfigured_diagnostic(item); | 917 | if !self.is_cfg_enabled(&cfg) { |
917 | continue; | 918 | self.emit_unconfigured_diagnostic(item, &cfg); |
919 | continue; | ||
920 | } | ||
918 | } | 921 | } |
919 | let module = | 922 | let module = |
920 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; | 923 | ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; |
@@ -1321,20 +1324,22 @@ impl ModCollector<'_, '_> { | |||
1321 | } | 1324 | } |
1322 | } | 1325 | } |
1323 | 1326 | ||
1324 | fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { | 1327 | fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool { |
1325 | attrs.is_cfg_enabled(self.def_collector.cfg_options) | 1328 | self.def_collector.cfg_options.check(cfg) != Some(false) |
1326 | } | 1329 | } |
1327 | 1330 | ||
1328 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem) { | 1331 | fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) { |
1329 | let ast_id = item.ast_id(self.item_tree); | 1332 | let ast_id = item.ast_id(self.item_tree); |
1330 | let id_map = self.def_collector.db.ast_id_map(self.file_id); | 1333 | let id_map = self.def_collector.db.ast_id_map(self.file_id); |
1331 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); | 1334 | let syntax_ptr = id_map.get(ast_id).syntax_node_ptr(); |
1332 | 1335 | ||
1333 | let ast_node = InFile::new(self.file_id, syntax_ptr); | 1336 | let ast_node = InFile::new(self.file_id, syntax_ptr); |
1334 | self.def_collector | 1337 | self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code( |
1335 | .def_map | 1338 | self.module_id, |
1336 | .diagnostics | 1339 | ast_node, |
1337 | .push(DefDiagnostic::unconfigured_code(self.module_id, ast_node)); | 1340 | cfg.clone(), |
1341 | self.def_collector.cfg_options.clone(), | ||
1342 | )); | ||
1338 | } | 1343 | } |
1339 | } | 1344 | } |
1340 | 1345 | ||
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 576b813d2..5972248de 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -129,3 +129,25 @@ fn unresolved_module() { | |||
129 | ", | 129 | ", |
130 | ); | 130 | ); |
131 | } | 131 | } |
132 | |||
133 | #[test] | ||
134 | fn inactive_item() { | ||
135 | // Additional tests in `cfg` crate. This only tests disabled cfgs. | ||
136 | |||
137 | check_diagnostics( | ||
138 | r#" | ||
139 | //- /lib.rs | ||
140 | #[cfg(no)] pub fn f() {} | ||
141 | //^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no is disabled | ||
142 | |||
143 | #[cfg(no)] #[cfg(no2)] mod m; | ||
144 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: no and no2 are disabled | ||
145 | |||
146 | #[cfg(all(not(a), b))] enum E {} | ||
147 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: b is disabled | ||
148 | |||
149 | #[cfg(feature = "std")] use std; | ||
150 | //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ code is inactive due to #[cfg] directives: feature = "std" is disabled | ||
151 | "#, | ||
152 | ); | ||
153 | } | ||
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 6466422c5..0332c7be0 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -2128,7 +2128,7 @@ fn foo_<|>test() {} | |||
2128 | ignore: false, | 2128 | ignore: false, |
2129 | }, | 2129 | }, |
2130 | }, | 2130 | }, |
2131 | cfg_exprs: [], | 2131 | cfg: None, |
2132 | }, | 2132 | }, |
2133 | ), | 2133 | ), |
2134 | ] | 2134 | ] |
@@ -2166,7 +2166,7 @@ mod tests<|> { | |||
2166 | kind: TestMod { | 2166 | kind: TestMod { |
2167 | path: "tests", | 2167 | path: "tests", |
2168 | }, | 2168 | }, |
2169 | cfg_exprs: [], | 2169 | cfg: None, |
2170 | }, | 2170 | }, |
2171 | ), | 2171 | ), |
2172 | ] | 2172 | ] |
diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 752ef2f21..eb82456ad 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs | |||
@@ -15,7 +15,7 @@ use crate::{display::ToNav, FileId, NavigationTarget}; | |||
15 | pub struct Runnable { | 15 | pub struct Runnable { |
16 | pub nav: NavigationTarget, | 16 | pub nav: NavigationTarget, |
17 | pub kind: RunnableKind, | 17 | pub kind: RunnableKind, |
18 | pub cfg_exprs: Vec<CfgExpr>, | 18 | pub cfg: Option<CfgExpr>, |
19 | } | 19 | } |
20 | 20 | ||
21 | #[derive(Debug, Clone)] | 21 | #[derive(Debug, Clone)] |
@@ -168,7 +168,7 @@ fn runnable_fn( | |||
168 | }; | 168 | }; |
169 | 169 | ||
170 | let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def)); | 170 | let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def)); |
171 | let cfg_exprs = attrs.cfg().collect(); | 171 | let cfg = attrs.cfg(); |
172 | 172 | ||
173 | let nav = if let RunnableKind::DocTest { .. } = kind { | 173 | let nav = if let RunnableKind::DocTest { .. } = kind { |
174 | NavigationTarget::from_doc_commented( | 174 | NavigationTarget::from_doc_commented( |
@@ -179,7 +179,7 @@ fn runnable_fn( | |||
179 | } else { | 179 | } else { |
180 | NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def)) | 180 | NavigationTarget::from_named(sema.db, InFile::new(file_id.into(), &fn_def)) |
181 | }; | 181 | }; |
182 | Some(Runnable { nav, kind, cfg_exprs }) | 182 | Some(Runnable { nav, kind, cfg }) |
183 | } | 183 | } |
184 | 184 | ||
185 | #[derive(Debug, Copy, Clone)] | 185 | #[derive(Debug, Copy, Clone)] |
@@ -255,9 +255,9 @@ fn runnable_mod( | |||
255 | .join("::"); | 255 | .join("::"); |
256 | 256 | ||
257 | let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &module)); | 257 | let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &module)); |
258 | let cfg_exprs = attrs.cfg().collect(); | 258 | let cfg = attrs.cfg(); |
259 | let nav = module_def.to_nav(sema.db); | 259 | let nav = module_def.to_nav(sema.db); |
260 | Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg_exprs }) | 260 | Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) |
261 | } | 261 | } |
262 | 262 | ||
263 | // We could create runnables for modules with number_of_test_submodules > 0, | 263 | // We could create runnables for modules with number_of_test_submodules > 0, |
@@ -348,7 +348,7 @@ fn bench() {} | |||
348 | docs: None, | 348 | docs: None, |
349 | }, | 349 | }, |
350 | kind: Bin, | 350 | kind: Bin, |
351 | cfg_exprs: [], | 351 | cfg: None, |
352 | }, | 352 | }, |
353 | Runnable { | 353 | Runnable { |
354 | nav: NavigationTarget { | 354 | nav: NavigationTarget { |
@@ -373,7 +373,7 @@ fn bench() {} | |||
373 | ignore: false, | 373 | ignore: false, |
374 | }, | 374 | }, |
375 | }, | 375 | }, |
376 | cfg_exprs: [], | 376 | cfg: None, |
377 | }, | 377 | }, |
378 | Runnable { | 378 | Runnable { |
379 | nav: NavigationTarget { | 379 | nav: NavigationTarget { |
@@ -398,7 +398,7 @@ fn bench() {} | |||
398 | ignore: true, | 398 | ignore: true, |
399 | }, | 399 | }, |
400 | }, | 400 | }, |
401 | cfg_exprs: [], | 401 | cfg: None, |
402 | }, | 402 | }, |
403 | Runnable { | 403 | Runnable { |
404 | nav: NavigationTarget { | 404 | nav: NavigationTarget { |
@@ -420,7 +420,7 @@ fn bench() {} | |||
420 | "bench", | 420 | "bench", |
421 | ), | 421 | ), |
422 | }, | 422 | }, |
423 | cfg_exprs: [], | 423 | cfg: None, |
424 | }, | 424 | }, |
425 | ] | 425 | ] |
426 | "#]], | 426 | "#]], |
@@ -507,7 +507,7 @@ fn should_have_no_runnable_6() {} | |||
507 | docs: None, | 507 | docs: None, |
508 | }, | 508 | }, |
509 | kind: Bin, | 509 | kind: Bin, |
510 | cfg_exprs: [], | 510 | cfg: None, |
511 | }, | 511 | }, |
512 | Runnable { | 512 | Runnable { |
513 | nav: NavigationTarget { | 513 | nav: NavigationTarget { |
@@ -527,7 +527,7 @@ fn should_have_no_runnable_6() {} | |||
527 | "should_have_runnable", | 527 | "should_have_runnable", |
528 | ), | 528 | ), |
529 | }, | 529 | }, |
530 | cfg_exprs: [], | 530 | cfg: None, |
531 | }, | 531 | }, |
532 | Runnable { | 532 | Runnable { |
533 | nav: NavigationTarget { | 533 | nav: NavigationTarget { |
@@ -547,7 +547,7 @@ fn should_have_no_runnable_6() {} | |||
547 | "should_have_runnable_1", | 547 | "should_have_runnable_1", |
548 | ), | 548 | ), |
549 | }, | 549 | }, |
550 | cfg_exprs: [], | 550 | cfg: None, |
551 | }, | 551 | }, |
552 | Runnable { | 552 | Runnable { |
553 | nav: NavigationTarget { | 553 | nav: NavigationTarget { |
@@ -567,7 +567,7 @@ fn should_have_no_runnable_6() {} | |||
567 | "should_have_runnable_2", | 567 | "should_have_runnable_2", |
568 | ), | 568 | ), |
569 | }, | 569 | }, |
570 | cfg_exprs: [], | 570 | cfg: None, |
571 | }, | 571 | }, |
572 | ] | 572 | ] |
573 | "#]], | 573 | "#]], |
@@ -609,7 +609,7 @@ impl Data { | |||
609 | docs: None, | 609 | docs: None, |
610 | }, | 610 | }, |
611 | kind: Bin, | 611 | kind: Bin, |
612 | cfg_exprs: [], | 612 | cfg: None, |
613 | }, | 613 | }, |
614 | Runnable { | 614 | Runnable { |
615 | nav: NavigationTarget { | 615 | nav: NavigationTarget { |
@@ -629,7 +629,7 @@ impl Data { | |||
629 | "Data::foo", | 629 | "Data::foo", |
630 | ), | 630 | ), |
631 | }, | 631 | }, |
632 | cfg_exprs: [], | 632 | cfg: None, |
633 | }, | 633 | }, |
634 | ] | 634 | ] |
635 | "#]], | 635 | "#]], |
@@ -668,7 +668,7 @@ mod test_mod { | |||
668 | kind: TestMod { | 668 | kind: TestMod { |
669 | path: "test_mod", | 669 | path: "test_mod", |
670 | }, | 670 | }, |
671 | cfg_exprs: [], | 671 | cfg: None, |
672 | }, | 672 | }, |
673 | Runnable { | 673 | Runnable { |
674 | nav: NavigationTarget { | 674 | nav: NavigationTarget { |
@@ -693,7 +693,7 @@ mod test_mod { | |||
693 | ignore: false, | 693 | ignore: false, |
694 | }, | 694 | }, |
695 | }, | 695 | }, |
696 | cfg_exprs: [], | 696 | cfg: None, |
697 | }, | 697 | }, |
698 | ] | 698 | ] |
699 | "#]], | 699 | "#]], |
@@ -748,7 +748,7 @@ mod root_tests { | |||
748 | kind: TestMod { | 748 | kind: TestMod { |
749 | path: "root_tests::nested_tests_0", | 749 | path: "root_tests::nested_tests_0", |
750 | }, | 750 | }, |
751 | cfg_exprs: [], | 751 | cfg: None, |
752 | }, | 752 | }, |
753 | Runnable { | 753 | Runnable { |
754 | nav: NavigationTarget { | 754 | nav: NavigationTarget { |
@@ -768,7 +768,7 @@ mod root_tests { | |||
768 | kind: TestMod { | 768 | kind: TestMod { |
769 | path: "root_tests::nested_tests_0::nested_tests_1", | 769 | path: "root_tests::nested_tests_0::nested_tests_1", |
770 | }, | 770 | }, |
771 | cfg_exprs: [], | 771 | cfg: None, |
772 | }, | 772 | }, |
773 | Runnable { | 773 | Runnable { |
774 | nav: NavigationTarget { | 774 | nav: NavigationTarget { |
@@ -793,7 +793,7 @@ mod root_tests { | |||
793 | ignore: false, | 793 | ignore: false, |
794 | }, | 794 | }, |
795 | }, | 795 | }, |
796 | cfg_exprs: [], | 796 | cfg: None, |
797 | }, | 797 | }, |
798 | Runnable { | 798 | Runnable { |
799 | nav: NavigationTarget { | 799 | nav: NavigationTarget { |
@@ -818,7 +818,7 @@ mod root_tests { | |||
818 | ignore: false, | 818 | ignore: false, |
819 | }, | 819 | }, |
820 | }, | 820 | }, |
821 | cfg_exprs: [], | 821 | cfg: None, |
822 | }, | 822 | }, |
823 | Runnable { | 823 | Runnable { |
824 | nav: NavigationTarget { | 824 | nav: NavigationTarget { |
@@ -838,7 +838,7 @@ mod root_tests { | |||
838 | kind: TestMod { | 838 | kind: TestMod { |
839 | path: "root_tests::nested_tests_0::nested_tests_2", | 839 | path: "root_tests::nested_tests_0::nested_tests_2", |
840 | }, | 840 | }, |
841 | cfg_exprs: [], | 841 | cfg: None, |
842 | }, | 842 | }, |
843 | Runnable { | 843 | Runnable { |
844 | nav: NavigationTarget { | 844 | nav: NavigationTarget { |
@@ -863,7 +863,7 @@ mod root_tests { | |||
863 | ignore: false, | 863 | ignore: false, |
864 | }, | 864 | }, |
865 | }, | 865 | }, |
866 | cfg_exprs: [], | 866 | cfg: None, |
867 | }, | 867 | }, |
868 | ] | 868 | ] |
869 | "#]], | 869 | "#]], |
@@ -906,12 +906,14 @@ fn test_foo1() {} | |||
906 | ignore: false, | 906 | ignore: false, |
907 | }, | 907 | }, |
908 | }, | 908 | }, |
909 | cfg_exprs: [ | 909 | cfg: Some( |
910 | KeyValue { | 910 | Atom( |
911 | key: "feature", | 911 | KeyValue { |
912 | value: "foo", | 912 | key: "feature", |
913 | }, | 913 | value: "foo", |
914 | ], | 914 | }, |
915 | ), | ||
916 | ), | ||
915 | }, | 917 | }, |
916 | ] | 918 | ] |
917 | "#]], | 919 | "#]], |
@@ -954,20 +956,24 @@ fn test_foo1() {} | |||
954 | ignore: false, | 956 | ignore: false, |
955 | }, | 957 | }, |
956 | }, | 958 | }, |
957 | cfg_exprs: [ | 959 | cfg: Some( |
958 | All( | 960 | All( |
959 | [ | 961 | [ |
960 | KeyValue { | 962 | Atom( |
961 | key: "feature", | 963 | KeyValue { |
962 | value: "foo", | 964 | key: "feature", |
963 | }, | 965 | value: "foo", |
964 | KeyValue { | 966 | }, |
965 | key: "feature", | 967 | ), |
966 | value: "bar", | 968 | Atom( |
967 | }, | 969 | KeyValue { |
970 | key: "feature", | ||
971 | value: "bar", | ||
972 | }, | ||
973 | ), | ||
968 | ], | 974 | ], |
969 | ), | 975 | ), |
970 | ], | 976 | ), |
971 | }, | 977 | }, |
972 | ] | 978 | ] |
973 | "#]], | 979 | "#]], |
diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs index ddc028148..1ab72bd91 100644 --- a/crates/rust-analyzer/src/cargo_target_spec.rs +++ b/crates/rust-analyzer/src/cargo_target_spec.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! See `CargoTargetSpec` | 1 | //! See `CargoTargetSpec` |
2 | 2 | ||
3 | use cfg::CfgExpr; | 3 | use cfg::{CfgAtom, CfgExpr}; |
4 | use ide::{FileId, RunnableKind, TestId}; | 4 | use ide::{FileId, RunnableKind, TestId}; |
5 | use project_model::{self, TargetKind}; | 5 | use project_model::{self, TargetKind}; |
6 | use vfs::AbsPathBuf; | 6 | use vfs::AbsPathBuf; |
@@ -24,7 +24,7 @@ impl CargoTargetSpec { | |||
24 | snap: &GlobalStateSnapshot, | 24 | snap: &GlobalStateSnapshot, |
25 | spec: Option<CargoTargetSpec>, | 25 | spec: Option<CargoTargetSpec>, |
26 | kind: &RunnableKind, | 26 | kind: &RunnableKind, |
27 | cfgs: &[CfgExpr], | 27 | cfg: &Option<CfgExpr>, |
28 | ) -> Result<(Vec<String>, Vec<String>)> { | 28 | ) -> Result<(Vec<String>, Vec<String>)> { |
29 | let mut args = Vec::new(); | 29 | let mut args = Vec::new(); |
30 | let mut extra_args = Vec::new(); | 30 | let mut extra_args = Vec::new(); |
@@ -87,7 +87,7 @@ impl CargoTargetSpec { | |||
87 | args.push("--all-features".to_string()); | 87 | args.push("--all-features".to_string()); |
88 | } else { | 88 | } else { |
89 | let mut features = Vec::new(); | 89 | let mut features = Vec::new(); |
90 | for cfg in cfgs { | 90 | if let Some(cfg) = cfg.as_ref() { |
91 | required_features(cfg, &mut features); | 91 | required_features(cfg, &mut features); |
92 | } | 92 | } |
93 | for feature in &snap.config.cargo.features { | 93 | for feature in &snap.config.cargo.features { |
@@ -160,7 +160,9 @@ impl CargoTargetSpec { | |||
160 | /// Fill minimal features needed | 160 | /// Fill minimal features needed |
161 | fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) { | 161 | fn required_features(cfg_expr: &CfgExpr, features: &mut Vec<String>) { |
162 | match cfg_expr { | 162 | match cfg_expr { |
163 | CfgExpr::KeyValue { key, value } if key == "feature" => features.push(value.to_string()), | 163 | CfgExpr::Atom(CfgAtom::KeyValue { key, value }) if key == "feature" => { |
164 | features.push(value.to_string()) | ||
165 | } | ||
164 | CfgExpr::All(preds) => { | 166 | CfgExpr::All(preds) => { |
165 | preds.iter().for_each(|cfg| required_features(cfg, features)); | 167 | preds.iter().for_each(|cfg| required_features(cfg, features)); |
166 | } | 168 | } |
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 24ad49206..d0fb92f89 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs | |||
@@ -762,7 +762,7 @@ pub(crate) fn runnable( | |||
762 | let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone()); | 762 | let workspace_root = spec.as_ref().map(|it| it.workspace_root.clone()); |
763 | let target = spec.as_ref().map(|s| s.target.clone()); | 763 | let target = spec.as_ref().map(|s| s.target.clone()); |
764 | let (cargo_args, executable_args) = | 764 | let (cargo_args, executable_args) = |
765 | CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg_exprs)?; | 765 | CargoTargetSpec::runnable_args(snap, spec, &runnable.kind, &runnable.cfg)?; |
766 | let label = runnable.label(target); | 766 | let label = runnable.label(target); |
767 | let location = location_link(snap, None, runnable.nav)?; | 767 | let location = location_link(snap, None, runnable.nav)?; |
768 | 768 | ||