Open
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
chenyukang commentedon Aug 21, 2022
@rustbot claim
chenyukang commentedon 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:
rust/compiler/rustc_typeck/src/check/demand.rs
Line 704 in f07d6e8
The result is also not right:
The
res
is fromformat!
: