rust-lang / rust-lang/rust

HRTB implied bounds due to dyn causes rustc to describe a non-reference as a reference

Open
#152,151 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics A-dyn-trait A-higher-ranked A-implied-bounds T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct Contra<'a>(fn(&'a ()));

trait Trait<'a>: 'a {
    fn method(self: Box<Self>, _: Contra<'a>);
}

fn what<'b>(x: Box<dyn for<'a> Trait<'a> + 'b>, y: Contra<'static>) {
    x.method(y);
}
Current output
error[E0521]: borrowed data escapes outside of function
 --> src/lib.rs:8:5
  |
7 | fn what<'b>(x: Box<dyn for<'a> Trait<'a> + 'b>, y: Contra<'static>) {
  |         --  -                                   - `y` declared here, outside of the function body
  |         |   |
  |         |   `x` is a reference that is only valid in the function body
  |         lifetime `'b` defined here
8 |     x.method(y);
  |     ^^^^^^^^^^^
  |     |
  |     `x` escapes the function body here
  |     argument requires that `'b` must outlive `'static`

For more information about this error, try `rustc --explain E0521`.
Desired output
x is not a reference, so it should not be described as a reference
Rationale and extra context

I believe that there's an implied 'b: 'a bound here. See https://github.com/rust-lang/rust/issues/104684#issuecomment-3850928492

Other cases

Rust Version
Reproducible on the playground with version 1.95.0-nightly (2026-02-03 366a1b93e7f466ebe559)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by compiling the provided Rust snippet on the reported nightly version and compare the E0521 diagnostic with the desired wording, then trace the compiler diagnostic path for this lifetime and dyn-trait case. Done means the output no longer describes x as a reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.