Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static lifetime requirement in boxed trait object (Box<dyn Trait>) is not clearly explained #116765

Closed
Tiwalun opened this issue Oct 15, 2023 · 5 comments
Assignees
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-objects Area: trait objects, vtable layout D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Tiwalun
Copy link
Contributor

Tiwalun commented Oct 15, 2023

Code

struct Factory<'a> {
    value: &'a usize,
}

impl<'a> Factory<'a> {
    fn generate(&self) -> Box<dyn std::fmt::Debug + 'a> {
        Box::new(Value  { value: self.value })
    }
}


struct Owner {
    value: Box<dyn std::fmt::Debug>
}


#[derive(Debug)]
struct Value<'a> {
    value: &'a usize,
}


fn build_owner<'a,'b>(factory: &'b Factory<'a>) -> Owner {
    let value = factory.generate();

    Owner { value }
}

fn main() {
    let value = 10;

    let factory = Factory { value: &value };

    let _owner = build_owner(&factory);
}

Current output

Compiling playground v0.0.1 (/playground)

error: lifetime may not live long enough
  --> src/main.rs:29:13
   |
26 | fn build_owner<'a,'b>(factory: &'b Factory<'a>) -> Owner {
   |                -- lifetime `'a` defined here
...
29 |     Owner { value }
   |             ^^^^^ cast requires that `'a` must outlive `'static`

Desired output

error: lifetime may not live long enough
  --> src/main.rs:29:13
   |
26 | fn build_owner<'a,'b>(factory: &'b Factory<'a>) -> Owner {
   |                -- lifetime `'a` defined here
...
29 |     Owner { value }
   |             ^^^^^ cast requires that `'a` must outlive `'static`

note: value has type Box<dyn Test>, which means that a default lifetime requirement of 'static is added. 
note: Adding an explicit lifetime could help: Box<dyn Test +'a>

Rationale and extra context

I think it's not obvious that a boxed trait object means that there is a default lifetime requirement of 'static, and I've spent too much time looking for where the 'static requirement comes from. Would be great to get a hint from the compiler.

Other cases

No response

Anything else?

No response

@Tiwalun Tiwalun added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 15, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 15, 2023
@fmease fmease added A-lifetimes Area: Lifetimes / regions D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-trait-objects Area: trait objects, vtable layout A-borrow-checker Area: The borrow checker and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 16, 2023
@fmease
Copy link
Member

fmease commented Oct 17, 2023

@obeis You might've found this already but I think you can reuse or even generalize what maybe_suggest_constrain_dyn_trait_impl or add_static_impl_trait_suggestion in compiler/rustc_borrowck/src/diagnostics/region_errors.rs is doing (esp. with TraitObjectVisitor + HirTraitObjectVisitor). Just mentioning this information to you since I've already briefly looked at the code.

@Noratrieb
Copy link
Member

@obeis sorry for stealing your issue, I didn't check whether there was one 😅
@rustbot claim

@obeis
Copy link
Contributor

obeis commented Nov 21, 2023

@Nilstrieb No problem

@Noratrieb
Copy link
Member

I messed up the fixes in the PR description lol
#117835 has been merged

@Tiwalun
Copy link
Contributor Author

Tiwalun commented Nov 21, 2023

Thanks for improving this! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-objects Area: trait objects, vtable layout D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants