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 · 4 comments

Comments

Projects
None yet
4 participants
@Nemo157
Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

withoutboats commented Apr 3, 2018

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.

@alexreg

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor Author

Nemo157 commented Nov 15, 2018

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

@alexreg

This comment has been minimized.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment