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

HRTBs: lifetime "does not appear in the trait input types" #70262

Closed
comex opened this issue Mar 22, 2020 · 1 comment
Closed

HRTBs: lifetime "does not appear in the trait input types" #70262

comex opened this issue Mar 22, 2020 · 1 comment

Comments

@comex
Copy link
Contributor

comex commented Mar 22, 2020

Consider this code:

#![feature(unboxed_closures)]
fn foo<F>(f: F) where
    for<'a> F: FnOnce<&'a i32>,
    for<'a> <F as FnOnce<&'a i32>>::Output: Iterator<Item=&'a i64> {}

The bound is intended to match functions roughly of the form

fn callback<'a>(x: &'a i32) -> impl Iterator<Item=&'a i64> { ... }

However, rustc rejects foo with this error:

error[E0582]: binding for associated type `Item` references lifetime `'a`, which does not appear in the trait input types
 --> src/lib.rs:4:54
  |
4 |     for<'a> <F as FnOnce<&'a i32>>::Output: Iterator<Item=&'a i64> {}
  |                                                      ^^^^^^^^^^^^

The rustc --explain output points to issue #33685, which had to do with declarations where a lifetime appeared in a function's return type and nowhere else, like:

fn foo<'a>() -> &'a u32 { .. }

In that issue, @nikomatsakis wrote:

These constructs are being rejected as part of the fix for issue #32330. This is a soundness bug that concerns cases like these.
[..]
These sorts of where-clauses and types are not only ill-formed, they are useless. For example, now that #32330 is fixed, there are no types that could satisfy such a where-clause, nor are there any functions with a type like for<'a> fn() -> &'a i32 (see next section for more details).
[..]
The crux of the problem concerns lifetime parameters that only appear in the return type of a fn declaration -- these are changing to no longer be considered "higher-ranked" (or "late-bound").

My example is different, though, in that the lifetime does appear in the function's arguments. Thus, while I don't know whether allowing the bound would implicate the same soundness issue, I'm pretty sure the bound is not useless: it seems like it should be possible to satisfy.

@comex
Copy link
Contributor Author

comex commented Mar 22, 2020

Just found #49601 which this seems to be a dupe of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant