[PATCH 3/6] scripts: generate_rust_analyzer: plumb crate-attrs

Eliot Courtney posted 6 patches 2 weeks, 5 days ago
[PATCH 3/6] scripts: generate_rust_analyzer: plumb crate-attrs
Posted by Eliot Courtney 2 weeks, 5 days ago
Add --crate-attrs argument to pass per-crate attributes.

The crate_attrs field was added to rust-analyzer in v0.3.2727 (~1.94.0)
and is silently ignored by older versions, so it's safe to add it.

Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
---
 rust/Makefile                     | 5 ++++-
 scripts/generate_rust_analyzer.py | 9 +++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/rust/Makefile b/rust/Makefile
index 2238b0b69197..e6c5108ab625 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -84,9 +84,11 @@ proc_macro2-cfgs := \
     $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
 
 # Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
+proc_macro2-crate-attrs := feature(proc_macro_byte_character,proc_macro_c_str_literals)
+
 proc_macro2-flags := \
     --cap-lints=allow \
-    -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
+    -Zcrate-attr='$(proc_macro2-crate-attrs)' \
     $(call cfgs-to-flags,$(proc_macro2-cfgs))
 
 quote-cfgs := \
@@ -575,6 +577,7 @@ rust-analyzer:
 		--cfgs='syn=$(syn-cfgs)' \
 		--editions='core=$(core-edition)' \
 		--editions='quote=$(quote-edition)' \
+		--crate-attrs='proc_macro2=$(proc_macro2-crate-attrs)' \
 		$(realpath $(srctree)) $(realpath $(objtree)) \
 		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
 		> rust-project.json
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 17ed5546504b..e8c50812fb9f 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -11,6 +11,7 @@ import pathlib
 import subprocess
 import sys
 
+
 def args_single(args):
     result = {}
     for arg in args:
@@ -26,7 +27,7 @@ def args_crates_cfgs(cfgs):
 
     return crates_cfgs
 
-def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions):
+def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, crate_attrs):
     # Generate the configuration list.
     generated_cfg = []
     with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -42,6 +43,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions)
     crates_indexes = {}
     crates_cfgs = args_crates_cfgs(cfgs)
     crates_editions = args_single(editions)
+    crates_crate_attrs = args_crates_cfgs(crate_attrs)
 
     def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
         # Miguel Ojeda writes:
@@ -78,6 +80,8 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions)
             "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
             "cfg": cfg,
             "edition": edition,
+            # Crate attributes were introduced in 1.94.0 but older versions will silently ignore this.
+            "crate_attrs": crates_crate_attrs.get(display_name, []),
             "env": {
                 "RUST_MODFILE": "This is only for rust-analyzer"
             }
@@ -233,6 +237,7 @@ def main():
     parser.add_argument('--verbose', '-v', action='store_true')
     parser.add_argument('--cfgs', action='append', default=[])
     parser.add_argument('--editions', action='append', default=[])
+    parser.add_argument('--crate-attrs', action='append', default=[])
     parser.add_argument("srctree", type=pathlib.Path)
     parser.add_argument("objtree", type=pathlib.Path)
     parser.add_argument("sysroot", type=pathlib.Path)
@@ -246,7 +251,7 @@ def main():
     )
 
     rust_project = {
-        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions),
+        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions, args.crate_attrs),
         "sysroot": str(args.sysroot),
     }
 

-- 
2.52.0
Re: [PATCH 3/6] scripts: generate_rust_analyzer: plumb crate-attrs
Posted by Tamir Duberstein 2 weeks, 5 days ago
On Tue, Jan 20, 2026 at 3:54 AM Eliot Courtney <ecourtney@nvidia.com> wrote:
>
> Add --crate-attrs argument to pass per-crate attributes.
>
> The crate_attrs field was added to rust-analyzer in v0.3.2727 (~1.94.0)
> and is silently ignored by older versions, so it's safe to add it.

Please add citations for both claims made here.

>
> Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
> ---
>  rust/Makefile                     | 5 ++++-
>  scripts/generate_rust_analyzer.py | 9 +++++++--
>  2 files changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/rust/Makefile b/rust/Makefile
> index 2238b0b69197..e6c5108ab625 100644
> --- a/rust/Makefile
> +++ b/rust/Makefile
> @@ -84,9 +84,11 @@ proc_macro2-cfgs := \
>      $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
>
>  # Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
> +proc_macro2-crate-attrs := feature(proc_macro_byte_character,proc_macro_c_str_literals)
> +
>  proc_macro2-flags := \
>      --cap-lints=allow \
> -    -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
> +    -Zcrate-attr='$(proc_macro2-crate-attrs)' \
>      $(call cfgs-to-flags,$(proc_macro2-cfgs))
>
>  quote-cfgs := \
> @@ -575,6 +577,7 @@ rust-analyzer:
>                 --cfgs='syn=$(syn-cfgs)' \
>                 --editions='core=$(core-edition)' \
>                 --editions='quote=$(quote-edition)' \
> +               --crate-attrs='proc_macro2=$(proc_macro2-crate-attrs)' \
>                 $(realpath $(srctree)) $(realpath $(objtree)) \
>                 $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
>                 > rust-project.json
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 17ed5546504b..e8c50812fb9f 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -11,6 +11,7 @@ import pathlib
>  import subprocess
>  import sys
>
> +
>  def args_single(args):
>      result = {}
>      for arg in args:
> @@ -26,7 +27,7 @@ def args_crates_cfgs(cfgs):
>
>      return crates_cfgs
>
> -def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions):
> +def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, crate_attrs):
>      # Generate the configuration list.
>      generated_cfg = []
>      with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
> @@ -42,6 +43,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions)
>      crates_indexes = {}
>      crates_cfgs = args_crates_cfgs(cfgs)
>      crates_editions = args_single(editions)
> +    crates_crate_attrs = args_crates_cfgs(crate_attrs)
>
>      def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
>          # Miguel Ojeda writes:
> @@ -78,6 +80,8 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions)
>              "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
>              "cfg": cfg,
>              "edition": edition,
> +            # Crate attributes were introduced in 1.94.0 but older versions will silently ignore this.

Citations needed.


> +            "crate_attrs": crates_crate_attrs.get(display_name, []),
>              "env": {
>                  "RUST_MODFILE": "This is only for rust-analyzer"
>              }
> @@ -233,6 +237,7 @@ def main():
>      parser.add_argument('--verbose', '-v', action='store_true')
>      parser.add_argument('--cfgs', action='append', default=[])
>      parser.add_argument('--editions', action='append', default=[])
> +    parser.add_argument('--crate-attrs', action='append', default=[])
>      parser.add_argument("srctree", type=pathlib.Path)
>      parser.add_argument("objtree", type=pathlib.Path)
>      parser.add_argument("sysroot", type=pathlib.Path)
> @@ -246,7 +251,7 @@ def main():
>      )
>
>      rust_project = {
> -        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions),
> +        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions, args.crate_attrs),
>          "sysroot": str(args.sysroot),
>      }
>
>
> --
> 2.52.0
>
>