Skip to content

Poor diagnostics due to CompileTimeAssert and weird try_from / try_new #36

Description

@SV-97

I just ran into some problems when trying to do something like

let x = 123_u32;
let y = u10::try_from(x);

This induced an error

error[E0080]: evaluation of `arbitrary_int::CompileTimeAssert::<32, 10>::SMALLER_OR_EQUAL` failed

from deep inside arbitrary-int's source without any source spans for the user code, which made finding the problem rather hard. I don't see why there's a compile time assert like that on an explicitly fallible conversion. Judging from the error message it seems like the try_from is internally using from which seems rather odd to me.

I also tried using try_new rather than try_from, however this would requires chaining multiple conversions when a single one should do: try to get u16 from u32 and then try to get u10 from u16. Since the error types between u10::try_new and u16::try_from don't match up this requires some rather ugly code (even when discarding the errors):

u16::try_from(x).ok().and_then(|x| u10::try_new(x).ok())

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions