Skip to content

Commit 7280f6a

Browse files
rustc_parse: correct span on cast expr with attrs
1 parent 38d911d commit 7280f6a

File tree

1 file changed

+16
-6
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+16
-6
lines changed

compiler/rustc_parse/src/parser/expr.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ impl<'a> Parser<'a> {
245245
this.parse_assoc_expr_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
246246
})?;
247247

248-
// Make sure that the span of the parent node is larger than the span of lhs and rhs,
249-
// including the attributes.
250-
let lhs_span =
251-
lhs.attrs.iter().find(|a| a.style == AttrStyle::Outer).map_or(lhs_span, |a| a.span);
252-
let span = lhs_span.to(rhs.span);
248+
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
253249
lhs = match op {
254250
AssocOp::Add
255251
| AssocOp::Subtract
@@ -570,7 +566,11 @@ impl<'a> Parser<'a> {
570566
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
571567
) -> PResult<'a, P<Expr>> {
572568
let mk_expr = |this: &mut Self, rhs: P<Ty>| {
573-
this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs), AttrVec::new())
569+
this.mk_expr(
570+
this.mk_expr_sp(&lhs, lhs_span, rhs.span),
571+
expr_kind(lhs, rhs),
572+
AttrVec::new(),
573+
)
574574
};
575575

576576
// Save the state of the parser before parsing type normally, in case there is a
@@ -2298,4 +2298,14 @@ impl<'a> Parser<'a> {
22982298
pub(super) fn mk_expr_err(&self, span: Span) -> P<Expr> {
22992299
self.mk_expr(span, ExprKind::Err, AttrVec::new())
23002300
}
2301+
2302+
/// Create expression span ensuring the span of the parent node
2303+
/// is larger than the span of lhs and rhs, including the attributes.
2304+
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
2305+
lhs.attrs
2306+
.iter()
2307+
.find(|a| a.style == AttrStyle::Outer)
2308+
.map_or(lhs_span, |a| a.span)
2309+
.to(rhs_span)
2310+
}
23012311
}

0 commit comments

Comments
 (0)