-
Notifications
You must be signed in to change notification settings - Fork 47
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
Compilation error with CsMat and generic Float #278
Comments
In short: Longer: To support addition for arbitrary types, we must restrict fn test<F: Float>(val: F) -> CsMat<F>
where
F: num_traits::Zero + PartialEq + Clone + Default,
{ and your code should compile. Edit: It should be enough to specify the additional trait bound |
Thanks for the swift reply. Actually I went down that way, but in that case I got: error[E0308]: mismatched types
--> src\main.rs:14:18
|
14 | let b = &a + &a;
| ^^ expected struct `ndarray::ArrayBase`, found struct `CsMatBase`
|
= note: expected reference `&ndarray::ArrayBase<_, ndarray::dimension::dim::Dim<[usize; 2]>>`
found reference `&CsMatBase<F, usize, Vec<usize>, Vec<usize>, Vec<F>>`
error[E0308]: mismatched types
--> src\main.rs:15:5
|
4 | fn test<F: Float>(val: F) -> CsMat<F>
| -------- expected `CsMatBase<F, usize, Vec<usize>, Vec<usize>, Vec<F>>` because of return type
...
15 | b
| ^ expected struct `CsMatBase`, found struct `ndarray::ArrayBase`
|
= note: expected struct `CsMatBase<F, usize, Vec<usize>, Vec<usize>, Vec<F>>`
found struct `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<F>, ndarray::dimension::dim::Dim<[usize; 2]>>`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.
error: could not compile `test-sprs` It seems to pick the addition with a ndarray! Here the modified program: use num_traits::Float;
use sprs::CsMat;
fn test<F>(val: F) -> CsMat<F>
where
F: Float + num_traits::Zero + PartialEq + Clone + Default,
{
let a = CsMat::new_csc(
(3, 3),
vec![0, 2, 4, 5],
vec![0, 1, 0, 2, 2],
vec![val, val, val, val, val],
);
let b = &a + &a;
b
}
fn main() {
let a = test(1.);
} |
Oh, I see my |
My next attempt involves adding this bound: for<'r> &'r F: core::ops::Add<&'r F, Output = F>, but this leads to a recursion error in type resolution. |
Yep did that too! 👀 Then I opened the issue thinking I was missing something. |
Paging @vbarrielle, this was introduced in d2f0da7. Any ideas in why type-checking would go haywire here? |
I confirm it compiles with 0.9.3 adding only the |
I'll need to investigate this issue. I've definitely encountered the type resolution recursion issue (see rust-lang/rust#82779) while developping This error happens because I've tried to minimize the number of clones in the binary operations, and I picked my traits to be sure that it would work on classic scalar types. However I failed to consider the case of a dependent crate using a generic scalar type, which is quite a problem... |
any update here? This blocks bumping |
As a temporary measure we could backport the |
Yes, I think it would be great to have that backup solution in the meantime, not to fall too much behind as |
@vbarrielle I've added a new branch |
@mulimoen FWIW, your |
Thanks @vbarrielle, |
sure take your time, it is not super urgent but we don't want to lack too far behind ndarray
cool thanks! |
Thanks @vbarrielle for your hard work! |
I think the original compilation error is gone with the following extra bounds: fn test<F>(val: F) -> CsMat<F>
where
F: Float,
F: Default + Send + Sync + num_traits::MulAdd<Output = F>,
{
let a = CsMat::new_csc(
(3, 3),
vec![0, 2, 4, 5],
vec![0, 1, 0, 2, 2],
vec![val, val, val, val, val],
);
let b = &a * &a;
b
} |
Yes, it works, thanks! I am wondering how you found that list of bound spells though! 😄 I still have to try this with |
this seems to be broken again (perhaps a regression introduced in #290). The problem can be worked around by using |
@bytesnake This does not seem related to the original issue. I've opened #292 to address this issue |
I also see that my "fix" did not actually work as I replaced addition by multiplication |
Did anybody find a workaround? How does this recursion error occur? |
Would it help to create another blanket type around for the HRBT? That code does not raise the recursion error where it did before but I cannot really test it right now: pub trait Real<T>: Num + NdFloat + Default {}
pub trait RealRef<S, T>: Add<S, Output=T> + Mul<S, Output=T> {}
impl Real<f32> for f32 {}
impl Real<f64> for f64 {}
impl RealRef<&f32,f32> for &f32 {}
impl RealRef<&f64,f64> for &f64 {}
fn test1<T, D>( y: Array<T, D>)
where
T: Real<T>,
for<'r> &'r T: RealRef<&'r T, T>,
D: Dimension,
{
} Could sprs provide a suitable blanket trait comparable to Edit: It seems to work |
@StefanUlbrich How should this fix be integrated into |
It should suffice to include the two traits with implementations for the relevant types (does integer or complex make sense?). That way, edit: do you have a recommendation for a location, @mulimoen ? I’d be happy to make a PR |
I am new to sprs library, I do not understand why the following program does not compile:
with the following dependencies
and when I try to build I get:
I cannot see what is missing to enable the addition. Thanks for your help.
The text was updated successfully, but these errors were encountered: