Staticlib rename internal symbols
Follow-up to rust-lang/rust#155338.
`-Zstaticlib-rename-internal-symbols` appends a crate-specific suffix (`_rs{StableCrateId}`) to non-exported symbols, resolving duplicate symbol conflicts when linking multiple Rust staticlibs into the same binary.
The implementation collects all defined `GLOBAL/WEAK` symbol names not in the exported set across all .o files, then renames them by extending the strtab and patching symbol name offsets. When combined with `-Zstaticlib-hide-internal-symbols`, the renamed symbols also receive `STV_HIDDEN` visibility.
Supported on ELF targets (Linux, BSD, etc.) and Apple targets (macOS, iOS, etc.). On unsupported targets (Windows), a warning is emitted and the flag has no effect.
r?@bjorn3 @petrochenkov
Don't track cwd for `-Zremap-cwd-prefix` in incremental compilation
## Summary
`-Zremap-cwd-prefix=<to>` defeats incremental compilation whenever the build directory changes (Bazel sandboxes, CI checkouts with per-build paths), even though the remapped output is identical:
```
[incremental] completely ignoring cache because of differing commandline arguments
```
This fixes the cwd half of rust-lang/rust#132132.
### Root cause
`-Zremap-cwd-prefix` and `--remap-path-prefix` have two separate options (`remap_cwd_prefix` and `remap_path_prefix`), but `parse_remap_path_prefix` *merges* them: it resolves `std::env::current_dir()` and pushes `(cwd, to)` onto `remap_path_prefix`, which is `[TRACKED_NO_CRATE_HASH]`. The **absolute** cwd thus enters `dep_tracking_hash(false)` (compared in `rustc_incremental::persist::load`), so the same build from a different directory purges the whole incremental session.
### Fix: apply the cwd prefix where the mapping is built, not where options are tracked
Keep each option holding exactly its own flag, and apply `-Zremap-cwd-prefix` in `file_path_mapping` — i.e. while building the (untracked) applied `FilePathMapping` — instead of in `parse_remap_path_prefix`. The absolute cwd then lives only in the runtime mapping and never in a tracked option.
**Behaviour-preserving for output:** the applied mapping is identical (the cwd entry is still appended last, so it keeps taking precedence over `--remap-path-prefix` as documented), and all emitted paths (debuginfo, diagnostics, metadata) are unchanged.
**Sound for tracking:** the cwd's effect is still tracked, by the two options that *should* carry it —
* `remap_cwd_prefix` (`[TRACKED]`) — the target prefix baked into output;
* `working_dir` (`[TRACKED]`) — and `RealFileName`'s hash already includes the real local path **only** when the path was not fully remapped (`was_fully_remapped()` = `scopes.is_all()`):
```rust
if !self.was_fully_remapped() { self.local.hash(state); }
self.maybe_remapped.hash(state);
```
So under a restrictive `-Zremap-path-scope` (where the cwd can still leak into output) the real cwd stays tracked via `working_dir`; under full remapping (where it cannot leak) it is correctly ignored. Dropping the cwd from `remap_path_prefix`'s hash is therefore sound in **every** scope.
The **stable** `--remap-path-prefix` flag is entirely unchanged in data and in tracking. No SVH/crate-hash change (`remap_path_prefix` is `TRACKED_NO_CRATE_HASH`); no new types and no special-case hashing.
### Notes
* A unit test (`tests/ui`-style, in `rustc_interface`) guards against the cwd being merged back into the tracked `remap_path_prefix`, while asserting it is still applied via `file_path_mapping`.
* The unstable book entry for `-Zremap-cwd-prefix` is updated to document the incremental-cache behaviour across build directories.
* A `tests/run-make` end-to-end test (build the same sources from two directories sharing one `-Cincremental` dir, asserting cache reuse) would be a good follow-up; happy to add it here if preferred.
### Relation to stabilization (tracking issue rust-lang/rust#89434)
This is a behaviour fix to an unstable flag and does not depend on or block stabilization. It is, however, mildly relevant to one of the open questions in the tracking issue (rust-lang/rust#89434) — *"Will this always be defined to be the same as `--remap-path-prefix=$(pwd)=VALUE`?"*. After this PR the two are **no longer** equivalent for incremental compilation: a literal `--remap-path-prefix=$(pwd)=.` still bakes the absolute cwd into the tracked hash, whereas `-Zremap-cwd-prefix=.` no longer does. Worth keeping in mind whenever that question is revisited (e.g. alongside RFC 3127).
r? compiler
Emit error for unused target expression in glob and list delegations
This PR emits error if unuse target expression is specified for glob delegation. Second part of rust-lang/rust#156798.
Part of rust-lang/rust#118212.
r? @petrochenkov
This is an UnordMap internally. Iteration order for the work product map
should not matter aside from the place it is serialized where sorting by
WorkProductId is sufficient.
delegation: split resolution and lowering
This PR splits delegation's AST -> HIR lowering and its resolution. Now we resolve all delegations and then lower them. This should have benefits:
- ~For rust-lang/rust#156798, where it will be convenient to insert diagnostics about specifying target expressions for glob reuses of only static functions (the `delegations_resolutions` map will contain information whether to lower or delete target expression)~
- For rust-lang/rust#155337 and delegations to inherent methods resolution, as from what I tested up until now we should resolve them in iterative manner before AST -> HIR lowering.
Part of rust-lang/rust#118212.
r? @petrochenkov
Rollup of 3 pull requests
Successful merges:
- rust-lang/rust#156210 (Emit retags in codegen to support BorrowSanitizer (part 2))
- rust-lang/rust#157317 (Fix ICE when wrong intra-doc link on type alias)
- rust-lang/rust#157391 (Reorganize `tests/ui/issues` [4/N])
Emit retags in codegen to support BorrowSanitizer (part 2)
Tracking issue: rust-lang/rust#154760
[Zulip Thread](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Staging.20for.20emitting.20retags.20in.20codegen/with/593004012)
This is one of several PRs that will add experimental support for emitting retags as function calls in codegen. Each PR will be a minimal, improved slice of the changes in rust-lang/rust#155965.
This PR adds a new unstable flag `-Zcodegen-emit-retag`, which will enable experimental retag calls in generated code. This flag is a nop for now, but the relevant methods have been added to codegen_ssa, and they are called wherever retags are necessary. Subsequent PRs will complete this implementation.
This does not depend on rust-lang/rust#156208.
r? @RalfJung
Introduce `TypingMode::Codegen` to avoid layout cycles on coroutines
Computing layout of coroutines depends on their `optimized_mir`. At the same time, MIR opts can require using layouts to work. For instance to evaluate constants. This leads to cycles and clumsy workarounds.
This PR creates a new typing mode for layout computations:
- when a coroutine's layout is requested with `TypingMode::PostAnalysis` or earlier, return `LayourError::TooGeneric`;
- when a coroutine's layout is requested with `TypingMode::Codegen`, actually compute it.
`TypingMode::Codegen` is meant be be used by codegen code, and analyses that require coroutine layout, like transmute check and coroutine recursion check.
With this PR, we can remove all `is_coroutine` checks from `rustc_mir_transform` and unlock simplifying coroutine MIR.
Perf is not terrific. This PR causes recomputation of a few queries, and I had to insert workarounds.
`-Zremap-cwd-prefix=<to>` was applied inside `parse_remap_path_prefix`, which
resolved `std::env::current_dir()` and pushed `(cwd, to)` onto
`remap_path_prefix`. That option is `[TRACKED_NO_CRATE_HASH]`, so the absolute
working directory entered the incremental command-line-args hash, and building
the same sources from a different directory (e.g. a Bazel sandbox, whose path
contains a per-action counter) purged the whole incremental cache even though the
remapped output is identical.
`--remap-path-prefix` and `-Zremap-cwd-prefix` already have separate options
(`remap_path_prefix` and `remap_cwd_prefix`). Keep each option holding exactly its
own flag, and apply `-Zremap-cwd-prefix` in `file_path_mapping` -- i.e. when
building the (untracked) applied `FilePathMapping` -- rather than when
parsing/tracking options. The absolute cwd then lives only in the runtime mapping,
never in a tracked field.
Output is unchanged (the cwd entry is still appended last). Tracking stays sound:
the cwd's effect is tracked via `remap_cwd_prefix` (the target prefix baked into
output) and `working_dir` (whose `RealFileName` hash includes the real path only
when it was not fully remapped, i.e. exactly when the cwd can leak into output).
The stable `--remap-path-prefix` flag is unchanged in data and in tracking.
A unit test guards against the cwd being merged back into `remap_path_prefix`.
This was necessary when transitioning from JS to wasm exception handling
on Emscripten. Enough time has probably passed that we no longer need to
support JS exception handling on Emscripten. This enables cleaning up a
fair bit of code.
MIR inlining: allow backends to opt-in to inlining intrinsics
This is the same as https://github.com/rust-lang/rust/pull/156398. Github stopped processing updates on that other branch.
r? @oli-obk
Merge several HIR-level queries into one
Now four queries (`local_def_id_to_hir_id`, `opt_hir_owner_nodes`, `opt_ast_lowering_delayed_lints`, `in_scope_traits_map`) were replaced with regular methods which acts like getters.
An `hir_owner` query was added that returns a `ProjectedMaybeOwner` that contains all those fields. `hir_attr_map` remains a separate query as adding attributes to `ProjectedMaybeOwner` led to [perf regressions](https://github.com/rust-lang/rust/pull/155678#issuecomment-4304597871).
There is a similar issue with `in_scopes_trait_map`, but according to the comments from https://github.com/rust-lang/rustc-perf/pull/2436 it is a rare case.
Most of the changes in incremental tests are renames from `opt_hir_owner_nodes` -> `hir_owner`, but there are few cases when new dirty queries were added.
r? @petrochenkov
r? @oli-obk
turn some long-deprecated -C options into errors
- `-Car` has been documented to do nothing for more than 8 years (691ab6c5bf) and causes a warning for more than a year (https://github.com/rust-lang/rust/pull/135126).
- `-Cno-stack-check` has been made a NOP and deprecated with a warning more than 9 years ago (c670293630)
- `-Cinline-threshold` has been made a NOP and deprecated with a warning almost 2 years ago (https://github.com/rust-lang/rust/pull/124712).
With them being ignored there's always a risk someone thinks they'll do something and they don't notice the warning. I think these have been deprecated for long enough that we can turn them into hard errors.
Also change the type of these fields to `()` so there's no information here that the rest of the compiler could use. This supersedes the `rustc_lint_opt_deny_field_access` trick.
MCP: https://github.com/rust-lang/compiler-team/issues/978
Add `sync` option to `-Z threads` to force synchronization on one thread
This adds a `sync` option to `-Z threads` to force synchronization while still using one thread. This is useful to measure overhead of synchronization without the noise of additional threads.
Clean up `TyCtxt::needs_crate_hash` usage and rename it to `needs_hir_hash`.
While reviewing `crate_hash` query usage for https://github.com/rust-lang/rust/pull/155871, the `needs_crate_hash` function turned out to be the cause of unnecessary calls to the query. The `needs_crate_hash` name is easy to mistake for the functionality of "needs the crate_hash query". This PR removes the usage of `needs_crate_hash` where it was not appropriate and renames the function to `needs_hir_hash` which better reflects its functionality.
Remove unused spans from AttributeKind
Recently I noticed some spans in diagnostic attributes were never used. I went through and checked the other variants too.
Exactly as the comment on the `crate_hash` field of `Linker` says, that
field is only used when incremental compilation is enabled.
`needs_crate_hash` returns true in some other cases too.
Move `feature*` methods from `parse` mod to `errors` mod.
As the FIXME comment says, these no longer use `ParseSess` and so the `parse` mod is not a good place for them. The `errors` mod is a better home.
r? @TaKO8Ki
Catch unwinds from the global ctxt callback to complete queries profiling data in more cases
The driver/compiler interface provides multiple callbacks, and we sometimes catch unwinds to flush diagnostics, ensure ICEs appear, and so on even when fatal errors occur.
When these panics happen, we don't `finish` the `TyCtxt`, and there's a fixme about that. Unfortunately this is where we also allocate the self-profile strings: query events for example start as virtual and are turned into real strings by this finalization process; they are _invalid_ without this step. When fatal errors happen, the in-flight query name will be `<unknown>`, event counts will be inaccurate, etc.
This PR catches panics from another of these callbacks, where the `TyCtxt` is available, to allow for the self-profiling data to be computed before continuing the unwinding process as before. `finish` does more things, that I don't want to introduce here.
I remember seeing this discussed in GH issues in the past, but can't find any open ones now. It may also have been only mentioned while trying to profile existing slowness issues. I stumbled upon this again recently when looking into `tests/ui/try-trait/deep-try-chain-issue-153583.rs`, where the slowest query is `<unknown>`.
```
> rm *.mm_profdata ; rustc +nightly -Zself-profile tests/ui/try-trait/deep-try-chain-issue-153583.rs ; summarize summarize *.mm_profdata | head -n 5
error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> tests/ui/try-trait/deep-try-chain-issue-153583.rs:6:5
|
6 | 0?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
| ^^ the `?` operator cannot be applied to type `{integer}`
|
= help: the nightly-only, unstable trait `Try` is not implemented for `{integer}`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
+------------------------------------------------------+-----------+-----------------+----------+------------+
| Item | Self time | % of total time | Time | Item count |
+------------------------------------------------------+-----------+-----------------+----------+------------+
| <unknown> | 1.55s | 99.416 | 3.21s | 15230 |
+------------------------------------------------------+-----------+-----------------+----------+------------+
```
This PR fixes that case at least. In general, it should fix the invalid profiling records for fatal errors happening while a query is running.
```
+------------------------------------------------------+-----------+-----------------+----------+------------+
| Item | Self time | % of total time | Time | Item count |
+------------------------------------------------------+-----------+-----------------+----------+------------+
| typeck_root | 5.04s | 95.588 | 5.08s | 1 |
+------------------------------------------------------+-----------+-----------------+----------+------------+
```
r? @bjorn3
fatal errors currently abort the compiler process without allocating the self-profile
strings: query events aren't always correctly recorded, and will show up as <unknown>
in the data.
catching the unwinding panic allows us to finalize the self-profiling process correctly
before continuing unwinding as before.