Skip to content

Commit 7baf870

Browse files
authored
Rollup merge of rust-lang#63146 - Mark-Simulacrum:clean-attr, r=petrochenkov
Cleanup syntax::attr Mostly removing needless arguments to constructors r? @petrochenkov
2 parents 2955448 + c146344 commit 7baf870

40 files changed

+206
-538
lines changed

src/doc/unstable-book/src/language-features/plugin.md

-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ extern crate rustc_plugin;
5959
use syntax::parse::token::{self, Token};
6060
use syntax::tokenstream::TokenTree;
6161
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
62-
use syntax::ext::build::AstBuilder; // A trait for expr_usize.
6362
use syntax_pos::Span;
6463
use rustc_plugin::Registry;
6564
@@ -164,13 +163,6 @@ can continue and find further errors.
164163
To print syntax fragments for debugging, you can use `span_note` together with
165164
`syntax::print::pprust::*_to_string`.
166165

167-
The example above produced an integer literal using `AstBuilder::expr_usize`.
168-
As an alternative to the `AstBuilder` trait, `libsyntax` provides a set of
169-
quasiquote macros. They are undocumented and very rough around the edges.
170-
However, the implementation may be a good starting point for an improved
171-
quasiquote as an ordinary plugin library.
172-
173-
174166
# Lint plugins
175167

176168
Plugins can extend [Rust's lint

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5168,7 +5168,7 @@ impl<'a> LoweringContext<'a> {
51685168
let uc_nested = attr::mk_nested_word_item(uc_ident);
51695169
attr::mk_list_item(e.span, allow_ident, vec![uc_nested])
51705170
};
5171-
attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow)
5171+
attr::mk_attr_outer(allow)
51725172
};
51735173
let attrs = vec![attr];
51745174

src/librustc_metadata/decoder.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,7 @@ impl<'a, 'tcx> CrateMetadata {
980980
}
981981

982982
fn get_attributes(&self, item: &Entry<'tcx>, sess: &Session) -> Vec<ast::Attribute> {
983-
item.attributes
984-
.decode((self, sess))
985-
.map(|mut attr| {
986-
// Need new unique IDs: old thread-local IDs won't map to new threads.
987-
attr.id = attr::mk_attr_id();
988-
attr
989-
})
990-
.collect()
983+
item.attributes.decode((self, sess)).collect()
991984
}
992985

993986
// Translate a DefId from the current compilation environment to a DefId

src/libsyntax/ast.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -2104,9 +2104,7 @@ pub enum AttrStyle {
21042104
Inner,
21052105
}
21062106

2107-
#[derive(
2108-
Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialOrd, Ord, Copy,
2109-
)]
2107+
#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)]
21102108
pub struct AttrId(pub usize);
21112109

21122110
impl Idx for AttrId {
@@ -2118,6 +2116,18 @@ impl Idx for AttrId {
21182116
}
21192117
}
21202118

2119+
impl rustc_serialize::Encodable for AttrId {
2120+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2121+
s.emit_unit()
2122+
}
2123+
}
2124+
2125+
impl rustc_serialize::Decodable for AttrId {
2126+
fn decode<D: Decoder>(d: &mut D) -> Result<AttrId, D::Error> {
2127+
d.read_nil().map(|_| crate::attr::mk_attr_id())
2128+
}
2129+
}
2130+
21212131
/// Metadata associated with an item.
21222132
/// Doc-comments are promoted to attributes that have `is_sugared_doc = true`.
21232133
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]

src/libsyntax/attr/builtin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::ast::{self, Attribute, MetaItem, NestedMetaItem};
44
use crate::early_buffered_lints::BufferedEarlyLintId;
55
use crate::ext::base::ExtCtxt;
6-
use crate::ext::build::AstBuilder;
76
use crate::feature_gate::{Features, GatedCfg};
87
use crate::parse::ParseSess;
98

