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

Compiler's help suggestion is incorrect when passing Option<String> into a function expecting Option<&str> #107604

Closed
scizzorz opened this issue Feb 2, 2023 · 2 comments · Fixed by #107633
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scizzorz
Copy link

scizzorz commented Feb 2, 2023

Code

fn foo(_s: Option<&str>) {
}

fn main() {
    let s = Some("".to_string());
    foo(s);
}

// AND:

fn foo(_s: Option<&str>) {
}

fn main() {
    let s = Some("".to_string());
    foo(s.map(|x| &*x));
}

Current output

error[E0308]: mismatched types
 --> foo.rs:6:9
  |
6 |     foo(s);
  |     --- ^ expected `&str`, found struct `String`
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected enum `Option<&str>`
             found enum `Option<String>`
note: function defined here
 --> foo.rs:1:4
  |
1 | fn foo(_s: Option<&str>) {
  |    ^^^ ----------------
help: try converting the passed type into a `&str`
  |
6 |     foo(s.map(|x| &*x));
  |          +++++++++++++

error: aborting due to previous error

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

// AND:

error[E0515]: cannot return value referencing function parameter `x`
 --> foo.rs:6:19
  |
6 |     foo(s.map(|x| &*x));
  |                   ^^-
  |                   | |
  |                   | `x` is borrowed here
  |                   returns a value referencing data owned by the current function

error: aborting due to previous error

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

Desired output

I believe the help suggestion should be valid code. I was able to solve this error with:

fn foo(s: Option<&str>) {
}

fn main() {
    let s = Some("".to_string());
    foo(s.as_ref().map(AsRef::as_ref));
}

Rationale and extra context

rustc's help suggestions are often extremely helpful, but in this case the suggested change simply does not work and also doesn't provide any further help or explanation about why it doesn't work.

Other cases

No response

Anything else?

No response

@scizzorz scizzorz added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 2, 2023
@kadiwa4
Copy link
Contributor

kadiwa4 commented Feb 2, 2023

It should preferably suggest foo(s.as_deref());

@clubby789
Copy link
Contributor

clubby789 commented Feb 3, 2023

as_deref doesn't work for Option<&String> -> Option<&str>, discussed in the original issue which resulted in the first diagnostic #89856
Misread the issue!

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 3, 2023
…, r=Nilstrieb

Fix suggestion for coercing Option<&String> to Option<&str>

Fixes rust-lang#107604

This also makes the diagnostic `MachineApplicable`, and runs `rustfix` to check we're not producing incorrect code.

`@rustbot` label +A-diagnostics
@bors bors closed this as completed in c927027 Feb 3, 2023
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 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.

3 participants