Skip to content

Commit bca08d8

Browse files
committed
Revert "update diagnostic for variables moved by dbg!"
This reverts commit 1374038.
1 parent cc969f3 commit bca08d8

6 files changed

Lines changed: 84 additions & 96 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
545545
}
546546

547547
// for dbg!(x) which may take ownership, suggest dbg!(&x) instead
548+
// but here we actually do not check whether the macro name is `dbg!`
549+
// so that we may extend the scope a bit larger to cover more cases
548550
fn suggest_ref_for_dbg_args(
549551
&self,
550552
body: &hir::Expr<'_>,
@@ -558,41 +560,29 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
558560
});
559561
let Some(var_info) = var_info else { return };
560562
let arg_name = var_info.name;
561-
struct MatchArgFinder<'tcx> {
562-
tcx: TyCtxt<'tcx>,
563-
move_span: Span,
563+
struct MatchArgFinder {
564+
expr_span: Span,
565+
match_arg_span: Option<Span>,
564566
arg_name: Symbol,
565-
match_arg_span: Option<Span> = None,
566567
}
567-
impl Visitor<'_> for MatchArgFinder<'_> {
568+
impl Visitor<'_> for MatchArgFinder {
568569
fn visit_expr(&mut self, e: &hir::Expr<'_>) {
569570
// dbg! is expanded into a match pattern, we need to find the right argument span
570-
if let hir::ExprKind::Match(scrutinee, ..) = &e.kind
571-
&& let hir::ExprKind::Tup(args) = scrutinee.kind
572-
&& e.span.macro_backtrace().any(|expn| {
573-
expn.macro_def_id.is_some_and(|macro_def_id| {
574-
self.tcx.is_diagnostic_item(sym::dbg_macro, macro_def_id)
575-
})
576-
})
571+
if let hir::ExprKind::Match(expr, ..) = &e.kind
572+
&& let hir::ExprKind::Path(hir::QPath::Resolved(
573+
_,
574+
path @ Path { segments: [seg], .. },
575+
)) = &expr.kind
576+
&& seg.ident.name == self.arg_name
577+
&& self.expr_span.source_callsite().contains(expr.span)
577578
{
578-
for arg in args {
579-
if let hir::ExprKind::Path(hir::QPath::Resolved(
580-
_,
581-
path @ Path { segments: [seg], .. },
582-
)) = &arg.kind
583-
&& seg.ident.name == self.arg_name
584-
&& self.move_span.source_equal(arg.span)
585-
{
586-
self.match_arg_span = Some(path.span);
587-
return;
588-
}
589-
}
579+
self.match_arg_span = Some(path.span);
590580
}
591581
hir::intravisit::walk_expr(self, e);
592582
}
593583
}
594584

595-
let mut finder = MatchArgFinder { tcx: self.infcx.tcx, move_span, arg_name, .. };
585+
let mut finder = MatchArgFinder { expr_span: move_span, match_arg_span: None, arg_name };
596586
finder.visit_expr(body);
597587
if let Some(macro_arg_span) = finder.match_arg_span {
598588
err.span_suggestion_verbose(

compiler/rustc_span/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ symbols! {
763763
custom_test_frameworks,
764764
d,
765765
d32,
766-
dbg_macro,
767766
dead_code,
768767
dealloc,
769768
debug,

src/tools/clippy/clippy_utils/src/sym.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ generate! {
199199
cx,
200200
cycle,
201201
cyclomatic_complexity,
202+
dbg_macro,
202203
de,
203204
debug_struct,
204205
deprecated_in_future,

tests/ui/borrowck/dbg-issue-120327.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
1-
//! Diagnostic test for <https://github.com/rust-lang/rust/issues/120327>: suggest borrowing
2-
//! variables passed to `dbg!` that are later used.
3-
//@ dont-require-annotations: HELP
4-
51
fn s() -> String {
62
let a = String::new();
7-
dbg!(a); //~ HELP consider borrowing instead of transferring ownership
3+
dbg!(a);
84
return a; //~ ERROR use of moved value:
95
}
106

117
fn m() -> String {
128
let a = String::new();
13-
dbg!(1, 2, a, 1, 2); //~ HELP consider borrowing instead of transferring ownership
9+
dbg!(1, 2, a, 1, 2);
1410
return a; //~ ERROR use of moved value:
1511
}
1612

1713
fn t(a: String) -> String {
1814
let b: String = "".to_string();
19-
dbg!(a, b); //~ HELP consider borrowing instead of transferring ownership
15+
dbg!(a, b);
2016
return b; //~ ERROR use of moved value:
2117
}
2218

2319
fn x(a: String) -> String {
2420
let b: String = "".to_string();
25-
dbg!(a, b); //~ HELP consider borrowing instead of transferring ownership
21+
dbg!(a, b);
2622
return a; //~ ERROR use of moved value:
2723
}
2824

29-
fn two_of_them(a: String) -> String {
30-
dbg!(a, a); //~ ERROR use of moved value
31-
//~| HELP consider borrowing instead of transferring ownership
32-
//~| HELP consider borrowing instead of transferring ownership
33-
return a; //~ ERROR use of moved value
25+
macro_rules! my_dbg {
26+
() => {
27+
eprintln!("[{}:{}:{}]", file!(), line!(), column!())
28+
};
29+
($val:expr $(,)?) => {
30+
match $val {
31+
tmp => {
32+
eprintln!("[{}:{}:{}] {} = {:#?}",
33+
file!(), line!(), column!(), stringify!($val), &tmp);
34+
tmp
35+
}
36+
}
37+
};
38+
($($val:expr),+ $(,)?) => {
39+
($(my_dbg!($val)),+,)
40+
};
41+
}
42+
43+
fn test_my_dbg() -> String {
44+
let b: String = "".to_string();
45+
my_dbg!(b, 1);
46+
return b; //~ ERROR use of moved value:
47+
}
48+
49+
fn test_not_macro() -> String {
50+
let a = String::new();
51+
let _b = match a {
52+
tmp => {
53+
eprintln!("dbg: {}", tmp);
54+
tmp
55+
}
56+
};
57+
return a; //~ ERROR use of moved value:
3458
}
3559

3660
fn get_expr(_s: String) {}
3761

38-
// The suggestion is purely syntactic; applying it here will result in a type error.
3962
fn test() {
4063
let a: String = "".to_string();
41-
let _res = get_expr(dbg!(a)); //~ HELP consider borrowing instead of transferring ownership
64+
let _res = get_expr(dbg!(a));
4265
let _l = a.len(); //~ ERROR borrow of moved value
4366
}
4467

tests/ui/borrowck/dbg-issue-120327.stderr

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0382]: use of moved value: `a`
2-
--> $DIR/dbg-issue-120327.rs:8:12
2+
--> $DIR/dbg-issue-120327.rs:4:12
33
|
44
LL | let a = String::new();
55
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
@@ -12,13 +12,9 @@ help: consider cloning the value if the performance cost is acceptable
1212
|
1313
LL | dbg!(a.clone());
1414
| ++++++++
15-
help: consider borrowing instead of transferring ownership
16-
|
17-
LL | dbg!(&a);
18-
| +
1915

2016
error[E0382]: use of moved value: `a`
21-
--> $DIR/dbg-issue-120327.rs:14:12
17+
--> $DIR/dbg-issue-120327.rs:10:12
2218
|
2319
LL | let a = String::new();
2420
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
@@ -31,13 +27,9 @@ help: consider cloning the value if the performance cost is acceptable
3127
|
3228
LL | dbg!(1, 2, a.clone(), 1, 2);
3329
| ++++++++
34-
help: consider borrowing instead of transferring ownership
35-
|
36-
LL | dbg!(1, 2, &a, 1, 2);
37-
| +
3830

3931
error[E0382]: use of moved value: `b`
40-
--> $DIR/dbg-issue-120327.rs:20:12
32+
--> $DIR/dbg-issue-120327.rs:16:12
4133
|
4234
LL | let b: String = "".to_string();
4335
| - move occurs because `b` has type `String`, which does not implement the `Copy` trait
@@ -50,13 +42,9 @@ help: consider cloning the value if the performance cost is acceptable
5042
|
5143
LL | dbg!(a, b.clone());
5244
| ++++++++
53-
help: consider borrowing instead of transferring ownership
54-
|
55-
LL | dbg!(a, &b);
56-
| +
5745

5846
error[E0382]: use of moved value: `a`
59-
--> $DIR/dbg-issue-120327.rs:26:12
47+
--> $DIR/dbg-issue-120327.rs:22:12
6048
|
6149
LL | fn x(a: String) -> String {
6250
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
@@ -70,52 +58,47 @@ help: consider cloning the value if the performance cost is acceptable
7058
|
7159
LL | dbg!(a.clone(), b);
7260
| ++++++++
73-
help: consider borrowing instead of transferring ownership
74-
|
75-
LL | dbg!(&a, b);
76-
| +
7761

78-
error[E0382]: use of moved value: `a`
79-
--> $DIR/dbg-issue-120327.rs:30:13
80-
|
81-
LL | fn two_of_them(a: String) -> String {
82-
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
83-
LL | dbg!(a, a);
84-
| - ^ value used here after move
85-
| |
86-
| value moved here
62+
error[E0382]: use of moved value: `b`
63+
--> $DIR/dbg-issue-120327.rs:46:12
8764
|
88-
help: consider cloning the value if the performance cost is acceptable
65+
LL | tmp => {
66+
| --- value moved here
67+
...
68+
LL | let b: String = "".to_string();
69+
| - move occurs because `b` has type `String`, which does not implement the `Copy` trait
70+
LL | my_dbg!(b, 1);
71+
LL | return b;
72+
| ^ value used here after move
8973
|
90-
LL | dbg!(a.clone(), a);
91-
| ++++++++
9274
help: consider borrowing instead of transferring ownership
9375
|
94-
LL | dbg!(&a, a);
95-
| +
76+
LL | my_dbg!(&b, 1);
77+
| +
78+
help: borrow this binding in the pattern to avoid moving the value
79+
|
80+
LL | ref tmp => {
81+
| +++
9682

9783
error[E0382]: use of moved value: `a`
98-
--> $DIR/dbg-issue-120327.rs:33:12
84+
--> $DIR/dbg-issue-120327.rs:57:12
9985
|
100-
LL | fn two_of_them(a: String) -> String {
101-
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
102-
LL | dbg!(a, a);
103-
| - value moved here
86+
LL | let a = String::new();
87+
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
88+
LL | let _b = match a {
89+
LL | tmp => {
90+
| --- value moved here
10491
...
10592
LL | return a;
10693
| ^ value used here after move
10794
|
108-
help: consider cloning the value if the performance cost is acceptable
109-
|
110-
LL | dbg!(a, a.clone());
111-
| ++++++++
112-
help: consider borrowing instead of transferring ownership
95+
help: borrow this binding in the pattern to avoid moving the value
11396
|
114-
LL | dbg!(a, &a);
115-
| +
97+
LL | ref tmp => {
98+
| +++
11699

117100
error[E0382]: borrow of moved value: `a`
118-
--> $DIR/dbg-issue-120327.rs:42:14
101+
--> $DIR/dbg-issue-120327.rs:65:14
119102
|
120103
LL | let a: String = "".to_string();
121104
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
@@ -128,10 +111,6 @@ help: consider cloning the value if the performance cost is acceptable
128111
|
129112
LL | let _res = get_expr(dbg!(a.clone()));
130113
| ++++++++
131-
help: consider borrowing instead of transferring ownership
132-
|
133-
LL | let _res = get_expr(dbg!(&a));
134-
| +
135114

136115
error: aborting due to 7 previous errors
137116

tests/ui/rfcs/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ LL | struct NoCopy(usize);
1616
...
1717
LL | let _ = dbg!(a);
1818
| - you could clone this value
19-
help: consider borrowing instead of transferring ownership
20-
|
21-
LL | let _ = dbg!(&a);
22-
| +
2319

2420
error: aborting due to 1 previous error
2521

0 commit comments

Comments
 (0)