Skip to contentSkip to conversation

rust-analyzer: update generate_rust_analyzer to pass cfg to macros crate

PATCH Superseded Malte Wechter 3 participants 5 messages May 8 – May 11

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust-analyzer: update generate_rust_analyzer to pass cfg to macros crate

pass kernel configurations to the macros crate to prevent rust-analyzer from marking conditionally included macros as 'never included'. Add the generated configurations to the macros cfg list
so rust-analyzer correctly hints inclusion and exclusion.

---
 scripts/generate_rust_analyzer.py | 1 +
 1 file changed, 1 insertion(+)
scripts/generate_rust_analyzer.py +1 -0
index d5f9a0ca742c..69990a96522e 100755
@@ -238,6 +238,7 @@ def generate_crates(
         "macros",
         srctree / "rust" / "macros" / "lib.rs",
         [std, proc_macro, proc_macro2, quote, syn],
+        cfg=generated_cfg,
     )
 
     build_error = append_crate(

---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260506-rust-analyzer-macro-22f387f8eff4

Best regards,

Signature · 1 line
Malte Wechter <[email protected]>
maintainer
#1 lore raw
Quoted 7 lines · Malte Wechter

pass kernel configurations to the macros crate to prevent rust-analyzer from marking conditionally included macros as 'never included'. Add the generated configurations to the macros cfg list
so rust-analyzer correctly hints inclusion and exclusion.

Hi Malte, thanks for your patch. I haven't seen this issue. Could you please help me reproduce what you're seeing?

Directionally I think this patch is incorrect because the macros crate is a host-side crate and generated_cfg is for target-side cfgs.

Tamir

#2 lore raw

Hi Tamir,

Quoted 11 lines · Tamir Duberstein
Quoted 7 lines · Malte Wechter

pass kernel configurations to the macros crate to prevent rust-analyzer from marking conditionally included macros as 'never included'. Add the generated configurations to the macros cfg list
so rust-analyzer correctly hints inclusion and exclusion.

Hi Malte, thanks for your patch. I haven't seen this issue. Could you please help me reproduce what you're seeing?

We are moving some of the configfs macro logic to procedural implementations, now that we have syn. We are going to gate the macro on CONFIG_CONFIGFS_FS. Malte noticed that rust-analyzer does not understand the gating in the macros crate. rust-analyzer does not know that CONFIG_CONFIGFS_FS is set, so rust-analyzer does not function at all for our module:

rust/macros/lib.rs +2 -0
@@ -15,6 +15,8 @@
 #![cfg_attr(not(CONFIG_RUSTC_HAS_SPAN_FILE), feature(proc_macro_span))]
 
 mod concat_idents;
+#[cfg(CONFIG_CONFIGFS_FS)]
+mod configfs_attrs;
 mod export;
 
Quoted 2 lines

Directionally I think this patch is incorrect because the macros crate is a host-side crate and generated_cfg is for target-side cfgs.

We might want to differentiate macro implementations based on kernel configuration, or as in our case, skip compilation of certain macros when they are not used in the kernel build.

Best regards,
Andreas Hindborg

#3 lore raw

Andreas Hindborg <[email protected]> writes:

<cut>

Quoted 6 lines
Quoted 2 lines

Directionally I think this patch is incorrect because the macros crate is a host-side crate and generated_cfg is for target-side cfgs.

We might want to differentiate macro implementations based on kernel configuration, or as in our case, skip compilation of certain macros when they are not used in the kernel build.

Also, the macros crate is actually built with these configuration attributes applied. It is only logical that the rust-analyzer configuration reflect how we build the crate.

Best regards,
Andreas Hindborg

maintainer
#4 lore raw
Quoted 8 lines · Andreas Hindborg

Andreas Hindborg <[email protected]> writes:

<cut>

Quoted 3 lines
Quoted 2 lines

Directionally I think this patch is incorrect because the macros crate is a host-side crate and generated_cfg is for target-side cfgs.

Quoted 7 lines
Quoted 3 lines

We might want to differentiate macro implementations based on kernel configuration, or as in our case, skip compilation of certain macros when they are not used in the kernel build.

Also, the macros crate is actually built with these configuration attributes applied. It is only logical that the rust-analyzer configuration reflect how we build the crate.

Hi Andreas,

These justifications should be in the commit message. I agree that the macros crate is receiving those cfgs, that was added in 36174d16 ("rust: kunit: support KUnit-mapped assert! macros in #[test]s") - this should also be mentioned in the commit message please.