rust/tests
Guillaume Gomez 5ee347ece4
Rollup merge of #132172 - dianne:suggest-borrow-generic, r=matthewjasper
borrowck diagnostics: suggest borrowing function inputs in generic positions

# Summary
This generalizes borrowck's existing suggestions to borrow instead of moving when passing by-value to a function that's generic in that input. Previously, this was special-cased to `AsRef`/`Borrow`-like traits and `Fn`-like traits. This PR changes it to test if, for a moved place with type `T`, that the callee's signature and clauses don't break if you substitute in `&T` or `&mut T`. For instance, it now works with `Read`/`Write`-like traits.

Fixes https://github.com/rust-lang/rust/issues/131413

# Incidental changes
- No longer spuriously suggests mutable borrows of closures in some situations (see e.g. the tests in [tests/ui/closures/2229_closure_analysis/](https://github.com/rust-lang/rust/compare/master...dianne:rust:suggest-borrow-generic?expand=1#diff-8dfb200c559f0995d0f2ffa2f23bc6f8041b263e264e5c329a1f4171769787c0)).
- No longer suggests cloning closures that implement `Fn`, since they can be borrowed (see e.g. [tests/ui/moves/borrow-closures-instead-of-move.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:suggest-borrow-generic?expand=1#diff-5db268aac405eec56d099a72d8b58ac46dab523cf013e29008104840168577fb)).

This keeps the behavior to suppress suggestions of `fn_once.clone()()`. I think it might make sense to suggest it with a "but this might not be your desired behavior" caveat, as is done when something is used after being consumed as the receiver for a method call. That's probably out of the scope of this PR though.

# Limitations and possible improvements
- This doesn't work for receivers of method calls. This is a small change, and I have it implemented locally, but I'm not sure it's useful on its own. In most cases I've found borrowing the receiver would change the call's output type (see below). In other cases (e.g. `Iterator::sum`), borrowing the receiver isn't useful since it's consumed.
- This doesn't work when it would change the call's output type. In general, I figure inserting references into the output type is an unwanted change. However, this also means it doesn't work in cases where the new output type would be effectively the same as the old one. For example, from the rand crate, the iterator returned by [`Rng::sample_iter`](https://docs.rs/rand/latest/rand/trait.Rng.html#method.sample_iter) is effectively the same (modulo regions) whether you borrow or consume the receiver `Rng`, so common usage involves borrowing it. I'm not sure whether the best approach is to add a more complex check of approximate equivalence, to forego checking the call's output type and give spurious suggestions, or to leave it as-is.
- This doesn't work when it would change the call's other input types. Instead, it could suggest borrowing any others that have the same parameter type (but only when suggesting shared borrows). I think this would be a pretty easy change, but I don't think it's very useful so long as the normalized output type can't change.

I'm happy to implement any of these (or other potential improvements to this), but I'm not sure which are common enough patterns to justify the added complexity. Please let me know if any sound worthwhile.
2024-11-14 15:16:08 +01:00
..
assembly tests: use max-llvm-major-version instead of ignore-llvm-version range like N - 99 2024-11-14 17:44:54 +08:00
auxiliary Fix typo in tests/auxiliary/minicore.rs 2024-11-03 17:32:33 +09:00
codegen tests: use max-llvm-major-version instead of ignore-llvm-version range like N - 99 2024-11-14 17:44:54 +08:00
codegen-units
coverage coverage: Restrict empty-span expansion to only cover { and } 2024-11-08 20:43:08 +11:00
coverage-run-rustdoc coverage: Restrict empty-span expansion to only cover { and } 2024-11-08 20:43:08 +11:00
crashes Auto merge of #132943 - matthiaskrgr:rollup-164l3ej, r=matthiaskrgr 2024-11-12 08:15:38 +00:00
debuginfo Auto merge of #123550 - GnomedDev:remove-initial-arc, r=Noratrieb 2024-10-24 13:35:50 +00:00
incremental
mir-opt coverage: Restrict empty-span expansion to only cover { and } 2024-11-08 20:43:08 +11:00
pretty Add pretty, ui, and feature-gate tests for the enzyme/autodiff frontend 2024-10-11 20:38:43 +02:00
run-make Re-enable version-verbose-commit-hash run-make test 2024-11-10 16:28:54 +01:00
rustdoc Rollup merge of #133000 - GuillaumeGomez:footnote-ids, r=notriddle 2024-11-13 21:04:26 +01:00
rustdoc-gui rustdoc-search: simplify rules for generics and type params 2024-10-30 12:27:48 -07:00
rustdoc-js Adjust ranking so that duplicates count against rank 2024-10-31 13:12:14 -07:00
rustdoc-js-std Adjust ranking so that duplicates count against rank 2024-10-31 13:12:14 -07:00
rustdoc-json Rename Receiver -> LegacyReceiver 2024-10-22 12:55:16 +00:00
rustdoc-ui Auto merge of #126597 - estebank:unicode-output, r=fmease 2024-11-11 00:00:58 +00:00
ui Rollup merge of #132172 - dianne:suggest-borrow-generic, r=matthewjasper 2024-11-14 15:16:08 +01:00
ui-fulldeps Fix error message for direct usage of sess.opts.crate_types 2024-11-10 10:37:53 +00:00
COMPILER_TESTS.md