You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a problem when implementing a bitfield macro in Rust.
pubtraitBits{constBITS:usize;// How many bits this field hastypeUnderlying;// Minimum underlying uint (u8, u16, ...) to store this fieldfnfrom_arbint(from: arbitrary_int::UInt<Self::Underlying,{Self::BITS}>) -> Self;}
This code uses an arbitrary precision integer library: arbitrary-int. Trait Bits is used to describe:
How many bits this field has.
Minimum underlying uint type to store this field, like u8, u16, .. etc.
How to convert an arbitrary_int::UInt<T, BITS> to this field.
For example, for a bool type:
// Used to calculate the minimun uint type to store bits pubtraitCalcUint{typeUint;}implCalcUintfor[();1]{typeUint = u8;}
...implCalcUintfor[();9]{typeUint = u16;}
...
generic constants in where-bounds are unfortunately very hard to support without getting cycles. I can't given you any tips to fix this apart from trying to remove the generic constants in where-clauses which is probably not useful 😁
The cycle happens while typechecking the outer array length.
Outer array here means [u8; {...}]? Then the where-clause [u8; {...}]: Sized requires {let _: Foo<[u8; 3+4]>; 3} to be sized? And then?
This happens because Foo<[u8; 3 + 4]> requires [u8; 3 + 4] to be sized.
Is this because Foo's definition struct Foo<T>(T) implied T: sized?
While trying to fulfill this we see the [u8; ...]: Sized bound in the param_env and try to unify these two. This causes us to evaluate both constants which once again relies on typechecking them.
I'm completely lost here ..
In the second example:
#![feature(generic_const_exprs)]traitFoo{constN:usize;}structNum<constN:usize>where[(); <SelfasFoo>::N]:;impl<constN:usize>FooforNum<N>{constN:usize = N - 1;}
Is this how the cycle formed?
where-clause [(); <Self as Foo>::N] of Num's definition requires Self: Foo
Self is just Num<N> (with where-clause)
evaluation of where-clause in (2) brings back to (1)
This is a problem when implementing a bitfield macro in Rust.
This code uses an arbitrary precision integer library: arbitrary-int. Trait Bits is used to describe:
For example, for a bool type:
For array type it becomes a bit more complicated:
Then the compiler report error at { Self::BITS } in from_arbint:
Can someone help to explain why is this happening and how to fix it? Thanks!
PS: For complete code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a893e99e95aa2a964fa49546a9fbdaaa .
NOTE: The playground does not support random 3rd party library(?), so one need to copy code to local for test/debug.
The text was updated successfully, but these errors were encountered: