Make doc comments cheaper with AttrKind.
`AttrKind` is a new type with two variants, `Normal` and `DocComment`. It's a
big performance win (over 10% in some cases) because `DocComment` lets doc
comments (which are common) be represented very cheaply.
`Attribute` gets some new helper methods to ease the transition:
- `has_name()`: check if the attribute name matches a single `Symbol`; for
`DocComment` variants it succeeds if the symbol is `sym::doc`.
- `is_doc_comment()`: check if it has a `DocComment` kind.
- `{get,unwrap}_normal_item()`: extract the item from a `Normal` variant;
panic otherwise.
Fixes #60935.
This commit is contained in:
@@ -249,9 +249,11 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
|
||||
for attr in &item.attrs {
|
||||
if is_proc_macro_attr(&attr) {
|
||||
if let Some(prev_attr) = found_attr {
|
||||
let path_str = pprust::path_to_string(&attr.item.path);
|
||||
let msg = if attr.item.path.segments[0].ident.name ==
|
||||
prev_attr.item.path.segments[0].ident.name {
|
||||
let prev_item = prev_attr.get_normal_item();
|
||||
let item = attr.get_normal_item();
|
||||
let path_str = pprust::path_to_string(&item.path);
|
||||
let msg = if item.path.segments[0].ident.name ==
|
||||
prev_item.path.segments[0].ident.name {
|
||||
format!(
|
||||
"only one `#[{}]` attribute is allowed on any given function",
|
||||
path_str,
|
||||
@@ -261,7 +263,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
|
||||
"`#[{}]` and `#[{}]` attributes cannot both be applied
|
||||
to the same function",
|
||||
path_str,
|
||||
pprust::path_to_string(&prev_attr.item.path),
|
||||
pprust::path_to_string(&prev_item.path),
|
||||
)
|
||||
};
|
||||
|
||||
@@ -290,7 +292,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
|
||||
if !is_fn {
|
||||
let msg = format!(
|
||||
"the `#[{}]` attribute may only be used on bare functions",
|
||||
pprust::path_to_string(&attr.item.path),
|
||||
pprust::path_to_string(&attr.get_normal_item().path),
|
||||
);
|
||||
|
||||
self.handler.span_err(attr.span, &msg);
|
||||
@@ -304,7 +306,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
|
||||
if !self.is_proc_macro_crate {
|
||||
let msg = format!(
|
||||
"the `#[{}]` attribute is only usable with crates of the `proc-macro` crate type",
|
||||
pprust::path_to_string(&attr.item.path),
|
||||
pprust::path_to_string(&attr.get_normal_item().path),
|
||||
);
|
||||
|
||||
self.handler.span_err(attr.span, &msg);
|
||||
|
||||
Reference in New Issue
Block a user