Skip to content

refining_impl_trait suggestion in invalid code #129404

Closed
@cyrgani

Description

@cyrgani

Code

trait FromRow {
    fn prepare(self) -> impl Fn() -> T;
}

impl<T> FromRow for T {
    fn prepare(self) -> impl Fn() -> T {
        || loop {}
    }
}

Current output

error[E0412]: cannot find type `T` in this scope
 --> src/lib.rs:2:38
  |
2 |     fn prepare(self) -> impl Fn() -> T;
  |                                      ^ not found in this scope

warning: impl trait in impl method signature does not match trait method signature
 --> src/lib.rs:6:38
  |
2 |     fn prepare(self) -> impl Fn() -> T;
  |                         -------------- return type from trait method defined here
...
6 |     fn prepare(self) -> impl Fn() -> T {
  |                                      ^ this bound is stronger than that defined on the trait
  |
  = note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate
  = note: we are soliciting feedback, see issue #121718 <https://github.com/rust-lang/rust/issues/121718> for more information
  = note: `#[warn(refining_impl_trait_internal)]` on by default
help: replace the return type so that it matches the trait
  |
6 |     fn prepare(self) -> impl Fn() -> {type error} {
  |                         ~~~~~~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0412`.

Desired output

error[E0412]: cannot find type `T` in this scope
 --> src/lib.rs:2:38
  |
2 |     fn prepare(self) -> impl Fn() -> T;
  |                                      ^ not found in this scope

Rationale and extra context

Not sure if this is worth fixing, but the warning message is not helpful here at all and the suggestion fn prepare(self) -> impl Fn() -> {type error} is not usable.

Other cases

No response

Rust Version

rustc 1.80.1

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions