Skip to content

"Type annotations needed" with mixed lifetimes in trait bounds #34979

Open
@arcnmx

Description

@arcnmx
Contributor

This was encountered from a derive plugin that created where bounds for all member types. Occurs on stable, beta, and nightly.

struct S<'a>(&'a ());

trait Tr { }
impl<T> Tr for Option<T> { }
impl<'a> Tr for S<'a> where Option<&'a str>: Tr, Option<&'static str>: Tr { }
error: mismatched types [--explain E0308]
 --> <anon>:5:1
  |>
5 |> impl<'a> Tr for S<'a> where Option<&'a str>: Tr, Option<&'static str>: Tr { }
  |> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
note: expected type `Tr`
note:    found type `Tr`
note: the lifetime 'a as defined on the impl at 5:0...
 --> <anon>:5:1
  |>
5 |> impl<'a> Tr for S<'a> where Option<&'a str>: Tr, Option<&'static str>: Tr { }
  |> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the static lifetime

error: aborting due to previous error

playpen repro

Activity

added
T-langRelevant to the language team
and removed on Mar 24, 2017
estebank

estebank commented on Mar 19, 2018

@estebank
Contributor

Doesn't repro anymore with the linked code.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Mar 19, 2018
jackh726

jackh726 commented on Aug 8, 2019

@jackh726
Member

Error on latest stable:

error[E0283]: type annotations required: cannot resolve `std::option::Option<&'a str>: Tr`
 --> src/main.rs:5:1
  |
5 | impl<'a> Tr for S<'a> where Option<&'a str>: Tr, Option<&'static str>: Tr { }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: required by `Tr`
 --> src/main.rs:3:1
  |
3 | trait Tr { }
  | ^^^^^^^^
lcnr

lcnr commented on Apr 26, 2020

@lcnr
Contributor

Minimal repro:

trait Foo {}
impl<'a, T> Foo for &'a T {}

// we need `'a _` and `'static _` in the same
// `where` bound afaict.
struct Ctx<'a>(&'a ()) where
    &'a (): Foo,
    &'static (): Foo;

While this is a quite rare edge case, the error is very unhelpful rn 😅

added 3 commits that reference this issue on May 7, 2020

Rollup merge of rust-lang#71952 - JohnTitor:add-tests, r=Dylan-DPC

2c49d0a

Rollup merge of rust-lang#71952 - JohnTitor:add-tests, r=Dylan-DPC

1a2e46e

Rollup merge of rust-lang#71952 - JohnTitor:add-tests, r=Dylan-DPC

480f718

21 remaining items

Loading
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-lifetimesArea: Lifetimes / regionsA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priorityT-langRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @steveklabnik@estebank@jonas-schievink@dtolnay@Mark-Simulacrum

      Issue actions

        "Type annotations needed" with mixed lifetimes in trait bounds · Issue #34979 · rust-lang/rust