Skip to content

Commit aac203f

Browse files
Rollup merge of #164744 - mejrs:attr_cleanup, r=JonathanBrouwer
some `Attribute`/`AttributeExt` cleanups r? @JonathanBrouwer
2 parents 48184ab + 5638439 commit aac203f

29 files changed

Lines changed: 157 additions & 326 deletions

File tree

‎compiler/rustc_ast/src/attr/mod.rs‎

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ impl Attribute {
7777
}
7878

7979
impl AttributeExt for Attribute {
80-
fn id(&self) -> AttrId {
81-
self.id
82-
}
83-
8480
fn value_span(&self) -> Option<Span> {
8581
match &self.kind {
8682
AttrKind::Normal(normal) => match &normal.item.args {
@@ -146,10 +142,6 @@ impl AttributeExt for Attribute {
146142
}
147143
}
148144

149-
fn span(&self) -> Span {
150-
self.span
151-
}
152-
153145
fn is_word(&self) -> bool {
154146
match &self.kind {
155147
AttrKind::Normal(normal) => matches!(normal.item.args, AttrArgs::Empty),
@@ -195,20 +187,24 @@ impl AttributeExt for Attribute {
195187
}
196188
}
197189

198-
/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
199-
/// * `///doc` returns `Some(("doc", DocFragmentKind::Sugared(CommentKind::Line)))`.
200-
/// * `/** doc */` returns `Some(("doc", DocFragmentKind::Sugared(CommentKind::Block)))`.
201-
/// * `#[doc = "doc"]` returns `Some(("doc", DocFragmentKind::Raw))`.
190+
/// Returns the documentation, its kind if this is a doc comment or a sugared doc comment
191+
/// and the span of the entire comment (or the entire attribute if raw - see the span in
192+
/// [`DocFragmentKind`] for the literal span.)
193+
/// * `///doc` returns `Some(("doc", DocFragmentKind::Sugared(CommentKind::Line)), _)`.
194+
/// * `/** doc */` returns `Some(("doc", DocFragmentKind::Sugared(CommentKind::Block), _))`.
195+
/// * `#[doc = "doc"]` returns `Some(("doc", DocFragmentKind::Raw), _)`.
202196
/// * `#[doc(...)]` returns `None`.
203-
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {
197+
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind, Span)> {
204198
match &self.kind {
205-
AttrKind::DocComment(kind, data) => Some((*data, DocFragmentKind::Sugared(*kind))),
199+
AttrKind::DocComment(kind, data) => {
200+
Some((*data, DocFragmentKind::Sugared(*kind), self.span))
201+
}
206202
AttrKind::Normal(normal)
207203
if normal.item.path == sym::doc
208204
&& let Some(value) = normal.item.value_str()
209205
&& let Some(value_span) = normal.item.value_span() =>
210206
{
211-
Some((value, DocFragmentKind::Raw(value_span)))
207+
Some((value, DocFragmentKind::Raw(value_span), self.span))
212208
}
213209
AttrKind::Normal(..) | AttrKind::Synthetic(..) => None,
214210
}
@@ -263,6 +259,14 @@ impl AttributeExt for Attribute {
263259
fn is_rustc_doc_primitive(&self) -> bool {
264260
self.has_name(sym::rustc_doc_primitive)
265261
}
262+
263+
/// Returns whether this attribute is any of the proc macro attributes.
264+
/// i.e. `proc_macro`, `proc_macro_attribute` or `proc_macro_derive`.
265+
fn is_proc_macro_attr(&self) -> bool {
266+
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
267+
.iter()
268+
.any(|kind| self.has_name(*kind))
269+
}
266270
}
267271

268272
impl Attribute {
@@ -909,9 +913,7 @@ impl MetaItemLit {
909913
}
910914
}
911915

912-
pub trait AttributeExt: Debug {
913-
fn id(&self) -> AttrId;
914-
916+
pub trait AttributeExt: Debug + 'static {
915917
/// For a single-segment attribute (i.e., `#[attr]` and not `#[path::atrr]`),
916918
/// return the name of the attribute; otherwise, returns `None`.
917919
fn name(&self) -> Option<Symbol>;
@@ -948,10 +950,6 @@ pub trait AttributeExt: Debug {
948950
fn has_any_name(&self, names: &[Symbol]) -> bool {
949951
names.iter().any(|&name| self.has_name(name))
950952
}
951-
952-
/// get the span of the entire attribute
953-
fn span(&self) -> Span;
954-
955953
/// Returns whether the attribute is a path, without any arguments.
956954
fn is_word(&self) -> bool;
957955

@@ -972,11 +970,8 @@ pub trait AttributeExt: Debug {
972970

973971
/// Returns whether this attribute is any of the proc macro attributes.
974972
/// i.e. `proc_macro`, `proc_macro_attribute` or `proc_macro_derive`.
975-
fn is_proc_macro_attr(&self) -> bool {
976-
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
977-
.iter()
978-
.any(|kind| self.has_name(*kind))
979-
}
973+
fn is_proc_macro_attr(&self) -> bool;
974+
980975
/// Returns true if this attribute is `#[automatically_deived]`.
981976
fn is_automatically_derived_attr(&self) -> bool;
982977

@@ -985,7 +980,7 @@ pub trait AttributeExt: Debug {
985980
/// * `/** doc */` returns `Some(("doc", CommentKind::Block))`.
986981
/// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
987982
/// * `#[doc(...)]` returns `None`.
988-
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)>;
983+
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind, Span)>;
989984

990985
/// Returns outer or inner if this is a doc attribute or a sugared doc
991986
/// comment, otherwise None.
@@ -1009,10 +1004,6 @@ pub trait AttributeExt: Debug {
10091004
// FIXME(fn_delegation): use function delegation instead of manually forwarding
10101005

10111006
impl Attribute {
1012-
pub fn id(&self) -> AttrId {
1013-
AttributeExt::id(self)
1014-
}
1015-
10161007
pub fn name(&self) -> Option<Symbol> {
10171008
AttributeExt::name(self)
10181009
}
@@ -1048,10 +1039,6 @@ impl Attribute {
10481039
AttributeExt::has_any_name(self, names)
10491040
}
10501041

1051-
pub fn span(&self) -> Span {
1052-
AttributeExt::span(self)
1053-
}
1054-
10551042
pub fn is_word(&self) -> bool {
10561043
AttributeExt::is_word(self)
10571044
}
@@ -1068,7 +1055,7 @@ impl Attribute {
10681055
AttributeExt::is_proc_macro_attr(self)
10691056
}
10701057

1071-
pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {
1058+
pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind, Span)> {
10721059
AttributeExt::doc_str_and_fragment_kind(self)
10731060
}
10741061
}

‎compiler/rustc_attr_ir/src/attr.rs‎

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt;
22

33
use rustc_ast::attr::AttributeExt;
44
use rustc_ast::token::DocFragmentKind;
5-
use rustc_ast::{AttrStyle, DelimArgs, MetaItemInner, MetaItemLit, ast, join_path_idents};
5+
use rustc_ast::{AttrStyle, DelimArgs, MetaItemLit, ast, join_path_idents};
66
use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
77
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
88
use rustc_macros::{Decodable, Encodable, StableHash};
@@ -117,13 +117,6 @@ pub enum Attribute {
117117
}
118118

119119
impl Attribute {
120-
pub fn get_normal_item(&self) -> &AttrItem {
121-
match &self {
122-
Attribute::Unparsed(normal) => &normal,
123-
_ => panic!("unexpected parsed attribute"),
124-
}
125-
}
126-
127120
pub fn value_lit(&self) -> Option<&MetaItemLit> {
128121
match &self {
129122
Attribute::Unparsed(n) => match n.as_ref() {
@@ -134,13 +127,6 @@ impl Attribute {
134127
}
135128
}
136129

137-
pub fn is_parsed_attr(&self) -> bool {
138-
match self {
139-
Attribute::Parsed(_) => true,
140-
Attribute::Unparsed(_) => false,
141-
}
142-
}
143-
144130
pub fn is_prefix_attr_for_suggestions(&self) -> bool {
145131
match self {
146132
Attribute::Unparsed(attr) => attr.span.desugaring_kind().is_none(),
@@ -152,14 +138,6 @@ impl Attribute {
152138
}
153139

154140
impl AttributeExt for Attribute {
155-
#[inline]
156-
fn id(&self) -> AttrId {
157-
match &self {
158-
Attribute::Unparsed(u) => u.id.attr_id,
159-
_ => panic!(),
160-
}
161-
}
162-
163141
#[inline]
164142
fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
165143
match &self {
@@ -215,18 +193,6 @@ impl AttributeExt for Attribute {
215193
}
216194
}
217195

218-
#[inline]
219-
fn span(&self) -> Span {
220-
match &self {
221-
Attribute::Unparsed(u) => u.span,
222-
// FIXME: should not be needed anymore when all attrs are parsed
223-
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
224-
Attribute::Parsed(AttributeKind::Deprecated { span, .. }) => *span,
225-
Attribute::Parsed(AttributeKind::CfgTrace(cfgs)) => cfgs[0].1,
226-
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
227-
}
228-
}
229-
230196
#[inline]
231197
fn is_word(&self) -> bool {
232198
match &self {
@@ -265,10 +231,10 @@ impl AttributeExt for Attribute {
265231
}
266232

267233
#[inline]
268-
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {
234+
fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind, Span)> {
269235
match &self {
270-
Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
271-
Some((*comment, *kind))
236+
Attribute::Parsed(AttributeKind::DocComment { kind, comment, span, .. }) => {
237+
Some((*comment, *kind, *span))
272238
}
273239
_ => None,
274240
}
@@ -307,81 +273,3 @@ impl AttributeExt for Attribute {
307273
matches!(self, Attribute::Parsed(AttributeKind::RustcDocPrimitive(..)))
308274
}
309275
}
310-
311-
// FIXME(fn_delegation): use function delegation instead of manually forwarding
312-
impl Attribute {
313-
#[inline]
314-
pub fn id(&self) -> AttrId {
315-
AttributeExt::id(self)
316-
}
317-
318-
#[inline]
319-
pub fn name(&self) -> Option<Symbol> {
320-
AttributeExt::name(self)
321-
}
322-
323-
#[inline]
324-
pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
325-
AttributeExt::meta_item_list(self)
326-
}
327-
328-
#[inline]
329-
pub fn value_str(&self) -> Option<Symbol> {
330-
AttributeExt::value_str(self)
331-
}
332-
333-
#[inline]
334-
pub fn value_span(&self) -> Option<Span> {
335-
AttributeExt::value_span(self)
336-
}
337-
338-
#[inline]
339-
pub fn path_matches(&self, name: &[Symbol]) -> bool {
340-
AttributeExt::path_matches(self, name)
341-
}
342-
343-
#[inline]
344-
pub fn is_doc_comment(&self) -> Option<Span> {
345-
AttributeExt::is_doc_comment(self)
346-
}
347-
348-
#[inline]
349-
pub fn has_name(&self, name: Symbol) -> bool {
350-
AttributeExt::has_name(self, name)
351-
}
352-
353-
#[inline]
354-
pub fn has_any_name(&self, names: &[Symbol]) -> bool {
355-
AttributeExt::has_any_name(self, names)
356-
}
357-
358-
#[inline]
359-
pub fn span(&self) -> Span {
360-
AttributeExt::span(self)
361-
}
362-
363-
#[inline]
364-
pub fn is_word(&self) -> bool {
365-
AttributeExt::is_word(self)
366-
}
367-
368-
#[inline]
369-
pub fn path(&self) -> SmallVec<[Symbol; 1]> {
370-
AttributeExt::path(self)
371-
}
372-
373-
#[inline]
374-
pub fn doc_str(&self) -> Option<Symbol> {
375-
AttributeExt::doc_str(self)
376-
}
377-
378-
#[inline]
379-
pub fn is_proc_macro_attr(&self) -> bool {
380-
AttributeExt::is_proc_macro_attr(self)
381-
}
382-
383-
#[inline]
384-
pub fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind)> {
385-
AttributeExt::doc_str_and_fragment_kind(self)
386-
}
387-
}

‎compiler/rustc_builtin_macros/src/asm.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn validate_asm_args<'a>(
6565
for arg in args {
6666
for attr in arg.attributes.0.iter() {
6767
if !matches!(attr.name(), Some(sym::cfg | sym::cfg_attr)) {
68-
ecx.dcx().emit_err(diagnostics::AsmAttributeNotSupported { span: attr.span() });
68+
ecx.dcx().emit_err(diagnostics::AsmAttributeNotSupported { span: attr.span });
6969
}
7070
}
7171

‎compiler/rustc_builtin_macros/src/eii.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ fn split_attrs(
237237
}
238238
Some(sym::eii) => unreachable!("should already be filtered out"),
239239
_ => {
240-
ecx.dcx()
241-
.emit_err(EiiAttributeNotSupported { span, attr_span: attr.span() });
240+
ecx.dcx().emit_err(EiiAttributeNotSupported { span, attr_span: attr.span });
242241
}
243242
}
244243
}

‎compiler/rustc_metadata/src/rmeta/encoder.rs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc_span::hygiene::{
4242
};
4343
use rustc_span::{
4444
ByteSymbol, ExternalSource, FileName, LocalExpnId, SourceFile, SpanData, SpanEncoder,
45-
StableSourceFileId, Symbol, SyntaxContext, bug, span_bug, sym,
45+
StableSourceFileId, Symbol, SyntaxContext, bug, span_bug,
4646
};
4747
use rustc_structures::CrateType;
4848
use tracing::{debug, instrument, trace};
@@ -949,12 +949,6 @@ fn analyze_attr(attr: &rustc_attr_ir::Attribute, state: &mut AnalyzeAttrState) -
949949
if let rustc_attr_ir::Attribute::Parsed(p) = attr
950950
&& p.encode_cross_crate() == EncodeCrossCrate::No
951951
{
952-
// Attributes not marked encode-cross-crate don't need to be encoded for downstream crates.
953-
} else if let Some(name) = attr.name()
954-
&& [sym::warn, sym::allow, sym::expect, sym::forbid, sym::deny].contains(&name)
955-
{
956-
// Lint attributes don't need to be encoded for downstream crates.
957-
// FIXME remove this when #152369 is re-merged
958952
} else if let rustc_attr_ir::Attribute::Parsed(AttributeKind::DocComment { .. }) = attr {
959953
// We keep all doc comments reachable to rustdoc because they might be "imported" into
960954
// downstream crates if they use `#[doc(inline)]` to copy an item's documentation into

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,17 +1906,6 @@ impl<'tcx> TyCtxt<'tcx> {
19061906
body
19071907
}
19081908

1909-
/// Gets all attributes with the given name.
1910-
#[deprecated = "Though there are valid usecases for this method, especially when your attribute is not a parsed attribute, usually you want to use `rustc_attr_ir::find_attr!` instead."]
1911-
pub fn get_attrs(
1912-
self,
1913-
did: impl Into<DefId>,
1914-
attr: Symbol,
1915-
) -> impl Iterator<Item = &'tcx rustc_attr_ir::Attribute> {
1916-
#[expect(deprecated)]
1917-
self.get_all_attrs(did).iter().filter(move |a: &&rustc_attr_ir::Attribute| a.has_name(attr))
1918-
}
1919-
19201909
/// Gets all attributes.
19211910
///
19221911
/// <div class="warning">
@@ -1936,19 +1925,6 @@ impl<'tcx> TyCtxt<'tcx> {
19361925
}
19371926
}
19381927

1939-
pub fn get_attrs_by_path(
1940-
self,
1941-
did: DefId,
1942-
attr: &[Symbol],
1943-
) -> impl Iterator<Item = &'tcx rustc_attr_ir::Attribute> {
1944-
let filter_fn = move |a: &&rustc_attr_ir::Attribute| a.path_matches(attr);
1945-
if let Some(did) = did.as_local() {
1946-
self.hir_attrs(self.local_def_id_to_hir_id(did)).iter().filter(filter_fn)
1947-
} else {
1948-
self.attrs_for_def(did).iter().filter(filter_fn)
1949-
}
1950-
}
1951-
19521928
/// Returns `true` if this is an `auto trait`.
19531929
pub fn trait_is_auto(self, trait_def_id: DefId) -> bool {
19541930
self.trait_def(trait_def_id).has_auto_impl

0 commit comments

Comments
 (0)