Skip to content

Commit 18be356

Browse files
Make attr path symbols rather than idents
1 parent 2a3a62d commit 18be356

File tree

32 files changed

+112
-116
lines changed

32 files changed

+112
-116
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ impl AttributeExt for Attribute {
9696
}
9797

9898
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
99-
fn ident(&self) -> Option<Ident> {
99+
fn name(&self) -> Option<Symbol> {
100100
match &self.kind {
101101
AttrKind::Normal(normal) => {
102102
if let [ident] = &*normal.item.path.segments {
103-
Some(ident.ident)
103+
Some(ident.ident.name)
104104
} else {
105105
None
106106
}
@@ -109,9 +109,18 @@ impl AttributeExt for Attribute {
109109
}
110110
}
111111

112-
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
112+
fn symbol_path(&self) -> Option<SmallVec<[Symbol; 1]>> {
113113
match &self.kind {
114-
AttrKind::Normal(p) => Some(p.item.path.segments.iter().map(|i| i.ident).collect()),
114+
AttrKind::Normal(p) => {
115+
Some(p.item.path.segments.iter().map(|i| i.ident.name).collect())
116+
}
117+
AttrKind::DocComment(_, _) => None,
118+
}
119+
}
120+
121+
fn path_span(&self) -> Option<Span> {
122+
match &self.kind {
123+
AttrKind::Normal(attr) => Some(attr.item.path.span),
115124
AttrKind::DocComment(_, _) => None,
116125
}
117126
}
@@ -797,9 +806,7 @@ pub trait AttributeExt: Debug {
797806

798807
/// For a single-segment attribute (i.e., `#[attr]` and not `#[path::atrr]`),
799808
/// return the name of the attribute; otherwise, returns `None`.
800-
fn name(&self) -> Option<Symbol> {
801-
self.ident().map(|ident| ident.name)
802-
}
809+
fn name(&self) -> Option<Symbol>;
803810

804811
/// Get the meta item list, `#[attr(meta item list)]`
805812
fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>>;
@@ -810,9 +817,6 @@ pub trait AttributeExt: Debug {
810817
/// Gets the span of the value literal, as string, when using `#[attr = value]`
811818
fn value_span(&self) -> Option<Span>;
812819

813-
/// For a single-segment attribute, returns its ident; otherwise, returns `None`.
814-
fn ident(&self) -> Option<Ident>;
815-
816820
/// Checks whether the path of this attribute matches the name.
817821
///
818822
/// Matches one segment of the path to each element in `name`
@@ -825,7 +829,7 @@ pub trait AttributeExt: Debug {
825829

826830
#[inline]
827831
fn has_name(&self, name: Symbol) -> bool {
828-
self.ident().map(|x| x.name == name).unwrap_or(false)
832+
self.name().map(|x| x == name).unwrap_or(false)
829833
}
830834

831835
#[inline]
@@ -839,13 +843,13 @@ pub trait AttributeExt: Debug {
839843
fn is_word(&self) -> bool;
840844

841845
fn path(&self) -> SmallVec<[Symbol; 1]> {
842-
self.ident_path()
843-
.map(|i| i.into_iter().map(|i| i.name).collect())
844-
.unwrap_or(smallvec![sym::doc])
846+
self.symbol_path().unwrap_or(smallvec![sym::doc])
845847
}
846848

849+
fn path_span(&self) -> Option<Span>;
850+
847851
/// Returns None for doc comments
848-
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>>;
852+
fn symbol_path(&self) -> Option<SmallVec<[Symbol; 1]>>;
849853

850854
/// Returns the documentation if this is a doc comment or a sugared doc comment.
851855
/// * `///doc` returns `Some("doc")`.
@@ -906,10 +910,6 @@ impl Attribute {
906910
AttributeExt::value_span(self)
907911
}
908912

909-
pub fn ident(&self) -> Option<Ident> {
910-
AttributeExt::ident(self)
911-
}
912-
913913
pub fn path_matches(&self, name: &[Symbol]) -> bool {
914914
AttributeExt::path_matches(self, name)
915915
}
@@ -941,10 +941,6 @@ impl Attribute {
941941
AttributeExt::path(self)
942942
}
943943

944-
pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
945-
AttributeExt::ident_path(self)
946-
}
947-
948944
pub fn doc_str(&self) -> Option<Symbol> {
949945
AttributeExt::doc_str(self)
950946
}

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
@@ -369,13 +369,7 @@ fn parse_cfg_attr_internal<'a>(
369369
attribute.span,
370370
attribute.get_normal_item().span(),
371371
attribute.style,
372-
AttrPath {
373-
segments: attribute
374-
.ident_path()
375-
.expect("cfg_attr is not a doc comment")
376-
.into_boxed_slice(),
377-
span: attribute.span,
378-
},
372+
AttrPath { segments: attribute.path().into_boxed_slice(), span: attribute.span },
379373
Some(attribute.get_normal_item().unsafety),
380374
ParsedDescription::Attribute,
381375
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
/// Parse a single integer.

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

@@ -47,7 +47,7 @@ fn parse_cfg(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream) -> Result<CfgEntry,
4747
span,
4848
span,
4949
AttrStyle::Inner,
50-
AttrPath { segments: vec![Ident::from_str("cfg")].into_boxed_slice(), span },
50+
AttrPath { segments: vec![sym::cfg].into_boxed_slice(), span },
5151
None,
5252
ParsedDescription::Macro,
5353
span,

0 commit comments

Comments
 (0)