Skip to contentSkip to conversation

scripts: generate_rust_analyzer.py: tidy and add type hints

PATCH Applied Tamir Duberstein 3 participants 13 messages Jan 22 – Mar 2

Subsystems (2)
lore
Share & tools
lore

Tamir Duberstein

author maintainer
#0 lore raw

cover scripts: generate_rust_analyzer.py: tidy and add type hints

This series adds type annotations to generate_rust_analyzer.py. It is a subset of an earlier series[0] with formatting-only and drive-by patches removed (I sent some of these as separate single-patch series).

The final commit leverages type hints to tidy the implementation. It could be done separately from this series, but serves as a nice demonstration.

---
Tamir Duberstein (4):
scripts: generate_rust_analyzer.py: extract {build,register}_crate
scripts: generate_rust_analyzer.py: drop "is_proc_macro": false
scripts: generate_rust_analyzer.py: add type hints
scripts: generate_rust_analyzer.py: identify crates explicitly

 scripts/generate_rust_analyzer.py | 256 ++++++++++++++++++++++++++++----------
 1 file changed, 191 insertions(+), 65 deletions(-)

---
base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7
change-id: 20260122-rust-analyzer-types-f90bda766749

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
author maintainer
#1 lore raw

PATCH 1/4 scripts: generate_rust_analyzer.py: extract `{build,register}_crate`

Extract helpers from append_crate to avoid the need to peek into crates[-1]. This improves readability.

Change default parameters to None with true defaults applied in build_crate to avoid repeating the defaults in wrapper functions such as append_crate.

Use ruff format to format modified lines.

---
 scripts/generate_rust_analyzer.py | 76 +++++++++++++++++++++++++++++++--------
 1 file changed, 62 insertions(+), 14 deletions(-)
scripts/generate_rust_analyzer.py +62 -14
index 147d0cc94068..fbf686f0dced 100755
@@ -35,7 +35,22 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     crates_indexes = {}
     crates_cfgs = args_crates_cfgs(cfgs)
 
-    def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False, edition="2021"):
+    def build_crate(
+        display_name,
+        root_module,
+        deps,
+        *,
+        cfg,
+        is_workspace_member,
+        is_proc_macro,
+        edition,
+    ):
+        cfg = cfg if cfg is not None else []
+        is_workspace_member = (
+            is_workspace_member if is_workspace_member is not None else True
+        )
+        is_proc_macro = is_proc_macro if is_proc_macro is not None else False
+        edition = edition if edition is not None else "2021"
         crate = {
             "display_name": display_name,
             "root_module": str(root_module),
@@ -54,20 +69,46 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
                 stdin=subprocess.DEVNULL,
             ).decode('utf-8').strip()
             crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
-        crates_indexes[display_name] = len(crates)
+        return crate
+
+    def register_crate(crate):
+        crates_indexes[crate["display_name"]] = len(crates)
         crates.append(crate)
 
+    def append_crate(
+        display_name,
+        root_module,
+        deps,
+        *,
+        cfg=None,
+        is_workspace_member=None,
+        is_proc_macro=None,
+        edition=None,
+    ):
+        return register_crate(
+            build_crate(
+                display_name,
+                root_module,
+                deps,
+                cfg=cfg,
+                is_workspace_member=is_workspace_member,
+                is_proc_macro=is_proc_macro,
+                edition=edition,
+            )
+        )
+
     def append_sysroot_crate(
         display_name,
         deps,
-        cfg=[],
-        edition="2021",
+        *,
+        cfg=None,
+        edition=None,
     ):
-        append_crate(
+        return append_crate(
             display_name,
             sysroot_src / display_name / "src" / "lib.rs",
             deps,
-            cfg,
+            cfg=cfg,
             is_workspace_member=False,
             edition=edition,
         )
@@ -145,20 +186,27 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         display_name,
         deps,
     ):
