Skip to contentSkip to conversation

scripts: generate_rust_analyzer: compile sysroot with correct edition

PATCH Superseded Tamir Duberstein 4 participants 10 messages Jan 15 – Jan 16

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tamir Duberstein

author maintainer
#0 lore raw

PATCH 1/1 scripts: generate_rust_analyzer: compile sysroot with correct edition

From: Tamir Duberstein <[email protected]>

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

Note that backporting this will conflict unless commit 46e58a9637ec ("rust:
kbuild: introduce core-flags and core-skip_flags") is also backported.

Fixes f4daa80d6be7 ("rust: compile libcore with edition 2024 for 1.87+")
---
 rust/Makefile                     |  7 ++++---
 scripts/generate_rust_analyzer.py | 11 +++++------
 2 files changed, 9 insertions(+), 9 deletions(-)
rust/Makefile +4 -3
index 4dcc2eff51cb..a8ded6c07255 100644
@@ -67,7 +67,7 @@ cfgs-to-flags = $(patsubst %,--cfg='%',$1)
 core-cfgs := \
     no_fp_fmt_parse
 
-core-edition := $(if $(call rustc-min-version,108700),2024,2021)
+sysroot_edition := $(if $(call rustc-min-version,108700),2024,2021)
 
 core-skip_flags := \
     --edition=2021 \
@@ -75,7 +75,7 @@ core-skip_flags := \
     -Wrustdoc::unescaped_backticks
 
 core-flags := \
-    --edition=$(core-edition) \
+    --edition=$(sysroot_edition) \
     $(call cfgs-to-flags,$(core-cfgs))
 
 proc_macro2-cfgs := \
@@ -567,7 +567,8 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
 
 rust-analyzer:
 	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
-		--cfgs='core=$(core-cfgs)' $(core-edition) \
+		$(sysroot_edition) \
+		--cfgs='core=$(core-cfgs)' \
 		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
 		--cfgs='quote=$(quote-cfgs)' \
 		--cfgs='syn=$(syn-cfgs)' \
scripts/generate_rust_analyzer.py +5 -6
index 6061bd6e2ebd..47e49f8dacbc 100755
@@ -19,7 +19,7 @@ def args_crates_cfgs(cfgs):
 
     return crates_cfgs
 
-def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edition):
+def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, sysroot_edition):
     # Generate the configuration list.
     cfg = []
     with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -61,7 +61,6 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
         display_name,
         deps,
         cfg=[],
-        edition="2021",
     ):
         append_crate(
             display_name,
@@ -69,13 +68,13 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
             deps,
             cfg,
             is_workspace_member=False,
-            edition=edition,
+            edition=sysroot_edition,
         )
 
     # 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("core", [], cfg=crates_cfgs.get("core", []))
     append_sysroot_crate("alloc", ["core"])
     append_sysroot_crate("std", ["alloc", "core"])
     append_sysroot_crate("proc_macro", ["core", "std"])
@@ -200,7 +199,7 @@ def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('--verbose', '-v', action='store_true')
     parser.add_argument('--cfgs', action='append', default=[])
-    parser.add_argument("core_edition")
+    parser.add_argument("sysroot_edition")
     parser.add_argument("srctree", type=pathlib.Path)
     parser.add_argument("objtree", type=pathlib.Path)
     parser.add_argument("sysroot", type=pathlib.Path)
@@ -217,7 +216,7 @@ def main():
     assert args.sysroot in args.sysroot_src.parents
 
     rust_project = {
-        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.core_edition),
+        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.sysroot_edition),
         "sysroot": str(args.sysroot),
     }
 

---
base-commit: 74e15ac34b098934895fd27655d098971d2b43d9
change-id: 20260115-rust-analyzer-sysroot-5fb95aa985d2

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw
Quoted 4 lines · Tamir Duberstein

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

I think, in principle, even the sysroot crates may have different editions, which I think I used that variable name.

