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

Improve compile errors for type mismatch when encoutering unwrap_or #127545

Closed
tesuji opened this issue Jul 10, 2024 · 0 comments · Fixed by #127629
Closed

Improve compile errors for type mismatch when encoutering unwrap_or #127545

tesuji opened this issue Jul 10, 2024 · 0 comments · Fixed by #127629
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tesuji
Copy link
Contributor

tesuji commented Jul 10, 2024

Given this code:

use std::collections::HashMap;

pub fn get(index: &HashMap<usize, Vec<i32>>, id: usize) -> &[i32] {
    index.get(&id).unwrap_or(&[])
}

Current error, for the motivating use case above:

error[E0308]: mismatched types
   --> src/lib.rs:5:30
    |
5   |     index.get(&id).unwrap_or(&[])
    |                    --------- ^^^ expected `&Vec<i32>`, found `&[_; 0]`
    |                    |
    |                    arguments to this method are incorrect
    |
    = note: expected reference `&Vec<i32>`
               found reference `&[_; 0]`
help: the return type of this call is `&[_; 0]` due to the type of the argument passed
   --> src/lib.rs:5:5
    |
5   |     index.get(&id).unwrap_or(&[])
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^---^
    |                              |
    |                              this argument influences the return type of `unwrap_or`

It should be possible for rustc to suggest replacing this specific instantiation of unwrap_or with map_or(&[], Vec::as_slice). Maybe similarly for some other common cases: map_or("", String::as_str).

Originally posted by @dtolnay in rust-lang/libs-team#407 (comment)

@rustbot label -T-rustdoc T-compiler

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jul 10, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 15, 2024
…r, r=estebank

Suggest using `map_or` when `Option<&T>::unwrap_or where T: Deref` fails

Fix rust-lang#127545

Split from rust-lang#127596 (review)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 15, 2024
…r, r=estebank

Suggest using `map_or` when `Option<&T>::unwrap_or where T: Deref` fails

Fix rust-lang#127545

Split from rust-lang#127596 (review)
@bors bors closed this as completed in cae4a84 Jul 16, 2024
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 C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants