diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 264719ca56916..d4726e111ff65 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1263,7 +1263,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expr_ty: Ty<'tcx>, expected_ty: Ty<'tcx>, ) -> bool { - if let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind() + if !expr.span.in_external_macro(self.tcx.sess.source_map()) + && let ty::Ref(_, inner_ty, hir::Mutability::Not) = expr_ty.kind() && let Some(clone_trait_def) = self.tcx.lang_items().clone_trait() && expected_ty == *inner_ty && self diff --git a/tests/ui/macros/suggest-in-extern-macro-issue-139253.rs b/tests/ui/macros/suggest-in-extern-macro-issue-139253.rs new file mode 100644 index 0000000000000..1532eaf7e58e6 --- /dev/null +++ b/tests/ui/macros/suggest-in-extern-macro-issue-139253.rs @@ -0,0 +1,18 @@ +#[derive(Clone, Debug)] +struct Point { + v: T, +} + +macro_rules! local_macro { + ($val:expr $(,)?) => { + match $val { + tmp => tmp, //~ ERROR mismatched types [E0308] + } + }; +} + +fn main() { + let a: Point = dbg!(Point { v: 42 }); + let b: Point = dbg!(&a); //~ ERROR mismatched types [E0308] + let c: Point = local_macro!(&a); +} diff --git a/tests/ui/macros/suggest-in-extern-macro-issue-139253.stderr b/tests/ui/macros/suggest-in-extern-macro-issue-139253.stderr new file mode 100644 index 0000000000000..469066b3e8cac --- /dev/null +++ b/tests/ui/macros/suggest-in-extern-macro-issue-139253.stderr @@ -0,0 +1,30 @@ +error[E0308]: mismatched types + --> $DIR/suggest-in-extern-macro-issue-139253.rs:16:24 + | +LL | let b: Point = dbg!(&a); + | ^^^^^^^^ expected `Point`, found `&Point` + | + = note: expected struct `Point<_>` + found reference `&Point<_>` + = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0308]: mismatched types + --> $DIR/suggest-in-extern-macro-issue-139253.rs:9:20 + | +LL | tmp => tmp, + | ^^^ expected `Point`, found `&Point` +... +LL | let c: Point = local_macro!(&a); + | ---------------- in this macro invocation + | + = note: expected struct `Point<_>` + found reference `&Point<_>` + = note: this error originates in the macro `local_macro` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider using clone here + | +LL | tmp => tmp.clone(), + | ++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.