-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Trait bounds that would be satisfied if autoderef was applied are not considered for function calls but are considered when using methods via the dot-operator #102839
Comments
Updated to make it clear that the very same currently works fine via autoderef and the dot-operator. |
This is also a problem with fn print_a<'a>(s: impl Into<Option<&'a str>>) {
match s.into() {
None => println!("none"),
Some(s) => println!("{}", s),
}
}
fn print_b(s: Option<&str>) {
match s {
None => println!("none"),
Some(s) => println!("{}", s),
}
}
fn main() {
print_a("123");
print_a(None);
// Doesn't compile:
print_a(&String::from("123"));
print_a(&*String::from("123"));
print_b(Some("123"));
print_b(None);
print_b(Some(&String::from("123")));
print_b(Some(&*String::from("123")));
} This gives the following error:
CC @fengalin |
Triage: I'm a bit confused, because the code you posted fails to compile because of a missing #[derive(Debug)]
struct D(String);
impl std::ops::Deref for D {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
fn print(val: &(impl std::fmt::Debug + ?Sized)) {
println!("{:?}", val);
}
trait T {
fn print(&self);
}
impl T for str {
fn print(&self) {
println!("{}", self);
}
}
fn main() {
print("123");
print(&String::from("123"));
// XXX: Doesn't compile but probably should via autoderef
print(&D(String::from("123")));
// because the below compiles
print(&*D(String::from("123")));
"123".print();
D(String::from("123")).print();
} Your second examples does not compile though. So maybe it would be good to edit the original issue and change it to the second example. |
That's the whole point. The |
I tried this code (playground link):
I expected to see this happen: The above code compiles
Instead, this happened:
Note that it works fine via the dot-operator and autoderef.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: