328 Commits

Author SHA1 Message Date
Stuart Cook 63712e0f9d Rollup merge of #154529 - Zalathar:setup-dep-graph, r=cjgillot
Make `setup_dep_graph` incremental-only and more straightforward

The existing code contains some very strange control flow that can put the dep graph into an inconsistent and untested state if streaming output setup fails.

(Specifically, that failure would put `DepGraph` into an "empty" state intended for non-incremental compilation, but other parts of the compiler would still think that incremental mode is enabled due to `sess.opts.incremental`.)

This PR therefore performs a big overhaul of `setup_dep_graph` by:

- Returning immediately in non-incremental mode.
- Exiting immediately if dep-graph streaming output couldn't be set up.
- Inlining some "helper" functions that were more confusing than helpful.
2026-04-05 13:38:16 +10:00
bors a25435bcf7 Auto merge of #154304 - zetanumbers:typeck_root, r=petrochenkov
Make typeck a tcx method which calls typeck_root query



Currently typeck query itself calls `tcx.typeck(tcx.typeck_root_def_id(key))` if its key isn't a type-check root. I thought this might be an overhead and made typeck a tcx method which calls typeck_root query instead.

This is a step to simplify `cache_on_disk_if` query modifier.

@petrochenkov please run perf
2026-03-29 19:37:29 +00:00
Zalathar 369c801eaf Clean up enum LoadResult
- Make the enum non-public
- Replace the generic `<T>` with concrete fields
- Rename variant `LoadDepGraph` to `IoError`
2026-03-29 15:00:01 +11:00
Zalathar 6e6701e2bd Make setup_dep_graph incremental-only and more straightforward
The existing code contains some very strange control flow that can put the dep
graph into an inconsistent and untested state if streaming output setup fails.

(Specifically, that failure would put `DepGraph` into an "empty" state intended
for non-incremental compilation, but other parts of the compiler would still
think that incremental mode is enabled due to `sess.opts.incremental`.)

This commit therefore performs a big overhaul of `setup_dep_graph` by:

- Returning immediately in non-incremental mode.
- Exiting immediately if dep-graph streaming output couldn't be set up.
- Inlining some "helper" functions that were more confusing than helpful.
2026-03-29 14:59:51 +11:00
Zalathar a63d476105 Move build_dep_graph into rustc_incremental::persist::load
Moving this code here makes the subsequent cleanup easier.
2026-03-29 14:37:44 +11:00
Zalathar 10d29ee606 Extract -Zassert-incr-state into a separate helper function
This code is only needed by an obscure `-Z` flag, so pulling it out of the main
path makes the normal logic easier to follow.
2026-03-28 21:52:55 +11:00
Zalathar 29be0590f4 Use the normal arg-parsing machinery for -Zassert-incr-state 2026-03-28 21:52:55 +11:00
Daria Sukhonina 94ed03acb5 Make typeck a tcx method which calls typeck_root query 2026-03-28 12:38:44 +03:00
Stuart Cook ee64422ffa Rollup merge of #154110 - lambdageek:fix/incr-compile-note, r=wesleywiser
Change "error finalizing incremental compilation" text and emit it as a note, not a warning

As mentioned in https://github.com/rust-lang/rust/issues/151181#issuecomment-3762036971 and https://github.com/rust-lang/rust/issues/151181#issuecomment-4068842260 the current message could be improved:

1. Right now it displays as "warning: error ..." which is confusing (is it an error or a warning)
2. It doesn't give the user a clear indication of what the consequences are
3. The _current_ build is successful. The _next_ build might be slower

The new message is now

```text
note: did not finalize incremental compilation session directory ...
  |
  = help: the next build will not be able to reuse work from this compilation
```

