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

Add OriginalType<A, B>: 'pin bound to projected type #188

Merged
merged 1 commit into from
Apr 14, 2020

Conversation

Aaron1011
Copy link
Collaborator

@Aaron1011 Aaron1011 commented Apr 11, 2020

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.

@Aaron1011 Aaron1011 requested a review from taiki-e as a code owner April 11, 2020 23:33
@Aaron1011 Aaron1011 force-pushed the feature/lifetime-bound branch 3 times, most recently from 7fb8b7f to 689c40a Compare April 11, 2020 23:43
@Aaron1011
Copy link
Collaborator Author

The UI test output seems to have changes on Nightly (the span is slightly different). I'm not sure how best to deal with this.

Copy link
Owner

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the investigation and the patch! I believe we can fix this without breaking changes.

pin-project-internal/src/project.rs Outdated Show resolved Hide resolved
@taiki-e
Copy link
Owner

taiki-e commented Apr 13, 2020

@Aaron1011
It seems to infer of lifetime bounds in strust/enum (infer-outlives-requirements, rfc2093) and infer of lifetime bounds in impl/fn (implied-(lifetime)-bounds, rfc2089, rfc1214) are different features, so I would prefer to add lifetime bounds to strust/enum only.
This is because the reported issue is only about lifetime bounds in strust/enum and we don't know if implied-(lifetime)-bounds have a similar problem or can fix it in a similar way, at this time.

Fixes taiki-e#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.

The generated bound does not need to be added to the `where` clause of
any impls for the generated projection types. For more details, see
https://github.com/rust-lang/rfcs/blob/d89de6a5857597cca9f6e1c7ebb70476f31a9159/text/2093-infer-outlives.md#background-outlives-requirements-today
@taiki-e
Copy link
Owner

taiki-e commented Apr 14, 2020

Looks good, Thanks!

bors r+

@bors
Copy link
Contributor

bors bot commented Apr 14, 2020

@bors bors bot merged commit 1ead860 into taiki-e:master Apr 14, 2020
@Aaron1011 Aaron1011 deleted the feature/lifetime-bound branch April 14, 2020 04:52
@taiki-e taiki-e added the A-pin-projection Area: #[pin_project] label May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pin-projection Area: #[pin_project]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lifetime issue when #[pin_project] is used
2 participants