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

Explain default trait object liftime bounds. #31353

Closed
wants to merge 2 commits into from

Conversation

steveklabnik
Copy link
Member

Fixes #31137

I feel a little weird about how short this is. It's really hard to not just say the exact rule, about T: 'a. It feels so arbitrary here, though.

r? @aturon @nikomatsakis

@nikomatsakis
Copy link
Contributor

I'm not sure if this gets at the essence of the rule. For example, it doesn't example what happens with Box<Trait>. I think the rule is kind of aptly summarized as consulting, in order:

  1. Lifetime bounds on the trait, if any. For example, trait Foo: 'static or trait Bar<'a>: 'a.
  2. Lifetime of enclosing reference, if any. So, &Trait => &'a (Trait+'a)
  3. Else, 'static.

@nikomatsakis
Copy link
Contributor

Though I think there may be some subtleties about Ref<'a, Trait> -- I can't remember where we ended up on that (did we modify that case when we tweaked the default object lifetime rule?)

@steveklabnik
Copy link
Member Author

@nikomatsakis updated, what do you think of this attempt?

@nikomatsakis
Copy link
Contributor

@steveklabnik seems better. Something still gives me pause. I feel like the way the importance of the cases is the reverse of they are listed. That is, another way to think of it is "trait objects cannot contain borrowed data unless one of these cases is met":

  • the trait declares a bound, e.g. trait Foo<'a>: 'a
  • the object type appears underneath a reference, e.g., &Foo
  • the object type has an explicit bound Box<Foo+'a>

It's just a matter of emphasis though. Thoughts?

@steveklabnik
Copy link
Member Author

Ah, I like that.

@steveklabnik
Copy link
Member Author

@nikomatsakis r?

@@ -3603,6 +3603,41 @@ fn main() {
In this example, the trait `Printable` occurs as a trait object in both the
type signature of `print`, and the cast expression in `main`.

Trait objects may contain references, and so those references will need a
lifetime. If the trait contains some sort of lifetime bound, like this:
Copy link
Contributor

Choose a reason for hiding this comment

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

feels weird to me that this sentence just... ends (and doesn't resume after the code snippet)

@steveklabnik
Copy link
Member Author

@nikomatsakis after a very long time, nits addressed, i think.

@steveklabnik steveklabnik deleted the gh31137 branch June 19, 2016 20:29
@solson
Copy link
Member

solson commented Sep 13, 2016

Why was this closed without merging? #31137 is still open as well.

@steveklabnik
Copy link
Member Author

I believe I was cleaning up my branches and may have accidentally deleted it. It was also sitting in the queue for a very, very long time...

@solson
Copy link
Member

solson commented Sep 13, 2016

@steveklabnik Would it be okay if I took your changes from this PR and submitted them as a new PR?

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

Successfully merging this pull request may close these issues.

4 participants