Skip to content

Remove tls::with_related_context.#153316

Open
nnethercote wants to merge 1 commit intorust-lang:mainfrom
nnethercote:rm-with_related_context
Open

Remove tls::with_related_context.#153316
nnethercote wants to merge 1 commit intorust-lang:mainfrom
nnethercote:rm-with_related_context

Conversation

@nnethercote
Copy link
Contributor

This function gets the current ImplicitCtxt and checks that its tcx matches the passed-in tcx. It's an extra bit of sanity checking: when you already have a tcx, and you need access to the non-tcx parts of ImplicitCtxt, check that your tcx matches the one in ImplicitCtxt.

However, it's only used in two places: start_query and current_query_job. The non-checked alternatives (with_context, with_context_opt) are used in more places, including some where a tcx is available. And things would have to go catastrophically wrong for the check to fail -- e.g. if we somehow end up with multiple TyCtxts. In my opinion it's just an extra case to understand in tls.rs that adds little value.

This commit removes it. This avoids the need for tcx parameters in a couple of places. The commit also adjusts how start_query sets up its ImplicitCtxt to more closely match how similar functions do it, i.e. with ..icx.clone() for the unchanged fields.

r? @oli-obk

This function gets the current `ImplicitCtxt` and checks that its `tcx`
matches the passed-in `tcx`. It's an extra bit of sanity checking: when
you already have a `tcx`, and you need access to the non-`tcx` parts of
`ImplicitCtxt`, check that your `tcx` matches the one in `ImplicitCtxt`.

However, it's only used in two places: `start_query` and
`current_query_job`. The non-checked alternatives (`with_context`,
`with_context_opt`) are used in more places, including some where a
`tcx` is available. And things would have to go catastrophically wrong
for the check to fail -- e.g. if we somehow end up with multiple
`TyCtxt`s. In my opinion it's just an extra case to understand in
`tls.rs` that adds little value.

This commit removes it. This avoids the need for `tcx` parameters in a
couple of places. The commit also adjusts how `start_query` sets up its
`ImplicitCtxt` to more closely match how similar functions do it, i.e.
with `..icx.clone()` for the unchanged fields.
@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 2, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 2, 2026

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 2, 2026
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 2, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 3, 2026

☀️ Try build successful (CI)
Build commit: f3f58ae (f3f58ae2f52fbf2b8360b59e129d9e5dafc6a56b, parent: ec818fda361ca216eb186f5cf45131bd9c776bb4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f3f58ae): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.3%, -0.1%] 4
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.1%] 16
All ❌✅ (primary) -0.2% [-0.3%, -0.1%] 4

Max RSS (memory usage)

Results (primary -4.9%, secondary -3.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-4.9% [-8.0%, -1.8%] 2
Improvements ✅
(secondary)
-3.1% [-3.9%, -2.4%] 2
All ❌✅ (primary) -4.9% [-8.0%, -1.8%] 2

Cycles

Results (secondary 2.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 480.002s -> 480.332s (0.07%)
Artifact size: 394.97 MiB -> 394.94 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 3, 2026
@nnethercote
Copy link
Contributor Author

Very minor perf wins, doc only, presumably due to fewer assertions running.

let icx = ImplicitCtxt {
query: Some(job_id),
query_depth: icx.query_depth + if depth_limit { 1 } else { 0 },
..icx.clone()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion/nit: I'm pretty sure this can just be ..*icx, because all the fields are Copy anyway.

Comment on lines -69 to +70
/// new query job while it executes.
/// Executes a job by changing the `ImplicitCtxt` to point to the new query job while it executes.
#[inline(always)]
pub(crate) fn start_query<'tcx, R>(
tcx: TyCtxt<'tcx>,
token: QueryJobId,
pub(crate) fn start_query<R>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: I've been meaning to rename this function at some point, because I find start_query pretty unhelpful for indicating what it actually does and doesn't do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants