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

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

Open
arcnmx opened this issue Jul 22, 2016 · 9 comments · Fixed by #71952
Open

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

arcnmx opened this issue Jul 22, 2016 · 9 comments · Fixed by #71952
Labels
A-lifetimes Area: Lifetimes / regions A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@arcnmx
Copy link
Contributor

arcnmx commented Jul 22, 2016

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

@steveklabnik steveklabnik added A-typesystem Area: The type system A-lang labels Jul 25, 2016
@steveklabnik steveklabnik added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-lang labels Mar 24, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
Contributor

Doesn't repro anymore with the linked code.

@estebank estebank added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 19, 2018
@jackh726
Copy link
Member

jackh726 commented Aug 8, 2019

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
Copy link
Contributor

lcnr commented Apr 26, 2020

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 😅

JohnTitor added a commit to JohnTitor/rust that referenced this issue May 6, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
Add some regression tests

Closes rust-lang#29988
Closes rust-lang#34979
Pick up two snippets that have been fixed from rust-lang#67945 (shouldn't be closed yet!)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 7, 2020
Add some regression tests

Closes rust-lang#29988
Closes rust-lang#34979
Pick up two snippets that have been fixed from rust-lang#67945 (shouldn't be closed yet!)
@bors bors closed this as completed in 480f718 May 7, 2020
@lcnr
Copy link
Contributor

lcnr commented May 7, 2020

This behavior is still a bug, this code snippet should compile afaict.

Removing either the &'a (): Foo or &'static (): Foo bound removes this error, so there
is probably some weird interaction here.

@lcnr lcnr reopened this May 7, 2020
@lcnr lcnr added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 7, 2020
@lcnr
Copy link
Contributor

lcnr commented May 7, 2020

This only occurs if bounds have different lifetimes. Completely identical bounds work fine:

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,
    &'a (): Foo;

@lcnr lcnr added the A-lifetimes Area: Lifetimes / regions label May 7, 2020
@JohnTitor JohnTitor removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label May 7, 2020
@JohnTitor
Copy link
Member

JohnTitor commented May 7, 2020

@lcnr Ahh, I missed that!

So, if someone fixes this issue, then please also tweak the test (I'll add a FIXME to it later if needed).

@jonas-schievink
Copy link
Contributor

Ran into this issue in #77052

@dtolnay
Copy link
Member

dtolnay commented Mar 27, 2023

I am retitling the issue to reflect the diagnostic from newer compilers.

  • 1.7.0 through 1.20: "mismatched types"
  • 1.21 through 1.27: no error (!?)
  • 1.28 through 1.39: "type annotations required"
  • 1.40 through present (1.68) : "type annotations needed"

@dtolnay dtolnay changed the title Mismatched types error with mixed lifetimes in trait bounds "Type annotations needed" with mixed lifetimes in trait bounds Mar 27, 2023
@lcnr
Copy link
Contributor

lcnr commented Mar 31, 2023

I think the examples here will be solved by the -Ztrait-solver=next stabilization1 (cases where the obligation is exactly equal to the param env candidate). The more general issue of param env candidates which are equal up to regions will require OR bounds for regions which I would like to add in the long term but that will still take a while.

Footnotes

  1. depending on how we canonicalize regions

@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants