diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index cc4707b6b72..e1d6b5d2bd4 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -282,11 +282,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // found when exploring `all_traits`, but we also need them to be acurate on // our suggestions (#47759). let found_assoc = |ty: Ty<'tcx>| { - simplify_type(tcx, ty, TreatParams::AsPlaceholders).and_then(|simp| { - tcx.incoherent_impls(simp) - .iter() - .find_map(|&id| self.associated_value(id, item_name)) - }).is_some() + simplify_type(tcx, ty, TreatParams::AsPlaceholders) + .and_then(|simp| { + tcx.incoherent_impls(simp) + .iter() + .find_map(|&id| self.associated_value(id, item_name)) + }) + .is_some() }; let found_candidate = candidates.next().is_some() || found_assoc(tcx.types.i8) diff --git a/compiler/rustc_typeck/src/coherence/inherent_impls.rs b/compiler/rustc_typeck/src/coherence/inherent_impls.rs index 573d072da4b..e7f9ad91172 100644 --- a/compiler/rustc_typeck/src/coherence/inherent_impls.rs +++ b/compiler/rustc_typeck/src/coherence/inherent_impls.rs @@ -7,7 +7,7 @@ //! `tcx.inherent_impls(def_id)`). That value, however, //! is computed by selecting an idea from this table. -use rustc_errors::{pluralize, struct_span_err}; +use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; use rustc_hir::itemlikevisit::ItemLikeVisitor; @@ -151,24 +151,33 @@ impl<'tcx> InherentCollect<'tcx> { const ADD_ATTR: &str = "alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items"; if !self.tcx.hir().rustc_coherence_is_core() { - for item in items { - if !self.tcx.has_attr(item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl) - { - let mut err = struct_span_err!( - self.tcx.sess, - span, - E0390, - "cannot define inherent `impl` for primitive types", - ); - - if self.tcx.features().rustc_attrs { - err.help(INTO_CORE).span_help(item.span, ADD_ATTR); - } else { - err.help("consider using a trait instead"); + if self.tcx.features().rustc_attrs { + for item in items { + if !self + .tcx + .has_attr(item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl) + { + struct_span_err!( + self.tcx.sess, + span, + E0390, + "cannot define inherent `impl` for primitive types outside of `core`", + ) + .help(INTO_CORE) + .span_help(item.span, ADD_ATTR) + .emit(); + return; } - err.emit(); - return; } + } else { + struct_span_err!( + self.tcx.sess, + span, + E0390, + "cannot define inherent `impl` for primitive types", + ) + .help("consider using an extension trait instead") + .emit(); } } diff --git a/src/test/ui/error-codes/E0390.stderr b/src/test/ui/error-codes/E0390.stderr index 4a3f4660070..e635d4ec196 100644 --- a/src/test/ui/error-codes/E0390.stderr +++ b/src/test/ui/error-codes/E0390.stderr @@ -4,7 +4,7 @@ error[E0390]: cannot define inherent `impl` for primitive types LL | impl *mut Foo {} | ^^^^^^^^ | - = help: consider using a trait instead + = help: consider using an extension trait instead error: aborting due to previous error diff --git a/src/test/ui/kinds-of-primitive-impl.stderr b/src/test/ui/kinds-of-primitive-impl.stderr index c49396a53a8..c788317d786 100644 --- a/src/test/ui/kinds-of-primitive-impl.stderr +++ b/src/test/ui/kinds-of-primitive-impl.stderr @@ -4,7 +4,7 @@ error[E0390]: cannot define inherent `impl` for primitive types LL | impl u8 { | ^^ | - = help: consider using a trait instead + = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types --> $DIR/kinds-of-primitive-impl.rs:6:6 @@ -12,7 +12,7 @@ error[E0390]: cannot define inherent `impl` for primitive types LL | impl str { | ^^^ | - = help: consider using a trait instead + = help: consider using an extension trait instead error[E0390]: cannot define inherent `impl` for primitive types --> $DIR/kinds-of-primitive-impl.rs:12:6 @@ -20,7 +20,7 @@ error[E0390]: cannot define inherent `impl` for primitive types LL | impl char { | ^^^^ | - = help: consider using a trait instead + = help: consider using an extension trait instead error: aborting due to 3 previous errors