Skip to content

Commit

Permalink
Rollup merge of rust-lang#115743 - compiler-errors:no-impls, r=davidtwco
Browse files Browse the repository at this point in the history
Point out if a local trait has no implementations

Slightly helps with rust-lang#115741
  • Loading branch information
Dylan-DPC authored Sep 11, 2023
2 parents b829a5f + 30e6cea commit 7270f85
Show file tree
Hide file tree
Showing 63 changed files with 658 additions and 6 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ pub struct TraitImpls {
}

impl TraitImpls {
pub fn is_empty(&self) -> bool {
self.blanket_impls.is_empty() && self.non_blanket_impls.is_empty()
}

pub fn blanket_impls(&self) -> &[DefId] {
self.blanket_impls.as_slice()
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ trait_selection_no_value_in_rustc_on_unimplemented = this attribute must have a
.label = expected value here
.note = eg `#[rustc_on_unimplemented(message="foo")]`
trait_selection_trait_has_no_impls = this trait has no implementations, consider adding one
trait_selection_ty_alias_overflow = in case this is a recursive type alias, consider using a struct, enum, or union instead
trait_selection_unable_to_construct_constant_value = unable to construct a constant value for the unevaluated constant {$unevaluated}
24 changes: 19 additions & 5 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3009,10 +3009,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Try to report a help message
if is_fn_trait
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
obligation.param_env,
trait_ref.self_ty(),
trait_predicate.skip_binder().polarity,
)
obligation.param_env,
trait_ref.self_ty(),
trait_predicate.skip_binder().polarity,
)
{
self.add_help_message_for_fn_trait(trait_ref, err, implemented_kind, params);
} else if !trait_ref.has_non_region_infer()
Expand All @@ -3031,6 +3031,15 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
None,
obligation.cause.body_id,
);
} else if trait_ref.def_id().is_local()
&& self.tcx.trait_impls_of(trait_ref.def_id()).is_empty()
&& !self.tcx.trait_is_auto(trait_ref.def_id())
&& !self.tcx.trait_is_alias(trait_ref.def_id())
{
err.span_help(
self.tcx.def_span(trait_ref.def_id()),
crate::fluent_generated::trait_selection_trait_has_no_impls,
);
} else if !suggested && !unsatisfied_const {
// Can't show anything else useful, try to find similar impls.
let impl_candidates = self.find_similar_impl_candidates(*trait_predicate);
Expand All @@ -3041,7 +3050,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
err,
true,
) {
self.report_similar_impl_candidates_for_root_obligation(&obligation, *trait_predicate, body_def_id, err);
self.report_similar_impl_candidates_for_root_obligation(
&obligation,
*trait_predicate,
body_def_id,
err,
);
}

