Unify and test attribute handling

This commit is contained in:
Jonas Schievink 2020-06-23 18:20:51 +02:00
parent 689e147c9d
commit ae7a296c85
5 changed files with 227 additions and 33 deletions

View file

@ -107,6 +107,18 @@ impl Attrs {
Attrs { entries }
}
pub fn merge(&self, other: Attrs) -> Attrs {
match (&self.entries, &other.entries) {
(None, None) => Attrs { entries: None },
(Some(entries), None) | (None, Some(entries)) => {
Attrs { entries: Some(entries.clone()) }
}
(Some(a), Some(b)) => {
Attrs { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
}
}
}
pub fn by_key(&self, key: &'static str) -> AttrQuery<'_> {
AttrQuery { attrs: self, key }
}

View file

@ -40,7 +40,7 @@ impl FunctionData {
name: func.name.clone(),
params: func.params.clone(),
ret_type: func.ret_type.clone(),
attrs: func.attrs.clone(),
attrs: item_tree.attrs(loc.id.value.into()).clone(),
has_self_param: func.has_self_param,
is_unsafe: func.is_unsafe,
visibility: func.visibility.clone(),
@ -224,7 +224,8 @@ fn collect_items(
match item {
AssocItem::Function(id) => {
let item = &item_tree[id];
if !item.attrs.is_cfg_enabled(&cfg_options) {
let attrs = item_tree.attrs(id.into());
if !attrs.is_cfg_enabled(&cfg_options) {
continue;
}
let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);

View file

@ -355,7 +355,6 @@ pub struct ExternCrate {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Function {
pub name: Name,
pub attrs: Attrs,
pub visibility: RawVisibility,
pub generic_params: GenericParams,
pub has_self_param: bool,
@ -368,7 +367,6 @@ pub struct Function {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Struct {
pub name: Name,
pub attrs: Attrs,
pub visibility: RawVisibility,
pub generic_params: GenericParams,
pub fields: Fields,
@ -389,7 +387,6 @@ pub enum StructDefKind {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Union {
pub name: Name,
pub attrs: Attrs,
pub visibility: RawVisibility,
pub generic_params: GenericParams,
pub fields: Fields,
@ -399,7 +396,6 @@ pub struct Union {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Enum {
pub name: Name,
pub attrs: Attrs,
pub visibility: RawVisibility,
pub generic_params: GenericParams,
pub variants: Range<Idx<Variant>>,

View file

@ -12,7 +12,7 @@ use ra_syntax::{
SyntaxNode,
};
use smallvec::SmallVec;
use std::{mem, sync::Arc};
use std::{collections::hash_map::Entry, mem, sync::Arc};
fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
FileItemTreeId { index, _p: PhantomData }
@ -116,13 +116,24 @@ impl Ctx {
if !attrs.is_empty() {
for item in items.iter().flat_map(|items| &items.0) {
self.tree.attrs.insert(*item, attrs.clone());
self.add_attrs(*item, attrs.clone());
}
}
items
}
fn add_attrs(&mut self, item: ModItem, attrs: Attrs) {
match self.tree.attrs.entry(item) {
Entry::Occupied(mut entry) => {
*entry.get_mut() = entry.get().merge(attrs);
}
Entry::Vacant(entry) => {
entry.insert(attrs);
}
}
}
fn collect_inner_items(&mut self, container: &SyntaxNode) {
let forced_vis = self.forced_visibility.take();
let mut inner_items = mem::replace(&mut self.tree.inner_items, FxHashMap::default());
@ -147,7 +158,6 @@ impl Ctx {
}
fn lower_struct(&mut self, strukt: &ast::StructDef) -> Option<FileItemTreeId<Struct>> {
let attrs = self.lower_attrs(strukt);
let visibility = self.lower_visibility(strukt);
let name = strukt.name()?.as_name();
let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt);
@ -158,7 +168,7 @@ impl Ctx {
ast::StructKind::Tuple(_) => StructDefKind::Tuple,
ast::StructKind::Unit => StructDefKind::Unit,
};
let res = Struct { name, attrs, visibility, generic_params, fields, ast_id, kind };
let res = Struct { name, visibility, generic_params, fields, ast_id, kind };
Some(id(self.tree.structs.alloc(res)))
}
@ -215,7 +225,6 @@ impl Ctx {
}
fn lower_union(&mut self, union: &ast::UnionDef) -> Option<FileItemTreeId<Union>> {
let attrs = self.lower_attrs(union);
let visibility = self.lower_visibility(union);
let name = union.name()?.as_name();
let generic_params = self.lower_generic_params(GenericsOwner::Union, union);
@ -226,12 +235,11 @@ impl Ctx {
None => Fields::Record(self.next_field_idx()..self.next_field_idx()),
};
let ast_id = self.source_ast_id_map.ast_id(union);
let res = Union { name, attrs, visibility, generic_params, fields, ast_id };
let res = Union { name, visibility, generic_params, fields, ast_id };
Some(id(self.tree.unions.alloc(res)))
}
fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option<FileItemTreeId<Enum>> {
let attrs = self.lower_attrs(enum_);
let visibility = self.lower_visibility(enum_);
let name = enum_.name()?.as_name();
let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_);
@ -240,7 +248,7 @@ impl Ctx {
None => self.next_variant_idx()..self.next_variant_idx(),
};
let ast_id = self.source_ast_id_map.ast_id(enum_);
let res = Enum { name, attrs, visibility, generic_params, variants, ast_id };
let res = Enum { name, visibility, generic_params, variants, ast_id };
Some(id(self.tree.enums.alloc(res)))
}
@ -263,7 +271,6 @@ impl Ctx {
}
fn lower_function(&mut self, func: &ast::FnDef) -> Option<FileItemTreeId<Function>> {
let attrs = self.lower_attrs(func);
let visibility = self.lower_visibility(func);
let name = func.name()?.as_name();
@ -309,7 +316,6 @@ impl Ctx {
let ast_id = self.source_ast_id_map.ast_id(func);
let mut res = Function {
name,
attrs,
visibility,
generic_params: GenericParams::default(),
has_self_param,
@ -390,9 +396,13 @@ impl Ctx {
let items = trait_def.item_list().map(|list| {
self.with_inherited_visibility(visibility.clone(), |this| {
list.items()
.flat_map(|item| {
.filter_map(|item| {
let attrs = Attrs::new(&item, &this.hygiene);
this.collect_inner_items(item.syntax());
this.lower_assoc_item(&item)
this.lower_assoc_item(&item).map(|item| {
this.add_attrs(item.into(), attrs);
item
})
})
.collect()
})
@ -422,6 +432,8 @@ impl Ctx {
.filter_map(|item| {
self.collect_inner_items(item.syntax());
let assoc = self.lower_assoc_item(&item)?;
let attrs = Attrs::new(&item, &self.hygiene);
self.add_attrs(assoc.into(), attrs);
Some(assoc)
})
.collect();
@ -506,6 +518,7 @@ impl Ctx {
list.extern_items()
.filter_map(|item| {
self.collect_inner_items(item.syntax());
let attrs = Attrs::new(&item, &self.hygiene);
let id = match item {
ast::ExternItem::FnDef(ast) => {
let func = self.lower_function(&ast)?;
@ -516,6 +529,7 @@ impl Ctx {
statik.into()
}
};
self.add_attrs(id, attrs);
Some(id)
})
.collect()
@ -576,9 +590,6 @@ impl Ctx {
}
}
fn lower_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
Attrs::new(item, &self.hygiene)
}
fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility {
if let Some(vis) = self.forced_visibility.as_ref() {
vis.clone()

View file

@ -77,13 +77,12 @@ fn print_item_tree(ra_fixture: &str) -> String {
}
if !tree.inner_items.is_empty() {
format_to!(out, "\ninner items:\n");
format_to!(out, "\ninner items:\n\n");
for (ast_id, items) in &tree.inner_items {
format_to!(out, "{:?}:\n", ast_id);
format_to!(out, "for AST {:?}:\n", ast_id);
for inner in items {
format_to!(out, "- ");
fmt_mod_item(&mut out, &tree, *inner);
format_to!(out, "\n");
format_to!(out, "\n\n");
}
}
}
@ -92,6 +91,12 @@ fn print_item_tree(ra_fixture: &str) -> String {
}
fn fmt_mod_item(out: &mut String, tree: &ItemTree, item: ModItem) {
let attrs = tree.attrs(item);
if !attrs.is_empty() {
format_to!(out, "#[{:?}]\n", attrs);
}
let mut children = String::new();
match item {
ModItem::ExternCrate(it) => {
format_to!(out, "{:?}", tree[it]);
@ -119,20 +124,41 @@ fn fmt_mod_item(out: &mut String, tree: &ItemTree, item: ModItem) {
}
ModItem::Trait(it) => {
format_to!(out, "{:?}", tree[it]);
for item in &tree[it].items {
fmt_mod_item(&mut children, tree, ModItem::from(*item));
format_to!(children, "\n");
}
}
ModItem::Impl(it) => {
format_to!(out, "{:?}", tree[it]);
for item in &tree[it].items {
fmt_mod_item(&mut children, tree, ModItem::from(*item));
format_to!(children, "\n");
}
}
ModItem::TypeAlias(it) => {
format_to!(out, "{:?}", tree[it]);
}
ModItem::Mod(it) => {
format_to!(out, "{:?}", tree[it]);
match &tree[it].kind {
ModKind::Inline { items } => {
for item in items {
fmt_mod_item(&mut children, tree, *item);
format_to!(children, "\n");
}
}
ModKind::Outline {} => {}
}
}
ModItem::MacroCall(it) => {
format_to!(out, "{:?}", tree[it]);
}
}
for line in children.lines() {
format_to!(out, "\n> {}", line);
}
}
#[test]
@ -140,44 +166,83 @@ fn smoke() {
assert_snapshot!(print_item_tree(r"
#![attr]
#[attr_on_use]
use {a, b::*};
#[ext_crate]
extern crate krate;
#[on_trait]
trait Tr<U> {
#[assoc_ty]
type AssocTy: Tr<()>;
#[assoc_const]
const CONST: u8;
#[assoc_method]
fn method(&self);
#[assoc_dfl_method]
fn dfl_method(&mut self) {}
}
#[struct0]
struct Struct0<T = ()>;
struct Struct1<T>(u8);
#[struct1]
struct Struct1<T>(#[struct1fld] u8);
#[struct2]
struct Struct2<T> {
#[struct2fld]
fld: (T, ),
}
#[en]
enum En {
#[enum_variant]
Variant {
#[enum_field]
field: u8,
},
}
#[un]
union Un {
#[union_fld]
fld: u16,
}
"), @r###"
inner attrs: Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr"))] }, input: None }]) }
top-level items:
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_on_use"))] }, input: None }]) }]
Import { path: ModPath { kind: Plain, segments: [Name(Text("a"))] }, alias: None, visibility: Module(ModPath { kind: Super(0), segments: [] }), is_glob: false, is_prelude: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UseItem>(0) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_on_use"))] }, input: None }]) }]
Import { path: ModPath { kind: Plain, segments: [Name(Text("b"))] }, alias: None, visibility: Module(ModPath { kind: Super(0), segments: [] }), is_glob: true, is_prelude: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UseItem>(0) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("ext_crate"))] }, input: None }]) }]
ExternCrate { path: ModPath { kind: Plain, segments: [Name(Text("krate"))] }, alias: None, visibility: Module(ModPath { kind: Super(0), segments: [] }), is_macro_use: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ExternCrateItem>(1) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_trait"))] }, input: None }]) }]
Trait { name: Name(Text("Tr")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 2, data: [TypeParamData { name: Some(Name(Text("Self"))), default: None, provenance: TraitSelf }, TypeParamData { name: Some(Name(Text("U"))), default: None, provenance: TypeParamList }] }, where_predicates: [] }, auto: false, items: [TypeAlias(Idx::<TypeAlias>(0)), Const(Idx::<Const>(0)), Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::TraitDef>(2) }
Struct { name: Name(Text("Struct0")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: Some(Tuple([])), provenance: TypeParamList }] }, where_predicates: [] }, fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit }
Struct { name: Name(Text("Struct1")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: None, provenance: TypeParamList }] }, where_predicates: [] }, fields: Tuple(Idx::<Field>(0)..Idx::<Field>(1)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple }
Struct { name: Name(Text("Struct2")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: None, provenance: TypeParamList }] }, where_predicates: [] }, fields: Record(Idx::<Field>(1)..Idx::<Field>(2)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record }
Enum { name: Name(Text("En")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, variants: Idx::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) }
Union { name: Name(Text("Un")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, fields: Record(Idx::<Field>(3)..Idx::<Field>(4)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_ty"))] }, input: None }]) }]
> TypeAlias { name: Name(Text("AssocTy")), visibility: Module(ModPath { kind: Super(0), segments: [] }), bounds: [Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Tr"))] }, generic_args: [Some(GenericArgs { args: [Type(Tuple([]))], has_self_type: false, bindings: [] })] })], generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, type_ref: None, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::TypeAliasDef>(8) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_const"))] }, input: None }]) }]
> Const { name: Some(Name(Text("CONST"))), visibility: Module(ModPath { kind: Super(0), segments: [] }), type_ref: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("u8"))] }, generic_args: [None] }), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ConstDef>(9) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_method"))] }, input: None }]) }]
> Function { name: Name(Text("method")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: true, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Shared)], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(10) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("assoc_dfl_method"))] }, input: None }]) }]
> Function { name: Name(Text("dfl_method")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: true, is_unsafe: false, params: [Reference(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Self"))] }, generic_args: [None] }), Mut)], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(11) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct0"))] }, input: None }]) }]
Struct { name: Name(Text("Struct0")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: Some(Tuple([])), provenance: TypeParamList }] }, where_predicates: [] }, fields: Unit, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(3), kind: Unit }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct1"))] }, input: None }]) }]
Struct { name: Name(Text("Struct1")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: None, provenance: TypeParamList }] }, where_predicates: [] }, fields: Tuple(Idx::<Field>(0)..Idx::<Field>(1)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(4), kind: Tuple }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("struct2"))] }, input: None }]) }]
Struct { name: Name(Text("Struct2")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: None, provenance: TypeParamList }] }, where_predicates: [] }, fields: Record(Idx::<Field>(1)..Idx::<Field>(2)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::StructDef>(5), kind: Record }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("en"))] }, input: None }]) }]
Enum { name: Name(Text("En")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, variants: Idx::<Variant>(0)..Idx::<Variant>(1), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::EnumDef>(6) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("un"))] }, input: None }]) }]
Union { name: Name(Text("Un")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, fields: Record(Idx::<Field>(3)..Idx::<Field>(4)), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::UnionDef>(7) }
"###);
}
@ -201,10 +266,92 @@ inner attrs: Attrs { entries: None }
top-level items:
Impl { generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("T"))), default: None, provenance: TypeParamList }] }, where_predicates: [WherePredicate { target: TypeRef(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("T"))] }, generic_args: [None] })), bound: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("A"))] }, generic_args: [None] }) }] }, target_trait: Some(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("D"))] }, generic_args: [None] })), target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Response"))] }, generic_args: [Some(GenericArgs { args: [Type(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("T"))] }, generic_args: [None] }))], has_self_type: false, bindings: [] })] }), is_negative: false, items: [Function(Idx::<Function>(1))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ImplDef>(0) }
> Function { name: Name(Text("foo")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(1) }
inner items:
FileAstId::<ra_syntax::ast::generated::nodes::ModuleItem>(2):
- Function { name: Name(Text("end")), attrs: Attrs { entries: None }, visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("W"))), default: None, provenance: TypeParamList }] }, where_predicates: [WherePredicate { target: TypeRef(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("W"))] }, generic_args: [None] })), bound: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Write"))] }, generic_args: [None] }) }] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(2) }
for AST FileAstId::<ra_syntax::ast::generated::nodes::ModuleItem>(2):
Function { name: Name(Text("end")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("W"))), default: None, provenance: TypeParamList }] }, where_predicates: [WherePredicate { target: TypeRef(Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("W"))] }, generic_args: [None] })), bound: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Write"))] }, generic_args: [None] }) }] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(2) }
"###);
}
#[test]
fn extern_attrs() {
let tree = print_item_tree(
r#"
#[block_attr]
extern "C" {
#[attr_a]
fn a() {}
#[attr_b]
fn b() {}
}
"#,
);
assert_snapshot!(tree, @r###"
inner attrs: Attrs { entries: None }
top-level items:
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }]
Function { name: Name(Text("a")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(1) }
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }, Attr { path: ModPath { kind: Plain, segments: [Name(Text("block_attr"))] }, input: None }]) }]
Function { name: Name(Text("b")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(2) }
"###);
}
#[test]
fn trait_attrs() {
let tree = print_item_tree(
r#"
#[trait_attr]
trait Tr {
#[attr_a]
fn a() {}
#[attr_b]
fn b() {}
}
"#,
);
assert_snapshot!(tree, @r###"
inner attrs: Attrs { entries: None }
top-level items:
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("trait_attr"))] }, input: None }]) }]
Trait { name: Name(Text("Tr")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 1, data: [TypeParamData { name: Some(Name(Text("Self"))), default: None, provenance: TraitSelf }] }, where_predicates: [] }, auto: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::TraitDef>(0) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }]
> Function { name: Name(Text("a")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(1) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }]
> Function { name: Name(Text("b")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(2) }
"###);
}
#[test]
fn impl_attrs() {
let tree = print_item_tree(
r#"
#[impl_attr]
impl Ty {
#[attr_a]
fn a() {}
#[attr_b]
fn b() {}
}
"#,
);
assert_snapshot!(tree, @r###"
inner attrs: Attrs { entries: None }
top-level items:
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("impl_attr"))] }, input: None }]) }]
Impl { generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, target_trait: None, target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Ty"))] }, generic_args: [None] }), is_negative: false, items: [Function(Idx::<Function>(0)), Function(Idx::<Function>(1))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ImplDef>(0) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_a"))] }, input: None }]) }]
> Function { name: Name(Text("a")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(1) }
> #[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("attr_b"))] }, input: None }]) }]
> Function { name: Name(Text("b")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(2) }
"###);
}
@ -235,6 +382,32 @@ fn cursed_inner_items() {
);
}
#[test]
fn inner_item_attrs() {
let tree = print_item_tree(
r"
fn foo() {
#[on_inner]
fn inner() {}
}
",
);
assert_snapshot!(tree, @r###"
inner attrs: Attrs { entries: None }
top-level items:
Function { name: Name(Text("foo")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(0) }
inner items:
for AST FileAstId::<ra_syntax::ast::generated::nodes::ModuleItem>(1):
#[Attrs { entries: Some([Attr { path: ModPath { kind: Plain, segments: [Name(Text("on_inner"))] }, input: None }]) }]
Function { name: Name(Text("inner")), visibility: Module(ModPath { kind: Super(0), segments: [] }), generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, has_self_param: false, is_unsafe: false, params: [], ret_type: Tuple([]), ast_id: FileAstId::<ra_syntax::ast::generated::nodes::FnDef>(1) }
"###);
}
#[test]
fn assoc_item_macros() {
let tree = print_item_tree(
@ -250,5 +423,6 @@ inner attrs: Attrs { entries: None }
top-level items:
Impl { generic_params: GenericParams { types: Arena { len: 0, data: [] }, where_predicates: [] }, target_trait: None, target_type: Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("S"))] }, generic_args: [None] }), is_negative: false, items: [MacroCall(Idx::<MacroCall>(0))], ast_id: FileAstId::<ra_syntax::ast::generated::nodes::ImplDef>(0) }
> MacroCall { name: None, path: ModPath { kind: Plain, segments: [Name(Text("items"))] }, is_export: false, is_local_inner: false, is_builtin: false, ast_id: FileAstId::<ra_syntax::ast::generated::nodes::MacroCall>(1) }
"###);
}