Rustdoc links can force constructors to run
Open
Nobody has claimed this yet.
A-crates
A-intra-doc-links
A-metadata
A-resolve
C-bug
T-compiler
T-rustdoc
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
The following repro makes a documentation link from a unused dependency linked to the final binary ->
dep.rs
#[no_mangle]
pub static mut m: u8 = 0;
#[no_mangle]
extern "C" fn mark_ctor() {
unsafe {
m = 1;
}
eprintln!("shouldn't be called");
}
#[used]
#[link_section = ".init_array"]
static INIT: extern "C" fn() = mark_ctor;
middle.rs with the doc link
//! [`dep`]
pub fn value() -> u8 {
7
}
app.rs
fn main() {
println!("{}", middle::value());
}
$ rustc --crate-type=rlib dep.rs && rustc --crate-type=rlib middle.rs --extern dep -L. && rustc app.rs --extern middle -L. && ./app
shouldn't be called
7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the behavior with dep.rs, middle.rs, and app.rs using the rustc commands shown. Then trace how the rustdoc link in middle.rs affects dependency inclusion and linking. Done means the documentation link still resolves while the unused dependency's constructor is not linked into or run by app.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, documentation
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100