Skip to content

"Consider borrowing here" message does not show up when argument is a format call #100830

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
Kampfkarren opened this issue Aug 21, 2022 · 2 comments
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

@Kampfkarren
Copy link
Contributor

Kampfkarren commented Aug 21, 2022

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

fn takes_str(_: &str) {}

fn main() {
    takes_str(String::from("e"));
    takes_str(format!("e"));
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:4:15
  |
4 |     takes_str(String::from("e"));
  |     --------- ^^^^^^^^^^^^^^^^^
  |     |         |
  |     |         expected `&str`, found struct `String`
  |     |         help: consider borrowing here: `&String::from("e")`
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn takes_str(_: &str) {}
  |    ^^^^^^^^^ -------

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:5:15
  |
5 |     takes_str(format!("e"));
  |               ^^^^^^^^^^^^ expected `&str`, found struct `String`
  |
  = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

Using String::from directly tells us we can borrow it with &. Using format! does not.

@Kampfkarren Kampfkarren 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 21, 2022
@chenyukang
Copy link
Member

@rustbot claim

@chenyukang
Copy link
Member

chenyukang commented Aug 31, 2022

I spent some time to debug this issue. If the span is from a macro, it's difficult to get the right
diagonstic message, this message is removed by PR: #85937

Note there a special note: this error originates in the macro format

If we remove this check at:

if in_external_macro(sess, sp) {

The result is also not right:

error[E0308]: mismatched types
   --> /home/cat/rust/library/alloc/src/macros.rs:117:9
    |
114 | macro_rules! format {
    | ------------------- in this expansion of `format!`
...
117 |         res
    |         ^^^
    |         |
    |         expected `&str`, found struct `String`
    |         help: consider borrowing here: `&res`
    |
   ::: a.rs:5:15
    |
5   |     takes_str(format!("e"));
    |               ------------ in this macro invocation

error: aborting due to previous error

The res is from format!:

macro_rules! format {
    ($($arg:tt)*) => {{
        let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
        res
    }}
}

@chenyukang chenyukang removed their assignment Sep 18, 2022
# for free to join this conversation on GitHub. Already have an account? # 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

No branches or pull requests

2 participants