self.suggest_convert_to_slice(
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/associated-consts/associated-const-array-len.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `i32: Foo` is not satisfied
|
LL | const X: [i32; <i32 as Foo>::ID] = [0, 1, 2];
| ^^^ the trait `Foo` is not implemented for `i32`
|
help: this trait has no implementations, consider adding one
--> $DIR/associated-const-array-len.rs:1:1
|
LL | trait Foo {
| ^^^^^^^^^

error: aborting due to previous error

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/associated-consts/issue-105330.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ error[E0277]: the trait bound `Demo: TraitWAssocConst` is not satisfied
LL | foo::<Demo>()();
| ^^^^ the trait `TraitWAssocConst` is not implemented for `Demo`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-105330.rs:1:1
|
LL | pub trait TraitWAssocConst {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `foo`
--> $DIR/issue-105330.rs:11:11
|
Expand Down Expand Up @@ -87,6 +92,11 @@ error[E0277]: the trait bound `Demo: TraitWAssocConst` is not satisfied
LL | foo::<Demo>();
| ^^^^ the trait `TraitWAssocConst` is not implemented for `Demo`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-105330.rs:1:1
|
LL | pub trait TraitWAssocConst {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `foo`
--> $DIR/issue-105330.rs:11:11
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `(): Add<A>` is not satisfied
|
LL | r = r + a;
| ^ the trait `Add<A>` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/associated-types-ICE-when-projecting-out-of-err.rs:15:1
|
LL | trait Add<RHS=Self> {
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `(T, U): Get` is not satisfied
|
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
|
help: this trait has no implementations, consider adding one
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
|
LL | trait Get {
| ^^^^^^^^^

error[E0277]: the trait bound `Self: Get` is not satisfied
--> $DIR/associated-types-no-suitable-supertrait.rs:17:40
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/associated-types/defaults-suitability.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ error[E0277]: the trait bound `(): Foo<Self>` is not satisfied
LL | type Assoc: Foo<Self> = ();
| ^^ the trait `Foo<Self>` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/defaults-suitability.rs:27:1
|
LL | trait Foo<T> {
| ^^^^^^^^^^^^
note: required by a bound in `Bar::Assoc`
--> $DIR/defaults-suitability.rs:34:17
|
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ error[E0277]: the trait bound `(): Foo` is not satisfied
|
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-59324.rs:3:1
|
LL | pub trait Foo: NotFoo {
| ^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `Bug: Foo` is not satisfied
--> $DIR/issue-59324.rs:19:10
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/associated-types/issue-64855.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `Bar<T>: Foo` is not satisfied
|
LL | pub struct Bar<T>(<Self as Foo>::Type) where Self: ;
| ^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bar<T>`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-64855.rs:1:1
|
LL | pub trait Foo {
| ^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0277]: the trait bound `bool: Bar` is not satisfied
LL | type Assoc = bool;
| ^^^^ the trait `Bar` is not implemented for `bool`
|
help: this trait has no implementations, consider adding one
--> $DIR/point-at-type-on-obligation-failure-2.rs:1:1
|
LL | trait Bar {}
| ^^^^^^^^^
note: required by a bound in `Foo::Assoc`
--> $DIR/point-at-type-on-obligation-failure-2.rs:4:17
|
Expand All @@ -16,6 +21,11 @@ error[E0277]: the trait bound `bool: Bar` is not satisfied
LL | type Assoc = bool;
| ^^^^ the trait `Bar` is not implemented for `bool`
|
help: this trait has no implementations, consider adding one
--> $DIR/point-at-type-on-obligation-failure-2.rs:1:1
|
LL | trait Bar {}
| ^^^^^^^^^
note: required by a bound in `Baz::Assoc`
--> $DIR/point-at-type-on-obligation-failure-2.rs:13:18
|
Expand All @@ -31,6 +41,11 @@ error[E0277]: the trait bound `bool: Bar` is not satisfied
LL | type Assoc = bool;
| ^^^^ the trait `Bar` is not implemented for `bool`
|
help: this trait has no implementations, consider adding one
--> $DIR/point-at-type-on-obligation-failure-2.rs:1:1
|
LL | trait Bar {}
| ^^^^^^^^^
note: required by a bound in `Bat::Assoc`
--> $DIR/point-at-type-on-obligation-failure-2.rs:24:27
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/coherence/coherence-unsafe-trait-object-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | takes_t(t);
| |
| required by a bound introduced by this call
|
help: this trait has no implementations, consider adding one
--> $DIR/coherence-unsafe-trait-object-impl.rs:6:1
|
LL | trait Trait: Sized {
| ^^^^^^^^^^^^^^^^^^
note: required by a bound in `takes_t`
--> $DIR/coherence-unsafe-trait-object-impl.rs:10:15
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `[Adt; std::mem::size_of::<Self::Assoc>()]: Foo` i
|
LL | <[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; std::mem::size_of::<Self::Assoc>()]`
|
help: this trait has no implementations, consider adding one
--> $DIR/dont-evaluate-array-len-on-err-1.rs:9:1
|
LL | trait Foo {
| ^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ LL | writes_to_specific_path(&cap);
| |
| required by a bound introduced by this call
|
= help: the trait `Delegates<U>` is implemented for `T`
help: this trait has no implementations, consider adding one
--> $DIR/issue-85848.rs:4:1
|
LL | trait _Contains<T> {
| ^^^^^^^^^^^^^^^^^^
note: required for `&C` to implement `Contains<(), true>`
--> $DIR/issue-85848.rs:21:12
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/const-generics/issues/issue-86530.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | z(" ");
| |
| required by a bound introduced by this call
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-86530.rs:4:1
|
LL | pub trait X {
| ^^^^^^^^^^^
note: required by a bound in `z`
--> $DIR/issue-86530.rs:10:8
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/cross/cross-fn-cache-hole.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0277]: the trait bound `i32: Bar<u32>` is not satisfied
LL | where i32: Foo<u32, A>
| ^^^^^^^^^^^^^^^^ the trait `Bar<u32>` is not implemented for `i32`
|
help: this trait has no implementations, consider adding one
--> $DIR/cross-fn-cache-hole.rs:11:1
|
LL | trait Bar<X> { }
| ^^^^^^^^^^^^
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/dst/dst-bad-coerce1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ error[E0277]: the trait bound `Foo: Bar` is not satisfied
LL | let f3: &Fat<dyn Bar> = f2;
| ^^ the trait `Bar` is not implemented for `Foo`
|
help: this trait has no implementations, consider adding one
--> $DIR/dst-bad-coerce1.rs:10:1
|
LL | trait Bar { fn bar(&self) {} }
| ^^^^^^^^^
= note: required for the cast from `&Fat<Foo>` to `&Fat<dyn Bar>`

error[E0308]: mismatched types
Expand All @@ -34,6 +39,11 @@ error[E0277]: the trait bound `Foo: Bar` is not satisfied
LL | let f3: &(dyn Bar,) = f2;
| ^^ the trait `Bar` is not implemented for `Foo`
|
help: this trait has no implementations, consider adding one
--> $DIR/dst-bad-coerce1.rs:10:1
|
LL | trait Bar { fn bar(&self) {} }
| ^^^^^^^^^
= note: required for the cast from `&(Foo,)` to `&(dyn Bar,)`

error: aborting due to 4 previous errors
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/dyn-star/error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `{integer}: Foo` is not satisfied
|
LL | let dyn_i: dyn* Foo = i;
| ^ the trait `Foo` is not implemented for `{integer}`
|
help: this trait has no implementations, consider adding one
--> $DIR/error.rs:6:1
|
LL | trait Foo {}
| ^^^^^^^^^

error: aborting due to previous error

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/error-codes/E0277.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ LL | some_func(5i32);
| |
| required by a bound introduced by this call
|
help: this trait has no implementations, consider adding one
--> $DIR/E0277.rs:3:1
|
LL | trait Foo {
| ^^^^^^^^^
note: required by a bound in `some_func`
--> $DIR/E0277.rs:7:17
|
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/generic-associated-types/issue-101020.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0277]: the trait bound `for<'a> &'a mut (): Foo<&'a mut ()>` is not satis
LL | (&mut EmptyIter).consume(());
| ^^^^^^^ the trait `for<'a> Foo<&'a mut ()>` is not implemented for `&'a mut ()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-101020.rs:28:1
|
LL | trait Foo<T> {}
| ^^^^^^^^^^^^
note: required for `&'a mut ()` to implement `for<'a> FuncInput<'a, &'a mut ()>`
--> $DIR/issue-101020.rs:27:20
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
LL | C: StackContext,
| ^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-89118.rs:1:1
|
LL | trait BufferMut {}
| ^^^^^^^^^^^^^^^
note: required for `Ctx<()>` to implement `for<'a> BufferUdpStateContext<&'a ()>`
--> $DIR/issue-89118.rs:5:23
|
Expand All @@ -26,6 +31,11 @@ error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
LL | impl<C> EthernetWorker<C> {}
| ^^^^^^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-89118.rs:1:1
|
LL | trait BufferMut {}
| ^^^^^^^^^^^^^^^
note: required for `Ctx<()>` to implement `for<'a> BufferUdpStateContext<&'a ()>`
--> $DIR/issue-89118.rs:5:23
|
Expand All @@ -48,6 +58,11 @@ error[E0277]: the trait bound `for<'a> &'a (): BufferMut` is not satisfied
LL | type Handler = Ctx<C::Dispatcher>;
| ^^^^^^^^^^^^^^^^^^ the trait `for<'a> BufferMut` is not implemented for `&'a ()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-89118.rs:1:1
|
LL | trait BufferMut {}
| ^^^^^^^^^^^^^^^
note: required for `Ctx<()>` to implement `for<'a> BufferUdpStateContext<&'a ()>`
--> $DIR/issue-89118.rs:5:23
|
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-18611.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `isize: HasState` is not satisfied
|
LL | fn add_state(op: <isize as HasState>::State) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasState` is not implemented for `isize`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-18611.rs:5:1
|
LL | trait HasState {
| ^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/issues/issue-25076.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ LL | do_fold(bot(), ());
| |
| required by a bound introduced by this call
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-25076.rs:3:1
|
LL | trait InOut<T> { type Out; }
| ^^^^^^^^^^^^^^
note: required by a bound in `do_fold`
--> $DIR/issue-25076.rs:5:18
|
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-35570.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ error[E0277]: the trait bound `for<'a> (): Trait2<'a>` is not satisfied
|
LL | fn _ice(param: Box<dyn for <'a> Trait1<<() as Trait2<'a>>::Ty>>) {
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> Trait2<'a>` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-35570.rs:4:1
|
LL | trait Trait2<'a> {
| ^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Loading

0 comments on commit 7270f85

Please sign in to comment.