I started a zulip thread [#t-compiler/incremental > Ergonomics of "error finalizing incremental session"](https://rust-lang.zulipchat.com/#narrow/channel/241847-t-compiler.2Fincremental/topic/Ergonomics.20of.20.22error.20finalizing.20incremental.20session.22/with/580191447)
2026-03-28 15:01:38 +11:00
Zalathar 505dabc2a0 Tiny improvements to incremental header reads/writes
The standard library has helper methods for converting integers to/from
little-endian bytes, for checked conversion from `usize` to `u8`, and for
treating `&mut T` as `&mut [T; 1]`.
2026-03-27 13:46:50 +11:00
Zalathar a7fa2dfd8e Clean up the API for opening/checking incremental-compilation files
Returning dedicated structs and enums makes the meaning of each return value
more obvious, and provides a more natural home for documentation.

The intermediate `load_data` function was unhelpful, and has been inlined into
the main function.
2026-03-27 13:46:50 +11:00
Zalathar afffa7d6e3 Don't store current-session side effects in OnDiskCache
Every other part of `OnDiskCache` deals with loading information from the
_previous_ session, except for this one field.

Moving it out to `QuerySystem` makes more sense, because that's also where
query return values are stored (inside the caches in their vtables).
2026-03-24 12:56:51 +11:00
Aleksey Kliger 4845f78422 Reword the incremental finalize diagnostic
Remove the confusing word "error".  The diagnostic is already prefixed
with a level when it is displayed, so this is redundant and possibly
confusing ("warning: error ...").

Add some help text summarizing the impact of what happened: the next
build won't be able to reuse work from the current run.
2026-03-17 16:46:05 -04:00
Aleksey Kliger 384f363b70 change "error finalizing incremental compilation" from warning to note
The warning has no error code, so in a `-D warnings` environment, it's
impossible to ignore if it consistently breaks your build.  Change it
to a note so it is still visible, but doesn't break the build
2026-03-13 14:37:26 -04:00
Zalathar d7490855d5 Inline and simplify some code for saving incremental data to disk
Main changes:
- Inline `encode_query_cache` and `TyCtxt::serialize_query_result_cache`
- Pull value promotion out of `OnDiskCache::drop_serialized_data`
- Panic if `on_disk_cache` is None in an incremental-only path
2026-03-08 21:27:50 +11:00
Josh Stone eb093cfd5d Reformat with the new stage0 2026-03-07 10:42:02 -08:00
Jonathan Brouwer eb5f275181 Rollup merge of #152844 - Zalathar:retain-dep-graph, r=JonathanBrouwer
Rename `DepGraphQuery` to `RetainedDepGraph`

This is a revised subset of https://github.com/rust-lang/rust/pull/152836 that only performs an internal renaming, and does not touch the `-Zquery-dep-graph` flag.

The new name and comments for `RetainedDepGraph` should hopefully do a better job of communicating that it is not used in normal compiler operation, even in incremental mode.
2026-02-20 13:24:57 +01:00
Jonathan Brouwer c0fe6943ae Rollup merge of #152759 - jdonszelmann:simpler-get-attrs, r=jonathanbrouwer
Simpler `find_attr!()`

r? @JonathanBrouwer
cc: @jyn514
2026-02-20 13:24:55 +01:00
Zalathar 19e0c62e10 Rename DepGraphQuery to RetainedDepGraph 2026-02-20 20:55:09 +11:00
Jana Dönszelmann decec173ec remove AttributeKind everywhere 2026-02-20 09:50:16 +01:00
Jana Dönszelmann 63edc913fa change all uses 2026-02-20 09:50:16 +01:00
Nicholas Nethercote c4a69d4712 Bring back enum DepKind.
It was removed in #115920 to enable it being moved to
`rustc_query_system`, a move that has recently been reversed. It's much
simpler as an enum.

Also:
- Remove the overly complicated `Debug` impl for `DepKind`.
- Remove the trivial `DepKind` associated constants (`NULL` et al.)
- Add an assertion to ensure that the number of `DepKinds` fits within a
  `u16`.
- Rename `DEP_KIND_VARIANTS` as `DEP_KIND_NUM_VARIANTS`, to make it
  clearer that it's a count, not a collection.
- Use `stringify!` in one place to make the code clearer.
2026-02-19 09:05:37 +11:00
Nicholas Nethercote 1d410c5831 Fix a typo. 2026-02-19 08:56:42 +11:00
Zalathar 9eaedddb7f Rename dep node "fingerprints" to distinguish key and value hashes 2026-02-18 17:20:32 +11:00
Nicholas Nethercote 1d83208683 De-genericize the dep graph.
By removing the generic `D` parameter from `DepGraph`, `DepGraphData`,
`CurrentDepGraph`, `SerializedDepGraph`, `SerializedNodeHeader`, and
`EncoderState`.
2026-02-15 13:07:37 +11:00
Nicholas Nethercote 32e6a1a0ab Remove QueryContext.
`rustc_query_system` has been reduced so much that it's no longer
needed. This avoids a lot of indirection and abstraction.
2026-02-15 13:07:35 +11:00
John Kåre Alsaker 8c5ce26e43 Replace parallel! macro with par_fns function and rename join to par_join 2026-02-12 12:20:18 +01:00
Jonathan Brouwer edd43c9e1f Fix existing messages in the diag structs 2026-02-07 09:11:34 +01:00
Jamie Hill-Daniel de7067938d Port rustc_if_this_changed/rustc_then_this_would_need to attr parser 2026-02-04 21:00:36 +00:00
Jamie Hill-Daniel 94a0ba50e1 Port rustc_clean to attribute parser
Also remove mentions of removed `rustc_dirty`
2026-02-04 20:58:02 +00:00
Jonathan Brouwer 4cacfc00bc Convert to inline diagnostics in rustc_incremental 2026-02-03 23:22:48 +01:00
Zalathar 154255698a Make Deps::name lookup a non-self associated function
The dep names needed here are statically available from `rustc_middle`.
2026-01-21 12:02:26 +11:00
Felix Rath 8fa2f693bb Implement incremental caching for derive macro expansions 2026-01-16 07:36:36 +01:00
bjorn3 973c7527b4 Unify the configuration of the compiler docs
Previously it was rather inconsistent which crates got the rust logo and
which didn't and setting html_root_url was forgotten in many cases.
2025-11-05 11:25:27 +00:00
bjorn3 3ce89e257d Use the actual StableCrateId for the incr comp session dir
Previously only --crate-type would be taken into account, not #![crate_type].
2025-10-27 15:56:09 +00:00
iximeow 4e816d8bc5 Do not GC the current active incremental session directory
In `setup_dep_graph`, we set up a session directory for the current
incremental compilation session, load the dep graph, and then GC stale
incremental compilation sessions for the crate. The freshly-created
session directory ends up in this list of potentially-GC'd directories
but in practice is not typically even considered for GC because the new
directory is neither finalized nor `is_old_enough_to_be_collected`.

Unfortunately, `is_old_enough_to_be_collected` is a simple time check,
and if `load_dep_graph` is slow enough it's possible for the
freshly-created session directory to be tens of seconds old already.
Then, old enough to be *eligible* to GC, we try to `flock::Lock` it as
proof it is not owned by anyone else, and so is a stale working
directory.

Because we hold the lock in the same process, the behavior of
`flock::Lock` is dependent on platform-specifics about file locking
APIs. `fcntl(F_SETLK)`-style locks used on non-Linux Unices do not
provide mutual exclusion internal to a process. `fcntl_locking(2)` on
Linux describes some relevant problems:

```
       The record locks described above are associated with the process
       (unlike the open file description locks described below).  This
       has some unfortunate consequences:

       *  If a process closes any file descriptor referring to a file,
          then all of the process's locks on that file are released, [...]

       *  The threads in a process share locks.  In other words, a
          multithreaded program can't use record locking to ensure that
          threads don't simultaneously access the same region of a file.
```

`fcntl`-locks will appear to succeed to lock the fresh incremental
compilation directory, at which point we can remove it just before using
it later for incremental compilation. Saving incremental compilation
state later fails and takes rustc with it with an error like
```
[..]/target/debug/incremental/crate-<hash>/<name>/dep-graph.part.bin: No such file or directory (os error 2)
```

The release-lock-on-close behavior has uncomfortable consequences for
the freshly-opened file description for the lock, but I think in
practice isn't an issue. If we would close the file, we failed to
acquire the lock, so someone else had the lock ad we're not releasing
locks prematurely.

`flock(LOCK_EX)` doesn't seem to have these same issues, and because
`flock::Lock::new` always opens a new file description when locking, I
don't think Linux can have this issue.

From reading `LockFileEx` on MSDN I *think* Windows has locking
semantics similar to `flock`, but I haven't tested there at all.

My conclusion is that there is no way to write a pure-POSIX
`flock::Lock::new` which guarantees mutual exclusion across different
file descriptions of the same file in the same process, and
`flock::Lock::new` must not be used for that purpose. So, instead, avoid
considering the current incremental session directory for GC in the
first place. Our own `sess` is evidence we're alive and using it.
2025-10-20 02:39:55 +00:00
Nicholas Nethercote 301655eafe Revert introduction of [workspace.dependencies].
This was done in #145740 and #145947. It is causing problems for people
using r-a on anything that uses the rustc-dev rustup package, e.g. Miri,
clippy.

This repository has lots of submodules and subtrees and various
different projects are carved out of pieces of it. It seems like
`[workspace.dependencies]` will just be more trouble than it's worth.
2025-09-02 19:12:54 +10:00
Nicholas Nethercote c50d2cc807 Add tracing to [workspace.dependencies]. 2025-08-27 14:21:19 +10:00
Nicholas Nethercote 777e2d6a2a Add thin-vec to newly added [workspace.dependencies]. 2025-08-27 13:59:32 +10:00
bors 3ef8e64ce9 Auto merge of #139758 - Zoxc:thread-local-graph, r=oli-obk
Use thread local dep graph encoding

This adds thread local encoding of dep graph nodes. Each thread has a `MemEncoder` that gets flushed to the global `FileEncoder` when it exceeds 64 kB. Each thread also has a local cache of dep indices. This means there can now be empty gaps in `SerializedDepGraph`.

Indices are marked green and also allocated by the new atomic operation `DepNodeColorMap::try_mark_green` as the encoder lock is removed.
2025-05-07 12:39:54 +00:00
Zalathar 9e7fb67838 Rename graph::implementation::Graph to LinkedGraph 2025-05-06 14:35:06 +10:00
John Kåre Alsaker d3ec14bbec Use thread local dep graph encoding 2025-05-01 10:20:31 +02:00
Nicholas Nethercote 2fef0a30ae Replace infallible name_or_empty methods with fallible name methods.
I'm removing empty identifiers everywhere, because in practice they
always mean "no identifier" rather than "empty identifier". (An empty
identifier is impossible.) It's better to use `Option` to mean "no
identifier" because you then can't forget about the "no identifier"
possibility.

Some specifics:
- When testing an attribute for a single name, the commit uses the
  `has_name` method.
- When testing an attribute for multiple names, the commit uses the new
  `has_any_name` method.
- When using `match` on an attribute, the match arms now have `Some` on
  them.

In the tests, we now avoid printing empty identifiers by not printing
the identifier in the `error:` line at all, instead letting the carets
point out the problem.
2025-04-17 09:50:52 +10:00
Chris Denton 1d757833c6 Remove safe remove 2025-04-14 18:47:15 +00:00
Nicholas Nethercote 1830245a22 Remove recursion_limit increases.
These are no longer needed now that `Nonterminal` is gone.
2025-04-02 16:25:27 +11:00
bors 4510e86a41 Auto merge of #138629 - Zoxc:graph-anon-hashmap, r=oli-obk
Only use the new node hashmap for anonymous nodes

This is a rebase of https://github.com/rust-lang/rust/pull/112469.

cc `@cjgillot`
2025-03-24 15:02:09 +00:00
bors 48b36c9d59 Auto merge of #128320 - saethlin:link-me-maybe, r=compiler-errors
Avoid no-op unlink+link dances in incr comp

Incremental compilation scales quite poorly with the number of CGUs. This PR improves one reason for that.

The incr comp process hard-links all the files from an old session into a new one, then it runs the backend, which may just hard-link the new session files into the output directory. Then codegen hard-links all the output files back to the new session directory.

This PR (perhaps unimaginatively) fixes the silliness that ensues in the last step. The old `link_or_copy` implementation would be passed pairs of paths which are already the same inode, then it would blindly delete the destination and re-create the hard-link that it just deleted. This PR lets us skip both those operations. We don't skip the other two hard-links.

`cargo +stage1 b && touch crates/core/main.rs && strace -cfw -elink,linkat,unlink,unlinkat cargo +stage1 b` before and then after on `ripgrep-13.0.0`:
```
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 52.56    0.024950          25       978       485 unlink
 34.38    0.016318          22       727           linkat
 13.06    0.006200          24       249           unlinkat
------ ----------- ----------- --------- --------- ----------------
100.00    0.047467          24      1954       485 total
```
```
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 42.83    0.014521          57       252           unlink
 38.41    0.013021          26       486           linkat
 18.77    0.006362          25       249           unlinkat
------ ----------- ----------- --------- --------- ----------------
100.00    0.033904          34       987           total
```

This reduces the number of hard-links that are causing perf troubles, noted in https://github.com/rust-lang/rust/issues/64291 and https://github.com/rust-lang/rust/issues/137560
2025-03-21 21:03:49 +00:00
John Kåre Alsaker 68fd771bc1 Pass in dep kind names to the duplicate dep node check 2025-03-19 20:12:37 +01:00
Camille GILLOT 5a21f890e9 Only use the new node hashmap for anonymous nodes. 2025-03-19 20:12:37 +01:00
bors 8536f201ff Auto merge of #138416 - Manishearth:rollup-fejor9p, r=Manishearth
Rollup of 12 pull requests

Successful merges:

 - #134076 (Stabilize `std::io::ErrorKind::InvalidFilename`)
 - #137504 (Move methods from Map to TyCtxt, part 4.)
 - #138175 (Support rmeta inputs for --crate-type=bin --emit=obj)
 - #138259 (Disentangle `ForwardGenericParamBan` and `ConstParamTy` ribs)
 - #138280 (fix ICE in pretty-printing `global_asm!`)
 - #138318 (Rustdoc: remove a bunch of `@ts-expect-error` from main.js)
 - #138331 (Use `RUSTC_LINT_FLAGS` more)
 - #138357 (merge `TypeChecker` and `TypeVerifier`)
 - #138394 (remove unnecessary variant)
 - #138403 (Delegation: one more ICE fix for `MethodCall` generation)
 - #138407 (Delegation: reject C-variadics)
 - #138409 (Use sa_sigaction instead of sa_union.__su_sigaction for AIX)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-03-13 01:37:26 +00:00