Skip to content

Commit bb0dab1

Browse files
authored
Unrolled build for #142569
Rollup merge of #142569 - xizheyin:139253, r=davidtwco Suggest clone in user-write-code instead of inside macro Fixes #139253 Inspired by #142543 r? ````@fmease````
2 parents b56aaec + fd9c1b9 commit bb0dab1

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13021302
None => ".clone()".to_string(),
13031303
};
13041304

1305+
let span = expr.span.find_oldest_ancestor_in_same_ctxt().shrink_to_hi();
1306+
13051307
diag.span_suggestion_verbose(
1306-
expr.span.shrink_to_hi(),
1308+
span,
13071309
"consider using clone here",
13081310
suggestion,
13091311
Applicability::MachineApplicable,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[derive(Debug, Clone)]
2+
struct Struct { field: S }
3+
4+
#[derive(Debug, Clone)]
5+
struct S;
6+
7+
macro_rules! expand {
8+
($ident:ident) => { Struct { $ident } }
9+
}
10+
11+
fn test1() {
12+
let field = &S;
13+
let a: Struct = dbg!(expand!(field)); //~ ERROR mismatched types [E0308]
14+
let b: Struct = dbg!(Struct { field }); //~ ERROR mismatched types [E0308]
15+
let c: S = dbg!(field); //~ ERROR mismatched types [E0308]
16+
let c: S = dbg!(dbg!(field)); //~ ERROR mismatched types [E0308]
17+
}
18+
19+
fn main() {}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:13:34
3+
|
4+
LL | let a: Struct = dbg!(expand!(field));
5+
| ^^^^^ expected `S`, found `&S`
6+
|
7+
help: consider using clone here
8+
|
9+
LL | let a: Struct = dbg!(expand!(field: field.clone()));
10+
| +++++++++++++++
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:14:35
14+
|
15+
LL | let b: Struct = dbg!(Struct { field });
16+
| ^^^^^ expected `S`, found `&S`
17+
|
18+
help: consider using clone here
19+
|
20+
LL | let b: Struct = dbg!(Struct { field: field.clone() });
21+
| +++++++++++++++
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:15:16
25+
|
26+
LL | let c: S = dbg!(field);
27+
| ^^^^^^^^^^^ expected `S`, found `&S`
28+
|
29+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
help: consider using clone here
31+
|
32+
LL | let c: S = dbg!(field).clone();
33+
| ++++++++
34+
35+
error[E0308]: mismatched types
36+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:16:16
37+
|
38+
LL | let c: S = dbg!(dbg!(field));
39+
| ^^^^^^^^^^^^^^^^^ expected `S`, found `&S`
40+
|
41+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
help: consider using clone here
43+
|
44+
LL | let c: S = dbg!(dbg!(field)).clone();
45+
| ++++++++
46+
47+
error: aborting due to 4 previous errors
48+
49+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)