-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
OriginalType<A, B>: 'pin
bound to projected type
Fixes #185 Usually, Rust will automatically add lifetime bounds of the form `T: 'lifetime` when required (e.g. when a struct contains `&'a T`). However, the compiler will not normalize associated types before performing this analysis, which can require users to manually add `T: 'lifetime` bounds themselves. When this happens with a generated projection struct, the user has no ability to add such a bound, since they cannot name the 'pin lifetime. This PR adds a bound of the form `OriginalType<A, B>: 'pin` to the `where` clause of the generated projection types. This ensures that any type parameters which need to outlive 'pin will do so, without unnecessarily constraining other type parameters. Implementing this requires making two small breaking changes: 1. Writing `#[project] use path::MyType` will now import both the original type and the projected type. not just the projected type. This ensures that the original type can be resolved if it is referenced in a generated `where` clause predicate. 2. Anonymous lifetimes ('_) are no longer allowed in #[project] impl blocks. That is, the following code no longer compiler: `#[project] impl MyType<'_> {}`. This allows us to directly substitute the user-provided lifetimes into the generated `where` clause predicate, since anonymous lifetimes cannot be used in a `where` clause. This restriction could be lifted by re-writing all anonymous lifetimes to unique named lifetimes (e.g. '_a0, '_a1, ..), but I chose to leave this out of this PR. If this restriction proves cumbersome for users, it could be lifted in a separate PR.
- Loading branch information
Showing
8 changed files
with
101 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use pin_project::{pin_project, project}; | ||
|
||
trait Bar<X> { | ||
type Y; | ||
} | ||
|
||
struct Example<A>(A); | ||
|
||
impl<X, T> Bar<X> for Example<T> { | ||
type Y = Option<T>; | ||
} | ||
|
||
#[pin_project] | ||
struct Foo<A, B> { | ||
_x: <Example<A> as Bar<B>>::Y, | ||
} | ||
|
||
#[project] | ||
impl<A, B> Foo<A, B> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use pin_project::{pin_project, project}; | ||
|
||
#[pin_project] | ||
struct Foo<'a>(&'a u8); | ||
|
||
#[project] | ||
impl Foo<'_> {} //~ ERROR #[project] attribute may not | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: #[project] attribute may not be used with '_ lifetimes | ||
--> $DIR/anon_lifetime.rs:7:10 | ||
| | ||
7 | impl Foo<'_> {} //~ ERROR #[project] attribute may not | ||
| ^^ |