Num_derive now failing at rust-analyzer

Notice: it is an error at rust-analyzer only.

num_derive::FromPrimitive was working with my previous as3_parser project, but I'm now getting this with my new project:

no such associated item E0599

  • at #[derive(FromPrimitive)]
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

#[derive(FromPrimitive)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u32)]
pub enum OperatorPrecedence {
    Postfix = 17,
    Unary = 16,
    /// Internal type annotation operator
    TypeAnnotation = 15,
    Exponentiation = 14,
    Multiplicative = 13,
    Additive = 12,
    Shift = 11,
    Relational = 10,
    Equality = 9,
    BitwiseAnd = 8,
    BitwiseXor = 7,
    BitwiseOr = 6,
    LogicalAnd = 5,
    LogicalXor = 4,
    /// Includes logical OR and nullish coalescing (`??`).
    LogicalOrAndOther = 3,
    /// Includes assignment, conditional, `yield`, and rest (`...`) operators
    /// and arrow functions.
    AssignmentAndOther = 2,
    List = 1,
}

impl OperatorPrecedence {
    pub fn add(&self, value: u32) -> Option<Self> {
        FromPrimitive::from_u32(*self as u32 + value)
    }

    pub fn value_of(&self) -> u32 {
        *self as u32
    }

    pub fn includes(&self, other: &Self) -> bool {
        *self <= *other
    }
}

impl TryFrom<u32> for OperatorPrecedence {
    type Error = ();
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        if let Some(v) = FromPrimitive::from_u32(value as u32) { Ok(v) } else { Err(()) }
    }
}

That seems to be an error only in rust-analyzer. I’d assume some kind of issue/bug with rust-analyzer. Your program still compiles fine.

In the mean-time you can disable this error from rust-analyzer in the settings, e.g. with vscode something like

"rust-analyzer.diagnostics.disabled": ["E0599"],
1 Like

Looking into the macro expansion, this seems to be related to trait implementations inside named constants. The problem minimizes to something like:

trait Trait {
    fn foo();
    fn bar();
}

struct One;

const _NAMED: () = {
    impl Trait for One {
        fn foo() {
            todo!()
        }

        fn bar() {
            Self::foo() // no such associated item (E0599)
        }
    }
};

struct Two;

const _: () = {
    impl Trait for Two {
        fn foo() {
            todo!()
        }

        fn bar() {
            Self::foo()
        }
    }
};
1 Like

Seems not discussed on the rust-analyzer repo so far. Let’s see if we get any feedback there eventually :slight_smile:

I just saw someone elsewhere talking about issues with impls in consts and rustdoc, I wonder if the origins are related in any way.