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
271 changes: 259 additions & 12 deletions compiler/rustc_resolve/src/late/diagnostics.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
const ENTRY_LIMIT: usize = 900;
// FIXME: The following limits should be reduced eventually.
const ISSUES_ENTRY_LIMIT: usize = 1852;
const ROOT_ENTRY_LIMIT: usize = 867;
const ROOT_ENTRY_LIMIT: usize = 866;

const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
"rs", // test source files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ LL | fn elision<T: Fn() -> &i32>() {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn elision<T: Fn() -> &'static i32>() {
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL - fn elision<T: Fn() -> &i32>() {
LL + fn elision<T: Fn() -> i32>() {
|

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ LL | fn elision(_: fn() -> &i32) {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn elision(_: fn() -> &'static i32) {
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL - fn elision(_: fn() -> &i32) {
LL + fn elision(_: fn() -> i32) {
|

error: aborting due to previous error

Expand Down
11 changes: 10 additions & 1 deletion tests/ui/c-variadic/variadic-ffi-6.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ 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
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, you might want to return an owned value
|
LL - ) -> &usize {
LL + ) -> usize {
|

error: aborting due to previous error

Expand Down
8 changes: 0 additions & 8 deletions tests/ui/foreign-fn-return-lifetime.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/foreign-fn-return-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// run-rustfix

extern "C" {
pub fn g(_: &u8) -> &u8; // OK
pub fn f() -> &u8; //~ ERROR missing lifetime specifier
Expand Down
9 changes: 7 additions & 2 deletions tests/ui/foreign-fn-return-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
error[E0106]: missing lifetime specifier
--> $DIR/foreign-fn-return-lifetime.rs:5:19
--> $DIR/foreign-fn-return-lifetime.rs:3:19
|
LL | pub fn f() -> &u8;
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | pub fn f() -> &'static u8;
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL - pub fn f() -> &u8;
LL + pub fn f() -> u8;
|

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-70304.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | fn create_doc() -> impl Document<Cursor<'_> = DocCursorImpl<'_>> {
| ^^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
|
LL | fn create_doc() -> impl Document<Cursor<'_> = DocCursorImpl<'static>> {
| ~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/impl-fn-hrtb-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | fn d() -> impl Fn() -> (impl Debug + '_) {
| ^^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
|
LL | fn d() -> impl Fn() -> (impl Debug + 'static) {
| ~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/issues/issue-13497.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ LL | &str
| ^ 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
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 str
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL | String
| ~~~~~~

error: aborting due to previous error

Expand Down
16 changes: 14 additions & 2 deletions tests/ui/lifetimes/issue-26638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &'static str { iter() }
| +++++++
help: instead, you are more likely to want to change the argument to be borrowed...
|
LL | fn parse_type_2(iter: &fn(&u8)->&u8) -> &str { iter() }
| +
help: ...or alternatively, you might want to return an owned value
|
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> String { iter() }
| ~~~~~~

error[E0106]: missing lifetime specifier
--> $DIR/issue-26638.rs:10:22
Expand All @@ -29,10 +37,14 @@ LL | fn parse_type_3() -> &str { unimplemented!() }
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn parse_type_3() -> &'static str { unimplemented!() }
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL | fn parse_type_3() -> String { unimplemented!() }
| ~~~~~~

error[E0308]: mismatched types
--> $DIR/issue-26638.rs:1:69
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ LL | fn f() -> &isize {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn f() -> &'static isize {
| +++++++
help: instead, you are more likely to want to return an owned value
|
LL - fn f() -> &isize {
LL + fn f() -> isize {
|

error[E0106]: missing lifetime specifier
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:7:33
Expand Down Expand Up @@ -41,10 +46,19 @@ LL | fn i(_x: isize) -> &isize {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn i(_x: isize) -> &'static isize {
| +++++++
help: instead, you are more likely to want to change the argument to be borrowed...
|
LL | fn i(_x: &isize) -> &isize {
| +
help: ...or alternatively, you might want to return an owned value
|
LL - fn i(_x: isize) -> &isize {
LL + fn i(_x: isize) -> isize {
|

error[E0106]: missing lifetime specifier
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:34:24
Expand All @@ -53,10 +67,19 @@ LL | fn j(_x: StaticStr) -> &isize {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn j(_x: StaticStr) -> &'static isize {
| +++++++
help: instead, you are more likely to want to change the argument to be borrowed...
|
LL | fn j(_x: &StaticStr) -> &isize {
| +
help: ...or alternatively, you might want to return an owned value
|
LL - fn j(_x: StaticStr) -> &isize {
LL + fn j(_x: StaticStr) -> isize {
|

error[E0106]: missing lifetime specifier
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:40:49
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/self/elision/nested-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ LL | fn wrap(self: Wrap<{ fn bar(&self) {} }>) -> &() {
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn wrap(self: Wrap<{ fn bar(&self) {} }>) -> &'static () {
| +++++++
help: instead, you are more likely to want to change the argument to be borrowed...
|
LL | fn wrap(self: &Wrap<{ fn bar(&self) {} }>) -> &() {
| +
help: ...or alternatively, you might want to return an owned value
|
LL - fn wrap(self: Wrap<{ fn bar(&self) {} }>) -> &() {
LL + fn wrap(self: Wrap<{ fn bar(&self) {} }>) -> () {
|

error[E0412]: cannot find type `Wrap` in this scope
--> $DIR/nested-item.rs:5:15
Expand Down
66 changes: 60 additions & 6 deletions tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ LL | fn g(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn g(mut x: impl Iterator<Item = &()>) -> Option<&'static ()> { x.next() }
| +++++++
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
help: alternatively, you might want to return an owned value
|
LL - fn g(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
LL + fn g(mut x: impl Iterator<Item = &()>) -> Option<()> { x.next() }
|

error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:19:60
Expand All @@ -17,10 +26,19 @@ LL | async fn i(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next()
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | async fn i(mut x: impl Iterator<Item = &()>) -> Option<&'static ()> { x.next() }
| +++++++
help: consider introducing a named lifetime parameter
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
help: alternatively, you might want to return an owned value
|
LL - async fn i(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
LL + async fn i(mut x: impl Iterator<Item = &()>) -> Option<()> { x.next() }
|

error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:27:58
Expand All @@ -29,10 +47,19 @@ LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next()
| ^^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
|
LL | fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
help: alternatively, you might want to return an owned value
|
LL - fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
LL + fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|

error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:37:64
Expand All @@ -41,10 +68,19 @@ LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.n
| ^^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
|
LL | async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'static ()> { x.next() }
| ~~~~~~~
help: consider introducing a named lifetime parameter
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~ ~~~
help: alternatively, you might want to return an owned value
|
LL - async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
LL + async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|

error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37
Expand All @@ -53,10 +89,19 @@ LL | fn g(mut x: impl Foo) -> Option<&()> { x.next() }
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn g(mut x: impl Foo) -> Option<&'static ()> { x.next() }
| +++++++
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Foo) -> Option<&'a ()> { x.next() }
| ++++ ~~~
help: alternatively, you might want to return an owned value
|
LL - fn g(mut x: impl Foo) -> Option<&()> { x.next() }
LL + fn g(mut x: impl Foo) -> Option<()> { x.next() }
|

error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime-gated.rs:58:41
Expand All @@ -65,10 +110,19 @@ LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
| ^ 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
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`
|
LL | fn g(mut x: impl Foo<()>) -> Option<&'static ()> { x.next() }
| +++++++
help: consider introducing a named lifetime parameter
|
LL | fn g<'a>(mut x: impl Foo<()>) -> Option<&'a ()> { x.next() }
| ++++ ~~~
help: alternatively, you might want to return an owned value
|
LL - fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
LL + fn g(mut x: impl Foo<()>) -> Option<()> { x.next() }
|

error[E0658]: anonymous lifetimes in `impl Trait` are unstable
--> $DIR/impl-trait-missing-lifetime-gated.rs:6:35
Expand Down
Loading
Loading