Skip to content

Commit 571beb4

Browse files
committed
Don't use DesugaringKind to track desugared operators
See #73468 (comment) This removes the immediate need for PR #73468, since Clippy will no longer be confused by the precense of a new `ExpnId` 'on top of' a macro expansion `ExpnId`. This makes some of the 'self move diagnostics' added by PR #72389 slightly worse. I've left the operator-related diagnostics code in place, so it should be easy for a followup PR to plug in a new way of detecting desugared operators.
1 parent f455e46 commit 571beb4

File tree

10 files changed

+33
-46
lines changed

10 files changed

+33
-46
lines changed

src/librustc_ast_lowering/expr.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
2525
}
2626

2727
pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
28-
let mut span = e.span;
2928
ensure_sufficient_stack(|| {
3029
let kind = match e.kind {
3130
ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
@@ -54,7 +53,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
5453
hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args, span)
5554
}
5655
ExprKind::Binary(binop, ref lhs, ref rhs) => {
57-
span = self.mark_span_with_reason(DesugaringKind::Operator, e.span, None);
5856
let binop = self.lower_binop(binop);
5957
let lhs = self.lower_expr(lhs);
6058
let rhs = self.lower_expr(rhs);
@@ -224,7 +222,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
224222
hir::Expr {
225223
hir_id: self.lower_node_id(e.id),
226224
kind,
227-
span,
225+
span: e.span,
228226
attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into(),
229227
}
230228
})
@@ -239,7 +237,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
239237
}
240238

241239
fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
242-
let span = self.mark_span_with_reason(DesugaringKind::Operator, b.span, None);
243240
Spanned {
244241
node: match b.node {
245242
BinOpKind::Add => hir::BinOpKind::Add,
@@ -261,7 +258,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
261258
BinOpKind::Ge => hir::BinOpKind::Ge,
262259
BinOpKind::Gt => hir::BinOpKind::Gt,
263260
},
264-
span,
261+
span: b.span,
265262
}
266263
}
267264

src/librustc_middle/lint.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ pub fn struct_lint_level<'s, 'd>(
339339
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
340340
let expn_data = span.ctxt().outer_expn_data();
341341
match expn_data.kind {
342-
ExpnKind::Root
343-
| ExpnKind::Desugaring(DesugaringKind::ForLoop(_))
344-
| ExpnKind::Desugaring(DesugaringKind::Operator) => false,
342+
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => false,
345343
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
346344
ExpnKind::Macro(MacroKind::Bang, _) => {
347345
// Dummy span for the `def_site` means it's an external macro.

src/librustc_mir/borrow_check/diagnostics/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ pub(super) enum FnSelfUseKind {
568568
Normal { self_arg: Ident, implicit_into_iter: bool },
569569
/// A call to `FnOnce::call_once`, desugared from `my_closure(a, b, c)`
570570
FnOnceCall,
571-
/// A call to an operator trait, desuraged from operator syntax (e.g. `a << b`)
571+
/// A call to an operator trait, desugared from operator syntax (e.g. `a << b`)
572+
#[allow(dead_code)] // FIXME: Detect desugared operators
572573
Operator { self_arg: Ident },
573574
}
574575

@@ -823,8 +824,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
823824

824825
let kind = if is_fn_once {
825826
FnSelfUseKind::FnOnceCall
826-
} else if fn_call_span.is_desugaring(DesugaringKind::Operator) {
827-
FnSelfUseKind::Operator { self_arg }
828827
} else {
829828
debug!(
830829
"move_spans: method_did={:?}, fn_call_span={:?}",

src/librustc_span/hygiene.rs

-2
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ pub enum DesugaringKind {
823823
Async,
824824
Await,
825825
ForLoop(ForLoopLoc),
826-
Operator,
827826
}
828827

829828
/// A location in the desugaring of a `for` loop
@@ -844,7 +843,6 @@ impl DesugaringKind {
844843
DesugaringKind::TryBlock => "`try` block",
845844
DesugaringKind::OpaqueTy => "`impl Trait`",
846845
DesugaringKind::ForLoop(_) => "`for` loop",
847-
DesugaringKind::Operator => "operator",
848846
}
849847
}
850848
}

src/test/ui/binop/binop-consume-args.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0382]: use of moved value: `lhs`
44
LL | fn add<A: Add<B, Output=()>, B>(lhs: A, rhs: B) {
55
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
66
LL | lhs + rhs;
7-
| --------- `lhs` moved due to usage in operator
7+
| --------- `lhs` moved due to this method call
88
LL | drop(lhs);
99
| ^^^ value used here after move
1010
|
11-
note: calling this operator moves the left-hand side
11+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
1212
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
1313
|
1414
LL | fn add(self, rhs: Rhs) -> Self::Output;
@@ -40,11 +40,11 @@ error[E0382]: use of moved value: `lhs`
4040
LL | fn sub<A: Sub<B, Output=()>, B>(lhs: A, rhs: B) {
4141
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
4242
LL | lhs - rhs;
43-
| --------- `lhs` moved due to usage in operator
43+
| --------- `lhs` moved due to this method call
4444
LL | drop(lhs);
4545
| ^^^ value used here after move
4646
|
47-
note: calling this operator moves the left-hand side
47+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
4848
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
4949
|
5050
LL | fn sub(self, rhs: Rhs) -> Self::Output;
@@ -76,11 +76,11 @@ error[E0382]: use of moved value: `lhs`
7676
LL | fn mul<A: Mul<B, Output=()>, B>(lhs: A, rhs: B) {
7777
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
7878
LL | lhs * rhs;
79-
| --------- `lhs` moved due to usage in operator
79+
| --------- `lhs` moved due to this method call
8080
LL | drop(lhs);
8181
| ^^^ value used here after move
8282
|
83-
note: calling this operator moves the left-hand side
83+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
8484
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
8585
|
8686
LL | fn mul(self, rhs: Rhs) -> Self::Output;
@@ -112,11 +112,11 @@ error[E0382]: use of moved value: `lhs`
112112
LL | fn div<A: Div<B, Output=()>, B>(lhs: A, rhs: B) {
113113
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
114114
LL | lhs / rhs;
115-
| --------- `lhs` moved due to usage in operator
115+
| --------- `lhs` moved due to this method call
116116
LL | drop(lhs);
117117
| ^^^ value used here after move
118118
|
119-
note: calling this operator moves the left-hand side
119+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
120120
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
121121
|
122122
LL | fn div(self, rhs: Rhs) -> Self::Output;
@@ -148,11 +148,11 @@ error[E0382]: use of moved value: `lhs`
148148
LL | fn rem<A: Rem<B, Output=()>, B>(lhs: A, rhs: B) {
149149
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
150150
LL | lhs % rhs;
151-
| --------- `lhs` moved due to usage in operator
151+
| --------- `lhs` moved due to this method call
152152
LL | drop(lhs);
153153
| ^^^ value used here after move
154154
|
155-
note: calling this operator moves the left-hand side
155+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
156156
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
157157
|
158158
LL | fn rem(self, rhs: Rhs) -> Self::Output;
@@ -184,11 +184,11 @@ error[E0382]: use of moved value: `lhs`
184184
LL | fn bitand<A: BitAnd<B, Output=()>, B>(lhs: A, rhs: B) {
185185
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
186186
LL | lhs & rhs;
187-
| --------- `lhs` moved due to usage in operator
187+
| --------- `lhs` moved due to this method call
188188
LL | drop(lhs);
189189
| ^^^ value used here after move
190190
|
191-
note: calling this operator moves the left-hand side
191+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
192192
--> $SRC_DIR/libcore/ops/bit.rs:LL:COL
193193
|
194194
LL | fn bitand(self, rhs: Rhs) -> Self::Output;
@@ -220,11 +220,11 @@ error[E0382]: use of moved value: `lhs`
220220
LL | fn bitor<A: BitOr<B, Output=()>, B>(lhs: A, rhs: B) {
221221
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
222222
LL | lhs | rhs;
223-
| --------- `lhs` moved due to usage in operator
223+
| --------- `lhs` moved due to this method call
224224
LL | drop(lhs);
225225
| ^^^ value used here after move
226226
|
227-
note: calling this operator moves the left-hand side
227+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
228228
--> $SRC_DIR/libcore/ops/bit.rs:LL:COL
229229
|
230230
LL | fn bitor(self, rhs: Rhs) -> Self::Output;
@@ -256,11 +256,11 @@ error[E0382]: use of moved value: `lhs`
256256
LL | fn bitxor<A: BitXor<B, Output=()>, B>(lhs: A, rhs: B) {
257257
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
258258
LL | lhs ^ rhs;
259-
| --------- `lhs` moved due to usage in operator
259+
| --------- `lhs` moved due to this method call
260260
LL | drop(lhs);
261261
| ^^^ value used here after move
262262
|
263-
note: calling this operator moves the left-hand side
263+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
264264
--> $SRC_DIR/libcore/ops/bit.rs:LL:COL
265265
|
266266
LL | fn bitxor(self, rhs: Rhs) -> Self::Output;
@@ -292,11 +292,11 @@ error[E0382]: use of moved value: `lhs`
292292
LL | fn shl<A: Shl<B, Output=()>, B>(lhs: A, rhs: B) {
293293
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
294294
LL | lhs << rhs;
295-
| ---------- `lhs` moved due to usage in operator
295+
| ---------- `lhs` moved due to this method call
296296
LL | drop(lhs);
297297
| ^^^ value used here after move
298298
|
299-
note: calling this operator moves the left-hand side
299+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
300300
--> $SRC_DIR/libcore/ops/bit.rs:LL:COL
301301
|
302302
LL | fn shl(self, rhs: Rhs) -> Self::Output;
@@ -328,11 +328,11 @@ error[E0382]: use of moved value: `lhs`
328328
LL | fn shr<A: Shr<B, Output=()>, B>(lhs: A, rhs: B) {
329329
| --- move occurs because `lhs` has type `A`, which does not implement the `Copy` trait
330330
LL | lhs >> rhs;
331-
| ---------- `lhs` moved due to usage in operator
331+
| ---------- `lhs` moved due to this method call
332332
LL | drop(lhs);
333333
| ^^^ value used here after move
334334
|
335-
note: calling this operator moves the left-hand side
335+
note: this function consumes the receiver `self` by taking ownership of it, which moves `lhs`
336336
--> $SRC_DIR/libcore/ops/bit.rs:LL:COL
337337
|
338338
LL | fn shr(self, rhs: Rhs) -> Self::Output;

src/test/ui/binop/binop-move-semantics.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ LL | | x;
99
| | ^
1010
| | |
1111
| |_____value used here after move
12-
| `x` moved due to usage in operator
12+
| `x` moved due to this method call
1313
|
14-
note: calling this operator moves the left-hand side
14+
note: this function consumes the receiver `self` by taking ownership of it, which moves `x`
1515
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
1616
|
1717
LL | fn add(self, rhs: Rhs) -> Self::Output;

src/test/ui/consts/miri_unleashed/ptr_arith.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
static CMP: () = {
88
let x = &0 as *const _;
9-
let _v = x == x; //~ NOTE in this
9+
let _v = x == x;
1010
//~^ ERROR could not evaluate static initializer
1111
//~| NOTE pointer arithmetic or comparison
12-
//~| NOTE in this
1312
};
1413

1514
static INT_PTR_ARITH: () = unsafe {
1615
let x: usize = std::mem::transmute(&0);
17-
let _v = x + 0; //~ NOTE in this
16+
let _v = x + 0;
1817
//~^ ERROR could not evaluate static initializer
1918
//~| NOTE pointer-to-integer cast
2019
};

src/test/ui/consts/miri_unleashed/ptr_arith.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _v = x == x;
55
| ^^^^^^ "pointer arithmetic or comparison" needs an rfc before being allowed inside constants
66

77
error[E0080]: could not evaluate static initializer
8-
--> $DIR/ptr_arith.rs:17:14
8+
--> $DIR/ptr_arith.rs:16:14
99
|
1010
LL | let _v = x + 0;
1111
| ^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
@@ -18,7 +18,7 @@ help: skipping check for `const_compare_raw_pointers` feature
1818
LL | let _v = x == x;
1919
| ^^^^^^
2020
help: skipping check that does not even have a feature gate
21-
--> $DIR/ptr_arith.rs:16:20
21+
--> $DIR/ptr_arith.rs:15:20
2222
|
2323
LL | let x: usize = std::mem::transmute(&0);
2424
| ^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/hygiene/unpretty-debug.stdout

-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ fn y /* 0#0 */() { }
1818
Expansions:
1919
0: parent: ExpnId(0), call_site_ctxt: #0, kind: Root
2020
1: parent: ExpnId(0), call_site_ctxt: #0, kind: Macro(Bang, "foo")
21-
2: parent: ExpnId(0), call_site_ctxt: #1, kind: Desugaring(Operator)
22-
3: parent: ExpnId(0), call_site_ctxt: #1, kind: Desugaring(Operator)
2321

2422
SyntaxContexts:
2523
#0: parent: #0, outer_mark: (ExpnId(0), Opaque)
2624
#1: parent: #0, outer_mark: (ExpnId(1), SemiTransparent)
27-
#2: parent: #1, outer_mark: (ExpnId(2), Transparent)
28-
#3: parent: #1, outer_mark: (ExpnId(3), Transparent)
2925
*/

src/test/ui/moves/move-fn-self-receiver.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ error[E0382]: use of moved value: `foo_add`
103103
LL | let foo_add = Foo;
104104
| ------- move occurs because `foo_add` has type `Foo`, which does not implement the `Copy` trait
105105
LL | foo_add + Foo;
106-
| ------------- `foo_add` moved due to usage in operator
106+
| ------------- `foo_add` moved due to this method call
107107
LL | foo_add;
108108
| ^^^^^^^ value used here after move
109109
|
110-
note: calling this operator moves the left-hand side
110+
note: this function consumes the receiver `self` by taking ownership of it, which moves `foo_add`
111111
--> $SRC_DIR/libcore/ops/arith.rs:LL:COL
112112
|
113113
LL | fn add(self, rhs: Rhs) -> Self::Output;

0 commit comments

Comments
 (0)