Skip to content

Commit 4daa35c

Browse files
committed
lower attr spans and inline some functions to hopefully mitigate perf regressions
1 parent 2f06527 commit 4daa35c

File tree

5 files changed

+52
-13
lines changed

5 files changed

+52
-13
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
870870
if attrs.is_empty() {
871871
&[]
872872
} else {
873-
let lowered_attrs = self.lower_attrs_vec(attrs, target_span);
873+
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));
874874

875875
debug_assert_eq!(id.owner, self.current_hir_id_owner);
876876
let ret = self.arena.alloc_from_iter(lowered_attrs);
@@ -891,7 +891,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
891891
}
892892

893893
fn lower_attrs_vec(&self, attrs: &[Attribute], target_span: Span) -> Vec<hir::Attribute> {
894-
self.attribute_parser.parse_attribute_list(attrs, target_span, OmitDoc::Lower)
894+
self.attribute_parser
895+
.parse_attribute_list(attrs, target_span, OmitDoc::Lower, |s| self.lower_span(s))
895896
}
896897

897898
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {

Diff for: compiler/rustc_attr_parsing/src/context.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'sess> AttributeParser<'sess> {
178178
parse_only: Some(sym),
179179
limit_diagnostics,
180180
}
181-
.parse_attribute_list(attrs, target_span, OmitDoc::Skip);
181+
.parse_attribute_list(attrs, target_span, OmitDoc::Skip, std::convert::identity);
182182

183183
assert!(parsed.len() <= 1);
184184

@@ -210,6 +210,8 @@ impl<'sess> AttributeParser<'sess> {
210210
attrs: &'a [ast::Attribute],
211211
target_span: Span,
212212
omit_doc: OmitDoc,
213+
214+
lower_span: impl Copy + Fn(Span) -> Span,
213215
) -> Vec<Attribute> {
214216
let mut attributes = Vec::new();
215217

@@ -242,7 +244,7 @@ impl<'sess> AttributeParser<'sess> {
242244
attributes.push(Attribute::Parsed(AttributeKind::DocComment {
243245
style: attr.style,
244246
kind: *comment_kind,
245-
span: attr.span,
247+
span: lower_span(attr.span),
246248
comment: *symbol,
247249
}))
248250
}
@@ -264,7 +266,10 @@ impl<'sess> AttributeParser<'sess> {
264266

265267
if let Some(accepts) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {
266268
for f in accepts {
267-
let cx = AcceptContext { group_cx: &group_cx, attr_span: attr.span };
269+
let cx = AcceptContext {
270+
group_cx: &group_cx,
271+
attr_span: lower_span(attr.span),
272+
};
268273

269274
f(&cx, &args)
270275
}
@@ -286,10 +291,10 @@ impl<'sess> AttributeParser<'sess> {
286291

287292
attributes.push(Attribute::Unparsed(Box::new(AttrItem {
288293
path: AttrPath::from_ast(&n.item.path),
289-
args: self.lower_attr_args(&n.item.args),
294+
args: self.lower_attr_args(&n.item.args, lower_span),
290295
id: HashIgnoredAttrId { attr_id: attr.id },
291296
style: attr.style,
292-
span: attr.span,
297+
span: lower_span(attr.span),
293298
})));
294299
}
295300
}
@@ -308,7 +313,7 @@ impl<'sess> AttributeParser<'sess> {
308313
attributes
309314
}
310315

311-
fn lower_attr_args(&self, args: &ast::AttrArgs) -> AttrArgs {
316+
fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
312317
match args {
313318
ast::AttrArgs::Empty => AttrArgs::Empty,
314319
ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(DelimArgs {
@@ -323,7 +328,8 @@ impl<'sess> AttributeParser<'sess> {
323328
// In valid code the value always ends up as a single literal. Otherwise, a dummy
324329
// literal suffices because the error is handled elsewhere.
325330
let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
326-
&& let Ok(lit) = ast::MetaItemLit::from_token_lit(token_lit, expr.span)
331+
&& let Ok(lit) =
332+
ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
327333
{
328334
lit
329335
} else {
@@ -335,7 +341,7 @@ impl<'sess> AttributeParser<'sess> {
335341
span: DUMMY_SP,
336342
}
337343
};
338-
AttrArgs::Eq { eq_span: *eq_span, expr: lit }
344+
AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
339345
}
340346
}
341347
}

Diff for: compiler/rustc_hir/src/hir.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1093,13 +1093,15 @@ impl Attribute {
10931093
}
10941094

10951095
impl AttributeExt for Attribute {
1096+
#[inline]
10961097
fn id(&self) -> AttrId {
10971098
match &self {
10981099
Attribute::Unparsed(u) => u.id.attr_id,
10991100
_ => panic!(),
11001101
}
11011102
}
11021103

1104+
#[inline]
11031105
fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
11041106
match &self {
11051107
Attribute::Unparsed(n) => match n.as_ref() {
@@ -1112,15 +1114,18 @@ impl AttributeExt for Attribute {
11121114
}
11131115
}
11141116

1117+
#[inline]
11151118
fn value_str(&self) -> Option<Symbol> {
11161119
self.value_lit().and_then(|x| x.value_str())
11171120
}
11181121

1122+
#[inline]
11191123
fn value_span(&self) -> Option<Span> {
11201124
self.value_lit().map(|i| i.span)
11211125
}
11221126

11231127
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
1128+
#[inline]
11241129
fn ident(&self) -> Option<Ident> {
11251130
match &self {
11261131
Attribute::Unparsed(n) => {
@@ -1134,6 +1139,7 @@ impl AttributeExt for Attribute {
11341139
}
11351140
}
11361141

1142+
#[inline]
11371143
fn path_matches(&self, name: &[Symbol]) -> bool {
11381144
match &self {
11391145
Attribute::Unparsed(n) => {
@@ -1144,11 +1150,12 @@ impl AttributeExt for Attribute {
11441150
}
11451151
}
11461152

1153+
#[inline]
11471154
fn is_doc_comment(&self) -> bool {
1148-
// FIXME(jdonszelmann): make the 2nd check unnecessary here
11491155
matches!(self, Attribute::Parsed(AttributeKind::DocComment { .. }))
11501156
}
11511157

1158+
#[inline]
11521159
fn span(&self) -> Span {
11531160
match &self {
11541161
Attribute::Unparsed(u) => u.span,
@@ -1159,6 +1166,7 @@ impl AttributeExt for Attribute {
11591166
}
11601167
}
11611168

1169+
#[inline]
11621170
fn is_word(&self) -> bool {
11631171
match &self {
11641172
Attribute::Unparsed(n) => {
@@ -1168,20 +1176,23 @@ impl AttributeExt for Attribute {
11681176
}
11691177
}
11701178

1179+
#[inline]
11711180
fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
11721181
match &self {
11731182
Attribute::Unparsed(n) => Some(n.path.segments.iter().copied().collect()),
11741183
_ => None,
11751184
}
11761185
}
11771186

1187+
#[inline]
11781188
fn doc_str(&self) -> Option<Symbol> {
11791189
match &self {
11801190
Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment),
11811191
Attribute::Unparsed(_) if self.has_name(sym::doc) => self.value_str(),
11821192
_ => None,
11831193
}
11841194
}
1195+
#[inline]
11851196
fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
11861197
match &self {
11871198
Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
@@ -1194,6 +1205,7 @@ impl AttributeExt for Attribute {
11941205
}
11951206
}
11961207

1208+
#[inline]
11971209
fn style(&self) -> AttrStyle {
11981210
match &self {
11991211
Attribute::Unparsed(u) => u.style,
@@ -1205,34 +1217,42 @@ impl AttributeExt for Attribute {
12051217

12061218
// FIXME(fn_delegation): use function delegation instead of manually forwarding
12071219
impl Attribute {
1220+
#[inline]
12081221
pub fn id(&self) -> AttrId {
12091222
AttributeExt::id(self)
12101223
}
12111224

1225+
#[inline]
12121226
pub fn name_or_empty(&self) -> Symbol {
12131227
AttributeExt::name_or_empty(self)
12141228
}
12151229

1230+
#[inline]
12161231
pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
12171232
AttributeExt::meta_item_list(self)
12181233
}
12191234

1235+
#[inline]
12201236
pub fn value_str(&self) -> Option<Symbol> {
12211237
AttributeExt::value_str(self)
12221238
}
12231239

1240+
#[inline]
12241241
pub fn value_span(&self) -> Option<Span> {
12251242
AttributeExt::value_span(self)
12261243
}
12271244

1245+
#[inline]
12281246
pub fn ident(&self) -> Option<Ident> {
12291247
AttributeExt::ident(self)
12301248
}
12311249

1250+
#[inline]
12321251
pub fn path_matches(&self, name: &[Symbol]) -> bool {
12331252
AttributeExt::path_matches(self, name)
12341253
}
12351254

1255+
#[inline]
12361256
pub fn is_doc_comment(&self) -> bool {
12371257
AttributeExt::is_doc_comment(self)
12381258
}
@@ -1242,34 +1262,42 @@ impl Attribute {
12421262
AttributeExt::has_name(self, name)
12431263
}
12441264

1265+
#[inline]
12451266
pub fn span(&self) -> Span {
12461267
AttributeExt::span(self)
12471268
}
12481269

1270+
#[inline]
12491271
pub fn is_word(&self) -> bool {
12501272
AttributeExt::is_word(self)
12511273
}
12521274

1275+
#[inline]
12531276
pub fn path(&self) -> SmallVec<[Symbol; 1]> {
12541277
AttributeExt::path(self)
12551278
}
12561279

1280+
#[inline]
12571281
pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
12581282
AttributeExt::ident_path(self)
12591283
}
12601284

1285+
#[inline]
12611286
pub fn doc_str(&self) -> Option<Symbol> {
12621287
AttributeExt::doc_str(self)
12631288
}
12641289

1290+
#[inline]
12651291
pub fn is_proc_macro_attr(&self) -> bool {
12661292
AttributeExt::is_proc_macro_attr(self)
12671293
}
12681294

1295+
#[inline]
12691296
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
12701297
AttributeExt::doc_str_and_comment_kind(self)
12711298
}
12721299

1300+
#[inline]
12731301
pub fn style(&self) -> AttrStyle {
12741302
AttributeExt::style(self)
12751303
}

Diff for: compiler/rustc_passes/src/check_attr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
27322732
continue;
27332733
};
27342734

2735-
27362735
let item = tcx
27372736
.hir_free_items()
27382737
.map(|id| tcx.hir_item(id))

Diff for: compiler/rustc_resolve/src/def_collector.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
142142
self.resolver.tcx.features(),
143143
Vec::new(),
144144
);
145-
let attrs = parser.parse_attribute_list(&i.attrs, i.span, OmitDoc::Skip);
145+
let attrs = parser.parse_attribute_list(
146+
&i.attrs,
147+
i.span,
148+
OmitDoc::Skip,
149+
std::convert::identity,
150+
);
146151

147152
let macro_data =
148153
self.resolver.compile_macro(def, i.ident, &attrs, i.span, i.id, edition);

0 commit comments

Comments
 (0)