@@ -929,7 +928,7 @@ pub fn find_transparency(
929928
pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) {
930929
// All the built-in macro attributes are "words" at the moment.
931930
let template = AttributeTemplate { word: true, list: None, name_value_str: None };
932-
let attr = ecx.attribute(meta_item.span, meta_item.clone());
931+
let attr = ecx.attribute(meta_item.clone());
933932
check_builtin_attribute(ecx.parse_sess, &attr, name, template);
934933
}
935934

src/libsyntax/attr/mod.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ pub use builtin::*;
66
pub use IntType::*;
77
pub use ReprAttr::*;
88
pub use StabilityLevel::*;
9+
pub use crate::ast::Attribute;
910

1011
use crate::ast;
11-
use crate::ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
12+
use crate::ast::{AttrId, AttrStyle, Name, Ident, Path, PathSegment};
1213
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
1314
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
1415
use crate::mut_visit::visit_clobber;
@@ -328,13 +329,14 @@ impl Attribute {
328329
let meta = mk_name_value_item_str(
329330
Ident::with_empty_ctxt(sym::doc),
330331
dummy_spanned(Symbol::intern(&strip_doc_comment_decoration(&comment.as_str()))));
331-
let mut attr = if self.style == ast::AttrStyle::Outer {
332-
mk_attr_outer(self.span, self.id, meta)
333-
} else {
334-
mk_attr_inner(self.span, self.id, meta)
335-
};
336-
attr.is_sugared_doc = true;
337-
f(&attr)
332+
f(&Attribute {
333+
id: self.id,
334+
style: self.style,
335+
path: meta.path,
336+
tokens: meta.node.tokens(meta.span),
337+
is_sugared_doc: true,
338+
span: self.span,
339+
})
338340
} else {
339341
f(self)
340342
}
@@ -376,46 +378,36 @@ pub fn mk_attr_id() -> AttrId {
376378
AttrId(id)
377379
}
378380

379-
/// Returns an inner attribute with the given value.
380-
pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute {
381-
mk_spanned_attr_inner(span, id, item)
382-
}
383-
384381
/// Returns an inner attribute with the given value and span.
385-
pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
382+
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
386383
Attribute {
387-
id,
384+
id: mk_attr_id(),
388385
style: ast::AttrStyle::Inner,
389386
path: item.path,
390387
tokens: item.node.tokens(item.span),
391388
is_sugared_doc: false,
392-
span: sp,
389+
span: item.span,
393390
}
394391
}
395392

396-
/// Returns an outer attribute with the given value.
397-
pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute {
398-
mk_spanned_attr_outer(span, id, item)
399-
}
400-
401393
/// Returns an outer attribute with the given value and span.
402-
pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
394+
pub fn mk_attr_outer(item: MetaItem) -> Attribute {
403395
Attribute {
404-
id,
396+
id: mk_attr_id(),
405397
style: ast::AttrStyle::Outer,
406398
path: item.path,
407399
tokens: item.node.tokens(item.span),
408400
is_sugared_doc: false,
409-
span: sp,
401+
span: item.span,
410402
}
411403
}
412404

413-
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
405+
pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
414406
let style = doc_comment_style(&text.as_str());
415407
let lit_kind = LitKind::Str(text, ast::StrStyle::Cooked);
416408
let lit = Lit::from_lit_kind(lit_kind, span);
417409
Attribute {
418-
id,
410+
id: mk_attr_id(),
419411
style,
420412
path: Path::from_ident(Ident::with_empty_ctxt(sym::doc).with_span_pos(span)),
421413
tokens: MetaItemKind::NameValue(lit).tokens(span),

src/libsyntax/diagnostics/plugin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::env;
44
use crate::ast::{self, Ident, Name};
55
use crate::source_map;
66
use crate::ext::base::{ExtCtxt, MacEager, MacResult};
7-
use crate::ext::build::AstBuilder;
87
use crate::parse::token::{self, Token};
98
use crate::ptr::P;
109
use crate::symbol::kw;

0 commit comments

Comments
 (0)