Skip to content

Commit f279afb

Browse files
authored
Rollup merge of #115743 - compiler-errors:no-impls, r=davidtwco
Point out if a local trait has no implementations Slightly helps with #115741
2 parents d24f575 + 30e6cea commit f279afb

File tree

63 files changed

+658
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+658
-6
lines changed

compiler/rustc_middle/src/ty/trait_def.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ pub struct TraitImpls {
9090
}
9191

9292
impl TraitImpls {
93+
pub fn is_empty(&self) -> bool {
94+
self.blanket_impls.is_empty() && self.non_blanket_impls.is_empty()
95+
}
96+
9397
pub fn blanket_impls(&self) -> &[DefId] {
9498
self.blanket_impls.as_slice()
9599
}

compiler/rustc_trait_selection/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ trait_selection_no_value_in_rustc_on_unimplemented = this attribute must have a
4040
.label = expected value here
4141
.note = eg `#[rustc_on_unimplemented(message="foo")]`
4242
43+
trait_selection_trait_has_no_impls = this trait has no implementations, consider adding one
44+
4345
trait_selection_ty_alias_overflow = in case this is a recursive type alias, consider using a struct, enum, or union instead
4446
trait_selection_unable_to_construct_constant_value = unable to construct a constant value for the unevaluated constant {$unevaluated}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,10 +3009,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30093009
// Try to report a help message
30103010
if is_fn_trait
30113011
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
3012-
obligation.param_env,
3013-
trait_ref.self_ty(),
3014-
trait_predicate.skip_binder().polarity,
3015-
)
3012+
obligation.param_env,
3013+
trait_ref.self_ty(),
3014+
trait_predicate.skip_binder().polarity,
3015+
)
30163016
{
30173017
self.add_help_message_for_fn_trait(trait_ref, err, implemented_kind, params);
30183018
} else if !trait_ref.has_non_region_infer()
@@ -3031,6 +3031,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30313031
None,
30323032
obligation.cause.body_id,
30333033
);
3034+
} else if trait_ref.def_id().is_local()
3035+
&& self.tcx.trait_impls_of(trait_ref.def_id()).is_empty()
3036+
&& !self.tcx.trait_is_auto(trait_ref.def_id())
3037+
&& !self.tcx.trait_is_alias(trait_ref.def_id())
3038+
{
3039+
err.span_help(
3040+
self.tcx.def_span(trait_ref.def_id()),
3041+
crate::fluent_generated::trait_selection_trait_has_no_impls,
3042+
);
30343043
} else if !suggested && !unsatisfied_const {
30353044
// Can't show anything else useful, try to find similar impls.
30363045
let impl_candidates = self.find_similar_impl_candidates(*trait_predicate);
@@ -3041,7 +3050,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30413050
err,
30423051
true,
30433052
) {
3044-
self.report_similar_impl_candidates_for_root_obligation(&obligation, *trait_predicate, body_def_id, err);
3053+
self.report_similar_impl_candidates_for_root_obligation(
3054+
&obligation,
3055+
*trait_predicate,
3056+
body_def_id,
3057+
err,
3058+
);
30453059
}
30463060

30473061
self.suggest_convert_to_slice(

tests/ui/associated-consts/associated-const-array-len.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0277]: the trait bound `i32: Foo` is not satisfied
33
|
44
LL | const X: [i32; <i32 as Foo>::ID] = [0, 1, 2];
55
| ^^^ the trait `Foo` is not implemented for `i32`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/associated-const-array-len.rs:1:1
9+
|
10+
LL | trait Foo {
11+
| ^^^^^^^^^
612

713
error: aborting due to previous error
814

tests/ui/associated-consts/issue-105330.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ error[E0277]: the trait bound `Demo: TraitWAssocConst` is not satisfied
5151
LL | foo::<Demo>()();
5252
| ^^^^ the trait `TraitWAssocConst` is not implemented for `Demo`
5353
|
54+
help: this trait has no implementations, consider adding one
55+
--> $DIR/issue-105330.rs:1:1
56+
|
57+
LL | pub trait TraitWAssocConst {
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5459
note: required by a bound in `foo`
5560
--> $DIR/issue-105330.rs:11:11
5661
|
@@ -87,6 +92,11 @@ error[E0277]: the trait bound `Demo: TraitWAssocConst` is not satisfied
8792
LL | foo::<Demo>();
8893
| ^^^^ the trait `TraitWAssocConst` is not implemented for `Demo`
8994
|
95+
help: this trait has no implementations, consider adding one
96+
--> $DIR/issue-105330.rs:1:1
97+
|
98+
LL | pub trait TraitWAssocConst {
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
90100
note: required by a bound in `foo`
91101
--> $DIR/issue-105330.rs:11:11
92102
|

tests/ui/associated-types/associated-types-ICE-when-projecting-out-of-err.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0277]: the trait bound `(): Add<A>` is not satisfied
33
|
44
LL | r = r + a;
55
| ^ the trait `Add<A>` is not implemented for `()`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/associated-types-ICE-when-projecting-out-of-err.rs:15:1
9+
|
10+
LL | trait Add<RHS=Self> {
11+
| ^^^^^^^^^^^^^^^^^^^
612

713
error: aborting due to previous error
814

tests/ui/associated-types/associated-types-no-suitable-supertrait.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0277]: the trait bound `(T, U): Get` is not satisfied
33
|
44
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
55
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
9+
|
10+
LL | trait Get {
11+
| ^^^^^^^^^
612

713
error[E0277]: the trait bound `Self: Get` is not satisfied
814
--> $DIR/associated-types-no-suitable-supertrait.rs:17:40

tests/ui/associated-types/defaults-suitability.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ error[E0277]: the trait bound `(): Foo<Self>` is not satisfied
5858
LL | type Assoc: Foo<Self> = ();
5959
| ^^ the trait `Foo<Self>` is not implemented for `()`
6060
|
61+
help: this trait has no implementations, consider adding one
62+
--> $DIR/defaults-suitability.rs:27:1
63+
|
64+
LL | trait Foo<T> {
65+
| ^^^^^^^^^^^^
6166
note: required by a bound in `Bar::Assoc`
6267
--> $DIR/defaults-suitability.rs:34:17
6368
|

tests/ui/associated-types/issue-59324.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ error[E0277]: the trait bound `(): Foo` is not satisfied
4848
|
4949
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
5050
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()`
51+
|
52+
help: this trait has no implementations, consider adding one
53+
--> $DIR/issue-59324.rs:3:1
54+
|
55+
LL | pub trait Foo: NotFoo {
56+
| ^^^^^^^^^^^^^^^^^^^^^
5157

5258
error[E0277]: the trait bound `Bug: Foo` is not satisfied
5359
--> $DIR/issue-59324.rs:19:10

tests/ui/associated-types/issue-64855.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0277]: the trait bound `Bar<T>: Foo` is not satisfied
33
|
44
LL | pub struct Bar<T>(<Self as Foo>::Type) where Self: ;
55
| ^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bar<T>`
6+
|
7+
help: this trait has no implementations, consider adding one
8+
--> $DIR/issue-64855.rs:1:1
9+
|
10+
LL | pub trait Foo {
11+
| ^^^^^^^^^^^^^
612

713
error: aborting due to previous error
814

0 commit comments

Comments
 (0)