fix behavior for empty impls

This commit is contained in:
lcnr 2022-03-18 11:44:55 +01:00
parent 0d0d9cd718
commit 36a3ebde96
4 changed files with 37 additions and 26 deletions

View File

@ -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)

View File

@ -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();
}
}

View File

@ -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

View File

@ -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