rust/tests/ui/self
bors 5753b30676 Auto merge of #117967 - adetaylor:fix-lifetime-elision-bug, r=lcnr
Fix ambiguous cases of multiple & in elided self lifetimes

This change proposes simpler rules to identify the lifetime on `self` parameters which may be used to elide a return type lifetime.

## The old rules

(copied from [this comment](https://github.com/rust-lang/rust/pull/117967#discussion_r1420554242))

Most of the code can be found in [late.rs](https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_resolve/late.rs.html) and acts on AST types. The function [resolve_fn_params](https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_resolve/late.rs.html#2006), in the success case, returns a single lifetime which can be used to elide the lifetime of return types.

Here's how:
* If the first parameter is called self then we search that parameter using "`self` search rules", below
* If no unique applicable lifetime was found, search all other parameters using "regular parameter search rules", below

(In practice the code does extra work to assemble good diagnostic information, so it's not quite laid out like the above.)

### `self` search rules

This is primarily handled in [find_lifetime_for_self](https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_resolve/late.rs.html#2118) , and is described slightly [here](https://github.com/rust-lang/rust/issues/117715#issuecomment-1813115477) already. The code:

1. Recursively walks the type of the `self` parameter (there's some complexity about resolving various special cases, but it's essentially just walking the type as far as I can see)
2. Each time we find a reference anywhere in the type, if the **direct** referent is `Self` (either spelled `Self` or by some alias resolution which I don't fully understand), then we'll add that to a set of candidate lifetimes
3. If there's exactly one such unique lifetime candidate found, we return this lifetime.

### Regular parameter search rules

1. Find all the lifetimes in each parameter, including implicit, explicit etc.
2. If there's exactly one parameter containing lifetimes, and if that parameter contains exactly one (unique) lifetime, *and if we didn't find a `self` lifetime parameter already*, we'll return this lifetime.

## The new rules

There are no changes to the "regular parameter search rules" or to the overall flow, only to the `self` search rules which are now:

1. Recursively walks the type of the `self` parameter, searching for lifetimes of reference types whose referent **contains** `Self`.[^1]
2. Keep a record of:
   * Whether 0, 1 or n unique lifetimes are found on references encountered during the walk
4. If no lifetime was found, we don't return a lifetime. (This means other parameters' lifetimes may be used for return type lifetime elision).
5. If there's one lifetime found, we return the lifetime.
6. If multiple lifetimes were found, we abort elision entirely (other parameters' lifetimes won't be used).

[^1]: this prevents us from considering lifetimes from inside of the self-type

## Examples that were accepted before and will now be rejected

```rust
fn a(self: &Box<&Self>) -> &u32
fn b(self: &Pin<&mut Self>) -> &String
fn c(self: &mut &Self) -> Option<&Self>
fn d(self: &mut &Box<Self>, arg: &usize) -> &usize // previously used the lt from arg
```

### Examples that change the elided lifetime

```rust
fn e(self: &mut Box<Self>, arg: &usize) -> &usize
//         ^ new                ^ previous
```

## Examples that were rejected before and will now be accepted

```rust
fn f(self: &Box<Self>) -> &u32
```

---

*edit: old PR description:*

```rust
  struct Concrete(u32);

  impl Concrete {
      fn m(self: &Box<Self>) -> &u32 {
          &self.0
      }
  }
```

resulted in a confusing error.

```rust
  impl Concrete {
      fn n(self: &Box<&Self>) -> &u32 {
          &self.0
      }
  }
```

resulted in no error or warning, despite apparent ambiguity over the elided lifetime.

Fixes https://github.com/rust-lang/rust/issues/117715
2024-07-18 13:33:38 +00:00
..
auxiliary
elision Additional test due to Pin<&Self> discovery 2024-06-05 14:32:54 +00:00
arbitrary_self_type_mut_difference.rs
arbitrary_self_type_mut_difference.stderr Deduplicate some logic and reword output 2024-02-22 18:05:28 +00:00
arbitrary_self_types_needing_box_or_arc_wrapping.fixed [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_needing_box_or_arc_wrapping.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_needing_box_or_arc_wrapping.stderr
arbitrary_self_types_needing_mut_pin.fixed [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_needing_mut_pin.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_needing_mut_pin.stderr
arbitrary_self_types_nested.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_pin_lifetime_impl_trait-async.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_pin_lifetime_impl_trait-async.stderr
arbitrary_self_types_pin_lifetime_impl_trait.rs
arbitrary_self_types_pin_lifetime_impl_trait.stderr
arbitrary_self_types_pin_lifetime_mismatch-async.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_pin_lifetime_mismatch-async.stderr Detect when a lifetime is being reused in suggestion 2024-05-17 21:23:47 +00:00
arbitrary_self_types_pin_lifetime_mismatch.rs Run rustfmt on modified tests 2024-05-17 20:31:13 +00:00
arbitrary_self_types_pin_lifetime_mismatch.stderr Detect when a lifetime is being reused in suggestion 2024-05-17 21:23:47 +00:00
arbitrary_self_types_pin_lifetime-async.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_pin_lifetime.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_pin_needing_borrow.rs
arbitrary_self_types_pin_needing_borrow.stderr
arbitrary_self_types_pointers_and_wrappers.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_raw_pointer_struct.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_raw_pointer_trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_silly.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_stdlib_pointers.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_struct.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_trait.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary_self_types_unsized_struct.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary-self-from-method-substs-ice.rs Avoid an ICE reachable through const eval shenanigans 2024-07-01 10:14:42 +00:00
arbitrary-self-from-method-substs-ice.stderr Avoid an ICE reachable through const eval shenanigans 2024-07-01 10:14:42 +00:00
arbitrary-self-from-method-substs.default.stderr Avoid an ICE reachable through const eval shenanigans 2024-07-01 10:14:42 +00:00
arbitrary-self-from-method-substs.feature.stderr
arbitrary-self-from-method-substs.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
arbitrary-self-opaque.rs Require any function with a tait in its signature to actually constrain a hidden type 2024-06-12 08:53:59 +00:00
arbitrary-self-opaque.stderr Require any function with a tait in its signature to actually constrain a hidden type 2024-06-12 08:53:59 +00:00
arbitrary-self-types-not-object-safe.curr.stderr
arbitrary-self-types-not-object-safe.object_safe_for_dispatch.stderr
arbitrary-self-types-not-object-safe.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
builtin-superkinds-self-type.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
by-value-self-in-mut-slot.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
class-missing-self.rs
class-missing-self.stderr
explicit_self_xcrate_exe.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
explicit-self-closures.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
explicit-self-generic.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
explicit-self-objects-uniq.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
explicit-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-61882-2.rs
issue-61882-2.stderr
issue-61882.rs
issue-61882.stderr
move-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-sized-self-by-value-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-sized-self-generic-method.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
object-safety-sized-self-return-Self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
objects-owned-object-owned-method.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
point-at-arbitrary-self-type-method.rs
point-at-arbitrary-self-type-method.stderr
point-at-arbitrary-self-type-trait-method.rs
point-at-arbitrary-self-type-trait-method.stderr
self_lifetime-async.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self_lifetime.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self_type_keyword-2.rs
self_type_keyword-2.stderr
self_type_keyword.rs
self_type_keyword.stderr Deny keyword lifetimes pre-expansion 2024-07-16 12:06:25 -04:00
self-ctor-nongeneric.rs Warn/error on self ctor from outer item in inner item 2024-05-18 13:08:34 -04:00
self-ctor-nongeneric.stderr Warn/error on self ctor from outer item in inner item 2024-05-18 13:08:34 -04:00
self-ctor.rs Warn/error on self ctor from outer item in inner item 2024-05-18 13:08:34 -04:00
self-ctor.stderr Warn/error on self ctor from outer item in inner item 2024-05-18 13:08:34 -04:00
self-impl-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-impl.rs
self-impl.stderr
self-in-mut-slot-default-method.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-in-mut-slot-immediate-value.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-in-typedefs.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-infer.rs
self-infer.stderr
self-re-assign.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-shadowing-import.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-type-param.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
self-vs-path-ambiguity.rs
self-vs-path-ambiguity.stderr Make parse error suggestions verbose and fix spans 2024-07-12 03:02:57 +00:00
string-self-append.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
suggest-self-2.rs
suggest-self-2.stderr
suggest-self.rs
suggest-self.stderr
ufcs-explicit-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
uniq-self-in-mut-slot.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
where-for-self.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00