Skip to content

Do not suggest adding a new lifetime bound in returned impl Trait if 'static is already specified in the signature #67910

Closed

Description

@estebank

The following error

error: cannot infer an appropriate lifetime
--> $DIR/must_outlive_least_region_or_bound.rs:12:69
|
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
| -------------------------------- ^ ...but this borrow...
| |
| this return type evaluates to the `'static` lifetime...
|
note: ...can't outlive the lifetime `'a` as defined on the function body at 12:15
--> $DIR/must_outlive_least_region_or_bound.rs:12:15
|
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
| ^^
help: you can add a bound to the return type to make it last less than `'static` and match the lifetime `'a` as defined on the function body at 12:15
|
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static + 'a { x }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

should instead suggest changing the explicit 'static bound or not provide a suggestion at all.

Follow up to #67595.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    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-papercutDiagnostics: An error or lint that needs small tweaks.
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Jan 6, 2020
  2. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Jan 12, 2020
  3. estebank commented on Aug 30, 2020

    @estebank
    ContributorAuthor

    Current output:

    error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
      --> $DIR/must_outlive_least_region_or_bound.rs:7:46
       |
    LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
       |               ----                           ^ ...is captured here...
       |               |
       |               this data with an anonymous lifetime `'_`...
       |
    note: ...and is required to live as long as `'static` here
      --> $DIR/must_outlive_least_region_or_bound.rs:7:24
       |
    LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
       |                        ^^^^^^^^^^^^^^^^^^^
    help: consider changing the `impl Trait`'s explicit `'static` bound to the lifetime of argument `x`
       |
    LL | fn elided2(x: &i32) -> impl Copy + '_ { x }
       |                                    ^^
    help: alternatively, add an explicit `'static` bound to this reference
       |
    LL | fn elided2(x: &'static i32) -> impl Copy + 'static { x }
       |               ^^^^^^^^^^^^
    
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`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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