-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: introduce crate_attrs field in rust-project.json
#21282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I deliberately excluded lint attribute support in this PR, since it would involve a change to the public API. It might deserve a dedicated PR for discussion, since I'm not yet sure it's worth pursuing. Please let me know if anyone thinks this is the better place for it. |
|
We don't hesitate too much before breaking our public API (it's explicitly not stable), especially an API like that that is basically directly intended at its single consumer. |
I see, thanks! Then I'll add it here. |
ChayimFriedman2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some comments.
ChayimFriedman2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one nit, then LGTM.
4fd0e6e to
5b01d68
Compare
Since the commit 5038446 ("Rewrite method resolution to follow rustc more closely"), the method resolution logic has changed: rust-analyzer only looks up inherent methods for primitive types in sysroot crates. Unfortunately, this change broke at least one project that relies on `rust-project.json`: Rust-for-Linux. Its auto-generated `rust-project.json` directly embeds `core`, `alloc`, and `std` in the `crates` list without defining `sysroot_src`. Consequently, rust-analyzer fails to identify them as sysroot crates, breaking IDE support for primitive methods (e.g., `0_i32.rotate_left(0)`). However, specifying `sysroot_src` creates a new issue: it implicitly adds `std` as a dependency to all kernel module crates, which are actually compiled with `-Zcrate-attr=no_std`. Since rust-analyzer cannot see compiler flags passed outside of the project definition, we need a method to explicitly specify `#![no_std]` or, more generally, crate-level attributes through the project configuration. To resolve this, extend the `rust-project.json` format with a new `crate_attrs` field. This allows users to specify crate-level attributes such as `#![no_std]` directly into the configuration, enabling rust-analyzer to respect them when analyzing crates. References: - The original Zulip discussion: https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/Primitive.20type.20inherent.20method.20lookup.20fails/with/562983853
|
Sorry for the noise 😂 I've only changed the tests you mentioned. |
ChayimFriedman2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Since the commit 5038446
("Rewrite method resolution to follow rustc more closely"), the method resolution logic has changed: rust-analyzer only looks up inherent methods for primitive types in sysroot crates.
Unfortunately, this change broke at least one project that relies on
rust-project.json: Rust-for-Linux. Its auto-generatedrust-project.jsondirectly embedscore,alloc, andstdin thecrateslist without definingsysroot_src. Consequently, rust-analyzer fails to identify them as sysroot crates, breaking IDE support for primitive methods (e.g.,0_i32.rotate_left(0)).However, specifying
sysroot_srccreates a new issue: it implicitly addsstdas a dependency to all kernel module crates, which are actually compiled with-Zcrate-attr=no_std. Since rust-analyzer cannot see compiler flags passed outside of the project definition, we need a method to explicitly specify#![no_std]or, more generally, crate-level attributes through the project configuration.To resolve this, extend the
rust-project.jsonformat with a newcrate_attrsfield. This allows users to specify crate-level attributes such as#![no_std]directly into the configuration, enabling rust-analyzer to respect them when analyzing crates.References: