]> git.lizzy.rs Git - rust.git/commit
in which inferable outlives-requirements are linted
authorZack M. Davis <code@zackmdavis.net>
Sun, 26 Aug 2018 19:22:04 +0000 (12:22 -0700)
committerZack M. Davis <code@zackmdavis.net>
Fri, 28 Sep 2018 03:24:14 +0000 (20:24 -0700)
commit032d97fa018de3d50136b338e02dabe84ab9c695
tree5f573f7e9921d61e633af79e6d2336608e58941d
parent7d52cbce6db83e4fc2d8706b4e4b9c7da76cbcf8
in which inferable outlives-requirements are linted

RFC 2093 (tracking issue #44493) lets us leave off
commonsensically inferable `T: 'a` outlives requirements. (A separate
feature-gate was split off for the case of 'static lifetimes, for
which questions still remain.) Detecting these was requested as an
idioms-2018 lint.

It turns out that issuing a correct, autofixable suggestion here is
somewhat subtle in the presence of other bounds and generic
parameters. Basically, we want to handle these three cases:

 • One outlives-bound. We want to drop the bound altogether, including
   the colon—

   MyStruct<'a, T: 'a>
                 ^^^^ help: remove this bound

 • An outlives bound first, followed by a trait bound. We want to
   delete the outlives bound and the following plus sign (and
   hopefully get the whitespace right, too)—

   MyStruct<'a, T: 'a + MyTrait>
                   ^^^^^ help: remove this bound

 • An outlives bound after a trait bound. We want to delete the
   outlives lifetime and the preceding plus sign—

   MyStruct<'a, T: MyTrait + 'a>
                          ^^^^^ help: remove this bound

This gets (slightly) even more complicated in the case of where
clauses, where we want to drop the where clause altogether if there's
just the one bound. Hopefully the comments are enough to explain
what's going on!

A script (in Python, sorry) was used to generate the
hopefully-sufficiently-exhaustive UI test input. Some of these are
split off into a different file because rust-lang-nursery/rustfix#141
(and, causally upstream of that, #53934) prevents them from being
`run-rustfix`-tested.

We also make sure to include a UI test of a case (copied from RFC
2093) where the outlives-bound can't be inferred. Special thanks to
Niko Matsakis for pointing out the `inferred_outlives_of` query,
rather than blindly stripping outlives requirements as if we weren't a
production compiler and didn't care.

This concerns #52042.
src/librustc/lint/builtin.rs
src/librustc_lint/builtin.rs
src/librustc_lint/lib.rs
src/test/ui/rust-2018/edition-lint-infer-outlives-multispan.rs [new file with mode: 0644]
src/test/ui/rust-2018/edition-lint-infer-outlives-multispan.stderr [new file with mode: 0644]
src/test/ui/rust-2018/edition-lint-infer-outlives.fixed [new file with mode: 0644]
src/test/ui/rust-2018/edition-lint-infer-outlives.rs [new file with mode: 0644]
src/test/ui/rust-2018/edition-lint-infer-outlives.stderr [new file with mode: 0644]
src/test/ui/rust-2018/edition-lint-uninferable-outlives.rs [new file with mode: 0644]