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

suggest Option::as_deref(_mut) on type mismatch in option combinator if it passes typeck #111659

Merged
merged 2 commits into from
Jun 3, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented May 16, 2023

Fixes #106342.
This adds a suggestion to call .as_deref() (or .as_deref_mut() resp.) if typeck fails due to a type mismatch in the function passed to an Option combinator such as .map() or .and_then().
For example:

fn foo(_: &str) {}
Some(String::new()).map(foo);

The .map() method requires its argument to satisfy F: FnOnce(String), but it received fn(&str), which won't pass. However, placing a .as_deref() before the .map() call fixes this since &str == &<String as Deref>::Target

@rustbot
Copy link
Collaborator

rustbot commented May 16, 2023

r? @cjgillot

(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 May 16, 2023
//~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}`
let _ = produces_string().and_then(no_args);
//~^ ERROR function is expected to take 1 argument, but it takes 0 arguments
let _ = produces_string().and_then(generic_ref);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this case suggest as_ref?

Copy link
Member Author

Choose a reason for hiding this comment

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

I initially had it suggest as_ref() where applicable too, but there's already a help message for calling and_then with an fn(&T) on some Option<T>, though it suggests removing the borrow on the function parameter and I'm not sure how it would work out when there are two suggestions made by the compiler (is that fine?)

help: do not borrow the argument
     |
14   - fn generic<T>(_: &T) {}
14   + fn generic<T>(_: T) {}
     |
help: call `Option::as_ref()` first
     |
18   |     let x = produces_string().as_ref().and_then(generic);
     |                              +++++++++

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

One nit and r=me.
@bors rollup

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 3, 2023
@y21
Copy link
Member Author

y21 commented Jun 3, 2023

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 3, 2023
@cjgillot
Copy link
Contributor

cjgillot commented Jun 3, 2023

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 3, 2023

📌 Commit 268b08b has been approved by cjgillot

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 Jun 3, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 3, 2023
suggest `Option::as_deref(_mut)` on type mismatch in option combinator if it passes typeck

Fixes rust-lang#106342.
This adds a suggestion to call `.as_deref()` (or `.as_deref_mut()` resp.) if typeck fails due to a type mismatch in the function passed to an `Option` combinator such as `.map()` or `.and_then()`.
For example:
```rs
fn foo(_: &str) {}
Some(String::new()).map(foo);
```
The `.map()` method requires its argument to satisfy `F: FnOnce(String)`, but it received `fn(&str)`, which won't pass. However, placing a `.as_deref()` before the `.map()` call fixes this since `&str == &<String as Deref>::Target`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 3, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#111659 (suggest `Option::as_deref(_mut)` on type mismatch in option combinator if it passes typeck)
 - rust-lang#111702 (Option::map_or_else: Show an example of integrating with Result)
 - rust-lang#111878 (Fix codegen test suite for bare-metal-like targets)
 - rust-lang#111969 (bootstrap: Make `clean` respect `dry-run`)
 - rust-lang#111998 (Add other workspaces to `linkedProjects` in rust_analyzer_settings)
 - rust-lang#112215 (only suppress coercion error if type is definitely unsized)
 - rust-lang#112231 (Make sure the build.rustc version is either the same or 1 apart (revised))

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 91f222f into rust-lang:master Jun 3, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Calling and_then on Option<String> with a function that takes &str could provide a more helpful diagnostic
4 participants