Skip to content

Commit 7d1b41c

Browse files
committed
update diagnostic for variables moved by dbg!
1 parent 51816ef commit 7d1b41c

6 files changed

Lines changed: 96 additions & 84 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,6 @@ 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
550548
fn suggest_ref_for_dbg_args(
551549
&self,
552550
body: &hir::Expr<'_>,
@@ -560,29 +558,41 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
560558
});
561559
let Some(var_info) = var_info else { return };
562560
let arg_name = var_info.name;
563-
struct MatchArgFinder {
564-
expr_span: Span,
565-
match_arg_span: Option<Span>,
561+
struct MatchArgFinder<'tcx> {
562+
tcx: TyCtxt<'tcx>,
563+
move_span: Span,
566564
arg_name: Symbol,
565+
match_arg_span: Option<Span> = None,
567566
}
568-
impl Visitor<'_> for MatchArgFinder {
567+
impl Visitor<'_> for MatchArgFinder<'_> {
569568
fn visit_expr(&mut self, e: &hir::Expr<'_>) {
570569
// dbg! is expanded into a match pattern, we need to find the right argument span
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)
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+
})
578577
{
579-
self.match_arg_span = Some(path.span);
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+
}
580590
}
581591
hir::intravisit::walk_expr(self, e);
582592
}
583593
}
584594

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

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ symbols! {
746746
custom_mir,
747747
custom_test_frameworks,
748748
d32,
749+
dbg_macro,
749750
dead_code,
750751
dealloc,
751752
debug,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ generate! {
199199
cx,
200200
cycle,
201201
cyclomatic_complexity,
202-
dbg_macro,
203202
de,
204203
debug_struct,
205204
deprecated_in_future,

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

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,44 @@
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+
15
fn s() -> String {
26
let a = String::new();
3-
dbg!(a);
7+
dbg!(a); //~ HELP consider borrowing instead of transferring ownership
48
return a; //~ ERROR use of moved value:
59
}
610

711
fn m() -> String {
812
let a = String::new();
9-
dbg!(1, 2, a, 1, 2);
13+
dbg!(1, 2, a, 1, 2); //~ HELP consider borrowing instead of transferring ownership
1014
return a; //~ ERROR use of moved value:
1115
}
1216

1317
fn t(a: String) -> String {
1418
let b: String = "".to_string();
15-
dbg!(a, b);
19+
dbg!(a, b); //~ HELP consider borrowing instead of transferring ownership
1620
return b; //~ ERROR use of moved value:
1721
}
1822

1923
fn x(a: String) -> String {
2024
let b: String = "".to_string();
21-
dbg!(a, b);
25+
dbg!(a, b); //~ HELP consider borrowing instead of transferring ownership
2226
return a; //~ ERROR use of moved value:
2327
}
2428

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:
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
5834
}
5935

6036
fn get_expr(_s: String) {}
6137

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

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

Lines changed: 51 additions & 30 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:4:12
2+
--> $DIR/dbg-issue-120327.rs:8:12
33
|
44
LL | let a = String::new();
55
| - move occurs because `a` has type `String`, which does not implement the `Copy` trait
@@ -12,9 +12,13 @@ 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+
| +
1519

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

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

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

62-
error[E0382]: use of moved value: `b`
63-
--> $DIR/dbg-issue-120327.rs:46:12
78+
error[E0382]: use of moved value: `a`
79+
--> $DIR/dbg-issue-120327.rs:30:13
6480
|
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
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
7387
|
74-
help: consider borrowing instead of transferring ownership
88+
help: consider cloning the value if the performance cost is acceptable
7589
|
76-
LL | my_dbg!(&b, 1);
77-
| +
78-
help: borrow this binding in the pattern to avoid moving the value
90+
LL | dbg!(a.clone(), a);
91+
| ++++++++
92+
help: consider borrowing instead of transferring ownership
7993
|
80-
LL | ref tmp => {
81-
| +++
94+
LL | dbg!(&a, a);
95+
| +
8296

8397
error[E0382]: use of moved value: `a`
84-
--> $DIR/dbg-issue-120327.rs:57:12
98+
--> $DIR/dbg-issue-120327.rs:33:12
8599
|
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
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
91104
...
92105
LL | return a;
93106
| ^ value used here after move
94107
|
95-
help: borrow this binding in the pattern to avoid moving the value
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
96113
|
97-
LL | ref tmp => {
98-
| +++
114+
LL | dbg!(a, &a);
115+
| +
99116

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

115136
error: aborting due to 7 previous errors
116137

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ 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+
| +
1923

2024
error: aborting due to 1 previous error
2125

0 commit comments

Comments
 (0)