rust: pin-init: internal: init: set span location of guards

Set the span location of the guards to point to the identifier. The
generated diagnostic is still not ideal, as ideally it points the place
where the identifier is used rather than the identifier name in
initializer, but is still an improvement to status quo.

Before the change:

    error[E0505]: cannot move out of value because it is borrowed
      --> tests/ui/compile-fail/init/accessor_lifetime.rs:8:13
       |
     8 |       let _ = init!(Foo {
       |  _____________^
     9 | |         x: 0,
    10 | |         _: {
    11 | |             let _: &'static usize = x;
       | |                    -------------- type annotation requires that borrow lasts for `'static`
    12 | |         },
    13 | |     });
       | |      ^
       | |      |
       | |______move out of value occurs here
       |        borrow of value occurs here

After the change:

    error[E0505]: cannot move out of value because it is borrowed
      --> tests/ui/compile-fail/init/accessor_lifetime.rs:9:9
       |
     9 |         x: 0,
       |         ^
       |         |
       |         move out of value occurs here
       |         borrow of value occurs here
    10 |         _: {
    11 |             let _: &'static usize = x;
       |                    -------------- type annotation requires that borrow lasts for `'static`

Acked-by: Benno Lossin <lossin@kernel.org>
Link: https://patch.msgid.link/20261006-dev-selfref-dep-v1-2-da716f740d6d@garyguo.net
Signed-off-by: Gary Guo <gary@garyguo.net>
This commit is contained in:
Gary Guo 2026-10-06 17:29:32 +02:00
parent 70ff1ccded
commit da2b9009d4

View File

@ -413,7 +413,7 @@ fn init_fields(fields: &Punctuated<InitializerField, Token![,]>, pinned: bool) -
};
// `mixed_site` ensures that the guard is not accessible to the user-controlled code.
let guard = format_ident!("__{ident}_guard", span = Span::mixed_site());
let guard = format_ident!("__{ident}_guard", span = span);
let full_span = kind.span().resolved_at(Span::mixed_site());
let init = match kind {