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

Confusing error message when returning impl Trait with multiple lifetimes #49431

Open
Nemo157 opened this issue Mar 27, 2018 · 6 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Mar 27, 2018

Given

use std::fmt::Debug;

fn foobar<'a, 'b>(foo: &'a str, bar: &'b str) -> impl Debug + 'a + 'b {
    (foo, bar)
}

fn main() {
    let foo = "hello".to_owned();
    let bar = "world".to_owned();
    println!("{:?}", foobar(&foo, &bar));
}

you currently (rustc 1.26.0-nightly (188e693b3 2018-03-26)) get the errors

error[E0623]: lifetime mismatch
 --> foo.rs:3:50
  |
3 | fn foobar<'a, 'b>(foo: &'a str, bar: &'b str) -> impl Debug + 'a + 'b {
  |                                      -------     ^^^^^^^^^^^^^^^^^^^^
  |                                      |           |
  |                                      |           ...but data from `foo` is returned here
  |                                      this parameter and the return type are declared with different lifetimes...

error[E0623]: lifetime mismatch
 --> foo.rs:3:50
  |
3 | fn foobar<'a, 'b>(foo: &'a str, bar: &'b str) -> impl Debug + 'a + 'b {
  |                        -------                   ^^^^^^^^^^^^^^^^^^^^
  |                        |                         |
  |                        |                         ...but data from `bar` is returned here
  |                        this parameter and the return type are declared with different lifetimes...

This not being an allowed syntax was briefly mentioned by @nikomatsakis in #34511, quoting the relevant part of the comment:

This is kind of annoying to do; we can't use 'cx + 'gcx. We can I suppose make a dummy trait:

trait Captures<'a> { }
impl<T: ?Sized> Captures<'a> for T { }

and then return something like this impl Iterator<Item = &'tcx Foo> + Captures<'gcx> + Captures<'cx>.

As far as I recall/can discover this limitation was never followed up on in that thread.


After some RFC spelunking I cannot find anything restricting a type bound to having at most a single lifetime bound, RFC 192 Appendix B may be relevant, but I don't believe that the Trait part of impl Trait is an "object type", it seems to me to be a "type parameter bound". Re-reading the impl Trait RFC series doesn't appear to clear this up, exactly what sort of syntax Trait is is never explicitly named, the original RFC simply says

The proposed syntax is impl Trait in return type position, composing like trait objects to forms like impl Foo+Send+'a.

It seems to me that supporting multiple lifetime bounds on impl Trait should be fine, conceptually it seems to be the equivalent of:

use std::fmt::Debug;

fn foobar<'a, 'b, T: Debug + 'a + 'b>(foo: &'a &'b T) {
    println!("{:?}", foo);
}

fn main() {
    let foo = "hello world";
    {
        let bar = &foo;
        {
            let baz = &bar;
            foobar(baz);
        }
    }
}

Whether or not impl Trait can be extended to support multiple lifetimes, it seems like there should be an error message that the form impl Foo + 'a + 'b is not valid currently, rather than some weird lifetime mismatch errors.

@withoutboats
Copy link
Contributor

It seems like we should add an artificial syntactic restriction to impl Trait until this works. One already exists for dyn Trait - dyn Trait + 'a + 'b results in a syntax error message.

@pietroalbini pietroalbini added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 6, 2018
@alexreg
Copy link
Contributor

alexreg commented Nov 15, 2018

Does this work yet? I didn't get the above error message, but I did get others.

@Nemo157
Copy link
Member Author

Nemo157 commented Nov 15, 2018

I appear to still get the same error message on the playground.

@alexreg
Copy link
Contributor

alexreg commented Nov 15, 2018

Well, my case is similar but slightly different... the code is too tangled to post here. Does anyone fancy taking this on as a PR?

@estebank
Copy link
Contributor

Triage: no change.

@estebank
Copy link
Contributor

Current output:

error: lifetime may not live long enough
 --> src/main.rs:4:5
  |
3 | fn foobar<'a, 'b>(foo: &'a str, bar: &'b str) -> impl Debug + 'a + 'b {
  |           --  -- lifetime `'b` defined here
  |           |
  |           lifetime `'a` defined here
4 |     (foo, bar)
  |     ^^^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
  |
  = help: consider adding the following bound: `'a: 'b`

error: lifetime may not live long enough
 --> src/main.rs:4:5
  |
3 | fn foobar<'a, 'b>(foo: &'a str, bar: &'b str) -> impl Debug + 'a + 'b {
  |           --  -- lifetime `'b` defined here
  |           |
  |           lifetime `'a` defined here
4 |     (foo, bar)
  |     ^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'b`
  |
  = help: consider adding the following bound: `'b: 'a`

help: `'a` and `'b` must be the same: replace one with the other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. 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