rust/tests/ui/self
Stuart Cook 0a7401148e
Rollup merge of #146305 - Kivooeo:a-lot-of-references-in-self, r=JonathanBrouwer
Add correct suggestion for multi-references for self type in method

Currently the suggestion for this code

```rust
fn main() {}

struct A {
    field: i32,
}

impl A {
    fn f(&&self) {}
}
```

looks like this, which is incorrect and missleading

```rust
   Compiling playground v0.0.1 (/playground)
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> src/main.rs:8:16
  |
8 |     fn f(&&self) {}
  |                ^ expected one of 9 possible tokens
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
  |
8 |     fn f(_: &&self) {}
  |          ++
```

So this fixes it and make more correct suggestions

```rust
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
 --> /home/gh-Kivooeo/test_/src/main.rs:8:16
  |
8 |     fn f(&&self) {}
  |                ^ expected one of 9 possible tokens
  |
help: `self` should be `self`, `&self` or `&mut self`, please remove extra references
  |
8 -     fn f(&&self) {}
8 +     fn f(&self) {}
```

Implementation is pretty self-documenting, but if you have suggestions on how to improve this (according to current test, which may be not fully covering all cases, this is works very well) or have some funny edge cases to show, I would appreciate it

r? compiler
2025-11-09 13:22:23 +11:00
..
auxiliary
elision Use unnamed lifetime spans as primary spans for MISMATCHED_LIFETIME_SYNTAXES 2025-08-23 22:11:43 +00:00
arbitrary-self-from-method-substs-ice.rs Do not consider traits that have unsatisfied const conditions to be conditionally const 2025-01-14 18:52:42 +00:00
arbitrary-self-from-method-substs-ice.stderr Do not consider traits that have unsatisfied const conditions to be conditionally const 2025-01-14 18:52:42 +00:00
arbitrary-self-from-method-substs-with-receiver.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary-self-from-method-substs-with-receiver.stderr Do not assume const params are printed after type params 2025-01-24 16:51:20 +00:00
arbitrary-self-from-method-substs.default.stderr Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary-self-from-method-substs.feature.stderr Do not assume const params are printed after type params 2025-01-24 16:51:20 +00:00
arbitrary-self-from-method-substs.rs Reject generic self types. 2024-10-30 10:48:08 +00:00
arbitrary-self-opaque.rs Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
arbitrary-self-opaque.stderr Implement #[define_opaque] attribute for functions. 2025-03-11 12:05:02 +00:00
arbitrary-self-types-dyn-incompatible.curr.stderr Compiler: Finalize dyn compatibility renaming 2025-01-26 21:20:31 +01:00
arbitrary-self-types-dyn-incompatible.rs Deduplicate dyn compatibility violations due to coercion 2025-05-26 10:57:54 +00:00
arbitrary-self-types-dyn-incompatible.stderr Use more detailed spans in dyn compat errors within bodies 2025-05-27 08:18:11 +00:00
arbitrary_self_type_infinite_recursion.rs UI tests: add missing diagnostic kinds where possible 2025-04-08 23:06:31 +03:00
arbitrary_self_type_infinite_recursion.stderr Use tcx.short_string() in more diagnostics 2025-08-07 21:18:00 +00:00
arbitrary_self_type_mut_difference.rs
arbitrary_self_type_mut_difference.stderr Show diff suggestion format on verbose replacement 2025-02-10 20:21:39 +00:00
arbitrary_self_types_by_value_reborrow.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_dispatch_to_vtable.rs Make sure to use Receiver trait when extracting object method candidate 2025-01-07 02:27:15 +00:00
arbitrary_self_types_generic_over_receiver.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_generic_over_receiver.stderr Rework borrowing suggestions to use Expr instead of just Span 2025-07-10 17:23:29 +00:00
arbitrary_self_types_generic_receiver.rs Arbitrary self types v2: generics test. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_generic_receiver.stderr Arbitrary self types v2: generics test. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_lifetime_elision.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_needing_box_or_arc_wrapping.fixed
arbitrary_self_types_needing_box_or_arc_wrapping.rs
arbitrary_self_types_needing_box_or_arc_wrapping.stderr
arbitrary_self_types_needing_mut_pin.fixed
arbitrary_self_types_needing_mut_pin.rs
arbitrary_self_types_needing_mut_pin.stderr
arbitrary_self_types_nested.rs
arbitrary_self_types_niche_deshadowing.rs Arbitrary self types v2: niche deshadowing test 2024-12-19 12:02:08 +00:00
arbitrary_self_types_no_generics.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_nonnull.rs Arbitrary self types v2: Weak, NonNull hints 2024-12-14 20:27:15 +00:00
arbitrary_self_types_nonnull.stderr Arbitrary self types v2: Weak, NonNull hints 2024-12-14 20:27:15 +00:00
arbitrary_self_types_not_allow_call_with_no_deref.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_not_allow_call_with_no_deref.stderr Use tcx.short_string() in more diagnostics 2025-08-07 21:18:00 +00:00
arbitrary_self_types_pin_getref.feature.stderr Arbitrary self types v2: no deshadow pre feature. 2024-12-20 12:29:00 +00:00
arbitrary_self_types_pin_getref.rs Arbitrary self types v2: no deshadow pre feature. 2024-12-20 12:29:00 +00:00
arbitrary_self_types_pin_lifetime-async.rs
arbitrary_self_types_pin_lifetime.rs
arbitrary_self_types_pin_lifetime_impl_trait-async.rs
arbitrary_self_types_pin_lifetime_impl_trait-async.stderr Bless test fallout 2024-08-17 12:43:25 -04:00
arbitrary_self_types_pin_lifetime_impl_trait.rs
arbitrary_self_types_pin_lifetime_impl_trait.stderr Bless test fallout 2024-08-17 12:43:25 -04:00
arbitrary_self_types_pin_lifetime_mismatch-async.rs UI tests: add missing diagnostic kinds where possible 2025-04-08 23:06:31 +03:00
arbitrary_self_types_pin_lifetime_mismatch-async.stderr Bless ui tests. 2024-10-04 23:38:41 +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_needing_borrow.rs Use tcx.short_string() in more diagnostics 2025-08-07 21:18:00 +00:00
arbitrary_self_types_pin_needing_borrow.stderr Use tcx.short_string() in more diagnostics 2025-08-07 21:18:00 +00:00
arbitrary_self_types_pointers_and_wrappers.rs Unimplement unsized_locals 2025-06-13 01:16:36 +02:00
arbitrary_self_types_raw_pointer_struct.rs Arbitrary self types v2: pointers feature gate. 2024-08-27 17:32:35 +00:00
arbitrary_self_types_raw_pointer_trait.rs Arbitrary self types v2: pointers feature gate. 2024-08-27 17:32:35 +00:00
arbitrary_self_types_recursive_receiver.rs Arbitrary self types v2: explain test. 2025-01-27 11:11:27 +00:00
arbitrary_self_types_shadowing_val_constptr.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_silly.rs
arbitrary_self_types_stdlib_pointers.rs
arbitrary_self_types_struct.rs
arbitrary_self_types_struct_receiver_trait.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_trait.rs
arbitrary_self_types_trait_receiver_trait.rs Arbitrary self types v2: use Receiver trait 2024-12-11 11:59:12 +00:00
arbitrary_self_types_unshadowing.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_unshadowing.stderr Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_unshadowing_ptrs.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_unshadowing_ptrs.stderr Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
arbitrary_self_types_unsized_struct.rs
arbitrary_self_types_weak.rs Arbitrary self types v2: Weak, NonNull hints 2024-12-14 20:27:15 +00:00
arbitrary_self_types_weak.stderr Arbitrary self types v2: Weak, NonNull hints 2024-12-14 20:27:15 +00:00
builtin-superkinds-self-type.rs
by-value-self-in-mut-slot.rs
class-missing-self.rs
class-missing-self.stderr
conflicting_inner.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
conflicting_inner2.rs Arbitrary self types v2: detect shadowing problems. 2024-12-11 11:59:13 +00:00
dispatch-dyn-incompatible-that-does-not-deref.rs Do not try to confirm non-dyn compatible method 2025-05-23 12:20:35 +00:00
dispatch-dyn-incompatible-that-does-not-deref.stderr Do not try to confirm non-dyn compatible method 2025-05-23 12:20:35 +00:00
dispatch-from-dyn-layout-2.rs Remove unnecessary layout assertions for object-safe receivers 2025-02-04 01:50:31 +00:00
dispatch-from-dyn-layout-3.rs Remove unnecessary layout assertions for object-safe receivers 2025-02-04 01:50:31 +00:00
dispatch-from-dyn-layout.rs Remove unnecessary layout assertions for object-safe receivers 2025-02-04 01:50:31 +00:00
dispatch-from-dyn-zst-transmute-zst-nonzst.rs Consolidate and rework CoercePointee and DispatchFromDyn errors 2025-02-24 19:34:54 +00:00
dispatch-from-dyn-zst-transmute-zst-nonzst.stderr Consolidate and rework CoercePointee and DispatchFromDyn errors 2025-02-24 19:34:54 +00:00
dispatch-from-dyn-zst-transmute.rs Consolidate and rework CoercePointee and DispatchFromDyn errors 2025-02-24 19:34:54 +00:00
dispatch-from-dyn-zst-transmute.stderr Consolidate and rework CoercePointee and DispatchFromDyn errors 2025-02-24 19:34:54 +00:00
dyn-compatibility-sized-self-by-value-self.rs UI tests: Rename "object safe" to "dyn compatible" 2024-10-10 01:13:29 +02:00
dyn-compatibility-sized-self-generic-method.rs UI tests: Rename "object safe" to "dyn compatible" 2024-10-10 01:13:29 +02:00
dyn-compatibility-sized-self-return-Self.rs UI tests: Rename "object safe" to "dyn compatible" 2024-10-10 01:13:29 +02:00
dyn-dispatch-do-not-mix-normalized-and-unnormalized.rs Do not mix normalized and unnormalized caller bounds when constructing param-env for receiver_is_dispatchable 2025-03-30 02:39:19 +00:00
dyn-dispatch-requires-supertrait-norm.rs Elaborate param-env built for checking DispatchFromDyn for dyn compat 2025-03-11 16:32:56 +00:00
dyn-dispatch-requires-supertrait.rs Elaborate param-env built for checking DispatchFromDyn for dyn compat 2025-03-11 16:32:56 +00:00
explicit-self-closures.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
explicit-self-generic.rs
explicit-self-objects-uniq.rs
explicit-self.rs
explicit_self_xcrate_exe.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
invalid-self-dyn-receiver.rs Delay bug when method confirmation cannot upcast object pick of self 2025-02-04 04:23:04 +00:00
invalid-self-dyn-receiver.stderr Delay bug when method confirmation cannot upcast object pick of self 2025-02-04 04:23:04 +00:00
issue-61882-2.rs
issue-61882-2.stderr
issue-61882.rs
issue-61882.stderr
lot-of-references-self.rs add parser check for multi-reference self 2025-11-08 13:39:40 +00:00
lot-of-references-self.stderr add parser check for multi-reference self 2025-11-08 13:39:40 +00:00
move-self.rs
objects-owned-object-owned-method.rs
phantomdata-in-coerce-and-dispatch-impls.rs Normalize field before checking PhantomData in coerce/dispatch impl validation 2025-01-14 18:47:23 +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-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 Point at inner item on E0401 generated by hir_typeck 2025-11-01 19:09:56 +00: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 Point at inner item on E0401 generated by hir_typeck 2025-11-01 19:09:56 +00:00
self-impl-2.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
self-impl.rs
self-impl.stderr Preserve generic args in suggestions for ambiguous associated items 2025-05-06 17:04:03 +02:00
self-in-mut-slot-default-method.rs
self-in-mut-slot-immediate-value.rs
self-in-typedefs.rs
self-infer.rs Replace ItemCtxt::report_placeholder_type_error match with a call to TyCtxt::def_descr 2025-06-30 20:36:16 +02:00
self-infer.stderr Replace ItemCtxt::report_placeholder_type_error match with a call to TyCtxt::def_descr 2025-06-30 20:36:16 +02:00
self-re-assign.rs
self-shadowing-import.rs Use non-2015 edition paths in tests that do not test for their resolution 2025-06-05 09:19:31 +02:00
self-type-param.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08: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
self_lifetime-async.rs Reword mismatched-lifetime-syntaxes text based on feedback 2025-07-14 09:52:58 -04:00
self_lifetime-async.stderr Use unnamed lifetime spans as primary spans for MISMATCHED_LIFETIME_SYNTAXES 2025-08-23 22:11:43 +00:00
self_lifetime.rs Reword mismatched-lifetime-syntaxes text based on feedback 2025-07-14 09:52:58 -04:00
self_lifetime.stderr Use unnamed lifetime spans as primary spans for MISMATCHED_LIFETIME_SYNTAXES 2025-08-23 22:11:43 +00:00
self_type_keyword-2.rs
self_type_keyword-2.stderr
self_type_keyword.rs Update ui tests with new macro early erroring 2025-07-25 23:46:28 +02:00
self_type_keyword.stderr Update ui tests with new macro early erroring 2025-07-25 23:46:28 +02:00
self_type_macro_name.rs Update ui tests with new macro early erroring 2025-07-25 23:46:28 +02:00
self_type_macro_name.stderr Update ui tests with new macro early erroring 2025-07-25 23:46:28 +02:00
string-self-append.rs
suggest-self-2.rs
suggest-self-2.stderr
suggest-self.rs
suggest-self.stderr
ufcs-explicit-self.rs
uniq-self-in-mut-slot.rs
where-for-self.rs Update tests for hidden references to mutable static 2024-09-13 14:10:56 +03:00