diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-04-09 09:08:55 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-04-09 09:08:55 +0100 |
commit | 412eda73877c7a897561a70b83f55ee346e18a2c (patch) | |
tree | b30c91efe4050087f1935d477db4612357a2665a /crates/ra_hir_ty/src | |
parent | 01e5bd50f30ae22abab0733468d1e1c6dea7d506 (diff) | |
parent | 585bb83e2aec9c79dae8c2e031e9165f40937003 (diff) |
Merge #3880
3880: Add support for attributes for struct fields r=matklad a=bnjjj
Hello I try to solve this example:
```rust
struct MyStruct {
my_val: usize,
#[cfg(feature = "foo")]
bar: bool,
}
impl MyStruct {
#[cfg(feature = "foo")]
pub(crate) fn new(my_val: usize, bar: bool) -> Self {
Self { my_val, bar }
}
#[cfg(not(feature = "foo"))]
pub(crate) fn new(my_val: usize, _bar: bool) -> Self {
Self { my_val }
}
}
```
Here is a draft PR to try to solve this issue. In fact for now when i have this kind of example, rust-analyzer tells me that my second Self {} miss the bar field. Which is a bug.
I have some difficulties to add this features. Here in my draft I share my work about adding attributes support on struct field data. But I'm stuck when I have to fetch attributes from parent expressions. I don't really know how to do that. For the first iteration I just want to solve my issue without solving on all different expressions. And then after I will try to implement that on different kind of expression. I think I have to fetch my FunctionId and then I will be able to find attributes with myFunction.attrs() But I don't know if it's the right way.
@matklad (or anyone else) if you can help me it would be great :D
Co-authored-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src')
-rw-r--r-- | crates/ra_hir_ty/src/expr.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests.rs | 30 |
2 files changed, 31 insertions, 2 deletions
diff --git a/crates/ra_hir_ty/src/expr.rs b/crates/ra_hir_ty/src/expr.rs index b4592fbf5..e45e9ea14 100644 --- a/crates/ra_hir_ty/src/expr.rs +++ b/crates/ra_hir_ty/src/expr.rs | |||
@@ -4,8 +4,7 @@ use std::sync::Arc; | |||
4 | 4 | ||
5 | use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId}; | 5 | use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId}; |
6 | use hir_expand::diagnostics::DiagnosticSink; | 6 | use hir_expand::diagnostics::DiagnosticSink; |
7 | use ra_syntax::ast; | 7 | use ra_syntax::{ast, AstPtr}; |
8 | use ra_syntax::AstPtr; | ||
9 | use rustc_hash::FxHashSet; | 8 | use rustc_hash::FxHashSet; |
10 | 9 | ||
11 | use crate::{ | 10 | use crate::{ |
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index e4a103d1b..608408d88 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs | |||
@@ -319,3 +319,33 @@ fn no_such_field_diagnostics() { | |||
319 | "### | 319 | "### |
320 | ); | 320 | ); |
321 | } | 321 | } |
322 | |||
323 | #[test] | ||
324 | fn no_such_field_with_feature_flag_diagnostics() { | ||
325 | let diagnostics = TestDB::with_files( | ||
326 | r#" | ||
327 | //- /lib.rs crate:foo cfg:feature=foo | ||
328 | struct MyStruct { | ||
329 | my_val: usize, | ||
330 | #[cfg(feature = "foo")] | ||
331 | bar: bool, | ||
332 | } | ||
333 | |||
334 | impl MyStruct { | ||
335 | #[cfg(feature = "foo")] | ||
336 | pub(crate) fn new(my_val: usize, bar: bool) -> Self { | ||
337 | Self { my_val, bar } | ||
338 | } | ||
339 | |||
340 | #[cfg(not(feature = "foo"))] | ||
341 | pub(crate) fn new(my_val: usize, _bar: bool) -> Self { | ||
342 | Self { my_val } | ||
343 | } | ||
344 | } | ||
345 | "#, | ||
346 | ) | ||
347 | .diagnostics() | ||
348 | .0; | ||
349 | |||
350 | assert_snapshot!(diagnostics, @r###""###); | ||
351 | } | ||