Skip to content

refining_impl_trait: The suggested fix does not yet take precise capturing into account. #141047

Open
@Tamschi

Description

@Tamschi

Code

pub trait Trait {
    fn precise_capturing(&self) -> impl use<Self> + Send;
}

impl Trait for () {
    fn precise_capturing(&self) -> () {}
}

Current output

warning: impl trait in impl method signature does not match trait method signature
 --> src\lib.rs:6:36
  |
2 |     fn precise_capturing(&self) -> impl use<Self> + Send;
  |                                    --------------------- return type from trait method defined here
...
6 |     fn precise_capturing(&self) -> () {}
  |                                    ^^
  |
  = 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_reachable)]` on by default
help: replace the return type so that it matches the trait
  |
6 -     fn precise_capturing(&self) -> () {}
6 +     fn precise_capturing(&self) -> impl Send {}
  |

Desired output

warning: impl trait in impl method signature does not match trait method signature
 --> src\lib.rs:6:36
  |
2 |     fn precise_capturing(&self) -> impl use<Self> + Send;
  |                                    --------------------- return type from trait method defined here
...
6 |     fn precise_capturing(&self) -> () {}
  |                                    ^^
  |
  = 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_reachable)]` on by default
help: replace the return type so that it matches the trait
  |
6 -     fn precise_capturing(&self) -> () {}
6 +     fn precise_capturing(&self) -> impl use<> + Send {}
  |

Rationale and extra context

It's a slight inconvenience since the code won't compile after the current quick-fix is applied:

error: return type captures more lifetimes than trait definition
 --> src\lib.rs:6:36
  |
2 |     fn precise_capturing(&self) -> impl use<Self> + Send;
  |                          - this lifetime was captured
...
6 |     fn precise_capturing(&self) -> impl Send {}
  |                                    ^^^^^^^^^
  |
note: hidden type must only reference lifetimes captured by this impl trait
 --> src\lib.rs:2:36
  |
2 |     fn precise_capturing(&self) -> impl use<Self> + Send;
  |                                    ^^^^^^^^^^^^^^^^^^^^^
  = note: hidden type inferred to be `impl Send`

Copying it from the trait works, but requires removing Self from the use<> and occasionally more type parameters to be added.

Other cases

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-pc-windows-msvc
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-refining_impl_traitLint group: refining_impl_traitT-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