Skip to content

Commit 6b42c4e

Browse files
Make attr path symbols rather than idents
1 parent 018d269 commit 6b42c4e

File tree

21 files changed

+62
-94
lines changed

21 files changed

+62
-94
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ impl AttributeExt for Attribute {
9494
}
9595

9696
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
97-
fn ident(&self) -> Option<Ident> {
97+
fn name(&self) -> Option<Symbol> {
9898
match &self.kind {
9999
AttrKind::Normal(normal) => {
100100
if let [ident] = &*normal.item.path.segments {
101-
Some(ident.ident)
101+
Some(ident.ident.name)
102102
} else {
103103
None
104104
}
@@ -107,9 +107,11 @@ impl AttributeExt for Attribute {
107107
}
108108
}
109109

110-
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
110+
fn symbol_path(&self) -> Option<SmallVec<[Symbol; 1]>> {
111111
match &self.kind {
112-
AttrKind::Normal(p) => Some(p.item.path.segments.iter().map(|i| i.ident).collect()),
112+
AttrKind::Normal(p) => {
113+
Some(p.item.path.segments.iter().map(|i| i.ident.name).collect())
114+
}
113115
AttrKind::DocComment(_, _) => None,
114116
}
115117
}
@@ -752,9 +754,7 @@ pub trait AttributeExt: Debug {
752754

753755
/// For a single-segment attribute (i.e., `#[attr]` and not `#[path::atrr]`),
754756
/// return the name of the attribute; otherwise, returns `None`.
755-
fn name(&self) -> Option<Symbol> {
756-
self.ident().map(|ident| ident.name)
757-
}
757+
fn name(&self) -> Option<Symbol>;
758758

759759
/// Get the meta item list, `#[attr(meta item list)]`
760760
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>>;
@@ -765,9 +765,6 @@ pub trait AttributeExt: Debug {
765765
/// Gets the span of the value literal, as string, when using `#[attr = value]`
766766
fn value_span(&self) -> Option<Span>;
767767

768-
/// For a single-segment attribute, returns its ident; otherwise, returns `None`.
769-
fn ident(&self) -> Option<Ident>;
770-
771768
/// Checks whether the path of this attribute matches the name.
772769
///
773770
/// Matches one segment of the path to each element in `name`
@@ -780,7 +777,7 @@ pub trait AttributeExt: Debug {
780777

781778
#[inline]
782779
fn has_name(&self, name: Symbol) -> bool {
783-
self.ident().map(|x| x.name == name).unwrap_or(false)
780+
self.name().map(|x| x == name).unwrap_or(false)
784781
}
785782

786783
#[inline]
@@ -794,13 +791,11 @@ pub trait AttributeExt: Debug {
794791
fn is_word(&self) -> bool;
795792

796793
fn path(&self) -> SmallVec<[Symbol; 1]> {
797-
self.ident_path()
798-
.map(|i| i.into_iter().map(|i| i.name).collect())
799-
.unwrap_or(smallvec![sym::doc])
794+
self.symbol_path().unwrap_or(smallvec![sym::doc])
800795
}
801796

802797
/// Returns None for doc comments
803-
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>>;
798+
fn symbol_path(&self) -> Option<SmallVec<[Symbol; 1]>>;
804799

805800
/// Returns the documentation if this is a doc comment or a sugared doc comment.
806801
/// * `///doc` returns `Some("doc")`.
@@ -855,10 +850,6 @@ impl Attribute {
855850
AttributeExt::value_span(self)
856851
}
857852

858-
pub fn ident(&self) -> Option<Ident> {
859-
AttributeExt::ident(self)
860-
}
861-
862853
pub fn path_matches(&self, name: &[Symbol]) -> bool {
863854
AttributeExt::path_matches(self, name)
864855
}
@@ -890,10 +881,6 @@ impl Attribute {
890881
AttributeExt::path(self)
891882
}
892883

893-
pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
894-
AttributeExt::ident_path(self)
895-
}
896-
897884
pub fn doc_str(&self) -> Option<Symbol> {
898885
AttributeExt::doc_str(self)
899886
}

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a> PostExpansionVisitor<'a> {
160160

161161
impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
162162
fn visit_attribute(&mut self, attr: &ast::Attribute) {
163-
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
163+
let attr_info = attr.name().and_then(|name| BUILTIN_ATTRIBUTE_MAP.get(&name));
164164
// Check feature gates for built-in attributes.
165165
if let Some(BuiltinAttribute {
166166
gate: AttributeGate::Gated { feature, message, check, notes, .. },

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,7 @@ fn parse_cfg_attr_internal<'a>(
393393
attribute.span,
394394
attribute.get_normal_item().span(),
395395
attribute.style,
396-
AttrPath {
397-
segments: attribute
398-
.ident_path()
399-
.expect("cfg_attr is not a doc comment")
400-
.into_boxed_slice(),
401-
span: attribute.span,
402-
},
396+
AttrPath { segments: attribute.path().into_boxed_slice(), span: attribute.span },
403397
Some(attribute.get_normal_item().unsafety),
404398
ParsedDescription::Attribute,
405399
pred_span,

compiler/rustc_attr_parsing/src/attributes/cfg_select.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::attrs::CfgEntry;
77
use rustc_parse::exp;
88
use rustc_parse::parser::Parser;
99
use rustc_session::Session;
10-
use rustc_span::{ErrorGuaranteed, Ident, Span};
10+
use rustc_span::{ErrorGuaranteed, Span, sym};
1111

1212
use crate::parser::MetaItemOrLitParser;
1313
use crate::{AttributeParser, ParsedDescription, ShouldEmit, parse_cfg_entry};
@@ -59,10 +59,7 @@ pub fn parse_cfg_select(
5959
cfg_span,
6060
cfg_span,
6161
AttrStyle::Inner,
62-
AttrPath {
63-
segments: vec![Ident::from_str("cfg_select")].into_boxed_slice(),
64-
span: cfg_span,
65-
},
62+
AttrPath { segments: vec![sym::cfg_select].into_boxed_slice(), span: cfg_span },
6663
None,
6764
ParsedDescription::Macro,
6865
cfg_span,

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ pub fn parse_version(s: Symbol) -> Option<RustcVersion> {
2828
}
2929

3030
pub fn is_builtin_attr(attr: &impl AttributeExt) -> bool {
31-
attr.is_doc_comment().is_some()
32-
|| attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
31+
attr.is_doc_comment().is_some() || attr.name().is_some_and(|name| is_builtin_attr_name(name))
3332
}
3433

3534
pub fn is_doc_alias_attrs_contain_symbol<'tcx, T: AttributeExt + 'tcx>(

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct PathParser<'a>(pub Cow<'a, Path>);
3131
impl<'a> PathParser<'a> {
3232
pub fn get_attribute_path(&self) -> hir::AttrPath {
3333
AttrPath {
34-
segments: self.segments().copied().collect::<Vec<_>>().into_boxed_slice(),
34+
segments: self.segments().map(|s| s.name).collect::<Vec<_>>().into_boxed_slice(),
3535
span: self.span(),
3636
}
3737
}

compiler/rustc_attr_parsing/src/safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
2222
return;
2323
}
2424

25-
let name = (attr_path.segments.len() == 1).then_some(attr_path.segments[0].name);
25+
let name = (attr_path.segments.len() == 1).then_some(attr_path.segments[0]);
2626
if let Some(name) = name
2727
&& [sym::cfg_trace, sym::cfg_attr_trace].contains(&name)
2828
{

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
104104
let (applied, only) = allowed_targets_applied(allowed_targets, target, cx.features);
105105
let name = cx.attr_path.clone();
106106

107-
let lint = if name.segments[0].name == sym::deprecated
107+
let lint = if name.segments[0] == sym::deprecated
108108
&& ![
109109
Target::Closure,
110110
Target::Expression,

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
2727
return;
2828
}
2929

30-
let builtin_attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
30+
let builtin_attr_info = attr.name().and_then(|name| BUILTIN_ATTRIBUTE_MAP.get(&name));
3131

3232
// Check input tokens for built-in and key-value attributes.
3333
match builtin_attr_info {

compiler/rustc_builtin_macros/src/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpa
1313
use rustc_hir::AttrPath;
1414
use rustc_hir::attrs::CfgEntry;
1515
use rustc_parse::exp;
16-
use rustc_span::{ErrorGuaranteed, Ident, Span};
16+
use rustc_span::{ErrorGuaranteed, Span, sym};
1717

1818
use crate::errors;
1919

@@ -53,7 +53,7 @@ fn parse_cfg(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream) -> Result<CfgEntry,
5353
span,
5454
span,
5555
AttrStyle::Inner,
56-
AttrPath { segments: vec![Ident::from_str("cfg")].into_boxed_slice(), span },
56+
AttrPath { segments: vec![sym::cfg].into_boxed_slice(), span },
5757
None,
5858
ParsedDescription::Macro,
5959
span,

0 commit comments

Comments
 (0)