Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On borrow return type, suggest borrowing from arg or owned return type #117914

Merged
merged 9 commits into from
Dec 12, 2023

Conversation

estebank
Copy link
Contributor

When we encounter a function with a return type that has an anonymous lifetime with no argument to borrow from, besides suggesting the 'static lifetime we now also suggest changing the arguments to be borrows or changing the return type to be an owned type.

error[E0106]: missing lifetime specifier
  --> $DIR/variadic-ffi-6.rs:7:6
   |
LL | ) -> &usize {
   |      ^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
   |
LL | ) -> &'static usize {
   |       +++++++
help: instead, you are more likely to want to change one of the arguments to be borrowed...
   |
LL |     x: &usize,
   |        +
help: ...or alternatively, to want to return an owned value
   |
LL - ) -> &usize {
LL + ) -> usize {
   |

Fix #85843.

@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2023
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Nov 14, 2023
@bors

This comment was marked as resolved.

@estebank estebank force-pushed the issue-85843 branch 2 times, most recently from 61bc7d1 to 3e26c52 Compare November 16, 2023 16:55
@bors
Copy link
Contributor

bors commented Nov 19, 2023

☔ The latest upstream changes (presumably #117683) made this pull request unmergeable. Please resolve the merge conflicts.

When we encounter a function with a return type that has an anonymous
lifetime with no argument to borrow from, besides suggesting the
`'static` lifetime we now also suggest changing the arguments to be
borrows or changing the return type to be an owned type.

```
error[E0106]: missing lifetime specifier
  --> $DIR/variadic-ffi-6.rs:7:6
   |
LL | ) -> &usize {
   |      ^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
   |
LL | ) -> &'static usize {
   |       +++++++
help: instead, you are more likely to want to change one of the arguments to be borrowed...
   |
LL |     x: &usize,
   |        +
help: ...or alternatively, to want to return an owned value
   |
LL - ) -> &usize {
LL + ) -> usize {
   |
```

Fix rust-lang#85843.
When encountering
```rust
fn g(mut x: impl Iterator<Item = &()>) -> Option<&()> { /* */ }
```
Suggest
```rust
fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { /* */ }
```
Comment on lines +2957 to +2958
", but this is uncommon unless you're returning a borrowed value from a \
`const` or a `static`{owned}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make these messages translatable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should, but I didn't want to make the PR too big to review.

@wesleywiser
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Dec 12, 2023

📌 Commit eee4cc6 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 12, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 12, 2023
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#116740 (dont ICE when ConstKind::Expr for is_const_evaluatable)
 - rust-lang#117914 (On borrow return type, suggest borrowing from arg or owned return type)
 - rust-lang#117927 (Clarify how to choose a FutureIncompatibilityReason variant.)
 - rust-lang#118855 (Improve an error involving attribute values.)
 - rust-lang#118856 (rustdoc-search: clean up parser)
 - rust-lang#118865 (rustc_codegen_llvm: Enforce `rustc::potential_query_instability` lint)
 - rust-lang#118866 (llvm-wrapper: adapt for LLVM API change)
 - rust-lang#118868 (Correctly gate the parsing of match arms without body)
 - rust-lang#118877 (tests: CGU tests require build-pass, not check-pass (remove FIXME))

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ffdb471 into rust-lang:master Dec 12, 2023
11 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 12, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 12, 2023
Rollup merge of rust-lang#117914 - estebank:issue-85843, r=wesleywiser

On borrow return type, suggest borrowing from arg or owned return type

When we encounter a function with a return type that has an anonymous lifetime with no argument to borrow from, besides suggesting the `'static` lifetime we now also suggest changing the arguments to be borrows or changing the return type to be an owned type.

```
error[E0106]: missing lifetime specifier
  --> $DIR/variadic-ffi-6.rs:7:6
   |
LL | ) -> &usize {
   |      ^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
   |
LL | ) -> &'static usize {
   |       +++++++
help: instead, you are more likely to want to change one of the arguments to be borrowed...
   |
LL |     x: &usize,
   |        +
help: ...or alternatively, to want to return an owned value
   |
LL - ) -> &usize {
LL + ) -> usize {
   |
```

Fix rust-lang#85843.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest borrowing from argument when return type is a borrow without an applicable lifetime
5 participants