pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Reporting multiple errors from proc-macros

Open davidhewitt opened this issue 2 years ago • 0 comments

At the moment our proc-macros are implemented such that they return on the first error they encounter.

I think in some cases it should be possible to collect multiple errors together so that all of these could be reported to the user. For example, the following code:

use pyo3::prelude::*;

#[pyclass]
struct Example {
    #[pyo3(foo)]
    x: i32,
    #[pyo3(blah)]
    y: i32,
}

will at time of writing report compilation errors like the following:

error: expected one of: `get`, `set`, `name`
 --> src/lib.rs:5:12
  |
5 |     #[pyo3(foo)]
  |            ^^^


error: cannot find attribute `pyo3` in this scope
 --> src/lib.rs:7:7
  |
7 |     #[pyo3(blah)]
  |       ^^^^
  |
  = note: `pyo3` is in scope, but it is a crate, not an attribute

This is because as soon as the error on "foo" occurs, the macro stops parsing and so the #[pyo3(blah)] attribute is never consumed.

A better pair of error messages could be the following:

error: expected one of: `get`, `set`, `name`
 --> src/lib.rs:5:12
  |
5 |     #[pyo3(foo)]
  |            ^^^


error: expected one of: `get`, `set`, `name`
 --> src/lib.rs:7:7
  |
7 |     #[pyo3(blah)]
  |       ^^^^

I think I'm unlikely to work on this any time soon myself, but it would be a nice UX improvement if anyone is interested in getting dirty with our proc-macros 😄

davidhewitt avatar Jan 19 '23 08:01 davidhewitt