Skip to content

The link attribute is not scoped to the extern block it is written on - #2390

Open
isHeSatoshi wants to merge 1 commit into
rust-lang:masterfrom
isHeSatoshi:link-attribute-not-per-block
Open

isHeSatoshi wants to merge 1 commit into
rust-lang:masterfrom
isHeSatoshi:link-attribute-not-per-block

Conversation

@isHeSatoshi

@isHeSatoshi isHeSatoshi commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #1986.

The rule said the link attribute specifies a library "to link with for the items within an extern block", which suggests the library is bound to the items of that block.

It is not. I checked by asking rustc for the link line it would use, using a bin crate so that linking actually happens:

crate foo on the link line?
#[link(name = "foo")] unsafe extern "C" {} + fn main() {} yes ("foo.lib")
fn main() {} no
#[link(name = "foo")] unsafe extern "C" { fn bar(); } + fn main() {} yes

The first row is the interesting one: the extern block is empty, so there are no items for the library to be bound to, and the library is passed to the linker anyway.

The page already implies this. r[items.extern.attributes.link.empty-block], a few rules further down, says an empty block can be used "to satisfy the linking requirements of extern blocks elsewhere in your code (including upstream crates)", which does not fit "for the items within an extern block".

So the change states what the attribute does and points at that rule, so that the rule follows from the introduction rather than contradicting it.

cargo run --package=style-check -- src passes. No new links beyond a rule reference that mdbook-spec resolves automatically.

The rule said the `link` attribute names a library "to link with for the
items within an extern block", which suggests the library is bound to
those items. It is not: the library is added to the link, so it can
resolve symbols declared in any extern block, and that is why the rule
further down already says a `link` attribute on an empty extern block can
"satisfy the linking requirements of extern blocks elsewhere in your
code".

Say what the attribute actually does, and point at that rule so it
follows from the introduction instead of contradicting it.
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Oct 19, 2026

This branch has not been deployed

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

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect information about link attribute on extern blocks

2 participants