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

Lifetime elision #187

Merged
merged 2 commits into from
Mar 3, 2018
Merged

Conversation

matthewjasper
Copy link
Contributor

Second PR to add documentation for lifetimes (next will probably moving the relevant parts of the NLL RFC text into the reference when it gets stabilised).

This is mostly existing documentation from the nomicon/reference. Significant changes from that documentation:

  • Don't talk about possible future changes (elision in impl headers)
  • Be more concise in the description of function lifetime elision
  • Add an example of function pointer and function trait elision
  • Note that 'static is the preferred bound in trait objects with multiple bounds:
trait A<'x, 'y>: 'x + 'y {}
impl A<'a, 'static> {} // A<'a, 'static> + 'static

cc #9 (rust-lang/rfcs#141)

@@ -59,50 +59,6 @@ unsafe fn bump_levels_unsafe2() -> u32 {
Mutable statics have the same restrictions as normal statics, except that the
type does not have to implement the `Sync` trait.

## `'static` lifetime elision
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure how, but it'd be nice for this page to say it's possible and link to the rules for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is at least a mention of this on the constant items page with a link.

Copy link
Member

@aturon aturon left a comment

Choose a reason for hiding this comment

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

So sorry for the very late review! This looks great-- just a couple nits.

_default object lifetime bound_. These were defined in [RFC 599] and amended in
[RFC 1156].

If the trait object is used as for a type argument of a generic type then the
Copy link
Member

Choose a reason for hiding this comment

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

"used as for a type"

* If there is more than one bound from the containing type then an explicit
bound must be specified

Then the bounds on the trait are used:
Copy link
Member

Choose a reason for hiding this comment

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

The text doesn't make clear the relationship between these bullets and the previous ones.

@matthewjasper
Copy link
Contributor Author

changes made

In order to make common patterns more ergonomic, Rust allows lifetimes to be
*elided* in [function item], [function pointer] and [closure trait] signatures.
The following rules are used to infer lifetime parameters for elided lifetimes.
It is an error to elide lifetime parameters that cannot be infered.
Copy link
Contributor

Choose a reason for hiding this comment

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

inferred

type FunTrait = for<'a> Fn(&'a str) -> &'a str; // expanded
```

## Defaults trait object lifetimes
Copy link
Contributor

Choose a reason for hiding this comment

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

Default

* Don't talk about a future feature
* Add examples of elision in function pointers/traits
* A rule for 'static bounds on trait objects
* Static lifetime elision doesn't happen in function pointers/traits
@matthewjasper
Copy link
Contributor Author

Thanks, fixed now.

@Havvy
Copy link
Contributor

Havvy commented Mar 3, 2018

cc @alercah

@alercah
Copy link
Contributor

alercah commented Mar 3, 2018

I don't see anything that looks off to me, but I also don't have the time for a detailed read.

@alercah alercah merged commit d0921d9 into rust-lang:master Mar 3, 2018
@matthewjasper matthewjasper deleted the lifetime-elision branch October 18, 2018 21:00
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