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

Wrong diagnostic when returning _ with a lifetime #125488

Closed
dev-ardi opened this issue May 24, 2024 · 4 comments · Fixed by #127110
Closed

Wrong diagnostic when returning _ with a lifetime #125488

dev-ardi opened this issue May 24, 2024 · 4 comments · Fixed by #127110
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dev-ardi
Copy link
Contributor

dev-ardi commented May 24, 2024

The code

struct S<'a>(&'a ());
fn f(s: S<'_>) -> _ {
    s
}

Emits this diagnostic, which is wrong in this case.

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> src/lib.rs:2:19
  |
2 | fn f(s: S<'_>) -> _ {
  |                   ^
  |                   |
  |                   not allowed in type signatures
  |                   help: replace with the correct return type: `S<'static>`

For more information about this error, try `rustc --explain E0121`.

It should say help: replace with the correct return type: S<'_>
cc: rust-lang/rust-analyzer#16676

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 24, 2024
@jieyouxu

This comment was marked as resolved.

@jieyouxu
Copy link
Member

Did you mean to write this code snippet instead?

struct S<'a>(&'a ());
fn f(s: S<'_>) -> _ {
    s
}

@dev-ardi
Copy link
Contributor Author

dev-ardi commented May 24, 2024

Ooops yes 😅, edited

@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 24, 2024
@surechen
Copy link
Contributor

@rustbot claim

surechen added a commit to surechen/rust that referenced this issue Jun 5, 2024
surechen added a commit to surechen/rust that referenced this issue Jun 25, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 29, 2024
…-errors

Fix a error suggestion for E0121 when using placeholder _ as return types on function signature.

Recommit after refactoring based on comment:
rust-lang#126017 (comment)

But when changing return type's lifetime to `ReError` will affect the subsequent borrow check process and cause test11 in typeck_type_placeholder_item.rs to lost E0515 message.
```rust
fn test11(x: &usize) -> &_ {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
    &x //~ ERROR cannot return reference to function parameter(this E0515 msg will disappear)
}
```

fixes rust-lang#125488

r? `@pnkfelix`
@bors bors closed this as completed in 80cf576 Jun 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 30, 2024
Rollup merge of rust-lang#127110 - surechen:fix_125488_06, r=compiler-errors

Fix a error suggestion for E0121 when using placeholder _ as return types on function signature.

Recommit after refactoring based on comment:
rust-lang#126017 (comment)

But when changing return type's lifetime to `ReError` will affect the subsequent borrow check process and cause test11 in typeck_type_placeholder_item.rs to lost E0515 message.
```rust
fn test11(x: &usize) -> &_ {
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
    &x //~ ERROR cannot return reference to function parameter(this E0515 msg will disappear)
}
```

fixes rust-lang#125488

r? ``@pnkfelix``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants