Skip to content

Commit 2100a06

Browse files
committed
Cleanup some inert attribute stuff
1 parent 09d7dcc commit 2100a06

File tree

16 files changed

+151
-151
lines changed

16 files changed

+151
-151
lines changed

crates/hir-def/src/attr.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
//! A higher level attributes based on TokenTree, with also some shortcuts.
22
3-
pub mod builtin;
4-
5-
#[cfg(test)]
6-
mod tests;
7-
83
use std::{borrow::Cow, hash::Hash, ops, slice::Iter as SliceIter};
94

105
use base_db::CrateId;
@@ -646,3 +641,55 @@ pub(crate) fn fields_attrs_source_map(
646641

647642
Arc::new(res)
648643
}
644+
645+
#[cfg(test)]
646+
mod tests {
647+
//! This module contains tests for doc-expression parsing.
648+
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
649+
650+
use triomphe::Arc;
651+
652+
use base_db::FileId;
653+
use hir_expand::span_map::{RealSpanMap, SpanMap};
654+
use mbe::{syntax_node_to_token_tree, DocCommentDesugarMode};
655+
use syntax::{ast, AstNode, TextRange};
656+
657+
use crate::attr::{DocAtom, DocExpr};
658+
659+
fn assert_parse_result(input: &str, expected: DocExpr) {
660+
let source_file = ast::SourceFile::parse(input, span::Edition::CURRENT).ok().unwrap();
661+
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
662+
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
663+
let tt = syntax_node_to_token_tree(
664+
tt.syntax(),
665+
map.as_ref(),
666+
map.span_for_range(TextRange::empty(0.into())),
667+
DocCommentDesugarMode::ProcMacro,
668+
);
669+
let cfg = DocExpr::parse(&tt);
670+
assert_eq!(cfg, expected);
671+
}
672+
673+
#[test]
674+
fn test_doc_expr_parser() {
675+
assert_parse_result("#![doc(hidden)]", DocAtom::Flag("hidden".into()).into());
676+
677+
assert_parse_result(
678+
r#"#![doc(alias = "foo")]"#,
679+
DocAtom::KeyValue { key: "alias".into(), value: "foo".into() }.into(),
680+
);
681+
682+
assert_parse_result(r#"#![doc(alias("foo"))]"#, DocExpr::Alias(["foo".into()].into()));
683+
assert_parse_result(
684+
r#"#![doc(alias("foo", "bar", "baz"))]"#,
685+
DocExpr::Alias(["foo".into(), "bar".into(), "baz".into()].into()),
686+
);
687+
688+
assert_parse_result(
689+
r#"
690+
#[doc(alias("Bar", "Qux"))]
691+
struct Foo;"#,
692+
DocExpr::Alias(["Bar".into(), "Qux".into()].into()),
693+
);
694+
}
695+
}

crates/hir-def/src/attr/tests.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

crates/hir-def/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ impl<'a> AssocItemCollector<'a> {
642642
continue 'attrs;
643643
}
644644
let loc = self.db.lookup_intern_macro_call(call_id);
645-
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
645+
if let MacroDefKind::ProcMacro(_, exp, _) = loc.def.kind {
646646
// If there's no expander for the proc macro (e.g. the
647647
// proc macro is ignored, or building the proc macro
648648
// crate failed), skip expansion like we would if it was

crates/hir-def/src/db.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
294294
let in_file = InFile::new(file_id, m);
295295
match expander {
296296
MacroExpander::Declarative => MacroDefKind::Declarative(in_file),
297-
MacroExpander::BuiltIn(it) => MacroDefKind::BuiltIn(it, in_file),
298-
MacroExpander::BuiltInAttr(it) => MacroDefKind::BuiltInAttr(it, in_file),
299-
MacroExpander::BuiltInDerive(it) => MacroDefKind::BuiltInDerive(it, in_file),
300-
MacroExpander::BuiltInEager(it) => MacroDefKind::BuiltInEager(it, in_file),
297+
MacroExpander::BuiltIn(it) => MacroDefKind::BuiltIn(in_file, it),
298+
MacroExpander::BuiltInAttr(it) => MacroDefKind::BuiltInAttr(in_file, it),
299+
MacroExpander::BuiltInDerive(it) => MacroDefKind::BuiltInDerive(in_file, it),
300+
MacroExpander::BuiltInEager(it) => MacroDefKind::BuiltInEager(in_file, it),
301301
}
302302
};
303303

@@ -338,9 +338,9 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
338338
MacroDefId {
339339
krate: loc.container.krate,
340340
kind: MacroDefKind::ProcMacro(
341+
InFile::new(loc.id.file_id(), makro.ast_id),
341342
loc.expander,
342343
loc.kind,
343-
InFile::new(loc.id.file_id(), makro.ast_id),
344344
),
345345
local_inner: false,
346346
allow_internal_unsafe: false,

crates/hir-def/src/nameres/attr_resolution.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use base_db::CrateId;
44
use hir_expand::{
55
attrs::{Attr, AttrId, AttrInput},
6+
inert_attr_macro::find_builtin_attr_idx,
67
MacroCallId, MacroCallKind, MacroDefId,
78
};
89
use span::SyntaxContextId;
910
use syntax::{ast, SmolStr};
1011
use triomphe::Arc;
1112

1213
use crate::{
13-
attr::builtin::find_builtin_attr_idx,
1414
db::DefDatabase,
1515
item_scope::BuiltinShadowMode,
1616
nameres::path_resolution::ResolveMode,
@@ -89,9 +89,12 @@ impl DefMap {
8989
}
9090

9191
if segments.len() == 1 {
92-
let mut registered = self.data.registered_attrs.iter().map(SmolStr::as_str);
93-
let is_inert = find_builtin_attr_idx(&name).is_some() || registered.any(pred);
94-
return is_inert;
92+
if find_builtin_attr_idx(&name).is_some() {
93+
return true;
94+
}
95+
if self.data.registered_attrs.iter().map(SmolStr::as_str).any(pred) {
96+
return true;
97+
}
9598
}
9699
}
97100
false

crates/hir-def/src/nameres/collector.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ struct DefCollector<'a> {
270270
///
271271
/// This also stores the attributes to skip when we resolve derive helpers and non-macro
272272
/// non-builtin attributes in general.
273+
// FIXME: There has to be a better way to do this
273274
skip_attrs: FxHashMap<InFile<ModItem>, AttrId>,
274275
}
275276

@@ -1255,13 +1256,24 @@ impl DefCollector<'_> {
12551256
_ => return Resolved::No,
12561257
};
12571258

1258-
let call_id =
1259-
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def);
1259+
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1260+
// due to duplicating functions into macro expansions
1261+
if matches!(
1262+
def.kind,
1263+
MacroDefKind::BuiltInAttr(_, expander)
1264+
if expander.is_test() || expander.is_bench()
1265+
) {
1266+
return recollect_without(self);
1267+
}
1268+
1269+
let call_id = || {
1270+
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def)
1271+
};
12601272
if let MacroDefId {
12611273
kind:
12621274
MacroDefKind::BuiltInAttr(
1263-
BuiltinAttrExpander::Derive | BuiltinAttrExpander::DeriveConst,
12641275
_,
1276+
BuiltinAttrExpander::Derive | BuiltinAttrExpander::DeriveConst,
12651277
),
12661278
..
12671279
} = def
@@ -1290,6 +1302,7 @@ impl DefCollector<'_> {
12901302

12911303
match attr.parse_path_comma_token_tree(self.db.upcast()) {
12921304
Some(derive_macros) => {
1305+
let call_id = call_id();
12931306
let mut len = 0;
12941307
for (idx, (path, call_site)) in derive_macros.enumerate() {
12951308
let ast_id = AstIdWithPath::new(file_id, ast_id.value, path);
@@ -1312,13 +1325,6 @@ impl DefCollector<'_> {
13121325
// This is just a trick to be able to resolve the input to derives
13131326
// as proper paths in `Semantics`.
13141327
// Check the comment in [`builtin_attr_macro`].
1315-
let call_id = attr_macro_as_call_id(
1316-
self.db,
1317-
file_ast_id,
1318-
attr,
1319-
self.def_map.krate,
1320-
def,
1321-
);
13221328
self.def_map.modules[directive.module_id]
13231329
.scope
13241330
.init_derive_attribute(ast_id, attr.id, call_id, len + 1);
@@ -1336,17 +1342,8 @@ impl DefCollector<'_> {
13361342
return recollect_without(self);
13371343
}
13381344

1339-
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1340-
// due to duplicating functions into macro expansions
1341-
if matches!(
1342-
def.kind,
1343-
MacroDefKind::BuiltInAttr(expander, _)
1344-
if expander.is_test() || expander.is_bench()
1345-
) {
1346-
return recollect_without(self);
1347-
}
1348-
1349-
if let MacroDefKind::ProcMacro(exp, ..) = def.kind {
1345+
let call_id = call_id();
1346+
if let MacroDefKind::ProcMacro(_, exp, _) = def.kind {
13501347
// If proc attribute macro expansion is disabled, skip expanding it here
13511348
if !self.db.expand_proc_attr_macros() {
13521349
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(

crates/hir-expand/src/cfg_process.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ pub(crate) fn process_cfg_attrs(
189189
// FIXME: #[cfg_eval] is not implemented. But it is not stable yet
190190
let is_derive = match loc.def.kind {
191191
MacroDefKind::BuiltInDerive(..)
192-
| MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
193-
MacroDefKind::BuiltInAttr(expander, _) => expander.is_derive(),
192+
| MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) => true,
193+
MacroDefKind::BuiltInAttr(_, expander) => expander.is_derive(),
194194
_ => false,
195195
};
196196
if !is_derive {

crates/hir-expand/src/db.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn expand_speculative(
252252
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
253253
let mut speculative_expansion =
254254
match loc.def.kind {
255-
MacroDefKind::ProcMacro(expander, _, ast) => {
255+
MacroDefKind::ProcMacro(ast, expander, _) => {
256256
let span = db.proc_macro_span(ast);
257257
tt.delimiter = tt::Delimiter::invisible_spanned(span);
258258
expander.expand(
@@ -266,22 +266,22 @@ pub fn expand_speculative(
266266
span_with_mixed_site_ctxt(db, span, actual_macro_call),
267267
)
268268
}
269-
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
269+
MacroDefKind::BuiltInAttr(_, it) if it.is_derive() => {
270270
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, span)
271271
}
272272
MacroDefKind::Declarative(it) => db
273273
.decl_macro_expander(loc.krate, it)
274274
.expand_unhygienic(db, tt, loc.def.krate, span, loc.def.edition),
275-
MacroDefKind::BuiltIn(it, _) => {
275+
MacroDefKind::BuiltIn(_, it) => {
276276
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
277277
}
278-
MacroDefKind::BuiltInDerive(it, ..) => {
278+
MacroDefKind::BuiltInDerive(_, it) => {
279279
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
280280
}
281-
MacroDefKind::BuiltInEager(it, _) => {
281+
MacroDefKind::BuiltInEager(_, it) => {
282282
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
283283
}
284-
MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt, span),
284+
MacroDefKind::BuiltInAttr(_, it) => it.expand(db, actual_macro_call, &tt, span),
285285
};
286286

287287
let expand_to = loc.expand_to();
@@ -493,7 +493,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
493493
.map_or_else(|| node.syntax().text_range(), |it| it.syntax().text_range()),
494494
);
495495
// If derive attribute we need to censor the derive input
496-
if matches!(loc.def.kind, MacroDefKind::BuiltInAttr(expander, ..) if expander.is_derive())
496+
if matches!(loc.def.kind, MacroDefKind::BuiltInAttr(_, expander) if expander.is_derive())
497497
&& ast::Adt::can_cast(node.syntax().kind())
498498
{
499499
let adt = ast::Adt::cast(node.syntax().clone()).unwrap();
@@ -569,11 +569,11 @@ impl TokenExpander {
569569
MacroDefKind::Declarative(ast_id) => {
570570
TokenExpander::DeclarativeMacro(db.decl_macro_expander(id.krate, ast_id))
571571
}
572-
MacroDefKind::BuiltIn(expander, _) => TokenExpander::BuiltIn(expander),
573-
MacroDefKind::BuiltInAttr(expander, _) => TokenExpander::BuiltInAttr(expander),
574-
MacroDefKind::BuiltInDerive(expander, _) => TokenExpander::BuiltInDerive(expander),
575-
MacroDefKind::BuiltInEager(expander, ..) => TokenExpander::BuiltInEager(expander),
576-
MacroDefKind::ProcMacro(expander, ..) => TokenExpander::ProcMacro(expander),
572+
MacroDefKind::BuiltIn(_, expander) => TokenExpander::BuiltIn(expander),
573+
MacroDefKind::BuiltInAttr(_, expander) => TokenExpander::BuiltInAttr(expander),
574+
MacroDefKind::BuiltInDerive(_, expander) => TokenExpander::BuiltInDerive(expander),
575+
MacroDefKind::BuiltInEager(_, expander) => TokenExpander::BuiltInEager(expander),
576+
MacroDefKind::ProcMacro(_, expander, _) => TokenExpander::ProcMacro(expander),
577577
}
578578
}
579579
}
@@ -604,13 +604,13 @@ fn macro_expand(
604604
MacroDefKind::Declarative(id) => db
605605
.decl_macro_expander(loc.def.krate, id)
606606
.expand(db, arg.clone(), macro_call_id, span),
607-
MacroDefKind::BuiltIn(it, _) => {
607+
MacroDefKind::BuiltIn(_, it) => {
608608
it.expand(db, macro_call_id, arg, span).map_err(Into::into).zip_val(None)
609609
}
610-
MacroDefKind::BuiltInDerive(it, _) => {
610+
MacroDefKind::BuiltInDerive(_, it) => {
611611
it.expand(db, macro_call_id, arg, span).map_err(Into::into).zip_val(None)
612612
}
613-
MacroDefKind::BuiltInEager(it, _) => {
613+
MacroDefKind::BuiltInEager(_, it) => {
614614
// This might look a bit odd, but we do not expand the inputs to eager macros here.
615615
// Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
616616
// That kind of expansion uses the ast id map of an eager macros input though which goes through
@@ -634,12 +634,12 @@ fn macro_expand(
634634
}
635635
res.zip_val(None)
636636
}
637-
MacroDefKind::BuiltInAttr(it, _) => {
637+
MacroDefKind::BuiltInAttr(_, it) => {
638638
let mut res = it.expand(db, macro_call_id, arg, span);
639639
fixup::reverse_fixups(&mut res.value, &undo_info);
640640
res.zip_val(None)
641641
}
642-
_ => unreachable!(),
642+
MacroDefKind::ProcMacro(_, _, _) => unreachable!(),
643643
};
644644
(ExpandResult { value: res.value, err: res.err }, span)
645645
}
@@ -678,8 +678,8 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
678678
let loc = db.lookup_intern_macro_call(id);
679679
let (macro_arg, undo_info, span) = db.macro_arg_considering_derives(id, &loc.kind);
680680

681-
let (expander, ast) = match loc.def.kind {
682-
MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast),
681+
let (ast, expander) = match loc.def.kind {
682+
MacroDefKind::ProcMacro(ast, expander, _) => (ast, expander),
683683
_ => unreachable!(),
684684
};
685685

crates/hir-def/src/attr/builtin.rs renamed to crates/hir-expand/src/inert_attr_macro.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ pub fn find_builtin_attr_idx(name: &str) -> Option<usize> {
3636
.copied()
3737
}
3838

39-
// impl AttributeTemplate {
40-
// const DEFAULT: AttributeTemplate =
41-
// AttributeTemplate { word: false, list: None, name_value_str: None };
42-
// }
43-
4439
/// A convenience macro for constructing attribute templates.
4540
/// E.g., `template!(Word, List: "description")` means that the attribute
4641
/// supports forms `#[attr]` and `#[attr(description)]`.

0 commit comments

Comments
 (0)