[PATCH v4 2/2] scripts: generate_rust_analyzer.py: fix IDE support for primitive types

Jesung Yang via B4 Relay posted 2 patches 2 weeks, 6 days ago
[PATCH v4 2/2] scripts: generate_rust_analyzer.py: fix IDE support for primitive types
Posted by Jesung Yang via B4 Relay 2 weeks, 6 days ago
From: Jesung Yang <y.j3ms.n@gmail.com>

Update `generate_rust_analyzer.py` so that the generated
`rust-project.json` contains the `sysroot_src` field with
`"crate_attrs": ["no_std"]` specified for relevant crates. This ensures
that rust-analyzer provides proper IDE support for inherent methods of
primitive types.

Since commit 50384460c68f ("Rewrite method resolution to follow rustc
more closely") to rust-analyzer, it no longer provides language server
features like code completion and go-to-definition for inherent methods
of primitive types when sysroot crates (e.g., `core`, `std`) are inlined
in `rust-project.json` [1]. As `generate_rust_analyzer.py` currently
inlines these crates, our setup is affected by this change.

Specifying the `sysroot_src` field restores this functionality by
allowing rust-analyzer to locate sysroot crates by itself. However, this
causes `std` to be treated as a dependency for all local crates by
default. To align with our compilation settings, provide the `no_std`
attribute via the `crate_attrs` field, as the `-Zcrate-attr=no_std`
compiler flag is not visible to rust-analyzer. This combined approach
removes manual manipulation of sysroot dependencies while preventing
incorrect symbol resolution against the standard library.

Note that this configuration requires rust-analyzer release 2025-12-22
(v0.3.2727) or later, which introduced support for the `crate_attrs`
field.

Link: https://rust-lang.zulipchat.com/#narrow/channel/x/topic/x/near/561607963 [1]
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/x/topic/x/near/561607753
Signed-off-by: Jesung Yang <y.j3ms.n@gmail.com>
---
 scripts/generate_rust_analyzer.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 21832763c5be..bf5a0f16995a 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -54,9 +54,11 @@ class CrateWithGenerated(Crate):
     source: Source
 
 
-class RustProject(TypedDict):
+class RustProject(TypedDict, total=False):
     crates: List[Crate]
     sysroot: str
+    # TODO: use `typing.NotRequired` when Python 3.11 is adopted.
+    sysroot_src: str
 
 
 @dataclass(frozen=True)
@@ -372,6 +374,7 @@ def generate_crates(
                 path,
                 sysroot_deps(core) + [kernel, pin_init],
                 cfg=generated_cfg,
+                crate_attrs=["no_std"],
             )
 
     return crates
@@ -432,6 +435,18 @@ def generate_rust_project(
             ),
             "sysroot": str(sysroot),
         }
+    elif version_info == RaVersionInfo.SUPPORTS_CRATE_ATTRS:
+        ctx = RaVersionCtx(
+            use_crate_attrs=True,
+            manual_sysroot_crates=False,
+        )
+        return {
+            "crates": generate_crates(
+                ctx, srctree, objtree, sysroot_src, external_src, cfgs, core_edition
+            ),
+            "sysroot": str(sysroot),
+            "sysroot_src": str(sysroot_src),
+        }
     else:
         assert_never(version_info)
 

-- 
2.52.0
Re: [PATCH v4 2/2] scripts: generate_rust_analyzer.py: fix IDE support for primitive types
Posted by Tamir Duberstein 2 weeks, 4 days ago
On Tue, 17 Mar 2026 18:29:55 +0900, Jesung Yang <y.j3ms.n@gmail.com> wrote:
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 21832763c5be..bf5a0f16995a 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -372,6 +374,7 @@ def generate_crates(
>                  path,
>                  sysroot_deps(core) + [kernel, pin_init],
>                  cfg=generated_cfg,
> +                crate_attrs=["no_std"],
>              )

This seems to be independent of the RA version. Is this broken today? Should it
be a separate patch? Maybe that patch need not be in this series.

> @@ -432,6 +435,18 @@ def generate_rust_project(
>              ),
>              "sysroot": str(sysroot),
>          }
> +    elif version_info == RaVersionInfo.SUPPORTS_CRATE_ATTRS:
> +        ctx = RaVersionCtx(

I think you forgot to define RaVersionInfo.SUPPORTS_CRATE_ATTRS?

> [ ... skip 7 lines ... ]
> +            "sysroot": str(sysroot),
> +            "sysroot_src": str(sysroot_src),
> +        }
>      else:
>          assert_never(version_info)
>  

This bifurcates the handling of RA version differences: half of it is encoded
in RaVersionCtx, and the other half is here in the presence or absence of
"sysroot_src". Can we keep it somehow more contained?


-- 
Tamir Duberstein <tamird@kernel.org>