-        append_crate(
+        crate = build_crate(
             display_name,
             srctree / "rust"/ display_name / "lib.rs",
             deps,
             cfg=cfg,
+            is_workspace_member=True,
+            is_proc_macro=False,
+            edition=None,
         )
-        crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
-        crates[-1]["source"] = {
-            "include_dirs": [
-                str(srctree / "rust" / display_name),
-                str(objtree / "rust")
-            ],
-            "exclude_dirs": [],
+        crate["env"]["OBJTREE"] = str(objtree.resolve(True))
+        crate_with_generated = {
+            **crate,
+            "source": {
+                "include_dirs": [
+                    str(srctree / "rust" / display_name),
+                    str(objtree / "rust"),
+                ],
+                "exclude_dirs": [],
+            },
         }
+        return register_crate(crate_with_generated)
 
     append_crate_with_generated("bindings", ["core", "ffi", "pin_init"])
     append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH 2/4 scripts: generate_rust_analyzer.py: drop `"is_proc_macro": false`

In reply to Tamir Duberstein

Add a dedicated append_proc_macro_crate function to reduce overloading in append_crate. This has the effect of removing `"is_proc_macro":
false` from the output; this field is interpreted as false if absent[1] so this doesn't change the behavior of rust-analyzer.

Use the / operator on pathlib.Path rather than directly crafting a string. This is consistent with all other path manipulation in this script.

---
 scripts/generate_rust_analyzer.py | 60 ++++++++++++++++++++++++++++-----------
 1 file changed, 43 insertions(+), 17 deletions(-)
scripts/generate_rust_analyzer.py +43 -17
index fbf686f0dced..bc79e4d8e8e7 100755
@@ -42,20 +42,17 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         *,
         cfg,
         is_workspace_member,
-        is_proc_macro,
         edition,
     ):
         cfg = cfg if cfg is not None else []
         is_workspace_member = (
             is_workspace_member if is_workspace_member is not None else True
         )
-        is_proc_macro = is_proc_macro if is_proc_macro is not None else False
         edition = edition if edition is not None else "2021"
-        crate = {
+        return {
             "display_name": display_name,
             "root_module": str(root_module),
             "is_workspace_member": is_workspace_member,
-            "is_proc_macro": is_proc_macro,
             "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
             "cfg": cfg,
             "edition": edition,
@@ -63,13 +60,47 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
                 "RUST_MODFILE": "This is only for rust-analyzer"
             }
         }
-        if is_proc_macro:
-            proc_macro_dylib_name = subprocess.check_output(
-                [os.environ["RUSTC"], "--print", "file-names", "--crate-name", display_name, "--crate-type", "proc-macro", "-"],
+
+    def append_proc_macro_crate(
+        display_name,
+        root_module,
+        deps,
+        *,
+        cfg=None,
+        is_workspace_member=None,
+        edition=None,
+    ):
+        crate = build_crate(
+            display_name,
+            root_module,
+            deps,
+            cfg=cfg,
+            is_workspace_member=is_workspace_member,
+            edition=edition,
+        )
+        proc_macro_dylib_name = (
+            subprocess.check_output(
+                [
+                    os.environ["RUSTC"],
+                    "--print",
+                    "file-names",
+                    "--crate-name",
+                    display_name,
+                    "--crate-type",
+                    "proc-macro",
+                    "-",
+                ],
                 stdin=subprocess.DEVNULL,
-            ).decode('utf-8').strip()
-            crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
-        return crate
+            )
+            .decode("utf-8")
+            .strip()
+        )
+        proc_macro_crate = {
+            **crate,
+            "is_proc_macro": True,
+            "proc_macro_dylib_path": str(objtree / "rust" / proc_macro_dylib_name),
+        }
+        return register_crate(proc_macro_crate)
 
     def register_crate(crate):
         crates_indexes[crate["display_name"]] = len(crates)
@@ -82,7 +113,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         *,
         cfg=None,
         is_workspace_member=None,
-        is_proc_macro=None,
         edition=None,
     ):
         return register_crate(
@@ -92,7 +122,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
                 deps,
                 cfg=cfg,
                 is_workspace_member=is_workspace_member,
-                is_proc_macro=is_proc_macro,
                 edition=edition,
             )
         )
@@ -148,11 +177,10 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         cfg=crates_cfgs["syn"],
     )
 
