Skip to content

Replace weak only lang items with a custom attribute#159344

Merged
rust-bors[bot] merged 6 commits into
rust-lang:mainfrom
Urgau:canonical-symbols
Jul 20, 2026
Merged

Replace weak only lang items with a custom attribute#159344
rust-bors[bot] merged 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
@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:

@Urgau

Urgau commented Jul 20, 2026

Copy link
Copy Markdown
Member Author

thread 'main' (2856) panicked at library\alloctests\tests\c_str_alloc_error.rs:55:30:
alloc error

@bors retry alloc error

@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 20, 2026
@rust-bors

This comment has been minimized.

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 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 2f6963a failed: CI. Failed job:

@Urgau

Urgau commented Jul 20, 2026

Copy link
Copy Markdown
Member Author

@bors retry no logs

@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 20, 2026
@JonathanBrouwer

JonathanBrouwer commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

@bors p=6
Scheduling

@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. 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 successful - CI
Approved by: mejrs
Duration: 3h 12m 56s
Pushing d527bc9 to main...

@rust-bors
rust-bors Bot merged commit d527bc9 into rust-lang:main Jul 20, 2026
14 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 9e71b3b (parent) -> d527bc9 (this PR)

Test differences

Show 198 test diffs

Stage 1

  • [ui (polonius)] tests/ui/tool-attributes/duplicate-canonical-symbols.rs: [missing] -> pass (J0)
  • [ui] tests/ui/tool-attributes/duplicate-canonical-symbols.rs: [missing] -> pass (J2)

Stage 2

  • [ui] tests/ui/tool-attributes/duplicate-canonical-symbols.rs: [missing] -> pass (J1)

Additionally, 195 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard d527bc9bfa297ca7fd7f5ae93781eeec42073170 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. x86_64-mingw-2: 3h 19m -> 1h 44m (-47.4%)
  2. x86_64-gnu-aux: 2h 1m -> 2h 48m (+38.8%)
  3. dist-armv7-linux: 1h 9m -> 1h 29m (+29.6%)
  4. arm-android: 1h 21m -> 1h 44m (+27.9%)
  5. dist-i686-linux: 1h 22m -> 1h 40m (+21.8%)
  6. dist-aarch64-msvc: 2h 2m -> 1h 36m (-21.5%)
  7. dist-arm-linux-musl: 1h 39m -> 1h 19m (-20.1%)
  8. dist-powerpc64-linux-musl: 1h 33m -> 1h 14m (-19.9%)
  9. dist-apple-various: 1h 39m -> 1h 56m (+17.9%)
  10. dist-riscv64-linux: 1h 25m -> 1h 10m (-17.3%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (d527bc9): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -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%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 0.8%)

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.3% [1.7%, 9.3%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-4.8%, -2.5%] 5
All ❌✅ (primary) - - 0

Cycles

Results (primary 8.5%, secondary 3.1%)

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

mean range count
Regressions ❌
(primary)
8.5% [8.5%, 8.5%] 1
Regressions ❌
(secondary)
3.1% [3.1%, 3.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 8.5% [8.5%, 8.5%] 1

Binary size

Results (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
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Bootstrap: 486.168s -> 489.235s (0.63%)
Artifact size: 392.47 MiB -> 391.81 MiB (-0.17%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) merged-by-bors This PR was explicitly merged by bors. 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