Skip to content

rust-analyzer shows false positive E0308 type mismatch errors with PyO3 macros #20068

Open
@bigg-S

Description

@bigg-S

Description

rust-analyzer shows false positive E0308 type mismatch errors when using PyO3's #[pyclass] macro in combination with third-party procedural macros (specifically pyo3_stub_gen_derive::gen_stub_pyclass). The errors appear as:

expected &'static [fn() -> TypeInfo], found &[fn() -> TypeInfo; 0]
expected &'static [MemberInfo], found &[MemberInfo; 0]
expected &'static [PyType_Slot], found &[PyType_Slot; 0]
expected &'static [MaybeRuntimePyMethodDef], found &[MaybeRuntimePyMethodDef; 0]

Despite these rust-analyzer errors, the code compiles and runs perfectly.

Steps to Reproduce

  1. Create a PyO3 project with the following dependencies:
[dependencies]
pyo3 = { version = "*" }
pyo3-polars = { version = "0.22.0", features = ["derive", "dtype-full", "lazy"] }
pyo3-stub-gen = { version = "0.10.0", default-features = false }
  1. Create a struct with both PyO3 and stub generation macros:
use pyo3::prelude::*;
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};

#[gen_stub_pyclass]
#[pyclass]
#[derive(Clone)]
pub struct YourStruct;

#[gen_stub_pymethods]
#[pymethods]
impl YourStruct {
    #[staticmethod]
    fn example_method() -> PyResult<f64> {
        Ok(42.0)
    }
}
  1. Open the project in VS Code with rust-analyzer extension
  2. Observe the E0308 errors in rust-analyzer, while cargo check passes without issues

Expected Behavior

rust-analyzer should not show type mismatch errors for properly written PyO3 code that compiles successfully.

Actual Behavior

rust-analyzer shows multiple E0308 type mismatch errors related to array-to-slice coercion in macro-generated code.

Environment

  • Rust version: 1.89.0 (nightly)
  • rust-analyzer version: v0.3.2509
  • Operating System: macOS
  • Python version: 3.13
  • Cargo.toml setup:
  pyo3 = { version = "*" }
  pyo3-polars = { version = "0.22.0", features = ["derive", "dtype-full", "lazy"] }
  pyo3-stub-gen = { version = "0.10.0", default-features = false }

Additional Context

  • The issue appears to be related to how rust-analyzer handles procedural macro expansion when multiple proc macros are combined
  • The code works perfectly at runtime and passes all cargo commands
  • This affects developer experience as it creates noise in the IDE with false error indicators

Workaround

Currently, disabling E0308 diagnostics in rust-analyzer settings as a workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-bugCategory: bug

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions