Open
Description
I came across this scenario where having two functions with the same where
-bounds causes type inference failure:
pub fn foo<S>(_: S) where String: From<S> {}
pub fn bar<S>(s: S) where String: From<S> {
// Mismatched types error!
foo(String::from(s));
}
I would expect the type inference to hold up and allow for calling foo
with a String
constructed in bar
. Instead it fails with a mismatched types error (E0308) saying expected type parameter `S`, found `String`
Notably, ANY String
or &str
constructed in bar
and passed to foo
will fail type inference. For example:
pub fn baz<S>(_: S) where String: From<S> {
// Mismatched types error!
foo("");
}
pub fn bam<S>(_: S) where String: From<S> {
// Mismatched types error!
let s: String = "".to_owned();
foo(s);
}
Some examples of similar scenarios that work, but show that there shouldn't be anything wrong with the actual operation (particularly bap
):
pub fn bam<S>(s: S) where String: From<S> {
// OK!
foo(s);
}
pub fn bap<S>(s: S) where String: From<S> {
// OK!
foo::<String>(String::from(s));
}
pub fn bao() {
// OK!
let s: String = "".to_owned();
foo(s);
}
// Strangely, inverting the From bound to an Into bound fixes things
pub fn foo2<S>(_: S) where S: Into<String> {}
pub fn bar2<S>(s: S) where String: From<S> {
// OK!
foo2(String::from(s));
}
Meta
This occurs on stable (1.82.0), beta (1.83.0-beta.4 // 2024-11-02 67512de), and nightly (1.83.0-nightly // 2024-11-03 b8c8287) in debug and release modes.