For instance, in the move to 2024, it seems all happened at once in 1.87.0 in these upstream commits, so that seems OK:

    0e071c2c6a58 ("Migrate core to Rust 2024")
    f505d4e8e380 ("Migrate alloc to Rust 2024")
    0b2489c226c3 ("Migrate proc_macro to Rust 2024")
    993359e70112 ("Migrate std to Rust 2024")

But in the previous move to 2021, std moved in 1.59.0, while the others in 1.60.0:

    b656384d8398 ("Update stdlib to the 2021 edition")
    06a1c14d52a8 ("Switch all libraries to the 2021 edition")

Hmm... I guess the new name is fine, but we may need to go back to separate naming eventually if they get updated at different times next time.

By the way, it says sysroot-edition in the message -- I guess you then later used underscore because it is now not attached to a particular crate? In that case, I can update the message.

Quoted 2 lines

Note that backporting this will conflict unless commit 46e58a9637ec ("rust:
kbuild: introduce core-flags and core-skip_flags") is also backported.

This normally is solved on backporting time providing a resolved patch or, if you think it is worth backporting the other one too, as a prerequisite in the Cc: stable lines (see Documentation/process/stable-kernel-rules.rst).

Quoted 1 line
Fixes f4daa80d6be7 ("rust: compile libcore with edition 2024 for 1.87+")

Since this commit is previous to existing kernel branches and you mention it above, please consider adding Cc: stable.

Quoted 1 line

Cc'ing Jesung who is becoming a reviewer for rust-analyzer.

Thanks!

Cheers,
Miguel

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

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

I think, in principle, even the sysroot crates may have different editions, which I think I used that variable name.

For instance, in the move to 2024, it seems all happened at once in 1.87.0 in these upstream commits, so that seems OK:

    0e071c2c6a58 ("Migrate core to Rust 2024")
    f505d4e8e380 ("Migrate alloc to Rust 2024")
    0b2489c226c3 ("Migrate proc_macro to Rust 2024")
    993359e70112 ("Migrate std to Rust 2024")
Quoted 5 lines

But in the previous move to 2021, std moved in 1.59.0, while the others in 1.60.0:

    b656384d8398 ("Update stdlib to the 2021 edition")
    06a1c14d52a8 ("Switch all libraries to the 2021 edition")

Good point, I hadn't considered that (nor did I do that archaeology).

Quoted 3 lines

Hmm... I guess the new name is fine, but we may need to go back to separate naming eventually if they get updated at different times next time.

👍

Quoted 3 lines

By the way, it says sysroot-edition in the message -- I guess you then later used underscore because it is now not attached to a particular crate? In that case, I can update the message.

Ah, I think I wrote that when I was under the impression that --sysroot-edition was a named argument to the script but it turned out to be positional. So: you are right that the message should be updated.

Quoted 8 lines
Quoted 3 lines

Note that backporting this will conflict unless commit 46e58a9637ec ("rust:
kbuild: introduce core-flags and core-skip_flags") is also backported.

This normally is solved on backporting time providing a resolved patch or, if you think it is worth backporting the other one too, as a prerequisite in the Cc: stable lines (see Documentation/process/stable-kernel-rules.rst).

Ah, thanks. If the prerequisite isn't backported, who provides the resolved patch?

Quoted 5 lines
Quoted 2 lines
Fixes f4daa80d6be7 ("rust: compile libcore with edition 2024 for 1.87+")

Since this commit is previous to existing kernel branches and you mention it above, please consider adding Cc: stable.

Ack, I guess that will have to happen at apply time for this one?

Quoted 8 lines
Quoted 1 line

Cc'ing Jesung who is becoming a reviewer for rust-analyzer.

Thanks!

Cheers,
Miguel

Thanks Miguel.

#3 lore raw
Quoted 3 lines · Tamir Duberstein

Ah, thanks. If the prerequisite isn't backported, who provides the resolved patch?

The maintainers, typically, or perhaps authors of the patch -- when a patch doesn't apply cleanly, the stable team sends an email to the relevant people with instructions on the top, which then can used to provide them with a resolution (essentially replying with a new patch).

Quoted 1 line

Ack, I guess that will have to happen at apply time for this one?

Yeah, I can do that:

Quoted 1 line

Thanks Miguel.

You're welcome!

Cheers,
Miguel

reviewer
#4 lore raw
In reply to Miguel Ojeda
Quoted 20 lines · Miguel Ojeda
Quoted 4 lines · Tamir Duberstein

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

I think, in principle, even the sysroot crates may have different editions, which I think I used that variable name.

For instance, in the move to 2024, it seems all happened at once in 1.87.0 in these upstream commits, so that seems OK:

    0e071c2c6a58 ("Migrate core to Rust 2024")
    f505d4e8e380 ("Migrate alloc to Rust 2024")
    0b2489c226c3 ("Migrate proc_macro to Rust 2024")
    993359e70112 ("Migrate std to Rust 2024")

But in the previous move to 2021, std moved in 1.59.0, while the others in 1.60.0:

Quoted 6 lines
    b656384d8398 ("Update stdlib to the 2021 edition")
    06a1c14d52a8 ("Switch all libraries to the 2021 edition")

Hmm... I guess the new name is fine, but we may need to go back to separate naming eventually if they get updated at different times next time.

Perhaps we should introduce std-edition and proc_macro-edition in rust/Makefile? Passing those down to the script would make things more future-proof.

Best regards,
Jesung

reviewer
#5 lore raw
Quoted 24 lines · Jesung Yang
Quoted 23 lines · Miguel Ojeda
Quoted 4 lines · Tamir Duberstein

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

I think, in principle, even the sysroot crates may have different editions, which I think I used that variable name.

For instance, in the move to 2024, it seems all happened at once in 1.87.0 in these upstream commits, so that seems OK:

    0e071c2c6a58 ("Migrate core to Rust 2024")
    f505d4e8e380 ("Migrate alloc to Rust 2024")
    0b2489c226c3 ("Migrate proc_macro to Rust 2024")
    993359e70112 ("Migrate std to Rust 2024")

But in the previous move to 2021, std moved in 1.59.0, while the others in 1.60.0:

    b656384d8398 ("Update stdlib to the 2021 edition")
    06a1c14d52a8 ("Switch all libraries to the 2021 edition")
Quoted 8 lines
Quoted 3 lines

Hmm... I guess the new name is fine, but we may need to go back to separate naming eventually if they get updated at different times next time.

Perhaps we should introduce std-edition and proc_macro-edition in rust/Makefile? Passing those down to the script would make things more future-proof.

We don't build std nor proc-macro ourselves in rust/Makefile, so I don't think this should got there.

I wonder if we should encode the fact about Rust version -> crate edition mapping inside the Rust analyzer script? then we still call the variable core-edition (which is the crate we need to build).

Best,
Gary

author maintainer
#6 lore raw
Quoted 35 lines · Gary Guo
Quoted 24 lines · Jesung Yang
Quoted 23 lines · Miguel Ojeda
Quoted 4 lines · Tamir Duberstein

Rename core-edition to sysroot-edition to align with the naming used to refer to standard library crates in generate_rust_analyzer.py and apply it to all standard library crates rather than just core.

I think, in principle, even the sysroot crates may have different editions, which I think I used that variable name.

For instance, in the move to 2024, it seems all happened at once in 1.87.0 in these upstream commits, so that seems OK:

    0e071c2c6a58 ("Migrate core to Rust 2024")
    f505d4e8e380 ("Migrate alloc to Rust 2024")
    0b2489c226c3 ("Migrate proc_macro to Rust 2024")
    993359e70112 ("Migrate std to Rust 2024")

But in the previous move to 2021, std moved in 1.59.0, while the others in 1.60.0:

    b656384d8398 ("Update stdlib to the 2021 edition")
    06a1c14d52a8 ("Switch all libraries to the 2021 edition")
Quoted 8 lines
Quoted 3 lines

Hmm... I guess the new name is fine, but we may need to go back to separate naming eventually if they get updated at different times next time.

Perhaps we should introduce std-edition and proc_macro-edition in rust/Makefile? Passing those down to the script would make things more future-proof.

Quoted 6 lines

We don't build std nor proc-macro ourselves in rust/Makefile, so I don't think this should got there.

I wonder if we should encode the fact about Rust version -> crate edition mapping inside the Rust analyzer script? then we still call the variable core-edition (which is the crate we need to build).

If that's the approach we want to take, that would likely build on
https://lore.kernel.org/all/[email protected]/.

#7 lore raw
Quoted 6 lines · Tamir Duberstein
Quoted 4 lines · Gary Guo

We don't build std nor proc-macro ourselves in rust/Makefile, so I don't think this should got there.

Quoted 6 lines
Quoted 3 lines

I wonder if we should encode the fact about Rust version -> crate edition mapping inside the Rust analyzer script? then we still call the variable core-edition (which is the crate we need to build).

If that's the approach we want to take, that would likely build on
https://lore.kernel.org/all/[email protected]/.

For fixes, we try to keep them simple as possible in general, i.e. like this patch. Or, actually, even simpler: we could keep the variable name but use it as the sysroot edition on the other side, since it happens to match at the moment.

After all, it is a script call -- the script wants the sysroot edition, and we happen to have it from our core edition, so it all works fine.

i.e. we can just drop the rust/Makefile side change for the fix, which also means a smaller backport (if we don't use the prerrequisite).

Another option is dropping these, if there are no practical consequences (did you notice anything very broken using rust-analyzer? It would be nice to mention those in the commit message, if you saw any), but it seems best to fix it, especially since it comes from the sysroot and thus I imagine it could have an impact later on due to change in upstream code.

Cheers,
Miguel

author maintainer
#8 lore raw
Quoted 20 lines · Miguel Ojeda
Quoted 6 lines · Tamir Duberstein
Quoted 4 lines · Gary Guo

We don't build std nor proc-macro ourselves in rust/Makefile, so I don't think this should got there.

Quoted 6 lines
Quoted 3 lines

I wonder if we should encode the fact about Rust version -> crate edition mapping inside the Rust analyzer script? then we still call the variable core-edition (which is the crate we need to build).

If that's the approach we want to take, that would likely build on
https://lore.kernel.org/all/[email protected]/.

For fixes, we try to keep them simple as possible in general, i.e. like this patch. Or, actually, even simpler: we could keep the variable name but use it as the sysroot edition on the other side, since it happens to match at the moment.

Quoted 7 lines

After all, it is a script call -- the script wants the sysroot edition, and we happen to have it from our core edition, so it all works fine.

i.e. we can just drop the rust/Makefile side change for the fix, which also means a smaller backport (if we don't use the prerrequisite).

This makes sense to me. I can send v2 if there's consensus on this approach.

Quoted 6 lines

Another option is dropping these, if there are no practical consequences (did you notice anything very broken using rust-analyzer? It would be nice to mention those in the commit message, if you saw any), but it seems best to fix it, especially since it comes from the sysroot and thus I imagine it could have an impact later on due to change in upstream code.

I didn't notice anything specifically broken, but I did confirm that things break generally if RA is configured with the wrong edition. This prevents it from indexing sysroot crates (I tested locally with edition 2015 and observed broken navigation in macros).

In other words it isn't known to be broken today, but a new Rust release could break it tomorrow.

Quoted 3 lines

Cheers,
Miguel

#9 lore raw
Quoted 2 lines · Tamir Duberstein

This makes sense to me. I can send v2 if there's consensus on this approach.

Sounds good, thanks!

I guess I could also do it on apply since, unless I missed something, it just requires dropping all the rust/Makefile changes, but a v2 is always good for clarity.

Quoted 7 lines

I didn't notice anything specifically broken, but I did confirm that things break generally if RA is configured with the wrong edition. This prevents it from indexing sysroot crates (I tested locally with edition 2015 and observed broken navigation in macros).

In other words it isn't known to be broken today, but a new Rust release could break it tomorrow.

Yeah, agreed.

Cheers,
Miguel