Skip to content

Replace weak only lang items with a custom attribute#159344

Open
Urgau wants to merge 6 commits into
rust-lang:mainfrom
Urgau:canonical-symbols
Open

Replace weak only lang items with a custom attribute#159344
Urgau wants to merge 6 commits into
rust-lang:mainfrom
Urgau:canonical-symbols

Conversation

@Urgau

@Urgau Urgau commented Jul 15, 2026

Copy link
Copy Markdown
Member

When I added the invalid_runtime_symbol_definitions and suspicious_runtime_symbol_definitions lints in #155521 I added the concept of "weak only" lang item.

It's a LangItem that has no implementation, this is useful because the core symbols where not directly called, they were inserted by the compiler.

This mechanism work well, but T-lang approved the extension to "all extern functions referenced by the standard library", which makes the lang item approach impractical as we needs to update 5 files just to declare them. I'm not sure we can ask library contributor to do the dance for each extern "C" functions.

But the most problematic thing is that many extern functions in std come from libc not std, and adding lang items there is just no feasible I think.

Instead this PR proposed that we introduce a proper mechanism for them by adding a attribute #[rustc_canonical_symbol = "..."], which is encoded and decoded in rmeta like lang items and diagnostics items.

This simplifies the declaration as we only need to put the attribute to be effective #[rustc_canonical_symbol = "open"]. It also opens up many possibilities (none implemented here) like putting them on use statements (so we don't need to modify libc) or having the attribute be placed on a module. The table may also be useful on it's own if we want someday to do it for all crates, not just the standard library.

Follow up to #155521 and #158522

@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Jul 15, 2026
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 15, 2026
@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@rust-log-analyzer

This comment has been minimized.

@Urgau
Urgau force-pushed the canonical-symbols branch from f204a55 to 475f1bc Compare July 15, 2026 16:49
@Urgau
Urgau force-pushed the canonical-symbols branch from 475f1bc to 89cc3a5 Compare July 15, 2026 17:48
@mejrs

mejrs commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

What is the reason for the name field, can't this be a word attribute like #[rustc_canonical_symbol]? Only the tests have a case where the name is different from the item.

@Urgau

Urgau commented Jul 17, 2026

Copy link
Copy Markdown
Member Author

What is the reason for the name field

Calling the symbol_name query can be quite expensive, in particular when done repeatedly, but you're right that the name field could probably just be deduced (and maybe should).

Let's do a perf run without it.
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 17, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 17, 2026
Replace weak only lang items with a custom attribute
@rust-bors

rust-bors Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 9809522 (9809522494587bd84b1cae60f99fb7080a2a2ec1)
Base parent: 656ccbe (656ccbe796ff98def9b555c118e1620c5389e3b2)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (9809522): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.1%, 0.6%] 10
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.4%, -0.4%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -4.3%, secondary 0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.3% [1.1%, 6.5%] 7
Improvements ✅
(primary)
-4.3% [-6.2%, -2.5%] 3
Improvements ✅
(secondary)
-4.2% [-6.0%, -2.0%] 7
All ❌✅ (primary) -4.3% [-6.2%, -2.5%] 3

Cycles

This perf run didn't have relevant results for this metric.

Binary size

Results (primary 0.0%, secondary 0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 27
Regressions ❌
(secondary)
0.0% [0.0%, 0.1%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.0%] 27

Bootstrap: 490.561s -> 491.765s (0.25%)
Artifact size: 390.05 MiB -> 389.50 MiB (-0.14%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 17, 2026
@Urgau

Urgau commented Jul 18, 2026

Copy link
Copy Markdown
Member Author

I've switch to deducing the symbol name in the latest commit. Let's see if it has a perf impact.

@bors try @rust-timer queue

@rust-timer

Copy link
Copy Markdown
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 18, 2026
@rust-bors

rust-bors Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

⌛ Trying commit 0a4dcfc with merge 4c682e3

To cancel the try build, run the command @bors try cancel.

Workflow: https://github.com/rust-lang/rust/actions/runs/29644571721

rust-bors Bot pushed a commit that referenced this pull request Jul 18, 2026
Replace weak only lang items with a custom attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-perf Status: Waiting on a perf run to be completed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants