Skip to content
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

add suggestion when there is a impl of external trait on pointer with wrong coherence rules #99686

Merged
merged 1 commit into from
Jul 29, 2022
Merged
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
37 changes: 36 additions & 1 deletion compiler/rustc_typeck/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_errors::ErrorGuaranteed;
use rustc_errors::{Diagnostic, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::ty::subst::GenericArgKind;
Expand Down Expand Up @@ -107,6 +107,7 @@ fn do_orphan_check_impl<'tcx>(
Err(err) => emit_orphan_check_error(
tcx,
sp,
item.span,
tr.path.span,
trait_ref.self_ty(),
impl_.self_ty.span,
Expand Down Expand Up @@ -207,6 +208,7 @@ fn do_orphan_check_impl<'tcx>(
fn emit_orphan_check_error<'tcx>(
tcx: TyCtxt<'tcx>,
sp: Span,
full_impl_span: Span,
trait_span: Span,
self_ty: Ty<'tcx>,
self_ty_span: Span,
Expand Down Expand Up @@ -247,8 +249,20 @@ fn emit_orphan_check_error<'tcx>(
ty::Slice(_) => (this, " because slices are always foreign"),
vincenzopalazzo marked this conversation as resolved.
Show resolved Hide resolved
ty::Array(..) => (this, " because arrays are always foreign"),
ty::Tuple(..) => (this, " because tuples are always foreign"),
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")
}
_ => (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>`
Expand Down Expand Up @@ -330,6 +344,27 @@ fn emit_orphan_check_error<'tcx>(
})
}

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(*{}{});\n\n", mut_key, ptr_ty.ty),
),
(self_ty_span, "WrapperType".to_owned()),
];
diag.multipart_suggestion(msg_sugg, sugg, rustc_errors::Applicability::MaybeIncorrect);
}
}

/// Lint impls of auto traits if they are likely to have
/// unsound or surprising effects on auto impls.
fn lint_auto_trait_impl<'tcx>(
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/errors/issue-99572-impl-trait-on-pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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() {}
31 changes: 31 additions & 0 deletions src/test/ui/errors/issue-99572-impl-trait-on-pointer.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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`.