Skip to content

Suggest clone in user-write-code instead of inside macro #142569

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None => ".clone()".to_string(),
};

let span = expr.span.find_oldest_ancestor_in_same_ctxt().shrink_to_hi();

diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
span,
"consider using clone here",
suggestion,
Applicability::MachineApplicable,
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[derive(Debug, Clone)]
struct Struct { field: S }

#[derive(Debug, Clone)]
struct S;

macro_rules! expand {
($ident:ident) => { Struct { $ident } }
}

fn test1() {
let field = &S;
let a: Struct = dbg!(expand!(field)); //~ ERROR mismatched types [E0308]
let b: Struct = dbg!(Struct { field }); //~ ERROR mismatched types [E0308]
let c: S = dbg!(field); //~ ERROR mismatched types [E0308]
let c: S = dbg!(dbg!(field)); //~ ERROR mismatched types [E0308]
}

fn main() {}
Comment on lines +1 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the test case and as you can see from the diff of the two commits, this has no effect on the shorthand case.

But this is actually strange, for the shorthand case, expr.span doesn't come from macro expansion, is this a bug? maybe fix it in another PR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error[E0308]: mismatched types
--> $DIR/suggest-clone-in-macro-issue-139253.rs:13:34
|
LL | let a: Struct = dbg!(expand!(field));
| ^^^^^ expected `S`, found `&S`
|
help: consider using clone here
|
LL | let a: Struct = dbg!(expand!(field: field.clone()));
| +++++++++++++++

error[E0308]: mismatched types
--> $DIR/suggest-clone-in-macro-issue-139253.rs:14:35
|
LL | let b: Struct = dbg!(Struct { field });
| ^^^^^ expected `S`, found `&S`
|
help: consider using clone here
|
LL | let b: Struct = dbg!(Struct { field: field.clone() });
| +++++++++++++++

error[E0308]: mismatched types
--> $DIR/suggest-clone-in-macro-issue-139253.rs:15:16
|
LL | let c: S = dbg!(field);
| ^^^^^^^^^^^ expected `S`, found `&S`
|
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using clone here
|
LL | let c: S = dbg!(field).clone();
| ++++++++

error[E0308]: mismatched types
--> $DIR/suggest-clone-in-macro-issue-139253.rs:16:16
|
LL | let c: S = dbg!(dbg!(field));
| ^^^^^^^^^^^^^^^^^ expected `S`, found `&S`
|
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using clone here
|
LL | let c: S = dbg!(dbg!(field)).clone();
| ++++++++

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading