[PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea

SeungJong Ha via B4 Relay posted 1 patch 2 weeks ago
scripts/generate_rust_analyzer.py | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
[PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea
Posted by SeungJong Ha via B4 Relay 2 weeks ago
From: SeungJong Ha <engineer.jjhama@gmail.com>

Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
scripts: generate_rust_analyzer.py: Fix pin-init configuration for
better macro resolve

Hello,

I encountered an issue where rust-analyzer fails to analyze
structs decorated with the #[pin_data] attribute while testing
Rust kernel modules.

It appears that the dependencies for pin-init and drivers were
missing from the analyzer's configuration. This patch adds
those missing dependencies.

I have verified that my rust-analyzer correctly processes
\#[pin_data] structs after applying this fix.

Best regards, SeungJong
---
 scripts/generate_rust_analyzer.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 147d0cc94068..0c6c0444917f 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -123,10 +123,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     append_crate(
         "pin_init_internal",
         srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
-        [],
+        ["std", "proc_macro"],
         cfg=["kernel"],
         is_proc_macro=True,
     )
+    # pin_init_internal uses #[path = "../../../macros/quote.rs"] to include quote.rs
+    crates[-1]["source"] = {
+        "include_dirs": [
+            str(srctree / "rust" / "pin-init" / "internal" / "src"),
+            str(srctree / "rust" / "macros"),
+        ],
+        "exclude_dirs": [],
+    }
 
     append_crate(
         "pin_init",
@@ -190,7 +198,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
             append_crate(
                 name,
                 path,
-                ["core", "kernel"],
+                ["core", "kernel", "pin_init"],
                 cfg=cfg,
             )
 

---
base-commit: 2ad6c5cdc89acfefb01b84afa5e55262c40d6fec
change-id: 20260123-fix-pin-init-crate-dependecies-b94ac4247a48

Best regards,
-- 
SeungJong Ha <engineer.jjhama@gmail.com>
Re: [PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea
Posted by Jesung Yang 2 weeks ago
Hi SeungJong,

I think I could give you some background on where things stand :)

On Fri Jan 23, 2026 at 7:02 PM KST, SeungJong Ha wrote:
[...]
>  scripts/generate_rust_analyzer.py | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 147d0cc94068..0c6c0444917f 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -123,10 +123,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
>      append_crate(
>          "pin_init_internal",
>          srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
> -        [],
> +        ["std", "proc_macro"],

As Miguel pointed out, this fix has already landed on the `rust-fixes`
branch as commit "scripts: generate_rust_analyzer: Add pin_init_internal
deps" [1].

>          cfg=["kernel"],
>          is_proc_macro=True,
>      )
> +    # pin_init_internal uses #[path = "../../../macros/quote.rs"] to include quote.rs
> +    crates[-1]["source"] = {
> +        "include_dirs": [
> +            str(srctree / "rust" / "pin-init" / "internal" / "src"),
> +            str(srctree / "rust" / "macros"),
> +        ],
> +        "exclude_dirs": [],
> +    }

AFAIK, the `#[path = "../../../macros/quote.rs"]` part was removed
recently by this series [2], which is set to land in this cycle.

So I would recommend checking if the problem is still present even after
rebasing on `rust-fixes` with the new pin-init update applied (or simply
checking the `linux-next` tree as I think it has already picked up the
relevant changes).

Thanks!

[1] https://github.com/Rust-for-Linux/linux/commit/74e15ac34b098934895fd27655d098971d2b43d9
[2] https://lore.kernel.org/rust-for-linux/20260116105514.3794384-4-lossin@kernel.org/

Best regards,
Jesung
Re: [PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea
Posted by 하승종 2 weeks ago
2026년 1월 23일 (금) PM 9:23, Jesung Yang <y.j3ms.n@gmail.com>님이 작성:
>
> Hi SeungJong,
>
> I think I could give you some background on where things stand :)
>
> On Fri Jan 23, 2026 at 7:02 PM KST, SeungJong Ha wrote:
> [...]
> >  scripts/generate_rust_analyzer.py | 12 ++++++++++--
> >  1 file changed, 10 insertions(+), 2 deletions(-)
> >
> > diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> > index 147d0cc94068..0c6c0444917f 100755
> > --- a/scripts/generate_rust_analyzer.py
> > +++ b/scripts/generate_rust_analyzer.py
> > @@ -123,10 +123,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
> >      append_crate(
> >          "pin_init_internal",
> >          srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
> > -        [],
> > +        ["std", "proc_macro"],
>
> As Miguel pointed out, this fix has already landed on the `rust-fixes`
> branch as commit "scripts: generate_rust_analyzer: Add pin_init_internal
> deps" [1].

Thank you, Jesung!
I successfully rebased the next version onto `rust-fixes`.
And I also found the other line to use "go to definition" feature on
#[pin_data] struct. I will introduce it in the next version.

>
> >          cfg=["kernel"],
> >          is_proc_macro=True,
> >      )
> > +    # pin_init_internal uses #[path = "../../../macros/quote.rs"] to include quote.rs
> > +    crates[-1]["source"] = {
> > +        "include_dirs": [
> > +            str(srctree / "rust" / "pin-init" / "internal" / "src"),
> > +            str(srctree / "rust" / "macros"),
> > +        ],
> > +        "exclude_dirs": [],
> > +    }
>
> AFAIK, the `#[path = "../../../macros/quote.rs"]` part was removed
> recently by this series [2], which is set to land in this cycle.

Oh, It would be better to check this point after the series lands.

>
> So I would recommend checking if the problem is still present even after
> rebasing on `rust-fixes` with the new pin-init update applied (or simply
> checking the `linux-next` tree as I think it has already picked up the
> relevant changes).

After rebasing, it also seems that just editing a single line looks enough.
I will bring it soon.

>
> Thanks!
>
> [1] https://github.com/Rust-for-Linux/linux/commit/74e15ac34b098934895fd27655d098971d2b43d9
> [2] https://lore.kernel.org/rust-for-linux/20260116105514.3794384-4-lossin@kernel.org/
>
> Best regards,
> Jesung
Re: [PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea
Posted by Miguel Ojeda 2 weeks ago
On Fri, Jan 23, 2026 at 11:02 AM SeungJong Ha via B4 Relay
<devnull+engineer.jjhama.gmail.com@kernel.org> wrote:
>
> From: SeungJong Ha <engineer.jjhama@gmail.com>
>
> Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>

The commit message appears to be empty -- I think you may have wanted
to use the text below the `---`, since I see another title there
(although it isn't exactly worded like the usual commit message
either, i.e. with "Hello" etc., so I am not sure what happened in this
one).

Also, typo in the (patch) title.

> @@ -123,10 +123,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
>      append_crate(
>          "pin_init_internal",
>          srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
> -        [],
> +        ["std", "proc_macro"],
>          cfg=["kernel"],
>          is_proc_macro=True,
>      )

Is this commit 74e15ac34b09 ("scripts: generate_rust_analyzer: Add
pin_init_internal deps")?

I think you based this on top of `rust-next` so you did not see the
fixes currently queued in `rust-fixes` (soon to arrive in mainline).

> +    # pin_init_internal uses #[path = "../../../macros/quote.rs"] to include quote.rs

For comments etc., we use the usual style used in other Rust files, e.g.

    # `pin_init_internal` uses `#[path = "../../../macros/quote.rs"]`
to include `quote.rs`.

> +            str(srctree / "rust" / "pin-init" / "internal" / "src"),
> +            str(srctree / "rust" / "macros"),

There is this rule in rust-analyzer about `source`:

    /// If two crates share an `.rs` file in common,
    /// they *must* have the same `source`.
    /// rust-analyzer assumes that files from one
    /// source can't refer to files in another source.

I am not sure how that is supposed to work or why rust-analyzer
requires it, i.e. does it mean `macros` must have also the `pin-init`
path too? Or just that, for each file, the path that included it (not
the entire `source`) must match?

If the former, we may be also "abusing" it in the other place we use
`source` for the generated files. If the latter, then it is fine
there, but here we would need `macros` to also have the path, I guess.

Or perhaps that "rule" in the docs isn't as strict as it sounds?

Thanks!

Cheers,
Miguel
Re: [PATCH] scripts: generate_rust_analyzer.py: better pin-init macro resolvea
Posted by 하승종 2 weeks ago
2026년 1월 23일 (금) PM 7:26, Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>님이 작성:
>
> On Fri, Jan 23, 2026 at 11:02 AM SeungJong Ha via B4 Relay
> <devnull+engineer.jjhama.gmail.com@kernel.org> wrote:
> >
> > From: SeungJong Ha <engineer.jjhama@gmail.com>
> >
> > Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
>
> The commit message appears to be empty -- I think you may have wanted
> to use the text below the `---`, since I see another title there
> (although it isn't exactly worded like the usual commit message
> either, i.e. with "Hello" etc., so I am not sure what happened in this
> one).
>
> Also, typo in the (patch) title.

I got it, maybe I got something wrong on my b4,
Let me check and get back to you!

>
> > @@ -123,10 +123,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
> >      append_crate(
> >          "pin_init_internal",
> >          srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
> > -        [],
> > +        ["std", "proc_macro"],
> >          cfg=["kernel"],
> >          is_proc_macro=True,
> >      )
>
> Is this commit 74e15ac34b09 ("scripts: generate_rust_analyzer: Add
> pin_init_internal deps")?
>
> I think you based this on top of `rust-next` so you did not see the
> fixes currently queued in `rust-fixes` (soon to arrive in mainline).
>

Oh, I was wandering the project on `rust-next`.
I will make v2 rebased on `rust-fixes`

> > +    # pin_init_internal uses #[path = "../../../macros/quote.rs"] to include quote.rs
>
> For comments etc., we use the usual style used in other Rust files, e.g.
>

Got it!

>     # `pin_init_internal` uses `#[path = "../../../macros/quote.rs"]`
> to include `quote.rs`.
>
> > +            str(srctree / "rust" / "pin-init" / "internal" / "src"),
> > +            str(srctree / "rust" / "macros"),
>
> There is this rule in rust-analyzer about `source`:
>
>     /// If two crates share an `.rs` file in common,
>     /// they *must* have the same `source`.
>     /// rust-analyzer assumes that files from one
>     /// source can't refer to files in another source.
>
> I am not sure how that is supposed to work or why rust-analyzer
> requires it, i.e. does it mean `macros` must have also the `pin-init`
> path too? Or just that, for each file, the path that included it (not
> the entire `source`) must match?

I had some brief experiments(after rebase to rust-fixes).
It seems to be related with lsp's "go to reference" feature.
Without this patch,
1. The analyzer fails to resolve the mod `quote` on
`rust/pin-init/internal/src/lib.rs`.
2. The analyzer also fails to find references outside of the
`rust/macros` directory.

And I couldn't find `pin-init` usage on the `macro` crate.

So I guess the latter one has more sounds.

>
> If the former, we may be also "abusing" it in the other place we use
> `source` for the generated files. If the latter, then it is fine
> there, but here we would need `macros` to also have the path, I guess.
>
> Or perhaps that "rule" in the docs isn't as strict as it sounds?

    /// By default, rust-analyzer assumes that only
    /// files under `root_module.parent` can belong
    /// to a crate. `include_dirs` are included
    /// recursively, unless a subdirectory is in
    /// `exclude_dirs`.
    ///
    /// Different crates can share the same `source`.
    ///
    /// If two crates share an `.rs` file in common,
    /// they *must* have the same `source`.
    /// rust-analyzer assumes that files from one
    /// source can't refer to files in another source.

I think the analyzer already includes the path while the parent of `macro` lib
already contains `quote`!

>
> Thanks!
>
> Cheers,
> Miguel