Skip to content

Commit

Permalink
Streamline attribute stitching on AST nodes.
Browse files Browse the repository at this point in the history
It can be done more concisely.
  • Loading branch information
nnethercote committed Aug 1, 2024
1 parent fe647f0 commit 2eb2ef1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
7 changes: 3 additions & 4 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ impl AttrWrapper {

/// Prepend `self.attrs` to `attrs`.
// FIXME: require passing an NT to prevent misuse of this method
pub(crate) fn prepend_to_nt_inner(self, attrs: &mut AttrVec) {
let mut self_attrs = self.attrs;
mem::swap(attrs, &mut self_attrs);
attrs.extend(self_attrs);
pub(crate) fn prepend_to_nt_inner(mut self, attrs: &mut AttrVec) {
mem::swap(attrs, &mut self.attrs);
attrs.extend(self.attrs);
}

pub fn is_empty(&self) -> bool {
Expand Down
18 changes: 7 additions & 11 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ impl<'a> Parser<'a> {
mut e: P<Expr>,
lo: Span,
) -> PResult<'a, P<Expr>> {
let res = ensure_sufficient_stack(|| {
let mut res = ensure_sufficient_stack(|| {
loop {
let has_question =
if self.prev_token.kind == TokenKind::Ident(kw::Return, IdentIsRaw::No) {
Expand Down Expand Up @@ -924,17 +924,13 @@ impl<'a> Parser<'a> {

// Stitch the list of outer attributes onto the return value. A little
// bit ugly, but the best way given the current code structure.
if attrs.is_empty() {
res
} else {
res.map(|expr| {
expr.map(|mut expr| {
attrs.extend(expr.attrs);
expr.attrs = attrs;
expr
})
})
if !attrs.is_empty()
&& let Ok(expr) = &mut res
{
mem::swap(&mut expr.attrs, &mut attrs);
expr.attrs.extend(attrs)
}
res
}

pub(super) fn parse_dot_suffix_expr(
Expand Down

0 comments on commit 2eb2ef1

Please sign in to comment.