Skip to content

Consider operator's span when computing binop expr span #144711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,11 @@ impl<'a> Parser<'a> {
continue;
}

let op_span = op.span;
let op = op.node;
// Special cases:
if op == AssocOp::Cast {
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
lhs = self.parse_assoc_op_cast(lhs, lhs_span, op_span, ExprKind::Cast)?;
continue;
} else if let AssocOp::Range(limits) = op {
// If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
Expand All @@ -284,7 +285,7 @@ impl<'a> Parser<'a> {
this.parse_expr_assoc_with(min_prec, attrs)
})?;

let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
let span = self.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span);
lhs = match op {
AssocOp::Binary(ast_op) => {
let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs);
Expand Down Expand Up @@ -429,7 +430,7 @@ 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);
let span = self.mk_expr_sp(&lhs, lhs.span, cur_op_span, rhs_span);
let range = self.mk_range(Some(lhs), rhs, limits);
Ok(self.mk_expr(span, range))
}
Expand Down Expand Up @@ -654,10 +655,11 @@ impl<'a> Parser<'a> {
&mut self,
lhs: P<Expr>,
lhs_span: Span,
op_span: Span,
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.mk_expr(this.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span), expr_kind(lhs, rhs))
};

// Save the state of the parser before parsing type normally, in case there is a
Expand Down Expand Up @@ -4005,11 +4007,12 @@ impl<'a> Parser<'a> {

/// 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 {
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {
lhs.attrs
.iter()
.find(|a| a.style == AttrStyle::Outer)
.map_or(lhs_span, |a| a.span)
.to(op_span)
.to(rhs_span)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Met
res
} else {
// Example cases:
// - `#[foo = 1+1]`: results in `ast::ExprKind::BinOp`.
// - `#[foo = 1+1]`: results in `ast::ExprKind::Binary`.
// - `#[foo = include_str!("nonexistent-file.rs")]`:
// results in `ast::ExprKind::Err`. In that case we delay
// the error because an earlier error will have already
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/lint/wide_pointer_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,10 @@ fn main() {
{
macro_rules! cmp {
($a:tt, $b:tt) => { $a == $b }
//~^ WARN ambiguous wide pointer comparison
}

// FIXME: This lint uses some custom span combination logic.
// Rewrite it to adapt to the new metavariable span rules.
cmp!(a, b);
//~^ WARN ambiguous wide pointer comparison
}

{
Expand Down
18 changes: 10 additions & 8 deletions tests/ui/lint/wide_pointer_comparisons.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -720,18 +720,20 @@ LL + std::ptr::eq(*a, *b)
|

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> $DIR/wide_pointer_comparisons.rs:153:14
--> $DIR/wide_pointer_comparisons.rs:148:33
|
LL | ($a:tt, $b:tt) => { $a == $b }
| ^^^^^^^^
...
LL | cmp!(a, b);
| ^^^^
|
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
| ---------- in this macro invocation
|
LL | cmp!(std::ptr::addr_eq(a, b));
| ++++++++++++++++++ +
= help: use explicit `std::ptr::eq` method to compare metadata and addresses
= help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
= note: this warning originates in the macro `cmp` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> $DIR/wide_pointer_comparisons.rs:159:39
--> $DIR/wide_pointer_comparisons.rs:157:39
|
LL | ($a:ident, $b:ident) => { $a == $b }
| ^^^^^^^^
Expand All @@ -747,7 +749,7 @@ LL + ($a:ident, $b:ident) => { std::ptr::addr_eq($a, $b) }
|

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
--> $DIR/wide_pointer_comparisons.rs:169:37
--> $DIR/wide_pointer_comparisons.rs:167:37
|
LL | ($a:expr, $b:expr) => { $a == $b }
| ^^^^^^^^
Expand Down
Loading