Tehforsch / diman

Define rust compile time unit systems using const generics
53 stars 2 forks source link

Better error messages for rational dimensions #51

Open Tehforsch opened 9 months ago

Tehforsch commented 9 months ago

Rational dimensions are supported since #48 but the compiler error messages on dimension mismatches are hard to read when the rational-dimensions feature is enabled:

error[E0308]: mismatched types
  --> src/my_mod.rs:22:17
   |
22 |     let c = b - a;
   |                 ^ expected `my_mod::Dimension { length: Ratio { num: 2, denom: 1 } }`, found `my_mod::Dimension { length: Ratio { num: 1, denom: 1 } }`
   |
   = note: expected constant `my_mod::Dimension { length: Ratio { num: 2, denom: 1 } }`
              found constant `my_mod::Dimension { length: Ratio { num: 1, denom: 1 } }`

In an ideal world, it would be nice to be able to provide a custom Display impl for the Ratio that the compiler uses, but in practice I don't think this is possible at the moment (or I don't know how), since the compiler seems to use its own internal debug-like representation of const types.

One possibility could be to rewrite the Ratio struct such that the error message is "optimized", but I haven't found a satisfying solution yet. For example, implementing it as a tuple struct Ratio(i32, i32) would yield error messages such as

   = note: expected constant `my_mod::Dimension { length: Ratio(2, 1) }`
              found constant `my_mod::Dimension { length: Ratio(1, 1) }`

which is partly easier to read, but also confusing because it is not completely obvious that the second number is the denominator. I chose not to implement this for now.