-    append_crate(
+    append_proc_macro_crate(
         "macros",
         srctree / "rust" / "macros" / "lib.rs",
         ["std", "proc_macro", "proc_macro2", "quote", "syn"],
-        is_proc_macro=True,
     )
 
     append_crate(
@@ -161,12 +189,11 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         ["core", "compiler_builtins"],
     )
 
-    append_crate(
+    append_proc_macro_crate(
         "pin_init_internal",
         srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
         [],
         cfg=["kernel"],
-        is_proc_macro=True,
     )
 
     append_crate(
@@ -192,7 +219,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
             deps,
             cfg=cfg,
             is_workspace_member=True,
-            is_proc_macro=False,
             edition=None,
         )
         crate["env"]["OBJTREE"] = str(objtree.resolve(True))
Signature · 1 line
2.52.0
author maintainer
#3 lore raw

PATCH 3/4 scripts: generate_rust_analyzer.py: add type hints

In reply to Tamir Duberstein

Python type hints allow static analysis tools like mypy to detect type errors during development, improving the developer experience.

Python type hints have been present in the kernel since 2019 at the latest; see commit 6ebf5866f2e8 ("kunit: tool: add Python wrappers for running KUnit tests").

Add a subclass of argparse.Namespace to get type checking on the CLI arguments.

Run mypy --strict scripts/generate_rust_analyzer.py --python-version 3.9 to verify. Note that mypy no longer supports python < 3.9.

---
 scripts/generate_rust_analyzer.py | 130 ++++++++++++++++++++++++++------------
 1 file changed, 90 insertions(+), 40 deletions(-)
scripts/generate_rust_analyzer.py +90 -40
index bc79e4d8e8e7..2723154f207c 100755
@@ -10,8 +10,9 @@ import os
 import pathlib
 import subprocess
 import sys
+from typing import Dict, Iterable, List, Literal, Optional, TypedDict
 
-def args_crates_cfgs(cfgs):
+def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
     crates_cfgs = {}
     for cfg in cfgs:
         crate, vals = cfg.split("=", 1)
@@ -19,7 +20,43 @@ def args_crates_cfgs(cfgs):
 
     return crates_cfgs
 
-def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edition):
+class Dependency(TypedDict):
+    crate: int
+    name: str
+
+
+class Source(TypedDict):
+    include_dirs: List[str]
+    exclude_dirs: List[str]
+
+
+class Crate(TypedDict):
+    display_name: str
+    root_module: str
+    is_workspace_member: bool
+    deps: List[Dependency]
+    cfg: List[str]
+    edition: str
+    env: Dict[str, str]
+
+
+class ProcMacroCrate(Crate):
+    is_proc_macro: Literal[True]
+    proc_macro_dylib_path: str  # `pathlib.Path` is not JSON serializable.
+
+
+class CrateWithGenerated(Crate):
+    source: Source
+
+
+def generate_crates(
+    srctree: pathlib.Path,
+    objtree: pathlib.Path,
+    sysroot_src: pathlib.Path,
+    external_src: Optional[pathlib.Path],
+    cfgs: List[str],
+    core_edition: str,
+) -> List[Crate]:
     # Generate the configuration list.
     cfg = []
     with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -31,19 +68,19 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     # Now fill the crates list -- dependencies need to come first.
     #
     # Avoid O(n^2) iterations by keeping a map of indexes.
-    crates = []
-    crates_indexes = {}
+    crates: List[Crate] = []
+    crates_indexes: Dict[str, int] = {}
     crates_cfgs = args_crates_cfgs(cfgs)
 
     def build_crate(
-        display_name,
-        root_module,
-        deps,
+        display_name: str,
+        root_module: pathlib.Path,
+        deps: List[str],
         *,
-        cfg,
-        is_workspace_member,
-        edition,
-    ):
+        cfg: Optional[List[str]],
+        is_workspace_member: Optional[bool],
+        edition: Optional[str],
+    ) -> Crate:
         cfg = cfg if cfg is not None else []
         is_workspace_member = (
             is_workspace_member if is_workspace_member is not None else True
@@ -62,14 +99,14 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         }
 
     def append_proc_macro_crate(
-        display_name,
-        root_module,
-        deps,
+        display_name: str,
+        root_module: pathlib.Path,
+        deps: List[str],
         *,
-        cfg=None,
-        is_workspace_member=None,
-        edition=None,
-    ):
+        cfg: Optional[List[str]] = None,
+        is_workspace_member: Optional[bool] = None,
+        edition: Optional[str] = None,
+    ) -> None:
         crate = build_crate(
             display_name,
             root_module,
@@ -95,26 +132,26 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
             .decode("utf-8")
             .strip()
         )
-        proc_macro_crate = {
+        proc_macro_crate: ProcMacroCrate = {
             **crate,
             "is_proc_macro": True,
             "proc_macro_dylib_path": str(objtree / "rust" / proc_macro_dylib_name),
         }
         return register_crate(proc_macro_crate)
 
-    def register_crate(crate):
+    def register_crate(crate: Crate) -> None:
         crates_indexes[crate["display_name"]] = len(crates)
         crates.append(crate)
 
     def append_crate(
-        display_name,
-        root_module,
-        deps,
+        display_name: str,
+        root_module: pathlib.Path,
+        deps: List[str],
         *,
-        cfg=None,
-        is_workspace_member=None,
-        edition=None,
-    ):
+        cfg: Optional[List[str]] = None,
+        is_workspace_member: Optional[bool] = None,
+        edition: Optional[str] = None,
+    ) -> None:
         return register_crate(
             build_crate(
                 display_name,
@@ -127,12 +164,12 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         )
 
     def append_sysroot_crate(
-        display_name,
-        deps,
+        display_name: str,
+        deps: List[str],
         *,
-        cfg=None,
-        edition=None,
-    ):
+        cfg: Optional[List[str]] = None,
+        edition: Optional[str] = None,
+    ) -> None:
         return append_crate(
             display_name,
             sysroot_src / display_name / "src" / "lib.rs",
@@ -210,9 +247,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     )
 
     def append_crate_with_generated(
-        display_name,
-        deps,
-    ):
+        display_name: str,
+        deps: List[str],
+    ) -> None:
         crate = build_crate(
             display_name,
             srctree / "rust"/ display_name / "lib.rs",
@@ -222,7 +259,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
             edition=None,
         )
         crate["env"]["OBJTREE"] = str(objtree.resolve(True))
-        crate_with_generated = {
+        crate_with_generated: CrateWithGenerated = {
             **crate,
             "source": {
                 "include_dirs": [
@@ -238,7 +275,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
     append_crate_with_generated("kernel", ["core", "macros", "build_error", "pin_init", "ffi", "bindings", "uapi"])
 
-    def is_root_crate(build_file, target):
+    def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
         try:
             return f"{target}.o" in open(build_file).read()
         except FileNotFoundError:
@@ -247,7 +284,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
     # Then, the rest outside of `rust/`.
     #
     # We explicitly mention the top-level folders we want to cover.
-    extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
+    extra_dirs: Iterable[pathlib.Path] = (
+        srctree / dir for dir in ("samples", "drivers")
+    )
     if external_src is not None:
         extra_dirs = [external_src]
     for folder in extra_dirs:
@@ -270,7 +309,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
 
     return crates
 
-def main():
+def main() -> None:
     parser = argparse.ArgumentParser()
     parser.add_argument('--verbose', '-v', action='store_true')
     parser.add_argument('--cfgs', action='append', default=[])
@@ -280,7 +319,18 @@ def main():
     parser.add_argument("sysroot", type=pathlib.Path)
     parser.add_argument("sysroot_src", type=pathlib.Path)
     parser.add_argument("exttree", type=pathlib.Path, nargs="?")
-    args = parser.parse_args()
+
+    class Args(argparse.Namespace):
+        verbose: bool
+        cfgs: List[str]
+        srctree: pathlib.Path
+        objtree: pathlib.Path
+        sysroot: pathlib.Path
+        sysroot_src: pathlib.Path
+        exttree: Optional[pathlib.Path]
+        core_edition: str
+
+    args = parser.parse_args(namespace=Args())
 
     logging.basicConfig(
         format="[%(asctime)s] [%(levelname)s] %(message)s",
Signature · 1 line
2.52.0
author maintainer
#4 lore raw

PATCH 4/4 scripts: generate_rust_analyzer.py: identify crates explicitly

In reply to Tamir Duberstein

Use the return of append_crate to declare dependency on that crate. This removes the need to build an index of crates and allows multiple crates with the same display_name be defined, which allows e.g. host crates to be defined separately from target crates.

---
 scripts/generate_rust_analyzer.py | 82 ++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 40 deletions(-)
scripts/generate_rust_analyzer.py +42 -40
index 2723154f207c..b8fa1b4c37b0 100755
@@ -65,17 +65,14 @@ def generate_crates(
             line = line.replace("\n", "")
             cfg.append(line)
 
-    # Now fill the crates list -- dependencies need to come first.
-    #
-    # Avoid O(n^2) iterations by keeping a map of indexes.
+    # Now fill the crates list.
     crates: List[Crate] = []
-    crates_indexes: Dict[str, int] = {}
     crates_cfgs = args_crates_cfgs(cfgs)
 
     def build_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[str],
+        deps: List[Dependency],
         *,
         cfg: Optional[List[str]],
         is_workspace_member: Optional[bool],
@@ -90,7 +87,7 @@ def generate_crates(
             "display_name": display_name,
             "root_module": str(root_module),
             "is_workspace_member": is_workspace_member,
-            "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
+            "deps": deps,
             "cfg": cfg,
             "edition": edition,
             "env": {
@@ -101,12 +98,12 @@ def generate_crates(
     def append_proc_macro_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[str],
+        deps: List[Dependency],
         *,
         cfg: Optional[List[str]] = None,
         is_workspace_member: Optional[bool] = None,
         edition: Optional[str] = None,
-    ) -> None:
+    ) -> Dependency:
         crate = build_crate(
             display_name,
             root_module,
@@ -139,19 +136,20 @@ def generate_crates(
         }
         return register_crate(proc_macro_crate)
 
-    def register_crate(crate: Crate) -> None:
-        crates_indexes[crate["display_name"]] = len(crates)
+    def register_crate(crate: Crate) -> Dependency:
+        index = len(crates)
         crates.append(crate)
+        return {"crate": index, "name": crate["display_name"]}
 
     def append_crate(
         display_name: str,
         root_module: pathlib.Path,
-        deps: List[str],
+        deps: List[Dependency],
         *,
         cfg: Optional[List[str]] = None,
         is_workspace_member: Optional[bool] = None,
         edition: Optional[str] = None,
-    ) -> None:
+    ) -> Dependency:
         return register_crate(
             build_crate(
                 display_name,
@@ -165,11 +163,11 @@ def generate_crates(
 
     def append_sysroot_crate(
         display_name: str,
-        deps: List[str],
+        deps: List[Dependency],
         *,
         cfg: Optional[List[str]] = None,
         edition: Optional[str] = None,
-    ) -> None:
+    ) -> Dependency:
         return append_crate(
             display_name,
             sysroot_src / display_name / "src" / "lib.rs",
@@ -182,74 +180,76 @@ def generate_crates(
     # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
     # here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
     # for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
-    append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []), edition=core_edition)
-    append_sysroot_crate("alloc", ["core"])
-    append_sysroot_crate("std", ["alloc", "core"])
-    append_sysroot_crate("proc_macro", ["core", "std"])
+    core = append_sysroot_crate(
+        "core", [], cfg=crates_cfgs.get("core", []), edition=core_edition
+    )
+    alloc = append_sysroot_crate("alloc", [core])
+    std = append_sysroot_crate("std", [alloc, core])
+    proc_macro = append_sysroot_crate("proc_macro", [core, std])
 
-    append_crate(
+    compiler_builtins = append_crate(
         "compiler_builtins",
         srctree / "rust" / "compiler_builtins.rs",
         [],
     )
 
-    append_crate(
+    proc_macro2 = append_crate(
         "proc_macro2",
         srctree / "rust" / "proc-macro2" / "lib.rs",
-        ["core", "alloc", "std", "proc_macro"],
+        [core, alloc, std, proc_macro],
         cfg=crates_cfgs["proc_macro2"],
     )
 
-    append_crate(
+    quote = append_crate(
         "quote",
         srctree / "rust" / "quote" / "lib.rs",
-        ["alloc", "proc_macro", "proc_macro2"],
+        [alloc, proc_macro, proc_macro2],
         cfg=crates_cfgs["quote"],
     )
 
-    append_crate(
+    syn = append_crate(
         "syn",
         srctree / "rust" / "syn" / "lib.rs",
-        ["proc_macro", "proc_macro2", "quote"],
+        [proc_macro, proc_macro2, quote],
         cfg=crates_cfgs["syn"],
     )
 
-    append_proc_macro_crate(
+    macros = append_proc_macro_crate(
         "macros",
         srctree / "rust" / "macros" / "lib.rs",
-        ["std", "proc_macro", "proc_macro2", "quote", "syn"],
+        [std, proc_macro, proc_macro2, quote, syn],
     )
 
-    append_crate(
+    build_error = append_crate(
         "build_error",
         srctree / "rust" / "build_error.rs",
-        ["core", "compiler_builtins"],
+        [core, compiler_builtins],
     )
 
-    append_proc_macro_crate(
+    pin_init_internal = append_proc_macro_crate(
         "pin_init_internal",
         srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
         [],
         cfg=["kernel"],
     )
 
-    append_crate(
+    pin_init = append_crate(
         "pin_init",
         srctree / "rust" / "pin-init" / "src" / "lib.rs",
-        ["core", "pin_init_internal", "macros"],
+        [core, pin_init_internal, macros],
         cfg=["kernel"],
     )
 
-    append_crate(
+    ffi = append_crate(
         "ffi",
         srctree / "rust" / "ffi.rs",
-        ["core", "compiler_builtins"],
+        [core, compiler_builtins],
     )
 
     def append_crate_with_generated(
         display_name: str,
-        deps: List[str],
-    ) -> None:
+        deps: List[Dependency],
+    ) -> Dependency:
         crate = build_crate(
             display_name,
             srctree / "rust"/ display_name / "lib.rs",
@@ -271,9 +271,11 @@ def generate_crates(
         }
         return register_crate(crate_with_generated)
 
-    append_crate_with_generated("bindings", ["core", "ffi", "pin_init"])
-    append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
-    append_crate_with_generated("kernel", ["core", "macros", "build_error", "pin_init", "ffi", "bindings", "uapi"])
+    bindings = append_crate_with_generated("bindings", [core, ffi, pin_init])
+    uapi = append_crate_with_generated("uapi", [core, ffi, pin_init])
+    kernel = append_crate_with_generated(
+        "kernel", [core, macros, build_error, pin_init, ffi, bindings, uapi]
+    )
 
     def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
         try:
@@ -303,7 +305,7 @@ def generate_crates(
             append_crate(
                 name,
                 path,
-                ["core", "kernel"],
+                [core, kernel],
                 cfg=cfg,
             )
 
Signature · 1 line
2.52.0
reviewer Reviewed-by Tested-by on the v1 series
#5 lore raw
In reply to Tamir Duberstein
Quoted 11 lines · Tamir Duberstein

This series adds type annotations to generate_rust_analyzer.py. It is a subset of an earlier series[0] with formatting-only and drive-by patches removed (I sent some of these as separate single-patch series).

The final commit leverages type hints to tidy the implementation. It could be done separately from this series, but serves as a nice demonstration.

Great work, thanks! For the entire series,

Best regards,
Jesung

Quoted 16 lines

---
Tamir Duberstein (4):
scripts: generate_rust_analyzer.py: extract {build,register}_crate
scripts: generate_rust_analyzer.py: drop "is_proc_macro": false
scripts: generate_rust_analyzer.py: add type hints
scripts: generate_rust_analyzer.py: identify crates explicitly

 scripts/generate_rust_analyzer.py | 256 ++++++++++++++++++++++++++++----------
 1 file changed, 191 insertions(+), 65 deletions(-)

---
base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7
change-id: 20260122-rust-analyzer-types-f90bda766749

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
author maintainer Applied
#6 lore raw
In reply to Tamir Duberstein
Quoted 5 lines · Tamir Duberstein

This series adds type annotations to generate_rust_analyzer.py. It is a subset of an earlier series[0] with formatting-only and drive-by patches removed (I sent some of these as separate single-patch series).

Quoted 7 lines

The final commit leverages type hints to tidy the implementation. It could be done separately from this series, but serves as a nice demonstration.

Applied to rust-analyzer-next, thanks all!

#7 lore raw
Quoted 2 lines · Tamir Duberstein

Applied to rust-analyzer-next, thanks all!

To clarify, in most cases, branches are supposed to start from a tag from Linus, not something in between his tags.

In addition, the idea was that you would start applying patches next cycle, i.e. normally the branch should start applying patches with an -rc1 or -rc2 base, rather than at this time of the cycle, and e.g. finish around -rc5 time, and then you can send me your PR for that cycle and so on.

Let's talk about it in the next meeting or we can schedule a call.

(The patches seem to be applied with the msgid link and with tags collected, so that is fine :)

Cheers,
Miguel

author maintainer
#8 lore raw
Quoted 8 lines · Miguel Ojeda
Quoted 2 lines · Tamir Duberstein

Applied to rust-analyzer-next, thanks all!

To clarify, in most cases, branches are supposed to start from a tag from Linus, not something in between his tags.

Quoted 7 lines

In addition, the idea was that you would start applying patches next cycle, i.e. normally the branch should start applying patches with an -rc1 or -rc2 base, rather than at this time of the cycle, and e.g. finish around -rc5 time, and then you can send me your PR for that cycle and so on.

Let's talk about it in the next meeting or we can schedule a call.

Sure, will plan on it. I've updated the branch to start with a tag. Because _this_ series is so invasive, I wanted to get it applied sooner rather than later to unblock Jesung and Eliot.

Quoted 6 lines

(The patches seem to be applied with the msgid link and with tags collected, so that is fine :)

Cheers,
Miguel

author maintainer
#9 lore raw
Quoted 17 lines · Tamir Duberstein
Quoted 14 lines · Miguel Ojeda
Quoted 2 lines · Tamir Duberstein

Applied to rust-analyzer-next, thanks all!

To clarify, in most cases, branches are supposed to start from a tag from Linus, not something in between his tags.

In addition, the idea was that you would start applying patches next cycle, i.e. normally the branch should start applying patches with an -rc1 or -rc2 base, rather than at this time of the cycle, and e.g. finish around -rc5 time, and then you can send me your PR for that cycle and so on.

Quoted 12 lines
Quoted 1 line

Let's talk about it in the next meeting or we can schedule a call.

Sure, will plan on it. I've updated the branch to start with a tag. Because _this_ series is so invasive, I wanted to get it applied sooner rather than later to unblock Jesung and Eliot.

Quoted 6 lines

(The patches seem to be applied with the msgid link and with tags collected, so that is fine :)

Cheers,
Miguel

Apologies for the whiplash, I reset the branch back to rc7. In the meantime, these commits are available in
https://github.com/tamird/linux/tree/ra-next for folks working on the next round of improvements.

#12 lore raw
In reply to Tamir Duberstein
Quoted 4 lines · Tamir Duberstein

Sure, will plan on it. I've updated the branch to start with a tag. Because _this_ series is so invasive, I wanted to get it applied sooner rather than later to unblock Jesung and Eliot.

Thanks (I saw you then reset it to the tag itself, which is what I meant, yeah).

Sometimes maintainers have "private" (as in outside linux-next) for testing, collaboration, etc., but that can be done outside linux-next.

(We can create such a branch in the same repository if you feel you need it, e.g. rust-analyzer-dev or -testing or similar.)

Cheers,
Miguel

#10 lore raw
In reply to Tamir Duberstein
Quoted 5 lines · Tamir Duberstein

Apologies for the whiplash, I reset the branch back to rc7. In the meantime, these commits are available in
https://github.com/tamird/linux/tree/ra-next for folks working on the next round of improvements.

No worries at all, there is no harm done (linux-next was already done today, and it will likely be closed until Monday).

We can go through the usual timelines and how to prepare a PR etc. in the call (i.e. the usual bits I explain to new maintainers -- in the first call we didn't have time for much).

Thanks!

Cheers,
Miguel

author maintainer
#11 lore raw
Quoted 11 lines · Miguel Ojeda
Quoted 5 lines · Tamir Duberstein

Apologies for the whiplash, I reset the branch back to rc7. In the meantime, these commits are available in
https://github.com/tamird/linux/tree/ra-next for folks working on the next round of improvements.

No worries at all, there is no harm done (linux-next was already done today, and it will likely be closed until Monday).

Quoted 8 lines

We can go through the usual timelines and how to prepare a PR etc. in the call (i.e. the usual bits I explain to new maintainers -- in the first call we didn't have time for much).

Thanks!

Cheers,
Miguel

This is now applied to rust-analyzer-next for real. Thanks all!