Skip to content

Commit

Permalink
Rollup merge of rust-lang#135561 - Zalathar:link-dead-code, r=saethlin
Browse files Browse the repository at this point in the history
Update docs for `-Clink-dead-code` to discourage its use

The `-Clink-dead-code` flag was originally added way back in rust-lang#31368, apparently to help improve the output of some older forms of code coverage measurement, and also to address some use-cases for wanting to suppress linker flags like `-dead_strip` and `--gc-section`.

In the past it might have also been useful in conjunction with `-Cinstrument-coverage`, but subsequent improvements to coverage instrumentation have made it unnecessary there.

[It is also currently used by cargo-fuzz by default](rust-fuzz/cargo-fuzz#391), for reasons that are possibly no longer relevant.

---

The flag currently does more than its name suggests, affecting not just linker flags, but also monomorphization decisions. It has also contributed to ICEs (e.g. rust-lang#135515) that would not have occurred without link-dead-code.

---

For now, this PR just updates the documentation to be more realistic about what the flag does, and when it should be used (approximately never). In the future, it might be worth looking into properly deprecating this flag, and perhaps making it a no-op if feasible.
  • Loading branch information
matthiaskrgr authored Jan 16, 2025
2 parents bbc6d16 + 2238b00 commit f7e1ae4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
7 changes: 0 additions & 7 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ pub enum LtoCli {
}

/// The different settings that the `-C instrument-coverage` flag can have.
///
/// Coverage instrumentation now supports combining `-C instrument-coverage`
/// with compiler and linker optimization (enabled with `-O` or `-C opt-level=1`
/// and higher). Nevertheless, there are many variables, depending on options
/// selected, code structure, and enabled attributes. If errors are encountered,
/// either while compiling or when generating `llvm-cov show` reports, consider
/// lowering the optimization level, or including/excluding `-C link-dead-code`.
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub enum InstrumentCoverage {
/// `-C instrument-coverage=no` (or `off`, `false` etc.)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ options! {
"extra arguments to append to the linker invocation (space separated)"),
#[rustc_lint_opt_deny_field_access("use `Session::link_dead_code` instead of this field")]
link_dead_code: Option<bool> = (None, parse_opt_bool, [TRACKED],
"keep dead code at link time (useful for code coverage) (default: no)"),
"try to generate and link dead code (default: no)"),
link_self_contained: LinkSelfContained = (LinkSelfContained::default(), parse_link_self_contained, [UNTRACKED],
"control whether to link Rust provided C objects/libraries or rely \
on a C toolchain or linker installed in the system"),
Expand Down
10 changes: 5 additions & 5 deletions src/doc/rustc/src/codegen-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ options should be separated by spaces.

## link-dead-code

This flag controls whether the linker will keep dead code. It takes one of
the following values:
Tries to generate and link dead code that would otherwise not be generated or
linked. It takes one of the following values:

* `y`, `yes`, `on`, `true` or no value: keep dead code.
* `y`, `yes`, `on`, `true` or no value: try to keep dead code.
* `n`, `no`, `off` or `false`: remove dead code (the default).

An example of when this flag might be useful is when trying to construct code coverage
metrics.
This flag was historically used to help improve some older forms of code
coverage measurement. Its use is not recommended.

## link-self-contained

Expand Down

0 comments on commit f7e1ae4

Please sign in to comment.