Change:
<!-- Thank you for filing a bug report! Please provide a short summary of the bug, along with any information you feel relevant to replicating the bug. If you cannot produce a minimal reproduction case (something that would work in isolation), please provide the steps or even link to a repository that causes the problematic output to occur. --Given the following code: The current output is: <!-- The following is not always necessary. --Ideally the output should look like: <!-- If the problem is not self-explanatory, please provide a rationale for the change. --<!-- If dramatically different output is caused by small changes, consider also adding them here. If you're using the stable version of the compiler, you should also check if the bug also exists in the beta or nightly versions. The output might also be different depending on the Edition. --<!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
This could use some work, too, I think. Maybe "impl mentions type from outside the current crate" might be a sufficient replacement, though we should probably make sure that wording is compatible with current coherence rules. Though, in this case, for a non- self type like , we could then replace that^ message with something even more specific. I'm a bit skeptical of mentioning "try implementing on " when we have , since implementing something for and can have very different meanings depending on the signatures inside the trait.
Let's improve this case! claim
This was actually closer to my original error I got, it uses a type alias to create some extra confusion:
Amazing example :) thanks, I'm on it
This is a reproducible example without serde!

Discussion:
use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; <del> use rustc_errors::ErrorGuaranteed; </del> <ins> use rustc_errors::{Diagnostic, ErrorGuaranteed}; </ins> use rustc_hir as hir; use rustc_infer::infer::TyCtxtInferExt; use rustc_middle::ty::subst::GenericArgKind;

Err(err) => emit_orphan_check_error( tcx, sp, <ins> item.span, </ins> tr.path.span, trait_ref.self_ty(), impl_.self_ty.span,

fn emit_orphan_check_error<'tcx>( tcx: TyCtxt<'tcx>, sp: Span, <ins> full_impl_span: Span, </ins> trait_span: Span, self_ty: Ty<'tcx>, self_ty_span: Span,

ty::Slice(_) => (this, " because slices are always foreign"), ty::Array(..) => (this, " because arrays are always foreign"), ty::Tuple(..) => (this, " because tuples are always foreign"), <ins> ty::RawPtr(ptr_ty) => { emit_newtype_suggestion_for_raw_ptr( full_impl_span, self_ty, self_ty_span, ptr_ty, &mut err, ); (format!("`{}`", ty), " because raw pointers are always foreign") } </ins> _ => (format!("`{}`", ty), ""), }; let msg = format!("{} is not defined in the current crate{}", ty, postfix); if *is_target_ty { // Point at `D<A>` in `impl<A, B> for C<B> in D<A>`

}) } <ins> fn emit_newtype_suggestion_for_raw_ptr( full_impl_span: Span, self_ty: Ty<'_>, self_ty_span: Span, ptr_ty: &ty::TypeAndMut<'_>, diag: &mut Diagnostic, ) { if !self_ty.needs_subst() { let mut_key = if ptr_ty.mutbl == rustc_middle::mir::Mutability::Mut { "mut " } else { "" }; let msg_sugg = "consider introducing a new wrapper type".to_owned(); let sugg = vec![ ( full_impl_span.shrink_to_lo(), format!("struct WrapperType(*{}{});nn", mut_key, ptr_ty.ty), ), (self_ty_span, "WrapperType".to_owned()), ]; diag.multipart_suggestion(msg_sugg, sugg, rustc_errors::Applicability::MaybeIncorrect); } } </ins> /// Lint impls of auto traits if they are likely to have /// unsound or surprising effects on auto impls. fn lint_auto_trait_impl<'tcx>(

<ins> // Emit additional suggestion to correct the trait implementation // on a pointer use std::{fmt, marker}; struct LocalType; impl fmt::Display for *mut LocalType { //~^ ERROR only traits defined in the current crate can be implemented for arbitrary types //~| NOTE impl doesn't use only types from inside the current crate //~| NOTE `*mut LocalType` is not defined in the current crate because raw pointers are always foreign //~| NOTE define and implement a trait or new type instead //~| HELP consider introducing a new wrapper type fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "This not compile") } } impl<T> marker::Copy for *mut T { //~^ ERROR only traits defined in the current crate can be implemented for arbitrary types //~| NOTE impl doesn't use only types from inside the current crate //~| NOTE `*mut T` is not defined in the current crate because raw pointers are always foreign //~| NOTE define and implement a trait or new type instead } fn main() {} </ins>

<ins> error[E0117]: only traits defined in the current crate can be implemented for arbitrary types --> $DIR/issue-99572-impl-trait-on-pointer.rs:7:1 | LL | impl fmt::Display for *mut LocalType { | ^^^^^^^^^^^^^^^^^^^^^^-------------- | |                     | | |                     `*mut LocalType` is not defined in the current crate because raw pointers are always foreign | impl doesn't use only types from inside the current crate | = note: define and implement a trait or new type instead help: consider introducing a new wrapper type | LL + struct WrapperType(*mut LocalType); LL + LL ~ impl fmt::Display for WrapperType { | error[E0117]: only traits defined in the current crate can be implemented for arbitrary types --> $DIR/issue-99572-impl-trait-on-pointer.rs:18:1 | LL | impl<T> marker::Copy for *mut T { | ^^^^^^^^^^^^^^^^^^^^^^^^^------ | |                        | | |                        `*mut T` is not defined in the current crate because raw pointers are always foreign | impl doesn't use only types from inside the current crate | = note: define and implement a trait or new type instead error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0117`. </ins>