Skip to content

Provide better span for diagnostics - #174

Merged
nbdd0121 merged 4 commits into
mainfrom
dev/hygiene
Sep 18, 2026
Merged

nbdd0121 merged 4 commits into
mainfrom
dev/hygiene

Conversation

@nbdd0121

Copy link
Copy Markdown
Member

When initializing a field, pin-init first projects the full slot to a slot
of the field, then initialize it by invoking a method. Currently the span
location is not explicitly set, so the error message points to the full
macro invocation.

Improve it by provide a span, so Rust will point to the specific fields
causing the error when type check fails.

Old error message:

error[E0308]: mismatched types
 --> tests/ui/compile-fail/init/field_value_wrong_type.rs:8:28
  |
8 |     let _ = init!(Foo { a: () });
  |             ---------------^^---
  |             |              |
  |             |              expected `usize`, found `()`
  |             arguments to this method are incorrect

New error message:

error[E0308]: mismatched types
 --> tests/ui/compile-fail/init/field_value_wrong_type.rs:8:28
  |
8 |     let _ = init!(Foo { a: () });
  |                         ---^^
  |                         |  |
  |                         |  expected `usize`, found `()`
  |                         arguments to this method are incorrect

@nbdd0121
nbdd0121 force-pushed the dev/hygiene branch 2 times, most recently from 34111ca to 14ceb96 Compare September 17, 2026 09:26
Syn/quote's default is the call site hygiene. If user provides a field
named `slot`, it will conflict with pin-init generated `slot` identifier.
Change it to use mixed site hygiene instead.

Fixes: e4d9a56 ("rewrite the initializer macros using `syn`")
Signed-off-by: Gary Guo <gary@garyguo.net>
Use `quote_spanned!(Span::mixed_site() => ...)` directly instead of
creating `slot` identifier and interpolate it later. Do the same for
`__data`, too.

This is needed so that we can customize the location of the span without
having the `slot` identifier getting in the way. For example:

    quote_spanned!(Span::mixed_site().located_at(loc) => ...)

with `slot` mentioned directly will have the diagnostic pointing to the
desired location, while `#slot` won't.

Signed-off-by: Gary Guo <gary@garyguo.net>
When initializing a field, pin-init first projects the full slot to a slot
of the field, then initialize it by invoking a method. Currently the span
location is not explicitly set, so the error message points to the full
macro invocation.

Improve it by provide a span, so Rust will point to the specific fields
causing the error when type check fails.

Old error message:

    error[E0308]: mismatched types
     --> tests/ui/compile-fail/init/field_value_wrong_type.rs:8:28
      |
    8 |     let _ = init!(Foo { a: () });
      |             ---------------^^---
      |             |              |
      |             |              expected `usize`, found `()`
      |             arguments to this method are incorrect

New error message:

    error[E0308]: mismatched types
     --> tests/ui/compile-fail/init/field_value_wrong_type.rs:8:28
      |
    8 |     let _ = init!(Foo { a: () });
      |                         ---^^
      |                         |  |
      |                         |  expected `usize`, found `()`
      |                         arguments to this method are incorrect

Signed-off-by: Gary Guo <gary@garyguo.net>
Use the span of `Index` for generated identifiers, so diagnostics can point
to the span of the index.

Signed-off-by: Gary Guo <gary@garyguo.net>
@nbdd0121
nbdd0121 merged commit d4d9b05 into main Sep 18, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant