Disallow ambiguous attributes on expressions
This commit is contained in:
parent
6c6b3027ef
commit
5af861cf7b
15 changed files with 141 additions and 49 deletions
|
@ -327,7 +327,9 @@ impl<'a> Parser<'a> {
|
|||
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
|
||||
})?;
|
||||
|
||||
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
|
||||
self.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span);
|
||||
let span = lhs_span.to(rhs.span);
|
||||
|
||||
lhs = match op {
|
||||
AssocOp::Add
|
||||
| AssocOp::Subtract
|
||||
|
@ -426,6 +428,18 @@ impl<'a> Parser<'a> {
|
|||
});
|
||||
}
|
||||
|
||||
fn error_ambiguous_outer_attrs(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) {
|
||||
if let Some(attr) = lhs.attrs.iter().find(|a| a.style == AttrStyle::Outer) {
|
||||
self.dcx().emit_err(errors::AmbiguousOuterAttributes {
|
||||
span: attr.span.to(rhs_span),
|
||||
sugg: errors::WrapInParentheses::Expression {
|
||||
left: attr.span.shrink_to_lo(),
|
||||
right: lhs_span.shrink_to_hi(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Possibly translate the current token to an associative operator.
|
||||
/// The method does not advance the current token.
|
||||
///
|
||||
|
@ -506,7 +520,8 @@ impl<'a> Parser<'a> {
|
|||
None
|
||||
};
|
||||
let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
|
||||
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
|
||||
self.error_ambiguous_outer_attrs(&lhs, lhs.span, rhs_span);
|
||||
let span = lhs.span.to(rhs_span);
|
||||
let limits =
|
||||
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
|
||||
let range = self.mk_range(Some(lhs), rhs, limits);
|
||||
|
@ -722,7 +737,8 @@ impl<'a> Parser<'a> {
|
|||
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
|
||||
) -> PResult<'a, P<Expr>> {
|
||||
let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
|
||||
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs))
|
||||
this.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span);
|
||||
this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs))
|
||||
};
|
||||
|
||||
// Save the state of the parser before parsing type normally, in case there is a
|
||||
|
@ -3807,16 +3823,6 @@ impl<'a> Parser<'a> {
|
|||
self.mk_expr(span, ExprKind::Err(guar))
|
||||
}
|
||||
|
||||
/// Create expression span ensuring the span of the parent node
|
||||
/// is larger than the span of lhs and rhs, including the attributes.
|
||||
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
|
||||
lhs.attrs
|
||||
.iter()
|
||||
.find(|a| a.style == AttrStyle::Outer)
|
||||
.map_or(lhs_span, |a| a.span)
|
||||
.to(rhs_span)
|
||||
}
|
||||
|
||||
fn collect_tokens_for_expr(
|
||||
&mut self,
|
||||
attrs: AttrWrapper,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue