rust-lang / rust-lang/rust

show reason why bound is nessesary

Open
#127,458 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct TypedFunction<'gc, I, O> {
    f: &'gc i32,
    _phantom: std::marker::PhantomData<fn(I) -> O>,
}
#[derive(Clone, Copy)]
struct Context<'gc> {
    f: &'gc i32,
}
pub trait UserDataMethodsCollector<'gc, X> {}
pub trait UserData<'gc> {
    fn add_methods<Collector: UserDataMethodsCollector<'gc, Self>>(
        collector: Collector,
    ) -> Collector
    where
        Self: Sized;
}


pub struct RuntimeCollector<'gc> {
    context: Context<'gc>,
}

impl<'gc> RuntimeCollector<'gc> {
    pub fn finalize<X: UserData<'gc> + 'gc>(ctx: Context<'gc>, _value: X) {
        let this = Self {
            context: ctx
        };
        //this line ends up causing the error
        let _this = X::add_methods(this);
    }
}

//because of the bound here. Would be nice if that got shown in the error message
impl<'gc, X: UserData<'gc> + 'static> UserDataMethodsCollector<'gc, X> for RuntimeCollector<'gc> {}
Current output
error[E0310]: the parameter type `X` may not live long enough
  --> src/lib.rs:29:21
   |
29 |         let _this = X::add_methods(this);
   |                     ^^^^^^^^^^^^^^
   |                     |
   |                     the parameter type `X` must be valid for the static lifetime...
   |                     ...so that the type `X` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
24 |     pub fn finalize<X: UserData<'gc> + 'gc + 'static>(ctx: Context<'gc>, _value: X) {
   |                                            +++++++++

For more information about this error, try `rustc --explain E0310`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
error[E0310]: the parameter type `X` may not live long enough
  --> src/lib.rs:29:21
   |
29 |         let _this = X::add_methods(this);
   |                     ^^^^^^^^^^^^^^
   |                     |
   |                     the parameter type `X` must be valid for the static lifetime...
   |                     ...so that the type `X` will meet its required lifetime bounds
   |

because of `'static` bound here
   |
34 | impl<'gc, X: UserData<'gc> + 'static> UserDataMethodsCollector<'gc, X> for RuntimeCollector<'gc> {}
   |
help: consider adding an explicit lifetime bound
   |
24 |     pub fn finalize<X: UserData<'gc> + 'gc + 'static>(ctx: Context<'gc>, _value: X) {
   |                                            +++++++++


For more information about this error, try `rustc --explain E0310`.
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context

It is sometimes hard to track down where a lifetime requirement comes from. Adding a line that points in the right direction would help track it down.

Other cases

No response

Rust Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Anything else?

playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d8fc4728e68d1f4e7e504543d536db50

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

Reproduce the E0310 diagnostic from the issue's src/lib.rs example using rustc 1.79.0, then trace how the compiler reports the required 'static bound from the impl. Done means the diagnostic identifies the bound location at the impl while preserving the existing help text; no repository test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.