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

View all comments

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 2 times, most recently 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

This comment has been minimized.

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

This comment has been minimized.

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

rust-bors Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 4c682e3 (4c682e32f0cd928dd72d1e25402074a8cf953a8f)
Base parent: b26c8ef (b26c8ef0535b2de24a0af4048370caf449eebabd)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (4c682e3): 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.2% [0.1%, 0.6%] 12
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 1.2%)

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)
5.6% [2.4%, 9.5%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.3% [-5.3%, -2.1%] 4
All ❌✅ (primary) - - 0

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
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 10
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 4
All ❌✅ (primary) -0.0% [-0.0%, -0.0%] 10

Bootstrap: 492.122s -> 488.922s (-0.65%)
Artifact size: 389.50 MiB -> 389.47 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 18, 2026
@Urgau
Urgau force-pushed the canonical-symbols branch from 0a4dcfc to deac9ea Compare July 19, 2026 15:49
@rustbot

rustbot commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Urgau

Urgau commented Jul 19, 2026

Copy link
Copy Markdown
Member Author

@bors r=mejrs

@rust-bors

rust-bors Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit deac9ea has been approved by mejrs

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 19, 2026
@rust-bors

This comment has been minimized.

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



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](#158522 (comment)) 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
@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 19, 2026
@rust-bors

rust-bors Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 1003648 failed: CI. Failed job:

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors retry

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 19, 2026
@rust-bors

This comment has been minimized.

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



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](#158522 (comment)) 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
@JonathanBrouwer

JonathanBrouwer commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

@bors yield

@rust-bors

rust-bors Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #159579.

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



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](#158522 (comment)) 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
@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

⌛ Testing commit deac9ea with merge adce1b0...

1 similar comment
@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

⌛ Testing commit deac9ea with merge adce1b0...

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job x86_64-mingw-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Bootstrap failed while executing `test --stage 2 --skip=compiler --skip=src`
Currently active steps:
test::Crate { build_compiler: Compiler { stage: 2, host: x86_64-pc-windows-gnu, forced_compiler: false }, target: x86_64-pc-windows-gnu, mode: Std, crates: ["alloc", "alloctests", "compiler_builtins", "core", "coretests", "panic_abort", "panic_unwind", "proc_macro", "rustc-std-workspace-core", "std", "std_detect", "sysroot", "test", "unwind"] } at src\bootstrap\src\core\build_steps\test.rs:3361
Build completed unsuccessfully in 2:11:18
make: *** [Makefile:126: ci-mingw-x] Error 1
  local time: Mon Jul 20 03:03:09 CUT 2026
  network time: Mon, 20 Jul 2026 03:03:14 GMT
##[error]Process completed with exit code 2.
##[group]Run echo "disk usage:"
echo "disk usage:"

@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 20, 2026
@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

💔 Test for adce1b0 failed: CI. Failed job:

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-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.

7 participants