From: Daniel Almeida <daniel.almeida@collabora.com>
To: Tamir Duberstein <tamird@gmail.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Boris-Chengbiao Zhou" <bobo1239@web.de>,
"Kees Cook" <kees@kernel.org>, "Fiona Behrens" <me@kloenk.dev>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
"Lukas Wirth" <lukas.wirth@ferrous-systems.com>
Subject: Re: [PATCH v4 05/11] scripts: generate_rust_analyzer.py: add type hints
Date: Tue, 25 Mar 2025 10:37:25 -0300 [thread overview]
Message-ID: <FB3D0F03-B162-4AD5-B288-4E307421276B@collabora.com> (raw)
In-Reply-To: <20250322-rust-analyzer-host-v4-5-1f51f9c907eb@gmail.com>
Hi Tamir,
> On 22 Mar 2025, at 10:23, Tamir Duberstein <tamird@gmail.com> wrote:
>
> 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").
>
> Run `mypy --strict scripts/generate_rust_analyzer.py --python-version
> 3.8` to verify. Note that `mypy` no longer supports python < 3.8.
>
> This removes `"is_proc_macro": false` from `rust-project.json` in
> exchange for stricter types. This field is interpreted as false if
> absent[1] so this doesn't change the behavior of rust-analyzer.
Can this be a separate patch? Not sure how this is related to Python type
hints, but it makes the current patch harder to review.
>
> Link: https://github.com/rust-lang/rust-analyzer/blob/8d01570b5e812a49daa1f08404269f6ea5dd73a1/crates/project-model/src/project_json.rs#L372-L373 [1]
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> scripts/generate_rust_analyzer.py | 172 +++++++++++++++++++++++++-------------
> 1 file changed, 112 insertions(+), 60 deletions(-)
>
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 03f55cce673c..0772ea309f94 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -10,8 +10,10 @@ 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: Iterable[str]) -> Dict[str, List[str]]:
> crates_cfgs = {}
> for cfg in cfgs:
> crate, vals = cfg.split("=", 1)
> @@ -19,7 +21,45 @@ def args_crates_cfgs(cfgs):
>
> return crates_cfgs
>
> -def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> +
> +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: Literal["2021"]
> + env: Dict[str, str]
> +
> +
> +# `NotRequired` fields on `Crate` would be better but `NotRequired` was added in 3.11.
> +class ProcMacroCrate(Crate):
> + is_proc_macro: Literal[True]
> + proc_macro_dylib_path: Optional[str] # `pathlib.Path` is not JSON serializable.
> +
> +
> +# `NotRequired` fields on `Crate` would be better but `NotRequired` was added in 3.11.
> +class CrateWithGenerated(Crate):
> + source: Optional[Source]
> +
> +
> +def generate_crates(
> + srctree: pathlib.Path,
> + objtree: pathlib.Path,
> + sysroot_src: pathlib.Path,
> + external_src: pathlib.Path,
> + cfgs: List[str],
> +) -> List[Crate]:
> # Generate the configuration list.
> cfg = []
> with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
> @@ -31,67 +71,75 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> # 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,
> - cfg=[],
> - is_workspace_member=True,
> - is_proc_macro=False,
> - ):
> - crate = {
> + display_name: str,
> + root_module: pathlib.Path,
> + deps: List[str],
> + cfg: List[str] = [],
> + is_workspace_member: bool = True,
> + ) -> 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": "2021",
> "env": {
> "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", "-"],
> - stdin=subprocess.DEVNULL,
> - ).decode('utf-8').strip()
> - crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
> - return 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,
> - cfg=[],
> - is_workspace_member=True,
> - is_proc_macro=False,
> - ):
> + display_name: str,
> + root_module: pathlib.Path,
> + deps: List[str],
> + cfg: List[str] = [],
> + is_workspace_member: bool = True,
> + ) -> None:
> register_crate(
> - build_crate(
> - display_name, root_module, deps, cfg, is_workspace_member, is_proc_macro
> - )
> + build_crate(display_name, root_module, deps, cfg, is_workspace_member)
> )
>
> + def append_proc_macro_crate(
> + display_name: str,
> + root_module: pathlib.Path,
> + deps: List[str],
> + cfg: List[str] = [],
> + ) -> None:
> + crate = build_crate(display_name, root_module, deps, cfg)
> + 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()
> + proc_macro_crate: ProcMacroCrate = {
> + **crate,
> + "is_proc_macro": True,
> + "proc_macro_dylib_path": f"{objtree}/rust/{proc_macro_dylib_name}",
> + }
> + register_crate(proc_macro_crate)
> +
Same here. Is this supposed to be related to Python type hints somehow?
> def append_sysroot_crate(
> - display_name,
> - deps,
> - cfg=[],
> - ):
> - append_crate(
> - display_name,
> - sysroot_src / display_name / "src" / "lib.rs",
> - deps,
> - cfg,
> - is_workspace_member=False,
> + display_name: str,
> + deps: List[str],
> + cfg: List[str] = [],
> + ) -> None:
> + register_crate(
Why is register_crate here now? Maybe this change belongs to the preceding patch?
> + build_crate(
> + display_name,
> + sysroot_src / display_name / "src" / "lib.rs",
> + deps,
> + cfg,
> + is_workspace_member=False,
> + )
> )
>
> # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
> @@ -108,11 +156,10 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> [],
> )
>
> - append_crate(
> + append_proc_macro_crate(
> "macros",
> srctree / "rust" / "macros" / "lib.rs",
> ["std", "proc_macro"],
> - is_proc_macro=True,
> )
>
> append_crate(
> @@ -121,12 +168,11 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> ["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(
> @@ -137,9 +183,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> )
>
> 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",
> @@ -147,20 +193,23 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> cfg=cfg,
> )
> crate["env"]["OBJTREE"] = str(objtree.resolve(True))
> - crate["source"] = {
> - "include_dirs": [
> - str(srctree / "rust" / display_name),
> - str(objtree / "rust")
> - ],
> - "exclude_dirs": [],
> + crate_with_generated: CrateWithGenerated = {
> + **crate,
> + "source": {
> + "include_dirs": [
> + str(srctree / "rust" / display_name),
> + str(objtree / "rust")
> + ],
> + "exclude_dirs": [],
> + }
> }
> - register_crate(crate)
> + register_crate(crate_with_generated)
>
> append_crate_with_generated("bindings", ["core"])
> append_crate_with_generated("uapi", ["core"])
> append_crate_with_generated("kernel", ["core", "macros", "build_error", "bindings", "pin_init", "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:
> @@ -169,7 +218,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
> # 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] = map(
> + lambda dir: srctree / dir, ("samples", "drivers")
> + )
> if external_src is not None:
> extra_dirs = [external_src]
> for folder in extra_dirs:
> @@ -192,7 +243,8 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
>
> return crates
>
> -def main():
> +
What is this extra blank line for? Automatically generated by a formatter?
> +def main() -> None:
> parser = argparse.ArgumentParser()
> parser.add_argument("--verbose", "-v", action="store_true")
> parser.add_argument("--cfgs", action="append", default=[])
>
> --
> 2.48.1
>
>
Other than what I said above, the type hints themselves are fine.
— Daniel
next prev parent reply other threads:[~2025-03-25 13:38 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-22 13:23 [PATCH v4 00/11] rust: generate_rust_analyzer.py: define host crates and scripts Tamir Duberstein
2025-03-22 13:23 ` [PATCH v4 01/11] scripts: generate_rust_analyzer.py: add missing whitespace Tamir Duberstein
2025-03-25 13:08 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 02/11] scripts: generate_rust_analyzer.py: use double quotes Tamir Duberstein
2025-03-25 13:11 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 03/11] scripts: generate_rust_analyzer.py: add trailing comma Tamir Duberstein
2025-03-25 13:12 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 04/11] scripts: generate_rust_analyzer.py: extract `{build,register}_crate` Tamir Duberstein
2025-03-25 13:24 ` Daniel Almeida
2025-03-25 16:51 ` Fiona Behrens
2025-03-22 13:23 ` [PATCH v4 05/11] scripts: generate_rust_analyzer.py: add type hints Tamir Duberstein
2025-03-25 13:37 ` Daniel Almeida [this message]
2025-03-25 13:39 ` Daniel Almeida
2025-03-25 14:19 ` Miguel Ojeda
2025-03-25 14:40 ` Daniel Almeida
2025-03-25 16:47 ` Tamir Duberstein
2025-03-22 13:23 ` [PATCH v4 06/11] scripts: generate_rust_analyzer.py: use str(pathlib.Path) Tamir Duberstein
2025-03-25 13:56 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 07/11] scripts: generate_rust_analyzer.py: identify crates explicitly Tamir Duberstein
2025-03-25 14:01 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 08/11] scripts: generate_rust_analyzer.py: define host crates Tamir Duberstein
2025-03-25 14:05 ` Daniel Almeida
2025-03-22 13:23 ` [PATCH v4 09/11] scripts: generate_rust_analyzer.py: avoid FD leak Tamir Duberstein
2025-03-25 14:06 ` Daniel Almeida
2025-03-25 16:51 ` Fiona Behrens
2025-03-22 13:23 ` [PATCH v4 10/11] scripts: generate_rust_analyzer.py: define scripts Tamir Duberstein
2025-03-25 14:08 ` Daniel Almeida
2025-03-25 16:53 ` Fiona Behrens
2025-03-22 13:23 ` [PATCH v4 11/11] scripts: generate_rust_analyzer.py: use `cfg_groups` Tamir Duberstein
2025-03-23 12:40 ` Miguel Ojeda
2025-03-25 14:38 ` Daniel Almeida
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=FB3D0F03-B162-4AD5-B288-4E307421276B@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=bobo1239@web.de \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas.wirth@ferrous-systems.com \
--cc=me@kloenk.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®