Skip to content

Commit 36a3ebd

Browse files
committed
fix behavior for empty impls
1 parent 0d0d9cd commit 36a3ebd

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

compiler/rustc_typeck/src/check/method/suggest.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
282282
// found when exploring `all_traits`, but we also need them to be acurate on
283283
// our suggestions (#47759).
284284
let found_assoc = |ty: Ty<'tcx>| {
285-
simplify_type(tcx, ty, TreatParams::AsPlaceholders).and_then(|simp| {
286-
tcx.incoherent_impls(simp)
287-
.iter()
288-
.find_map(|&id| self.associated_value(id, item_name))
289-
}).is_some()
285+
simplify_type(tcx, ty, TreatParams::AsPlaceholders)
286+
.and_then(|simp| {
287+
tcx.incoherent_impls(simp)
288+
.iter()
289+
.find_map(|&id| self.associated_value(id, item_name))
290+
})
291+
.is_some()
290292
};
291293
let found_candidate = candidates.next().is_some()
292294
|| found_assoc(tcx.types.i8)

compiler/rustc_typeck/src/coherence/inherent_impls.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! `tcx.inherent_impls(def_id)`). That value, however,
88
//! is computed by selecting an idea from this table.
99
10-
use rustc_errors::{pluralize, struct_span_err};
10+
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
1313
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -151,24 +151,33 @@ impl<'tcx> InherentCollect<'tcx> {
151151
const ADD_ATTR: &str =
152152
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
153153
if !self.tcx.hir().rustc_coherence_is_core() {
154-
for item in items {
155-
if !self.tcx.has_attr(item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl)
156-
{
157-
let mut err = struct_span_err!(
158-
self.tcx.sess,
159-
span,
160-
E0390,
161-
"cannot define inherent `impl` for primitive types",
162-
);
163-
164-
if self.tcx.features().rustc_attrs {
165-
err.help(INTO_CORE).span_help(item.span, ADD_ATTR);
166-
} else {
167-
err.help("consider using a trait instead");
154+
if self.tcx.features().rustc_attrs {
155+
for item in items {
156+
if !self
157+
.tcx
158+
.has_attr(item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl)
159+
{
160+
struct_span_err!(
161+
self.tcx.sess,
162+
span,
163+
E0390,
164+
"cannot define inherent `impl` for primitive types outside of `core`",
165+
)
166+
.help(INTO_CORE)
167+
.span_help(item.span, ADD_ATTR)
168+
.emit();
169+
return;
168170
}
169-
err.emit();
170-
return;
171171
}
172+
} else {
173+
struct_span_err!(
174+
self.tcx.sess,
175+
span,
176+
E0390,
177+
"cannot define inherent `impl` for primitive types",
178+
)
179+
.help("consider using an extension trait instead")
180+
.emit();
172181
}
173182
}
174183

src/test/ui/error-codes/E0390.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0390]: cannot define inherent `impl` for primitive types
44
LL | impl *mut Foo {}
55
| ^^^^^^^^
66
|
7-
= help: consider using a trait instead
7+
= help: consider using an extension trait instead
88

99
error: aborting due to previous error
1010

src/test/ui/kinds-of-primitive-impl.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ error[E0390]: cannot define inherent `impl` for primitive types
44
LL | impl u8 {
55
| ^^
66
|
7-
= help: consider using a trait instead
7+
= help: consider using an extension trait instead
88

99
error[E0390]: cannot define inherent `impl` for primitive types
1010
--> $DIR/kinds-of-primitive-impl.rs:6:6
1111
|
1212
LL | impl str {
1313
| ^^^
1414
|
15-
= help: consider using a trait instead
15+
= help: consider using an extension trait instead
1616

1717
error[E0390]: cannot define inherent `impl` for primitive types
1818
--> $DIR/kinds-of-primitive-impl.rs:12:6
1919
|
2020
LL | impl char {
2121
| ^^^^
2222
|
23-
= help: consider using a trait instead
23+
= help: consider using an extension trait instead
2424

2525
error: aborting due to 3 previous errors
2626

0 commit comments

Comments
 (0)