Skip to content

Conversation

@J3m3
Copy link
Contributor

@J3m3 J3m3 commented Dec 16, 2025

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:

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 16, 2025
@J3m3
Copy link
Contributor Author

J3m3 commented Dec 16, 2025

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.

@ChayimFriedman2
Copy link
Contributor

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.

@J3m3
Copy link
Contributor Author

J3m3 commented Dec 16, 2025

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.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a 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.

@J3m3 J3m3 force-pushed the crate-attrs branch 2 times, most recently from 4fd0e6e to 5b01d68 Compare December 18, 2025 01:18
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
@J3m3
Copy link
Contributor Author

J3m3 commented Dec 18, 2025

Sorry for the noise 😂 I've only changed the tests you mentioned.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Dec 18, 2025
Merged via the queue into rust-lang:master with commit ea1d299 Dec 18, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 18, 2025
@J3m3 J3m3 deleted the crate-attrs branch December 18, 2025 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants