Skip to content
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

impl From<Infallible> for MyType does not solve coercion problems #69448

Closed
AaronKutch opened this issue Feb 24, 2020 · 2 comments
Closed

impl From<Infallible> for MyType does not solve coercion problems #69448

AaronKutch opened this issue Feb 24, 2020 · 2 comments

Comments

@AaronKutch
Copy link
Contributor

playground link with all the impls and functions in example usage
The gist is that I have an impl impl TryFrom<usize> for InvariantType and function signatures with TryInto<InvariantType, Error = MyErrorType>. This enables many ergonomics, but trying to input a plain InvariantType into the function conflicts with the blanket impl TryFrom<T> for T which has a return type ! instead of Error = MyErrorType.

Since ! can and will coerce into anything through a future impl<T> From<!> for T , and there is a current workaround:

impl From<Infallible> for MyError {
    fn from(_: Infallible) -> Self {
        unreachable!()
    }
}

it should work, but adding this to the playground link does not change the error message at all.

Also in the playground link, there is a nearly identical error related to a From<NonZeroUsize> for BitWidth and its blanket impls. In that case, I can't even apply the attempted workaround because of orphan rules.

@jonas-schievink
Copy link
Contributor

Adding the From impl does not create a TryFrom impl for an unrelated type. Change typical_function to take a generic error E instead and add where MyError: From<E> to its bounds.

@AaronKutch
Copy link
Contributor Author

Thank you, the fix is

pub fn typical_function<W, E>(target_width: W) -> Result<(), MyError>
where
    W: TryInto<BitWidth, Error = E>,
    MyError: From<E>,
{

and add the From<Infallible> impl. playground link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants