rust-lang / rust-lang/rust

Rustdoc links can force constructors to run

Open
#158,078 4 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.