Help test and provide feedback on `'_` lifetimes!


#1

Hot on the heels of last week’s call to action about the module system changes in Rust 2018, this week we’d like to draw attention to and make a call for action on '_ (wildcard) lifetimes coming to Rust 2018!

One of the features of RFC 2115 is the “wildcard lifetime”, written as '_. Today lifetime elision allows signatures like:

fn foo(&self) -> Ref<SomeType>
fn iter(&self) -> Iter<T>

but these are hiding the fact that there’s a connected lifetime there. To ergonomically convey that there’s a lifetime contained inside you can now write this as:

fn foo(&self) -> Ref<'_, SomeType>
fn iter(&self) -> Iter<'_, T>

and the two snippets mean the same thing!


It’s not always easy to find locations in your crate that can use from '_, we’re after all trying to fix the problem that they’re so hidden! To help you with the transition we’ve developed a new lint for this feature, and the easiest way to follow the lint is to use cargo fix. Note that you do not need to be in the 2018 edition to test this feature.

  1. Add #![feature(rust_2018_preview)] to your crate (skip this if you’re already on the 2018 edition)
  2. Add #![warn(elided_lifetimes_in_paths)] to your crate
  3. Run cargo fix

And that’s it! If you see any bugs please report them to rust-lang/cargo or rust-lang/rust. This includes things like cases the compiler should have linted for but didn’t, lint suggestions that fail to apply, etc.

Afterwards be sure to run cargo doc, try it out with the RLS, run rustfmt to see what happens, otherwise try to stress this feature! Like the module system we want to make sure that wildcard lifetimes are as polished as they can be for the 2018 edition, and we need your help!

In addition to testing our your own code and reporting bugs, we’d love to see some community-driven tutorials/documentation/podcasts about the feature! If you’re busy this week don’t worry as well, we’ll be making a call to action for a new feature next week!

And of course, please feel free to leave feedback here as well!


2018 Edition End of Week Post (2018-08-04)
#2

workspace users: If you previously did the module system stuff last week, here’s a quick fix to get all the annotations inserted. (assuming you didn’t do anything weird like cfg_attr; I’ve simply been doing the migrations on a separate branch)

sed -Ei 's/(\#!\[feature\(rust_2018_preview\)\])/\1\n#![warn(elided_lifetimes_in_paths)]/' $(find -name '*.rs')

(it is inserting the new warn annotation after each appearance of the rust_2018_preview feature)


#3

One tiny annoyance I’ve noticed so far: documentation examples rarely (if ever) include the '_, which means that you can get warnings or errors when copying code straight from the docs.

The error messages are pretty clear, and I haven’t found it to be too much trouble to adapt, but it is a thing that made me realize how much I’m used to code from docs just working.


#4

Until that becomes an only option with implicit syntax deprecation – it is probably a good feature to have.

Though I can’t see a usecase if explicit elided lifetimes won’t give any improvements in lifetime inference.


#5

I’d love if rustdoc displayed them this way regardless of how the source code looks like.


#6

One question is whether you should be allowed to elide lifetimes on argument types that are already behind a reference, e.g. task::Context has a lifetime so currently rustfix rewrites signatures in implementations of Future::poll to

fn poll(self: PinMut<'_, Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output>

where the <'_> on Context is not really providing much since the reference itself already has a limited lifetime.