Skip to content

Account for type param in '_ constraint for returned trait suggestion #73497

Closed

Description

@estebank

Given

use std::any::Any;
fn foo<T: Any>(value: &T) -> Box<dyn Any> {
Box::new(value) as Box<dyn Any>
//~^ ERROR cannot infer an appropriate lifetime
}

we currently output

error[E0759]: cannot infer an appropriate lifetime
--> $DIR/issue-16922.rs:4:14
|
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
| -- this data with an anonymous lifetime `'_`...
LL | Box::new(value) as Box<dyn Any>
| ^^^^^ ...is captured here, requiring it to live as long as `'static`
|
help: to declare that the trait object captures data from argument `value`, you can add an explicit `'_` lifetime bound
|
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {
| ^^^^

The suggestion is incomplete and will not result in compilable code. Because the lifetime is coming from an argument of type T, which is an unconstrained type parameter, T itself will not have the needed lifetime and causes the 'static obligation that we're trying to reverse by suggesting '_. The appropriate changes to the code would be either fn foo<'a, T: Any + 'a>(value: &'a T) -> Box<dyn Any + 'a> or fn foo(value: &dyn Any) -> Box<dyn Any + '_>, depending on whether T appears in any other argument or not.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    on Jun 19, 2020
  2. estebank commented on Sep 28, 2023

    @estebank
    ContributorAuthor

    The suggested code works since 1.64.

  3. added a commit that references this issue on Sep 28, 2023
    7bdb6fe
  4. added a commit that references this issue on Oct 11, 2023
    efb822e
  5. added 2 commits that reference this issue on Oct 24, 2023
    1970b15
    6d7302d
  6. added 2 commits that reference this issue on Nov 7, 2023
    0a05fe7
    aae391c
  7. added a commit that references this issue on Nov 14, 2023
    00ba7fc
  8. added a commit that references this issue on Nov 15, 2023
    e51196d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.P-lowLow priorityT-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