Skip to content

Commit

Permalink
lower attr spans and inline some functions to hopefully mitigate perf…
Browse files Browse the repository at this point in the history
… regressions
  • Loading branch information
jdonszelmann committed Feb 24, 2025
1 parent 2f06527 commit 4daa35c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
if attrs.is_empty() {
&[]
} else {
let lowered_attrs = self.lower_attrs_vec(attrs, target_span);
let lowered_attrs = self.lower_attrs_vec(attrs, self.lower_span(target_span));

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

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

fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
Expand Down
22 changes: 14 additions & 8 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'sess> AttributeParser<'sess> {
parse_only: Some(sym),
limit_diagnostics,
}
.parse_attribute_list(attrs, target_span, OmitDoc::Skip);
.parse_attribute_list(attrs, target_span, OmitDoc::Skip, std::convert::identity);

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

Expand Down Expand Up @@ -210,6 +210,8 @@ impl<'sess> AttributeParser<'sess> {
attrs: &'a [ast::Attribute],
target_span: Span,
omit_doc: OmitDoc,

lower_span: impl Copy + Fn(Span) -> Span,
) -> Vec<Attribute> {
let mut attributes = Vec::new();

Expand Down Expand Up @@ -242,7 +244,7 @@ impl<'sess> AttributeParser<'sess> {
attributes.push(Attribute::Parsed(AttributeKind::DocComment {
style: attr.style,
kind: *comment_kind,
span: attr.span,
span: lower_span(attr.span),
comment: *symbol,
}))
}
Expand All @@ -264,7 +266,10 @@ impl<'sess> AttributeParser<'sess> {

if let Some(accepts) = ATTRIBUTE_MAPPING.0.get(parts.as_slice()) {
for f in accepts {
let cx = AcceptContext { group_cx: &group_cx, attr_span: attr.span };
let cx = AcceptContext {
group_cx: &group_cx,
attr_span: lower_span(attr.span),
};

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

attributes.push(Attribute::Unparsed(Box::new(AttrItem {
path: AttrPath::from_ast(&n.item.path),
args: self.lower_attr_args(&n.item.args),
args: self.lower_attr_args(&n.item.args, lower_span),
id: HashIgnoredAttrId { attr_id: attr.id },
style: attr.style,
span: attr.span,
span: lower_span(attr.span),
})));
}
}
Expand All @@ -308,7 +313,7 @@ impl<'sess> AttributeParser<'sess> {
attributes
}

fn lower_attr_args(&self, args: &ast::AttrArgs) -> AttrArgs {
fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
match args {
ast::AttrArgs::Empty => AttrArgs::Empty,
ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(DelimArgs {
Expand All @@ -323,7 +328,8 @@ impl<'sess> AttributeParser<'sess> {
// In valid code the value always ends up as a single literal. Otherwise, a dummy
// literal suffices because the error is handled elsewhere.
let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
&& let Ok(lit) = ast::MetaItemLit::from_token_lit(token_lit, expr.span)
&& let Ok(lit) =
ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
{
lit
} else {
Expand All @@ -335,7 +341,7 @@ impl<'sess> AttributeParser<'sess> {
span: DUMMY_SP,
}
};
AttrArgs::Eq { eq_span: *eq_span, expr: lit }
AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,13 +1093,15 @@ impl Attribute {
}

impl AttributeExt for Attribute {
#[inline]
fn id(&self) -> AttrId {
match &self {
Attribute::Unparsed(u) => u.id.attr_id,
_ => panic!(),
}
}

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

#[inline]
fn value_str(&self) -> Option<Symbol> {
self.value_lit().and_then(|x| x.value_str())
}

#[inline]
fn value_span(&self) -> Option<Span> {
self.value_lit().map(|i| i.span)
}

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

#[inline]
fn path_matches(&self, name: &[Symbol]) -> bool {
match &self {
Attribute::Unparsed(n) => {
Expand All @@ -1144,11 +1150,12 @@ impl AttributeExt for Attribute {
}
}

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

#[inline]
fn span(&self) -> Span {
match &self {
Attribute::Unparsed(u) => u.span,
Expand All @@ -1159,6 +1166,7 @@ impl AttributeExt for Attribute {
}
}

#[inline]
fn is_word(&self) -> bool {
match &self {
Attribute::Unparsed(n) => {
Expand All @@ -1168,20 +1176,23 @@ impl AttributeExt for Attribute {
}
}

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

#[inline]
fn doc_str(&self) -> Option<Symbol> {
match &self {
Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment),
Attribute::Unparsed(_) if self.has_name(sym::doc) => self.value_str(),
_ => None,
}
}
#[inline]
fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
match &self {
Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
Expand All @@ -1194,6 +1205,7 @@ impl AttributeExt for Attribute {
}
}

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

// FIXME(fn_delegation): use function delegation instead of manually forwarding
impl Attribute {
#[inline]
pub fn id(&self) -> AttrId {
AttributeExt::id(self)
}

#[inline]
pub fn name_or_empty(&self) -> Symbol {
AttributeExt::name_or_empty(self)
}

#[inline]
pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
AttributeExt::meta_item_list(self)
}

#[inline]
pub fn value_str(&self) -> Option<Symbol> {
AttributeExt::value_str(self)
}

#[inline]
pub fn value_span(&self) -> Option<Span> {
AttributeExt::value_span(self)
}

#[inline]
pub fn ident(&self) -> Option<Ident> {
AttributeExt::ident(self)
}

#[inline]
pub fn path_matches(&self, name: &[Symbol]) -> bool {
AttributeExt::path_matches(self, name)
}

#[inline]
pub fn is_doc_comment(&self) -> bool {
AttributeExt::is_doc_comment(self)
}
Expand All @@ -1242,34 +1262,42 @@ impl Attribute {
AttributeExt::has_name(self, name)
}

#[inline]
pub fn span(&self) -> Span {
AttributeExt::span(self)
}

#[inline]
pub fn is_word(&self) -> bool {
AttributeExt::is_word(self)
}

#[inline]
pub fn path(&self) -> SmallVec<[Symbol; 1]> {
AttributeExt::path(self)
}

#[inline]
pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
AttributeExt::ident_path(self)
}

#[inline]
pub fn doc_str(&self) -> Option<Symbol> {
AttributeExt::doc_str(self)
}

#[inline]
pub fn is_proc_macro_attr(&self) -> bool {
AttributeExt::is_proc_macro_attr(self)
}

#[inline]
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
AttributeExt::doc_str_and_comment_kind(self)
}

#[inline]
pub fn style(&self) -> AttrStyle {
AttributeExt::style(self)
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2732,7 +2732,6 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
continue;
};


let item = tcx
.hir_free_items()
.map(|id| tcx.hir_item(id))
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
self.resolver.tcx.features(),
Vec::new(),
);
let attrs = parser.parse_attribute_list(&i.attrs, i.span, OmitDoc::Skip);
let attrs = parser.parse_attribute_list(
&i.attrs,
i.span,
OmitDoc::Skip,
std::convert::identity,
);

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

0 comments on commit 4daa35c

Please sign in to comment.