Here is a simple testcase, which still works on the playpen:
use std::num;
use std::str::FromStr;
use std::convert::From;
#[derive(Debug)]
struct Error(String);
impl From<num::ParseFloatError> for Error {
fn from(err: num::ParseFloatError) -> Error {
Error(format!("{}", err))
}
}
fn parse(s: &String) -> Result<f64, Error> {
Ok(try!(<f64 as FromStr>::from_str(&s[..])))
}
fn main() {
println!("{:?}", parse(&"10.01".to_string()));
}
However, after I built the latest rustc from git (now it's rustc 1.1.0-dev (1114fcd94 2015-04-23)
), it stopped compiling with following error:
<std macros>:6:1: 6:32 error: the trait `core::convert::From<core::num::ParseFloatError>` is not implemented for the type `Error` [E0277]
<std macros>:6 $ crate:: convert:: From:: from ( err ) ) } } )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<std macros>:1:1: 6:48 note: in expansion of try!
exp.rs:15:8: 15:48 note: expansion site
error: aborting due to previous error
I'm unable to find out what's wrong. Why is the compiler unable to find my trait implementation?
This looks like it is a bug: std::num::ParseFloatError
and <f64 as FromStr>::Err
are different types:
impl
of FromStr
for f64
is in core
, and hence uses a ParseFloatError
type defined in that crate, so any uses of FromStr
/.parse()
will get this type.std::num
defines a new ParseFloatError
type, so an import from std::num
gets this one.The impl From<num::ParseFloatError> for Error
is using the latter, while <f64 as FromStr>::from_str(...)
is returning the former.
I opened #24748 about it. I also opened #24747 about improving the diagnostics to make this easier to debug in future.
One can work around this by insteading implementing the trait for core::num::ParseFloatError
. You'll need to load the core
crate with extern crate core;
and will need some feature gates.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments