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

rustc suggests &None.as_ref() as well as None #100605

Closed
tgeoghegan opened this issue Aug 15, 2022 · 2 comments · Fixed by #100617
Closed

rustc suggests &None.as_ref() as well as None #100605

tgeoghegan opened this issue Aug 15, 2022 · 2 comments · Fixed by #100617
Assignees
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

@tgeoghegan
Copy link

tgeoghegan commented Aug 15, 2022

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9d89810932e63366d4910a3d8cca200e

fn takes_option(_arg: Option<&String>) {}

fn main() {
    takes_option(&None)
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:4:18
  |
4 |     takes_option(&None)
  |     ------------ ^^^^^ expected enum `Option`, found `&Option<_>`
  |     |
  |     arguments to this function are incorrect
  |
  = note:   expected enum `Option<&String>`
          found reference `&Option<_>`
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn takes_option(_arg: Option<&String>) {}
  |    ^^^^^^^^^^^^ ---------------------
help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`
  |
4 |     takes_option(&None.as_ref())
  |                  ~~~~~~~~~~~~~~
help: consider removing the borrow
  |
4 -     takes_option(&None)
4 +     takes_option(None)
  |

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

The error is correct, but I think the diagnostic is wrong. The second recommendation to call takes_option(None) is sound, but the first one (&None.as_ref()) seems like bad advice.

This reproduces on rustc 1.61 (on my workstation), as well as 1.63 (stable channel in playground), 1.64 (beta channel in playground), and 1.65 (nightly channel in playground).

@tgeoghegan tgeoghegan 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 Aug 15, 2022
@chenyukang
Copy link
Member

Intresting..

This code also make wrong help, https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9d89810932e63366d4910a3d8cca200e

fn takes_option(_arg: Option<&String>) {}

fn main() {
    let x = String::from("x");
    let res = Some(x);
    takes_option(&res);
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:18
  |
6 |     takes_option(&res);
  |     ------------ ^^^^
  |     |            |
  |     |            expected enum `Option`, found `&Option<String>`
  |     |            help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`: `&res.as_ref()`
  |     arguments to this function are incorrect
  |
  = note:   expected enum `Option<&String>`
          found reference `&Option<String>`
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn takes_option(_arg: Option<&String>) {}
  |    ^^^^^^^^^^^^ ---------------------

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

Using &res.as_ref() is a bad advice.

@chenyukang
Copy link
Member

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Aug 20, 2022
…errors

Suggest the right help message for as_ref

Fixes rust-lang#100605
@bors bors closed this as completed in 61a529d Aug 20, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 5, 2023
Rename tests/ui/issues/issue-100605.rs to ../type/option-ref-advice.rs

The test is a regression test for a [bug ](rust-lang#100605) where the compiler gave bad advice for an `Option<&String>`. Rename the file appropriately.

Part of rust-lang#73494
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.

2 participants