2022-02-02 15:03:46 +01:00
|
|
|
//! Completion for (built-in) attributes, derives and lints.
|
2020-04-23 18:22:33 +02:00
|
|
|
//!
|
2022-02-02 15:03:46 +01:00
|
|
|
//! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
|
2020-04-23 18:22:33 +02:00
|
|
|
|
2021-12-04 18:01:22 +01:00
|
|
|
use ide_db::{
|
2022-03-06 19:01:30 +01:00
|
|
|
generated::lints::{
|
|
|
|
Lint, CLIPPY_LINTS, CLIPPY_LINT_GROUPS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS,
|
2021-12-04 18:01:22 +01:00
|
|
|
},
|
2022-03-06 19:01:30 +01:00
|
|
|
syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
|
2022-04-25 18:51:59 +02:00
|
|
|
FxHashMap, SymbolKind,
|
2021-11-25 00:21:29 +09:00
|
|
|
};
|
2021-10-17 12:44:41 +02:00
|
|
|
use itertools::Itertools;
|
2021-05-27 23:28:14 +02:00
|
|
|
use once_cell::sync::Lazy;
|
2022-02-02 13:35:46 +01:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, AttrKind},
|
|
|
|
AstNode, SyntaxKind, T,
|
|
|
|
};
|
2020-04-23 18:22:33 +02:00
|
|
|
|
2022-02-02 13:35:46 +01:00
|
|
|
use crate::{
|
2022-06-20 14:23:46 +02:00
|
|
|
context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
|
2022-02-02 13:35:46 +01:00
|
|
|
item::CompletionItem,
|
|
|
|
Completions,
|
|
|
|
};
|
2020-05-04 15:07:51 +02:00
|
|
|
|
2021-05-30 14:52:19 +01:00
|
|
|
mod cfg;
|
2021-05-27 23:40:33 +02:00
|
|
|
mod derive;
|
|
|
|
mod lint;
|
2021-06-17 21:15:35 +02:00
|
|
|
mod repr;
|
2023-12-16 16:19:58 +00:00
|
|
|
mod macro_use;
|
2021-05-27 23:40:33 +02:00
|
|
|
|
2022-06-20 14:47:30 +02:00
|
|
|
pub(crate) use self::derive::complete_derive_path;
|
2022-03-10 20:53:50 +01:00
|
|
|
|
2022-02-02 13:35:46 +01:00
|
|
|
/// Complete inputs to known builtin attributes as well as derive attributes
|
|
|
|
pub(crate) fn complete_known_attribute_input(
|
|
|
|
acc: &mut Completions,
|
2022-07-20 15:02:08 +02:00
|
|
|
ctx: &CompletionContext<'_>,
|
2022-06-20 15:07:48 +02:00
|
|
|
&colon_prefix: &bool,
|
2022-06-17 23:36:39 +02:00
|
|
|
fake_attribute_under_caret: &ast::Attr,
|
2023-12-16 16:19:58 +00:00
|
|
|
extern_crate: Option<&ast::ExternCrate>,
|
2022-02-02 13:35:46 +01:00
|
|
|
) -> Option<()> {
|
2022-06-17 23:36:39 +02:00
|
|
|
let attribute = fake_attribute_under_caret;
|
2021-09-14 00:31:14 +02:00
|
|
|
let name_ref = match attribute.path() {
|
|
|
|
Some(p) => Some(p.as_single_name_ref()?),
|
|
|
|
None => None,
|
|
|
|
};
|
2022-02-02 13:35:46 +01:00
|
|
|
let (path, tt) = name_ref.zip(attribute.token_tree())?;
|
2024-01-18 13:59:49 +01:00
|
|
|
tt.l_paren_token()?;
|
2021-12-17 08:34:08 +09:00
|
|
|
|
2022-02-02 13:35:46 +01:00
|
|
|
match path.text().as_str() {
|
|
|
|
"repr" => repr::complete_repr(acc, ctx, tt),
|
2022-06-20 15:07:48 +02:00
|
|
|
"feature" => {
|
|
|
|
lint::complete_lint(acc, ctx, colon_prefix, &parse_tt_as_comma_sep_paths(tt)?, FEATURES)
|
|
|
|
}
|
2022-02-02 13:35:46 +01:00
|
|
|
"allow" | "warn" | "deny" | "forbid" => {
|
|
|
|
let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
|
2021-12-17 08:34:08 +09:00
|
|
|
|
2022-02-02 13:35:46 +01:00
|
|
|
let lints: Vec<Lint> = CLIPPY_LINT_GROUPS
|
|
|
|
.iter()
|
|
|
|
.map(|g| &g.lint)
|
|
|
|
.chain(DEFAULT_LINTS)
|
|
|
|
.chain(CLIPPY_LINTS)
|
|
|
|
.chain(RUSTDOC_LINTS)
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
2022-06-20 15:07:48 +02:00
|
|
|
lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
|
2022-02-02 13:35:46 +01:00
|
|
|
}
|
2022-03-10 20:53:50 +01:00
|
|
|
"cfg" => cfg::complete_cfg(acc, ctx),
|
2023-12-16 16:19:58 +00:00
|
|
|
"macro_use" => {
|
|
|
|
macro_use::complete_macro_use(acc, ctx, extern_crate, &parse_tt_as_comma_sep_paths(tt)?)
|
|
|
|
}
|
2022-02-02 13:35:46 +01:00
|
|
|
_ => (),
|
2020-05-01 03:46:17 +03:00
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
2020-04-23 18:22:33 +02:00
|
|
|
|
2022-06-20 14:47:30 +02:00
|
|
|
pub(crate) fn complete_attribute_path(
|
2022-06-17 23:36:39 +02:00
|
|
|
acc: &mut Completions,
|
2022-07-20 15:02:08 +02:00
|
|
|
ctx: &CompletionContext<'_>,
|
2022-06-20 17:41:04 +02:00
|
|
|
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
|
2022-06-20 14:23:46 +02:00
|
|
|
&AttrCtx { kind, annotated_item_kind }: &AttrCtx,
|
2022-06-17 23:36:39 +02:00
|
|
|
) {
|
2022-06-20 14:23:46 +02:00
|
|
|
let is_inner = kind == AttrKind::Inner;
|
2022-02-02 13:35:46 +01:00
|
|
|
|
2022-06-17 16:36:22 +02:00
|
|
|
match qualified {
|
2022-06-17 17:49:25 +02:00
|
|
|
Qualified::With {
|
|
|
|
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
|
2022-07-10 14:08:49 +05:30
|
|
|
super_chain_len,
|
2022-06-17 17:49:25 +02:00
|
|
|
..
|
|
|
|
} => {
|
2022-07-10 14:08:49 +05:30
|
|
|
acc.add_super_keyword(ctx, *super_chain_len);
|
2022-02-02 13:35:46 +01:00
|
|
|
|
2022-03-31 11:12:08 +02:00
|
|
|
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
|
2022-06-20 14:47:30 +02:00
|
|
|
match def {
|
|
|
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
|
2022-06-20 17:41:04 +02:00
|
|
|
acc.add_macro(ctx, path_ctx, m, name)
|
2022-06-20 14:47:30 +02:00
|
|
|
}
|
|
|
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
|
2023-03-29 14:08:25 +02:00
|
|
|
acc.add_module(ctx, path_ctx, m, name, vec![])
|
2022-06-20 14:47:30 +02:00
|
|
|
}
|
|
|
|
_ => (),
|
2022-02-02 15:03:46 +01:00
|
|
|
}
|
2022-02-02 13:35:46 +01:00
|
|
|
}
|
2022-02-02 15:03:46 +01:00
|
|
|
return;
|
2022-02-02 13:35:46 +01:00
|
|
|
}
|
2022-02-02 15:03:46 +01:00
|
|
|
// fresh use tree with leading colon2, only show crate roots
|
2022-06-20 20:16:40 +02:00
|
|
|
Qualified::Absolute => acc.add_crate_roots(ctx, path_ctx),
|
2022-02-02 15:03:46 +01:00
|
|
|
// only show modules in a fresh UseTree
|
2022-06-17 16:36:22 +02:00
|
|
|
Qualified::No => {
|
2023-03-29 14:08:25 +02:00
|
|
|
ctx.process_all_names(&mut |name, def, doc_aliases| match def {
|
2022-06-20 14:47:30 +02:00
|
|
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
|
2022-06-20 17:41:04 +02:00
|
|
|
acc.add_macro(ctx, path_ctx, m, name)
|
2022-02-02 15:03:46 +01:00
|
|
|
}
|
2022-06-20 20:16:40 +02:00
|
|
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
|
2023-03-29 14:08:25 +02:00
|
|
|
acc.add_module(ctx, path_ctx, m, name, doc_aliases)
|
2022-06-20 20:16:40 +02:00
|
|
|
}
|
2022-06-20 14:47:30 +02:00
|
|
|
_ => (),
|
2022-02-02 15:03:46 +01:00
|
|
|
});
|
2022-04-17 21:53:58 +02:00
|
|
|
acc.add_nameref_keywords_with_colon(ctx);
|
2022-02-02 15:03:46 +01:00
|
|
|
}
|
2022-07-28 10:05:21 +02:00
|
|
|
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
|
2022-02-02 15:03:46 +01:00
|
|
|
}
|
2022-02-02 13:35:46 +01:00
|
|
|
|
|
|
|
let attributes = annotated_item_kind.and_then(|kind| {
|
2021-05-27 23:28:14 +02:00
|
|
|
if ast::Expr::can_cast(kind) {
|
|
|
|
Some(EXPR_ATTRIBUTES)
|
|
|
|
} else {
|
|
|
|
KIND_TO_ATTRIBUTES.get(&kind).copied()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let add_completion = |attr_completion: &AttrCompletion| {
|
2021-12-04 18:01:22 +01:00
|
|
|
let mut item =
|
|
|
|
CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), attr_completion.label);
|
2020-04-23 18:22:33 +02:00
|
|
|
|
2020-06-21 19:14:08 +02:00
|
|
|
if let Some(lookup) = attr_completion.lookup {
|
2021-03-11 17:46:41 +02:00
|
|
|
item.lookup_by(lookup);
|
2020-06-21 19:14:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-07 22:48:54 +01:00
|
|
|
if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
|
2021-03-11 17:46:41 +02:00
|
|
|
item.insert_snippet(cap, snippet);
|
2020-04-23 18:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 23:28:14 +02:00
|
|
|
if is_inner || !attr_completion.prefer_inner {
|
2023-05-24 18:04:29 +02:00
|
|
|
item.add_to(acc, ctx.db);
|
2020-04-23 18:22:33 +02:00
|
|
|
}
|
2021-05-27 23:28:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
match attributes {
|
|
|
|
Some(applicable) => applicable
|
|
|
|
.iter()
|
|
|
|
.flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
|
|
|
|
.flat_map(|idx| ATTRIBUTES.get(idx))
|
|
|
|
.for_each(add_completion),
|
|
|
|
None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
|
|
|
|
None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
|
2020-04-23 18:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AttrCompletion {
|
|
|
|
label: &'static str,
|
2020-06-21 19:14:08 +02:00
|
|
|
lookup: Option<&'static str>,
|
2020-04-23 18:22:33 +02:00
|
|
|
snippet: Option<&'static str>,
|
2020-07-03 15:38:20 +02:00
|
|
|
prefer_inner: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AttrCompletion {
|
2021-05-27 23:28:14 +02:00
|
|
|
fn key(&self) -> &'static str {
|
|
|
|
self.lookup.unwrap_or(self.label)
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:38:20 +02:00
|
|
|
const fn prefer_inner(self) -> AttrCompletion {
|
|
|
|
AttrCompletion { prefer_inner: true, ..self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn attr(
|
|
|
|
label: &'static str,
|
|
|
|
lookup: Option<&'static str>,
|
|
|
|
snippet: Option<&'static str>,
|
|
|
|
) -> AttrCompletion {
|
|
|
|
AttrCompletion { label, lookup, snippet, prefer_inner: false }
|
2020-04-23 18:22:33 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 23:28:14 +02:00
|
|
|
macro_rules! attrs {
|
2021-05-29 14:02:06 +02:00
|
|
|
// attributes applicable to all items
|
2021-05-28 00:35:21 +02:00
|
|
|
[@ { item $($tt:tt)* } {$($acc:tt)*}] => {
|
|
|
|
attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
|
|
|
|
};
|
2021-05-29 14:02:06 +02:00
|
|
|
// attributes applicable to all adts
|
2021-05-28 00:35:21 +02:00
|
|
|
[@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
|
|
|
|
attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
|
|
|
|
};
|
2021-05-29 14:02:06 +02:00
|
|
|
// attributes applicable to all linkable things aka functions/statics
|
2021-05-28 00:35:21 +02:00
|
|
|
[@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
|
2021-05-29 14:02:06 +02:00
|
|
|
attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
|
|
|
|
};
|
|
|
|
// error fallback for nicer error message
|
|
|
|
[@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
|
|
|
|
compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
|
2021-05-28 00:35:21 +02:00
|
|
|
};
|
2021-05-29 14:02:06 +02:00
|
|
|
// general push down accumulation
|
2021-05-28 00:35:21 +02:00
|
|
|
[@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
|
|
|
|
attrs!(@ { $($tt)* } { $($acc)*, $lit })
|
|
|
|
};
|
|
|
|
[@ {$($tt:tt)+} {$($tt2:tt)*}] => {
|
|
|
|
compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
|
|
|
|
};
|
2021-05-29 14:02:06 +02:00
|
|
|
// final output construction
|
2021-05-28 00:35:21 +02:00
|
|
|
[@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
|
2021-05-29 14:02:06 +02:00
|
|
|
// starting matcher
|
2021-05-28 00:35:21 +02:00
|
|
|
[$($tt:tt),*] => {
|
|
|
|
attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
|
2021-05-27 23:28:14 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:35:21 +02:00
|
|
|
#[rustfmt::skip]
|
2021-05-27 23:28:14 +02:00
|
|
|
static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
|
2021-05-29 14:02:06 +02:00
|
|
|
use SyntaxKind::*;
|
2021-10-22 09:23:29 +03:00
|
|
|
[
|
2021-05-28 00:35:21 +02:00
|
|
|
(
|
2021-05-29 14:02:06 +02:00
|
|
|
SOURCE_FILE,
|
2021-05-28 00:35:21 +02:00
|
|
|
attrs!(
|
|
|
|
item,
|
|
|
|
"crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
|
|
|
|
"recursion_limit", "type_length_limit", "windows_subsystem"
|
|
|
|
),
|
|
|
|
),
|
2021-06-24 21:37:07 +02:00
|
|
|
(MODULE, attrs!(item, "macro_use", "no_implicit_prelude", "path")),
|
2021-05-29 14:02:06 +02:00
|
|
|
(ITEM_LIST, attrs!(item, "no_implicit_prelude")),
|
|
|
|
(MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
|
|
|
|
(MACRO_DEF, attrs!(item)),
|
|
|
|
(EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
|
|
|
|
(USE, attrs!(item)),
|
|
|
|
(TYPE_ALIAS, attrs!(item)),
|
|
|
|
(STRUCT, attrs!(item, adt, "non_exhaustive")),
|
|
|
|
(ENUM, attrs!(item, adt, "non_exhaustive")),
|
|
|
|
(UNION, attrs!(item, adt)),
|
|
|
|
(CONST, attrs!(item)),
|
2021-05-28 00:35:21 +02:00
|
|
|
(
|
2021-05-29 14:02:06 +02:00
|
|
|
FN,
|
2021-05-28 00:35:21 +02:00
|
|
|
attrs!(
|
|
|
|
item, linkable,
|
|
|
|
"cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
|
|
|
|
"proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
|
|
|
|
"test", "track_caller"
|
|
|
|
),
|
|
|
|
),
|
2021-05-29 14:02:06 +02:00
|
|
|
(STATIC, attrs!(item, linkable, "global_allocator", "used")),
|
|
|
|
(TRAIT, attrs!(item, "must_use")),
|
|
|
|
(IMPL, attrs!(item, "automatically_derived")),
|
|
|
|
(ASSOC_ITEM_LIST, attrs!(item)),
|
|
|
|
(EXTERN_BLOCK, attrs!(item, "link")),
|
|
|
|
(EXTERN_ITEM_LIST, attrs!(item, "link")),
|
|
|
|
(MACRO_CALL, attrs!()),
|
|
|
|
(SELF_PARAM, attrs!()),
|
|
|
|
(PARAM, attrs!()),
|
|
|
|
(RECORD_FIELD, attrs!()),
|
|
|
|
(VARIANT, attrs!("non_exhaustive")),
|
|
|
|
(TYPE_PARAM, attrs!()),
|
|
|
|
(CONST_PARAM, attrs!()),
|
|
|
|
(LIFETIME_PARAM, attrs!()),
|
|
|
|
(LET_STMT, attrs!()),
|
|
|
|
(EXPR_STMT, attrs!()),
|
|
|
|
(LITERAL, attrs!()),
|
|
|
|
(RECORD_EXPR_FIELD_LIST, attrs!()),
|
|
|
|
(RECORD_EXPR_FIELD, attrs!()),
|
|
|
|
(MATCH_ARM_LIST, attrs!()),
|
|
|
|
(MATCH_ARM, attrs!()),
|
|
|
|
(IDENT_PAT, attrs!()),
|
|
|
|
(RECORD_PAT_FIELD, attrs!()),
|
2021-10-22 09:23:29 +03:00
|
|
|
]
|
|
|
|
.into_iter()
|
2021-05-27 23:28:14 +02:00
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
const EXPR_ATTRIBUTES: &[&str] = attrs!();
|
|
|
|
|
2021-06-13 21:57:10 -07:00
|
|
|
/// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
|
2021-05-27 23:28:14 +02:00
|
|
|
// Keep these sorted for the binary search!
|
2020-04-23 18:22:33 +02:00
|
|
|
const ATTRIBUTES: &[AttrCompletion] = &[
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr("automatically_derived", None, None),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
|
2021-05-27 23:28:14 +02:00
|
|
|
attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr("cold", None, None),
|
|
|
|
attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
|
|
|
|
.prefer_inner(),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
|
2021-01-06 11:07:57 +01:00
|
|
|
attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
|
2021-05-27 23:28:14 +02:00
|
|
|
attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
|
|
|
|
attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
|
|
|
|
attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr(
|
|
|
|
r#"export_name = "…""#,
|
|
|
|
Some("export_name"),
|
|
|
|
Some(r#"export_name = "${0:exported_symbol_name}""#),
|
|
|
|
),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
|
|
|
|
attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
|
2021-05-30 17:10:42 +02:00
|
|
|
attr("global_allocator", None, None),
|
2020-07-12 11:17:15 +03:00
|
|
|
attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
|
2021-01-18 20:49:59 +01:00
|
|
|
attr("inline", Some("inline"), Some("inline")),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("link", None, None),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
|
|
|
|
attr(
|
|
|
|
r#"link_section = "…""#,
|
|
|
|
Some("link_section"),
|
|
|
|
Some(r#"link_section = "${0:section_name}""#),
|
|
|
|
),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("macro_export", None, None),
|
|
|
|
attr("macro_use", None, None),
|
2021-01-06 12:04:23 +01:00
|
|
|
attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr("no_implicit_prelude", None, None).prefer_inner(),
|
2021-05-27 23:28:14 +02:00
|
|
|
attr("no_link", None, None).prefer_inner(),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr("no_main", None, None).prefer_inner(),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("no_mangle", None, None),
|
|
|
|
attr("no_std", None, None).prefer_inner(),
|
|
|
|
attr("non_exhaustive", None, None),
|
2021-05-30 17:10:42 +02:00
|
|
|
attr("panic_handler", None, None),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("proc_macro", None, None),
|
|
|
|
attr("proc_macro_attribute", None, None),
|
|
|
|
attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
|
2021-09-05 17:57:11 +09:00
|
|
|
attr(
|
|
|
|
r#"recursion_limit = "…""#,
|
|
|
|
Some("recursion_limit"),
|
|
|
|
Some(r#"recursion_limit = "${0:128}""#),
|
|
|
|
)
|
|
|
|
.prefer_inner(),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
|
2021-01-06 12:23:19 +01:00
|
|
|
attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr(
|
2022-06-26 00:50:41 +01:00
|
|
|
r#"target_feature(enable = "…")"#,
|
2020-07-03 15:38:20 +02:00
|
|
|
Some("target_feature"),
|
2022-06-26 00:50:41 +01:00
|
|
|
Some(r#"target_feature(enable = "${0:feature}")"#),
|
2020-07-03 15:38:20 +02:00
|
|
|
),
|
|
|
|
attr("test", None, None),
|
2020-12-05 22:23:13 +01:00
|
|
|
attr("track_caller", None, None),
|
|
|
|
attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
|
|
|
|
.prefer_inner(),
|
2020-07-03 15:38:20 +02:00
|
|
|
attr("used", None, None),
|
|
|
|
attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
|
|
|
|
attr(
|
|
|
|
r#"windows_subsystem = "…""#,
|
|
|
|
Some("windows_subsystem"),
|
|
|
|
Some(r#"windows_subsystem = "${0:subsystem}""#),
|
|
|
|
)
|
|
|
|
.prefer_inner(),
|
2020-04-23 18:22:33 +02:00
|
|
|
];
|
|
|
|
|
2021-10-17 12:44:41 +02:00
|
|
|
fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
|
|
|
|
let r_paren = input.r_paren_token()?;
|
|
|
|
let tokens = input
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.skip(1)
|
|
|
|
.take_while(|it| it.as_token() != Some(&r_paren));
|
2022-03-10 20:53:50 +01:00
|
|
|
let input_expressions = tokens.group_by(|tok| tok.kind() == T![,]);
|
2021-10-17 12:44:41 +02:00
|
|
|
Some(
|
|
|
|
input_expressions
|
|
|
|
.into_iter()
|
2022-12-30 08:30:23 +00:00
|
|
|
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
|
2021-12-28 18:57:13 +03:00
|
|
|
.filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
|
2021-10-17 12:44:41 +02:00
|
|
|
.collect::<Vec<ast::Expr>>(),
|
|
|
|
)
|
2020-05-01 03:46:17 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 16:27:01 +02:00
|
|
|
#[test]
|
|
|
|
fn attributes_are_sorted() {
|
|
|
|
let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
|
|
|
|
let mut prev = attrs.next().unwrap();
|
|
|
|
|
|
|
|
attrs.for_each(|next| {
|
|
|
|
assert!(
|
|
|
|
prev < next,
|
2022-12-30 07:59:11 +00:00
|
|
|
r#"ATTRIBUTES array is not sorted, "{prev}" should come after "{next}""#
|
2021-05-28 00:54:52 +02:00
|
|
|
);
|
2021-07-15 16:27:01 +02:00
|
|
|
prev = next;
|
|
|
|
});
|
2020-04-23 18:22:33 +02:00
|
|
|
}
|