Skip to content

Add support for attributes for struct fields #3880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ra_hir_def/src/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct StructFieldData {
impl StructData {
pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
let src = id.lookup(db).source(db);

let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
let variant_data = VariantData::new(db, src.map(|s| s.kind()));
let variant_data = Arc::new(variant_data);
Expand Down
31 changes: 26 additions & 5 deletions crates/ra_hir_def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
use std::sync::Arc;

use hir_expand::{
hygiene::Hygiene,
name::{name, AsName, Name},
AstId, InFile,
};
use ra_cfg::CfgOptions;
use ra_prof::profile;
use ra_syntax::ast::{
self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner,
};

use crate::{
attr::Attrs,
db::DefDatabase,
path::{path, GenericArgs, Path},
src::HasSource,
Expand All @@ -26,6 +29,7 @@ pub struct FunctionData {
pub name: Name,
pub params: Vec<TypeRef>,
pub ret_type: TypeRef,
pub attrs: Attrs,
/// True if the first param is `self`. This is relevant to decide whether this
/// can be called as a method.
pub has_self_param: bool,
Expand Down Expand Up @@ -63,6 +67,8 @@ impl FunctionData {
params.push(type_ref);
}
}
let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id));

let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
TypeRef::from_ast(type_ref)
} else {
Expand All @@ -81,7 +87,7 @@ impl FunctionData {
let visibility =
RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility()));

let sig = FunctionData { name, params, ret_type, has_self_param, visibility };
let sig = FunctionData { name, params, ret_type, has_self_param, visibility, attrs };
Arc::new(sig)
}
}
Expand Down Expand Up @@ -211,6 +217,7 @@ impl ImplData {
let module_id = impl_loc.container.module(db);

let mut items = Vec::new();

if let Some(item_list) = src.value.item_list() {
items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id));
items.extend(collect_impl_items_in_macros(
Expand Down Expand Up @@ -311,39 +318,53 @@ fn collect_impl_items_in_macro(
}
}

fn is_cfg_enabled(cfg_options: &CfgOptions, attrs: &Attrs) -> bool {
attrs.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false))
}

fn collect_impl_items(
db: &dyn DefDatabase,
impl_items: impl Iterator<Item = ImplItem>,
file_id: crate::HirFileId,
id: ImplId,
) -> Vec<AssocItemId> {
let items = db.ast_id_map(file_id);
let crate_graph = db.crate_graph();
let module_id = id.lookup(db).container.module(db);

impl_items
.map(|item_node| match item_node {
.filter_map(|item_node| match item_node {
ast::ImplItem::FnDef(it) => {
let def = FunctionLoc {
container: AssocContainerId::ImplId(id),
ast_id: AstId::new(file_id, items.ast_id(&it)),
}
.intern(db);
def.into()

if !is_cfg_enabled(
&crate_graph[module_id.krate].cfg_options,
&db.function_data(def).attrs,
) {
None
} else {
Some(def.into())
}
}
ast::ImplItem::ConstDef(it) => {
let def = ConstLoc {
container: AssocContainerId::ImplId(id),
ast_id: AstId::new(file_id, items.ast_id(&it)),
}
.intern(db);
def.into()
Some(def.into())
}
ast::ImplItem::TypeAliasDef(it) => {
let def = TypeAliasLoc {
container: AssocContainerId::ImplId(id),
ast_id: AstId::new(file_id, items.ast_id(&it)),
}
.intern(db);
def.into()
Some(def.into())
}
})
.collect()
Expand Down
3 changes: 1 addition & 2 deletions crates/ra_hir_ty/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::sync::Arc;

use hir_def::{path::path, resolver::HasResolver, AdtId, FunctionId};
use hir_expand::diagnostics::DiagnosticSink;
use ra_syntax::ast;
use ra_syntax::AstPtr;
use ra_syntax::{ast, AstPtr};
use rustc_hash::FxHashSet;

use crate::{
Expand Down
30 changes: 30 additions & 0 deletions crates/ra_hir_ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,33 @@ fn no_such_field_diagnostics() {
"###
);
}

#[test]
fn no_such_field_with_feature_flag_diagnostics() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let diagnostics = TestDB::with_files(
r#"
//- /lib.rs crate:foo cfg:feature=foo
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 }
}
}
"#,
)
.diagnostics()
.0;

assert_snapshot!(diagnostics, @r###""###);
}