1

I am trying to compile code such as the following:

lib.rs

use nalgebra::ComplexField;
use num::FromPrimitive;
use bacon_sci::polynomial;
use bacon_sci::polynomial::Polynomial;

fn foo <R> () -> Polynomial <R>
where
    R: FromPrimitive + ComplexField + Copy,
    <R as ComplexField>::RealField: Copy + FromPrimitive
{
    polynomial! (1, 2, 3)
}

It fails with:

   Compiling learning_engine v0.1.0 (/home/zistack/Projects/learning_engine)
error[E0277]: the trait bound `R: simba::scalar::complex::ComplexField` is not satisfied
  --> src/lib.rs:6:18
   |
6  | fn foo <R> () -> Polynomial <R>
   |                  ^^^^^^^^^^^^^^ the trait `simba::scalar::complex::ComplexField` is not implemented for `R`
   |
note: required by a bound in `Polynomial`
  --> /home/zistack/.cargo/registry/src/github.com-1ecc6299db9ec823/bacon-sci-0.13.1/src/polynomial/mod.rs:10:26
   |
10 | pub struct Polynomial<N: ComplexField + FromPrimitive + Copy>
   |                          ^^^^^^^^^^^^ required by this bound in `Polynomial`
help: consider further restricting this bound
   |
8  |     R: FromPrimitive + ComplexField + Copy + simba::scalar::complex::ComplexField,
   |                                            ++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `learning_engine` due to previous error

Why?

Here's what really confuses me about this. bacon-sci imports their ComplexField trait from nalgebra exactly the same way I do in the file that defines the Polynomial type, and nalgebra does indeed get their definition from simba. The compiler correctly identifies the trait that needs to be satisfied, but somehow doesn't believe that I'm forcing it to be satisfied, even though I'm pulling the trait from exactly the same place.

Adding the code that the compiler suggests doesn't work, because simba::scalar::complex is a private module. Adding simba::scalar::ComplexField (which is what you would normally do) also doesn't work, failing in exactly the same way as before.

I am using rust nightly.

The relevant section of my Cargo.toml:

[dependencies]
bacon-sci = "0.13"
nalgebra = "0.31"
num = "0.4"
simba = "0.7"
Zistack
  • 516
  • 3
  • 10
  • 1
    Actually, the polynomial type from `bacon-sci` seems stronger to me, when reading their source code. The also require `::RealField: FromPrimitive + Copy`. If you add these bounds, does it work? – jthulhu Jun 18 '22 at 18:34
  • Looks like there's two different types called `ComplexField` from different crates. Bounding on one doesn't implicitly bound on the other. – cdhowie Jun 18 '22 at 18:51
  • @cdhowie The module that the Polynomial implementation is in is defined [here](https://docs.rs/bacon-sci/latest/src/bacon_sci/polynomial/mod.rs.html#10-17). Note that they're importing the exact same `ComplexField` as I am at the top of the file. – Zistack Jun 18 '22 at 20:03
  • @BlackBeans Good question. It does not. I have updated my post to include that bound, since it probably needs to be there anyways. – Zistack Jun 18 '22 at 20:09
  • 1
    You are using `nalgebra` v0.31 but [`bacon-sci` v0.13](https://crates.io/crates/bacon-sci/0.13.1/dependencies) uses `nalgebra` v0.29, so what you're importing for `ComplexField` is not the same trait that `Polynomial` expects. You should change your dependency in `Cargo.toml` to 0.29. See: [Why is a trait not implemented for a type that clearly has it implemented?](https://stackoverflow.com/q/44437123/2189130) – kmdreko Jun 18 '22 at 20:51
  • @kmdreko Yup. That's it. – Zistack Jun 18 '22 at 21:04

0 Answers0