Files
rust/compiler/rustc_middle
bors d527bc9bfa Auto merge of #159344 - Urgau:canonical-symbols, r=mejrs
Replace weak only lang items with a custom attribute



When I added the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints in rust-lang/rust#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](https://github.com/rust-lang/rust/pull/158522#issuecomment-4856902233) 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 rust-lang/rust#155521 and rust-lang/rust#158522
2026-07-20 16:05:44 +00:00
..
…

For more information about how rustc works, see the rustc dev guide.